@switchbot/homebridge-switchbot 3.5.1-beta.8 → 3.5.1

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 (159) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/config.schema.json +19 -3
  3. package/dist/device/blindtilt.d.ts +6 -4
  4. package/dist/device/blindtilt.d.ts.map +1 -1
  5. package/dist/device/blindtilt.js +60 -46
  6. package/dist/device/blindtilt.js.map +1 -1
  7. package/dist/device/bot.d.ts +6 -3
  8. package/dist/device/bot.d.ts.map +1 -1
  9. package/dist/device/bot.js +59 -25
  10. package/dist/device/bot.js.map +1 -1
  11. package/dist/device/ceilinglight.d.ts +6 -3
  12. package/dist/device/ceilinglight.d.ts.map +1 -1
  13. package/dist/device/ceilinglight.js +47 -29
  14. package/dist/device/ceilinglight.js.map +1 -1
  15. package/dist/device/colorbulb.d.ts +6 -3
  16. package/dist/device/colorbulb.d.ts.map +1 -1
  17. package/dist/device/colorbulb.js +53 -36
  18. package/dist/device/colorbulb.js.map +1 -1
  19. package/dist/device/contact.d.ts +6 -3
  20. package/dist/device/contact.d.ts.map +1 -1
  21. package/dist/device/contact.js +45 -31
  22. package/dist/device/contact.js.map +1 -1
  23. package/dist/device/curtain.d.ts +6 -4
  24. package/dist/device/curtain.d.ts.map +1 -1
  25. package/dist/device/curtain.js +41 -27
  26. package/dist/device/curtain.js.map +1 -1
  27. package/dist/device/device.d.ts +6 -3
  28. package/dist/device/device.d.ts.map +1 -1
  29. package/dist/device/device.js +59 -152
  30. package/dist/device/device.js.map +1 -1
  31. package/dist/device/fan.d.ts +6 -3
  32. package/dist/device/fan.d.ts.map +1 -1
  33. package/dist/device/fan.js +53 -32
  34. package/dist/device/fan.js.map +1 -1
  35. package/dist/device/hub.d.ts +6 -3
  36. package/dist/device/hub.d.ts.map +1 -1
  37. package/dist/device/hub.js +49 -30
  38. package/dist/device/hub.js.map +1 -1
  39. package/dist/device/humidifier.d.ts +6 -3
  40. package/dist/device/humidifier.d.ts.map +1 -1
  41. package/dist/device/humidifier.js +47 -28
  42. package/dist/device/humidifier.js.map +1 -1
  43. package/dist/device/iosensor.d.ts +6 -3
  44. package/dist/device/iosensor.d.ts.map +1 -1
  45. package/dist/device/iosensor.js +44 -27
  46. package/dist/device/iosensor.js.map +1 -1
  47. package/dist/device/lightstrip.d.ts +6 -3
  48. package/dist/device/lightstrip.d.ts.map +1 -1
  49. package/dist/device/lightstrip.js +46 -28
  50. package/dist/device/lightstrip.js.map +1 -1
  51. package/dist/device/lock.d.ts +6 -3
  52. package/dist/device/lock.d.ts.map +1 -1
  53. package/dist/device/lock.js +38 -25
  54. package/dist/device/lock.js.map +1 -1
  55. package/dist/device/meter.d.ts +6 -3
  56. package/dist/device/meter.d.ts.map +1 -1
  57. package/dist/device/meter.js +43 -27
  58. package/dist/device/meter.js.map +1 -1
  59. package/dist/device/meterplus.d.ts +6 -3
  60. package/dist/device/meterplus.d.ts.map +1 -1
  61. package/dist/device/meterplus.js +42 -27
  62. package/dist/device/meterplus.js.map +1 -1
  63. package/dist/device/motion.d.ts +6 -3
  64. package/dist/device/motion.d.ts.map +1 -1
  65. package/dist/device/motion.js +37 -24
  66. package/dist/device/motion.js.map +1 -1
  67. package/dist/device/plug.d.ts +6 -3
  68. package/dist/device/plug.d.ts.map +1 -1
  69. package/dist/device/plug.js +30 -17
  70. package/dist/device/plug.js.map +1 -1
  71. package/dist/device/robotvacuumcleaner.d.ts +6 -3
  72. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  73. package/dist/device/robotvacuumcleaner.js +37 -22
  74. package/dist/device/robotvacuumcleaner.js.map +1 -1
  75. package/dist/device/waterdetector.d.ts +6 -3
  76. package/dist/device/waterdetector.d.ts.map +1 -1
  77. package/dist/device/waterdetector.js +44 -25
  78. package/dist/device/waterdetector.js.map +1 -1
  79. package/dist/homebridge-ui/public/index.html +2 -2
  80. package/dist/irdevice/airconditioner.d.ts +1 -3
  81. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  82. package/dist/irdevice/airconditioner.js +67 -177
  83. package/dist/irdevice/airconditioner.js.map +1 -1
  84. package/dist/irdevice/airpurifier.d.ts +0 -1
  85. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  86. package/dist/irdevice/airpurifier.js +24 -53
  87. package/dist/irdevice/airpurifier.js.map +1 -1
  88. package/dist/irdevice/camera.d.ts.map +1 -1
  89. package/dist/irdevice/camera.js +12 -17
  90. package/dist/irdevice/camera.js.map +1 -1
  91. package/dist/irdevice/fan.d.ts.map +1 -1
  92. package/dist/irdevice/fan.js +22 -38
  93. package/dist/irdevice/fan.js.map +1 -1
  94. package/dist/irdevice/irdevice.d.ts +22 -10
  95. package/dist/irdevice/irdevice.d.ts.map +1 -1
  96. package/dist/irdevice/irdevice.js +94 -100
  97. package/dist/irdevice/irdevice.js.map +1 -1
  98. package/dist/irdevice/light.d.ts.map +1 -1
  99. package/dist/irdevice/light.js +20 -38
  100. package/dist/irdevice/light.js.map +1 -1
  101. package/dist/irdevice/other.d.ts.map +1 -1
  102. package/dist/irdevice/other.js +79 -77
  103. package/dist/irdevice/other.js.map +1 -1
  104. package/dist/irdevice/tv.d.ts.map +1 -1
  105. package/dist/irdevice/tv.js +38 -53
  106. package/dist/irdevice/tv.js.map +1 -1
  107. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  108. package/dist/irdevice/vacuumcleaner.js +11 -16
  109. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  110. package/dist/irdevice/waterheater.d.ts.map +1 -1
  111. package/dist/irdevice/waterheater.js +11 -16
  112. package/dist/irdevice/waterheater.js.map +1 -1
  113. package/dist/platform.d.ts +4 -6
  114. package/dist/platform.d.ts.map +1 -1
  115. package/dist/platform.js +564 -665
  116. package/dist/platform.js.map +1 -1
  117. package/dist/settings.d.ts +10 -8
  118. package/dist/settings.d.ts.map +1 -1
  119. package/dist/settings.js.map +1 -1
  120. package/dist/types/bledevicestatus.d.ts +2 -1
  121. package/dist/types/bledevicestatus.d.ts.map +1 -1
  122. package/dist/types/devicestatus.d.ts +1 -1
  123. package/dist/types/devicestatus.d.ts.map +1 -1
  124. package/package.json +11 -11
  125. package/src/device/blindtilt.ts +64 -48
  126. package/src/device/bot.ts +79 -26
  127. package/src/device/ceilinglight.ts +57 -31
  128. package/src/device/colorbulb.ts +57 -43
  129. package/src/device/contact.ts +49 -31
  130. package/src/device/curtain.ts +51 -28
  131. package/src/device/device.ts +61 -135
  132. package/src/device/fan.ts +67 -33
  133. package/src/device/hub.ts +62 -33
  134. package/src/device/humidifier.ts +60 -33
  135. package/src/device/iosensor.ts +51 -30
  136. package/src/device/lightstrip.ts +52 -29
  137. package/src/device/lock.ts +55 -26
  138. package/src/device/meter.ts +60 -29
  139. package/src/device/meterplus.ts +56 -29
  140. package/src/device/motion.ts +50 -26
  141. package/src/device/plug.ts +38 -18
  142. package/src/device/robotvacuumcleaner.ts +53 -24
  143. package/src/device/waterdetector.ts +63 -27
  144. package/src/homebridge-ui/public/index.html +2 -2
  145. package/src/irdevice/airconditioner.ts +76 -165
  146. package/src/irdevice/airpurifier.ts +29 -49
  147. package/src/irdevice/camera.ts +13 -16
  148. package/src/irdevice/fan.ts +25 -35
  149. package/src/irdevice/irdevice.ts +97 -96
  150. package/src/irdevice/light.ts +21 -35
  151. package/src/irdevice/other.ts +79 -77
  152. package/src/irdevice/tv.ts +40 -51
  153. package/src/irdevice/vacuumcleaner.ts +12 -15
  154. package/src/irdevice/waterheater.ts +12 -15
  155. package/src/platform.ts +566 -696
  156. package/src/settings.ts +11 -8
  157. package/src/types/bledevicestatus.ts +4 -2
  158. package/src/types/devicestatus.ts +1 -1
  159. package/.github/npm-version-script.cjs +0 -81
