forked from senorshaun/homebridge-radiora2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
377 lines (308 loc) · 19.1 KB
/
index.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
'use strict';
let RadioRa2 = require('./lib/radiora2');
const EventEmitter = require('events').EventEmitter;
const util = require('util');
let FanAccessory = require('./lib/accessories/fan');
let LightbulbAccessory = require('./lib/accessories/lightbulb');
let OccupancySensorAccessory = require('./lib/accessories/occupancysensor');
let KeypadButtonStatelessAccessory = require('./lib/accessories/statelessswitch');
let KeypadButtonAccessory = require('./lib/accessories/keypadbutton');
let VisorControlReceiverAccessory = require('./lib/accessories/visorcontrolreceiver')
let ThermostatAccessory = require('./lib/accessories/hvaccontroller');
let Homebridge, Accessory, PlatformAccessory, Characteristic, Service, UUIDGen;
module.exports = function (homebridge) {
Homebridge = homebridge;
Accessory = homebridge.hap.Accessory;
PlatformAccessory = homebridge.platformAccessory;
Characteristic = homebridge.hap.Characteristic;
Service = homebridge.hap.Service;
UUIDGen = homebridge.hap.uuid;
homebridge.registerPlatform('homebridge-radiora2', 'RadioRA2', RadioRA2Platform, true);
};
function addDefaultValues(deviceConfig, deviceType) {
deviceConfig.name = (deviceConfig.name || deviceType + " " + deviceConfig.id ).toString();
deviceConfig.model = (deviceConfig.model || "Homebridge-RadioRa2-" + deviceType).toString();
deviceConfig.serial = (deviceConfig.serial || deviceType + deviceConfig.id ).toString();
return deviceConfig
}
class RadioRA2Platform {
constructor(log, config, api) {
if ((!config) || (!config.repeater) || (!config.username) || (!config.password)) {
log.warn("Ignoring Lutron RadioRa2 Platform setup because it is not configured");
this.disabled = true;
return;
}
this.config = config;
this.api = api;
this.accessories = {};
this.log = log;
this.setupListeners();
}
addFanAccessory(accessoryUUID, fanConfig) {
let accessoryName = fanConfig.name;
let accessory = new PlatformAccessory(accessoryName, accessoryUUID);
accessory.getService(Service.AccessoryInformation)
.setCharacteristic(Characteristic.Manufacturer, "Lutron")
.setCharacteristic(Characteristic.SerialNumber, (fanConfig.serial|| "Fan" + fanConfig.id).toString());
let fanService = accessory.addService(Service.Fan, accessoryName);
this.accessories[accessory.UUID] = new FanAccessory(this.log, fanConfig, accessory, this.radiora2, Homebridge);
this.api.registerPlatformAccessories("homebridge-radiora2", "RadioRA2", [accessory]);
}
addLightAccessory(accessoryUUID, lightConfig) {
let accessoryName = lightConfig.name;
let accessory = new PlatformAccessory(accessoryName, accessoryUUID);
accessory.getService(Service.AccessoryInformation)
.setCharacteristic(Characteristic.Manufacturer, "Lutron")
.setCharacteristic(Characteristic.SerialNumber, (lightConfig.serial || "Light" + lightConfig.id).toString());
let lightBulbService = accessory.addService(Service.Lightbulb, accessoryName);
if (lightConfig.adjustable) {
lightBulbService.addCharacteristic(Characteristic.Brightness);
}
this.accessories[accessory.UUID] = new LightbulbAccessory(this.log, lightConfig, accessory, this.radiora2, Homebridge);
this.api.registerPlatformAccessories("homebridge-radiora2", "RadioRA2", [accessory]);
}
addOccupancyAccessory(accessoryUUID, occupancySensorConfig) {
let accessoryName = occupancySensorConfig.name;
let accessory = new PlatformAccessory(accessoryName, accessoryUUID);
accessory.getService(Service.AccessoryInformation)
.setCharacteristic(Characteristic.Manufacturer, "Lutron")
.setCharacteristic(Characteristic.SerialNumber, (occupancySensorConfig.serial || "Occ" + occupancySensorConfig.id).toString());
let occupancyService = accessory.addService(Service.OccupancySensor, accessoryName)
occupancyService.addOptionalCharacteristic(Characteristic.StatusActive);
this.accessories[accessory.UUID] = new OccupancySensorAccessory(this.log, occupancySensorConfig, accessory, this.radiora2, Homebridge);
this.api.registerPlatformAccessories("homebridge-radiora2", "RadioRA2", [accessory]);
}
addKeypadButtonStatelessAccessory(accessoryUUID, keypadConfig) {
let accessoryName = keypadConfig.name;
let accessory = new PlatformAccessory(accessoryName, accessoryUUID);
accessory.getService(Service.AccessoryInformation)
.setCharacteristic(Characteristic.Manufacturer, "Lutron")
.setCharacteristic(Characteristic.SerialNumber, (keypadConfig.serial || "Keypad" + keypadConfig.id).toString());
let buttonArray = keypadConfig.buttons || [];
buttonArray.forEach(function(buttonConfig) {
let buttonService = accessory.addService(Service.StatelessProgrammableSwitch, "Button " + buttonConfig.id, "Button " + buttonConfig.id);
let switchEventCharacteristic = buttonService.getCharacteristic(Characteristic.ProgrammableSwitchEvent);
switchEventCharacteristic.setProps({
format: Characteristic.Formats.UINT8,
maxValue: 2,
minValue: 0,
validValues: [0],
perms: [Characteristic.Perms.READ, Characteristic.Perms.NOTIFY]
});
buttonService.getCharacteristic(Characteristic.ServiceLabelIndex).updateValue(buttonConfig.id);
buttonService.getCharacteristic(Characteristic.Name).updateValue(buttonConfig.name);
}.bind(this));
this.accessories[accessory.UUID] = new KeypadButtonStatelessAccessory(this.log, keypadConfig, accessory, this.radiora2, Homebridge);
this.api.registerPlatformAccessories("homebridge-radiora2", "RadioRA2", [accessory]);
}
addKeypadButtonAccessory(accessoryUUID, keypadConfig) {
let accessoryName = keypadConfig.name;
let accessory = new PlatformAccessory(accessoryName, accessoryUUID);
accessory.getService(Service.AccessoryInformation)
.setCharacteristic(Characteristic.Manufacturer, "Lutron")
.setCharacteristic(Characteristic.SerialNumber, (keypadConfig.serial || "Keypad" + keypadConfig.id).toString());
let buttonArray = keypadConfig.buttons || [];
buttonArray.forEach(function(buttonConfig) {
let buttonService = accessory.addService(Service.Switch, buttonConfig.name, buttonConfig.name);
}.bind(this));
this.accessories[accessory.UUID] = new KeypadButtonAccessory(this.log, keypadConfig, accessory, this.radiora2, Homebridge);
this.api.registerPlatformAccessories("homebridge-radiora2", "RadioRA2", [accessory]);
}
addVisorControlReceiverAccessory(accessoryUUID, visorControlConfig) {
let accessoryName = visorControlConfig.name;
let accessory = new PlatformAccessory(accessoryName, accessoryUUID);
accessory.getService(Service.AccessoryInformation)
.setCharacteristic(Characteristic.Manufacturer, "Lutron")
.setCharacteristic(Characteristic.SerialNumber, (visorControlConfig.serial || "VCR" + visorControlConfig.id).toString());
let inputArray = visorControlConfig.inputs || [];
inputArray.forEach(function(buttonConfig) {
let buttonService = accessory.addService(Service.Switch, buttonConfig.name, buttonConfig.name);
}.bind(this));
let outputArray = visorControlConfig.outputs || [];
outputArray.forEach(function(buttonConfig) {
let buttonService = accessory.addService(Service.Switch, buttonConfig.name, buttonConfig.name);
}.bind(this));
let buttonArray = visorControlConfig.buttons || [];
buttonArray.forEach(function(buttonConfig) {
let buttonService = accessory.addService(Service.Switch, buttonConfig.name, buttonConfig.name);
}.bind(this));
this.accessories[accessory.UUID] = new VisorControlReceiverAccessory(this.log, visorControlConfig, accessory, this.radiora2, Homebridge);
this.api.registerPlatformAccessories("homebridge-radiora2", "RadioRA2", [accessory]);
}
addThermostatAccessory(accessoryUUID, hvacControllerConfig) {
let accessoryName = hvacControllerConfig.name;
let accessory = new PlatformAccessory(accessoryName, accessoryUUID);
accessory.getService(Service.AccessoryInformation)
.setCharacteristic(Characteristic.Manufacturer, "Lutron")
.setCharacteristic(Characteristic.SerialNumber, (hvacControllerConfig.serial || "HVAC" + hvacControllerConfig.id).toString());
let hvacControllerService = accessory.addService(Service.Thermostat, accessoryName);
this.accessories[accessory.UUID] = new ThermostatAccessory(this.log, hvacControllerConfig, accessory, this.radiora2, Homebridge);
this.api.registerPlatformAccessories("homebridge-radiora2", "RadioRA2", [accessory]);
}
configureAccessory(accessory) {
this.accessories[accessory.UUID] = accessory;
}
setupListeners() {
this.log("Attempting connection to " + this.config.repeater + "...");
this.radiora2 = new RadioRa2(this.config.repeater, this.config.username, this.config.password, this.log);
this.radiora2.connect();
this.radiora2.on("loggedIn", function () {
this.log("Connected to Lutron!");
//////////////////////////
// Fans
let fansArray = this.config.fans || [];
fansArray.forEach(function (fanConfig) {
fanConfig = addDefaultValues(fanConfig, "Fan");
if ((fanConfig.id) && (fanConfig.name)) {
var uuid = UUIDGen.generate("fan:" + fanConfig.id);
let fanAccessory = this.accessories[uuid];
if (!fanAccessory) {
this.addFanAccessory(uuid, fanConfig);
}
else {
this.accessories[uuid] = new FanAccessory(this.log, fanConfig, (fanAccessory instanceof FanAccessory ? fanAccessory.accessory : fanAccessory), this.radiora2, Homebridge);
}
this.accessories[uuid].existsInConfig = true;
this.log("Loaded fan '" + fanConfig.name + "'");
}
else {
this.log.warn("Invalid fan in config. Not loading it.");
}
}.bind(this));
this.log("Loaded " + fansArray.length + " fan(s)");
//////////////////////////
// Lights
let lightsArray = this.config.lights || [];
lightsArray.forEach(function (lightConfig) {
lightConfig = addDefaultValues(lightConfig, "Light");
if ((lightConfig.id) && (lightConfig.name)) {
var uuid = UUIDGen.generate("light:" + lightConfig.id);
let lightAccessory = this.accessories[uuid];
if (!lightAccessory) {
this.addLightAccessory(uuid, lightConfig);
}
else {
this.accessories[uuid] = new LightbulbAccessory(this.log, lightConfig, (lightAccessory instanceof LightbulbAccessory ? lightAccessory.accessory : lightAccessory), this.radiora2, Homebridge);
}
this.accessories[uuid].existsInConfig = true;
this.log("Loaded light '" + lightConfig.name + "'");
}
else {
this.log.warn("Invalid Light in config. Not loading it.");
}
}.bind(this));
this.log("Loaded " + lightsArray.length + " light(s)");
//////////////////////////
// Occupancy Sensors
let occupancySensorsArray = this.config.occupancysensors || [];
occupancySensorsArray.forEach(function (occupancySensorConfig) {
occupancySensorConfig = addDefaultValues(occupancySensorConfig, "Occupancy");
if ((occupancySensorConfig.id) && (occupancySensorConfig.name)) {
var uuid = UUIDGen.generate("group:" + occupancySensorConfig.id);
let occupancySensorAccessory = this.accessories[uuid];
if (!occupancySensorAccessory) {
this.addOccupancyAccessory(uuid, occupancySensorConfig);
}
else {
this.accessories[uuid] = new OccupancySensorAccessory(this.log, occupancySensorConfig, (occupancySensorAccessory instanceof OccupancySensorAccessory ? occupancySensorAccessory.accessory : occupancySensorAccessory), this.radiora2, Homebridge);
}
this.accessories[uuid].existsInConfig = true;
this.log("Loaded occupancy sensor '" + occupancySensorConfig.name + "'");
}
else {
this.log.warn("Invalid occupancy sensor in config. Not loading it.");
}
}.bind(this));
this.log("Loaded " + occupancySensorsArray.length + " occupancy sensor(s)");
//////////////////////////
// Keypads
let keypadsArray = this.config.keypads || [];
keypadsArray.forEach(function (keypadConfig) {
keypadConfig = addDefaultValues(keypadConfig, "Keypad");
if ((keypadConfig.id) && (keypadConfig.name)) {
var uuid = UUIDGen.generate("keypad:" + keypadConfig.id);
let keypadAccessory = this.accessories[uuid];
if (!keypadAccessory) {
if (keypadConfig.stateless) {
this.addKeypadButtonStatelessAccessory(uuid, keypadConfig);
}
else {
this.addKeypadButtonAccessory(uuid, keypadConfig);
}
}
else {
if (keypadConfig.stateless) {
this.accessories[uuid] = new KeypadButtonStatelessAccessory(this.log, keypadConfig, (keypadAccessory instanceof KeypadButtonStatelessAccessory ? keypadAccessory.accessory : keypadAccessory), this.radiora2, Homebridge);
}
else {
this.accessories[uuid] = new KeypadButtonAccessory(this.log, keypadConfig, (keypadAccessory instanceof KeypadButtonAccessory ? keypadAccessory.accessory : keypadAccessory), this.radiora2, Homebridge);
}
}
this.accessories[uuid].existsInConfig = true;
this.log("Loaded keypad '" + keypadConfig.name + "'");
}
else {
this.log.warn("Invalid Keypad in config. Not loading it.");
}
}.bind(this));
this.log("Loaded " + keypadsArray.length + " keypad(s)");
//////////////////////////
// Visor Control Receivers
let visorControlReceiversArray = this.config.visorcontrolreceivers || [];
visorControlReceiversArray.forEach(function (visorControlReceiverConfig) {
visorControlReceiverConfig = addDefaultValues(visorControlReceiverConfig, "VisorControlReceiver");
if ((visorControlReceiverConfig.id) && (visorControlReceiverConfig.name)) {
var uuid = UUIDGen.generate("visorcontrolreceiver:" + visorControlReceiverConfig.id);
let visorControlReceiverAccessory = this.accessories[uuid];
if (!visorControlReceiverAccessory) {
this.addVisorControlReceiverAccessory(uuid, visorControlReceiverConfig);
}
else {
this.accessories[uuid] = new VisorControlReceiverAccessory(this.log, visorControlReceiverConfig, (visorControlReceiverAccessory instanceof VisorControlReceiverAccessory ? visorControlReceiverAccessory.accessory : visorControlReceiverAccessory), this.radiora2, Homebridge);
}
this.accessories[uuid].existsInConfig = true;
this.log("Loaded visor control receiver '" + visorControlReceiverConfig.name + "'");
}
else {
this.log.warn("Invalid visor control receiver in config. Not loading it.");
}
}.bind(this));
this.log("Loaded " + visorControlReceiversArray.length + " visor control receiver(s)");
//////////////////////////
// HVAC Controllers
let hvacControllersArray = this.config.hvaccontrollers || [];
hvacControllersArray.forEach(function (hvacControllerConfig) {
hvacControllerConfig = addDefaultValues(hvacControllerConfig, "hvacController");
if ((hvacControllerConfig.id) && (hvacControllerConfig.name)) {
var uuid = UUIDGen.generate("hvacController:" + hvacControllerConfig.id);
let hvacControllerAccessory = this.accessories[uuid];
if (!hvacControllerAccessory) {
this.addThermostatAccessory(uuid, hvacControllerConfig);
}
else {
this.accessories[uuid] = new ThermostatAccessory(this.log, hvacControllerConfig, (hvacControllerAccessory instanceof ThermostatAccessory ? hvacControllerAccessory.accessory : hvacControllerAccessory), this.radiora2, Homebridge);
}
this.accessories[uuid].existsInConfig = true;
this.log("Loaded HVAC controller '" + hvacControllerConfig.name + "'");
}
else {
this.log.warn("Invalid HVAC controller in config. Not loading it.");
}
}.bind(this));
this.log("Loaded " + hvacControllersArray.length + " HVAC controller(s)");
//////////////////////////
// Remove Deleted
// Iterate over all accessories in the dictionary, and anything without the flag needs to be removed
Object.keys(this.accessories).forEach(function(accessoryUuid) {
var thisAccessory = this.accessories[accessoryUuid];
if (thisAccessory.existsInConfig !== true) {
this.api.unregisterPlatformAccessories(undefined, undefined, [thisAccessory]);
this.log("Deleted removed accessory");
}
}.bind(this));
}.bind(this));
//Disconnect cleaning when homebridge is shutting down
process.on("SIGINT", function() {this.radiora2.disconnect()}.bind(this));
process.on("SIGTERM", function() {this.radiora2.disconnect()}.bind(this));
}
}