@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
package/src/device/fan.ts CHANGED
@@ -39,6 +39,15 @@ export class Fan extends deviceBase {
39
39
  Brightness: CharacteristicValue;
40
40
  };
41
41
 
42
+ // OpenAPI
43
+ deviceStatus!: batteryCirculatorFanStatus;
44
+
45
+ //Webhook
46
+ webhookContext!: batteryCirculatorFanWebhookContext;
47
+
48
+ // BLE
49
+ serviceData!: batteryCirculatorFanServiceData;
50
+
42
51
  // Updates
43
52
  fanUpdateInProgress!: boolean;
44
53
  doFanUpdate!: Subject<void>;
@@ -49,6 +58,9 @@ export class Fan extends deviceBase {
49
58
  device: device & devicesConfig,
50
59
  ) {
51
60
  super(platform, accessory, device);
61
+ // Set category
62
+ accessory.category = this.hap.Categories.FAN;
63
+
52
64
  // this is subject we use to track when we need to POST changes to the SwitchBot API
53
65
  this.doFanUpdate = new Subject();
54
66
  this.fanUpdateInProgress = false;
@@ -149,10 +161,12 @@ export class Fan extends deviceBase {
149
161
  .onSet(this.BrightnessSet.bind(this));
150
162
 
151
163
  // Retrieve initial values and updateHomekit
164
+ this.debugLog('Retrieve initial values and update Homekit');
152
165
  this.refreshStatus();
153
166
 
154
- // Update Homekit
155
- this.updateHomeKitCharacteristics();
167
+ //regisiter webhook event handler
168
+ this.debugLog('Registering Webhook Event Handler');
169
+ this.registerWebhook();
156
170
 
157
171
  //regisiter webhook event handler
158
172
  this.registerWebhook();
@@ -184,35 +198,45 @@ export class Fan extends deviceBase {
184
198
  });
185
199
  }
186
200
 
