@switchbot/homebridge-switchbot 3.5.1-beta.11 → 3.5.1-beta.13

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 (153) hide show
  1. package/config.schema.json +19 -3
  2. package/dist/device/blindtilt.d.ts +6 -4
  3. package/dist/device/blindtilt.d.ts.map +1 -1
  4. package/dist/device/blindtilt.js +57 -45
  5. package/dist/device/blindtilt.js.map +1 -1
  6. package/dist/device/bot.d.ts +6 -3
  7. package/dist/device/bot.d.ts.map +1 -1
  8. package/dist/device/bot.js +38 -24
  9. package/dist/device/bot.js.map +1 -1
  10. package/dist/device/ceilinglight.d.ts +6 -3
  11. package/dist/device/ceilinglight.d.ts.map +1 -1
  12. package/dist/device/ceilinglight.js +44 -27
  13. package/dist/device/ceilinglight.js.map +1 -1
  14. package/dist/device/colorbulb.d.ts +6 -3
  15. package/dist/device/colorbulb.d.ts.map +1 -1
  16. package/dist/device/colorbulb.js +47 -32
  17. package/dist/device/colorbulb.js.map +1 -1
  18. package/dist/device/contact.d.ts +6 -3
  19. package/dist/device/contact.d.ts.map +1 -1
  20. package/dist/device/contact.js +40 -28
  21. package/dist/device/contact.js.map +1 -1
  22. package/dist/device/curtain.d.ts +6 -4
  23. package/dist/device/curtain.d.ts.map +1 -1
  24. package/dist/device/curtain.js +35 -22
  25. package/dist/device/curtain.js.map +1 -1
  26. package/dist/device/device.d.ts +13 -2
  27. package/dist/device/device.d.ts.map +1 -1
  28. package/dist/device/device.js +223 -108
  29. package/dist/device/device.js.map +1 -1
  30. package/dist/device/fan.d.ts +6 -3
  31. package/dist/device/fan.d.ts.map +1 -1
  32. package/dist/device/fan.js +48 -29
  33. package/dist/device/fan.js.map +1 -1
  34. package/dist/device/hub.d.ts +6 -3
  35. package/dist/device/hub.d.ts.map +1 -1
  36. package/dist/device/hub.js +43 -26
  37. package/dist/device/hub.js.map +1 -1
  38. package/dist/device/humidifier.d.ts +6 -3
  39. package/dist/device/humidifier.d.ts.map +1 -1
  40. package/dist/device/humidifier.js +43 -26
  41. package/dist/device/humidifier.js.map +1 -1
  42. package/dist/device/iosensor.d.ts +6 -3
  43. package/dist/device/iosensor.d.ts.map +1 -1
  44. package/dist/device/iosensor.js +39 -24
  45. package/dist/device/iosensor.js.map +1 -1
  46. package/dist/device/lightstrip.d.ts +6 -3
  47. package/dist/device/lightstrip.d.ts.map +1 -1
  48. package/dist/device/lightstrip.js +43 -27
  49. package/dist/device/lightstrip.js.map +1 -1
  50. package/dist/device/lock.d.ts +6 -3
  51. package/dist/device/lock.d.ts.map +1 -1
  52. package/dist/device/lock.js +33 -22
  53. package/dist/device/lock.js.map +1 -1
  54. package/dist/device/meter.d.ts +6 -3
  55. package/dist/device/meter.d.ts.map +1 -1
  56. package/dist/device/meter.js +38 -24
  57. package/dist/device/meter.js.map +1 -1
  58. package/dist/device/meterplus.d.ts +6 -3
  59. package/dist/device/meterplus.d.ts.map +1 -1
  60. package/dist/device/meterplus.js +37 -24
  61. package/dist/device/meterplus.js.map +1 -1
  62. package/dist/device/motion.d.ts +6 -3
  63. package/dist/device/motion.d.ts.map +1 -1
  64. package/dist/device/motion.js +33 -22
  65. package/dist/device/motion.js.map +1 -1
  66. package/dist/device/plug.d.ts +6 -3
  67. package/dist/device/plug.d.ts.map +1 -1
  68. package/dist/device/plug.js +27 -16
  69. package/dist/device/plug.js.map +1 -1
  70. package/dist/device/robotvacuumcleaner.d.ts +6 -3
  71. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  72. package/dist/device/robotvacuumcleaner.js +34 -21
  73. package/dist/device/robotvacuumcleaner.js.map +1 -1
  74. package/dist/device/waterdetector.d.ts +6 -3
  75. package/dist/device/waterdetector.d.ts.map +1 -1
  76. package/dist/device/waterdetector.js +38 -23
  77. package/dist/device/waterdetector.js.map +1 -1
  78. package/dist/irdevice/airconditioner.d.ts +1 -3
  79. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  80. package/dist/irdevice/airconditioner.js +32 -138
  81. package/dist/irdevice/airconditioner.js.map +1 -1
  82. package/dist/irdevice/airpurifier.d.ts +0 -1
  83. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  84. package/dist/irdevice/airpurifier.js +13 -44
  85. package/dist/irdevice/airpurifier.js.map +1 -1
  86. package/dist/irdevice/camera.d.ts.map +1 -1
  87. package/dist/irdevice/camera.js +3 -10
  88. package/dist/irdevice/camera.js.map +1 -1
  89. package/dist/irdevice/fan.d.ts.map +1 -1
  90. package/dist/irdevice/fan.js +7 -25
  91. package/dist/irdevice/fan.js.map +1 -1
  92. package/dist/irdevice/irdevice.d.ts +1 -1
  93. package/dist/irdevice/irdevice.js +5 -5
  94. package/dist/irdevice/irdevice.js.map +1 -1
  95. package/dist/irdevice/light.d.ts.map +1 -1
  96. package/dist/irdevice/light.js +9 -27
  97. package/dist/irdevice/light.js.map +1 -1
  98. package/dist/irdevice/other.js +1 -1
  99. package/dist/irdevice/other.js.map +1 -1
  100. package/dist/irdevice/tv.d.ts.map +1 -1
  101. package/dist/irdevice/tv.js +3 -18
  102. package/dist/irdevice/tv.js.map +1 -1
  103. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  104. package/dist/irdevice/vacuumcleaner.js +2 -9
  105. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  106. package/dist/irdevice/waterheater.d.ts.map +1 -1
  107. package/dist/irdevice/waterheater.js +2 -9
  108. package/dist/irdevice/waterheater.js.map +1 -1
  109. package/dist/platform.d.ts +0 -11
  110. package/dist/platform.d.ts.map +1 -1
  111. package/dist/platform.js +11 -184
  112. package/dist/platform.js.map +1 -1
  113. package/dist/settings.d.ts +9 -8
  114. package/dist/settings.d.ts.map +1 -1
  115. package/dist/settings.js.map +1 -1
  116. package/dist/types/bledevicestatus.d.ts +2 -1
  117. package/dist/types/bledevicestatus.d.ts.map +1 -1
  118. package/dist/types/devicestatus.d.ts +1 -1
  119. package/dist/types/devicestatus.d.ts.map +1 -1
  120. package/package.json +9 -9
  121. package/src/device/blindtilt.ts +60 -47
  122. package/src/device/bot.ts +47 -25
  123. package/src/device/ceilinglight.ts +53 -27
  124. package/src/device/colorbulb.ts +50 -39
  125. package/src/device/contact.ts +43 -28
  126. package/src/device/curtain.ts +45 -23
  127. package/src/device/device.ts +226 -96
  128. package/src/device/fan.ts +61 -30
  129. package/src/device/hub.ts +55 -29
  130. package/src/device/humidifier.ts +55 -31
  131. package/src/device/iosensor.ts +45 -27
  132. package/src/device/lightstrip.ts +48 -28
  133. package/src/device/lock.ts +49 -23
  134. package/src/device/meter.ts +54 -26
  135. package/src/device/meterplus.ts +50 -26
  136. package/src/device/motion.ts +45 -24
  137. package/src/device/plug.ts +34 -17
  138. package/src/device/robotvacuumcleaner.ts +49 -23
  139. package/src/device/waterdetector.ts +56 -25
  140. package/src/irdevice/airconditioner.ts +41 -125
  141. package/src/irdevice/airpurifier.ts +18 -40
  142. package/src/irdevice/camera.ts +4 -9
  143. package/src/irdevice/fan.ts +10 -22
  144. package/src/irdevice/irdevice.ts +5 -5
  145. package/src/irdevice/light.ts +10 -24
  146. package/src/irdevice/other.ts +1 -1
  147. package/src/irdevice/tv.ts +5 -16
  148. package/src/irdevice/vacuumcleaner.ts +3 -8
  149. package/src/irdevice/waterheater.ts +3 -8
  150. package/src/platform.ts +11 -184
  151. package/src/settings.ts +10 -8
  152. package/src/types/bledevicestatus.ts +4 -2
  153. package/src/types/devicestatus.ts +1 -1
