@switchbot/homebridge-switchbot 3.5.1-beta.12 → 3.5.1-beta.14

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 +56 -44
  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 +37 -23
  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 +39 -20
  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 +41 -30
  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 +39 -27
  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 +34 -21
  25. package/dist/device/curtain.js.map +1 -1
  26. package/dist/device/device.d.ts +6 -3
  27. package/dist/device/device.d.ts.map +1 -1
  28. package/dist/device/device.js +54 -125
  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 +47 -28
  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 +42 -25
  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 +42 -25
  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 +38 -23
  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 +42 -26
  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 +32 -21
  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 +37 -23
  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 +36 -23
  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 +32 -21
  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 +26 -15
  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 +33 -20
  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 +37 -22
  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 +2 -2
  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 +3 -5
  107. package/dist/platform.d.ts.map +1 -1
  108. package/dist/platform.js +13 -13
  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 +59 -46
  119. package/src/device/bot.ts +46 -24
  120. package/src/device/ceilinglight.ts +51 -20
  121. package/src/device/colorbulb.ts +44 -30
  122. package/src/device/contact.ts +42 -27
  123. package/src/device/curtain.ts +44 -22
  124. package/src/device/device.ts +56 -112
  125. package/src/device/fan.ts +60 -29
  126. package/src/device/hub.ts +54 -28
  127. package/src/device/humidifier.ts +54 -30
  128. package/src/device/iosensor.ts +44 -26
  129. package/src/device/lightstrip.ts +47 -27
  130. package/src/device/lock.ts +48 -22
  131. package/src/device/meter.ts +53 -25
  132. package/src/device/meterplus.ts +49 -25
  133. package/src/device/motion.ts +44 -23
  134. package/src/device/plug.ts +33 -16
  135. package/src/device/robotvacuumcleaner.ts +48 -22
  136. package/src/device/waterdetector.ts +55 -24
  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 +2 -2
  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 +14 -14
  148. package/src/settings.ts +10 -8
  149. package/src/types/bledevicestatus.ts +4 -2
  150. package/src/types/devicestatus.ts +1 -1
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>;
@@ -139,12 +148,11 @@ export class Hub extends deviceBase {
139
148
  }
140
149
 
141
150
  // Retrieve initial values and updateHomekit
151
+ this.debugLog('Retrieve initial values and update Homekit');
142
152
  this.refreshStatus();
143
153
 
144
- // Retrieve initial values and update Homekit
145
- this.updateHomeKitCharacteristics();
146
-
147
154
  //regisiter webhook event handler
155
+ this.debugLog('Registering Webhook Event Handler');
148
156
  this.registerWebhook();
149
157
 
150
158
  // Start an update interval
@@ -156,39 +164,49 @@ export class Hub extends deviceBase {
156
164
 
157
165
  }
158
166
 
