@switchbot/homebridge-switchbot 2.0.0-beta.19 → 2.0.0-beta.20

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 (112) hide show
  1. package/CHANGELOG.md +6 -1
  2. package/README.md +1 -2
  3. package/config.schema.json +51 -32
  4. package/dist/device/bot.d.ts +32 -29
  5. package/dist/device/bot.d.ts.map +1 -1
  6. package/dist/device/bot.js +549 -575
  7. package/dist/device/bot.js.map +1 -1
  8. package/dist/device/colorbulb.d.ts +33 -15
  9. package/dist/device/colorbulb.d.ts.map +1 -1
  10. package/dist/device/colorbulb.js +426 -341
  11. package/dist/device/colorbulb.js.map +1 -1
  12. package/dist/device/contact.d.ts +16 -9
  13. package/dist/device/contact.d.ts.map +1 -1
  14. package/dist/device/contact.js +246 -208
  15. package/dist/device/contact.js.map +1 -1
  16. package/dist/device/curtain.d.ts +21 -18
  17. package/dist/device/curtain.d.ts.map +1 -1
  18. package/dist/device/curtain.js +449 -449
  19. package/dist/device/curtain.js.map +1 -1
  20. package/dist/device/humidifier.d.ts +22 -20
  21. package/dist/device/humidifier.d.ts.map +1 -1
  22. package/dist/device/humidifier.js +346 -324
  23. package/dist/device/humidifier.js.map +1 -1
  24. package/dist/device/lock.d.ts +22 -10
  25. package/dist/device/lock.d.ts.map +1 -1
  26. package/dist/device/lock.js +257 -178
  27. package/dist/device/lock.js.map +1 -1
  28. package/dist/device/meter.d.ts +21 -14
  29. package/dist/device/meter.d.ts.map +1 -1
  30. package/dist/device/meter.js +287 -239
  31. package/dist/device/meter.js.map +1 -1
  32. package/dist/device/meterplus.d.ts +23 -10
  33. package/dist/device/meterplus.d.ts.map +1 -1
  34. package/dist/device/meterplus.js +308 -190
  35. package/dist/device/meterplus.js.map +1 -1
  36. package/dist/device/motion.d.ts +17 -10
  37. package/dist/device/motion.d.ts.map +1 -1
  38. package/dist/device/motion.js +225 -193
  39. package/dist/device/motion.js.map +1 -1
  40. package/dist/device/plug.d.ts +24 -17
  41. package/dist/device/plug.d.ts.map +1 -1
  42. package/dist/device/plug.js +270 -267
  43. package/dist/device/plug.js.map +1 -1
  44. package/dist/device/striplight.d.ts +36 -14
  45. package/dist/device/striplight.d.ts.map +1 -1
  46. package/dist/device/striplight.js +447 -299
  47. package/dist/device/striplight.js.map +1 -1
  48. package/dist/irdevice/airconditioner.d.ts +7 -14
  49. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  50. package/dist/irdevice/airconditioner.js +177 -169
  51. package/dist/irdevice/airconditioner.js.map +1 -1
  52. package/dist/irdevice/airpurifier.d.ts +3 -5
  53. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  54. package/dist/irdevice/airpurifier.js +72 -57
  55. package/dist/irdevice/airpurifier.js.map +1 -1
  56. package/dist/irdevice/camera.d.ts +3 -4
  57. package/dist/irdevice/camera.d.ts.map +1 -1
  58. package/dist/irdevice/camera.js +47 -33
  59. package/dist/irdevice/camera.js.map +1 -1
  60. package/dist/irdevice/fan.d.ts +3 -4
  61. package/dist/irdevice/fan.d.ts.map +1 -1
  62. package/dist/irdevice/fan.js +66 -48
  63. package/dist/irdevice/fan.js.map +1 -1
  64. package/dist/irdevice/light.d.ts +3 -4
  65. package/dist/irdevice/light.d.ts.map +1 -1
  66. package/dist/irdevice/light.js +48 -47
  67. package/dist/irdevice/light.js.map +1 -1
  68. package/dist/irdevice/other.d.ts +3 -4
  69. package/dist/irdevice/other.d.ts.map +1 -1
  70. package/dist/irdevice/other.js +47 -33
  71. package/dist/irdevice/other.js.map +1 -1
  72. package/dist/irdevice/tv.d.ts +10 -11
  73. package/dist/irdevice/tv.d.ts.map +1 -1
  74. package/dist/irdevice/tv.js +64 -48
  75. package/dist/irdevice/tv.js.map +1 -1
  76. package/dist/irdevice/vacuumcleaner.d.ts +7 -12
  77. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  78. package/dist/irdevice/vacuumcleaner.js +52 -41
  79. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  80. package/dist/irdevice/waterheater.d.ts +6 -11
  81. package/dist/irdevice/waterheater.d.ts.map +1 -1
  82. package/dist/irdevice/waterheater.js +52 -42
  83. package/dist/irdevice/waterheater.js.map +1 -1
  84. package/dist/platform.d.ts +0 -4
  85. package/dist/platform.d.ts.map +1 -1
  86. package/dist/platform.js +44 -66
  87. package/dist/platform.js.map +1 -1
  88. package/dist/settings.d.ts +3 -3
  89. package/dist/settings.d.ts.map +1 -1
  90. package/package.json +7 -6
  91. package/src/device/bot.ts +551 -589
  92. package/src/device/colorbulb.ts +441 -375
  93. package/src/device/contact.ts +249 -213
  94. package/src/device/curtain.ts +452 -461
  95. package/src/device/humidifier.ts +358 -365
  96. package/src/device/lock.ts +263 -179
  97. package/src/device/meter.ts +302 -253
  98. package/src/device/meterplus.ts +319 -194
  99. package/src/device/motion.ts +230 -199
  100. package/src/device/plug.ts +273 -274
  101. package/src/device/striplight.ts +460 -316
  102. package/src/irdevice/airconditioner.ts +181 -183
  103. package/src/irdevice/airpurifier.ts +70 -60
  104. package/src/irdevice/camera.ts +46 -36
  105. package/src/irdevice/fan.ts +64 -49
  106. package/src/irdevice/light.ts +47 -51
  107. package/src/irdevice/other.ts +46 -36
  108. package/src/irdevice/tv.ts +64 -51
  109. package/src/irdevice/vacuumcleaner.ts +52 -45
  110. package/src/irdevice/waterheater.ts +50 -45
  111. package/src/platform.ts +44 -68
  112. package/src/settings.ts +3 -3
