@switchbot/homebridge-switchbot 3.5.1-beta.12 → 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 (150) 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 +40 -21
  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 +42 -31
  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.map +1 -1
  79. package/dist/irdevice/airconditioner.js +19 -72
  80. package/dist/irdevice/airconditioner.js.map +1 -1
  81. package/dist/irdevice/airpurifier.js +4 -4
  82. package/dist/irdevice/airpurifier.js.map +1 -1
  83. package/dist/irdevice/camera.d.ts.map +1 -1
  84. package/dist/irdevice/camera.js +3 -10
  85. package/dist/irdevice/camera.js.map +1 -1
  86. package/dist/irdevice/fan.d.ts.map +1 -1
  87. package/dist/irdevice/fan.js +7 -25
  88. package/dist/irdevice/fan.js.map +1 -1
  89. package/dist/irdevice/irdevice.d.ts +1 -1
  90. package/dist/irdevice/irdevice.js +1 -1
  91. package/dist/irdevice/irdevice.js.map +1 -1
  92. package/dist/irdevice/light.d.ts.map +1 -1
  93. package/dist/irdevice/light.js +9 -27
  94. package/dist/irdevice/light.js.map +1 -1
  95. package/dist/irdevice/other.js +1 -1
  96. package/dist/irdevice/other.js.map +1 -1
  97. package/dist/irdevice/tv.d.ts.map +1 -1
  98. package/dist/irdevice/tv.js +3 -18
  99. package/dist/irdevice/tv.js.map +1 -1
  100. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  101. package/dist/irdevice/vacuumcleaner.js +2 -9
  102. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  103. package/dist/irdevice/waterheater.d.ts.map +1 -1
  104. package/dist/irdevice/waterheater.js +2 -9
  105. package/dist/irdevice/waterheater.js.map +1 -1
  106. package/dist/platform.d.ts +0 -11
  107. package/dist/platform.d.ts.map +1 -1
  108. package/dist/platform.js +11 -184
  109. package/dist/platform.js.map +1 -1
  110. package/dist/settings.d.ts +9 -8
  111. package/dist/settings.d.ts.map +1 -1
  112. package/dist/settings.js.map +1 -1
  113. package/dist/types/bledevicestatus.d.ts +2 -1
  114. package/dist/types/bledevicestatus.d.ts.map +1 -1
  115. package/dist/types/devicestatus.d.ts +1 -1
  116. package/dist/types/devicestatus.d.ts.map +1 -1
  117. package/package.json +9 -9
  118. package/src/device/blindtilt.ts +60 -47
  119. package/src/device/bot.ts +47 -25
  120. package/src/device/ceilinglight.ts +52 -21
  121. package/src/device/colorbulb.ts +45 -31
  122. package/src/device/contact.ts +43 -28
  123. package/src/device/curtain.ts +45 -23
  124. package/src/device/device.ts +226 -96
  125. package/src/device/fan.ts +61 -30
  126. package/src/device/hub.ts +55 -29
  127. package/src/device/humidifier.ts +55 -31
  128. package/src/device/iosensor.ts +45 -27
  129. package/src/device/lightstrip.ts +48 -28
  130. package/src/device/lock.ts +49 -23
  131. package/src/device/meter.ts +54 -26
  132. package/src/device/meterplus.ts +50 -26
  133. package/src/device/motion.ts +45 -24
  134. package/src/device/plug.ts +34 -17
  135. package/src/device/robotvacuumcleaner.ts +49 -23
  136. package/src/device/waterdetector.ts +56 -25
  137. package/src/irdevice/airconditioner.ts +19 -65
  138. package/src/irdevice/airpurifier.ts +4 -4
  139. package/src/irdevice/camera.ts +4 -9
  140. package/src/irdevice/fan.ts +10 -22
  141. package/src/irdevice/irdevice.ts +1 -1
  142. package/src/irdevice/light.ts +10 -24
  143. package/src/irdevice/other.ts +1 -1
  144. package/src/irdevice/tv.ts +5 -16
  145. package/src/irdevice/vacuumcleaner.ts +3 -8
  146. package/src/irdevice/waterheater.ts +3 -8
  147. package/src/platform.ts +11 -184
  148. package/src/settings.ts +10 -8
  149. package/src/types/bledevicestatus.ts +4 -2
  150. 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,32 +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}`);
225
+
203
226
  // Brightness
204
- this.LightBulb.Brightness = deviceStatus.brightness;
227
+ this.LightBulb.Brightness = this.deviceStatus.brightness;
205
228
  await this.debugLog(`Brightness: ${this.LightBulb.Brightness}`);
229
+
206
230
  // ColorTemperature
207
- const miredColorTemperature = Math.round(1000000 / deviceStatus.colorTemperature);
231
+ const miredColorTemperature = Math.round(1000000 / this.deviceStatus.colorTemperature);
208
232
  this.LightBulb.ColorTemperature = Math.max(Math.min(miredColorTemperature, 500), 140);
209
233
  await this.debugLog(`ColorTemperature: ${this.LightBulb.ColorTemperature}`);
234
+
210
235
  // Firmware Version
211
- const version = deviceStatus.version.toString();
212
- await this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
213
- 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, '')}`);
214
239
  const deviceVersion = version.replace(/^V|-.*$/g, '') ?? '0.0.0';