package/src/device/bot.ts CHANGED
@@ -88,6 +88,15 @@ export class Bot extends deviceBase {
88
88
  On: CharacteristicValue;
89
89
  };
90
90
 
91
+ // OpenAPI
92
+ deviceStatus!: botStatus;
93
+
94
+ //Webhook
95
+ webhookContext!: botWebhookContext;
96
+
97
+ // BLE
98
+ serviceData!: botServiceData;
99
+
91
100
  // Config
92
101
  botMode!: string;
93
102
  allowPush?: boolean;
@@ -106,6 +115,7 @@ export class Bot extends deviceBase {
106
115
  device: device & devicesConfig,
107
116
  ) {
108
117
  super(platform, accessory, device);
118
+
109
119
  // default placeholders
110
120
  this.getBotConfigSettings(device);
111
121
 
@@ -131,6 +141,9 @@ export class Bot extends deviceBase {
131
141
 
132
142
  // deviceType
133
143
  if (this.botDeviceType === 'switch') {
144
+ // Set category
145
+ accessory.category = this.hap.Categories.SWITCH
146
+ ;
134
147
  // Initialize Switch Service
135
148
  accessory.context.Switch = accessory.context.Switch ?? {};
136
149
  this.Switch = {
@@ -162,6 +175,9 @@ export class Bot extends deviceBase {
162
175
  this.removeWindowCoveringService(accessory);
163
176
  this.removeStatefulProgrammableSwitchService(accessory);
164
177
  } else if (this.botDeviceType === 'garagedoor') {
178
+ // Set category
179
+ accessory.category = this.hap.Categories.GARAGE_DOOR_OPENER;
180
+
165
181
  // Initialize GarageDoor Service
166
182
  accessory.context.GarageDoor = accessory.context.GarageDoor ?? {};
167
183
  this.GarageDoor = {
@@ -198,6 +214,9 @@ export class Bot extends deviceBase {
198
214
  this.removeWindowCoveringService(accessory);
199
215
  this.removeStatefulProgrammableSwitchService(accessory);
200
216
  } else if (this.botDeviceType === 'door') {
217
+ // Set category
218
+ accessory.category = this.hap.Categories.DOOR;
219
+
201
220
  // Initialize Door Service
202
221
  accessory.context.Door = accessory.context.Door ?? {};
203
222
  this.Door = {
@@ -234,6 +253,9 @@ export class Bot extends deviceBase {
234
253
  this.removeWindowCoveringService(accessory);
235
254
  this.removeStatefulProgrammableSwitchService(accessory);
236
255
  } else if (this.botDeviceType === 'window') {
256
+ // Set category
257
+ accessory.category = this.hap.Categories.WINDOW;
258
+
237
259
  // Initialize Window Service
238
260
  accessory.context.Window = accessory.context.Window ?? {};
239
261
  this.Window = {
@@ -270,6 +292,9 @@ export class Bot extends deviceBase {
270
292
  this.removeWindowCoveringService(accessory);
271
293
  this.removeStatefulProgrammableSwitchService(accessory);
272
294
  } else if (this.botDeviceType === 'windowcovering') {
295
+ // Set category
296
+ accessory.category = this.hap.Categories.WINDOW_COVERING;
297
+
273
298
  // Initialize WindowCovering Service
274
299
  accessory.context.WindowCovering = accessory.context.WindowCovering ?? {};
275
300
  this.WindowCovering = {
@@ -306,6 +331,9 @@ export class Bot extends deviceBase {
306
331
  this.removeGarageDoorService(accessory);
307
332
  this.removeStatefulProgrammableSwitchService(accessory);
308
333
  } else if (this.botDeviceType === 'lock') {
334
+ // Set category
335
+ accessory.category = this.hap.Categories.DOOR_LOCK;
336
+
309
337
  // Initialize Lock Service
310
338
  accessory.context.LockMechanism = accessory.context.LockMechanism ?? {};
311
339
  this.LockMechanism = {
@@ -336,6 +364,9 @@ export class Bot extends deviceBase {
336
364
  this.removeWindowCoveringService(accessory);
337
365
  this.removeStatefulProgrammableSwitchService(accessory);
338
366
  } else if (this.botDeviceType === 'faucet') {
367
+ // Set category
368
+ accessory.category = this.hap.Categories.FAUCET;
369
+
339
370
  // Initialize Faucet Service
340
371
  accessory.context.Faucet = accessory.context.Faucet ?? {};
341
372
  this.Faucet = {
@@ -366,6 +397,9 @@ export class Bot extends deviceBase {
366
397
  this.removeWindowCoveringService(accessory);
367
398
  this.removeStatefulProgrammableSwitchService(accessory);
368
399
  } else if (this.botDeviceType === 'fan') {
400
+ // Set category
401
+ accessory.category = this.hap.Categories.FAN;
402
+
369
403
  // Initialize Fan Service
370
404
  accessory.context.Fan = accessory.context.Fan ?? {};
371
405
  this.Fan = {
@@ -396,6 +430,9 @@ export class Bot extends deviceBase {
396
430
  this.removeWindowCoveringService(accessory);
397
431
  this.removeStatefulProgrammableSwitchService(accessory);
398
432
  } else if (this.botDeviceType === 'stateful') {
433
+ // Set category
434
+ accessory.category = this.hap.Categories.PROGRAMMABLE_SWITCH;
435
+
399
436
  // Initialize StatefulProgrammableSwitch Service
400
437
  accessory.context.StatefulProgrammableSwitch = accessory.context.StatefulProgrammableSwitch ?? {};
401
438
  this.StatefulProgrammableSwitch = {
@@ -427,6 +464,9 @@ export class Bot extends deviceBase {
427
464
  this.removeGarageDoorService(accessory);
428
465
  this.removeWindowCoveringService(accessory);
429
466
  } else {
467
+ // Set category
468
+ accessory.category = this.hap.Categories.OUTLET;
469
+
430
470
  // Initialize Switch property
431
471
  accessory.context.Outlet = accessory.context.Outlet ?? {};
432
472
  this.Outlet = {
@@ -459,12 +499,11 @@ export class Bot extends deviceBase {
459
499
  }
460
500
 
461
501
  // Retrieve initial values and updateHomekit
502
+ this.debugLog('Retrieve initial values and update Homekit');
462
503
  this.refreshStatus();
463
504
 
464
- // Retrieve initial values and updateHomekit
465
- this.updateHomeKitCharacteristics();
466
-
467
- // regisiter webhook event handler
505
+ //regisiter webhook event handler
506
+ this.debugLog('Registering Webhook Event Handler');
468
507
  this.registerWebhook();
469
508
 
470
509
  // Start an update interval
@@ -505,23 +544,26 @@ export class Bot extends deviceBase {
505
544
  /**
506
545
  * Parse the device status from the SwitchBotBLE API
507
546
  */
508
- async BLEparseStatus(serviceData: botServiceData): Promise<void> {
547
+ async BLEparseStatus(): Promise<void> {
509
548
  await this.debugLog('BLEparseStatus');
549
+ await this.debugLog(`(power, battery, deviceMode) = BLE:(${this.serviceData.state}, ${this.serviceData.battery}, ${this.serviceData.mode}),`
550
+ + ` current:(${this.accessory.context.On}, ${this.Battery.BatteryLevel}, ${this.botMode})`);
551
+
510
552
  // BLEmode (true if Switch Mode) | (false if Press Mode)
511
- if (serviceData.mode) {
553
+ if (this.serviceData.mode) {
512
554
  this.accessory.context.On = await this.getOn();
513
555
  if (this.getOn() === undefined) {
514
- this.setOn(Boolean(serviceData.state));
556
+ this.setOn(Boolean(this.serviceData.state));
515
557
  }
516
- this.debugLog(`Switch Mode, mode: ${serviceData.mode}, On: ${this.accessory.context.On}`);
558
+ this.debugLog(`Switch Mode, mode: ${this.serviceData.mode}, On: ${this.accessory.context.On}`);
517
559
  } else {
518
560
  this.setOn(false);
519
561
  this.accessory.context.On = await this.getOn();
520
- this.debugLog(`Press Mode, mode: ${serviceData.mode}, On: ${this.accessory.context.On}`);
562
+ this.debugLog(`Press Mode, mode: ${this.serviceData.mode}, On: ${this.accessory.context.On}`);
521
563
  }
522
564
 
523
565
  // BatteryLevel
524
- this.Battery.BatteryLevel = Number(serviceData.battery);
566
+ this.Battery.BatteryLevel = this.serviceData.battery;
525
567
  await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`);
526
568
 
527
569
  // StatusLowBattery
@@ -534,8 +576,12 @@ export class Bot extends deviceBase {
534
576
  /**
535
577
  * Parse the device status from the SwitchBot OpenAPI
536
578
  */
537
- async openAPIparseStatus(deviceStatus: botStatus): Promise<void> {
579
+ async openAPIparseStatus(): Promise<void> {
538
580
  await this.debugLog('openAPIparseStatus');
581
+ await this.debugLog(`(power, battery, deviceMode) = API:(${this.deviceStatus.power}, ${this.deviceStatus.battery}, ${this.botMode}),`
582
+ + ` current:(${this.accessory.context.On}, ${this.Battery.BatteryLevel}, ${this.botMode})`);
583
+
584
+ // On
539
585
  if (this.botMode === 'press') {
540
586
  this.setOn(false);
541
587
  this.accessory.context.On = await this.getOn();
@@ -548,7 +594,7 @@ export class Bot extends deviceBase {
548
594
  await this.debugLog(`On: ${this.accessory.context.On}`);
549
595
 
550
596
  // Battery Level
551
- this.Battery.BatteryLevel = Number(deviceStatus.battery);
597
+ this.Battery.BatteryLevel = this.deviceStatus.battery;
552
598
  await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`);
553
599
 
554
600
  // StatusLowBattery
@@ -557,9 +603,9 @@ export class Bot extends deviceBase {
557
603
  await this.debugLog(`StatusLowBattery: ${this.Battery.StatusLowBattery}`);
558
604
 
559
605
  // Firmware Version
560
- const version = deviceStatus.version.toString();
561
- this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
562
- if (deviceStatus.version) {
606
+ if (this.deviceStatus.version) {
607
+ const version = this.deviceStatus.version.toString();
608
+ this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
563
609
  const deviceVersion = version.replace(/^V|-.*$/g, '') ?? '0.0.0';
564
610
  this.accessory
565
611
  .getService(this.hap.Service.AccessoryInformation)!
@@ -567,29 +613,33 @@ export class Bot extends deviceBase {
567
613
  .setCharacteristic(this.hap.Characteristic.FirmwareRevision, deviceVersion)
568
614
  .getCharacteristic(this.hap.Characteristic.FirmwareRevision)
569
615
  .updateValue(deviceVersion);
570
- this.accessory.context.deviceVersion = deviceVersion;
571
- await this.debugLog(`deviceVersion: ${this.accessory.context.deviceVersion}`);
616
+ this.accessory.context.version = deviceVersion;
617
+ await this.debugLog(`version: ${this.accessory.context.version}`);
572
618
  }
573
619
  }
574
620
 
575
- async parseStatusWebhook(context: botWebhookContext): Promise<void> {
621
+ async parseStatusWebhook(): Promise<void> {
576
622
  await this.debugLog('parseStatusWebhook');
577
623
  const getOn = await this.getOn();
578
- await this.debugLog(`(power, battery, deviceMode) = Webhook:(${context.power}, ${context.battery}, ${context.deviceMode}),`
579
- + ` current:(${getOn}, ${this.Battery.BatteryLevel}, ${this.botMode})`);
624
+ await this.debugLog(`(power, battery, deviceMode) = Webhook:(${this.webhookContext.power}, ${this.webhookContext.battery},`
625
+ + ` ${this.webhookContext.deviceMode}), current:(${getOn}, ${this.Battery.BatteryLevel}, ${this.botMode})`);
626
+
580
627
  // On
581
- const setOn = context.power === 'on' ? true : false;
628
+ const setOn = this.webhookContext.power === 'on' ? true : false;
582
629
  await this.setOn(setOn);
583
630
  await this.debugLog(`On: ${setOn}`);
631
+
584
632
  // BatteryLevel
585
- this.Battery.BatteryLevel = context.battery;
633
+ this.Battery.BatteryLevel = this.webhookContext.battery;
586
634
  await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`);
635
+
587
636
  // StatusLowBattery
588
637
  this.Battery.StatusLowBattery = this.Battery.BatteryLevel < 10
589
638
  ? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW : this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
590
639
  await this.debugLog(`StatusLowBattery: ${this.Battery.StatusLowBattery}`);
640
+
591
641
  // Mode
592
- this.botMode = context.deviceMode;
642
+ this.botMode = this.webhookContext.deviceMode;
593
643
  await this.debugLog(`Mode: ${this.botMode}`);
594
644
  }
595
645
 
@@ -622,7 +672,8 @@ export class Bot extends deviceBase {
622
672
  const serviceData = await this.monitorAdvertisementPackets(switchbot) as botServiceData;
623
673
  // Update HomeKit
624
674
  if (serviceData.model === SwitchBotBLEModel.Bot && serviceData.modelName === SwitchBotBLEModelName.Bot) {
625
- await this.BLEparseStatus(serviceData);
675
+ this.serviceData = serviceData;
676
+ await this.BLEparseStatus();
626
677
  await this.updateHomeKitCharacteristics();
627
678
  } else {
628
679
  await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
@@ -640,7 +691,8 @@ export class Bot extends deviceBase {
640
691
  await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);;
641
692
  if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
642
693
  await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
643
- await this.openAPIparseStatus(deviceStatus.body);
694
+ this.deviceStatus = deviceStatus.body;
695
+ await this.openAPIparseStatus();
644
696
  await this.updateHomeKitCharacteristics();
645
697
  } else {
646
698
  await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
@@ -658,7 +710,8 @@ export class Bot extends deviceBase {
658
710
  this.platform.webhookEventHandler[this.device.deviceId] = async (context: botWebhookContext) => {
659
711
  try {
660
712
  await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
661
- await this.parseStatusWebhook(context);
713
+ this.webhookContext = context;
714
+ await this.parseStatusWebhook();
662
715
  await this.updateHomeKitCharacteristics();
663
716
  } catch (e: any) {
664
717
  await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
@@ -32,6 +32,15 @@ export class CeilingLight extends deviceBase {
32
32
  ColorTemperature?: CharacteristicValue;
33
33
  };
34
34
 
35
+ // OpenAPI
36
+ deviceStatus!: ceilingLightStatus | ceilingLightProStatus;
37
+
38
+ //Webhook
39
+ webhookContext!: ceilingLightWebhookContext | ceilingLightProWebhookContext;
40
+
41
+ // BLE
42
+ serviceData!: ceilingLightServiceData | ceilingLightProServiceData;
43
+
35
44
  // Adaptive Lighting
36
45
  AdaptiveLightingController?: ControllerConstructor | Controller<ControllerServiceMap>;
37
46
  adaptiveLightingShift?: number;
@@ -46,6 +55,9 @@ export class CeilingLight extends deviceBase {
46
55
  device: device & devicesConfig,
47
56
  ) {
48
57
  super(platform, accessory, device);
58
+ // Set category
59
+ accessory.category = this.hap.Categories.LIGHTBULB;
60
+
49
61
  // default placeholders
50
62
  this.adaptiveLighting(device);
51
63
 
@@ -147,10 +159,12 @@ export class CeilingLight extends deviceBase {
147
159
  .onSet(this.SaturationSet.bind(this));
148
160
 
149
161
  // Retrieve initial values and updateHomekit
162
+ this.debugLog('Retrieve initial values and update Homekit');
150
163
  this.refreshStatus();
151
164
 
152
- // Update Homekit
153
- this.updateHomeKitCharacteristics();
165
+ //regisiter webhook event handler
166
+ this.debugLog('Registering Webhook Event Handler');
167
+ this.registerWebhook();
154
168
 
155
169
  //regisiter webhook event handler
156
170
  this.registerWebhook();
@@ -185,41 +199,46 @@ export class CeilingLight extends deviceBase {
185
199
  /**
186
200
  * Parse the device status from the SwitchBotBLE API
187
201
  */
188
- async BLEparseStatus(serviceData: ceilingLightServiceData | ceilingLightProServiceData): Promise<void> {
202
+ async BLEparseStatus(): Promise<void> {
189
203
  await this.debugLog('BLEparseStatus');
204
+ await this.debugLog(`(power, brightness, colorTemperature) = BLE:(${this.serviceData.state}, ${this.serviceData.brightness}, `
205
+ + `${this.serviceData.color_temperature}), current:(${this.LightBulb.On}, ${this.LightBulb.Brightness}, ${this.LightBulb.ColorTemperature})`);
206
+
190
207
  // On
191
- this.LightBulb.On = serviceData.state;
208
+ this.LightBulb.On = this.serviceData.state;
192
209
  await this.debugLog(`On: ${this.LightBulb.On}`);
210
+
211
+ // ColorTemperature
212
+ const miredColorTemperature = Math.round(1000000 / this.serviceData.color_temperature);
213
+ this.LightBulb.ColorTemperature = Math.max(Math.min(miredColorTemperature, 500), 140);
214
+ await this.debugLog(`ColorTemperature: ${this.LightBulb.ColorTemperature}`);
193
215
  }
194
216
 
195
217
  /**
196
218
  * Parse the device status from the SwitchBot OpenAPI
197
219
  */
198
- async openAPIparseStatus(deviceStatus: ceilingLightStatus | ceilingLightProStatus): Promise<void> {
220
+ async openAPIparseStatus(): Promise<void> {
199
221
  await this.debugLog('openAPIparseStatus');
222
+ await this.debugLog(`(power, brightness, colorTemperature) = OpenAPI:(${this.deviceStatus.power}, ${this.deviceStatus.brightness}, `
223
+ + `${this.deviceStatus.colorTemperature}), current:(${this.LightBulb.On}, ${this.LightBulb.Brightness}, ${this.LightBulb.ColorTemperature})`);
224
+
200
225
  // On
201
- this.LightBulb.On = deviceStatus.power;
226
+ this.LightBulb.On = this.deviceStatus.power;
202
227
  await this.debugLog(`On: ${this.LightBulb.On}`);
203
228
 
204
229
  // Brightness
205
- this.LightBulb.Brightness = deviceStatus.brightness;
230
+ this.LightBulb.Brightness = this.deviceStatus.brightness;
206
231
  await this.debugLog(`Brightness: ${this.LightBulb.Brightness}`);
207
232
 
208
233
  // ColorTemperature
209
- if (!Number.isNaN(deviceStatus.colorTemperature)) {
210
- await this.debugLog(`OpenAPI ColorTemperature: ${deviceStatus.colorTemperature}`);
211
- const mired = Math.round(1000000 / deviceStatus.colorTemperature);
212
-
213
- this.LightBulb.ColorTemperature = Number(mired);
214
-
215
- this.LightBulb.ColorTemperature = Math.max(Math.min(this.LightBulb.ColorTemperature, 500), 140);
216
- await this.debugLog(`ColorTemperature: ${this.LightBulb.ColorTemperature}`);
217
- }
234
+ const miredColorTemperature = Math.round(1000000 / this.deviceStatus.colorTemperature);
235
+ this.LightBulb.ColorTemperature = Math.max(Math.min(miredColorTemperature, 500), 140);
236
+ await this.debugLog(`ColorTemperature: ${this.LightBulb.ColorTemperature}`);
218
237
 
219
238
  // Firmware Version
220
- const version = deviceStatus.version.toString();
221
- await this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
222
- if (deviceStatus.version) {
239
+ if (this.deviceStatus.version) {
240
+ const version = this.deviceStatus.version.toString();
241
+ await this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
223
242
  const deviceVersion = version.replace(/^V|-.*$/g, '') ?? '0.0.0';
224
243
  this.accessory
225
244
  .getService(this.hap.Service.AccessoryInformation)!
@@ -227,23 +246,27 @@ export class CeilingLight extends deviceBase {
227
246
  .setCharacteristic(this.hap.Characteristic.FirmwareRevision, deviceVersion)
228
247
  .getCharacteristic(this.hap.Characteristic.FirmwareRevision)
229
248
  .updateValue(deviceVersion);
230
- this.accessory.context.deviceVersion = deviceVersion;
231
- await this.debugSuccessLog(`deviceVersion: ${this.accessory.context.deviceVersion}`);
249
+ this.accessory.context.version = deviceVersion;
250
+ await this.debugSuccessLog(`version: ${this.accessory.context.version}`);
232
251
  }
233
252
  }
234
253
 
235
- async parseStatusWebhook(context: ceilingLightWebhookContext | ceilingLightProWebhookContext): Promise<void> {
254
+ async parseStatusWebhook(): Promise<void> {
236
255
  await this.debugLog('parseStatusWebhook');
237
- await this.debugLog(`(powerState, brightness, colorTemperature) = Webhook:(${context.powerState}, ${context.brightness},`
238
- + ` ${context.colorTemperature}), current:(${this.LightBulb.On}, ${this.LightBulb.Brightness}, ${this.LightBulb.ColorTemperature})`);
256
+ await this.debugLog(`(powerState, brightness, colorTemperature) = Webhook:(${this.webhookContext.powerState}, ${this.webhookContext.brightness}, `
257
+ + `${this.webhookContext.colorTemperature}), current:(${this.LightBulb.On}, ${this.LightBulb.Brightness}, ${this.LightBulb.ColorTemperature})`);
258
+
239
259
  // On
240
- this.LightBulb.On = context.powerState === 'ON' ? true : false;
260
+ this.LightBulb.On = this.webhookContext.powerState === 'ON' ? true : false;
241
261
  await this.debugLog(`On: ${this.LightBulb.On}`);
262
+
242
263
  // Brightness
243
- this.LightBulb.Brightness = context.brightness;
264
+ this.LightBulb.Brightness = this.webhookContext.brightness;
244
265
  await this.debugLog(`Brightness: ${this.LightBulb.Brightness}`);
266
+
245
267
  // ColorTemperature
246
- this.LightBulb.ColorTemperature = context.colorTemperature;
268
+ const miredColorTemperature = Math.round(1000000 / this.webhookContext.colorTemperature);
269
+ this.LightBulb.ColorTemperature = Math.max(Math.min(miredColorTemperature, 500), 140);
247
270
  await this.debugLog(`ColorTemperature: ${this.LightBulb.ColorTemperature}`);
248
271
  }
249
272
 
@@ -276,7 +299,8 @@ export class CeilingLight extends deviceBase {
276
299
  // Update HomeKit
277
300
  if ((serviceData.model === SwitchBotBLEModel.CeilingLight || SwitchBotBLEModel.CeilingLightPro)
278
301
  && serviceData.modelName === SwitchBotBLEModelName.CeilingLight || SwitchBotBLEModelName.CeilingLightPro) {
279
- await this.BLEparseStatus(serviceData);
302
+ this.serviceData = serviceData;
303
+ await this.BLEparseStatus();
280
304
  await this.updateHomeKitCharacteristics();
281
305
  } else {
282
306
  await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
@@ -294,7 +318,8 @@ export class CeilingLight extends deviceBase {
294
318
  await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);;
295
319
  if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
296
320
  await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
297
- await this.openAPIparseStatus(deviceStatus.body);
321
+ this.deviceStatus = deviceStatus.body;
322
+ await this.openAPIparseStatus();
298
323
  await this.updateHomeKitCharacteristics();
299
324
  } else {
300
325
  await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
@@ -312,7 +337,8 @@ export class CeilingLight extends deviceBase {
312
337
  this.platform.webhookEventHandler[this.device.deviceId] = async (context: ceilingLightWebhookContext | ceilingLightProWebhookContext) => {
313
338
  try {
314
339
  await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
315
- await this.parseStatusWebhook(context);
340
+ this.webhookContext = context;
341
+ await this.parseStatusWebhook();
316
342
  await this.updateHomeKitCharacteristics();
317
343
  } catch (e: any) {
318
344
  await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
@@ -476,7 +502,7 @@ export class CeilingLight extends deviceBase {
476
502
  async pushColorTemperatureChanges(): Promise<void> {
477
503
  await this.debugLog('pushColorTemperatureChanges');
478
504
  if (this.LightBulb.ColorTemperature !== this.accessory.context.ColorTemperature) {
479
- const kelvin = Math.round(1000000 / Number(this.LightBulb.ColorTemperature));
505
+ const kelvin = Math.round(1000000 * Number(this.LightBulb.ColorTemperature));
480
506
  this.accessory.context.kelvin = kelvin;
481
507
  const bodyChange = JSON.stringify({
482
508
  command: 'setColorTemperature',