homebridge-savantlightingcn 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/README.md +45 -0
  2. package/accessories/alarm_control_panel.js +187 -0
  3. package/accessories/binary_sensor.js +243 -0
  4. package/accessories/climate.js +526 -0
  5. package/accessories/cover.js +214 -0
  6. package/accessories/device_tracker.js +152 -0
  7. package/accessories/fan.js +254 -0
  8. package/accessories/floor_heating.js +266 -0
  9. package/accessories/light.js +519 -0
  10. package/accessories/lock.js +177 -0
  11. package/accessories/media_player.js +276 -0
  12. package/accessories/sensor.js +225 -0
  13. package/accessories/switch.js +217 -0
  14. package/dist/css/app.ddbde395.css +1 -0
  15. package/dist/css/chunk-vendors.f2db5e15.css +1 -0
  16. package/dist/css/test.0ebe5642.css +0 -0
  17. package/dist/favicon.ico +0 -0
  18. package/dist/index.html +1 -0
  19. package/dist/js/about.7c566887.js +2 -0
  20. package/dist/js/about.7c566887.js.map +1 -0
  21. package/dist/js/about.ecf592a0.js +2 -0
  22. package/dist/js/about.ecf592a0.js.map +1 -0
  23. package/dist/js/app.01e8fa44.js +2 -0
  24. package/dist/js/app.01e8fa44.js.map +1 -0
  25. package/dist/js/app.15ed2e3b.js +2 -0
  26. package/dist/js/app.15ed2e3b.js.map +1 -0
  27. package/dist/js/app.25976869.js +2 -0
  28. package/dist/js/app.25976869.js.map +1 -0
  29. package/dist/js/app.3112044b.js +2 -0
  30. package/dist/js/app.3112044b.js.map +1 -0
  31. package/dist/js/app.3a289bce.js +2 -0
  32. package/dist/js/app.3a289bce.js.map +1 -0
  33. package/dist/js/app.734f9d85.js +2 -0
  34. package/dist/js/app.734f9d85.js.map +1 -0
  35. package/dist/js/app.83545550.js +2 -0
  36. package/dist/js/app.83545550.js.map +1 -0
  37. package/dist/js/app.926ce9ff.js +2 -0
  38. package/dist/js/app.926ce9ff.js.map +1 -0
  39. package/dist/js/app.a6ba37fb.js +2 -0
  40. package/dist/js/app.a6ba37fb.js.map +1 -0
  41. package/dist/js/app.aa309e99.js +2 -0
  42. package/dist/js/app.aa309e99.js.map +1 -0
  43. package/dist/js/app.b2b4f482.js +2 -0
  44. package/dist/js/app.b2b4f482.js.map +1 -0
  45. package/dist/js/app.f9643858.js +2 -0
  46. package/dist/js/app.f9643858.js.map +1 -0
  47. package/dist/js/chunk-vendors.372a828c.js +52 -0
  48. package/dist/js/chunk-vendors.372a828c.js.map +1 -0
  49. package/dist/js/chunk-vendors.4b731e62.js +52 -0
  50. package/dist/js/chunk-vendors.4b731e62.js.map +1 -0
  51. package/dist/js/chunk-vendors.941b76e9.js +46 -0
  52. package/dist/js/chunk-vendors.941b76e9.js.map +1 -0
  53. package/dist/js/test.041333f7.js +2 -0
  54. package/dist/js/test.041333f7.js.map +1 -0
  55. package/dist/js/test.70bf0852.js +8 -0
  56. package/dist/js/test.70bf0852.js.map +1 -0
  57. package/dist/js/test.840bc653.js +2 -0
  58. package/dist/js/test.840bc653.js.map +1 -0
  59. package/index.js +1635 -0
  60. package/package.json +44 -0