159
- async BLEparseStatus(serviceData: hub2ServiceData): Promise<void> {
167
+ async BLEparseStatus(): Promise<void> {
160
168
  await this.debugLog('BLEparseStatus');
169
+ await this.debugLog(`(temperature, humidity, lightLevel) = BLE:(${this.serviceData.temperature}, ${this.serviceData.humidity},`
170
+ + ` ${this.serviceData.lightLevel}), current:(${this.TemperatureSensor?.CurrentTemperature}, ${this.HumiditySensor?.CurrentRelativeHumidity},`
171
+ + ` ${this.LightSensor?.CurrentAmbientLightLevel})`);
172
+
161
173
  // CurrentTemperature
162
174
  if (!this.device.hub?.hide_temperature && this.TemperatureSensor?.Service) {
163
- this.TemperatureSensor.CurrentTemperature = serviceData.temperature;
175
+ this.TemperatureSensor.CurrentTemperature = this.serviceData.temperature;
164
176
  await this.debugLog(`CurrentTemperature: ${this.TemperatureSensor.CurrentTemperature}°c`);
165
177
  }
178
+
166
179
  // CurrentRelativeHumidity
167
180
  if (!this.device.hub?.hide_humidity && this.HumiditySensor?.Service) {
168
- this.HumiditySensor!.CurrentRelativeHumidity = Number(serviceData.humidity);
181
+ this.HumiditySensor!.CurrentRelativeHumidity = this.serviceData.humidity;
169
182
  await this.debugLog(`CurrentRelativeHumidity: ${this.HumiditySensor.CurrentRelativeHumidity}%`);
170
183
  }
184
+
171
185
  // CurrentAmbientLightLevel
172
186
  if (!this.device.hub?.hide_lightsensor && this.LightSensor?.Service) {
173
187
  const set_minLux = this.device.blindTilt?.set_minLux ?? 1;
174
188
  const set_maxLux = this.device.blindTilt?.set_maxLux ?? 6001;
175
- const lightLevel = Number(serviceData.lightLevel);
189
+ const lightLevel = this.serviceData.lightLevel;
176
190
  this.LightSensor.CurrentAmbientLightLevel = await this.getLightLevel(lightLevel, set_minLux, set_maxLux, 19);
177
- await this.debugLog(`LightLevel: ${serviceData.lightLevel}, CurrentAmbientLightLevel: ${this.LightSensor.CurrentAmbientLightLevel}`);
191
+ await this.debugLog(`LightLevel: ${this.serviceData.lightLevel}, CurrentAmbientLightLevel: ${this.LightSensor.CurrentAmbientLightLevel}`);
178
192
  }
179
193
  }
180
194
 
181
- async openAPIparseStatus(deviceStatus: hub2Status): Promise<void> {
195
+ async openAPIparseStatus(): Promise<void> {
182
196
  await this.debugLog('openAPIparseStatus');
197
+ await this.debugLog(`(temperature, humidity, lightLevel) = OpenAPI:(${this.deviceStatus.temperature}, ${this.deviceStatus.humidity},`
198
+ + ` ${this.deviceStatus.lightLevel}), current:(${this.TemperatureSensor?.CurrentTemperature}, ${this.HumiditySensor?.CurrentRelativeHumidity},`
199
+ + ` ${this.LightSensor?.CurrentAmbientLightLevel})`);
200
+
183
201
  // CurrentRelativeHumidity
184
202
  if (!this.device.hub?.hide_humidity && this.HumiditySensor?.Service) {
185
- this.HumiditySensor.CurrentRelativeHumidity = deviceStatus.humidity;
203
+ this.HumiditySensor.CurrentRelativeHumidity = this.deviceStatus.humidity;
186
204
  await this.debugLog(`CurrentRelativeHumidity: ${this.HumiditySensor.CurrentRelativeHumidity}%`);
187
205
  }
188
206
 
189
207
  // CurrentTemperature
190
208
  if (!this.device.hub?.hide_temperature && this.TemperatureSensor?.Service) {
191
- this.TemperatureSensor.CurrentTemperature = deviceStatus.temperature;
209
+ this.TemperatureSensor.CurrentTemperature = this.deviceStatus.temperature;
192
210
  await this.debugLog(`CurrentTemperature: ${this.TemperatureSensor.CurrentTemperature}°c`);
193
211
  }
194
212
 
@@ -196,15 +214,15 @@ export class Hub extends deviceBase {
196
214
  if (!this.device.hub?.hide_lightsensor && this.LightSensor?.Service) {
197
215
  const set_minLux = this.device.blindTilt?.set_minLux ?? 1;
198
216
  const set_maxLux = this.device.blindTilt?.set_maxLux ?? 6001;
199
- const lightLevel = Number(deviceStatus.lightLevel);
217
+ const lightLevel = this.deviceStatus.lightLevel;
200
218
  this.LightSensor.CurrentAmbientLightLevel = await this.getLightLevel(lightLevel, set_minLux, set_maxLux, 19);
201
- await this.debugLog(`LightLevel: ${deviceStatus.lightLevel}, CurrentAmbientLightLevel: ${this.LightSensor!.CurrentAmbientLightLevel}`);
219
+ await this.debugLog(`LightLevel: ${this.deviceStatus.lightLevel}, CurrentAmbientLightLevel: ${this.LightSensor!.CurrentAmbientLightLevel}`);
202
220
  }
203
221
 
204
222
  // Firmware Version
205
- const version = deviceStatus.version.toString();
206
- await this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
207
- if (deviceStatus.version) {
223
+ if (this.deviceStatus.version) {
224
+ const version = this.deviceStatus.version.toString();
225
+ await this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
208
226
  const deviceVersion = version.replace(/^V|-.*$/g, '') ?? '0.0.0';
209
227
  this.accessory
210
228
  .getService(this.hap.Service.AccessoryInformation)!
@@ -217,31 +235,36 @@ export class Hub extends deviceBase {
217
235
  }
218
236
  }
219
237
 
220
- async parseStatusWebhook(context: hub2WebhookContext): Promise<void> {
238
+ async parseStatusWebhook(): Promise<void> {
221
239
  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},`
240
+ await this.debugLog(`(scale, temperature, humidity, lightLevel) = Webhook:(${this.webhookContext.scale},`
241
+ + ` ${convertUnits(this.webhookContext.temperature, this.webhookContext.scale, this.device.hub?.convertUnitTo)},`
242
+ + ` ${this.webhookContext.humidity}, ${this.webhookContext.lightLevel}), current:(${this.TemperatureSensor?.CurrentTemperature},`
224
243
  + ` ${this.HumiditySensor?.CurrentRelativeHumidity}, ${this.LightSensor?.CurrentAmbientLightLevel})`);
225
244
  // 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,`
245
+ if (this.webhookContext.scale !== 'CELSIUS' && this.device.hub?.convertUnitTo === undefined) {
246
+ await this.warnLog(`received a non-CELSIUS Webhook scale: ${this.webhookContext.scale}, Use the *convertUnitsTo* config under Hub settings,`
228
247
  + ' if displaying incorrectly in HomeKit.');
229
248
  }
249
+
230
250
  // CurrentRelativeHumidity
231
251
  if (!this.device.hub?.hide_humidity && this.HumiditySensor?.Service) {
232
- this.HumiditySensor.CurrentRelativeHumidity = context.humidity;
252
+ this.HumiditySensor.CurrentRelativeHumidity = this.webhookContext.humidity;
233
253
  await this.debugLog(`CurrentRelativeHumidity: ${this.HumiditySensor.CurrentRelativeHumidity}`);
234
254
  }
255
+
235
256
  // CurrentTemperature
236
257
  if (!this.device.hub?.hide_temperature && this.TemperatureSensor?.Service) {
237
- this.TemperatureSensor.CurrentTemperature = convertUnits(context.temperature, context.scale, this.device.hub?.convertUnitTo);
258
+ this.TemperatureSensor.CurrentTemperature = convertUnits(this.webhookContext.temperature, this.webhookContext.scale,
259
+ this.device.hub?.convertUnitTo);
238
260
  await this.debugLog(`CurrentTemperature: ${this.TemperatureSensor.CurrentTemperature}`);
239
261
  }
262
+
240
263
  // CurrentAmbientLightLevel
241
264
  if (!this.device.hub?.hide_lightsensor && this.LightSensor?.Service) {
242
265
  const set_minLux = this.device.blindTilt?.set_minLux ?? 1;
243
266
  const set_maxLux = this.device.blindTilt?.set_maxLux ?? 6001;
244
- this.LightSensor.CurrentAmbientLightLevel = await this.getLightLevel(context.lightLevel, set_minLux, set_maxLux, 19);
267
+ this.LightSensor.CurrentAmbientLightLevel = await this.getLightLevel(this.webhookContext.lightLevel, set_minLux, set_maxLux, 19);
245
268
  await this.debugLog(`CurrentAmbientLightLevel: ${this.LightSensor.CurrentAmbientLightLevel}`);
246
269
  }
247
270
  }
@@ -272,10 +295,11 @@ export class Hub extends deviceBase {
272
295
  // Start to monitor advertisement packets
273
296
  (async () => {
274
297
  // Start to monitor advertisement packets
275
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as unknown as hub2ServiceData;
298
+ const serviceData = await this.monitorAdvertisementPackets(switchbot) as hub2ServiceData;
276
299
  // Update HomeKit
277
300
  if (serviceData.model === SwitchBotBLEModel.Hub2 && serviceData.modelName === SwitchBotBLEModelName.Hub2) {
278
- await this.BLEparseStatus(serviceData);
301
+ this.serviceData = serviceData;
302
+ await this.BLEparseStatus();
279
303
  await this.updateHomeKitCharacteristics();
280
304
  } else {
281
305
  await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
@@ -293,7 +317,8 @@ export class Hub extends deviceBase {
293
317
  await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);;
294
318
  if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
295
319
  await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
296
- await this.openAPIparseStatus(deviceStatus.body);
320
+ this.deviceStatus = deviceStatus.body;
321
+ await this.openAPIparseStatus();
297
322
  await this.updateHomeKitCharacteristics();
298
323
  } else {
299
324
  await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
@@ -311,7 +336,8 @@ export class Hub extends deviceBase {
311
336
  this.platform.webhookEventHandler[this.device.deviceId] = async (context: hub2WebhookContext) => {
312
337
  try {
313
338
  await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
314
- await this.parseStatusWebhook(context);
339
+ this.webhookContext = context;
340
+ await this.parseStatusWebhook();
315
341
  await this.updateHomeKitCharacteristics();
316
342
  } catch (e: any) {
317
343
  await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
@@ -39,6 +39,15 @@ export class Humidifier extends deviceBase {
39
39
  CurrentTemperature: CharacteristicValue;
40
40
  };
41
41
 
42
+ // OpenAPI
43
+ deviceStatus!: humidifierStatus;
44
+
45
+ //Webhook
46
+ webhookContext!: humidifierWebhookContext;
47
+
48
+ // BLE
49
+ serviceData!: humidifierServiceData;
50
+
42
51
  // Updates
43
52
  humidifierUpdateInProgress!: boolean;
44
53
  doHumidifierUpdate!: Subject<void>;
@@ -139,12 +148,11 @@ export class Humidifier extends deviceBase {
139
148
  }
140
149
 
141
150
  // Retrieve initial values and updateHomekit
151
+ this.debugLog('Retrieve initial values and update Homekit');
142
152
  this.refreshStatus();
143
153
 
144
- // Retrieve initial values and updateHomekit
145
- this.updateHomeKitCharacteristics();
146
-
147
154
  //regisiter webhook event handler
155
+ this.debugLog('Registering Webhook Event Handler');
148
156
  this.registerWebhook();
149
157
 
150
158
  // Start an update interval
@@ -174,18 +182,23 @@ export class Humidifier extends deviceBase {
174
182
  });
175
183
  }
176
184
 
177
- async BLEparseStatus(serviceData: humidifierServiceData): Promise<void> {
185
+ async BLEparseStatus(): Promise<void> {
178
186
  await this.debugLog('BLEparseStatus');
187
+ await this.debugLog(`(onState, percentage, autoMode) = BLE:(${this.serviceData.onState}, ${this.serviceData.percentage},`
188
+ + ` ${this.serviceData.autoMode}), current:(${this.HumidifierDehumidifier.Active}, ${this.HumidifierDehumidifier.CurrentRelativeHumidity},`
189
+ + ` ${this.HumidifierDehumidifier.TargetHumidifierDehumidifierState}, ${this.HumidifierDehumidifier.RelativeHumidityHumidifierThreshold})`,
190
+ );
191
+
179
192
  // Active
180
- this.HumidifierDehumidifier.Active = serviceData.onState ? this.hap.Characteristic.Active.ACTIVE : this.hap.Characteristic.Active.INACTIVE;
193
+ this.HumidifierDehumidifier.Active = this.serviceData.onState ? this.hap.Characteristic.Active.ACTIVE : this.hap.Characteristic.Active.INACTIVE;
181
194
  await this.debugLog(`Active: ${this.HumidifierDehumidifier.Active}`);
182
195
 
183
196
  // Current Relative Humidity
184
- this.HumidifierDehumidifier.CurrentRelativeHumidity = serviceData.percentage;
197
+ this.HumidifierDehumidifier.CurrentRelativeHumidity = this.serviceData.percentage;
185
198
  await this.debugLog(`CurrentRelativeHumidity: ${this.HumidifierDehumidifier.CurrentRelativeHumidity}`);
186
199
 
187
200
  // Target Humidifier Dehumidifier State
188
- switch (serviceData.autoMode) {
201
+ switch (this.serviceData.autoMode) {
189
202
  case true:
190
203
  this.HumidifierDehumidifier.TargetHumidifierDehumidifierState =
191
204
  this.hap.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER_OR_DEHUMIDIFIER;
@@ -194,7 +207,7 @@ export class Humidifier extends deviceBase {
194
207
  break;
195
208
  default:
196
209
  this.HumidifierDehumidifier.TargetHumidifierDehumidifierState = this.hap.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER;
197
- this.HumidifierDehumidifier.RelativeHumidityHumidifierThreshold = serviceData.percentage > 100 ? 100 : serviceData.percentage;
210
+ this.HumidifierDehumidifier.RelativeHumidityHumidifierThreshold = this.serviceData.percentage > 100 ? 100 : this.serviceData.percentage;
198
211
  if (this.HumidifierDehumidifier.CurrentRelativeHumidity > this.HumidifierDehumidifier.RelativeHumidityHumidifierThreshold) {
199
212
  this.HumidifierDehumidifier.CurrentHumidifierDehumidifierState = this.hap.Characteristic.CurrentHumidifierDehumidifierState.IDLE;
200
213
  } else if (this.HumidifierDehumidifier.Active === this.hap.Characteristic.Active.INACTIVE) {
@@ -208,24 +221,30 @@ export class Humidifier extends deviceBase {
208
221
  + ` CurrentHumidifierDehumidifierState: ${this.HumidifierDehumidifier.CurrentHumidifierDehumidifierState}`);
209
222
  }
210
223
 
211
- async openAPIparseStatus(deviceStatus: humidifierStatus): Promise<void> {
224
+ async openAPIparseStatus(): Promise<void> {
212
225
  await this.debugLog('openAPIparseStatus');
226
+ await this.debugLog(`(power, auto, temperature, lackWater, nebulizationEfficiency, version) = OpenAPI:(${this.deviceStatus.power},`
227
+ + ` ${this.deviceStatus.auto}, ${this.deviceStatus.temperature}, ${this.deviceStatus.lackWater},`
228
+ + ` ${this.deviceStatus.nebulizationEfficiency}, ${this.deviceStatus.version}), current:(${this.HumidifierDehumidifier.Active},`
229
+ + ` ${this.HumidifierDehumidifier.CurrentRelativeHumidity}, ${this.HumidifierDehumidifier.TargetHumidifierDehumidifierState},`
230
+ + ` ${this.HumidifierDehumidifier.RelativeHumidityHumidifierThreshold})`);
231
+
213
232
  // Active
214
- this.HumidifierDehumidifier.Active = deviceStatus.power === 'on'
233
+ this.HumidifierDehumidifier.Active = this.deviceStatus.power === 'on'
215
234
  ? this.hap.Characteristic.Active.ACTIVE : this.hap.Characteristic.Active.INACTIVE;
216
235
  await this.debugLog(`Active: ${this.HumidifierDehumidifier.Active}`);
217
236
 
218
237
  // Current Relative Humidity
219
- this.HumidifierDehumidifier.CurrentRelativeHumidity = deviceStatus.temperature!;
238
+ this.HumidifierDehumidifier.CurrentRelativeHumidity = this.deviceStatus.temperature!;
220
239
  await this.debugLog(`CurrentRelativeHumidity: ${this.HumidifierDehumidifier.CurrentRelativeHumidity}`);
221
240
 
222
241
  // Current Temperature
223
242
  if (!this.device.humidifier?.hide_temperature && this.TemperatureSensor?.Service) {
224
- this.TemperatureSensor.CurrentTemperature = deviceStatus.temperature;
243
+ this.TemperatureSensor.CurrentTemperature = this.deviceStatus.temperature;
225
244
  await this.debugLog(`CurrentTemperature: ${this.TemperatureSensor.CurrentTemperature}`);
226
245
  }
227
246
  // Target Humidifier Dehumidifier State
228
- switch (deviceStatus.auto) {
247
+ switch (this.deviceStatus.auto) {
229
248
  case true:
230
249
  this.HumidifierDehumidifier.TargetHumidifierDehumidifierState =
231
250
  this.hap.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER_OR_DEHUMIDIFIER;
@@ -234,8 +253,8 @@ export class Humidifier extends deviceBase {
234
253
  break;
235
254
  default:
236
255
  this.HumidifierDehumidifier.TargetHumidifierDehumidifierState = this.hap.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER;
237
- this.HumidifierDehumidifier.RelativeHumidityHumidifierThreshold = deviceStatus.nebulizationEfficiency > 100
238
- ? 100 : deviceStatus.nebulizationEfficiency;
256
+ this.HumidifierDehumidifier.RelativeHumidityHumidifierThreshold = this.deviceStatus.nebulizationEfficiency > 100
257
+ ? 100 : this.deviceStatus.nebulizationEfficiency;
239
258
  if (this.HumidifierDehumidifier.CurrentRelativeHumidity > this.HumidifierDehumidifier.RelativeHumidityHumidifierThreshold) {
240
259
  this.HumidifierDehumidifier.CurrentHumidifierDehumidifierState = this.hap.Characteristic.CurrentHumidifierDehumidifierState.IDLE;
241
260
  } else if (this.HumidifierDehumidifier.Active === this.hap.Characteristic.Active.INACTIVE) {
@@ -249,7 +268,7 @@ export class Humidifier extends deviceBase {
249
268
  + ` CurrentHumidifierDehumidifierState: ${this.HumidifierDehumidifier.CurrentHumidifierDehumidifierState}`);
250
269
 
251
270
  // Water Level
252
- if (deviceStatus.lackWater) {
271
+ if (this.deviceStatus.lackWater) {
253
272
  this.HumidifierDehumidifier.WaterLevel = 0;
254
273
  } else {
255
274
  this.HumidifierDehumidifier.WaterLevel = 100;
@@ -257,8 +276,8 @@ export class Humidifier extends deviceBase {
257
276
  await this.debugLog(`WaterLevel: ${this.HumidifierDehumidifier.WaterLevel}`);
258
277
 
259
278
  // Firmware Version
260
- if (deviceStatus.version) {
261
- const version = deviceStatus.version.toString();
279
+ if (this.deviceStatus.version) {
280
+ const version = this.deviceStatus.version.toString();
262
281
  await this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
263
282
  const deviceVersion = version.replace(/^V|-.*$/g, '') ?? '0.0.0';
264
283
  this.accessory
@@ -272,18 +291,20 @@ export class Humidifier extends deviceBase {
272
291
  }
273
292
  }
274
293
 
275
- async parseStatusWebhook(context: humidifierWebhookContext): Promise<void> {
294
+ async parseStatusWebhook(): Promise<void> {
276
295
  await this.debugLog('parseStatusWebhook');
277
- await this.debugLog(`(temperature, humidity) = Webhook:(${convertUnits(context.temperature, context.scale, this.device.iosensor?.convertUnitTo)},`
278
- + ` ${context.humidity}), current:(${this.TemperatureSensor?.CurrentTemperature}, ${this.HumidifierDehumidifier.CurrentRelativeHumidity})`);
296
+ await this.debugLog(`(temperature, humidity) = Webhook:(${convertUnits(this.webhookContext.temperature, this.webhookContext.scale,
297
+ this.device.iosensor?.convertUnitTo)}, ${this.webhookContext.humidity}), current:(${this.TemperatureSensor?.CurrentTemperature},`
298
+ + ` ${this.HumidifierDehumidifier.CurrentRelativeHumidity})`);
279
299
 
280
300
  // CurrentRelativeHumidity
281
- this.HumidifierDehumidifier.CurrentRelativeHumidity = context.humidity;
301
+ this.HumidifierDehumidifier.CurrentRelativeHumidity = this.webhookContext.humidity;
282
302
  await this.debugLog(`CurrentRelativeHumidity: ${this.HumidifierDehumidifier.CurrentRelativeHumidity}`);
283
303
 
284
304
  // CurrentTemperature
285
305
  if (!this.device.humidifier?.hide_temperature && this.TemperatureSensor?.Service) {
286
- this.TemperatureSensor.CurrentTemperature = convertUnits(context.temperature, context.scale, this.device.iosensor?.convertUnitTo);
306
+ this.TemperatureSensor.CurrentTemperature = convertUnits(this.webhookContext.temperature, this.webhookContext.scale,
307
+ this.device.iosensor?.convertUnitTo);
287
308
  await this.debugLog(`CurrentTemperature: ${this.TemperatureSensor.CurrentTemperature}`);
288
309
  }
289
310
  }
@@ -314,10 +335,11 @@ export class Humidifier extends deviceBase {
314
335
  // Start to monitor advertisement packets
315
336
  (async () => {
316
337
  // Start to monitor advertisement packets
317
- const serviceData= await this.monitorAdvertisementPackets(switchbot) as unknown as humidifierServiceData;
338
+ const serviceData = await this.monitorAdvertisementPackets(switchbot) as humidifierServiceData;
318
339
  // Update HomeKit
319
340
  if (serviceData.model === SwitchBotBLEModel.Humidifier && serviceData.modelName === SwitchBotBLEModelName.Humidifier) {
320
- await this.BLEparseStatus(serviceData);
341
+ this.serviceData = serviceData;
342
+ await this.BLEparseStatus();
321
343
  await this.updateHomeKitCharacteristics();
322
344
  } else {
323
345
  await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
@@ -335,7 +357,8 @@ export class Humidifier extends deviceBase {
335
357
  await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);;
336
358
  if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
337
359
  await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
338
- await this.openAPIparseStatus(deviceStatus.body);
360
+ this.deviceStatus = deviceStatus.body;
361
+ await this.openAPIparseStatus();
339
362
  await this.updateHomeKitCharacteristics();
340
363
  } else {
341
364
  await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
@@ -353,7 +376,8 @@ export class Humidifier extends deviceBase {
353
376
  this.platform.webhookEventHandler[this.device.deviceId] = async (context: humidifierWebhookContext) => {
354
377
  try {
355
378
  await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
356
- await this.parseStatusWebhook(context);
379
+ this.webhookContext = context;
380
+ await this.parseStatusWebhook();
357
381
  await this.updateHomeKitCharacteristics();
358
382
  } catch (e: any) {
359
383
  await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
@@ -414,8 +438,8 @@ export class Humidifier extends deviceBase {
414
438
  }
415
439
  } else {
416
440
  await this.debugLog(`No changes (BLEpushChanges), Active: ${this.HumidifierDehumidifier.Active},`
417
- + ` RelativeHumidityHumidifierThreshold: ${this.HumidifierDehumidifier.RelativeHumidityHumidifierThreshold},`
418
- + ` CurrentRelativeHumidity: ${this.HumidifierDehumidifier.CurrentRelativeHumidity}`);
441
+ + ` RelativeHumidityHumidifierThreshold: ${this.HumidifierDehumidifier.RelativeHumidityHumidifierThreshold},`
442
+ + ` CurrentRelativeHumidity: ${this.HumidifierDehumidifier.CurrentRelativeHumidity}`);
419
443
  }
420
444
  }
421
445
 
@@ -423,7 +447,7 @@ export class Humidifier extends deviceBase {
423
447
  await this.debugLog('openAPIpushChanges');
424
448
  if ((this.HumidifierDehumidifier.TargetHumidifierDehumidifierState === this.hap.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER)
425
449
  && (this.HumidifierDehumidifier.Active === this.hap.Characteristic.Active.ACTIVE)
426
- && (this.HumidifierDehumidifier.RelativeHumidityHumidifierThreshold !== this.HumidifierDehumidifier.CurrentRelativeHumidity)) {
450
+ && (this.HumidifierDehumidifier.RelativeHumidityHumidifierThreshold !== this.HumidifierDehumidifier.CurrentRelativeHumidity)) {
427
451
  await this.debugLog(`Auto Off, RelativeHumidityHumidifierThreshold: ${this.HumidifierDehumidifier.RelativeHumidityHumidifierThreshold}!`);
428
452
  const bodyChange = JSON.stringify({
429
453
  command: 'setMode',
@@ -42,6 +42,15 @@ export class IOSensor extends deviceBase {
42
42
  CurrentTemperature: CharacteristicValue;
43
43
  };
44
44
 
45
+ // OpenAPI
46
+ deviceStatus!: outdoorMeterStatus;
47
+
48
+ //Webhook
49
+ webhookContext!: outdoorMeterWebhookContext;
50
+
51
+ // BLE
52
+ serviceData!: outdoorMeterServiceData;
53
+
45
54
  // Updates
46
55
  ioSensorUpdateInProgress!: boolean;
47
56
  doIOSensorUpdate: Subject<void>;
@@ -143,12 +152,11 @@ export class IOSensor extends deviceBase {
143
152
  }
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
- // Retrieve initial values and updateHomekit
149
- this.updateHomeKitCharacteristics();
150
-
151
158
  //regisiter webhook event handler
159
+ this.debugLog('Registering Webhook Event Handler');
152
160
  this.registerWebhook();
153
161
 
154
162
  // Start an update interval
@@ -159,11 +167,14 @@ export class IOSensor extends deviceBase {
159
167
  });
160
168
  }
161
169
 
162
- async BLEparseStatus(serviceData: outdoorMeterServiceData): Promise<void> {
170
+ async BLEparseStatus(): Promise<void> {
163
171
  await this.debugLog('BLEparseStatus');
172
+ await this.debugLog(`(battery, temperature, humidity) = BLE:(${this.serviceData.battery}, ${this.serviceData.temperature.c},`
173
+ + ` ${this.serviceData.humidity}), current:(${this.Battery.BatteryLevel}, ${this.TemperatureSensor?.CurrentTemperature},`
174
+ + ` ${this.HumiditySensor?.CurrentRelativeHumidity})`);
164
175
 
165
176
  // BatteryLevel
166
- this.Battery.BatteryLevel = Number(serviceData.battery);
177
+ this.Battery.BatteryLevel = this.serviceData.battery;
167
178
  await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`);
168
179
 
169
180
  // StatusLowBattery
@@ -173,23 +184,26 @@ export class IOSensor extends deviceBase {
173
184
 
174
185
  // CurrentRelativeHumidity
175
186
  if (!this.device.iosensor?.hide_humidity && this.HumiditySensor?.Service) {
176
- this.HumiditySensor.CurrentRelativeHumidity = serviceData.humidity;
187
+ this.HumiditySensor.CurrentRelativeHumidity = this.serviceData.humidity;
177
188
  await this.debugLog(`CurrentRelativeHumidity: ${this.HumiditySensor.CurrentRelativeHumidity}%`);
178
189
  }
179
190
 
180
191
  // Current Temperature
181
192
  if (!this.device.meter?.hide_temperature && this.TemperatureSensor?.Service) {
182
- const CELSIUS = serviceData.temperature.c < 0 ? 0 : serviceData.temperature.c > 100 ? 100 : serviceData.temperature.c;
193
+ const CELSIUS = this.serviceData.temperature.c < 0 ? 0 : this.serviceData.temperature.c > 100 ? 100 : this.serviceData.temperature.c;
183
194
  this.TemperatureSensor.CurrentTemperature = CELSIUS;
184
195
  await this.debugLog(`Temperature: ${this.TemperatureSensor.CurrentTemperature}°c`);
185
196
  }
186
197
  }
187
198
 
188
- async openAPIparseStatus(deviceStatus: outdoorMeterStatus): Promise<void> {
199
+ async openAPIparseStatus(): Promise<void> {
189
200
  await this.debugLog('openAPIparseStatus');
201
+ await this.debugLog(`(battery, temperature, humidity) = OpenAPI:(${this.deviceStatus.battery}, ${this.deviceStatus.temperature},`
202
+ + ` ${this.deviceStatus.humidity}), current:(${this.Battery.BatteryLevel}, ${this.TemperatureSensor?.CurrentTemperature},`
203
+ + ` ${this.HumiditySensor?.CurrentRelativeHumidity})`);
190
204
 
191
205
  // BatteryLevel
192
- this.Battery.BatteryLevel = Number(deviceStatus.battery);
206
+ this.Battery.BatteryLevel = this.deviceStatus.battery;
193
207
  await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`);
194
208
 
195
209
  // StatusLowBattery
@@ -199,20 +213,20 @@ export class IOSensor extends deviceBase {
199
213
 
200
214
  // CurrentRelativeHumidity
201
215
  if (!this.device.iosensor?.hide_humidity && this.HumiditySensor?.Service) {
202
- this.HumiditySensor.CurrentRelativeHumidity = deviceStatus.humidity;
216
+ this.HumiditySensor.CurrentRelativeHumidity = this.deviceStatus.humidity;
203
217
  await this.debugLog(`CurrentRelativeHumidity: ${this.HumiditySensor.CurrentRelativeHumidity}%`);
204
218
  }
205
219
 
206
220
  // Current Temperature
207
221
  if (!this.device.meter?.hide_temperature && this.TemperatureSensor?.Service) {
208
- this.TemperatureSensor.CurrentTemperature = deviceStatus.temperature;
222
+ this.TemperatureSensor.CurrentTemperature = this.deviceStatus.temperature;
209
223
  await this.debugLog(`CurrentTemperature: ${this.TemperatureSensor.CurrentTemperature}°c`);
210
224
  }
211
225
 
212
226
  // Firmware Version
213
- const version = deviceStatus.version.toString();
214
- await this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
215
- if (deviceStatus.version) {
227
+ if (this.deviceStatus.version) {
228
+ const version = this.deviceStatus.version.toString();
229
+ await this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
216
230
  const deviceVersion = version.replace(/^V|-.*$/g, '') ?? '0.0.0';
217
231
  this.accessory
218
232
  .getService(this.hap.Service.AccessoryInformation)!
@@ -225,24 +239,25 @@ export class IOSensor extends deviceBase {
225
239
  }
226
240
  }
227
241
 
228
- async parseStatusWebhook(context: outdoorMeterWebhookContext): Promise<void> {
242
+ async parseStatusWebhook(): Promise<void> {
229
243
  await this.debugLog('parseStatusWebhook');
230
- await this.debugLog(`(scale, temperature, humidity) = Webhook:(${context.scale}, ${convertUnits(context.temperature, context.scale,
231
- this.device.iosensor?.convertUnitTo)}, ${context.humidity}), current:(${this.TemperatureSensor?.CurrentTemperature},`
232
- + ` ${this.HumiditySensor?.CurrentRelativeHumidity})`);
244
+ await this.debugLog(`(scale, temperature, humidity) = Webhook:(${this.webhookContext.scale}, ${convertUnits(this.webhookContext.temperature,
245
+ this.webhookContext.scale, this.device.iosensor?.convertUnitTo)}, ${this.webhookContext.humidity}),`
246
+ + ` current:(${this.TemperatureSensor?.CurrentTemperature}, ${this.HumiditySensor?.CurrentRelativeHumidity})`);
233
247
 
234
- if (context.scale !== 'CELSIUS' && this.device.iosensor?.convertUnitTo === undefined) {
235
- 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.iosensor?.convertUnitTo === undefined) {
249
+ await this.warnLog(`received a non-CELSIUS Webhook scale: ${this.webhookContext.scale}, Use the *convertUnitsTo* config under Hub settings,`
236
250
  + ' if displaying incorrectly in HomeKit.');
237
251
  }
238
252
  // CurrentRelativeHumidity
239
253
  if (this.device.iosensor?.hide_humidity && this.HumiditySensor?.Service) {
240
- this.HumiditySensor.CurrentRelativeHumidity = context.humidity;
254
+ this.HumiditySensor.CurrentRelativeHumidity = this.webhookContext.humidity;
241
255
  await this.debugLog(`CurrentRelativeHumidity: ${this.HumiditySensor.CurrentRelativeHumidity}%`);
242
256
  }
243
257
  // CurrentTemperature
244
258
  if (this.device.iosensor?.hide_temperature && this.TemperatureSensor?.Service) {
245
- this.TemperatureSensor.CurrentTemperature = convertUnits(context.temperature, context.scale, this.device.iosensor?.convertUnitTo);
259
+ this.TemperatureSensor.CurrentTemperature = convertUnits(this.webhookContext.temperature, this.webhookContext.scale,
260
+ this.device.iosensor?.convertUnitTo);
246
261
  await this.debugLog(`CurrentTemperature: ${this.TemperatureSensor.CurrentTemperature}°c`);
247
262
  }
248
263
  }
@@ -273,10 +288,11 @@ export class IOSensor extends deviceBase {
273
288
  // Start to monitor advertisement packets
274
289
  (async () => {
275
290
  // Start to monitor advertisement packets
276
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as unknown as outdoorMeterServiceData;
291
+ const serviceData = await this.monitorAdvertisementPackets(switchbot) as outdoorMeterServiceData;
277
292
  // Update HomeKit
278
293
  if (serviceData.model === SwitchBotBLEModel.OutdoorMeter && serviceData.modelName === SwitchBotBLEModelName.OutdoorMeter) {
279
- await this.BLEparseStatus(serviceData);
294
+ this.serviceData = serviceData;
295
+ await this.BLEparseStatus();
280
296
  await this.updateHomeKitCharacteristics();
281
297
  } else {
282
298
  await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
@@ -294,7 +310,8 @@ export class IOSensor extends deviceBase {
294
310
  await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);;
295
311
  if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
296
312
  await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
297
- await this.openAPIparseStatus(deviceStatus.body);
313
+ this.deviceStatus = deviceStatus.body;
314
+ await this.openAPIparseStatus();
298
315
  await this.updateHomeKitCharacteristics();
299
316
  } else {
300
317
  await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
@@ -312,7 +329,8 @@ export class IOSensor extends deviceBase {
312
329
  this.platform.webhookEventHandler[this.device.deviceId] = async (context: outdoorMeterWebhookContext) => {
313
330
  try {
314
331
  await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
315
- await this.parseStatusWebhook(context);
332
+ this.webhookContext = context;
333
+ await this.parseStatusWebhook();
316
334
  await this.updateHomeKitCharacteristics();
317
335
  } catch (e: any) {
318
336
  await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);