-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.js
233 lines (186 loc) · 7.5 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
var server = "domain_or_ip_address";
var server_options = {
//"username": "",
//"password": "",
"port": 1883,
}
var hatopic = "homeassistant";
var flictopic = "flic";
var mqtt = require("./mqtt").create(server, server_options);
var buttonManager = require("buttons");
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);
console.log(btntopic+"/action: \t"+clickType);
mqtt.publish(btntopic + "/action", "ok");
mqtt.publish(btntopic + "/battery", button.batteryStatus+""); //NOW it seems to just want text, no decoration!
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
//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 Action";
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 button state
obj.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 = "Battery Level";
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);
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(){
console.log("\n'Connected' event\n");
register_allbuttons();
});
// Added by heroash88 to reconnect if MQTT server goes down
mqtt.on('disconnected', function() {
console.log("\n'Disconnected' event\n");
mqtt.connect();
});
mqtt.on('error', function () {
console.log("\n'Error' event\n");
setTimeout(function () {
throw new Error("Crashed")
}, 1000);
});
mqtt.connect();