From 52ae060956ad67dc50a80f7d05f3024a31aeece7 Mon Sep 17 00:00:00 2001
From: Erick Hitter <services@ethitter.com>
Date: Sat, 10 Sep 2016 14:55:47 -0700
Subject: [PATCH] Switch to Home Assistant's API

Currently uses the wrong endpoint for changing state, so only HA is updated.
---
 .gitignore         |  1 +
 README.md          |  9 ++++-
 config-sample.json | 11 +++++++
 index.js           | 82 +++++++++++++++++++++++++++++++++++++++++++---
 package.json       | 11 +++++--
 5 files changed, 106 insertions(+), 8 deletions(-)
 create mode 100644 .gitignore

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..d344ba6
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+config.json
diff --git a/README.md b/README.md
index a5841bf..ab8c595 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 e69de29..7ba4adc 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 dd4798d..4f3c5c5 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 82310b2..b0fec88 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",
-- 
GitLab