-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
492 lines (393 loc) · 16.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
'use strict';
const http = require('http');
const urllib = require('url');
let Accessory, Service, Characteristic, UUIDGen;
const PLUGIN_NAME = "homebridge-shelly-switch";
const PLATFORM_NAME = "Shelly Switch";
const DEVICE_TYPE_DIMMER = 'dimmer';
const DEVICE_TYPE_SWITCH = 'switch';
module.exports = function (api) {
Service = api.hap.Service;
Characteristic = api.hap.Characteristic;
UUIDGen = api.hap.uuid;
api.registerPlatform(PLATFORM_NAME, ShellySwitch);
};
class ShellySwitch {
deviceUuid(device) {
const key = `switch-${device.ip}`;
const uri = `homebridge-shelly-switch:platform:accessory:${key}`;
const uuid = this.api.hap.uuid.generate(uri);
return uuid;
}
deviceButtonUuid(device) {
const key = `switch-${device.ip}-button-0`;
const uri = `homebridge-shelly-switch:platform:accessory:${key}`;
const uuid = this.api.hap.uuid.generate(uri);
return uuid;
}
constructor(log, config, api) {
this.api = api;
this.accessories = [];
this.services = new Map();
this.log = log;
this.devices = config.devices;
this.uuids = new Map();
this.indexes = new Map();
this.current_status = new Map();
this.status_callbacks = new Map();
this.current_status_time = new Map();
this.status_timer = new Map();
this.notification_port = config.notification_port || null;
this.buttonDevices = new Map();
this.devices.forEach ((el) => {
if (!el.ip) {
throw new Error('Address of the switch is missing');
}
if (!el.name) {
throw new Error('Name of the switch is missing');
}
});
this.devices.forEach((el, i) => {
const uuid = this.deviceUuid(el);
this.uuids.set(uuid, i);
});
this.api.on('didFinishLaunching', () => {
this.cleanupUnknownDevices();
this.configureDevices();
this.configureServer();
});
this.updateStatus(true);
}
configureServer() {
if (this.notification_port) {
this.log.debug(`Starting status notification server at port ${this.notification_port}`);
this.notification_server = http.createServer((req, res) => {
this.serverHandler(req, res);
});
this.notification_server.listen(this.notification_port, () => {
this.log.debug(`Started status notification server at port ${this.notification_port}`);
});
}
}
configureDevices() {
this.devices.forEach((device, i) => {
const key = `switch-${device.ip}`;
const uri = `homebridge-shelly-switch:platform:accessory:${key}`;
const uuid = this.deviceUuid(device);
let deviceService;
let accessory = this.accessories.find(accessory => accessory.UUID == uuid);
const serviceType = this.isDimmer(device) ? this.api.hap.Service.Lightbulb : this.api.hap.Service.Switch;
if (!accessory) {
accessory = new this.api.platformAccessory(device.name, uuid);
deviceService = new serviceType();
accessory.addService(deviceService);
this.log.debug(`Registering switch accessory: ${uuid} for ${device.name} (deviceType:${device.deviceType})`);
this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
} else {
deviceService = accessory.getService(serviceType);
}
deviceService.getCharacteristic(Characteristic.On)
.on('get', (callback) => { this.getSwitchStatus(key, device, callback, Characteristic.On) } )
.on('set', (value, callback) => { this.setSwitchStatus(key, device, 0, value, callback) } );
if (this.isDimmer(device)) {
deviceService.getCharacteristic(Characteristic.Brightness)
.on('get', (callback) => {
this.getSwitchStatus(key, device, callback, Characteristic.Brightness);
})
.on('set', (value, callback) => {
this.log.info('brightness', value);
this.setSwitchStatus(key, device, 0, value, callback);
});
}
deviceService.getCharacteristic(Characteristic.Name)
.on('get', (callback) => { callback(device.name) } );
this.services.set(key, deviceService);
this.indexes.set(key, i);
this.canExposeButton(key, (canExposeButton) => {
let programmableSwitch = accessory.getService(Service.StatelessProgrammableSwitch);
if (typeof(canExposeButton) == Error || !canExposeButton) {
if (programmableSwitch) {
accessory.removeService(programmableSwitch);
}
return;
}
if (!programmableSwitch) {
programmableSwitch = new this.api.hap.Service.StatelessProgrammableSwitch(device.name + ' Button');
programmableSwitch
.getCharacteristic(Characteristic.ProgrammableSwitchEvent)
.setProps( { minValue: 0, maxValue: 2, validValues: [0, 2] }); // short and long press
accessory.addService(programmableSwitch);
} else {
this.log.debug(`Found programmable switch service of ${programmableSwitch.UUID}`);
}
const buttonKey = key + 'button-0';
this.indexes.set(buttonKey, i);
this.buttonDevices.set(key, programmableSwitch);
});
});
}
cleanupUnknownDevices() {
this.accessories.forEach((el, i) => {
let device = this.uuids.get(el.UUID);
if (device === undefined) {
this.api.unregisterPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [el]);
}
});
}
serverHandler(req, res) {
this.log.debug(`Notification received from ${req.socket.remoteAddress}`);
// find the device
let remoteAddress = req.socket.remoteAddress;
let foundId = null;
this.indexes.forEach((index, id) => {
if (foundId) { return; }
this.log.debug(`checking index ${id}, ${index} for ${remoteAddress}`);
if (remoteAddress.indexOf(this.devices[index].ip) != -1) {
foundId = id;
this.log.debug(`Found device ${id}`);
}
});
if (!foundId) {
res.writeHead(404);
res.end('Not Found');
return;
}
if (req.url.endsWith('/status')) {
this.updateStatus(true, foundId);
res.writeHead(200);
res.end('OK');
return;
}
if (req.url.endsWith('/button/0/short')) {
this.triggerShortPress(foundId, 0);
res.writeHead(200);
res.end('OK');
return;
}
if (req.url.endsWith('/button/0/long')) {
this.triggerLongPress(foundId, 0);
res.writeHead(200);
res.end('OK');
return;
}
res.writeHead(404);
res.end('Not Found');
}
configureAccessory(accessory) {
this.accessories.push(accessory);
}
triggerShortPress(id, index) {
const service = this.buttonDevices.get(id);
service
.getCharacteristic(Characteristic.ProgrammableSwitchEvent)
.setValue(Characteristic.ProgrammableSwitchEvent.SINGLE_PRESS);
}
triggerLongPress(id) {
const service = this.buttonDevices.get(id);
service
.getCharacteristic(Characteristic.ProgrammableSwitchEvent)
.setValue(Characteristic.ProgrammableSwitchEvent.LONG_PRESS);
}
async setSwitchStatus(id, device, index, status, callback) {
var log = this.log;
log.debug(`Setting status of device ${device.ip} to '${status}'`);
const url = this.getSetStatusUrl(device, index, status);
log.debug(`url: ${url}`);
try {
let response = await this.sendJSONRequest({url: url, method: 'POST', authentication: device.authentication})
this.current_status.set(id, response);
this.current_status_time.set(id, Date.now());
callback();
this.updateStatus(false, id);
} catch (e) {
log.error(`Failed to change switch status: ${e}`);
setTimeout(() => { callback(e); this.updateStatus(true, id); }, 3000);
}
}
getSetStatusUrl(device, index, status) {
const turn = `turn=${status ? 'on' : 'off'}`;
if (this.isDimmer(device)) {
const brightness = Number.isInteger(status) ? `&brightness=${status}` : '';
return 'http://' + device.ip + `/light/${index}/?${turn}${brightness}`;
}
return 'http://' + device.ip + `/relay/${index}/?${turn}`;
}
getSwitchStatus(id, device, callback, characteristic) {
this.getStatus(id, false, (error) => {
if (error) {
callback(error);
return;
}
const currentStatus = characteristic === Characteristic.Brightness ?
this.current_status.get(id).brightness : this.current_status.get(id).ison == true;
this.log.debug({currentStatus, characteristic, device})
callback(null, currentStatus);
});
}
updateStatus(forced = false, id = null) {
this.log.debug(`Updating switch status of ${id}`);
let identifiers;
if (!id) {
identifiers = [ ...this.indexes.keys()];
} else {
identifiers = [id];
}
identifiers.forEach((id) => {
this.getStatus(id, forced, (err) => {
if (err) {
return;
}
let status = this.current_status.get(id);
let isOn = status.ison == true;
this.log.debug(`Reported current state for ${id}: ${isOn}`);
this.services.get(id).updateCharacteristic(Characteristic.On, isOn);
const device = this.devices.find(device => device.id === id);
this.log.debug(`device ${id}`, device);
if (device && this.isDimmer(device)) {
const { brightness } = status;
this.log.debug(`Reported current state for ${id}: brightness=${brightness}`);
this.services.get(id).updateCharacteristic(Characteristic.Brightness, brightness);
}
});
});
}
updateInterval() {
return 30000;
}
clearUpdateTimer(id) {
if (this.status_timer.has(id)) {
clearTimeout(this.status_timer.get(id));
}
}
setupUpdateTimer(id) {
if (this.notification_server) { // don't schedule status updates for polling - we have them pushed by the switch
return;
}
this.clearUpdateTimer(id);
this.status_timer.set(
id,
setTimeout(() => { this.updateStatus(true, id); }, this.updateInterval()));
}
needsUpdate() {
return true;
};
async getStatus(id, forced, callback) {
let identifiers;
if (!id) {
identifiers = [ ...this.indexes.keys()];
} else {
identifiers = [id];
}
identifiers.forEach(async (id) => {
if (!this.status_callbacks.has(id)) {
this.status_callbacks.set(id, []);
}
if (this.status_callbacks.get(id).length > 0) {
this.log.debug('Pushing status callback to queue - updating');
this.status_callbacks.get(id).push(callback);
return;
}
const now = Date.now();
if (!forced && this.current_status.has(id) &&
this.current_status_time.has(id) &&
(now - this.current_status_time.get(id) < this.updateInterval())) {
this.log.debug('Returning cached status');
callback(null);
return;
}
this.clearUpdateTimer(id);
this.status_callbacks.get(id).push(callback);
const device = this.devices[this.indexes.get(id)];
try {
let response = await this.sendJSONRequest({url: this.getGetStatusUrl(device), authentication: device.authentication});
this.current_status.set(id, response);
this.current_status_time.set(id, Date.now());
const callbacks = this.status_callbacks.get(id);
this.status_callbacks.set(id, []);
this.log.debug(`Calling ${callbacks.length} queued callbacks`);
callbacks.forEach((element) => {
element(null, response);
});
this.setupUpdateTimer(id);
} catch (e) {
this.log.error(`Error parsing current status info: ${e}`);
const callbacks = this.status_callbacks.get(id);
this.status_callbacks.set(id, []);
callbacks.forEach((element) => {
element(e);
});
this.setupUpdateTimer(id);
}
});
}
getGetStatusUrl(device) {
return this.isDimmer(device) ? `http://${device.ip}/light/0` : `http://${device.ip}/relay/0`;
}
isExposable(type) {
const exposable = type === 'momentary' || type === 'detached';
this.log.debug(`Is ${type} exposable? ${exposable}`);
return exposable;
}
async canExposeButton(id, callback) {
try {
let settings = await this.getSettings(id);
if (!settings['relays']) {
callback(Error('No relays found'));
}
callback(this.isExposable(settings.relays[0].btn_type));
} catch(e) {
callback(e);
}
}
async getSettings(id) {
const device = this.devices[this.indexes.get(id)];
return await this.sendJSONRequest({url: 'http://' + device.ip + '/settings', authentication: device.authentication})
}
async sendJSONRequest(params) {
return new Promise((resolve, reject) => {
if (!params.url) {
reject(Error('Request URL missing'));
}
const components = new urllib.URL(params.url);
const options = {
method: params.method || 'GET',
host: components.hostname,
port: components.port,
path: components.pathname + (components.search ? components.search : ''),
protocol: components.protocol,
headers: { 'Content-Type': 'application/json' }
};
if (params.authentication) {
let credentials = Buffer.from(params.authentication).toString('base64');
options.headers['Authorization'] = 'Basic ' + credentials;
}
const req = http.request(options, (res) => {
res.setEncoding('utf8');
let chunks = '';
res.on('data', (chunk) => { chunks += chunk; });
res.on('end', () => {
try {
this.log.debug(`Raw response: ${chunks}`, options, params.url);
const parsed = JSON.parse(chunks);
resolve(parsed);
} catch (e) {
reject(e);
}
});
});
req.on('error', (err) => {
reject(err);
});
if (params.payload) {
const stringified = JSON.stringify(params.payload);
this.log(`sending payload: ${stringified}`);
req.write(stringified);
}
req.end();
});
}
isDimmer(device) {
const { deviceType } = device;
return deviceType === DEVICE_TYPE_DIMMER;
}
}