@@ -20,25 +20,16 @@ class ColorBulb {
20
20
  this.platform = platform;
21
21
  this.accessory = accessory;
22
22
  this.device = device;
23
+ // Connection
24
+ this.BLE = (this.device.connectionType === 'BLE' || this.device.connectionType === 'Both');
25
+ this.OpenAPI = (this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'Both');
23
26
  // default placeholders
24
27
  this.logs(device);
25
- this.openAPI();
26
28
  this.scan(device);
27
29
  this.refreshRate(device);
28
30
  this.adaptiveLighting(device);
29
31
  this.config(device);
30
- if (this.On === undefined) {
31
- this.On = false;
32
- }
33
- else {
34
- this.On = this.accessory.context.On;
35
- }
36
- this.Hue = 0;
37
- this.Brightness = 0;
38
- this.Saturation = 0;
39
- this.ColorTemperature = 140;
40
- this.minKelvin = 2000;
41
- this.maxKelvin = 9000;
32
+ this.context();
42
33
  // this is subject we use to track when we need to POST changes to the SwitchBot API
43
34
  this.doColorBulbUpdate = new rxjs_1.Subject();
44
35
  this.colorBulbUpdateInProgress = false;
@@ -150,12 +141,9 @@ class ColorBulb {
150
141
  await this.pushChanges();
151
142
  }
152
143
  catch (e) {
153
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges`);
154
- if (this.deviceLogging.includes('debug')) {
155
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges,`
156
- + ` Error Message: ${(0, super_stringify_1.default)(e.message)}`);
157
- }
158
144
  this.apiError(e);
145
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with ${this.device.connectionType} Connection,`
146
+ + ` Error Message: ${(0, super_stringify_1.default)(e.message)}`);
159
147
  }
160
148
  this.colorBulbUpdateInProgress = false;
161
149
  // Refresh the status from the API
@@ -167,16 +155,23 @@ class ColorBulb {
167
155
  });
168
156
  });
169
157
  }
158
+ /**
159
+ * Parse the device status from the SwitchBot api
160
+ */
170
161
  async parseStatus() {
171
- if (this.SwitchToOpenAPI || !this.device.ble) {
162
+ /*if (this.BLE && !this.SwitchToOpenAPI) {
163
+ await this.BLEparseStatus();
164
+ } else*/ if (this.OpenAPI && this.platform.config.credentials?.token) {
165
+ this.SwitchToOpenAPI = false;
172
166
  await this.openAPIparseStatus();
173
167
  }
174
168
  else {
175
- await this.BLEparseStatus();
169
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
170
+ + ` ${this.device.connectionType}, parseStatus will not happen.`);
176
171
  }
177
172
  }
178
173
  async BLEparseStatus() {
179
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE parseStatus`);
174
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
180
175
  // State
181
176
  switch (this.state) {
182
177
  case 'on':
@@ -188,6 +183,7 @@ class ColorBulb {
188
183
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
189
184
  }
190
185
  async openAPIparseStatus() {
186
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
191
187
  switch (this.power) {
192
188
  case 'on':
193
189
  this.On = true;
@@ -225,16 +221,22 @@ class ColorBulb {
225
221
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ColorTemperature: ${this.ColorTemperature}`);
226
222
  }
227
223
  }
224
+ /**
225
+ * Asks the SwitchBot API for the latest device information
226
+ */
228
227
  async refreshStatus() {
229
- if (this.device.ble) {
230
- await this.BLERefreshStatus();
228
+ /*if (this.BLE) {
229
+ await this.BLERefreshStatus();
230
+ } else*/ if (this.OpenAPI && this.platform.config.credentials?.token) {
231
+ await this.openAPIRefreshStatus();
231
232
  }
232
233
  else {
233
- await this.openAPIRefreshStatus();
234
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
235
+ + ` ${this.device.connectionType}, refreshStatus will not happen.`);
234
236
  }
235
237
  }
236
238
  async BLERefreshStatus() {
237
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE refreshStatus`);
239
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLERefreshStatus`);
238
240
  const switchbot = await this.platform.connectBLE();
239
241
  // Convert to BLE Address
240
242
  this.device.bleMac = this.device
@@ -295,214 +297,223 @@ class ColorBulb {
295
297
  this.updateHomeKitCharacteristics();
296
298
  }
297
299
  else {
298
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
299
- if (this.platform.config.credentials?.token) {
300
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection`);
301
- this.SwitchToOpenAPI = true;
302
- await this.openAPIRefreshStatus();
303
- }
300
+ await this.BLEconnection(switchbot);
304
301
  }
305
302
  })