@@ -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;
@@ -147,10 +156,12 @@ export class CeilingLight extends deviceBase {
147
156
  .onSet(this.SaturationSet.bind(this));
148
157
 
149
158
  // Retrieve initial values and updateHomekit
159
+ this.debugLog('Retrieve initial values and update Homekit');
150
160
  this.refreshStatus();
151
161
 
152
- // Update Homekit
153
- this.updateHomeKitCharacteristics();
162
+ //regisiter webhook event handler
163
+ this.debugLog('Registering Webhook Event Handler');
164
+ this.registerWebhook();
154
165
 
155
166
  //regisiter webhook event handler
156
167
  this.registerWebhook();
@@ -185,38 +196,46 @@ export class CeilingLight extends deviceBase {
185
196
  /**
186
197
  * Parse the device status from the SwitchBotBLE API
187
198
  */
188
- async BLEparseStatus(serviceData: ceilingLightServiceData | ceilingLightProServiceData): Promise<void> {
199
+ async BLEparseStatus(): Promise<void> {
189
200
  await this.debugLog('BLEparseStatus');
201
+ await this.debugLog(`(power, brightness, colorTemperature) = BLE:(${this.serviceData.state}, ${this.serviceData.brightness}, `
202
+ + `${this.serviceData.color_temperature}), current:(${this.LightBulb.On}, ${this.LightBulb.Brightness}, ${this.LightBulb.ColorTemperature})`);
203
+
190
204
  // On
191
- this.LightBulb.On = serviceData.state;
205
+ this.LightBulb.On = this.serviceData.state;
192
206
  await this.debugLog(`On: ${this.LightBulb.On}`);
207
+
208
+ // ColorTemperature
209
+ const miredColorTemperature = Math.round(1000000 / this.serviceData.color_temperature);
210
+ this.LightBulb.ColorTemperature = Math.max(Math.min(miredColorTemperature, 500), 140);
211
+ await this.debugLog(`ColorTemperature: ${this.LightBulb.ColorTemperature}`);
193
212
  }
194
213
 
195
214
  /**
196
215
  * Parse the device status from the SwitchBot OpenAPI
197
216
  */
198
- async openAPIparseStatus(deviceStatus: ceilingLightStatus | ceilingLightProStatus): Promise<void> {
217
+ async openAPIparseStatus(): Promise<void> {
199
218
  await this.debugLog('openAPIparseStatus');
219
+ await this.debugLog(`(power, brightness, colorTemperature) = OpenAPI:(${this.deviceStatus.power}, ${this.deviceStatus.brightness}, `
220
+ + `${this.deviceStatus.colorTemperature}), current:(${this.LightBulb.On}, ${this.LightBulb.Brightness}, ${this.LightBulb.ColorTemperature})`);
221
+
200
222
  // On
201
- this.LightBulb.On = deviceStatus.power;
223
+ this.LightBulb.On = this.deviceStatus.power;
202
224
  await this.debugLog(`On: ${this.LightBulb.On}`);
203
225
 
204
226
  // Brightness
205
- this.LightBulb.Brightness = deviceStatus.brightness;
227
+ this.LightBulb.Brightness = this.deviceStatus.brightness;
206
228
  await this.debugLog(`Brightness: ${this.LightBulb.Brightness}`);
207
229
 
208
230
  // ColorTemperature
209
- if (!Number.isNaN(deviceStatus.colorTemperature)) {
210
- await this.debugLog(`OpenAPI ColorTemperature: ${deviceStatus.colorTemperature}`);
211
- const miredColorTemperature = Math.round(1000000 / deviceStatus.colorTemperature);
212
- this.LightBulb.ColorTemperature = Math.max(Math.min(miredColorTemperature, 500), 140);
213
- await this.debugLog(`ColorTemperature: ${this.LightBulb.ColorTemperature}`);
214
- }
231
+ const miredColorTemperature = Math.round(1000000 / this.deviceStatus.colorTemperature);
232
+ this.LightBulb.ColorTemperature = Math.max(Math.min(miredColorTemperature, 500), 140);
233
+ await this.debugLog(`ColorTemperature: ${this.LightBulb.ColorTemperature}`);
215
234
 
216
235
  // Firmware Version
217
- const version = deviceStatus.version.toString();
218
- await this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
219
- if (deviceStatus.version) {
236
+ if (this.deviceStatus.version) {
237
+ const version = this.deviceStatus.version.toString();
238
+ await this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
220
239
  const deviceVersion = version.replace(/^V|-.*$/g, '') ?? '0.0.0';
221
240
  this.accessory
222
241
  .getService(this.hap.Service.AccessoryInformation)!
@@ -229,18 +248,22 @@ export class CeilingLight extends deviceBase {
229
248
  }
230
249
  }
231
250
 
232
- async parseStatusWebhook(context: ceilingLightWebhookContext | ceilingLightProWebhookContext): Promise<void> {
251
+ async parseStatusWebhook(): Promise<void> {
233
252
  await this.debugLog('parseStatusWebhook');
234
- await this.debugLog(`(powerState, brightness, colorTemperature) = Webhook:(${context.powerState}, ${context.brightness},`
235
- + ` ${context.colorTemperature}), current:(${this.LightBulb.On}, ${this.LightBulb.Brightness}, ${this.LightBulb.ColorTemperature})`);
253
+ await this.debugLog(`(powerState, brightness, colorTemperature) = Webhook:(${this.webhookContext.powerState}, ${this.webhookContext.brightness}, `
254
+ + `${this.webhookContext.colorTemperature}), current:(${this.LightBulb.On}, ${this.LightBulb.Brightness}, ${this.LightBulb.ColorTemperature})`);
255
+
236
256
  // On
237
- this.LightBulb.On = context.powerState === 'ON' ? true : false;
257
+ this.LightBulb.On = this.webhookContext.powerState === 'ON' ? true : false;
238
258
  await this.debugLog(`On: ${this.LightBulb.On}`);
259
+
239
260
  // Brightness
240
- this.LightBulb.Brightness = context.brightness;
261
+ this.LightBulb.Brightness = this.webhookContext.brightness;
241
262
  await this.debugLog(`Brightness: ${this.LightBulb.Brightness}`);
263
+
242
264
  // ColorTemperature
243
- this.LightBulb.ColorTemperature = Math.max(Math.min(context.colorTemperature, 500), 140);
265
+ const miredColorTemperature = Math.round(1000000 / this.webhookContext.colorTemperature);
266
+ this.LightBulb.ColorTemperature = Math.max(Math.min(miredColorTemperature, 500), 140);
244
267
  await this.debugLog(`ColorTemperature: ${this.LightBulb.ColorTemperature}`);
245
268
  }
246
269
 
@@ -273,7 +296,8 @@ export class CeilingLight extends deviceBase {
273
296
  // Update HomeKit
274
297
  if ((serviceData.model === SwitchBotBLEModel.CeilingLight || SwitchBotBLEModel.CeilingLightPro)
275
298
  && serviceData.modelName === SwitchBotBLEModelName.CeilingLight || SwitchBotBLEModelName.CeilingLightPro) {
276
- await this.BLEparseStatus(serviceData);
299
+ this.serviceData = serviceData;
300
+ await this.BLEparseStatus();
277
301
  await this.updateHomeKitCharacteristics();
278
302
  } else {
279
303
  await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
@@ -291,7 +315,8 @@ export class CeilingLight extends deviceBase {
291
315
  await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);;
292
316
  if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
293
317
  await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
294
- await this.openAPIparseStatus(deviceStatus.body);
318
+ this.deviceStatus = deviceStatus.body;
319
+ await this.openAPIparseStatus();
295
320
  await this.updateHomeKitCharacteristics();
296
321
  } else {
297
322
  await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
@@ -306,10 +331,11 @@ export class CeilingLight extends deviceBase {
306
331
  async registerWebhook() {
307
332
  if (this.device.webhook) {
308
333
  await this.debugLog('is listening webhook.');
309
- this.platform.webhookEventHandler[this.device.deviceId] = async (context: ceilingLightWebhookContext | ceilingLightProWebhookContext) => {
334
+ this.webhookEventHandler[this.device.deviceId] = async (context: ceilingLightWebhookContext | ceilingLightProWebhookContext) => {
310
335
  try {
311
336
  await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
312
- await this.parseStatusWebhook(context);
337
+ this.webhookContext = context;
338
+ await this.parseStatusWebhook();
313
339
  await this.updateHomeKitCharacteristics();
314
340
  } catch (e: any) {
315
341
  await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
@@ -473,7 +499,7 @@ export class CeilingLight extends deviceBase {
473
499
  async pushColorTemperatureChanges(): Promise<void> {
474
500
  await this.debugLog('pushColorTemperatureChanges');
475
501
  if (this.LightBulb.ColorTemperature !== this.accessory.context.ColorTemperature) {
476
- const kelvin = Math.round(1000000 / Number(this.LightBulb.ColorTemperature));
502
+ const kelvin = Math.round(1000000 * Number(this.LightBulb.ColorTemperature));
477
503
  this.accessory.context.kelvin = kelvin;
478
504
  const bodyChange = JSON.stringify({
479
505
  command: 'setColorTemperature',
@@ -32,6 +32,15 @@ export class ColorBulb extends deviceBase {
32
32
  ColorTemperature?: CharacteristicValue;
33
33
  };
34
34
 
35
+ // OpenAPI
36
+ deviceStatus!: colorBulbStatus;
37
+
38
+ //Webhook
39
+ webhookContext!: colorBulbWebhookContext;
40
+
41
+ // BLE
42
+ serviceData!: colorBulbServiceData;
43
+
35
44
  // Adaptive Lighting
36
45
  AdaptiveLightingController?: ControllerConstructor | Controller<ControllerServiceMap>;
37
46
  adaptiveLightingShift?: number;
@@ -143,10 +152,12 @@ export class ColorBulb extends deviceBase {
143
152
  .onSet(this.SaturationSet.bind(this));
144
153
 
145
154
  // Retrieve initial values and updateHomekit
155
+ this.debugLog('Retrieve initial values and update Homekit');
146
156
  this.refreshStatus();
147
157
 
148
- // Update Homekit
149
- this.updateHomeKitCharacteristics();
158
+ //regisiter webhook event handler
159
+ this.debugLog('Registering Webhook Event Handler');
160
+ this.registerWebhook();
150
161
 
151
162
  //regisiter webhook event handler
152
163
  this.registerWebhook();
@@ -181,18 +192,18 @@ export class ColorBulb extends deviceBase {
181
192
  /**
182
193
  * Parse the device status from the SwitchBotBLE API
183
194
  */
184
- async BLEparseStatus(serviceData: colorBulbServiceData): Promise<void> {
195
+ async BLEparseStatus(): Promise<void> {
185
196
  await this.debugLog('BLEparseStatus');
186
197
  // On
187
- this.LightBulb.On = serviceData.power;
198
+ this.LightBulb.On = this.serviceData.power;
188
199
  await this.debugLog(`On: ${this.LightBulb.On}`);
189
200
  // Brightness
190
- this.LightBulb.Brightness = serviceData.brightness;
201
+ this.LightBulb.Brightness = this.serviceData.brightness;
191
202
  await this.debugLog(`Brightness: ${this.LightBulb.Brightness}`);
192
203
  // Color, Hue & Brightness
193
- await this.debugLog(`red: ${serviceData.red}, green: ${serviceData.green}, blue: ${serviceData.blue}`);
194
- const [hue, saturation] = rgb2hs(serviceData.red, serviceData.green, serviceData.blue);
195
- await this.debugLog(`hs: ${JSON.stringify(rgb2hs(serviceData.red, serviceData.green, serviceData.blue))}`);
204
+ await this.debugLog(`red: ${this.serviceData.red}, green: ${this.serviceData.green}, blue: ${this.serviceData.blue}`);
205
+ const [hue, saturation] = rgb2hs(this.serviceData.red, this.serviceData.green, this.serviceData.blue);
206
+ await this.debugLog(`hs: ${JSON.stringify(rgb2hs(this.serviceData.red, this.serviceData.green, this.serviceData.blue))}`);
196
207
  // Hue
197
208
  this.LightBulb.Hue = hue;
198
209
  await this.debugLog(`Hue: ${this.LightBulb.Hue}`);
@@ -200,48 +211,42 @@ export class ColorBulb extends deviceBase {
200
211
  this.LightBulb.Saturation = saturation;
201
212
  await this.debugLog(`Saturation: ${this.LightBulb.Saturation}`);
202
213
  // ColorTemperature
203
- this.LightBulb.ColorTemperature = Math.max(Math.min(serviceData.color_temperature, 500), 140);
214
+ const miredColorTemperature = Math.round(1000000 / this.serviceData.color_temperature);
215
+ this.LightBulb.ColorTemperature = Math.max(Math.min(miredColorTemperature, 500), 140);
204
216
  await this.debugLog(`ColorTemperature: ${this.LightBulb.ColorTemperature}`);
205
217
  }
206
218
 
207
219
  /**
208
220
  * Parse the device status from the SwitchBot OpenAPI
209
221
  */
210
- async openAPIparseStatus(deviceStatus: colorBulbStatus): Promise<void> {
222
+ async openAPIparseStatus(): Promise<void> {
211
223
  await this.debugLog('openAPIparseStatus');
212
-
213
224
  // On
214
- this.LightBulb.On = deviceStatus.power === 'on' ? true : false;
225
+ this.LightBulb.On = this.deviceStatus.power === 'on' ? true : false;
215
226
  await this.debugLog(`On: ${this.LightBulb.On}`);
216
-
217
227
  // Brightness
218
- this.LightBulb.Brightness = deviceStatus.brightness;
228
+ this.LightBulb.Brightness = this.deviceStatus.brightness;
219
229
  await this.debugLog(`Brightness: ${this.LightBulb.Brightness}`);
220
-
221
230
  // Color, Hue & Brightness
222
- await this.debugLog(`color: ${JSON.stringify(deviceStatus.color)}`);
223
- const [red, green, blue] = deviceStatus.color.split(':');
231
+ await this.debugLog(`color: ${JSON.stringify(this.deviceStatus.color)}`);
232
+ const [red, green, blue] = this.deviceStatus.color.split(':');
224
233
  await this.debugLog(`red: ${JSON.stringify(red)}, green: ${JSON.stringify(green)}, blue: ${JSON.stringify(blue)}`);
225
234
  const [hue, saturation] = rgb2hs(red, green, blue);
226
235
  await this.debugLog(`hs: ${JSON.stringify(rgb2hs(red, green, blue))}`);
227
-
228
236
  // Hue
229
237
  this.LightBulb.Hue = hue;
230
238
  await this.debugLog(`Hue: ${this.LightBulb.Hue}`);
231
-
232
239
  // Saturation
233
240
  this.LightBulb.Saturation = saturation;
234
241
  await this.debugLog(`Saturation: ${this.LightBulb.Saturation}`);
235
-
236
242
  // ColorTemperature
237
- const miredColorTemperature = Math.round(1000000 / deviceStatus.colorTemperature);
243
+ const miredColorTemperature = Math.round(1000000 / this.deviceStatus.colorTemperature);
238
244
  this.LightBulb.ColorTemperature = Math.max(Math.min(miredColorTemperature, 500), 140);
239
245
  await this.debugLog(`ColorTemperature: ${this.LightBulb.ColorTemperature}`);
240
-
241
246
  // Firmware Version
242
- const version = deviceStatus.version.toString();
243
- await this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
244
- if (deviceStatus.version) {
247
+ if (this.deviceStatus.version) {
248
+ const version = this.deviceStatus.version.toString();
249
+ await this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
245
250
  const deviceVersion = version.replace(/^V|-.*$/g, '') ?? '0.0.0';
246
251
  this.accessory
247
252
  .getService(this.hap.Service.AccessoryInformation)!
@@ -254,20 +259,20 @@ export class ColorBulb extends deviceBase {
254
259
  }
255
260
  }
256
261
 
257
- async parseStatusWebhook(context: colorBulbWebhookContext): Promise<void> {
262
+ async parseStatusWebhook(): Promise<void> {
258
263
  await this.debugLog('parseStatusWebhook');
259
- await this.debugLog(`(powerState, brightness, color, colorTemperature) = Webhook:(${context.powerState}, ${context.brightness}, ${context.color},`
260
- + ` ${context.colorTemperature}), current:(${this.LightBulb.On}, ${this.LightBulb.Brightness}, ${this.LightBulb.Hue},`
261
- + ` ${this.LightBulb.Saturation}, ${this.LightBulb.ColorTemperature})`);
264
+ await this.debugLog(`(powerState, brightness, color, colorTemperature) = Webhook:(${this.webhookContext.powerState},`
265
+ + ` ${this.webhookContext.brightness}, ${this.webhookContext.color}, ${this.webhookContext.colorTemperature}), current:(${this.LightBulb.On},`
266
+ + ` ${this.LightBulb.Brightness}, ${this.LightBulb.Hue}, ${this.LightBulb.Saturation}, ${this.LightBulb.ColorTemperature})`);
262
267
  // On
263
- this.LightBulb.On = context.powerState === 'ON' ? true : false;
268
+ this.LightBulb.On = this.webhookContext.powerState === 'ON' ? true : false;
264
269
  await this.debugLog(`On: ${this.LightBulb.On}`);
265
270
  // Brightness
266
- this.LightBulb.Brightness = context.brightness;
271
+ this.LightBulb.Brightness = this.webhookContext.brightness;
267
272
  await this.debugLog(`Brightness: ${this.LightBulb.Brightness}`);
268
273
  // Color, Hue & Brightness
269
- await this.debugLog(`color: ${JSON.stringify(context.color)}`);
270
- const [red, green, blue] = context.color.split(':');
274
+ await this.debugLog(`color: ${JSON.stringify(this.webhookContext.color)}`);
275
+ const [red, green, blue] = this.webhookContext.color.split(':');
271
276
  await this.debugLog(`red: ${JSON.stringify(red)}, green: ${JSON.stringify(green)}, blue: ${JSON.stringify(blue)}`);
272
277
  const [hue, saturation] = rgb2hs(red, green, blue);
273
278
  await this.debugLog(`hs: ${JSON.stringify(rgb2hs(red, green, blue))}`);
@@ -278,7 +283,8 @@ export class ColorBulb extends deviceBase {
278
283
  this.LightBulb.Saturation = saturation;
279
284
  await this.debugLog(`Saturation: ${this.LightBulb.Saturation}`);
280
285
  // ColorTemperature
281
- this.LightBulb.ColorTemperature = Math.max(Math.min(context.colorTemperature, 500), 140);
286
+ const miredColorTemperature = Math.round(1000000 / this.webhookContext.colorTemperature);
287
+ this.LightBulb.ColorTemperature = Math.max(Math.min(miredColorTemperature, 500), 140);
282
288
  await this.debugLog(`ColorTemperature: ${this.LightBulb.ColorTemperature}`);
283
289
  }
284
290
 
@@ -311,7 +317,8 @@ export class ColorBulb extends deviceBase {
311
317
  const serviceData = await this.monitorAdvertisementPackets(switchbot) as colorBulbServiceData;
312
318
  // Update HomeKit
313
319
  if (serviceData.model === SwitchBotBLEModel.ColorBulb && serviceData.modelName === SwitchBotBLEModelName.ColorBulb) {
314
- await this.BLEparseStatus(serviceData);
320
+ this.serviceData = serviceData;
321
+ await this.BLEparseStatus();
315
322
  await this.updateHomeKitCharacteristics();
316
323
  } else {
317
324
  await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
@@ -329,7 +336,8 @@ export class ColorBulb extends deviceBase {
329
336
  await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);;
330
337
  if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
331
338
  await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
332
- await this.openAPIparseStatus(deviceStatus.body);
339
+ this.deviceStatus = deviceStatus.body;
340
+ await this.openAPIparseStatus();
333
341
  await this.updateHomeKitCharacteristics();
334
342
  } else {
335
343
  await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
@@ -344,10 +352,11 @@ export class ColorBulb extends deviceBase {
344
352
  async registerWebhook() {
345
353
  if (this.device.webhook) {
346
354
  await this.debugLog('is listening webhook.');
347
- this.platform.webhookEventHandler[this.device.deviceId] = async (context: colorBulbWebhookContext) => {
355
+ this.webhookEventHandler[this.device.deviceId] = async (context: colorBulbWebhookContext) => {
348
356
  try {
349
357
  await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
350
- await this.parseStatusWebhook(context);
358
+ this.webhookContext = context;
359
+ await this.parseStatusWebhook();
351
360
  await this.updateHomeKitCharacteristics();
352
361
  } catch (e: any) {
353
362
  await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
@@ -489,6 +498,8 @@ export class ColorBulb extends deviceBase {
489
498
  async BLEpushColorTemperatureChanges(): Promise<void> {
490
499
  await this.debugLog('BLEpushColorTemperatureChanges');
491
500
  if (this.LightBulb.ColorTemperature !== this.accessory.context.ColorTemperature) {
501
+ const kelvin = Math.round(1000000 / Number(this.LightBulb.ColorTemperature));
502
+ this.accessory.context.kelvin = kelvin;
492
503
  const switchbot = await this.platform.connectBLE(this.accessory, this.device);
493
504
  await this.convertBLEAddress();
494
505
  if (switchbot !== false) {
@@ -496,7 +507,7 @@ export class ColorBulb extends deviceBase {
496
507
  .discover({ model: this.device.bleModel, id: this.device.bleMac })
497
508
  .then(async (device_list: any) => {
498
509
  await this.infoLog(`ColorTemperature: ${this.LightBulb.ColorTemperature}`);
499
- return await device_list[0].setColorTemperature(this.LightBulb.ColorTemperature);
510
+ return await device_list[0].setColorTemperature(kelvin);
500
511
  })
501
512
  .then(async () => {
502
513
  await this.successLog(`ColorTemperature: ${this.LightBulb.ColorTemperature} sent over SwitchBot BLE, sent successfully`);
@@ -46,6 +46,15 @@ export class Contact extends deviceBase {
46
46
  CurrentAmbientLightLevel: CharacteristicValue;
47
47
  };
48
48
 
49
+ // OpenAPI
50
+ deviceStatus!: contactSensorStatus;
51
+
52
+ //Webhook
53
+ webhookContext!: contactSensorWebhookContext;
54
+
55
+ // BLE
56
+ serviceData!: contactSensorServiceData;
57
+
49
58
  // Updates
50
59
  contactUpdateInProgress!: boolean;
51
60
  doContactUpdate!: Subject<void>;
@@ -157,10 +166,12 @@ export class Contact extends deviceBase {
157
166
  }
158
167
 
159
168
  // Retrieve initial values and updateHomekit
169
+ this.debugLog('Retrieve initial values and update Homekit');
160
170
  this.refreshStatus();
161
171
 
162
- // Retrieve initial values and updateHomekit
163
- this.updateHomeKitCharacteristics();
172
+ //regisiter webhook event handler
173
+ this.debugLog('Registering Webhook Event Handler');
174
+ this.registerWebhook();
164
175
 
165
176
  //regisiter webhook event handler
166
177
  this.registerWebhook();
@@ -173,28 +184,28 @@ export class Contact extends deviceBase {
173
184
  });
174
185
  }
175
186
 
176
- async BLEparseStatus(serviceData: contactSensorServiceData): Promise<void> {
187
+ async BLEparseStatus(): Promise<void> {
177
188
  await this.debugLog('BLEparseStatus');
178
189
  // ContactSensorState
179
- this.ContactSensor.ContactSensorState = serviceData.doorState === 'open'
190
+ this.ContactSensor.ContactSensorState = this.serviceData.doorState === 'open'
180
191
  ? this.hap.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED : this.hap.Characteristic.ContactSensorState.CONTACT_DETECTED;
181
192
  await this.debugLog(`ContactSensorState: ${this.ContactSensor.ContactSensorState}`);
182
193
 
183
194
  // MotionDetected
184
195
  if (!this.device.contact?.hide_motionsensor && this.MotionSensor?.Service) {
185
- this.MotionSensor.MotionDetected = serviceData.movement;
196
+ this.MotionSensor.MotionDetected = this.serviceData.movement;
186
197
  await this.debugLog(`MotionDetected: ${this.MotionSensor.MotionDetected}`);
187
198
  }
188
199
  // CurrentAmbientLightLevel
189
200
  if (!this.device.contact?.hide_lightsensor && this.LightSensor?.Service) {
190
201
  const set_minLux = this.device.blindTilt?.set_minLux ?? 1;
191
202
  const set_maxLux = this.device.blindTilt?.set_maxLux ?? 6001;
192
- const lightLevel = serviceData.lightLevel === 'bright' ? set_maxLux : set_minLux;
203
+ const lightLevel = this.serviceData.lightLevel === 'bright' ? set_maxLux : set_minLux;
193
204
  this.LightSensor.CurrentAmbientLightLevel = await this.getLightLevel(lightLevel, set_minLux, set_maxLux, 2);
194
- await this.debugLog(`LightLevel: ${serviceData.lightLevel}, CurrentAmbientLightLevel: ${this.LightSensor.CurrentAmbientLightLevel}`);
205
+ await this.debugLog(`LightLevel: ${this.serviceData.lightLevel}, CurrentAmbientLightLevel: ${this.LightSensor.CurrentAmbientLightLevel}`);
195
206
  }
196
207
  // BatteryLevel
197
- this.Battery.BatteryLevel = serviceData.battery;
208
+ this.Battery.BatteryLevel = this.serviceData.battery;
198
209
  await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`);
199
210
  // StatusLowBattery
200
211
  this.Battery.StatusLowBattery = this.Battery.BatteryLevel < 10
@@ -202,37 +213,37 @@ export class Contact extends deviceBase {
202
213
  await this.debugLog(`StatusLowBattery: ${this.Battery.StatusLowBattery}`);
203
214
  }
204
215
 
205
- async openAPIparseStatus(deviceStatus: contactSensorStatus): Promise<void> {
216
+ async openAPIparseStatus(): Promise<void> {
206
217
  await this.debugLog('openAPIparseStatus');
207
218
  // Contact State
208
- this.ContactSensor.ContactSensorState = deviceStatus.openState === 'open'
219
+ this.ContactSensor.ContactSensorState = this.deviceStatus.openState === 'open'
209
220
  ? this.hap.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED : this.hap.Characteristic.ContactSensorState.CONTACT_DETECTED;
210
221
  await this.debugLog(`ContactSensorState: ${this.ContactSensor.ContactSensorState}`);
211
222
 
212
223
  // MotionDetected
213
224
  if (!this.device.contact?.hide_motionsensor && this.MotionSensor?.Service) {
214
- this.MotionSensor.MotionDetected = deviceStatus.moveDetected;
225
+ this.MotionSensor.MotionDetected = this.deviceStatus.moveDetected;
215
226
  await this.debugLog(`MotionDetected: ${this.MotionSensor.MotionDetected}`);
216
227
  }
217
228
  // Light Level
218
229
  if (!this.device.contact?.hide_lightsensor && this.LightSensor?.Service) {
219
230
  const set_minLux = this.device.blindTilt?.set_minLux ?? 1;
220
231
  const set_maxLux = this.device.blindTilt?.set_maxLux ?? 6001;
221
- const lightLevel = deviceStatus.brightness === 'bright' ? set_maxLux : set_minLux;
232
+ const lightLevel = this.deviceStatus.brightness === 'bright' ? set_maxLux : set_minLux;
222
233
  this.LightSensor.CurrentAmbientLightLevel = await this.getLightLevel(lightLevel, set_minLux, set_maxLux, 2);
223
- await this.debugLog(`LightLevel: ${deviceStatus.brightness}, CurrentAmbientLightLevel: ${this.LightSensor.CurrentAmbientLightLevel}`);
234
+ await this.debugLog(`LightLevel: ${this.deviceStatus.brightness}, CurrentAmbientLightLevel: ${this.LightSensor.CurrentAmbientLightLevel}`);
224
235
  }
225
236
  // BatteryLevel
226
- this.Battery.BatteryLevel = Number(deviceStatus.battery);
237
+ this.Battery.BatteryLevel = this.deviceStatus.battery;
227
238
  await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`);
228
239
  // StatusLowBattery
229
240
  this.Battery.StatusLowBattery = this.Battery.BatteryLevel < 10
230
241
  ? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW : this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
231
242
  await this.debugLog(`StatusLowBattery: ${this.Battery.StatusLowBattery}`);
232
243
  // FirmwareVersion
233
- const version = deviceStatus.version.toString();
234
- await this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
235
- if (deviceStatus.version) {
244
+ if (this.deviceStatus.version) {
245
+ const version = this.deviceStatus.version.toString();
246
+ await this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
236
247
  const deviceVersion = version.replace(/^V|-.*$/g, '') ?? '0.0.0';
237
248
  this.accessory
238
249
  .getService(this.hap.Service.AccessoryInformation)!
@@ -245,24 +256,25 @@ export class Contact extends deviceBase {
245
256
  }
246
257
  }
247
258
 
248
- async parseStatusWebhook(context: contactSensorWebhookContext): Promise<void> {
259
+ async parseStatusWebhook(): Promise<void> {
249
260
  await this.debugLog('parseStatusWebhook');
250
- await this.debugLog(`(detectionState, brightness, openState) = Webhook:(${context.detectionState}, ${context.brightness}, ${context.openState}),`
251
- + ` current:(${this.MotionSensor?.MotionDetected}, ${this.LightSensor?.CurrentAmbientLightLevel}, ${this.ContactSensor.ContactSensorState})`);
261
+ await this.debugLog(`(detectionState, brightness, openState) = Webhook:(${this.webhookContext.detectionState}, ${this.webhookContext.brightness},`
262
+ + ` ${this.webhookContext.openState}), current:(${this.MotionSensor?.MotionDetected}, ${this.LightSensor?.CurrentAmbientLightLevel},`
263
+ + ` ${this.ContactSensor.ContactSensorState})`);
252
264
  // ContactSensorState
253
- this.ContactSensor.ContactSensorState = context.openState === 'open' ? 1 : 0;
265
+ this.ContactSensor.ContactSensorState = this.webhookContext.openState === 'open' ? 1 : 0;
254
266
  await this.debugLog(`ContactSensorState: ${this.ContactSensor.ContactSensorState}`);
255
267
  if (!this.device.contact?.hide_motionsensor && this.MotionSensor?.Service) {
256
268
  // MotionDetected
257
- this.MotionSensor.MotionDetected = context.detectionState === 'DETECTED' ? true : false;
269
+ this.MotionSensor.MotionDetected = this.webhookContext.detectionState === 'DETECTED' ? true : false;
258
270
  await this.debugLog(`MotionDetected: ${this.MotionSensor.MotionDetected}`);
259
271
  }
260
272
  if (!this.device.contact?.hide_lightsensor && this.LightSensor?.Service) {
261
273
  const set_minLux = this.device.blindTilt?.set_minLux ?? 1;
262
274
  const set_maxLux = this.device.blindTilt?.set_maxLux ?? 6001;
263
- const lightLevel = context.brightness === 'bright' ? set_maxLux : set_minLux;
275
+ const lightLevel = this.webhookContext.brightness === 'bright' ? set_maxLux : set_minLux;
264
276
  this.LightSensor.CurrentAmbientLightLevel = await this.getLightLevel(lightLevel, set_minLux, set_maxLux, 2);
265
- await this.debugLog(`LightLevel: ${context.brightness}, CurrentAmbientLightLevel: ${this.LightSensor.CurrentAmbientLightLevel}`);
277
+ await this.debugLog(`LightLevel: ${this.webhookContext.brightness}, CurrentAmbientLightLevel: ${this.LightSensor.CurrentAmbientLightLevel}`);
266
278
  }
267
279
  }
268
280
 
@@ -295,7 +307,8 @@ export class Contact extends deviceBase {
295
307
  const serviceData = await this.monitorAdvertisementPackets(switchbot) as contactSensorServiceData;
296
308
  // Update HomeKit
297
309
  if (serviceData.model === SwitchBotBLEModel.ContactSensor && serviceData.modelName === SwitchBotBLEModelName.ContactSensor) {
298
- await this.BLEparseStatus(serviceData);
310
+ this.serviceData = serviceData;
311
+ await this.BLEparseStatus();
299
312
  await this.updateHomeKitCharacteristics();
300
313
  } else {
301
314
  await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
@@ -313,7 +326,8 @@ export class Contact extends deviceBase {
313
326
  await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);;
314
327
  if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
315
328
  await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
316
- await this.openAPIparseStatus(deviceStatus.body);
329
+ this.deviceStatus = deviceStatus.body;
330
+ await this.openAPIparseStatus();
317
331
  await this.updateHomeKitCharacteristics();
318
332
  } else {
319
333
  await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
@@ -328,10 +342,11 @@ export class Contact extends deviceBase {
328
342
  async registerWebhook() {
329
343
  if (this.device.webhook) {
330
344
  await this.debugLog('is listening webhook.');
331
- this.platform.webhookEventHandler[this.device.deviceId] = async (context: contactSensorWebhookContext) => {
345
+ this.webhookEventHandler[this.device.deviceId] = async (context: contactSensorWebhookContext) => {
332
346
  try {
333
347
  await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
334
- await this.parseStatusWebhook(context);
348
+ this.webhookContext = context;
349
+ await this.parseStatusWebhook();
335
350
  await this.updateHomeKitCharacteristics();
336
351
  } catch (e: any) {
337
352
  await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);