@switchbot/homebridge-switchbot 3.5.1-beta.9 → 3.5.2-beta.0

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 (158) 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 +61 -47
  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 +60 -26
  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 +48 -30
  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 +54 -37
  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 +46 -32
  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 +42 -28
  26. package/dist/device/curtain.js.map +1 -1
  27. package/dist/device/device.d.ts +7 -4
  28. package/dist/device/device.d.ts.map +1 -1
  29. package/dist/device/device.js +61 -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 +54 -33
  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 +50 -31
  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 +48 -29
  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 +45 -28
  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 +47 -29
  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 +39 -26
  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 +44 -28
  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 +43 -28
  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 +38 -25
  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 +31 -18
  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 +38 -23
  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 -27
  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 +14 -14
  125. package/src/device/blindtilt.ts +65 -49
  126. package/src/device/bot.ts +80 -27
  127. package/src/device/ceilinglight.ts +58 -32
  128. package/src/device/colorbulb.ts +58 -44
  129. package/src/device/contact.ts +50 -32
  130. package/src/device/curtain.ts +52 -29
  131. package/src/device/device.ts +63 -135
  132. package/src/device/fan.ts +68 -34
  133. package/src/device/hub.ts +63 -34
  134. package/src/device/humidifier.ts +61 -34
  135. package/src/device/iosensor.ts +52 -31
  136. package/src/device/lightstrip.ts +53 -30
  137. package/src/device/lock.ts +56 -27
  138. package/src/device/meter.ts +61 -30
  139. package/src/device/meterplus.ts +57 -30
  140. package/src/device/motion.ts +51 -27
  141. package/src/device/plug.ts +39 -19
  142. package/src/device/robotvacuumcleaner.ts +54 -25
  143. package/src/device/waterdetector.ts +63 -29
  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
@@ -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;
@@ -46,6 +55,9 @@ export class ColorBulb 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
 
@@ -79,10 +91,10 @@ export class ColorBulb extends deviceBase {
79
91
  });
80
92
  accessory.configureController(this.AdaptiveLightingController);
81
93
  accessory.context.adaptiveLighting = true;
82
- this.debugLog(`${device.deviceType}: ${this.accessory.displayName} adaptiveLighting: ${accessory.context.adaptiveLighting},`
94
+ this.debugLog(`adaptiveLighting: ${accessory.context.adaptiveLighting},`
83
95
  + ` adaptiveLightingShift: ${this.adaptiveLightingShift}`);
84
96
  }
85
- this.debugLog(`${device.deviceType}: ${this.accessory.displayName} adaptiveLightingShift: ${this.adaptiveLightingShift}`);
97
+ this.debugLog(`adaptiveLightingShift: ${this.adaptiveLightingShift}`);
86
98
 
87
99
  // Initialize LightBulb Characteristics
88
100
  this.LightBulb.Service