306
303
  .catch(async (e) => {
307
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
308
- if (this.deviceLogging.includes('debug')) {
309
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
310
- + ` Error Message: ${(0, super_stringify_1.default)(e.message)}`);
311
- }
312
- if (this.platform.config.credentials?.token) {
313
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection`);
314
- this.SwitchToOpenAPI = true;
315
- await this.openAPIRefreshStatus();
316
- }
317
304
  this.apiError(e);
305
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLERefreshStatus with ${this.device.connectionType}`
306
+ + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
307
+ await this.BLEconnection(switchbot);
318
308
  });
319
309
  }
320
310
  else {
321
311
  await this.BLEconnection(switchbot);
322
312
  }
323
313
  }
324
- async getCustomBLEAddress(switchbot) {
325
- if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
326
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} customBLEaddress: ${this.device.customBLEaddress}`);
327
- (async () => {
328
- // Start to monitor advertisement packets
329
- await switchbot.startScan({
330
- model: 'u',
314
+ async openAPIRefreshStatus() {
315
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
316
+ try {
317
+ const t = Date.now();
318
+ const nonce = 'requestID';
319
+ const data = this.platform.config.credentials?.token + t + nonce;
320
+ const signTerm = crypto_1.default.createHmac('sha256', this.platform.config.credentials?.secret).update(Buffer.from(data, 'utf-8')).digest();
321
+ const sign = signTerm.toString('base64');
322
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
323
+ const options = {
324
+ hostname: settings_1.HostDomain,
325
+ port: 443,
326
+ path: `${settings_1.DevicePath}/${this.device.deviceId}/status`,
327
+ method: 'GET',
328
+ headers: {
329
+ Authorization: this.platform.config.credentials?.token,
330
+ sign: sign,
331
+ nonce: nonce,
332
+ t: t,
333
+ 'Content-Type': 'application/json',
334
+ },
335
+ };
336
+ const req = https_1.default.request(options, (res) => {
337
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus statusCode: ${res.statusCode}`);
338
+ this.statusCode({ res });
339
+ let rawData = '';
340
+ res.on('data', (d) => {
341
+ rawData += d;
342
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
331
343
  });
332
- // Set an event handler
333
- switchbot.onadvertisement = (ad) => {
334
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${(0, super_stringify_1.default)(ad, null, ' ')}`);
335
- };
336
- await switchbot.wait(10000);
337
- // Stop to monitor
338
- switchbot.stopScan();
339
- })();
344
+ res.on('end', () => {
345
+ try {
346
+ this.deviceStatus = JSON.parse(rawData);
347
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus: ${(0, super_stringify_1.default)(this.deviceStatus)}`);
348
+ this.power = this.deviceStatus.body.power;
349
+ this.color = this.deviceStatus.body.color;
350
+ this.brightness = this.deviceStatus.body.brightness;
351
+ this.colorTemperature = this.deviceStatus.body.colorTemperature;
352
+ this.parseStatus();
353
+ this.updateHomeKitCharacteristics();
354
+ }
355
+ catch (e) {
356
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
357
+ }
358
+ });
359
+ });
360
+ req.on('error', (e) => {
361
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
362
+ });
363
+ req.end();
364
+ }
365
+ catch (e) {
366
+ this.apiError(e);
367
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}`
368
+ + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
340
369
  }
341
370
  }
342
- async BLEconnection(switchbot) {
343
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
344
- if (this.platform.config.credentials?.token) {
345
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection`);
346
- this.SwitchToOpenAPI = true;
347
- await this.openAPIRefreshStatus();
371
+ /**
372
+ * Pushes the requested changes to the SwitchBot API
373
+ * deviceType commandType Command command parameter Description
374
+ * Color Bulb - "command" "turnOff" "default" = set to OFF state
375
+ * Color Bulb - "command" "turnOn" "default" = set to ON state
376
+ * Color Bulb - "command" "toggle" "default" = toggle state
377
+ * Color Bulb - "command" "setBrightness" "{1-100}" = set brightness
378
+ * Color Bulb - "command" "setColor" "{0-255}:{0-255}:{0-255}" = set RGB color value
379
+ * Color Bulb - "command" "setColorTemperature" "{2700-6500}" = set color temperature
380
+ *
381
+ */
382
+ async pushChanges() {
383
+ /*if (this.BLE) {
384
+ await this.BLERefreshStatus();
385
+ } else*/ if (this.OpenAPI && this.platform.config.credentials?.token) {
386
+ await this.openAPIpushChanges();
387
+ }
388
+ else {
389
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
390
+ + ` ${this.device.connectionType}, pushChanges will not happen.`);
348
391
  }
392
+ (0, rxjs_1.interval)(5000)
393
+ .pipe((0, operators_1.take)(1))
394
+ .subscribe(async () => {
395
+ await this.refreshStatus();
396
+ });
349
397
  }
