Skip to content
Snippets Groups Projects
Verified Commit 6f01405a authored by Erick Hitter's avatar Erick Hitter
Browse files

Refactor how alarm state is manipulated, to use the proper endpoints that...

Refactor how alarm state is manipulated, to use the proper endpoints that actually change SHM, not just its representation within HA.
parent 52ae0609
No related branches found
No related tags found
No related merge requests found
...@@ -21,25 +21,25 @@ Object.keys( config.buttons ).forEach( function( index ) { ...@@ -21,25 +21,25 @@ Object.keys( config.buttons ).forEach( function( index ) {
button.on( 'detected', buttonActivated ); button.on( 'detected', buttonActivated );
} ); } );
var homeAssistantUrl = config.home_assistant_proto + '://' + config.home_assistant_host + ':' + config.home_assistant_port + '/api/'; var homeAssistantUrl = config.home_assistant_proto + '://' + config.home_assistant_host + ':' + config.home_assistant_port + '/api/';
var homeAssistantAlarmApi = homeAssistantUrl + 'states/' + config.home_assistant_alarm_entity_id;
/** /**
* BUTTON FUNCTIONALITY * BUTTON FUNCTIONALITY
*/ */
/** /**
* * Make HTTP(s) requests to Home Assistant to change SHM's status
*/ */
function buttonActivated( mac ) { function buttonActivated( mac ) {
var req = { var req = {
url: homeAssistantAlarmApi, url: homeAssistantUrl + 'states/' + config.home_assistant_alarm_entity_id,
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
"x-ha-access": config.home_assistant_pass "x-ha-access": config.home_assistant_pass
} }
}; };
// First, check the current status so we can toggle
request( req, function( err, res, body ) { request( req, function( err, res, body ) {
// Handle error states // Handle error states
if ( err ) { if ( err ) {
...@@ -52,20 +52,24 @@ function buttonActivated( mac ) { ...@@ -52,20 +52,24 @@ function buttonActivated( mac ) {
return; return;
} }
// HA only deals with JSON
body = JSON.parse( body ); body = JSON.parse( body );
// Identify new state based on current state // Identify service URL based on current state
var stateUpdate = {}; var serviceBase = 'services/alarm_control_panel/';
if ( 'disarmed' === body.state ) { if ( 'disarmed' === body.state ) {
stateUpdate.state = 'armed_home'; req.url = homeAssistantUrl + serviceBase + 'alarm_arm_home';
} else if ( 'armed_home' === body.state ) {
req.url = homeAssistantUrl + serviceBase + 'alarm_disarm';
} else { } else {
stateUpdate.state = 'disarmed'; console.error( 'Invalid request based on current SHM status' );
return;
} }
// Build new request to update state // Build new request to update state
req.method = 'POST'; req.method = 'POST';
req.body = JSON.stringify( stateUpdate ); req.body = JSON.stringify( { "entity_id": config.home_assistant_alarm_entity_id } );
request( req, function( err, res, body ) { request( req, function( err, res, body ) {
// Handle error states // Handle error states
...@@ -79,10 +83,9 @@ function buttonActivated( mac ) { ...@@ -79,10 +83,9 @@ function buttonActivated( mac ) {
return; return;
} }
body = JSON.parse( body ); // Nothing more to do
// services endpoints don't return anything useful
// Output new state to console, just for fun return true;
console.log( body.state );
} ); } );
} ); } );
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment