diff --git a/main.js b/main.js index e49232b..f2148fa 100644 --- a/main.js +++ b/main.js @@ -1,22 +1,102 @@ -//GITHUB URL: https://github.com/id628/flic2hass - -//CONFIGURATION - UPDATE AS NEEDED var server = "mqtt.home"; var hatopic = "homeassistant"; var flictopic = "flic"; -//CODE STARTS HERE. OPEN SOURCE LICENSE, USE OR CHANGE AS NEEDED - var buttonManager = require("buttons"); var mqtt = require("./mqtt").create(server); -//This runs when a button is clicked. -buttonManager.on("buttonSingleOrDoubleClickOrHold", function(obj) { +var myButtons = {}; //Dictionary of Button objects + +//This runs when a button is added via Flic's interface +buttonManager.on("buttonAdded", function(obj){ var button = buttonManager.getButton(obj.bdaddr); + + console.log("\nNew button Added! "+button.serialNumber+" "+button.name); + console.log("\nRAW obj info: " + JSON.stringify(obj, null, 4) + "\n"); + console.log("\nRAW button info: " + JSON.stringify(button, null, 4) + "\n"); + + if (button.serialNumber == null || button.name == null) { + return; + } + + if (button.serialNumber) { + register_button(button); + } +}); + + +//This runs when a button is added via Flic's interface +buttonManager.on("buttonConnected", function(obj){ + var button = buttonManager.getButton(obj.bdaddr); + + console.log("\nNew button Connected! "+button.serialNumber+" "+button.name); + console.log("\nRAW obj info: " + JSON.stringify(obj, null, 4) + "\n"); + console.log("\nRAW button info: " + JSON.stringify(button, null, 4) + "\n"); + + + if (button.serialNumber == null || button.name == null) { + return; + } + + if (button.serialNumber) { + register_button(button); + } +}); + +//This runs when a button is added via Flic's interface +buttonManager.on("buttonReady", function(obj){ + var button = buttonManager.getButton(obj.bdaddr); + + console.log("\nNew button Ready! "+button.serialNumber+" "+button.name); + console.log("\nRAW obj info: " + JSON.stringify(obj, null, 4) + "\n"); + console.log("\nRAW button info: " + JSON.stringify(button, null, 4) + "\n"); + + if (button.serialNumber == null || button.name == null) { + return; + } + + if (button.serialNumber) { + register_button(button); + } + +}); + +//This runs when a button is deleted via Flic's interface +buttonManager.on("buttonDeleted", function(obj){ + //Well crap. If it's deleted, it doesn't have an object anymore. + //So that means I have to save it by address, not ID so I can delete it from MQTT later + console.log("\nDeleting button "+obj.bdaddr); + console.log("\nRAW obj info: " + JSON.stringify(obj, null, 4) + "\n"); + + var configtopic = hatopic+"/sensor/"+myButtons[obj.bdaddr].serialNumber; + mqtt.publish(configtopic+"/action/config", null, {retain: false } ); + if (myButtons[obj.bdaddr]) { + delete myButtons[obj.bdaddr]; + //myButtons.delete[obj.bdaddr]; + } +}); + +buttonManager.on("buttonSingleOrDoubleClickOrHold", function(obj) { + //This runs when a button is clicked. + + var button = buttonManager.getButton(obj.bdaddr); + console.log("\nRAW obj info: " + JSON.stringify(obj, null, 4) + "\n"); + console.log("\nRAW button info: " + JSON.stringify(button, null, 4) + "\n"); + 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); + } + + if (myButtons[button.bdaddr].name!=button.name) { + console.log("*Name changed from "+myButtons[button.bdaddr].name+" to "+button.name); + register_button(button); + } + var btntopic = flictopic+"/"+sn; mqtt.publish(btntopic + "/action", clickType); @@ -26,47 +106,80 @@ buttonManager.on("buttonSingleOrDoubleClickOrHold", function(obj) { console.log(btntopic+"/battery: \t"+button.batteryStatus); }); +function register_button(button) { + //Register one button + //This sets up the MQTT Discovery topics to publish the Flic button + + //NOTE: When a button is added via the Flic interface, it gets named + //a bit late. So the API returns null for the name. + //What's worse, it continues to return null until you restart the + //Javascript app running in the Hub. I have no idea how to get the + //new name without crashing the app and relying on the + //"Restart after crash" flag - and I'm not willing to do that. + //TL;DR - new buttons are named "null" until the Hub is rebooted. + + console.log("\nRAW device info: " + JSON.stringify(button, null, 4) + "\n"); + + if (button.serialNumber == null) { + console.log("**Error registering button, still no serialNumber") + return; + } + + var configtopic = hatopic + "/sensor/" + button.serialNumber; + var buttontopic = flictopic + "/" + button.serialNumber; + var obj = {}; + + obj.device = { + name: button.name, + identifiers: [button.serialNumber], + manufacturer: "Flic", + model: "Button" + }; + + //Setup config and destination for button press + obj.name = button.name + " Flic Button"; + obj.state_topic = buttontopic + "/action"; + obj.unique_id = "Flic_" + button.serialNumber + "_action"; + + payload = JSON.stringify(obj, null, 4); + console.log(configtopic + "/action/config:\t" + payload); + mqtt.publish(configtopic + "/action/config", payload, { + retain: true + }); + + //Setup config and destination for battery level report + obj.name = button.name + " Flic Button Battery Level"; + obj.state_topic = buttontopic + "/battery"; + obj.unique_id = "Flic_" + button.serialNumber + "_battery"; + obj.device_class = "battery"; + //obj.unit_of_measurement = "%"; //It doesn't seem to actually like this. + + payload = JSON.stringify(obj, null, 4); + console.log(configtopic + "/battery/config:\t" + payload); + mqtt.publish(configtopic + "/battery/config", payload, { + retain: true + }); + myButtons[button.bdaddr] = { + name: button.name, + serialNumber: button.serialNumber + }; + + console.log("\nmyButtons: " + JSON.stringify(myButtons, null, 4) + "\n"); + +} + +//This sets up the MQTT Discovery topics to publish all the Flic buttons +function register_allbuttons(){ + var buttons = buttonManager.getButtons(); + for (var i = 0; i < buttons.length; i++) { + register_button(buttons[i]); + } +} + //This runs on startup, it performs the actual "discovery" announcement for //HomeAssistant to add the device to its inventory mqtt.on('connected', function(){ - //Register known buttons - might need to do this more regularly, not just on startup! - var buttons = buttonManager.getButtons(); - for (var i = 0; i < buttons.length; i++) { - var button = buttons[i]; - console.log("\nRAW device info: "+JSON.stringify(button, null, 4)+"\n"); - - var configtopic = hatopic+"/sensor/"+button.serialNumber; - var buttontopic = flictopic+"/"+button.serialNumber; - var obj = {}; - - obj.device = { - name: button.name, - identifiers: [button.serialNumber], - manufacturer: "Flic", - model: "Button" - }; - - //Setup config and destination for button press - obj.name = button.name+" Flic Button"; - obj.state_topic = buttontopic+"/action"; - obj.unique_id = "Flic_"+button.serialNumber+"_action"; - - payload = JSON.stringify(obj, null, 4); - console.log(configtopic+"/action/config:\t"+payload); - mqtt.publish(configtopic+"/action/config", payload, {retain: true } ); - - //Setup config and destination for battery level report - obj.name = button.name+" Flic Button Battery Level"; - obj.state_topic = buttontopic+"/battery"; - obj.unique_id = "Flic_"+button.serialNumber+"_battery"; - obj.device_class = "battery"; - //obj.unit_of_measurement = "%"; //It doesn't seem to actually like this. - - payload = JSON.stringify(obj, null, 4); - console.log(configtopic + "/battery/config:\t"+payload); - mqtt.publish(configtopic + "/battery/config", payload, {retain: true } ); - } + register_allbuttons(); }); mqtt.connect(); -