@@ -143,10 +155,12 @@ export class ColorBulb extends deviceBase {
143
155
  .onSet(this.SaturationSet.bind(this));
144
156
 
145
157
  // Retrieve initial values and updateHomekit
158
+ this.debugLog('Retrieve initial values and update Homekit');
146
159
  this.refreshStatus();
147
160
 
148
- // Update Homekit
149
- this.updateHomeKitCharacteristics();
161
+ //regisiter webhook event handler
162
+ this.debugLog('Registering Webhook Event Handler');
163
+ this.registerWebhook();
150
164
 
151
165
  //regisiter webhook event handler
152
166
  this.registerWebhook();
@@ -181,18 +195,18 @@ export class ColorBulb extends deviceBase {
181
195
  /**
182
196
  * Parse the device status from the SwitchBotBLE API
183
197
  */
184
- async BLEparseStatus(serviceData: colorBulbServiceData): Promise<void> {
198
+ async BLEparseStatus(): Promise<void> {
185
199
  await this.debugLog('BLEparseStatus');
186
200
  // On
187
- this.LightBulb.On = serviceData.power;
201
+ this.LightBulb.On = this.serviceData.power;
188
202
  await this.debugLog(`On: ${this.LightBulb.On}`);
189
203
  // Brightness
190
- this.LightBulb.Brightness = serviceData.brightness;
204
+ this.LightBulb.Brightness = this.serviceData.brightness;
191
205
  await this.debugLog(`Brightness: ${this.LightBulb.Brightness}`);
192
206
  // 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))}`);
207
+ await this.debugLog(`red: ${this.serviceData.red}, green: ${this.serviceData.green}, blue: ${this.serviceData.blue}`);
208
+ const [hue, saturation] = rgb2hs(this.serviceData.red, this.serviceData.green, this.serviceData.blue);
209
+ await this.debugLog(`hs: ${JSON.stringify(rgb2hs(this.serviceData.red, this.serviceData.green, this.serviceData.blue))}`);
196
210
  // Hue
197
211
  this.LightBulb.Hue = hue;
198
212
  await this.debugLog(`Hue: ${this.LightBulb.Hue}`);
@@ -200,48 +214,42 @@ export class ColorBulb extends deviceBase {
200
214
  this.LightBulb.Saturation = saturation;
201
215
  await this.debugLog(`Saturation: ${this.LightBulb.Saturation}`);
202
216
  // ColorTemperature
203
- this.LightBulb.ColorTemperature = Math.max(Math.min(serviceData.color_temperature, 500), 140);
217
+ const miredColorTemperature = Math.round(1000000 / this.serviceData.color_temperature);
218
+ this.LightBulb.ColorTemperature = Math.max(Math.min(miredColorTemperature, 500), 140);
204
219
  await this.debugLog(`ColorTemperature: ${this.LightBulb.ColorTemperature}`);
205
220
  }
206
221
 
207
222
  /**
208
223
  * Parse the device status from the SwitchBot OpenAPI
209
224
  */
210
- async openAPIparseStatus(deviceStatus: colorBulbStatus): Promise<void> {
225
+ async openAPIparseStatus(): Promise<void> {
211
226
  await this.debugLog('openAPIparseStatus');
212
-
213
227
  // On
214
- this.LightBulb.On = deviceStatus.power === 'on' ? true : false;
228
+ this.LightBulb.On = this.deviceStatus.power === 'on' ? true : false;
215
229
  await this.debugLog(`On: ${this.LightBulb.On}`);
216
-
217
230
  // Brightness
218
- this.LightBulb.Brightness = deviceStatus.brightness;
231
+ this.LightBulb.Brightness = this.deviceStatus.brightness;
219
232
  await this.debugLog(`Brightness: ${this.LightBulb.Brightness}`);
220
-
221
233
  // Color, Hue & Brightness
222
- await this.debugLog(`color: ${JSON.stringify(deviceStatus.color)}`);
223
- const [red, green, blue] = deviceStatus.color.split(':');
234
+ await this.debugLog(`color: ${JSON.stringify(this.deviceStatus.color)}`);
235
+ const [red, green, blue] = this.deviceStatus.color.split(':');
224
236
  await this.debugLog(`red: ${JSON.stringify(red)}, green: ${JSON.stringify(green)}, blue: ${JSON.stringify(blue)}`);
225
237
  const [hue, saturation] = rgb2hs(red, green, blue);
226
238
  await this.debugLog(`hs: ${JSON.stringify(rgb2hs(red, green, blue))}`);
227
-
228
239
  // Hue
229
240
  this.LightBulb.Hue = hue;
230
241
  await this.debugLog(`Hue: ${this.LightBulb.Hue}`);
231
-
232
242
  // Saturation
233
243
  this.LightBulb.Saturation = saturation;
234
244
  await this.debugLog(`Saturation: ${this.LightBulb.Saturation}`);
235
-
236
245
  // ColorTemperature
237
- const miredColorTemperature = Math.round(1000000 / deviceStatus.colorTemperature);
246
+ const miredColorTemperature = Math.round(1000000 / this.deviceStatus.colorTemperature);
238
247
  this.LightBulb.ColorTemperature = Math.max(Math.min(miredColorTemperature, 500), 140);
239
248
  await this.debugLog(`ColorTemperature: ${this.LightBulb.ColorTemperature}`);
240
-
241
249
  // Firmware Version
242
- const version = deviceStatus.version.toString();
243
- await this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
244
- if (deviceStatus.version) {
250
+ if (this.deviceStatus.version) {
251
+ const version = this.deviceStatus.version.toString();
252
+ await this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
245
253
  const deviceVersion = version.replace(/^V|-.*$/g, '') ?? '0.0.0';
246
254
  this.accessory
247
255
  .getService(this.hap.Service.AccessoryInformation)!
@@ -249,25 +257,25 @@ export class ColorBulb extends deviceBase {
249
257
  .setCharacteristic(this.hap.Characteristic.FirmwareRevision, deviceVersion)
250
258
  .getCharacteristic(this.hap.Characteristic.FirmwareRevision)
251
259
  .updateValue(deviceVersion);
252
- this.accessory.context.deviceVersion = deviceVersion;
253
- await this.debugSuccessLog(`deviceVersion: ${this.accessory.context.deviceVersion}`);
260
+ this.accessory.context.version = deviceVersion;
261
+ await this.debugSuccessLog(`version: ${this.accessory.context.version}`);
254
262
  }
255
263
  }
256
264
 
257
- async parseStatusWebhook(context: colorBulbWebhookContext): Promise<void> {
265
+ async parseStatusWebhook(): Promise<void> {
258
266
  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})`);
267
+ await this.debugLog(`(powerState, brightness, color, colorTemperature) = Webhook:(${this.webhookContext.powerState},`
268
+ + ` ${this.webhookContext.brightness}, ${this.webhookContext.color}, ${this.webhookContext.colorTemperature}), current:(${this.LightBulb.On},`
269
+ + ` ${this.LightBulb.Brightness}, ${this.LightBulb.Hue}, ${this.LightBulb.Saturation}, ${this.LightBulb.ColorTemperature})`);
262
270
  // On
263
- this.LightBulb.On = context.powerState === 'ON' ? true : false;
271
+ this.LightBulb.On = this.webhookContext.powerState === 'ON' ? true : false;
264
272
  await this.debugLog(`On: ${this.LightBulb.On}`);
265
273
  // Brightness
266
- this.LightBulb.Brightness = context.brightness;
274
+ this.LightBulb.Brightness = this.webhookContext.brightness;
267
275
  await this.debugLog(`Brightness: ${this.LightBulb.Brightness}`);
268
276
  // Color, Hue & Brightness
269
- await this.debugLog(`color: ${JSON.stringify(context.color)}`);
270
- const [red, green, blue] = context.color.split(':');
277
+ await this.debugLog(`color: ${JSON.stringify(this.webhookContext.color)}`);
278
+ const [red, green, blue] = this.webhookContext.color.split(':');
271
279
  await this.debugLog(`red: ${JSON.stringify(red)}, green: ${JSON.stringify(green)}, blue: ${JSON.stringify(blue)}`);
272
280
  const [hue, saturation] = rgb2hs(red, green, blue);
273
281
  await this.debugLog(`hs: ${JSON.stringify(rgb2hs(red, green, blue))}`);
@@ -278,7 +286,8 @@ export class ColorBulb extends deviceBase {
278
286
  this.LightBulb.Saturation = saturation;
279
287
  await this.debugLog(`Saturation: ${this.LightBulb.Saturation}`);
280
288
  // ColorTemperature
281
- this.LightBulb.ColorTemperature = context.colorTemperature;
289
+ const miredColorTemperature = Math.round(1000000 / this.webhookContext.colorTemperature);
290
+ this.LightBulb.ColorTemperature = Math.max(Math.min(miredColorTemperature, 500), 140);
282
291
  await this.debugLog(`ColorTemperature: ${this.LightBulb.ColorTemperature}`);
283
292
  }
284
293
 
@@ -311,7 +320,8 @@ export class ColorBulb extends deviceBase {
311
320
  const serviceData = await this.monitorAdvertisementPackets(switchbot) as colorBulbServiceData;
312
321
  // Update HomeKit
313
322
  if (serviceData.model === SwitchBotBLEModel.ColorBulb && serviceData.modelName === SwitchBotBLEModelName.ColorBulb) {
314
- await this.BLEparseStatus(serviceData);
323
+ this.serviceData = serviceData;
324
+ await this.BLEparseStatus();
315
325
  await this.updateHomeKitCharacteristics();
316
326
  } else {
317
327
  await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
@@ -329,7 +339,8 @@ export class ColorBulb extends deviceBase {
329
339
  await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);;
330
340
  if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
331
341
  await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
332
- await this.openAPIparseStatus(deviceStatus.body);
342
+ this.deviceStatus = deviceStatus.body;
343
+ await this.openAPIparseStatus();
333
344
  await this.updateHomeKitCharacteristics();
334
345
  } else {
335
346
  await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
@@ -347,7 +358,8 @@ export class ColorBulb extends deviceBase {
347
358
  this.platform.webhookEventHandler[this.device.deviceId] = async (context: colorBulbWebhookContext) => {
348
359
  try {
349
360
  await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
350
- await this.parseStatusWebhook(context);
361
+ this.webhookContext = context;
362
+ await this.parseStatusWebhook();
351
363
  await this.updateHomeKitCharacteristics();
352
364
  } catch (e: any) {
353
365
  await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
@@ -464,7 +476,7 @@ export class ColorBulb extends deviceBase {
464
476
  switchbot
465
477
  .discover({ model: this.device.bleModel, id: this.device.bleMac })
466
478
  .then(async (device_list: any) => {
467
- await this.infoLog(`${this.accessory.displayName} Target Brightness: ${this.LightBulb.Brightness}`);
479
+ await this.infoLog(`Target Brightness: ${this.LightBulb.Brightness}`);
468
480
  return await device_list[0].setBrightness(this.LightBulb.Brightness);
469
481
  })
470
482
  .then(async () => {
@@ -489,6 +501,8 @@ export class ColorBulb extends deviceBase {
489
501
  async BLEpushColorTemperatureChanges(): Promise<void> {
490
502
  await this.debugLog('BLEpushColorTemperatureChanges');
491
503
  if (this.LightBulb.ColorTemperature !== this.accessory.context.ColorTemperature) {
504
+ const kelvin = Math.round(1000000 / Number(this.LightBulb.ColorTemperature));
505
+ this.accessory.context.kelvin = kelvin;
492
506
  const switchbot = await this.platform.connectBLE(this.accessory, this.device);
493
507
  await this.convertBLEAddress();
494
508
  if (switchbot !== false) {
@@ -496,7 +510,7 @@ export class ColorBulb extends deviceBase {
496
510
  .discover({ model: this.device.bleModel, id: this.device.bleMac })
497
511
  .then(async (device_list: any) => {
498
512
  await this.infoLog(`ColorTemperature: ${this.LightBulb.ColorTemperature}`);
499
- return await device_list[0].setColorTemperature(this.LightBulb.ColorTemperature);
513
+ return await device_list[0].setColorTemperature(kelvin);
500
514
  })
501
515
  .then(async () => {
502
516
  await this.successLog(`ColorTemperature: ${this.LightBulb.ColorTemperature} sent over SwitchBot BLE, sent successfully`);
@@ -823,7 +837,7 @@ export class ColorBulb extends deviceBase {
823
837
  }
824
838
 
825
839
  async BLERefreshConnection(switchbot: any): Promise<void> {
826
- await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${JSON.stringify(switchbot)}`);
840
+ await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
827
841
  if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
828
842
  await this.warnLog('Using OpenAPI Connection to Refresh Status');
829
843
  await this.openAPIRefreshStatus();
@@ -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>;
@@ -56,6 +65,9 @@ export class Contact extends deviceBase {
56
65
  device: device & devicesConfig,
57
66
  ) {
58
67
  super(platform, accessory, device);
68
+ // Set category
69
+ accessory.category = this.hap.Categories.SENSOR;
70
+
59
71
  // this is subject we use to track when we need to POST changes to the SwitchBot API
60
72
  this.doContactUpdate = new Subject();
61
73
  this.contactUpdateInProgress = false;
@@ -107,7 +119,7 @@ export class Contact extends deviceBase {
107
119
  // Initialize Motion Sensor Service
108
120
  if (this.device.contact?.hide_motionsensor) {
109
121
  if (this.MotionSensor) {
110
- this.debugLog(`${device.deviceType}: ${accessory.displayName} Removing Motion Sensor Service`);
122
+ this.debugLog('Removing Motion Sensor Service');
111
123
  this.MotionSensor.Service = accessory.getService(this.hap.Service.MotionSensor) as Service;
112
124
  accessory.removeService(this.MotionSensor.Service);
113
125
  }
@@ -133,7 +145,7 @@ export class Contact extends deviceBase {
133
145
  // Initialize Light Sensor Service
134
146
  if (device.contact?.hide_lightsensor) {
135
147
  if (this.LightSensor) {
136
- this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Light Sensor Service`);
148
+ this.debugLog('Removing Light Sensor Service');
137
149
  this.LightSensor.Service = accessory.getService(this.hap.Service.LightSensor) as Service;
138
150
  accessory.removeService(this.LightSensor.Service);
139
151
  }
@@ -157,10 +169,12 @@ export class Contact extends deviceBase {
157
169
  }
158
170
 
159
171
  // Retrieve initial values and updateHomekit
172
+ this.debugLog('Retrieve initial values and update Homekit');
160
173
  this.refreshStatus();
161
174
 
162
- // Retrieve initial values and updateHomekit
163
- this.updateHomeKitCharacteristics();
175
+ //regisiter webhook event handler
176
+ this.debugLog('Registering Webhook Event Handler');
177
+ this.registerWebhook();
164
178
 
165
179
  //regisiter webhook event handler
166
180
  this.registerWebhook();
@@ -173,28 +187,28 @@ export class Contact extends deviceBase {
173
187
  });
174
188
  }
175
189
 
176
- async BLEparseStatus(serviceData: contactSensorServiceData): Promise<void> {
190
+ async BLEparseStatus(): Promise<void> {
177
191
  await this.debugLog('BLEparseStatus');
178
192
  // ContactSensorState
179
- this.ContactSensor.ContactSensorState = serviceData.doorState === 'open'
193
+ this.ContactSensor.ContactSensorState = this.serviceData.doorState === 'open'
180
194
  ? this.hap.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED : this.hap.Characteristic.ContactSensorState.CONTACT_DETECTED;
181
195
  await this.debugLog(`ContactSensorState: ${this.ContactSensor.ContactSensorState}`);
182
196
 
183
197
  // MotionDetected
184
198
  if (!this.device.contact?.hide_motionsensor && this.MotionSensor?.Service) {
185
- this.MotionSensor.MotionDetected = serviceData.movement;
199
+ this.MotionSensor.MotionDetected = this.serviceData.movement;
186
200
  await this.debugLog(`MotionDetected: ${this.MotionSensor.MotionDetected}`);
187
201
  }
188
202
  // CurrentAmbientLightLevel
189
203
  if (!this.device.contact?.hide_lightsensor && this.LightSensor?.Service) {
190
204
  const set_minLux = this.device.blindTilt?.set_minLux ?? 1;
191
205
  const set_maxLux = this.device.blindTilt?.set_maxLux ?? 6001;
192
- const lightLevel = serviceData.lightLevel === 'bright' ? set_maxLux : set_minLux;
206
+ const lightLevel = this.serviceData.lightLevel === 'bright' ? set_maxLux : set_minLux;
193
207
  this.LightSensor.CurrentAmbientLightLevel = await this.getLightLevel(lightLevel, set_minLux, set_maxLux, 2);
194
- await this.debugLog(`LightLevel: ${serviceData.lightLevel}, CurrentAmbientLightLevel: ${this.LightSensor.CurrentAmbientLightLevel}`);
208
+ await this.debugLog(`LightLevel: ${this.serviceData.lightLevel}, CurrentAmbientLightLevel: ${this.LightSensor.CurrentAmbientLightLevel}`);
195
209
  }
196
210
  // BatteryLevel
197
- this.Battery.BatteryLevel = serviceData.battery;
211
+ this.Battery.BatteryLevel = this.serviceData.battery;
198
212
  await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`);
199
213
  // StatusLowBattery
200
214
  this.Battery.StatusLowBattery = this.Battery.BatteryLevel < 10
@@ -202,37 +216,37 @@ export class Contact extends deviceBase {
202
216
  await this.debugLog(`StatusLowBattery: ${this.Battery.StatusLowBattery}`);
203
217
  }
204
218
 
205
- async openAPIparseStatus(deviceStatus: contactSensorStatus): Promise<void> {
219
+ async openAPIparseStatus(): Promise<void> {
206
220
  await this.debugLog('openAPIparseStatus');
207
221
  // Contact State
208
- this.ContactSensor.ContactSensorState = deviceStatus.openState === 'open'
222
+ this.ContactSensor.ContactSensorState = this.deviceStatus.openState === 'open'
209
223
  ? this.hap.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED : this.hap.Characteristic.ContactSensorState.CONTACT_DETECTED;
210
224
  await this.debugLog(`ContactSensorState: ${this.ContactSensor.ContactSensorState}`);
211
225
 
212
226
  // MotionDetected
213
227
  if (!this.device.contact?.hide_motionsensor && this.MotionSensor?.Service) {
214
- this.MotionSensor.MotionDetected = deviceStatus.moveDetected;
228
+ this.MotionSensor.MotionDetected = this.deviceStatus.moveDetected;
215
229
  await this.debugLog(`MotionDetected: ${this.MotionSensor.MotionDetected}`);
216
230
  }
217
231
  // Light Level
218
232
  if (!this.device.contact?.hide_lightsensor && this.LightSensor?.Service) {
219
233
  const set_minLux = this.device.blindTilt?.set_minLux ?? 1;
220
234
  const set_maxLux = this.device.blindTilt?.set_maxLux ?? 6001;
221
- const lightLevel = deviceStatus.brightness === 'bright' ? set_maxLux : set_minLux;
235
+ const lightLevel = this.deviceStatus.brightness === 'bright' ? set_maxLux : set_minLux;
222
236
  this.LightSensor.CurrentAmbientLightLevel = await this.getLightLevel(lightLevel, set_minLux, set_maxLux, 2);
223
- await this.debugLog(`LightLevel: ${deviceStatus.brightness}, CurrentAmbientLightLevel: ${this.LightSensor.CurrentAmbientLightLevel}`);
237
+ await this.debugLog(`LightLevel: ${this.deviceStatus.brightness}, CurrentAmbientLightLevel: ${this.LightSensor.CurrentAmbientLightLevel}`);
224
238
  }
225
239
  // BatteryLevel
226
- this.Battery.BatteryLevel = Number(deviceStatus.battery);
240
+ this.Battery.BatteryLevel = this.deviceStatus.battery;
227
241
  await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`);
228
242
  // StatusLowBattery
229
243
  this.Battery.StatusLowBattery = this.Battery.BatteryLevel < 10
230
244
  ? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW : this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
231
245
  await this.debugLog(`StatusLowBattery: ${this.Battery.StatusLowBattery}`);
232
246
  // FirmwareVersion
233
- const version = deviceStatus.version.toString();
234
- await this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
235
- 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, '')}`);
236
250
  const deviceVersion = version.replace(/^V|-.*$/g, '') ?? '0.0.0';
237
251
  this.accessory
238
252
  .getService(this.hap.Service.AccessoryInformation)!
@@ -240,29 +254,30 @@ export class Contact extends deviceBase {
240
254
  .setCharacteristic(this.hap.Characteristic.FirmwareRevision, deviceVersion)
241
255
  .getCharacteristic(this.hap.Characteristic.FirmwareRevision)
242
256
  .updateValue(deviceVersion);
243
- this.accessory.context.deviceVersion = deviceVersion;
244
- await this.debugLog(`deviceVersion: ${this.accessory.context.deviceVersion}`);
257
+ this.accessory.context.version = deviceVersion;
258
+ await this.debugLog(`version: ${this.accessory.context.version}`);
245
259
  }
246
260
  }
247
261
 
248
- async parseStatusWebhook(context: contactSensorWebhookContext): Promise<void> {
262
+ async parseStatusWebhook(): Promise<void> {
249
263
  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})`);
264
+ await this.debugLog(`(detectionState, brightness, openState) = Webhook:(${this.webhookContext.detectionState}, ${this.webhookContext.brightness},`
265
+ + ` ${this.webhookContext.openState}), current:(${this.MotionSensor?.MotionDetected}, ${this.LightSensor?.CurrentAmbientLightLevel},`
266
+ + ` ${this.ContactSensor.ContactSensorState})`);
252
267
  // ContactSensorState
253
- this.ContactSensor.ContactSensorState = context.openState === 'open' ? 1 : 0;
268
+ this.ContactSensor.ContactSensorState = this.webhookContext.openState === 'open' ? 1 : 0;
254
269
  await this.debugLog(`ContactSensorState: ${this.ContactSensor.ContactSensorState}`);
255
270
  if (!this.device.contact?.hide_motionsensor && this.MotionSensor?.Service) {
256
271
  // MotionDetected
257
- this.MotionSensor.MotionDetected = context.detectionState === 'DETECTED' ? true : false;
272
+ this.MotionSensor.MotionDetected = this.webhookContext.detectionState === 'DETECTED' ? true : false;
258
273
  await this.debugLog(`MotionDetected: ${this.MotionSensor.MotionDetected}`);
259
274
  }
260
275
  if (!this.device.contact?.hide_lightsensor && this.LightSensor?.Service) {
261
276
  const set_minLux = this.device.blindTilt?.set_minLux ?? 1;
262
277
  const set_maxLux = this.device.blindTilt?.set_maxLux ?? 6001;
263
- const lightLevel = context.brightness === 'bright' ? set_maxLux : set_minLux;
278
+ const lightLevel = this.webhookContext.brightness === 'bright' ? set_maxLux : set_minLux;
264
279
  this.LightSensor.CurrentAmbientLightLevel = await this.getLightLevel(lightLevel, set_minLux, set_maxLux, 2);
265
- await this.debugLog(`LightLevel: ${context.brightness}, CurrentAmbientLightLevel: ${this.LightSensor.CurrentAmbientLightLevel}`);
280
+ await this.debugLog(`LightLevel: ${this.webhookContext.brightness}, CurrentAmbientLightLevel: ${this.LightSensor.CurrentAmbientLightLevel}`);
266
281
  }
267
282
  }
268
283
 
@@ -295,7 +310,8 @@ export class Contact extends deviceBase {
295
310
  const serviceData = await this.monitorAdvertisementPackets(switchbot) as contactSensorServiceData;
296
311
  // Update HomeKit
297
312
  if (serviceData.model === SwitchBotBLEModel.ContactSensor && serviceData.modelName === SwitchBotBLEModelName.ContactSensor) {
298
- await this.BLEparseStatus(serviceData);
313
+ this.serviceData = serviceData;
314
+ await this.BLEparseStatus();
299
315
  await this.updateHomeKitCharacteristics();
300
316
  } else {
301
317
  await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
@@ -313,7 +329,8 @@ export class Contact extends deviceBase {
313
329
  await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);;
314
330
  if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
315
331
  await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
316
- await this.openAPIparseStatus(deviceStatus.body);
332
+ this.deviceStatus = deviceStatus.body;
333
+ await this.openAPIparseStatus();
317
334
  await this.updateHomeKitCharacteristics();
318
335
  } else {
319
336
  await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
@@ -331,7 +348,8 @@ export class Contact extends deviceBase {
331
348
  this.platform.webhookEventHandler[this.device.deviceId] = async (context: contactSensorWebhookContext) => {
332
349
  try {
333
350
  await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
334
- await this.parseStatusWebhook(context);
351
+ this.webhookContext = context;
352
+ await this.parseStatusWebhook();
335
353
  await this.updateHomeKitCharacteristics();
336
354
  } catch (e: any) {
337
355
  await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
@@ -368,7 +386,7 @@ export class Contact extends deviceBase {
368
386
  }
369
387
 
370
388
  async BLERefreshConnection(switchbot: any): Promise<void> {
371
- await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${JSON.stringify(switchbot)}`);
389
+ await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
372
390
  if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
373
391
  await this.warnLog('Using OpenAPI Connection to Refresh Status');
374
392
  await this.openAPIRefreshStatus();