If you use MQTT and have Auto discovery enabled, you can run this on the Flic Hub to have it automatically created flic buttons in Home assistant.
var server = ""
var options = {
client_id : "flic_hub",
keep_alive: 60,
port: 1883,
clean_session: true,
username: "",
password: "",
protocol_name: "MQTT",
protocol_level: 4
}
var mqtt = require("./mqtt.js").create(server, options)
var buttonManager = require("buttons")
var buttons = buttonManager.getButtons()
var actions = {
button_short_press: "click",
button_double_press: "double_click",
button_long_press: "hold"
}
var buttonsAddedToHa = false
buttonManager.on("buttonSingleOrDoubleClickOrHold", function(obj) {
console.log("Event called buttonSingleOrDoubleClickOrHold")
var button = buttonManager.getButton(obj.bdaddr)
var clickType = obj.isSingleClick ? actions.button_short_press : obj.isDoubleClick ? actions.button_double_press : actions.button_long_press
mqtt.publish("flic/"+ button.uuid +"/action", clickType)
})
buttonManager.on("buttonAdded", function(obj) {
console.log("Event called buttonAdded")
configureButtons()
})
buttonManager.on("buttonUpdated", function(obj) {
console.log("Event called buttonUpdated")
refreshButtonhState(obj)
})
buttonManager.on("buttonConnected", function(obj) {
console.log("Event called buttonConnected")
refreshButtonhState(buttonManager.getButton(obj.bdaddr))
})
buttonManager.on("buttonReady", function(obj) {
console.log("Event called buttonReady")
refreshButtonhState(buttonManager.getButton(obj.bdaddr))
})
buttonManager.on("buttonDisconnected", function(obj) {
console.log("Event called buttonDisconnected")
refreshButtonhState(buttonManager.getButton(obj.bdaddr))
})
mqtt.on('connected', function(){
console.log("MQTT Connected")
if(buttonsAddedToHa === false){
configureButtons()
}
})
mqtt.on('publish', function(pub){
console.log("Received a message with: ")
console.log("topic: "+pub.topic)
console.log("message: "+pub.message)
});
mqtt.on('disconnected', function() {
console.log("MQTT Disconnected")
mqtt.connect()
});
mqtt.on('error', function(err) {
console.log("MQTT Error: " + err)
setTimeout(function (){
mqtt.connect();
}, 5000);
});
function configureButtons(){
for (var i = 0; i < buttons.length; i++) {
var button = buttons[i];
var deviceConfig = {
identifiers: [
button.uuid
],
name: (button.name === null)? button.serialNumber : button.name.toString(),
sw_version: button.firmwareVersion.toString(),
hw_version: button.flicVersion.toString(),
model: "Flic " + button.flicVersion.toString(),
manufacturer: "Flic"
}
var deviceAutomationConfig = {
automation_type: "trigger",
type: "",
subtype: "button_1",
payload: "",
topic: "",
unique_id: "",
device: deviceConfig
}
console.log("Creating HA device: "+ deviceConfig.name)
Object.keys(actions).forEach(function(ha) {
var flic = actions[ha]
deviceAutomationConfig.type = ha
deviceAutomationConfig.payload = flic
deviceAutomationConfig.topic = "flic/"+ button.uuid +"/action"
deviceAutomationConfig.unique_id = button.uuid +"_"+ flic
mqtt.publish("homeassistant/device_automation/flic/"+ deviceAutomationConfig.device.name +"_"+ flic +"/config", JSON.stringify(deviceAutomationConfig), {retain: true})
})
var batteryConfig = {
name: deviceConfig.name + "_battery_charge_level",
device_class: "battery",
state_topic: "flic/"+ button.uuid +"/battery_charge_level/state",
availability: [
{
topic: "flic/"+ button.uuid +"/battery_charge_level/status"
},
{
topic: "flic/"+ button.uuid +"/status"
}
],
availability_mode: "all",
unit_of_measurement: "%",
value_template: "{{ value | int }}",
unique_id: button.uuid +"_battery_charge_level",
device: deviceConfig
}
mqtt.publish("homeassistant/sensor/"+ button.uuid +"/battery_charge_level/config", JSON.stringify(batteryConfig), {retain: true})
var connectedConfig = {
name: deviceConfig.name + "_connected",
device_class: "connectivity",
state_topic: "flic/"+ button.uuid +"/connected/state",
availability_topic: "flic/"+ button.uuid +"/status",
value_template: "{{ value }}",
unique_id: button.uuid +"_connected",
device: deviceConfig
}
mqtt.publish("homeassistant/binary_sensor/"+ button.uuid +"/connected/config", JSON.stringify(connectedConfig), {retain: true})
var readyConfig = {
name: deviceConfig.name + "_ready",
state_topic: "flic/"+ button.uuid +"/ready/state",
availability_topic: "flic/"+ button.uuid +"/status",
value_template: "{{ value }}",
unique_id: button.uuid +"_ready",
device: deviceConfig
}
mqtt.publish("homeassistant/binary_sensor/"+ button.uuid +"/ready/config", JSON.stringify(readyConfig), {retain: true})
var activeDisconnectConfig = {
name: deviceConfig.name + "_active_disconnect",
state_topic: "flic/"+ button.uuid +"/active_disconnect/state",
availability_topic: "flic/"+ button.uuid +"/status",
value_template: "{{ value }}",
unique_id: button.uuid +"_active_disconnect",
device: deviceConfig
}
mqtt.publish("homeassistant/binary_sensor/"+ button.uuid +"/active_disconnect/config", JSON.stringify(activeDisconnectConfig), {retain: true})
setTimeout(function(){ refreshButtonhState(button) }, 5 * 1000)
}
buttonsAddedToHa = true
}
function refreshButtonhState(button){
console.log("Refresh Button State: "+ JSON.stringify(button))
var availability = (button.ready)? "online" : "offline"
mqtt.publish("flic/"+ button.uuid +"/status", availability)
if(button.batteryStatus == null){
mqtt.publish("flic/"+ button.uuid +"/battery_charge_level/status", "offline")
} else {
mqtt.publish("flic/"+ button.uuid +"/battery_charge_level/status", "online")
mqtt.publish("flic/"+ button.uuid +"/battery_charge_level/state", JSON.stringify(button.batteryStatus))
}
mqtt.publish("flic/"+ button.uuid +"/connected/state", (button.connected)?"ON":"OFF")
mqtt.publish("flic/"+ button.uuid +"/ready/state", (button.ready)?"ON":"OFF")
mqtt.publish("flic/"+ button.uuid +"/active_disconnect/state", (button.activeDisconnect)?"ON":"OFF")
}
mqtt.connect()
Just add your MQTT server IP, username and password. Flic buttons should be automatically added with battery and other sensors and of course triggers for the button events.