350
- async openAPIRefreshStatus() {
351
- if (this.OpenAPI) {
352
- try {
398
+ async BLEpushChanges() {
399
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`);
400
+ if (this.On !== this.accessory.context.On) {
401
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges On: ${this.On} OnCached: ${this.accessory.context.On}`);
402
+ const switchbot = await this.platform.connectBLE();
403
+ // Convert to BLE Address
404
+ this.device.bleMac = this.device
405
+ .deviceId.match(/.{1,2}/g)
406
+ .join(':')
407
+ .toLowerCase();
408
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
409
+ switchbot
410
+ .discover({
411
+ model: 'u',
412
+ id: this.device.bleMac,
413
+ })
414
+ .then((device_list) => {
415
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
416
+ return this.turnOnOff(device_list);
417
+ })
418
+ .then(() => {
419
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
420
+ this.On = false;
421
+ })
422
+ .catch(async (e) => {
423
+ this.apiError(e);
424
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushChanges with ${this.device.connectionType}`
425
+ + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
426
+ if (this.platform.config.credentials?.token && this.device.connectionType !== 'Both') {
427
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection`);
428
+ await this.openAPIpushChanges();
429
+ }
430
+ });
431
+ }
432
+ else {
433
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushChanges.` + `On: ${this.On}, `
434
+ + `OnCached: ${this.accessory.context.On}`);
435
+ }
436
+ }
437
+ async openAPIpushChanges() {
438
+ try {
439
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges`);
440
+ if (this.On !== this.accessory.context.On) {
441
+ // Make Push On request to the API
353
442
  const t = Date.now();
354
443
  const nonce = 'requestID';
355
444
  const data = this.platform.config.credentials?.token + t + nonce;
356
- const signTerm = crypto_1.default.createHmac('sha256', this.platform.config.credentials?.secret).update(Buffer.from(data, 'utf-8')).digest();
445
+ const signTerm = crypto_1.default.createHmac('sha256', this.platform.config.credentials?.secret)
446
+ .update(Buffer.from(data, 'utf-8'))
447
+ .digest();
357
448
  const sign = signTerm.toString('base64');
358
449
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
450
+ let command = '';
451
+ if (this.On) {
452
+ command = 'turnOn';
453
+ }
454
+ else {
455
+ command = 'turnOff';
456
+ }
457
+ const body = (0, super_stringify_1.default)({
458
+ 'command': `${command}`,
459
+ 'parameter': 'default',
460
+ 'commandType': 'command',
461
+ });
462
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API. body: ${body},`);
359
463
  const options = {
360
464
  hostname: settings_1.HostDomain,
361
465
  port: 443,
362
- path: `${settings_1.DevicePath}/${this.device.deviceId}/status`,
363
- method: 'GET',
466
+ path: `${settings_1.DevicePath}/${this.device.deviceId}/commands`,
467
+ method: 'POST',
364
468
  headers: {
365
- Authorization: this.platform.config.credentials?.token,
366
- sign: sign,
367
- nonce: nonce,
368
- t: t,
469
+ 'Authorization': this.platform.config.credentials?.token,
470
+ 'sign': sign,
471
+ 'nonce': nonce,
472
+ 't': t,
369
473
  'Content-Type': 'application/json',
474
+ 'Content-Length': body.length,
370
475
  },
371
476
  };
372
- const req = https_1.default.request(options, (res) => {
373
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Refresh statusCode: ${res.statusCode}`);
374
- let rawData = '';
375
- res.on('data', (d) => {
376
- rawData += d;
477
+ const req = https_1.default.request(options, res => {
478
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges statusCode: ${res.statusCode}`);
479
+ this.statusCode({ res });
480
+ res.on('data', d => {
377
481
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
378
482
  });
379
- res.on('end', () => {
380
- try {
381
- this.deviceStatus = JSON.parse(rawData);
382
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} refreshStatus: ${(0, super_stringify_1.default)(this.deviceStatus)}`);
383
- this.power = this.deviceStatus.body.power;
384
- this.color = this.deviceStatus.body.color;
385
- this.brightness = this.deviceStatus.body.brightness;
386
- this.colorTemperature = this.deviceStatus.body.colorTemperature;
387
- this.parseStatus();
388
- this.updateHomeKitCharacteristics();
389
- }
390
- catch (e) {
391
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
392
- }
393
- });
394
483
  });
395
484
  req.on('error', (e) => {
396
485
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
397
486
  });
487
+ req.write(body);
398
488
  req.end();
489
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushChanges: ${(0, super_stringify_1.default)(req)}`);
399
490
  }
400
- catch (e) {
401
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection`);
402
- if (this.deviceLogging.includes('debug')) {
403
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,`
404
- + ` Error Message: ${(0, super_stringify_1.default)(e.message)}`);
405
- }
406
- this.apiError(e);
491
+ else {
492
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No openAPIpushChanges.` + `On: ${this.On}, `
493
+ + `OnCached: ${this.accessory.context.On}`);
407
494
  }
