generated from homebridge/homebridge-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 6
/
platform.ts
606 lines (553 loc) · 19.5 KB
/
platform.ts
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
import { APIEvent } from 'homebridge';
import type {
API,
DynamicPlatformPlugin,
Logger,
PlatformAccessory,
PlatformConfig,
} from 'homebridge';
import { fetch, Headers, AbortController } from 'popsicle/dist/node';
import { Policy, ConsecutiveBreaker } from 'cockatiel';
// Internal Types
import {
DingzDevices,
DingzDeviceInfo,
DeviceInfo,
DingzAccessories,
DeviceTypes,
MyStromDeviceInfo,
} from './util/internalTypes';
import {
InvalidTypeError,
DeviceNotImplementedError,
DeviceNotReachableError,
} from './util/errors';
import { PLATFORM_NAME, PLUGIN_NAME, DINGZ_DISCOVERY_PORT } from './settings';
import { createSocket, Socket, RemoteInfo } from 'dgram';
// TODO: Some refactoring for beter event handling, cleanup of the code and separation of concerns
import { DingzDaAccessory } from './dingzDaAccessory';
import { MyStromSwitchAccessory } from './myStromSwitchAccessory';
import { MyStromLightbulbAccessory } from './myStromLightbulbAccessory';
import { MYSTROM_SWITCH_TYPES } from './util/internalTypes';
// Define a policy that will retry 20 times at most
const retry = Policy.handleAll()
.retry()
.exponential({ maxDelay: 10 * 1000, maxAttempts: 20 });
// Create a circuit breaker that'll stop calling the executed function for 10
// seconds if it fails 5 times in a row. This can give time for e.g. a database
// to recover without getting tons of traffic.
const circuitBreaker = Policy.handleAll().circuitBreaker(
10 * 1000,
new ConsecutiveBreaker(5),
);
const retryWithBreaker = Policy.wrap(retry, circuitBreaker);
/**
* HomebridgePlatform
* This class is the main constructor for your plugin, this is where you should
* parse the user config and discover/register accessories with Homebridge.
*/
export class DingzDaHomebridgePlatform implements DynamicPlatformPlugin {
public readonly Service = this.api.hap.Service;
public readonly Characteristic = this.api.hap.Characteristic;
// this is used to track restored cached accessories
public accessories: DingzAccessories = {};
private readonly discoverySocket: Socket = createSocket('udp4');
constructor(
public readonly log: Logger,
public readonly config: PlatformConfig,
public readonly api: API,
) {
this.log.debug('Finished initializing platform:', this.config.name);
// When this event is fired it means Homebridge has restored all cached accessories from disk.
// Dynamic Platform plugins should only register new accessories after this event was fired,
// in order to ensure they weren't added to homebridge already. This event can also be used
// to start discovery of new accessories.
this.api.on(APIEvent.DID_FINISH_LAUNCHING, () => {
this.log.debug('Executed didFinishLaunching callback');
// run the method to discover / register your devices as accessories
this.addDevices(); // Adds decvices from Config
this.setupDeviceDiscovery(); // Discovers devices from UDP
});
}
/**
* This function is invoked when homebridge restores cached accessories from disk at startup.
* It should be used to setup event handlers for characteristics and update respective values.
*/
configureAccessory(accessory: PlatformAccessory) {
this.log.info(
'Restoring accessory from cache:',
accessory.displayName,
'->',
accessory.context.device.accessoryClass,
);
const context = accessory.context;
let platformAccessory:
| DingzDaAccessory
| MyStromSwitchAccessory
| MyStromLightbulbAccessory;
if (context.device && context.device.accessoryClass) {
this.log.debug(
'Restoring accessory of class ->',
context.device.accessoryClass,
);
switch (context.device.accessoryClass) {
case 'DingzDaAccessory':
// add the restored accessory to the accessories cache so we can track if it has already been registered
platformAccessory = new DingzDaAccessory(this, accessory);
break;
case 'MyStromSwitchAccessory':
// add the restored accessory to the accessories cache so we can track if it has already been registered
platformAccessory = new MyStromSwitchAccessory(this, accessory);
break;
case 'MyStromLightbulbAccessory':
// add the restored accessory to the accessories cache so we can track if it has already been registered
platformAccessory = new MyStromLightbulbAccessory(this, accessory);
break;
default:
this.log.warn(
'No Accessory type defined for Accessory',
accessory.displayName,
'can not restore',
);
return;
}
this.accessories[accessory.UUID] = platformAccessory;
} else {
this.log.warn(
'No Accessory device context for Accessory',
accessory.displayName,
'can not restore',
);
}
}
/**
* This is an example method showing how to register discovered accessories.
* Accessories must only be registered once, previously created accessories
* must not be registered again to prevent "duplicate UUID" errors.
*/
private async addDevices() {
// loop over the discovered devices and register each one if it has not already been registered
for (const device of this.config.devices) {
// Call addDevice, retry until found
this.log.info('Add Device from config: ', device.type, '->', device.name);
switch (device.type) {
case 'Dingz':
await retryWithBreaker.execute(() =>
this.addDingzDevice(
device.address,
device.name,
device.token ?? this.config.globalToken,
),
);
break;
case 'myStromSwitch':
await retryWithBreaker.execute(() =>
this.addMyStromSwitchDevice({
address: device.address,
name: device.name,
token: device.token ?? this.config.globalToken,
}),
);
break;
case 'myStromBulb':
await retryWithBreaker.execute(() =>
this.addMyStromLightbulbDevice({
address: device.address,
name: device.name,
token: device.token ?? this.config.globalToken,
}),
);
break;
case 'myStromLED':
case 'myStromPIR':
default:
this.log.warn(
'Device type',
device.deviceType,
'is currently unsupported. Will skip',
);
break;
}
}
}
// Add one device based on address and name
private addDingzDevice(
address: string,
name = 'Unnamed DingzDa Device',
token?: string,
): boolean {
// Run a diacovery of changed things every 10 seconds
this.log.debug(`Add configured device -> ${name} (${address})`);
const success = this.getDingzDeviceInfo({ address, token }).then((data) => {
this.log.debug('Got Device ->', JSON.stringify(data as DingzDevices));
if (typeof data !== 'undefined') {
const ddi = data as DingzDevices;
const keys = Object.keys(ddi);
const mac = keys[0]; // keys[0]
const info: DingzDeviceInfo = ddi[mac];
if (info.type !== 'dingz') {
throw new InvalidTypeError(
`Device ${name} at ${address} is of the wrong type (${info.type} instead of "dingz")`,
);
}
const deviceInfo: DeviceInfo = {
name: name,
address: address,
mac: mac,
token: token,
model: info.has_pir ? 'Dingz+' : 'Dingz',
hwInfo: info,
accessoryClass: 'DingzDaAccessory',
};
// generate a unique id for the accessory this should be generated from
// something globally unique, but constant, for example, the device serial
// number or MAC address
const uuid = this.api.hap.uuid.generate(deviceInfo.mac);
// check that the device has not already been registered by checking the
// cached devices we stored in the `configureAccessory` method above
if (!this.accessories[uuid]) {
this.log.info('Registering new accessory:', deviceInfo.name);
// create a new accessory
const accessory = new this.api.platformAccessory(
deviceInfo.name,
uuid,
);
// store a copy of the device object in the `accessory.context`
// the `context` property can be used to store any data about the accessory you may need
accessory.context.device = deviceInfo;
// create the accessory handler (which will add services as needed)
// this is imported from `dingzDaAccessory.ts`
const dingzDaAccessory = new DingzDaAccessory(this, accessory);
// link the accessory to your platform
this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [
accessory,
]);
// push into accessory cache
this.accessories[uuid] = dingzDaAccessory;
return true;
} else {
this.log.warn('Accessory already initialized');
this.accessories[uuid].identify();
return true;
}
}
});
if (!success) {
// Nothing found, throw error
throw new DeviceNotReachableError(
`Device not found -> ${name} (${address})`,
);
}
return true;
}
// Add one device based on address and name
private addMyStromSwitchDevice({
address,
name = 'Unidentified myStrom Switch',
token,
}: {
address: string;
name?: string;
token?: string;
}): boolean {
// Run a diacovery of changed things every 10 seconds
this.log.debug(`Add configured/discovered device -> ${name} (${address})`);
const success = this.getMyStromDeviceInfo({
address,
token,
}).then((data) => {
if (typeof data !== 'undefined') {
const info = data as MyStromDeviceInfo;
if (
// FIXME: Fix the API Documentation
// The type info returned varies with Firmware versions.
// Newer Firmwares seem to have string-based types whereas
// older ones use the numbers from the API documentation
info.type !== 'WS2' &&
info.type !== 106 &&
info.type !== 'WSEU' &&
info.type !== 107 &&
info.type !== undefined // Switch V1 does not have a type
) {
throw new InvalidTypeError(
`Device ${name} at ${address} is of the wrong type (${info.type} instead of "myStrom Switch")`,
);
}
const deviceInfo: DeviceInfo = {
name: info.name ?? name,
address: address,
mac: info.mac,
token: token,
model: MYSTROM_SWITCH_TYPES[info.type] ?? 'CH v1',
hwInfo: info,
accessoryClass: 'MyStromSwitchAccessory',
};
// generate a unique id for the accessory this should be generated from
// something globally unique, but constant, for example, the device serial
// number or MAC address
const uuid = this.api.hap.uuid.generate(deviceInfo.mac);
// check that the device has not already been registered by checking the
// cached devices we stored in the `configureAccessory` method above
if (!this.accessories[uuid]) {
this.log.info('Registering new accessory:', deviceInfo.name);
// create a new accessory
const accessory = new this.api.platformAccessory(
deviceInfo.name,
uuid,
);
// store a copy of the device object in the `accessory.context`
// the `context` property can be used to store any data about the accessory you may need
accessory.context.device = deviceInfo;
// create the accessory handler (which will add services as needed)
// this is imported from `dingzDaAccessory.ts`
const myStromSwitchAccessory = new MyStromSwitchAccessory(
this,
accessory,
);
// link the accessory to your platform
this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [
accessory,
]);
// push into accessory cache
this.accessories[uuid] = myStromSwitchAccessory;
return true;
} else {
this.log.warn('Accessory already initialized');
this.accessories[uuid].identify();
return true;
}
}
});
// Nothing found, throw error
if (!success) {
throw new DeviceNotReachableError(
`Device not found -> ${name} (${address})`,
);
}
return true;
}
// Add one device based on address and name
private addMyStromLightbulbDevice({
address,
name = 'Unidentified myStrom Lightbulb',
token,
}: {
address: string;
name?: string;
token?: string;
}): boolean {
// Run a diacovery of changed things every 10 seconds
this.log.debug(`Add configured/discovered device -> ${name} (${address})`);
const success = this.getMyStromDeviceInfo({
address,
token,
}).then((data) => {
if (typeof data !== 'undefined') {
const info = data as MyStromDeviceInfo;
if (info.type !== 102) {
throw new InvalidTypeError(
`Device ${name} at ${address} is of the wrong type (${info.type} instead of "myStrom Lightbulb")`,
);
}
const deviceInfo: DeviceInfo = {
name: info.name ?? name,
address: address,
mac: info.mac,
token: token,
model: '102',
hwInfo: info,
accessoryClass: 'MyStromLightbulbAccessory',
};
// generate a unique id for the accessory this should be generated from
// something globally unique, but constant, for example, the device serial
// number or MAC address
const uuid = this.api.hap.uuid.generate(deviceInfo.mac);
// check that the device has not already been registered by checking the
// cached devices we stored in the `configureAccessory` method above
if (!this.accessories[uuid]) {
this.log.info('Registering new accessory:', deviceInfo.name);
// create a new accessory
const accessory = new this.api.platformAccessory(
deviceInfo.name,
uuid,
);
// store a copy of the device object in the `accessory.context`
// the `context` property can be used to store any data about the accessory you may need
accessory.context.device = deviceInfo;
// create the accessory handler (which will add services as needed)
// this is imported from `dingzDaAccessory.ts`
const myStromLightbulbAccessory = new MyStromLightbulbAccessory(
this,
accessory,
);
// link the accessory to your platform
this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [
accessory,
]);
// push into accessory cache
this.accessories[uuid] = myStromLightbulbAccessory;
return true;
} else {
this.log.warn('Accessory already initialized');
this.accessories[uuid].identify();
return true;
}
}
});
// Nothing found, throw error
if (!success) {
throw new DeviceNotReachableError(
`Device not found -> ${name} (${address})`,
);
}
return true;
}
private datagramMessageHandler(msg: Uint8Array, remoteInfo: RemoteInfo) {
// const mac: string = dataBuffer.toString('hex', 0, 6);
try {
if (msg.length !== 8) {
throw new DeviceNotImplementedError('Detected data can not be parsed.');
}
const t: DeviceTypes = msg[6];
switch (t) {
case DeviceTypes.MYSTROM_BULB:
case DeviceTypes.MYSTROM_BUTTON_PLUS:
case DeviceTypes.MYSTROM_BUTTON:
case DeviceTypes.MYSTROM_LEDSTRIP:
throw new DeviceNotImplementedError(
`Device discovered at ${remoteInfo.address} of unsupported type ${DeviceTypes[t]}`,
);
break;
case DeviceTypes.MYSTROM_SWITCH_CHV1:
case DeviceTypes.MYSTROM_SWITCH_CHV2:
case DeviceTypes.MYSTROM_SWITCH_EU:
this.log.debug(
'Discovered MyStrom Switch',
DeviceTypes[t],
'at',
remoteInfo.address,
'-> Attempting to identify and add.',
);
retryWithBreaker.execute(() => {
this.addMyStromSwitchDevice({
address: remoteInfo.address,
name: 'Auto-Discovered MyStromSwitch',
token: this.config.globalToken,
});
});
break;
case DeviceTypes.DINGZ:
this.log.debug(
'Auto-Discovered Dingz Device at ',
remoteInfo.address,
'-> Attempting to identify and add.',
);
retryWithBreaker.execute(() => {
this.addDingzDevice(
remoteInfo.address,
'Auto-Discovered Dingz',
this.config.globalToken,
);
});
break;
default:
this.log.debug(`Unknown device: ${t}`);
break;
}
} catch (e) {
if (e instanceof DeviceNotImplementedError) {
// Degrade gracefully if type not found
this.log.debug(e.message);
} else {
throw e;
}
}
}
// Steup device discovery. This will run for 10 minutes and then stop
// If you want tore-discover, just restart Homebridge
private setupDeviceDiscovery() {
this.discoverySocket.on('message', this.datagramMessageHandler.bind(this));
this.discoverySocket.bind(DINGZ_DISCOVERY_PORT);
setTimeout(() => {
this.log.warn('Stopping discovery');
this.discoverySocket.close();
}, 600000); // Discover for 10 min then stop
return true;
}
/**
* Device Methods -- these are used to retrieve the data from the Dingz
* TODO: Refactor duplicate code into proper API caller
*/
async getDingzDeviceInfo({
address,
token,
}: {
address: string;
token?: string;
}): Promise<DingzDevices> {
const deviceInfoUrl = `http://${address}/api/v1/device`;
return await this.fetch({
url: deviceInfoUrl,
returnBody: true,
token,
});
}
/**
* Device Methods -- these are used to retrieve the data from the Dingz
* FIXME: API Endpoint
* Officially, the API is at /api/v1/info but there's
* an undocumenten API at /info which also works for V1 switches
*/
async getMyStromDeviceInfo({
address,
token,
}: {
address: string;
token?: string;
}): Promise<MyStromDeviceInfo> {
const deviceInfoUrl = `http://${address}/info`;
return await this.fetch({
url: deviceInfoUrl,
returnBody: true,
token,
});
}
async fetch({
url,
method = 'GET',
returnBody = false,
token,
body,
}: {
url: string;
method?: string;
returnBody?: boolean;
token?: string;
body?: string;
}) {
const controller = new AbortController();
setTimeout(() => controller.abort(), 2000);
const headers: Headers = new Headers();
if (token) {
headers.set('Token', token);
}
this.log.debug('Fetching ', url);
const data = await fetch(url, {
method: method,
headers: headers,
signal: controller.signal,
body: body,
})
.then((response) => {
if (returnBody) {
return response.json();
} else {
return response.status;
}
})
.catch((e) => {
this.log.error('Error:', e);
});
return data;
}
}