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

Switch to Home Assistant's API

Currently uses the wrong endpoint for changing state, so only HA is updated.
parent b99245f5
Branches
No related tags found
No related merge requests found
config.json
...@@ -13,4 +13,11 @@ A node.js module to control [Samsungs SmartThings' Smart Home Monitor](https://s ...@@ -13,4 +13,11 @@ A node.js module to control [Samsungs SmartThings' Smart Home Monitor](https://s
npm install npm install
## Configuration ### Dash Button Preparation
\ No newline at end of file
Follow Amazon's instructions to configure your button to send messages when you push them but not actually order anything. When you get a Dash button, Amazon gives you a list of setup instructions to get going. Just follow this list of instructions, but don’t complete the final step of selecting a product to order. **Do not select a product, just exit the app**.
## Configuration
1. Discover the Dash Buttons using `node node_modules/node-dash-button/bin/findbutton`.
1. Rename `config-sample.json` to `config.json`.
\ No newline at end of file
{
"arp_interface": null,
"home_assistant_host": "localhost",
"home_assistant_port": 8123,
"home_assistant_proto": "http",
"home_assistant_pass": null,
"home_assistant_alarm_entity_id": "alarm_control_panel.shm",
"buttons": {
0: { "mac": "", "label": "" }
}
}
...@@ -4,11 +4,85 @@ ...@@ -4,11 +4,85 @@
* LIBRARIES * LIBRARIES
*/ */
var async = require( 'async' ); var dashButton = require( 'node-dash-button' );
var mqtt = require( 'mqtt' ); var request = require( 'request' );
/** /**
* CONFIGURATION * CONFIGURATION & EVENT BINDING
*/ */
var config = require( './config.json' || './config-sample.json' ); var config = require( './config.json' );
Object.keys( config.buttons ).forEach( function( index ) {
var buttonConfig = config.buttons[index];
var button = dashButton( buttonConfig.mac, config.arp_interface );
button.on( 'detected', buttonActivated );
} );
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
*/
/**
*
*/
function buttonActivated( mac ) {
var req = {
url: homeAssistantAlarmApi,
headers: {
"Content-Type": "application/json",
"x-ha-access": config.home_assistant_pass
}
};
request( req, function( err, res, body ) {
// Handle error states
if ( err ) {
console.error( err );
return;
}
if ( 200 !== res.statusCode ) {
console.error( res.statusCode );
return;
}
body = JSON.parse( body );
// Identify new state based on current state
var stateUpdate = {};
if ( 'disarmed' === body.state ) {
stateUpdate.state = 'armed_home';
} else {
stateUpdate.state = 'disarmed';
}
// Build new request to update state
req.method = 'POST';
req.body = JSON.stringify( stateUpdate );
request( req, function( err, res, body ) {
// Handle error states
if ( err ) {
console.error( err );
return;
}
if ( 200 !== res.statusCode ) {
console.error( res.statusCode );
return;
}
body = JSON.parse( body );
// Output new state to console, just for fun
console.log( body.state );
} );
} );
}
...@@ -13,10 +13,15 @@ ...@@ -13,10 +13,15 @@
"amazon dash", "amazon dash",
"dash button" "dash button"
], ],
"scripts": {
"start": "node index.js"
},
"bin" : {
"dashshmfind": "sudo node node_modules/node-dash-button/bin/findbutton"
},
"dependencies": { "dependencies": {
"async": "*", "node-dash-button": "*",
"mqtt": "*", "request" : "*"
"node-dash-button": "*"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment