diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..d344ba6b06cb4611d3a27589d8f48b22c832b048 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +config.json diff --git a/README.md b/README.md index a5841bfa1aa1f437a84435f4ef96e7b15d68954e..ab8c5952d4cee393a56439b9ab9620b6a01b860e 100644 --- a/README.md +++ b/README.md @@ -13,4 +13,11 @@ A node.js module to control [Samsungs SmartThings' Smart Home Monitor](https://s npm install -## Configuration \ No newline at end of file +### Dash Button Preparation + +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 diff --git a/config-sample.json b/config-sample.json index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..7ba4adc2711ffacd0e15b8727c15b513782d2a5e 100644 --- a/config-sample.json +++ b/config-sample.json @@ -0,0 +1,11 @@ +{ + "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": "" } + } +} diff --git a/index.js b/index.js index dd4798df8f9d5d1d9402564ad410f10704225dac..4f3c5c58c4e585d2c29d5ccc65348174b6542311 100644 --- a/index.js +++ b/index.js @@ -4,11 +4,85 @@ * LIBRARIES */ -var async = require( 'async' ); -var mqtt = require( 'mqtt' ); +var dashButton = require( 'node-dash-button' ); +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 ); + } ); + } ); +} diff --git a/package.json b/package.json index 82310b284981698ab785c903b9eb9e2a7c16cdc4..b0fec88c1cfa512d60e14d8d7f3d11d99f680063 100644 --- a/package.json +++ b/package.json @@ -13,10 +13,15 @@ "amazon dash", "dash button" ], + "scripts": { + "start": "node index.js" + }, + "bin" : { + "dashshmfind": "sudo node node_modules/node-dash-button/bin/findbutton" + }, "dependencies": { - "async": "*", - "mqtt": "*", - "node-dash-button": "*" + "node-dash-button": "*", + "request" : "*" }, "repository": { "type": "git",