215
240
  this.accessory
216
241
  .getService(this.hap.Service.AccessoryInformation)!
@@ -223,18 +248,21 @@ export class CeilingLight extends deviceBase {
223
248
  }
224
249
  }
225
250
 
226
- async parseStatusWebhook(context: ceilingLightWebhookContext | ceilingLightProWebhookContext): Promise<void> {
251
+ async parseStatusWebhook(): Promise<void> {
227
252
  await this.debugLog('parseStatusWebhook');
228
- await this.debugLog(`(powerState, brightness, colorTemperature) = Webhook:(${context.powerState}, ${context.brightness},`
229
- + ` ${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
+
230
256
  // On
231
- this.LightBulb.On = context.powerState === 'ON' ? true : false;
257
+ this.LightBulb.On = this.webhookContext.powerState === 'ON' ? true : false;
232
258
  await this.debugLog(`On: ${this.LightBulb.On}`);
259
+
233
260
  // Brightness
234
- this.LightBulb.Brightness = context.brightness;
261
+ this.LightBulb.Brightness = this.webhookContext.brightness;
235
262
  await this.debugLog(`Brightness: ${this.LightBulb.Brightness}`);
263
+
236
264
  // ColorTemperature
237
- const miredColorTemperature = Math.round(1000000 / context.colorTemperature);
265
+ const miredColorTemperature = Math.round(1000000 / this.webhookContext.colorTemperature);
238
266
  this.LightBulb.ColorTemperature = Math.max(Math.min(miredColorTemperature, 500), 140);
239
267
  await this.debugLog(`ColorTemperature: ${this.LightBulb.ColorTemperature}`);
240
268
  }
@@ -268,7 +296,8 @@ export class CeilingLight extends deviceBase {
268
296
  // Update HomeKit
269
297
  if ((serviceData.model === SwitchBotBLEModel.CeilingLight || SwitchBotBLEModel.CeilingLightPro)
270
298
  && serviceData.modelName === SwitchBotBLEModelName.CeilingLight || SwitchBotBLEModelName.CeilingLightPro) {
271
- await this.BLEparseStatus(serviceData);
299
+ this.serviceData = serviceData;
300
+ await this.BLEparseStatus();
272
301
  await this.updateHomeKitCharacteristics();
273
302
  } else {
274
303
  await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
@@ -286,7 +315,8 @@ export class CeilingLight extends deviceBase {
286
315
  await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);;
287
316
  if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
288
317
  await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
289
- await this.openAPIparseStatus(deviceStatus.body);
318
+ this.deviceStatus = deviceStatus.body;
319
+ await this.openAPIparseStatus();
290
320
  await this.updateHomeKitCharacteristics();
291
321
  } else {
292
322
  await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
@@ -301,10 +331,11 @@ export class CeilingLight extends deviceBase {
301
331
  async registerWebhook() {
302
332
  if (this.device.webhook) {
303
333
  await this.debugLog('is listening webhook.');
304
- this.platform.webhookEventHandler[this.device.deviceId] = async (context: ceilingLightWebhookContext | ceilingLightProWebhookContext) => {
334
+ this.webhookEventHandler[this.device.deviceId] = async (context: ceilingLightWebhookContext | ceilingLightProWebhookContext) => {
305
335
  try {
306
336
  await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
307
- await this.parseStatusWebhook(context);
337
+ this.webhookContext = context;
338
+ await this.parseStatusWebhook();
308
339
  await this.updateHomeKitCharacteristics();
309
340
  } catch (e: any) {
310
341
  await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
@@ -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,7 +211,7 @@ export class ColorBulb extends deviceBase {
200
211
  this.LightBulb.Saturation = saturation;
201
212
  await this.debugLog(`Saturation: ${this.LightBulb.Saturation}`);
202
213
  // ColorTemperature
203
- const miredColorTemperature = Math.round(1000000 / serviceData.color_temperature);
214
+ const miredColorTemperature = Math.round(1000000 / this.serviceData.color_temperature);
204
215
  this.LightBulb.ColorTemperature = Math.max(Math.min(miredColorTemperature, 500), 140);
205
216
  await this.debugLog(`ColorTemperature: ${this.LightBulb.ColorTemperature}`);
206
217
  }
@@ -208,17 +219,17 @@ export class ColorBulb extends deviceBase {
208
219
  /**
209
220
  * Parse the device status from the SwitchBot OpenAPI
210
221
  */
211
- async openAPIparseStatus(deviceStatus: colorBulbStatus): Promise<void> {
222
+ async openAPIparseStatus(): Promise<void> {
212
223
  await this.debugLog('openAPIparseStatus');
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
227
  // Brightness
217
- this.LightBulb.Brightness = deviceStatus.brightness;
228
+ this.LightBulb.Brightness = this.deviceStatus.brightness;
218
229
  await this.debugLog(`Brightness: ${this.LightBulb.Brightness}`);
219
230
  // Color, Hue & Brightness
220
- await this.debugLog(`color: ${JSON.stringify(deviceStatus.color)}`);
221
- 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(':');
222
233
  await this.debugLog(`red: ${JSON.stringify(red)}, green: ${JSON.stringify(green)}, blue: ${JSON.stringify(blue)}`);
223
234
  const [hue, saturation] = rgb2hs(red, green, blue);
224
235
  await this.debugLog(`hs: ${JSON.stringify(rgb2hs(red, green, blue))}`);
@@ -229,13 +240,13 @@ export class ColorBulb extends deviceBase {
229
240
  this.LightBulb.Saturation = saturation;
230
241
  await this.debugLog(`Saturation: ${this.LightBulb.Saturation}`);
231
242
  // ColorTemperature
232
- const miredColorTemperature = Math.round(1000000 / deviceStatus.colorTemperature);
243
+ const miredColorTemperature = Math.round(1000000 / this.deviceStatus.colorTemperature);
233
244
  this.LightBulb.ColorTemperature = Math.max(Math.min(miredColorTemperature, 500), 140);
234
245
  await this.debugLog(`ColorTemperature: ${this.LightBulb.ColorTemperature}`);
235
246
  // Firmware Version
236
- const version = deviceStatus.version.toString();
237
- await this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
238
- 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, '')}`);
239
250
  const deviceVersion = version.replace(/^V|-.*$/g, '') ?? '0.0.0';
240
251
  this.accessory
241
252
  .getService(this.hap.Service.AccessoryInformation)!
@@ -248,20 +259,20 @@ export class ColorBulb extends deviceBase {
248
259
  }
249
260
  }
250
261
 
251
- async parseStatusWebhook(context: colorBulbWebhookContext): Promise<void> {
262
+ async parseStatusWebhook(): Promise<void> {
252
263
  await this.debugLog('parseStatusWebhook');
253
- await this.debugLog(`(powerState, brightness, color, colorTemperature) = Webhook:(${context.powerState}, ${context.brightness}, ${context.color},`
254
- + ` ${context.colorTemperature}), current:(${this.LightBulb.On}, ${this.LightBulb.Brightness}, ${this.LightBulb.Hue},`
255
- + ` ${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})`);
256
267
  // On
257
- this.LightBulb.On = context.powerState === 'ON' ? true : false;
268
+ this.LightBulb.On = this.webhookContext.powerState === 'ON' ? true : false;
258
269
  await this.debugLog(`On: ${this.LightBulb.On}`);
259
270
  // Brightness
260
- this.LightBulb.Brightness = context.brightness;
271
+ this.LightBulb.Brightness = this.webhookContext.brightness;
261
272
  await this.debugLog(`Brightness: ${this.LightBulb.Brightness}`);
262
273
  // Color, Hue & Brightness
263
- await this.debugLog(`color: ${JSON.stringify(context.color)}`);
264
- 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(':');
265
276
  await this.debugLog(`red: ${JSON.stringify(red)}, green: ${JSON.stringify(green)}, blue: ${JSON.stringify(blue)}`);
266
277
  const [hue, saturation] = rgb2hs(red, green, blue);
267
278
  await this.debugLog(`hs: ${JSON.stringify(rgb2hs(red, green, blue))}`);
@@ -272,7 +283,7 @@ export class ColorBulb extends deviceBase {
272
283
  this.LightBulb.Saturation = saturation;
273
284
  await this.debugLog(`Saturation: ${this.LightBulb.Saturation}`);
274
285
  // ColorTemperature
275
- const miredColorTemperature = Math.round(1000000 / context.colorTemperature);
286
+ const miredColorTemperature = Math.round(1000000 / this.webhookContext.colorTemperature);
276
287
  this.LightBulb.ColorTemperature = Math.max(Math.min(miredColorTemperature, 500), 140);
277
288
  await this.debugLog(`ColorTemperature: ${this.LightBulb.ColorTemperature}`);
278
289
  }
@@ -306,7 +317,8 @@ export class ColorBulb extends deviceBase {
306
317
  const serviceData = await this.monitorAdvertisementPackets(switchbot) as colorBulbServiceData;
307
318
  // Update HomeKit
308
319
  if (serviceData.model === SwitchBotBLEModel.ColorBulb && serviceData.modelName === SwitchBotBLEModelName.ColorBulb) {
309
- await this.BLEparseStatus(serviceData);
320
+ this.serviceData = serviceData;
321
+ await this.BLEparseStatus();
310
322
  await this.updateHomeKitCharacteristics();
311
323
  } else {
312
324
  await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
@@ -324,7 +336,8 @@ export class ColorBulb extends deviceBase {
324
336
  await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);;
325
337
  if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
326
338
  await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
327
- await this.openAPIparseStatus(deviceStatus.body);
339
+ this.deviceStatus = deviceStatus.body;
340
+ await this.openAPIparseStatus();
328
341
  await this.updateHomeKitCharacteristics();
329
342
  } else {
330
343
  await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
@@ -339,10 +352,11 @@ export class ColorBulb extends deviceBase {
339
352
  async registerWebhook() {
340
353
  if (this.device.webhook) {
341
354
  await this.debugLog('is listening webhook.');
342
- this.platform.webhookEventHandler[this.device.deviceId] = async (context: colorBulbWebhookContext) => {
355
+ this.webhookEventHandler[this.device.deviceId] = async (context: colorBulbWebhookContext) => {
343
356
  try {
344
357
  await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
345
- await this.parseStatusWebhook(context);
358
+ this.webhookContext = context;
359
+ await this.parseStatusWebhook();
346
360
  await this.updateHomeKitCharacteristics();
347
361
  } catch (e: any) {
348
362
  await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
@@ -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}`);