Working code!
This works properly. It creates a single Device with two entities, action and battery for each Flic button it knows of.
This commit is contained in:
1 parent
9be3e0a685
commit
64b650a224
1 file changed
+15
-15
@@ -1,11 +1,12 @@
|
||||
var server = "mqtt.home";
|
||||
var hatopic = "homeassistant";
|
||||
var flictopic = "homeassistant/flic";
|
||||
var flictopic = "flic";
|
||||
|
||||
var buttonManager = require("buttons");
|
||||
|
||||
var mqtt = require("./mqtt").create(server);
|
||||
|
||||
//This runs when a button is clicked.
|
||||
buttonManager.on("buttonSingleOrDoubleClickOrHold", function(obj) {
|
||||
var button = buttonManager.getButton(obj.bdaddr);
|
||||
var clickType = obj.isSingleClick ? "click" : obj.isDoubleClick ? "double_click" : "hold";
|
||||
@@ -14,18 +15,20 @@ buttonManager.on("buttonSingleOrDoubleClickOrHold", function(obj) {
|
||||
var btntopic = flictopic+"/"+sn;
|
||||
|
||||
mqtt.publish(btntopic + "/action", clickType);
|
||||
console.log("Sent "+clickType+" to "+btntopic+"/action");
|
||||
console.log(btntopic+"/action: \t"+clickType);
|
||||
mqtt.publish(btntopic + "/action", "ok");
|
||||
mqtt.publish(btntopic + "/battery", button.batteryStatus+"%");
|
||||
console.log("Sent "+button.batteryStatus+" to "+btntopic+"/battery");
|
||||
console.log(btntopic+"/battery: \t"+button.batteryStatus);
|
||||
});
|
||||
|
||||
//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(JSON.stringify(button));
|
||||
console.log("\nRAW device info: "+JSON.stringify(button, null, 4)+"\n");
|
||||
|
||||
var configtopic = hatopic+"/sensor/"+button.serialNumber;
|
||||
var buttontopic = flictopic+"/"+button.serialNumber;
|
||||
@@ -38,30 +41,27 @@ mqtt.on('connected', function(){
|
||||
model: "Button"
|
||||
};
|
||||
|
||||
//obj.hello = "goodbye";
|
||||
//delete obj.hello;
|
||||
|
||||
//Setup config and destination for button press
|
||||
obj.name = button.name+" Flic Button";
|
||||
obj.state_topic = buttontopic+"/action";
|
||||
//obj.unique_id = "Flic.Action."+button.serialNumber;
|
||||
obj.unique_id = "Flic_"+button.serialNumber+"_action";
|
||||
|
||||
payload = JSON.stringify(obj);
|
||||
console.log("Sending "+payload+" to "+configtopic+"/action/config");
|
||||
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.Battery."+button.serialNumber;
|
||||
obj.unique_id = "Flic_"+button.serialNumber+"_battery";
|
||||
obj.device_class = "battery";
|
||||
payload = JSON.stringify(obj);
|
||||
console.log("Sending "+payload+" to "+configtopic + "/battery/config");
|
||||
//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 } );
|
||||
}
|
||||
});
|
||||
|
||||
mqtt.connect();
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user