From 403175c911c7a03e7faace8d80e160a44773a16a Mon Sep 17 00:00:00 2001 From: Alexander Cato <52341592+alexander-cato@users.noreply.github.com> Date: Sat, 5 Aug 2023 22:22:25 -0400 Subject: [PATCH 1/4] Added current state sensor --- main.js | 40 ++++++++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/main.js b/main.js index 9adf1ba..08207f4 100644 --- a/main.js +++ b/main.js @@ -1,11 +1,9 @@ var server = "mqtt.home"; var hatopic = "homeassistant"; var flictopic = "flic"; - -var buttonManager = require("buttons"); - var mqtt = require("./mqtt").create(server); +var buttonManager = require("buttons"); var myButtons = {}; //Dictionary of Button objects //This runs when a button is added via Flic's interface @@ -86,7 +84,7 @@ buttonManager.on("buttonSingleOrDoubleClickOrHold", function(obj) { var clickType = obj.isSingleClick ? "click" : obj.isDoubleClick ? "double_click" : "hold"; var sn = button.serialNumber; - + if (!myButtons[button.bdaddr]) { console.log("**** Found an unregistered button. It must be new! ***") register_button(button); @@ -106,6 +104,25 @@ buttonManager.on("buttonSingleOrDoubleClickOrHold", function(obj) { console.log(btntopic+"/battery: \t"+button.batteryStatus); }); +buttonManager.on("buttonUp", function(obj) { + var button = buttonManager.getButton(obj.bdaddr); + console.log("\nButton Released! " + button.serialNumber + " " + button.name); + publishButtonState(button, "released"); +}); + +buttonManager.on("buttonDown", function(obj) { + var button = buttonManager.getButton(obj.bdaddr); + console.log("\nButton Pressed! " + button.serialNumber + " " + button.name); + publishButtonState(button, "pressed"); +}); + +function publishButtonState(button, state) { + var sn = button.serialNumber; + var statetopic = flictopic + "/" + sn + "/state"; + mqtt.publish(statetopic, state); + console.log(statetopic + ": \t" + state); +} + function register_button(button) { //Register one button //This sets up the MQTT Discovery topics to publish the Flic button @@ -137,7 +154,7 @@ function register_button(button) { }; //Setup config and destination for button press - obj.name = button.name + " Flic Button"; + obj.name = button.name + " Button Action"; obj.state_topic = buttontopic + "/action"; obj.unique_id = "Flic_" + button.serialNumber + "_action"; @@ -147,8 +164,19 @@ function register_button(button) { retain: true }); + // Setup config and destination for button state + obj.name = button.name + " Button State"; + obj.state_topic = buttontopic + "/state"; + obj.unique_id = "Flic_" + button.serialNumber + "_state"; + + payload = JSON.stringify(obj, null, 4); + console.log(configtopic + "/state/config:\t" + payload); + mqtt.publish(configtopic + "/state/config", payload, { + retain: true + }); + //Setup config and destination for battery level report - obj.name = button.name + " Flic Button Battery Level"; + obj.name = button.name + " Battery Level"; obj.state_topic = buttontopic + "/battery"; obj.unique_id = "Flic_" + button.serialNumber + "_battery"; obj.device_class = "battery"; From 0b5e4c773cf65eb7ddb68802c42c1766949a740b Mon Sep 17 00:00:00 2001 From: Alexander Cato <52341592+alexander-cato@users.noreply.github.com> Date: Sat, 5 Aug 2023 22:24:45 -0400 Subject: [PATCH 2/4] Improve variable defaults --- main.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/main.js b/main.js index 08207f4..6f98628 100644 --- a/main.js +++ b/main.js @@ -1,7 +1,9 @@ var server = "mqtt.home"; var hatopic = "homeassistant"; var flictopic = "flic"; -var mqtt = require("./mqtt").create(server); +var username = ""; +var password = ""; +var mqtt = require("./mqtt").create(server,{'username':username,'password':password}); var buttonManager = require("buttons"); var myButtons = {}; //Dictionary of Button objects From 9dbf0ab19695a64afe2d83157d11c16618399b91 Mon Sep 17 00:00:00 2001 From: Alexander Cato <52341592+alexander-cato@users.noreply.github.com> Date: Sat, 5 Aug 2023 22:25:19 -0400 Subject: [PATCH 3/4] Move battery to diagnostic category --- main.js | 1 + 1 file changed, 1 insertion(+) diff --git a/main.js b/main.js index 6f98628..33c6823 100644 --- a/main.js +++ b/main.js @@ -182,6 +182,7 @@ function register_button(button) { obj.state_topic = buttontopic + "/battery"; obj.unique_id = "Flic_" + button.serialNumber + "_battery"; obj.device_class = "battery"; + obj.entity_category = "diagnostic"; //obj.unit_of_measurement = "%"; //It doesn't seem to actually like this. payload = JSON.stringify(obj, null, 4); From c3d503cecc0f45fa85766a5427e0487b0cd91b0d Mon Sep 17 00:00:00 2001 From: Alexander Cato <52341592+alexander-cato@users.noreply.github.com> Date: Sat, 5 Aug 2023 22:54:06 -0400 Subject: [PATCH 4/4] Update and improve documentation --- README.md | 76 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 50 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 85743cf..337b188 100644 --- a/README.md +++ b/README.md @@ -1,31 +1,55 @@ # flic2hass -A Flic SDK utility to publish all Flic buttons to Home Assistant via MQTT +## A Flic SDK utility to publish all of your Flic buttons to Home Assistant via MQTT -Utilizing https://hubsdk.flic.io/static/tutorial/ +Requirements: +* A Flic Hub +* A functional MQTT server +## Basic Steps: -Your Flic Hub online IDE: https://hubsdk.flic.io/ +**1. Connect to Flic Hub IDE:** -Follow the tutorial steps above, substituting in main.js -- DON'T FORGET TO UPDATE IT TO USE YOUR OWN MQTT SERVER! +* Follow along with the beginning of [these instructions](https://hubsdk.flic.io/static/tutorial/) to enable SDK access. +* Go to: and login, your hub should be discovered automatically. -Requirements: -* Flic Hub -* Working MQTT system -* Enough know-how to get it to work - you will need to also follow the tutorial a bit (linked above) - -Basic steps: -* Connect to your Flic Hub IDE -* Create a new module, name it MQTT -* Paste in the main.js and mqtt.js files -* Update the main.js variables server and hatopic. -* IF you use a username/password on your MQTT, set that up: -> in main.js ->> var username = 'flichub'; ->> var password = 'xxxxx'; -> and change this line.... ->> var mqtt = require("./mqtt").create(server,{'username':username,'password':password}); - -* Start the "Module" in the IDE and watch the Console output - it'se extremely verbose right now - -* IF it started right, set the "restart after crash" checkbox just in case - -* IF it didn't start, try powercycling your Flic Hub and reconnect. Verify the Module saved properly and is running. +**2. Create `MQTT` module:** + +* One in the Web IDE, click "Create Module". +* Give the new module a name. "`MQTT`" is a good option but anything will work. + +**3. Insert `main.js` and `mqtt.js`:** + +* Copy content from `main.js` in this repo to main.js in the flic IDE. +* Right click the folder in the left pane and select "New File". +* Name the file `mqtt.js` (IT MUST BE NAMED THIS). +* Copy content from `mqtt.js` in this repo to mqtt.js in the flic IDE. + +**4. Update variables in `main.js`:** + +* Modify `server`, `hatopic`, `authentication` with your details. + + *If your MQTT server does not require authentication:* + +* Delete: + + ```javascript + var username = 'flichub'; + var password = 'xxxxx'; + ``` + +* Replace: + + ```javascript + var mqtt = require("./mqtt").create(server,{'username':username,'password':password}); + ``` + + With: + + ```javascript + var mqtt = require("./mqtt").create(server); + ``` + +1. Start the module in the IDE by clicking the green play button, and watch the Console output (it's extremely verbose right now) + + *If the module didn't start correctly, try powercycling your Flic Hub and reconnect. Verify the Module saved properly and is running.* + +2. Once the module has started and you have verified it is working as expected, turn on the "restart after crash" checkbox to ensure the module is always running after any unexpected crash or hub power cycle.