408
- }
409
- else {
410
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} OpenAPI is disabled, commands will not be sent.`);
411
- }
412
- }
413
- /**
414
- * Pushes the requested changes to the SwitchBot API
415
- * deviceType commandType Command command parameter Description
416
- * Color Bulb - "command" "turnOff" "default" = set to OFF state
417
- * Color Bulb - "command" "turnOn" "default" = set to ON state
418
- * Color Bulb - "command" "toggle" "default" = toggle state
419
- * Color Bulb - "command" "setBrightness" "{1-100}" = set brightness
420
- * Color Bulb - "command" "setColor" "{0-255}:{0-255}:{0-255}" = set RGB color value
421
- * Color Bulb - "command" "setColorTemperature" "{2700-6500}" = set color temperature
422
- *
423
- */
424
- async pushChanges() {
425
- if (this.OpenAPI) {
426
- try {
427
- if (this.On !== this.accessory.context.On) {
428
- // Make Push On request to the API
429
- const t = Date.now();
430
- const nonce = 'requestID';
431
- const data = this.platform.config.credentials?.token + t + nonce;
432
- const signTerm = crypto_1.default.createHmac('sha256', this.platform.config.credentials?.secret)
433
- .update(Buffer.from(data, 'utf-8'))
434
- .digest();
435
- const sign = signTerm.toString('base64');
436
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
437
- let command = '';
438
- if (this.On) {
439
- command = 'turnOn';
440
- }
441
- else {
442
- command = 'turnOff';
443
- }
444
- const body = (0, super_stringify_1.default)({
445
- 'command': `${command}`,
446
- 'parameter': 'default',
447
- 'commandType': 'command',
448
- });
449
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API. body: ${body},`);
450
- const options = {
451
- hostname: settings_1.HostDomain,
452
- port: 443,
453
- path: `${settings_1.DevicePath}/${this.device.deviceId}/commands`,
454
- method: 'POST',
455
- headers: {
456
- 'Authorization': this.platform.config.credentials?.token,
457
- 'sign': sign,
458
- 'nonce': nonce,
459
- 't': t,
460
- 'Content-Type': 'application/json',
461
- 'Content-Length': body.length,
462
- },
463
- };
464
- const req = https_1.default.request(options, res => {
465
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${res.statusCode}`);
466
- this.statusCode({ res });
467
- res.on('data', d => {
468
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
469
- });
470
- });
471
- req.on('error', (e) => {
472
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
473
- });
474
- req.write(body);
475
- req.end();
476
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushChanges: ${(0, super_stringify_1.default)(req)}`);
477
- }
478
- // Push Brightness Update
479
- if (this.On) {
480
- await this.pushBrightnessChanges();
481
- }
482
- // Push ColorTemperature Update
483
- if (this.On) {
484
- await this.pushColorTemperatureChanges();
485
- }
486
- // Push Hue & Saturation Update
487
- if (this.On) {
488
- await this.pushHueSaturationChanges();
489
- }
495
+ // Push Brightness Update
496
+ if (this.On) {
497
+ await this.pushBrightnessChanges();
490
498
  }
491
- catch (e) {
492
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
493
- if (this.deviceLogging.includes('debug')) {
494
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
495
- + ` Error Message: ${(0, super_stringify_1.default)(e.message)}`);
496
- }
497
- this.apiError(e);
499
+ // Push ColorTemperature Update
500
+ if (this.On) {
501
+ await this.pushColorTemperatureChanges();
502
+ }
503
+ // Push Hue & Saturation Update
504
+ if (this.On) {
505
+ await this.pushHueSaturationChanges();
498
506
  }
499
507
  }
500
- else {
501
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} OpenAPI is disabled, commands will not be sent.`);
508
+ catch (e) {
509
+ this.apiError(e);
510
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIpushChanges with ${this.device.connectionType}`
511
+ + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
502
512
  }
503
513
  }
504
514
  async pushHueSaturationChanges() {
505
515
  try {
516
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushHueSaturationChanges`);
506
517
  if (this.Hue !== this.accessory.context.Hue || this.Saturation !== this.accessory.context.Saturation) {
507
518
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${(0, super_stringify_1.default)(this.Hue)}`);
508
519
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${(0, super_stringify_1.default)(this.Saturation)}`);
@@ -538,7 +549,7 @@ class ColorBulb {
538
549
  },
539
550
  };
540
551
  const req = https_1.default.request(options, res => {
541
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${res.statusCode}`);
552
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushHueSaturationChanges statusCode: ${res.statusCode}`);
542
553
  res.on('data', d => {
543
554
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
544
555
  });
@@ -551,22 +562,19 @@ class ColorBulb {
551
562
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushHueSaturationChanges: ${(0, super_stringify_1.default)(req)}`);
552
563
  }
553
564
  else {
554
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes.` +
555
- `Hue: ${this.Hue}, HueCached: ${this.accessory.context.Hue}, Saturation: ${this.Saturation},`
556
- + ` SaturationCached: ${this.accessory.context.Saturation}`);
565
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushHueSaturationChanges. Hue: ${this.Hue}, `
566
+ + `HueCached: ${this.accessory.context.Hue}, Saturation: ${this.Saturation}, SaturationCached: ${this.accessory.context.Saturation}`);
557
567
  }
558
568
  }
559
569
  catch (e) {
560
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
561
- if (this.deviceLogging.includes('debug')) {
562
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
563
- + ` Error Message: ${(0, super_stringify_1.default)(e.message)}`);
564
- }
565
570
  this.apiError(e);
571
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushHueSaturationChanges with ${this.device.connectionType}`
572
+ + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
566
573
  }
567
574
  }
568
575
  async pushColorTemperatureChanges() {
569
576
  try {
577
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushColorTemperatureChanges`);
570
578
  if (this.ColorTemperature !== this.accessory.context.ColorTemperature) {
571
579
  const kelvin = Math.round(1000000 / Number(this.ColorTemperature));
572
580
  this.cacheKelvin = kelvin;
@@ -614,21 +622,19 @@ class ColorBulb {
614
622
  this.accessory.context.ColorTemperature = this.ColorTemperature;
615
623
  }
616
624
  else {
617
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes.` +
625
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushColorTemperatureChanges.` +
618
626
  `ColorTemperature: ${this.ColorTemperature}, ColorTemperatureCached: ${this.accessory.context.ColorTemperature}`);
619
627
  }
620
628
  }
621
629
  catch (e) {
622
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
623
- if (this.deviceLogging.includes('debug')) {
624
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
625
- + ` Error Message: ${(0, super_stringify_1.default)(e.message)}`);
626
- }
627
630
  this.apiError(e);
631
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushColorTemperatureChanges with ${this.device.connectionType}`
632
+ + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
628
633
  }
629
634
  }
630
635
  async pushBrightnessChanges() {
631
636
  try {
637
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushBrightnessChanges`);
632
638
  if (this.Brightness !== this.accessory.context.Brightness) {
633
639
  // Make Push On request to the API
634
640
  const t = Date.now();
@@ -660,7 +666,7 @@ class ColorBulb {
660
666
  },
661
667
  };