187
- async BLEparseStatus(serviceData: batteryCirculatorFanServiceData): Promise<void> {
201
+ async BLEparseStatus(): Promise<void> {
188
202
  await this.debugLog('BLEparseStatus');
203
+ await this.debugLog(`(powerState, fanSpeed) = BLE:(${this.serviceData.state}, ${this.serviceData.fanSpeed}), current:(${this.Fan.Active},`
204
+ + ` ${this.Fan.RotationSpeed})`);
205
+
189
206
  // Active
190
- this.Fan.Active = serviceData.state === 'on' ? this.hap.Characteristic.Active.ACTIVE : this.hap.Characteristic.Active.INACTIVE;
207
+ this.Fan.Active = this.serviceData.state === 'on' ? this.hap.Characteristic.Active.ACTIVE : this.hap.Characteristic.Active.INACTIVE;
191
208
  await this.debugLog(`Active: ${this.Fan.Active}`);
209
+
210
+ // RotationSpeed
211
+ this.Fan.RotationSpeed = this.serviceData.fanSpeed;
192
212
  }
193
213
 
194
- async openAPIparseStatus(deviceStatus: batteryCirculatorFanStatus) {
214
+ async openAPIparseStatus() {
195
215
  await this.debugLog('openAPIparseStatus');
216
+ await this.debugLog(`(version, battery, powerState, oscillation, chargingStatus, fanSpeed) = OpenAPI:(${this.deviceStatus.version},`
217
+ + ` ${this.deviceStatus.battery}, ${this.deviceStatus.power}, ${this.deviceStatus.oscillation}, ${this.deviceStatus.chargingStatus},`
218
+ + ` ${this.deviceStatus.fanSpeed}), current:(${this.accessory.context.version}, ${this.Battery.BatteryLevel}, ${this.Fan.Active},`
219
+ + ` ${this.Fan.SwingMode}, ${this.Battery.ChargingState}, ${this.Fan.RotationSpeed})`);
196
220
 
197
221
  // Active
198
- this.Fan.Active = deviceStatus.power === 'on' ? this.hap.Characteristic.Active.ACTIVE : this.hap.Characteristic.Active.INACTIVE;
222
+ this.Fan.Active = this.deviceStatus.power === 'on' ? this.hap.Characteristic.Active.ACTIVE : this.hap.Characteristic.Active.INACTIVE;
199
223
  this.debugLog(`Active: ${this.Fan.Active}`);
200
224
 
201
225
  // SwingMode
202
- this.Fan.SwingMode = deviceStatus.oscillation === 'on' ?
226
+ this.Fan.SwingMode = this.deviceStatus.oscillation === 'on' ?
203
227
  this.hap.Characteristic.SwingMode.SWING_ENABLED : this.hap.Characteristic.SwingMode.SWING_DISABLED;
204
228
  await this.debugLog(`SwingMode: ${this.Fan.SwingMode}`);
205
229
 
206
230
  // RotationSpeed
207
- this.Fan.RotationSpeed = deviceStatus.fanSpeed;
231
+ this.Fan.RotationSpeed = this.deviceStatus.fanSpeed;
208
232
  await this.debugLog(`RotationSpeed: ${this.Fan.RotationSpeed}`);
209
233
 
210
234
  // ChargingState
211
- this.Battery.ChargingState = deviceStatus.chargingStatus === 'charging' ?
235
+ this.Battery.ChargingState = this.deviceStatus.chargingStatus === 'charging' ?
212
236
  this.hap.Characteristic.ChargingState.CHARGING : this.hap.Characteristic.ChargingState.NOT_CHARGING;
213
237
 
214
238
  // BatteryLevel
215
- this.Battery.BatteryLevel = Number(deviceStatus.battery);
239
+ this.Battery.BatteryLevel = this.deviceStatus.battery;
216
240
  await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`);
217
241
 
218
242
  // StatusLowBattery
@@ -221,9 +245,9 @@ export class Fan extends deviceBase {
221
245
  await this.debugLog(`StatusLowBattery: ${this.Battery.StatusLowBattery}`);
222
246
 
223
247
  // Firmware Version
224
- const version = deviceStatus.version.toString();
225
- await this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
226
- if (deviceStatus.version) {
248
+ if (this.deviceStatus.version) {
249
+ const version = this.deviceStatus.version.toString();
250
+ await this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
227
251
  const deviceVersion = version.replace(/^V|-.*$/g, '') ?? '0.0.0';
228
252
  this.accessory
229
253
  .getService(this.hap.Service.AccessoryInformation)!
@@ -231,49 +255,56 @@ export class Fan extends deviceBase {
231
255
  .setCharacteristic(this.hap.Characteristic.FirmwareRevision, deviceVersion)
232
256
  .getCharacteristic(this.hap.Characteristic.FirmwareRevision)
233
257
  .updateValue(deviceVersion);
234
- this.accessory.context.deviceVersion = deviceVersion;
235
- await this.debugSuccessLog(`deviceVersion: ${this.accessory.context.deviceVersion}`);
258
+ this.accessory.context.version = deviceVersion;
259
+ await this.debugSuccessLog(`version: ${this.accessory.context.version}`);
236
260
  }
237
261
  }
238
262
 
239
- async parseStatusWebhook(context: batteryCirculatorFanWebhookContext): Promise<void> {
263
+ async parseStatusWebhook(): Promise<void> {
240
264
  await this.debugLog('parseStatusWebhook');
241
- await this.debugLog(`(version, battery, powerState, oscillation, chargingStatus, fanSpeed) = Webhook:(${context.version}, ${context.battery},`
242
- + ` ${context.powerState}, ${context.oscillation}, ${context.chargingStatus}, ${context.fanSpeed}),`
243
- + ` current:(${this.accessory.context.deviceVersion}, ${this.Battery.BatteryLevel}, ${this.Fan.Active}, ${this.Fan.SwingMode},`
244
- + ` ${this.Battery.ChargingState}, ${this.Fan.RotationSpeed})`);
265
+ await this.debugLog(`(version, battery, powerState, oscillation, chargingStatus, fanSpeed) = Webhook:(${this.webhookContext.version},`
266
+ + ` ${this.webhookContext.battery}, ${this.webhookContext.powerState}, ${this.webhookContext.oscillation},`
267
+ + ` ${this.webhookContext.chargingStatus}, ${this.webhookContext.fanSpeed}), current:(${this.accessory.context.version},`
268
+ + ` ${this.Battery.BatteryLevel}, ${this.Fan.Active}, ${this.Fan.SwingMode}, ${this.Battery.ChargingState}, ${this.Fan.RotationSpeed})`);
269
+
245
270
  // Active
246
- this.Fan.Active = context.powerState === 'ON' ? this.hap.Characteristic.Active.ACTIVE : this.hap.Characteristic.Active.INACTIVE;
271
+ this.Fan.Active = this.webhookContext.powerState === 'ON' ? this.hap.Characteristic.Active.ACTIVE : this.hap.Characteristic.Active.INACTIVE;
247
272
  await this.debugLog(`Active: ${this.Fan.Active}`);
273
+
248
274
  // SwingMode
249
- this.Fan.SwingMode = context.oscillation === 'on' ?
275
+ this.Fan.SwingMode = this.webhookContext.oscillation === 'on' ?
250
276
  this.hap.Characteristic.SwingMode.SWING_ENABLED : this.hap.Characteristic.SwingMode.SWING_DISABLED;
251
277
  await this.debugLog(`SwingMode: ${this.Fan.SwingMode}`);
278
+
252
279
  // RotationSpeed
253
- this.Fan.RotationSpeed = context.fanSpeed;
280
+ this.Fan.RotationSpeed = this.webhookContext.fanSpeed;
254
281
  await this.debugLog(`RotationSpeed: ${this.Fan.RotationSpeed}`);
282
+
255
283
  // BatteryLevel
256
- this.Battery.BatteryLevel = context.battery;
284
+ this.Battery.BatteryLevel = this.webhookContext.battery;
257
285
  await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`);
286
+
258
287
  // StatusLowBattery
259
288
  this.Battery.StatusLowBattery = this.Battery.BatteryLevel < 10
260
289
  ? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW : this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
261
290
  await this.debugLog(`StatusLowBattery: ${this.Battery.StatusLowBattery}`);
291
+
262
292
  // ChargingState
263
- this.Battery.ChargingState = context.chargingStatus === 'charging' ?
293
+ this.Battery.ChargingState = this.webhookContext.chargingStatus === 'charging' ?
264
294
  this.hap.Characteristic.ChargingState.CHARGING : this.hap.Characteristic.ChargingState.NOT_CHARGING;
265
295
  await this.debugLog(`ChargingState: ${this.Battery.ChargingState}`);
296
+
266
297
  // FirmwareVersion
267
- if (context.version) {
268
- const deviceVersion = context.version.replace(/^V|-.*$/g, '') ?? '0.0.0';
298
+ if (this.webhookContext.version) {
299
+ const deviceVersion = this.webhookContext.version.replace(/^V|-.*$/g, '') ?? '0.0.0';
269
300
  this.accessory
270
301
  .getService(this.hap.Service.AccessoryInformation)!
271
302
  .setCharacteristic(this.hap.Characteristic.HardwareRevision, deviceVersion)
272
303
  .setCharacteristic(this.hap.Characteristic.FirmwareRevision, deviceVersion)
273
304
  .getCharacteristic(this.hap.Characteristic.FirmwareRevision)
274
305
  .updateValue(deviceVersion);
275
- this.accessory.context.deviceVersion = deviceVersion;
276
- await this.debugSuccessLog(`deviceVersion: ${this.accessory.context.deviceVersion}`);
306
+ this.accessory.context.version = deviceVersion;
307
+ await this.debugSuccessLog(`version: ${this.accessory.context.version}`);
277
308
  }
278
309
  }
279
310
 
@@ -303,10 +334,11 @@ export class Fan extends deviceBase {
303
334
  // Start to monitor advertisement packets
304
335
  (async () => {
305
336
  // Start to monitor advertisement packets
306
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as unknown as batteryCirculatorFanServiceData;
337
+ const serviceData = await this.monitorAdvertisementPackets(switchbot) as batteryCirculatorFanServiceData;
307
338
  // Update HomeKit
308
339
  if (serviceData.model === SwitchBotBLEModel.Unknown && SwitchBotBLEModelName.Unknown) {
309
- await this.BLEparseStatus(serviceData);
340
+ this.serviceData = serviceData;
341
+ await this.BLEparseStatus();
310
342
  await this.updateHomeKitCharacteristics();
311
343
  } else {
312
344
  await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
@@ -324,7 +356,8 @@ export class Fan extends deviceBase {
324
356
  await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);;
325
357
  if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
326
358
  await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
327
- await this.openAPIparseStatus(deviceStatus.body);
359
+ this.deviceStatus = deviceStatus.body;
360
+ await this.openAPIparseStatus();
328
361
  await this.updateHomeKitCharacteristics();
329
362
  } else {
330
363
  await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
@@ -342,7 +375,8 @@ export class Fan extends deviceBase {
342
375
  this.platform.webhookEventHandler[this.device.deviceId] = async (context: batteryCirculatorFanWebhookContext) => {
343
376
  try {
344
377
  await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
345
- await this.parseStatusWebhook(context);
378
+ this.webhookContext = context;
379
+ await this.parseStatusWebhook();
346
380
  await this.updateHomeKitCharacteristics();
347
381
  } catch (e: any) {
348
382
  await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
@@ -634,7 +668,7 @@ export class Fan extends deviceBase {
634
668
  }
635
669
 
636
670
  async BLERefreshConnection(switchbot: any): Promise<void> {
637
- await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${JSON.stringify(switchbot)}`);
671
+ await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
638
672
  if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
639
673
  await this.warnLog('Using OpenAPI Connection to Refresh Status');
640
674
  await this.openAPIRefreshStatus();
package/src/device/hub.ts CHANGED
@@ -36,6 +36,15 @@ export class Hub extends deviceBase {
36
36
  CurrentTemperature: CharacteristicValue;
37
37
  };
38
38
 
39
+ // OpenAPI
40
+ deviceStatus!: hub2Status;
41
+
42
+ //Webhook
43
+ webhookContext!: hub2WebhookContext;
44
+
45
+ // BLE
46
+ serviceData!: hub2ServiceData;
47
+
39
48
  // Updates
40
49
  hubUpdateInProgress!: boolean;
41
50
  doHubUpdate!: Subject<void>;
@@ -46,6 +55,9 @@ export class Hub extends deviceBase {
46
55
  device: device & devicesConfig,
47
56
  ) {
48
57
  super(platform, accessory, device);
58
+ // Set category
59
+ accessory.category = this.hap.Categories.SENSOR;
60
+
49
61
  // this is subject we use to track when we need to POST changes to the SwitchBot API
50
62
  this.doHubUpdate = new Subject();
51
63
  this.hubUpdateInProgress = false;
@@ -53,7 +65,7 @@ export class Hub extends deviceBase {
53
65
  // Initialize Temperature Sensor Service
54
66
  if (device.hub?.hide_temperature) {
55
67
  if (this.TemperatureSensor) {
56
- this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Temperature Sensor Service`);
68
+ this.debugLog('Removing Temperature Sensor Service');
57
69
  this.TemperatureSensor.Service = this.accessory.getService(this.hap.Service.TemperatureSensor) as Service;
58
70
  accessory.removeService(this.TemperatureSensor.Service);
59
71
  }
@@ -85,7 +97,7 @@ export class Hub extends deviceBase {
85
97
  // Initialize Humidity Sensor Service
86
98
  if (device.hub?.hide_humidity) {
87
99
  if (this.HumiditySensor) {
88
- this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Humidity Sensor Service`);
100
+ this.debugLog('Removing Humidity Sensor Service');
89
101
  this.HumiditySensor.Service = this.accessory.getService(this.hap.Service.HumiditySensor) as Service;
90
102
  accessory.removeService(this.HumiditySensor.Service);
91
103
  }
@@ -113,7 +125,7 @@ export class Hub extends deviceBase {
113
125
  // Initialize Light Sensor Service
114
126
  if (device.hub?.hide_lightsensor) {
115
127
  if (this.LightSensor) {
116
- this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Light Sensor Service`);
128
+ this.debugLog('Removing Light Sensor Service');
117
129
  this.LightSensor.Service = this.accessory.getService(this.hap.Service.LightSensor) as Service;
118
130
  accessory.removeService(this.LightSensor.Service);
119
131
  }
@@ -139,12 +151,11 @@ export class Hub extends deviceBase {
139
151
  }
140
152
 
141
153
  // Retrieve initial values and updateHomekit
154
+ this.debugLog('Retrieve initial values and update Homekit');
142
155
  this.refreshStatus();
143
156
 
144
- // Retrieve initial values and update Homekit
145
- this.updateHomeKitCharacteristics();
146
-
147
157
  //regisiter webhook event handler
158
+ this.debugLog('Registering Webhook Event Handler');
148
159
  this.registerWebhook();
149
160
 
150
161
  // Start an update interval
@@ -156,39 +167,49 @@ export class Hub extends deviceBase {
156
167
 
157
168
  }
158
169
 
159
- async BLEparseStatus(serviceData: hub2ServiceData): Promise<void> {
170
+ async BLEparseStatus(): Promise<void> {
160
171
  await this.debugLog('BLEparseStatus');
172
+ await this.debugLog(`(temperature, humidity, lightLevel) = BLE:(${this.serviceData.temperature}, ${this.serviceData.humidity},`
173
+ + ` ${this.serviceData.lightLevel}), current:(${this.TemperatureSensor?.CurrentTemperature}, ${this.HumiditySensor?.CurrentRelativeHumidity},`
174
+ + ` ${this.LightSensor?.CurrentAmbientLightLevel})`);
175
+
161
176
  // CurrentTemperature
162
177
  if (!this.device.hub?.hide_temperature && this.TemperatureSensor?.Service) {
163
- this.TemperatureSensor.CurrentTemperature = serviceData.temperature;
178
+ this.TemperatureSensor.CurrentTemperature = this.serviceData.temperature;
164
179
  await this.debugLog(`CurrentTemperature: ${this.TemperatureSensor.CurrentTemperature}°c`);
165
180
  }
181
+
166
182
  // CurrentRelativeHumidity
167
183
  if (!this.device.hub?.hide_humidity && this.HumiditySensor?.Service) {
168
- this.HumiditySensor!.CurrentRelativeHumidity = Number(serviceData.humidity);
184
+ this.HumiditySensor!.CurrentRelativeHumidity = this.serviceData.humidity;
169
185
  await this.debugLog(`CurrentRelativeHumidity: ${this.HumiditySensor.CurrentRelativeHumidity}%`);
170
186
  }
187
+
171
188
  // CurrentAmbientLightLevel
172
189
  if (!this.device.hub?.hide_lightsensor && this.LightSensor?.Service) {
173
190
  const set_minLux = this.device.blindTilt?.set_minLux ?? 1;
174
191
  const set_maxLux = this.device.blindTilt?.set_maxLux ?? 6001;
175
- const lightLevel = Number(serviceData.lightLevel);
192
+ const lightLevel = this.serviceData.lightLevel;
176
193
  this.LightSensor.CurrentAmbientLightLevel = await this.getLightLevel(lightLevel, set_minLux, set_maxLux, 19);
177
- await this.debugLog(`LightLevel: ${serviceData.lightLevel}, CurrentAmbientLightLevel: ${this.LightSensor.CurrentAmbientLightLevel}`);
194
+ await this.debugLog(`LightLevel: ${this.serviceData.lightLevel}, CurrentAmbientLightLevel: ${this.LightSensor.CurrentAmbientLightLevel}`);
178
195
  }
179
196
  }
180
197
 
181
- async openAPIparseStatus(deviceStatus: hub2Status): Promise<void> {
198
+ async openAPIparseStatus(): Promise<void> {
182
199
  await this.debugLog('openAPIparseStatus');
200
+ await this.debugLog(`(temperature, humidity, lightLevel) = OpenAPI:(${this.deviceStatus.temperature}, ${this.deviceStatus.humidity},`
201
+ + ` ${this.deviceStatus.lightLevel}), current:(${this.TemperatureSensor?.CurrentTemperature}, ${this.HumiditySensor?.CurrentRelativeHumidity},`
202
+ + ` ${this.LightSensor?.CurrentAmbientLightLevel})`);
203
+
183
204
  // CurrentRelativeHumidity
184
205
  if (!this.device.hub?.hide_humidity && this.HumiditySensor?.Service) {
185
- this.HumiditySensor.CurrentRelativeHumidity = deviceStatus.humidity;
206
+ this.HumiditySensor.CurrentRelativeHumidity = this.deviceStatus.humidity;
186
207
  await this.debugLog(`CurrentRelativeHumidity: ${this.HumiditySensor.CurrentRelativeHumidity}%`);
187
208
  }
188
209
 
189
210
  // CurrentTemperature
190
211
  if (!this.device.hub?.hide_temperature && this.TemperatureSensor?.Service) {
191
- this.TemperatureSensor.CurrentTemperature = deviceStatus.temperature;
212
+ this.TemperatureSensor.CurrentTemperature = this.deviceStatus.temperature;
192
213
  await this.debugLog(`CurrentTemperature: ${this.TemperatureSensor.CurrentTemperature}°c`);
193
214
  }
194
215
 
@@ -196,15 +217,15 @@ export class Hub extends deviceBase {
196
217
  if (!this.device.hub?.hide_lightsensor && this.LightSensor?.Service) {
197
218
  const set_minLux = this.device.blindTilt?.set_minLux ?? 1;
198
219
  const set_maxLux = this.device.blindTilt?.set_maxLux ?? 6001;
199
- const lightLevel = Number(deviceStatus.lightLevel);
220
+ const lightLevel = this.deviceStatus.lightLevel;
200
221
  this.LightSensor.CurrentAmbientLightLevel = await this.getLightLevel(lightLevel, set_minLux, set_maxLux, 19);
201
- await this.debugLog(`LightLevel: ${deviceStatus.lightLevel}, CurrentAmbientLightLevel: ${this.LightSensor!.CurrentAmbientLightLevel}`);
222
+ await this.debugLog(`LightLevel: ${this.deviceStatus.lightLevel}, CurrentAmbientLightLevel: ${this.LightSensor!.CurrentAmbientLightLevel}`);
202
223
  }
203
224
 
204
225
  // Firmware Version
205
- const version = deviceStatus.version.toString();
206
- await this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
207
- if (deviceStatus.version) {
226
+ if (this.deviceStatus.version) {
227
+ const version = this.deviceStatus.version.toString();
228
+ await this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
208
229
  const deviceVersion = version.replace(/^V|-.*$/g, '') ?? '0.0.0';
209
230
  this.accessory
210
231
  .getService(this.hap.Service.AccessoryInformation)!
@@ -212,36 +233,41 @@ export class Hub extends deviceBase {
212
233
  .setCharacteristic(this.hap.Characteristic.FirmwareRevision, deviceVersion)
213
234
  .getCharacteristic(this.hap.Characteristic.FirmwareRevision)
214
235
  .updateValue(deviceVersion);
215
- this.accessory.context.deviceVersion = deviceVersion;
216
- await this.debugSuccessLog(`deviceVersion: ${this.accessory.context.deviceVersion}`);
236
+ this.accessory.context.version = deviceVersion;
237
+ await this.debugSuccessLog(`version: ${this.accessory.context.version}`);
217
238
  }
218
239
  }
219
240
 
220
- async parseStatusWebhook(context: hub2WebhookContext): Promise<void> {
241
+ async parseStatusWebhook(): Promise<void> {
221
242
  await this.debugLog('parseStatusWebhook');
222
- await this.debugLog(`(scale, temperature, humidity, lightLevel) = Webhook:(${context.scale}, ${convertUnits(context.temperature, context.scale,
223
- this.device.hub?.convertUnitTo)}, ${context.humidity}, ${context.lightLevel}), current:(${this.TemperatureSensor?.CurrentTemperature},`
243
+ await this.debugLog(`(scale, temperature, humidity, lightLevel) = Webhook:(${this.webhookContext.scale},`
244
+ + ` ${convertUnits(this.webhookContext.temperature, this.webhookContext.scale, this.device.hub?.convertUnitTo)},`
245
+ + ` ${this.webhookContext.humidity}, ${this.webhookContext.lightLevel}), current:(${this.TemperatureSensor?.CurrentTemperature},`
224
246
  + ` ${this.HumiditySensor?.CurrentRelativeHumidity}, ${this.LightSensor?.CurrentAmbientLightLevel})`);
225
247
  // Check if the scale is not CELSIUS
226
- if (context.scale !== 'CELSIUS' && this.device.hub?.convertUnitTo === undefined) {
227
- await this.warnLog(`received a non-CELSIUS Webhook scale: ${context.scale}, Use the *convertUnitsTo* config under Hub settings,`
248
+ if (this.webhookContext.scale !== 'CELSIUS' && this.device.hub?.convertUnitTo === undefined) {
249
+ await this.warnLog(`received a non-CELSIUS Webhook scale: ${this.webhookContext.scale}, Use the *convertUnitsTo* config under Hub settings,`
228
250
  + ' if displaying incorrectly in HomeKit.');
229
251
  }
252
+
230
253
  // CurrentRelativeHumidity
231
254
  if (!this.device.hub?.hide_humidity && this.HumiditySensor?.Service) {
232
- this.HumiditySensor.CurrentRelativeHumidity = context.humidity;
255
+ this.HumiditySensor.CurrentRelativeHumidity = this.webhookContext.humidity;
233
256
  await this.debugLog(`CurrentRelativeHumidity: ${this.HumiditySensor.CurrentRelativeHumidity}`);
234
257
  }
258
+
235
259
  // CurrentTemperature
236
260
  if (!this.device.hub?.hide_temperature && this.TemperatureSensor?.Service) {
237
- this.TemperatureSensor.CurrentTemperature = convertUnits(context.temperature, context.scale, this.device.hub?.convertUnitTo);
261
+ this.TemperatureSensor.CurrentTemperature = convertUnits(this.webhookContext.temperature, this.webhookContext.scale,
262
+ this.device.hub?.convertUnitTo);
238
263
  await this.debugLog(`CurrentTemperature: ${this.TemperatureSensor.CurrentTemperature}`);
239
264
  }
265
+
240
266
  // CurrentAmbientLightLevel
241
267
  if (!this.device.hub?.hide_lightsensor && this.LightSensor?.Service) {
242
268
  const set_minLux = this.device.blindTilt?.set_minLux ?? 1;
243
269
  const set_maxLux = this.device.blindTilt?.set_maxLux ?? 6001;
244
- this.LightSensor.CurrentAmbientLightLevel = await this.getLightLevel(context.lightLevel, set_minLux, set_maxLux, 19);
270
+ this.LightSensor.CurrentAmbientLightLevel = await this.getLightLevel(this.webhookContext.lightLevel, set_minLux, set_maxLux, 19);
245
271
  await this.debugLog(`CurrentAmbientLightLevel: ${this.LightSensor.CurrentAmbientLightLevel}`);
246
272
  }
247
273
  }
@@ -272,10 +298,11 @@ export class Hub extends deviceBase {
272
298
  // Start to monitor advertisement packets
273
299
  (async () => {
274
300
  // Start to monitor advertisement packets
275
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as unknown as hub2ServiceData;
301
+ const serviceData = await this.monitorAdvertisementPackets(switchbot) as hub2ServiceData;
276
302
  // Update HomeKit
277
303
  if (serviceData.model === SwitchBotBLEModel.Hub2 && serviceData.modelName === SwitchBotBLEModelName.Hub2) {
278
- await this.BLEparseStatus(serviceData);
304
+ this.serviceData = serviceData;
305
+ await this.BLEparseStatus();
279
306
  await this.updateHomeKitCharacteristics();
280
307
  } else {
281
308
  await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
@@ -293,7 +320,8 @@ export class Hub extends deviceBase {
293
320
  await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);;
294
321
  if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
295
322
  await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
296
- await this.openAPIparseStatus(deviceStatus.body);
323
+ this.deviceStatus = deviceStatus.body;
324
+ await this.openAPIparseStatus();
297
325
  await this.updateHomeKitCharacteristics();
298
326
  } else {
299
327
  await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
@@ -311,7 +339,8 @@ export class Hub extends deviceBase {
311
339
  this.platform.webhookEventHandler[this.device.deviceId] = async (context: hub2WebhookContext) => {
312
340
  try {
313
341
  await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
314
- await this.parseStatusWebhook(context);
342
+ this.webhookContext = context;
343
+ await this.parseStatusWebhook();
315
344
  await this.updateHomeKitCharacteristics();
316
345
  } catch (e: any) {
317
346
  await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
@@ -345,7 +374,7 @@ export class Hub extends deviceBase {
345
374
  }
346
375
 
347
376
  async BLERefreshConnection(switchbot: any): Promise<void> {
348
- await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${JSON.stringify(switchbot)}`);
377
+ await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
349
378
  if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
350
379
  await this.warnLog('Using OpenAPI Connection to Refresh Status');
351
380
  await this.openAPIRefreshStatus();