@@ -0,0 +1,519 @@
1
+ 'use strict';
2
+
3
+ let Service;
4
+ let Characteristic;
5
+ let communicationError;
6
+
7
+ /* eslint-disable */
8
+ const LightUtil = {
9
+ hsvToRgb(h, s, v) {//0.7,0.68,0
10
+ console.log("hsvToRgb(" + h + ", " + s + ", " + v + ")");
11
+ let r;
12
+ let g;
13
+ let b;
14
+ let i;
15
+ let f;
16
+ let p;
17
+ let q;
18
+ let t;
19
+ if (arguments.length === 1) {
20
+ s = h.s, v = h.v, h = h.h;
21
+ }
22
+ i = Math.floor(h * 6);//4
23
+ f = h * 6 - i;//0.2
24
+ p = v * (1 - s);//0
25
+ q = v * (1 - f * s);//0
26
+ t = v * (1 - (1 - f) * s);//0
27
+ switch (i % 6) {
28
+ case 0: r = v, g = t, b = p; break;
29
+ case 1: r = q, g = v, b = p; break;
30
+ case 2: r = p, g = v, b = t; break;
31
+ case 3: r = p, g = q, b = v; break;
32
+ case 4: r = t, g = p, b = v; break;
33
+ case 5: r = v, g = p, b = q; break;
34
+ }
35
+ return {
36
+ r: Math.round(r * 255),
37
+ g: Math.round(g * 255),
38
+ b: Math.round(b * 255),
39
+ };
40
+ },
41
+ rgbToHsv(r, g, b) {
42
+ if (arguments.length === 1) {
43
+ g = r.g, b = r.b, r = r.r;
44
+ }
45
+ let max = Math.max(r, g, b),
46
+ min = Math.min(r, g, b),
47
+ d = max - min,
48
+ h,
49
+ s = (max === 0 ? 0 : d / max),
50
+ v = max / 255;
51
+
52
+ switch (max) {
53
+ case min: h = 0; break;
54
+ case r: h = (g - b) + d * (g < b ? 6 : 0); h /= 6 * d; break;
55
+ case g: h = (b - r) + d * 2; h /= 6 * d; break;
56
+ case b: h = (r - g) + d * 4; h /= 6 * d; break;
57
+ }
58
+
59
+ return {
60
+ h,
61
+ s,
62
+ v,
63
+ };
64
+ },
65
+ rgbToCie(red, green, blue) {
66
+ // Apply a gamma correction to the RGB values, which makes the color more vivid and more the like the color displayed on the screen of your device
67
+ red = (red > 0.04045) ? Math.pow((red + 0.055) / (1.0 + 0.055), 2.4) : (red / 12.92);
68
+ green = (green > 0.04045) ? Math.pow((green + 0.055) / (1.0 + 0.055), 2.4) : (green / 12.92);
69
+ blue = (blue > 0.04045) ? Math.pow((blue + 0.055) / (1.0 + 0.055), 2.4) : (blue / 12.92);
70
+
71
+ // RGB values to XYZ using the Wide RGB D65 conversion formula
72
+ const X = red * 0.664511 + green * 0.154324 + blue * 0.162028;
73
+ const Y = red * 0.283881 + green * 0.668433 + blue * 0.047685;
74
+ const Z = red * 0.000088 + green * 0.072310 + blue * 0.986039;
75
+
76
+ // Calculate the xy values from the XYZ values
77
+ let x = (X / (X + Y + Z)).toFixed(4);
78
+ let y = (Y / (X + Y + Z)).toFixed(4);
79
+
80
+ if (isNaN(x)) {
81
+ x = 0;
82
+ }
83
+
84
+ if (isNaN(y)) { y = 0; }
85
+
86
+ return [x, y];
87
+ },
88
+ rgbToCct(rgb_R, rgb_G, rgb_B) {
89
+ var trimX = 0;
90
+ var trimY = 0;
91
+ var trimZ = 0;
92
+ var coorX = 0, coorY = 0;
93
+ var CCT = 0;
94
+ var n = 0;
95
+ const R = rgb_R;//255;
96
+ const G = rgb_G;//231;
97
+ const B = rgb_B;//131;
98
+ //以下公式实现RGB转三刺激值
99
+ trimX = 2.789 * R + 1.7517 * G + 1.1302 * B;
100
+ trimY = 1 * R + 4.5907 * G + 0.0601 * B;
101
+ trimZ = 0 * R + 0.0565 * G + 5.5943 * B;
102
+ //以下公式实现三刺激值转色坐标
103
+ coorX = trimX / (trimX + trimY + trimZ);
104
+ coorY = trimY / (trimX + trimY + trimZ);
105
+ n = (coorX - 0.3320) / (0.1858 - coorY);
106
+ 以下公式实现色坐标转色温
107
+ CCT = 437 * n * n * n + 3601 * n * n + 6831 * n + 5517;
108
+ System.out.println("X:" + trimX + "Y:" + trimY + "Z:" + trimZ + "\n CCT:" + CCT);
109
+ return CCT;
110
+ },
111
+
112
+ };
113
+ /* eslint-enable */
114
+
115
+ var ControlPowerDictionary = new Array();
116
+
117
+ function HomeAssistantLight(log, data, client, firmware) {
118
+ // device info
119
+ this.domain = 'light';
120
+ this.data = data;
121
+ this.entity_id = data.entity_id;
122
+ this.uuid_base = data.entity_id;
123
+ this.firmware = firmware;
124
+ if (data.attributes && data.attributes.friendly_name) {
125
+ this.name = data.attributes.friendly_name;
126
+ } else {
127
+ this.name = data.entity_id.split('.').pop().replace(/_/g, ' ');
128
+ }
129
+ if (data.attributes && data.attributes.homebridge_manufacturer) {
130
+ this.manufacturer = String(data.attributes.homebridge_manufacturer);
131
+ } else {
132
+ this.manufacturer = 'Home Assistant';
133
+ }
134
+ if (data.attributes && data.attributes.homebridge_model) {
135
+ this.model = String(data.attributes.homebridge_model);
136
+ } else {
137
+ this.model = 'Light';
138
+ }
139
+ if (data.attributes && data.attributes.homebridge_serial) {
140
+ this.serial = String(data.attributes.homebridge_serial);
141
+ } else {
142
+ this.serial = data.entity_id;
143
+ }
144
+ this.client = client;
145
+ this.log = log;
146
+
147
+ this.maxTemp = 400;
148
+ this.minTemp = 50;
149
+
150
+ if (data.attributes.homebridge_max_mireds) {
151
+ this.maxTemp = data.attributes.homebridge_max_mireds;
152
+ }
153
+
154
+ if (data.attributes.homebridge_min_mireds) {
155
+ this.minTemp = data.attributes.homebridge_min_mireds;
156
+ }
157
+
158
+ this.cachedColor = false;
159
+ }
160
+
161
+ HomeAssistantLight.prototype = {
162
+ features: Object.freeze({
163
+ BRIGHTNESS: 1,
164
+ COLOR_TEMP: 2,
165
+ EFFECT: 4,
166
+ FLASH: 8,
167
+ RGB_COLOR: 16,
168
+ TRANSITION: 32,
169
+ XY_COLOR: 64,
170
+ }),
171
+ is_supported(feature) {
172
+ // If the supported_features attribute doesn't exist, assume not supported
173
+ if (this.data.attributes.supported_features === undefined) {
174
+ return false;
175
+ }
176
+
177
+ return (this.data.attributes.supported_features & feature) > 0;
178
+ },
179
+ onEvent(oldState, newState) {
180
+ if (newState.state) {
181
+ //this.lightbulbService.getCharacteristic(Characteristic.On).setValue(newState.state === 'on', null, 'internal');
182
+ this.lightbulbService.updateCharacteristic(Characteristic.On, newState.state === 'on');
183
+ if (this.is_supported(this.features.BRIGHTNESS)) {
184
+ const brightness = Math.round(newState.attributes.brightness || 0);
185
+
186
+ //this.lightbulbService.getCharacteristic(Characteristic.Brightness).setValue(brightness, null, 'internal');
187
+ this.lightbulbService.updateCharacteristic(Characteristic.Brightness, brightness);
188
+ this.data.attributes.brightness = newState.attributes.brightness;
189
+ }
190
+
191
+ if (this.is_supported(this.features.RGB_COLOR) &&
192
+ newState.attributes.rgb_color !== undefined) {
193
+ const rgbColor = newState.attributes.rgb_color;
194
+ const hsv = LightUtil.rgbToHsv(rgbColor[0], rgbColor[1], rgbColor[2]);
195
+ const hue = hsv.h * 360;
196
+ const saturation = hsv.s * 100;
197
+
198
+ this.lightbulbService.updateCharacteristic(Characteristic.Hue, hue);
199
+ this.lightbulbService.updateCharacteristic(Characteristic.Saturation, saturation);
200
+ //this.lightbulbService.getCharacteristic(Characteristic.Hue).setValue(hue, null, 'internal');
201
+ //this.lightbulbService.getCharacteristic(Characteristic.Saturation).setValue(saturation, null, 'internal');
202
+
203
+ this.data.attributes.hue = hue;
204
+ this.data.attributes.saturation = saturation;
205
+ }
206
+
207
+ if (this.is_supported(this.features.COLOR_TEMP)) {
208
+ const colorTemperature = Math.round(newState.attributes.color_temp) || this.minTemp;
209
+
210
+ this.lightbulbService.updateCharacteristic(Characteristic.ColorTemperature, colorTemperature);
211
+ //this.lightbulbService.getCharacteristic(Characteristic.ColorTemperature).setValue(colorTemperature, null, 'internal');
212
+ }
213
+ }
214
+ },
215
+ identify(callback) {
216
+ this.log(`identifying: ${this.name}`);
217
+
218
+ const that = this;
219
+ const serviceData = {};
220
+ serviceData.entity_id = this.entity_id;
221
+ let service = 'toggle';
222
+ if (this.is_supported(this.features.FLASH)) {
223
+ service = 'turn_on';
224
+ serviceData.flash = 'short';
225
+ }
226
+ this.client.callService(this.domain, service, serviceData, (data) => {
227
+ if (data) {
228
+ that.log(`Successfully identified '${that.name}'`);
229
+ }
230
+ callback();
231
+ });
232
+ },
233
+ getPowerState(callback) {
234
+ this.log(`fetching power state for: ${this.name}`);
235
+ const that = this;
236
+ this.client.fetchState(this.entity_id, (data) => {
237
+ that.log(`getPowerState fetchState receved data for: ${this.name}`)
238
+ if (data) {
239
+ const powerState = data.state === 'on';
240
+ callback(null, powerState);
241
+ } else {
242
+ callback(communicationError);
243
+ }
244
+ });
245
+ },
246
+ getBrightness(callback) {
247
+ this.log(`fetching brightness for: ${this.name}`);
248
+ const that = this;
249
+ this.client.fetchState(this.entity_id, (data) => {
250
+ that.log(`getBrightness fetchState receved data for: ${this.name}`)
251
+ if (data) {
252
+ const brightness = data.attributes.brightness || 0;
253
+ that.data.attributes.brightness = brightness;
254
+ callback(null, brightness);
255
+ } else {
256
+ callback(communicationError);
257
+ }
258
+ });
259
+ },
260
+ getHue(callback) {
261
+ const that = this;
262
+ this.log(`fetching getHue for: ${this.name}`);
263
+ this.client.fetchState(this.entity_id, (data) => {
264
+ that.log(`getHue fetchState receved data for: ${this.name}`)
265
+ if (data) {
266
+ const rgb = data.attributes.rgb_color || [0, 0, 0];
267
+ const hsv = LightUtil.rgbToHsv(rgb[0], rgb[1], rgb[2]);
268
+ const hue = hsv.h * 360;
269
+ that.data.attributes.hue = hue;
270
+ that.log("hue=" + hue)
271
+ callback(null, hue);
272
+ } else {
273
+ that.log("getHue fetchState else")
274
+ callback(communicationError);
275
+ }
276
+ });
277
+ },
278
+ getSaturation(callback) {
279
+ const that = this;
280
+ this.log(`fetching getSaturation for: ${this.name}`);
281
+ this.client.fetchState(this.entity_id, (data) => {
282
+ that.log(`getSaturation fetchState receved data for: ${this.name}`)
283
+ if (data) {
284
+ const rgb = data.attributes.rgb_color || [0, 0, 0];
285
+ const hsv = LightUtil.rgbToHsv(rgb[0], rgb[1], rgb[2]);
286
+
287
+ const saturation = hsv.s * 100;
288
+ that.data.attributes.saturation = saturation;
289
+
290
+ callback(null, saturation);
291
+ } else {
292
+ callback(communicationError);
293
+ }
294
+ });
295
+ },
296
+ getColorTemperature(callback) {
297
+ const that = this;
298
+ this.log(`fetching getColorTemperature for: ${this.name}`);
299
+ this.client.fetchState(this.entity_id, (data) => {
300
+ that.log(`getColorTemperature fetchState receved data for: ${this.name}`)
301
+ if (data) {
302
+ const colorTemp = Math.round(data.attributes.color_temp) || this.minTemp;
303
+ callback(null, colorTemp);
304
+ } else {
305
+ callback(communicationError);
306
+ }
307
+ });
308
+ },
309
+ setPowerState(powerOn, callback, context) {
310
+ if (context === 'internal') {
311
+ callback();
312
+ return;
313
+ }
314
+
315
+ const that = this;
316
+ const serviceData = {};
317
+ serviceData.entity_id = this.entity_id;
318
+ ControlPowerDictionary[serviceData.entity_id] = true;
319
+
320
+ if (powerOn) {
321
+ setTimeout(() => {
322
+ if (ControlPowerDictionary[serviceData.entity_id]) {
323
+ this.log(`Setting power state on the '${this.name}' to on`);
324
+ this.client.callService(this.domain, 'turn_on', serviceData, (data) => {
325
+ if (data) {
326
+ that.log(`Successfully set power state on the '${that.name}' to on`);
327
+ const powerState = data.state === 'on';
328
+ callback(null, powerState);
329
+ } else {
330
+ callback(communicationError);
331
+ }
332
+ });
333
+ } else {
334
+ const powerState = that.data.state === 'on';
335
+ callback(null, powerState);
336
+ }
337
+ }, 200);
338
+ } else {
339
+ this.log(`Setting power state on the '${this.name}' to off`);
340
+
341
+ this.client.callService(this.domain, 'turn_off', serviceData, (data) => {
342
+ if (data) {
343
+ that.log(`Successfully set power state on the '${that.name}' to off`);
344
+ callback();
345
+ } else {
346
+ callback(communicationError);
347
+ }
348
+ });
349
+ }
350
+ },
351
+ setBrightness(level, callback, context) {
352
+ if (context === 'internal') {
353
+ callback();
354
+ return;
355
+ }
356
+
357
+ const that = this;
358
+ const serviceData = {};
359
+ serviceData.entity_id = this.entity_id;
360
+
361
+ serviceData.brightness = level;
362
+
363
+ this.log(`Setting brightness on the '${this.name}' to ${level}`);
364
+ this.client.callService(this.domain, 'turn_on', serviceData, (data) => {
365
+ if (data) {
366
+ ControlPowerDictionary[serviceData.entity_id] = false;
367
+ that.log(`Successfully set brightness on the '${that.name}' to ${level}`);
368
+ const brightness = data.attributes.brightness || level;
369
+ callback(null, brightness);
370
+ } else {
371
+ callback(communicationError);
372
+ }
373
+ });
374
+ },
375
+ setHue(level, callback, context) {
376
+ this.log("setHue", level, context);
377
+ if (context === 'internal') {
378
+ callback();
379
+ return;
380
+ }
381
+
382
+ this.data.attributes.hue = level;
383
+
384
+ if (this.cachedColor) {
385
+ this._setColor(callback);
386
+ } else {
387
+ this.cachedColor = true;
388
+ callback();
389
+ }
390
+ },
391
+ setSaturation(level, callback, context) {
392
+ this.log("setSaturation", level, context);
393
+ if (context === 'internal') {
394
+ callback();
395
+ return;
396
+ }
397
+ this.data.attributes.saturation = level;
398
+
399
+ if (this.cachedColor) {
400
+ this._setColor(callback);
401
+ } else {
402
+ this.cachedColor = true;
403
+ callback();
404
+ }
405
+ },
406
+ _setColor(callback) {
407
+ const that = this;
408
+ this.cachedColor = false;
409
+ const serviceData = {};
410
+ serviceData.entity_id = this.entity_id;
411
+ const rgb = LightUtil.hsvToRgb(
412
+ (this.data.attributes.hue || 0) / 360,
413
+ (this.data.attributes.saturation || 0) / 100,
414
+ (this.data.attributes.brightness || 0) / 100
415
+ );
416
+ that.log("rgb(" + rgb.r + ", " + rgb.g + ", " + rgb.b + ")");
417
+ if (this.data.attributes.hue !== undefined) {
418
+ if (this.is_supported(this.features.XY_COLOR)) {
419
+ serviceData.xy_color = LightUtil.rgbToCie(rgb.r, rgb.g, rgb.b);
420
+ } else {
421
+ serviceData.rgb_color = [rgb.r.toString(), rgb.g.toString(), rgb.b.toString()];
422
+ }
423
+ }
424
+
425
+ this.client.callService(this.domain, 'turn_on', serviceData, (data) => {
426
+ if (data) {
427
+ if (that.is_supported(that.features.XY_COLOR)) {
428
+ that.log(`Successfully set xy on the '${that.name}' to ${serviceData.xy_color}`);
429
+ } else {
430
+ that.log(`Successfully set rgb on the '${that.name}' to ${serviceData.rgb_color}`);
431
+ }
432
+ callback();
433
+ } else {
434
+ callback(communicationError);
435
+ }
436
+ });
437
+ },
438
+ setColorTemperature(level, callback, context) {
439
+ this.log("setColorTemperature", level, context);
440
+ if (context === 'internal') {
441
+ callback();
442
+ return;
443
+ }
444
+
445
+ const that = this;
446
+ const serviceData = {};
447
+ serviceData.entity_id = this.entity_id;
448
+ serviceData.color_temp = Math.round(level);
449
+
450
+ this.client.callService(this.domain, 'turn_on', serviceData, (data) => {
451
+ if (data) {
452
+ that.log(`Successfully set color temperature on the '${that.name}' to ${serviceData.color_temp}`);
453
+ callback();
454
+ } else {
455
+ callback(communicationError);
456
+ }
457
+ });
458
+ },
459
+ getServices() {
460
+ this.lightbulbService = new Service.Lightbulb();
461
+ const informationService = new Service.AccessoryInformation();
462
+
463
+ informationService
464
+ .setCharacteristic(Characteristic.Manufacturer, this.manufacturer)
465
+ .setCharacteristic(Characteristic.Model, this.model)
466
+ .setCharacteristic(Characteristic.SerialNumber, this.serial)
467
+ .setCharacteristic(Characteristic.FirmwareRevision, this.firmware);
468
+
469
+ informationService
470
+ .setCharacteristic(Characteristic.Identify)
471
+ .on('set', this.identify.bind(this));
472
+
473
+ this.lightbulbService
474
+ .getCharacteristic(Characteristic.On)
475
+ .on('get', this.getPowerState.bind(this))
476
+ .on('set', this.setPowerState.bind(this));
477
+
478
+ if (this.is_supported(this.features.BRIGHTNESS)) {
479
+ this.lightbulbService
480
+ .addCharacteristic(Characteristic.Brightness)
481
+ .on('get', this.getBrightness.bind(this))
482
+ .on('set', this.setBrightness.bind(this));
483
+ }
484
+
485
+ if (this.is_supported(this.features.RGB_COLOR)) {
486
+ this.lightbulbService
487
+ .addCharacteristic(Characteristic.Hue)
488
+ .on('get', this.getHue.bind(this))
489
+ .on('set', this.setHue.bind(this));
490
+
491
+ this.lightbulbService
492
+ .addCharacteristic(Characteristic.Saturation)
493
+ .on('get', this.getSaturation.bind(this))
494
+ .on('set', this.setSaturation.bind(this));
495
+ }
496
+
497
+ if (this.is_supported(this.features.COLOR_TEMP)) {
498
+ this.lightbulbService
499
+ .addCharacteristic(Characteristic.ColorTemperature)
500
+ .setProps({ maxValue: this.maxTemp, minValue: this.minTemp })
501
+ .on('get', this.getColorTemperature.bind(this))
502
+ .on('set', this.setColorTemperature.bind(this));
503
+ }
504
+
505
+ return [informationService, this.lightbulbService];
506
+ },
507
+
508
+ };
509
+
510
+ function HomeAssistantLightPlatform(oService, oCharacteristic, oCommunicationError) {
511
+ Service = oService;
512
+ Characteristic = oCharacteristic;
513
+ communicationError = oCommunicationError;
514
+
515
+ return HomeAssistantLight;
516
+ }
517
+
518
+ module.exports = HomeAssistantLightPlatform;
519
+ module.exports.HomeAssistantLight = HomeAssistantLight;
@@ -0,0 +1,177 @@
1
+ 'use strict';
2
+
3
+ let Service;
4
+ let Characteristic;
5
+ let communicationError;
6
+
7
+ function HomeAssistantLock(log, data, client, firmware) {
8
+ // device info
9
+ this.domain = 'lock';
10
+ this.data = data;
11
+ this.entity_id = data.entity_id;
12
+ this.uuid_base = data.entity_id;
13
+ this.firmware = firmware;
14
+ if (data.attributes && data.attributes.friendly_name) {
15
+ this.name = data.attributes.friendly_name;
16
+ } else {
17
+ this.name = data.entity_id.split('.').pop().replace(/_/g, ' ');
18
+ }
19
+ if (data.attributes && data.attributes.homebridge_manufacturer) {
20
+ this.manufacturer = String(data.attributes.homebridge_manufacturer);
21
+ } else {
22
+ this.manufacturer = 'Home Assistant';
23
+ }
24
+ if (data.attributes && data.attributes.homebridge_model) {
25
+ this.model = String(data.attributes.homebridge_model);
26
+ } else {
27
+ this.model = 'Lock';
28
+ }
29
+ if (data.attributes && data.attributes.homebridge_serial) {
30
+ this.serial = String(data.attributes.homebridge_serial);
31
+ } else {
32
+ this.serial = data.entity_id;
33
+ }
34
+ this.client = client;
35
+ this.log = log;
36
+ this.lockCode = data.attributes.homebridge_lock_code;
37
+ this.batterySource = data.attributes.homebridge_battery_source;
38
+ this.chargingSource = data.attributes.homebridge_charging_source;
39
+ }
40
+
41
+ HomeAssistantLock.prototype = {
42
+ onEvent(oldState, newState) {
43
+ if (newState.state) {
44
+ const lockState = newState.state === 'unlocked' ? 0 : 1;
45
+ this.lockService.getCharacteristic(Characteristic.LockCurrentState)
46
+ .setValue(lockState, null, 'internal');
47
+ this.lockService.getCharacteristic(Characteristic.LockTargetState)
48
+ .setValue(lockState, null, 'internal');
49
+ }
50
+ },
51
+ getLockState(callback) {
52
+ this.client.fetchState(this.entity_id, (data) => {
53
+ if (data) {
54
+ const lockState = data.state === 'locked';
55
+ callback(null, lockState);
56
+ } else {
57
+ callback(communicationError);
58
+ }
59
+ });
60
+ },
61
+ getBatteryLevel(callback) {
62
+ this.client.fetchState(this.batterySource, (data) => {
63
+ if (data) {
64
+ callback(null, parseFloat(data.state));
65
+ } else {
66
+ callback(communicationError);
67
+ }
68
+ });
69
+ },
70
+ getChargingState(callback) {
71
+ if (this.batterySource && this.chargingSource) {
72
+ this.client.fetchState(this.chargingSource, (data) => {
73
+ if (data) {
74
+ callback(null, data.state.toLowerCase() === 'charging' ? 1 : 0);
75
+ } else {
76
+ callback(communicationError);
77
+ }
78
+ });
79
+ } else {
80
+ callback(null, 2);
81
+ }
82
+ },
83
+ getLowBatteryStatus(callback) {
84
+ this.client.fetchState(this.batterySource, (data) => {
85
+ if (data) {
86
+ callback(null, parseFloat(data.state) > 20 ? 0 : 1);
87
+ } else {
88
+ callback(communicationError);
89
+ }
90
+ });
91
+ },
92
+ setLockState(lockOn, callback, context) {
93
+ if (context === 'internal') {
94
+ callback();
95
+ return;
96
+ }
97
+
98
+ const that = this;
99
+ const serviceData = {};
100
+ serviceData.entity_id = this.entity_id;
101
+ if (this.lockCode) {
102
+ serviceData.code = this.lockCode;
103
+ }
104
+
105
+ if (lockOn) {
106
+ this.log(`Setting lock state on the '${this.name}' to locked`);
107
+
108
+ this.client.callService(this.domain, 'lock', serviceData, (data) => {
109
+ if (data) {
110
+ that.log(`Successfully set lock state on the '${that.name}' to locked`);
111
+ callback();
112
+ } else {
113
+ callback(communicationError);
114
+ }
115
+ });
116
+ } else {
117
+ this.log(`Setting lock state on the '${this.name}' to unlocked`);
118
+
119
+ this.client.callService(this.domain, 'unlock', serviceData, (data) => {
120
+ if (data) {
121
+ that.log(`Successfully set lock state on the '${that.name}' to unlocked`);
122
+ callback();
123
+ } else {
124
+ callback(communicationError);
125
+ }
126
+ });
127
+ }
128
+ },
129
+ getServices() {
130
+ this.lockService = new Service.LockMechanism();
131
+ const informationService = new Service.AccessoryInformation();
132
+
133
+ informationService
134
+ .setCharacteristic(Characteristic.Manufacturer, this.manufacturer)
135
+ .setCharacteristic(Characteristic.Model, this.model)
136
+ .setCharacteristic(Characteristic.SerialNumber, this.serial)
137
+ .setCharacteristic(Characteristic.FirmwareRevision, this.firmware);
138
+
139
+ this.lockService
140
+ .getCharacteristic(Characteristic.LockCurrentState)
141
+ .on('get', this.getLockState.bind(this));
142
+
143
+ this.lockService
144
+ .getCharacteristic(Characteristic.LockTargetState)
145
+ .on('get', this.getLockState.bind(this))
146
+ .on('set', this.setLockState.bind(this));
147
+
148
+ if (this.batterySource) {
149
+ this.batteryService = new Service.BatteryService();
150
+ this.batteryService
151
+ .getCharacteristic(Characteristic.BatteryLevel)
152
+ .setProps({ maxValue: 100, minValue: 0, minStep: 1 })
153
+ .on('get', this.getBatteryLevel.bind(this));
154
+ this.batteryService
155
+ .getCharacteristic(Characteristic.ChargingState)
156
+ .setProps({ maxValue: 2 })
157
+ .on('get', this.getChargingState.bind(this));
158
+ this.batteryService
159
+ .getCharacteristic(Characteristic.StatusLowBattery)
160
+ .on('get', this.getLowBatteryStatus.bind(this));
161
+ return [informationService, this.lockService, this.batteryService];
162
+ }
163
+ return [informationService, this.lockService];
164
+ },
165
+
166
+ };
167
+
168
+ function HomeAssistantLockPlatform(oService, oCharacteristic, oCommunicationError) {
169
+ Service = oService;
170
+ Characteristic = oCharacteristic;
171
+ communicationError = oCommunicationError;
172
+
173
+ return HomeAssistantLock;
174
+ }
175
+
176
+ module.exports = HomeAssistantLockPlatform;
177
+ module.exports.HomeAssistantLock = HomeAssistantLock;