662
668
  const req = https_1.default.request(options, res => {
663
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${res.statusCode}`);
669
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushBrightnessChanges statusCode: ${res.statusCode}`);
664
670
  res.on('data', d => {
665
671
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
666
672
  });
@@ -673,18 +679,68 @@ class ColorBulb {
673
679
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushBrightnessChanges: ${(0, super_stringify_1.default)(req)}`);
674
680
  }
675
681
  else {
676
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes.` + `Brightness: ${this.Brightness}, `
682
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushBrightnessChanges.` + `Brightness: ${this.Brightness}, `
677
683
  + `BrightnessCached: ${this.accessory.context.Brightness}`);
678
684
  }
679
685
  }
680
686
  catch (e) {
681
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
682
- if (this.deviceLogging.includes('debug')) {
683
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
684
- + ` Error Message: ${(0, super_stringify_1.default)(e.message)}`);
685
- }
686
687
  this.apiError(e);
688
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushBrightnessChanges with ${this.device.connectionType}`
689
+ + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
690
+ }
691
+ }
692
+ /**
693
+ * Handle requests to set the value of the "On" characteristic
694
+ */
695
+ async OnSet(value) {
696
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set On: ${value}`);
697
+ this.On = value;
698
+ this.doColorBulbUpdate.next();
699
+ }
700
+ /**
701
+ * Handle requests to set the value of the "Brightness" characteristic
702
+ */
703
+ async BrightnessSet(value) {
704
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Brightness: ${value}`);
705
+ this.Brightness = value;
706
+ this.doColorBulbUpdate.next();
707
+ }
708
+ /**
709
+ * Handle requests to set the value of the "ColorTemperature" characteristic
710
+ */
711
+ async ColorTemperatureSet(value) {
712
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set ColorTemperature: ${value}`);
713
+ // Convert mired to kelvin to nearest 100 (SwitchBot seems to need this)
714
+ const kelvin = Math.round(1000000 / Number(value) / 100) * 100;
715
+ // Check and increase/decrease kelvin to range of device
716
+ const k = Math.min(Math.max(kelvin, this.minKelvin), this.maxKelvin);
717
+ if (!this.accessory.context.On || this.cacheKelvin === k) {
718
+ return;
687
719
  }
720
+ // Updating the hue/sat to the corresponding values mimics native adaptive lighting
721
+ const hs = (0, settings_1.m2hs)(value);
722
+ this.lightBulbService.updateCharacteristic(this.platform.Characteristic.Hue, hs[0]);
723
+ this.lightBulbService.updateCharacteristic(this.platform.Characteristic.Saturation, hs[1]);
724
+ this.ColorTemperature = value;
725
+ this.doColorBulbUpdate.next();
726
+ }
727
+ /**
728
+ * Handle requests to set the value of the "Hue" characteristic
729
+ */
730
+ async HueSet(value) {
731
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Hue: ${value}`);
732
+ this.lightBulbService.updateCharacteristic(this.platform.Characteristic.ColorTemperature, 140);
733
+ this.Hue = value;
734
+ this.doColorBulbUpdate.next();
735
+ }
736
+ /**
737
+ * Handle requests to set the value of the "Saturation" characteristic
738
+ */
739
+ async SaturationSet(value) {
740
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Saturation: ${value}`);
741
+ this.lightBulbService.updateCharacteristic(this.platform.Characteristic.ColorTemperature, 140);
742
+ this.Saturation = value;
743
+ this.doColorBulbUpdate.next();
688
744
  }
689
745
  async updateHomeKitCharacteristics() {
690
746
  if (this.On === undefined) {
@@ -728,13 +784,98 @@ class ColorBulb {
728
784
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Saturation: ${this.Saturation}`);
729
785
  }
730
786
  }
731
- apiError(e) {
732
- this.lightBulbService.updateCharacteristic(this.platform.Characteristic.On, e);
733
- this.lightBulbService.updateCharacteristic(this.platform.Characteristic.Hue, e);
734
- this.lightBulbService.updateCharacteristic(this.platform.Characteristic.Brightness, e);
735
- this.lightBulbService.updateCharacteristic(this.platform.Characteristic.Saturation, e);
736
- this.lightBulbService.updateCharacteristic(this.platform.Characteristic.ColorTemperature, e);
737
- //throw new this.platform.api.hap.HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
787
+ async adaptiveLighting(device) {
788
+ if (device.colorbulb?.adaptiveLightingShift) {
789
+ this.adaptiveLightingShift = device.colorbulb.adaptiveLightingShift;
790
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} adaptiveLightingShift: ${this.adaptiveLightingShift}`);
791
+ }
792
+ else {
793
+ this.adaptiveLightingShift = 0;
794
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} adaptiveLightingShift: ${this.adaptiveLightingShift}`);
795
+ }
796
+ }
797
+ async getCustomBLEAddress(switchbot) {
798
+ if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
799
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} customBLEaddress: ${this.device.customBLEaddress}`);
800
+ (async () => {
801
+ // Start to monitor advertisement packets
802
+ await switchbot.startScan({
803
+ model: 'u',
804
+ });
805
+ // Set an event handler
806
+ switchbot.onadvertisement = (ad) => {
807
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${(0, super_stringify_1.default)(ad, null, ' ')}`);
808
+ };
809
+ await switchbot.wait(10000);
810
+ // Stop to monitor
811
+ switchbot.stopScan();
812
+ })();
813
+ }
814
+ }
815
+ async BLEconnection(switchbot) {
816
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
817
+ if (this.platform.config.credentials?.token) {
818
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection`);
819
+ this.SwitchToOpenAPI = true;
820
+ await this.openAPIRefreshStatus();
821
+ }
822
+ }
823
+ async turnOnOff(device_list) {
824
+ return await this.retry({
825
+ max: await this.maxRetry(),
826
+ fn: () => {
827
+ if (this.On) {
828
+ return device_list[0].turnOn({ id: this.device.bleMac });
829
+ }
830
+ else {
831
+ return device_list[0].turnOff({ id: this.device.bleMac });
832
+ }
833
+ },
834
+ });
835
+ }
836
+ async retry({ max, fn }) {
837
+ return fn().catch(async (err) => {
838
+ if (max === 0) {
839
+ throw err;
840
+ }
841
+ this.infoLog(err);
842
+ this.infoLog('Retrying');
843
+ await this.switchbot.wait(1000);
844
+ return this.retry({ max: max - 1, fn });
845
+ });
846
+ }
847
+ async maxRetry() {
848
+ let maxRetry;
849
+ if (this.device.bot?.maxRetry) {
850
+ maxRetry = this.device.bot?.maxRetry;
851
+ }
852
+ else {
853
+ maxRetry = 5;
854
+ }
855
+ return maxRetry;
856
+ }
857
+ minStep(device) {
858
+ if (device.colorbulb?.set_minStep) {
859
+ this.set_minStep = device.colorbulb?.set_minStep;
860
+ }
861
+ else {
862
+ this.set_minStep = 1;
863
+ }
864
+ return this.set_minStep;
865
+ }
866
+ async scan(device) {
867
+ if (device.scanDuration) {
868
+ this.scanDuration = this.accessory.context.scanDuration = device.scanDuration;
869
+ if (this.BLE) {
870
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
871
+ }
872
+ }
873
+ else {
874
+ this.scanDuration = this.accessory.context.scanDuration = 1;
875
+ if (this.BLE) {
876
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
877
+ }
878
+ }
738
879
  }
739
880
  async statusCode({ res }) {
740
881
  switch (res.statusCode) {
@@ -774,87 +915,66 @@ class ColorBulb {
774
915
  if (this.device.offline) {
775
916
  this.On = false;
776
917
  this.lightBulbService.getCharacteristic(this.platform.Characteristic.On).updateValue(this.On);
918
+ await this.updateHomeKitCharacteristics();
777
919
  }
778
920
  }
779
- /**
780
- * Handle requests to set the value of the "On" characteristic
781
- */
782
- async OnSet(value) {
783
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${value}`);
784
- this.On = value;
785
- this.doColorBulbUpdate.next();
786
- }
787
- /**
788
- * Handle requests to set the value of the "Brightness" characteristic
789
- */
790
- async BrightnessSet(value) {
791
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Brightness: ${value}`);
792
- this.Brightness = value;
793
- this.doColorBulbUpdate.next();
921
+ apiError(e) {
922
+ this.lightBulbService.updateCharacteristic(this.platform.Characteristic.On, e);
923
+ this.lightBulbService.updateCharacteristic(this.platform.Characteristic.Hue, e);
924
+ this.lightBulbService.updateCharacteristic(this.platform.Characteristic.Brightness, e);
925
+ this.lightBulbService.updateCharacteristic(this.platform.Characteristic.Saturation, e);
926
+ this.lightBulbService.updateCharacteristic(this.platform.Characteristic.ColorTemperature, e);
794
927
  }
795
- /**
796
- * Handle requests to set the value of the "ColorTemperature" characteristic
797
- */
798
- async ColorTemperatureSet(value) {
799
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ColorTemperature: ${value}`);
800
- // Convert mired to kelvin to nearest 100 (SwitchBot seems to need this)
801
- const kelvin = Math.round(1000000 / Number(value) / 100) * 100;
802
- // Check and increase/decrease kelvin to range of device
803
- const k = Math.min(Math.max(kelvin, this.minKelvin), this.maxKelvin);
804
- if (!this.accessory.context.On || this.cacheKelvin === k) {
805
- return;
928
+ FirmwareRevision(accessory, device) {
929
+ let FirmwareRevision;
930
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
931
+ + ` accessory.context.FirmwareRevision: ${accessory.context.FirmwareRevision}`);
932
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} device.firmware: ${device.firmware}`);
933
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} this.platform.version: ${this.platform.version}`);
934
+ if (accessory.context.FirmwareRevision) {
935
+ FirmwareRevision = accessory.context.FirmwareRevision;
806
936
  }
807
- // Updating the hue/sat to the corresponding values mimics native adaptive lighting
808
- const hs = (0, settings_1.m2hs)(value);
809
- this.lightBulbService.updateCharacteristic(this.platform.Characteristic.Hue, hs[0]);
810
- this.lightBulbService.updateCharacteristic(this.platform.Characteristic.Saturation, hs[1]);
811
- this.ColorTemperature = value;
812
- this.doColorBulbUpdate.next();
813
- }
814
- /**
815
- * Handle requests to set the value of the "Hue" characteristic
816
- */
817
- async HueSet(value) {
818
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${value}`);
819
- this.lightBulbService.updateCharacteristic(this.platform.Characteristic.ColorTemperature, 140);
820
- this.Hue = value;
821
- this.doColorBulbUpdate.next();
822
- }
823
- /**
824
- * Handle requests to set the value of the "Saturation" characteristic
825
- */
826
- async SaturationSet(value) {
827
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${value}`);
828
- this.lightBulbService.updateCharacteristic(this.platform.Characteristic.ColorTemperature, 140);
829
- this.Saturation = value;
830
- this.doColorBulbUpdate.next();
937
+ else if (device.firmware) {
938
+ FirmwareRevision = device.firmware;
939
+ }
940
+ else {
941
+ FirmwareRevision = this.platform.version;
942
+ }
943
+ return FirmwareRevision;
831
944
  }
832
- async config(device) {
833
- let config = {};
834
- if (device.colorbulb) {
835
- config = device.colorbulb;
945
+ context() {
946
+ if (this.On === undefined) {
947
+ this.On = false;
836
948
  }
837
- if (device.ble) {
838
- config['ble'] = device.ble;
949
+ else {
950
+ this.On = this.accessory.context.On;
839
951
  }
840
- if (device.openAPI !== undefined) {
841
- config['openAPI'] = device.openAPI;
952
+ if (this.Hue === undefined) {
953
+ this.Hue = 0;
842
954
  }
843
- if (device.logging !== undefined) {
844
- config['logging'] = device.logging;
955
+ else {
956
+ this.Hue = this.accessory.context.Hue;
845
957
  }
846
- if (device.refreshRate !== undefined) {
847
- config['refreshRate'] = device.refreshRate;
958
+ if (this.Brightness === undefined) {
959
+ this.Brightness = 0;
848
960
  }
849
- if (device.scanDuration !== undefined) {
850
- config['scanDuration'] = device.scanDuration;
961
+ else {
962
+ this.Brightness = this.accessory.context.Brightness;
851
963
  }
852
- if (device.offline !== undefined) {
853
- config['offline'] = device.offline;
964
+ if (this.Brightness === undefined) {
965
+ this.Saturation = 0;
854
966
  }
855
- if (Object.entries(config).length !== 0) {
856
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${(0, super_stringify_1.default)(config)}`);
967
+ else {
968
+ this.Saturation = this.accessory.context.Saturation;
857
969
  }
970
+ if (this.ColorTemperature === undefined) {
971
+ this.ColorTemperature = 140;
972
+ }
973
+ else {
974
+ this.ColorTemperature = this.accessory.context.ColorTemperature;
975
+ }
976
+ this.minKelvin = 2000;
977
+ this.maxKelvin = 9000;
858
978
  }
859
979
  async refreshRate(device) {
860
980
  if (device.refreshRate) {
@@ -866,27 +986,28 @@ class ColorBulb {
866
986
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
867
987
  }
868
988
  }
869
- async openAPI() {
870
- if (!this.device.openAPI) {
871
- this.OpenAPI = true;
989
+ async config(device) {
990
+ let config = {};
991
+ if (device.colorbulb) {
992
+ config = device.colorbulb;
872
993
  }
873
- else {
874
- this.OpenAPI = this.device.openAPI;
994
+ if (device.connectionType !== undefined) {
995
+ config['connectionType'] = device.connectionType;
875
996
  }
876
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI: ${this.OpenAPI}`);
877
- }
878
- async scan(device) {
879
- if (device.scanDuration) {
880
- this.scanDuration = this.accessory.context.scanDuration = device.scanDuration;
881
- if (device.ble) {
882
- this.debugLog(`Bot: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
883
- }
997
+ if (device.logging !== undefined) {
998
+ config['logging'] = device.logging;
884
999
  }
885
- else {
886
- this.scanDuration = this.accessory.context.scanDuration = 1;
887
- if (this.device.ble) {
888
- this.debugLog(`Bot: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
889
- }
1000
+ if (device.refreshRate !== undefined) {
1001
+ config['refreshRate'] = device.refreshRate;
1002
+ }
1003
+ if (device.scanDuration !== undefined) {
1004
+ config['scanDuration'] = device.scanDuration;
1005
+ }
1006
+ if (device.offline !== undefined) {
1007
+ config['offline'] = device.offline;
1008
+ }
1009
+ if (Object.entries(config).length !== 0) {
1010
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${(0, super_stringify_1.default)(config)}`);
890
1011
  }
891
1012
  }
892
1013
  async logs(device) {
@@ -907,42 +1028,6 @@ class ColorBulb {
907
1028
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`);
908
1029
  }
909
1030
  }
910
- FirmwareRevision(accessory, device) {
911
- let FirmwareRevision;
912
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
913
- + ` accessory.context.FirmwareRevision: ${accessory.context.FirmwareRevision}`);
914
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} device.firmware: ${device.firmware}`);
915
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} this.platform.version: ${this.platform.version}`);
916
- if (accessory.context.FirmwareRevision) {
917
- FirmwareRevision = accessory.context.FirmwareRevision;
918
- }
919
- else if (device.firmware) {
920
- FirmwareRevision = device.firmware;
921
- }
922
- else {
923
- FirmwareRevision = this.platform.version;
924
- }
925
- return FirmwareRevision;
926
- }
927
- async adaptiveLighting(device) {
928
- if (device.colorbulb?.adaptiveLightingShift) {
929
- this.adaptiveLightingShift = device.colorbulb.adaptiveLightingShift;
930
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} adaptiveLightingShift: ${this.adaptiveLightingShift}`);
931
- }
932
- else {
933
- this.adaptiveLightingShift = 0;
934
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} adaptiveLightingShift: ${this.adaptiveLightingShift}`);
935
- }
936
- }
937
- minStep(device) {
938
- if (device.colorbulb?.set_minStep) {
939
- this.set_minStep = device.colorbulb?.set_minStep;
940
- }
941
- else {
942
- this.set_minStep = 1;
943
- }
944
- return this.set_minStep;
945
- }
946
1031
  /**
947
1032
  * Logging for Device
948
1033
  */