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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (150) hide show
  1. package/config.schema.json +19 -3
  2. package/dist/device/blindtilt.d.ts +6 -4
  3. package/dist/device/blindtilt.d.ts.map +1 -1
  4. package/dist/device/blindtilt.js +57 -45
  5. package/dist/device/blindtilt.js.map +1 -1
  6. package/dist/device/bot.d.ts +6 -3
  7. package/dist/device/bot.d.ts.map +1 -1
  8. package/dist/device/bot.js +38 -24
  9. package/dist/device/bot.js.map +1 -1
  10. package/dist/device/ceilinglight.d.ts +6 -3
  11. package/dist/device/ceilinglight.d.ts.map +1 -1
  12. package/dist/device/ceilinglight.js +40 -21
  13. package/dist/device/ceilinglight.js.map +1 -1
  14. package/dist/device/colorbulb.d.ts +6 -3
  15. package/dist/device/colorbulb.d.ts.map +1 -1
  16. package/dist/device/colorbulb.js +42 -31
  17. package/dist/device/colorbulb.js.map +1 -1
  18. package/dist/device/contact.d.ts +6 -3
  19. package/dist/device/contact.d.ts.map +1 -1
  20. package/dist/device/contact.js +40 -28
  21. package/dist/device/contact.js.map +1 -1
  22. package/dist/device/curtain.d.ts +6 -4
  23. package/dist/device/curtain.d.ts.map +1 -1
  24. package/dist/device/curtain.js +35 -22
  25. package/dist/device/curtain.js.map +1 -1
  26. package/dist/device/device.d.ts +13 -2
  27. package/dist/device/device.d.ts.map +1 -1
  28. package/dist/device/device.js +223 -108
  29. package/dist/device/device.js.map +1 -1
  30. package/dist/device/fan.d.ts +6 -3
  31. package/dist/device/fan.d.ts.map +1 -1
  32. package/dist/device/fan.js +48 -29
  33. package/dist/device/fan.js.map +1 -1
  34. package/dist/device/hub.d.ts +6 -3
  35. package/dist/device/hub.d.ts.map +1 -1
  36. package/dist/device/hub.js +43 -26
  37. package/dist/device/hub.js.map +1 -1
  38. package/dist/device/humidifier.d.ts +6 -3
  39. package/dist/device/humidifier.d.ts.map +1 -1
  40. package/dist/device/humidifier.js +43 -26
  41. package/dist/device/humidifier.js.map +1 -1
  42. package/dist/device/iosensor.d.ts +6 -3
  43. package/dist/device/iosensor.d.ts.map +1 -1
  44. package/dist/device/iosensor.js +39 -24
  45. package/dist/device/iosensor.js.map +1 -1
  46. package/dist/device/lightstrip.d.ts +6 -3
  47. package/dist/device/lightstrip.d.ts.map +1 -1
  48. package/dist/device/lightstrip.js +43 -27
  49. package/dist/device/lightstrip.js.map +1 -1
  50. package/dist/device/lock.d.ts +6 -3
  51. package/dist/device/lock.d.ts.map +1 -1
  52. package/dist/device/lock.js +33 -22
  53. package/dist/device/lock.js.map +1 -1
  54. package/dist/device/meter.d.ts +6 -3
  55. package/dist/device/meter.d.ts.map +1 -1
  56. package/dist/device/meter.js +38 -24
  57. package/dist/device/meter.js.map +1 -1
  58. package/dist/device/meterplus.d.ts +6 -3
  59. package/dist/device/meterplus.d.ts.map +1 -1
  60. package/dist/device/meterplus.js +37 -24
  61. package/dist/device/meterplus.js.map +1 -1
  62. package/dist/device/motion.d.ts +6 -3
  63. package/dist/device/motion.d.ts.map +1 -1
  64. package/dist/device/motion.js +33 -22
  65. package/dist/device/motion.js.map +1 -1
  66. package/dist/device/plug.d.ts +6 -3
  67. package/dist/device/plug.d.ts.map +1 -1
  68. package/dist/device/plug.js +27 -16
  69. package/dist/device/plug.js.map +1 -1
  70. package/dist/device/robotvacuumcleaner.d.ts +6 -3
  71. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  72. package/dist/device/robotvacuumcleaner.js +34 -21
  73. package/dist/device/robotvacuumcleaner.js.map +1 -1
  74. package/dist/device/waterdetector.d.ts +6 -3
  75. package/dist/device/waterdetector.d.ts.map +1 -1
  76. package/dist/device/waterdetector.js +38 -23
  77. package/dist/device/waterdetector.js.map +1 -1
  78. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  79. package/dist/irdevice/airconditioner.js +19 -72
  80. package/dist/irdevice/airconditioner.js.map +1 -1
  81. package/dist/irdevice/airpurifier.js +4 -4
  82. package/dist/irdevice/airpurifier.js.map +1 -1
  83. package/dist/irdevice/camera.d.ts.map +1 -1
  84. package/dist/irdevice/camera.js +3 -10
  85. package/dist/irdevice/camera.js.map +1 -1
  86. package/dist/irdevice/fan.d.ts.map +1 -1
  87. package/dist/irdevice/fan.js +7 -25
  88. package/dist/irdevice/fan.js.map +1 -1
  89. package/dist/irdevice/irdevice.d.ts +1 -1
  90. package/dist/irdevice/irdevice.js +1 -1
  91. package/dist/irdevice/irdevice.js.map +1 -1
  92. package/dist/irdevice/light.d.ts.map +1 -1
  93. package/dist/irdevice/light.js +9 -27
  94. package/dist/irdevice/light.js.map +1 -1
  95. package/dist/irdevice/other.js +1 -1
  96. package/dist/irdevice/other.js.map +1 -1
  97. package/dist/irdevice/tv.d.ts.map +1 -1
  98. package/dist/irdevice/tv.js +3 -18
  99. package/dist/irdevice/tv.js.map +1 -1
  100. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  101. package/dist/irdevice/vacuumcleaner.js +2 -9
  102. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  103. package/dist/irdevice/waterheater.d.ts.map +1 -1
  104. package/dist/irdevice/waterheater.js +2 -9
  105. package/dist/irdevice/waterheater.js.map +1 -1
  106. package/dist/platform.d.ts +0 -11
  107. package/dist/platform.d.ts.map +1 -1
  108. package/dist/platform.js +11 -184
  109. package/dist/platform.js.map +1 -1
  110. package/dist/settings.d.ts +9 -8
  111. package/dist/settings.d.ts.map +1 -1
  112. package/dist/settings.js.map +1 -1
  113. package/dist/types/bledevicestatus.d.ts +2 -1
  114. package/dist/types/bledevicestatus.d.ts.map +1 -1
  115. package/dist/types/devicestatus.d.ts +1 -1
  116. package/dist/types/devicestatus.d.ts.map +1 -1
  117. package/package.json +9 -9
  118. package/src/device/blindtilt.ts +60 -47
  119. package/src/device/bot.ts +47 -25
  120. package/src/device/ceilinglight.ts +52 -21
  121. package/src/device/colorbulb.ts +45 -31
  122. package/src/device/contact.ts +43 -28
  123. package/src/device/curtain.ts +45 -23
  124. package/src/device/device.ts +226 -96
  125. package/src/device/fan.ts +61 -30
  126. package/src/device/hub.ts +55 -29
  127. package/src/device/humidifier.ts +55 -31
  128. package/src/device/iosensor.ts +45 -27
  129. package/src/device/lightstrip.ts +48 -28
  130. package/src/device/lock.ts +49 -23
  131. package/src/device/meter.ts +54 -26
  132. package/src/device/meterplus.ts +50 -26
  133. package/src/device/motion.ts +45 -24
  134. package/src/device/plug.ts +34 -17
  135. package/src/device/robotvacuumcleaner.ts +49 -23
  136. package/src/device/waterdetector.ts +56 -25
  137. package/src/irdevice/airconditioner.ts +19 -65
  138. package/src/irdevice/airpurifier.ts +4 -4
  139. package/src/irdevice/camera.ts +4 -9
  140. package/src/irdevice/fan.ts +10 -22
  141. package/src/irdevice/irdevice.ts +1 -1
  142. package/src/irdevice/light.ts +10 -24
  143. package/src/irdevice/other.ts +1 -1
  144. package/src/irdevice/tv.ts +5 -16
  145. package/src/irdevice/vacuumcleaner.ts +3 -8
  146. package/src/irdevice/waterheater.ts +3 -8
  147. package/src/platform.ts +11 -184
  148. package/src/settings.ts +10 -8
  149. package/src/types/bledevicestatus.ts +4 -2
  150. package/src/types/devicestatus.ts +1 -1
@@ -37,6 +37,15 @@ export class Meter extends deviceBase {
37
37
  CurrentTemperature: CharacteristicValue;
38
38
  };
39
39
 
40
+ // OpenAPI
41
+ deviceStatus!: meterStatus;
42
+
43
+ //Webhook
44
+ webhookContext!: meterWebhookContext;
45
+
46
+ // BLE
47
+ serviceData!: meterServiceData;
48
+
40
49
  // Updates
41
50
  meterUpdateInProgress!: boolean;
42
51
  doMeterUpdate: Subject<void>;
@@ -136,12 +145,11 @@ export class Meter extends deviceBase {
136
145
  }
137
146
 
138
147
  // Retrieve initial values and updateHomekit
148
+ this.debugLog('Retrieve initial values and update Homekit');
139
149
  this.refreshStatus();
140
150
 
141
- // Retrieve initial values and update Homekit
142
- this.updateHomeKitCharacteristics();
143
-
144
151
  //regisiter webhook event handler
152
+ this.debugLog('Registering Webhook Event Handler');
145
153
  this.registerWebhook();
146
154
 
147
155
  // Start an update interval
@@ -152,51 +160,65 @@ export class Meter extends deviceBase {
152
160
  });
153
161
  }
154
162
 
155
- async BLEparseStatus(serviceData: meterServiceData): Promise<void> {
163
+ async BLEparseStatus(): Promise<void> {
156
164
  await this.debugLog('BLEparseStatus');
165
+ await this.debugLog(`(scale, temperature, humidity) = BLE:(${this.serviceData.fahrenheit}, ${this.serviceData.temperature.c},`
166
+ + ` ${this.serviceData.humidity}), current:(${this.TemperatureSensor?.CurrentTemperature}, ${this.HumiditySensor?.CurrentRelativeHumidity})`);
167
+
157
168
  // CurrentRelativeHumidity
158
169
  if (!this.device.iosensor?.hide_humidity && this.HumiditySensor?.Service) {
159
- this.HumiditySensor.CurrentRelativeHumidity = serviceData.humidity;
170
+ this.HumiditySensor.CurrentRelativeHumidity = this.serviceData.humidity;
160
171
  await this.debugLog(`CurrentRelativeHumidity: ${this.HumiditySensor.CurrentRelativeHumidity}%`);
161
172
  }
173
+
162
174
  // Current Temperature
163
175
  if (!this.device.meter?.hide_temperature && this.TemperatureSensor?.Service) {
164
- const CELSIUS = serviceData.temperature.c < 0 ? 0 : serviceData.temperature.c > 100 ? 100 : serviceData.temperature.c;
176
+ const CELSIUS = this.serviceData.temperature.c < 0 ? 0 : this.serviceData.temperature.c > 100 ? 100 : this.serviceData.temperature.c;
165
177
  this.TemperatureSensor.CurrentTemperature = CELSIUS;
166
178
  await this.debugLog(`CurrentTemperature: ${this.TemperatureSensor.CurrentTemperature}°c`);
167
179
  }
180
+
168
181
  // BatteryLevel
169
- this.Battery.BatteryLevel = Number(serviceData.battery);
182
+ this.Battery.BatteryLevel = this.serviceData.battery;
170
183
  await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`);
184
+
171
185
  // StatusLowBattery
172
186
  this.Battery.StatusLowBattery = this.Battery.BatteryLevel < 10
173
187
  ? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW : this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
174
188
  await this.debugLog(`StatusLowBattery: ${this.Battery.StatusLowBattery}`);
175
189
  }
176
190
 
177
- async openAPIparseStatus(deviceStatus: meterStatus): Promise<void> {
191
+ async openAPIparseStatus(): Promise<void> {
178
192
  await this.debugLog('openAPIparseStatus');
193
+ await this.debugLog(`(battery, temperature, humidity) = OpenAPI:(${this.deviceStatus.battery}, ${this.deviceStatus.temperature},`
194
+ + ` ${this.deviceStatus.humidity}), current:(${this.Battery?.BatteryLevel}, ${this.TemperatureSensor?.CurrentTemperature},`
195
+ + ` ${this.HumiditySensor?.CurrentRelativeHumidity})`);
196
+
179
197
  // CurrentRelativeHumidity
180
198
  if (!this.device.meter?.hide_humidity && this.HumiditySensor?.Service) {
181
- this.HumiditySensor.CurrentRelativeHumidity = deviceStatus.humidity;
199
+ this.HumiditySensor.CurrentRelativeHumidity = this.deviceStatus.humidity;
182
200
  await this.debugLog(`CurrentRelativeHumidity: ${this.HumiditySensor.CurrentRelativeHumidity}%`);
183
201
  }
202
+
184
203
  // CurrentTemperature
185
204
  if (!this.device.meter?.hide_temperature && this.TemperatureSensor?.Service) {
186
- this.TemperatureSensor.CurrentTemperature = deviceStatus.temperature;
205
+ this.TemperatureSensor.CurrentTemperature = this.deviceStatus.temperature;
187
206
  await this.debugLog(`CurrentTemperature: ${this.TemperatureSensor.CurrentTemperature}°c`);
188
207
  }
208
+
189
209
  // BatteryLevel
190
- this.Battery.BatteryLevel = Number(deviceStatus.battery);
210
+ this.Battery.BatteryLevel = this.deviceStatus.battery;
191
211
  await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`);
212
+
192
213
  // StatusLowBattery
193
214
  this.Battery.StatusLowBattery = this.Battery.BatteryLevel < 10
194
215
  ? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW : this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
195
216
  await this.debugLog(`StatusLowBattery: ${this.Battery.StatusLowBattery}`);
217
+
196
218
  // FirmwareVersion
197
- const version = deviceStatus.version.toString();
198
- await this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
199
- if (deviceStatus.version) {
219
+ if (this.deviceStatus.version) {
220
+ const version = this.deviceStatus.version.toString();
221
+ await this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
200
222
  const deviceVersion = version.replace(/^V|-.*$/g, '') ?? '0.0.0';
201
223
  this.accessory
202
224
  .getService(this.hap.Service.AccessoryInformation)!
@@ -209,24 +231,27 @@ export class Meter extends deviceBase {
209
231
  }
210
232
  }
211
233
 
212
- async parseStatusWebhook(context: meterWebhookContext): Promise<void> {
234
+ async parseStatusWebhook(): Promise<void> {
213
235
  await this.debugLog('parseStatusWebhook');
214
- await this.debugLog(`(scale, temperature, humidity) = Webhook:(${context.scale}, ${convertUnits(context.temperature, context.scale,
215
- this.device.meter?.convertUnitTo)}, ${context.humidity}), current:(${this.TemperatureSensor?.CurrentTemperature},`
216
- + ` ${this.HumiditySensor?.CurrentRelativeHumidity})`);
236
+ await this.debugLog(`(scale, temperature, humidity) = Webhook:(${this.webhookContext.scale}, ${convertUnits(this.webhookContext.temperature,
237
+ this.webhookContext.scale, this.device.meter?.convertUnitTo)}, ${this.webhookContext.humidity}),`
238
+ + ` current:(${this.TemperatureSensor?.CurrentTemperature}, ${this.HumiditySensor?.CurrentRelativeHumidity})`);
217
239
  // Check if the scale is not CELSIUS
218
- if (context.scale !== 'CELSIUS' && this.device.hub?.convertUnitTo === undefined) {
219
- await this.warnLog(`received a non-CELSIUS Webhook scale: ${context.scale}, Use the *convertUnitsTo* config under Hub settings,`
240
+ if (this.webhookContext.scale !== 'CELSIUS' && this.device.hub?.convertUnitTo === undefined) {
241
+ await this.warnLog(`received a non-CELSIUS Webhook scale: ${this.webhookContext.scale}, Use the *convertUnitsTo* config under Hub settings,`
220
242
  + ' if displaying incorrectly in HomeKit.');
221
243
  }
244
+
222
245
  // CurrentRelativeHumidity
223
246
  if (!this.device.meter?.hide_humidity && this.HumiditySensor?.Service) {
224
- this.HumiditySensor.CurrentRelativeHumidity = context.humidity;
247
+ this.HumiditySensor.CurrentRelativeHumidity = this.webhookContext.humidity;
225
248
  await this.debugLog(`CurrentRelativeHumidity: ${this.HumiditySensor.CurrentRelativeHumidity}`);
226
249
  }
250
+
227
251
  // CurrentTemperature
228
252
  if (!this.device.meter?.hide_temperature && this.TemperatureSensor?.Service) {
229
- this.TemperatureSensor.CurrentTemperature = convertUnits(context.temperature, context.scale, this.device.hub?.convertUnitTo);
253
+ this.TemperatureSensor.CurrentTemperature = convertUnits(this.webhookContext.temperature, this.webhookContext.scale,
254
+ this.device.hub?.convertUnitTo);
230
255
  await this.debugLog(`CurrentTemperature: ${this.TemperatureSensor.CurrentTemperature}`);
231
256
  }
232
257
  }
@@ -260,7 +285,8 @@ export class Meter extends deviceBase {
260
285
  const serviceData = await this.monitorAdvertisementPackets(switchbot) as meterServiceData;
261
286
  // Update HomeKit
262
287
  if (serviceData.model === SwitchBotBLEModel.Meter && serviceData.modelName === SwitchBotBLEModelName.Meter) {
263
- await this.BLEparseStatus(serviceData);
288
+ this.serviceData = serviceData;
289
+ await this.BLEparseStatus();
264
290
  await this.updateHomeKitCharacteristics();
265
291
  } else {
266
292
  await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
@@ -278,7 +304,8 @@ export class Meter extends deviceBase {
278
304
  await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);;
279
305
  if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
280
306
  await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
281
- await this.openAPIparseStatus(deviceStatus.body);
307
+ this.deviceStatus = deviceStatus.body;
308
+ await this.openAPIparseStatus();
282
309
  await this.updateHomeKitCharacteristics();
283
310
  } else {
284
311
  await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
@@ -293,10 +320,11 @@ export class Meter extends deviceBase {
293
320
  async registerWebhook() {
294
321
  if (this.device.webhook) {
295
322
  await this.debugLog('is listening webhook.');
296
- this.platform.webhookEventHandler[this.device.deviceId] = async (context: meterWebhookContext) => {
323
+ this.webhookEventHandler[this.device.deviceId] = async (context: meterWebhookContext) => {
297
324
  try {
298
325
  await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
299
- await this.parseStatusWebhook(context);
326
+ this.webhookContext = context;
327
+ await this.parseStatusWebhook();
300
328
  await this.updateHomeKitCharacteristics();
301
329
  } catch (e: any) {
302
330
  await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
@@ -42,6 +42,15 @@ export class MeterPlus extends deviceBase {
42
42
  CurrentTemperature: CharacteristicValue;
43
43
  };
44
44
 
45
+ // OpenAPI
46
+ deviceStatus!: meterPlusStatus;
47
+
48
+ //Webhook
49
+ webhookContext!: meterPlusWebhookContext;
50
+
51
+ // BLE
52
+ serviceData!: meterPlusServiceData;
53
+
45
54
  // Updates
46
55
  meterUpdateInProgress!: boolean;
47
56
  doMeterUpdate: Subject<void>;
@@ -141,12 +150,11 @@ export class MeterPlus extends deviceBase {
141
150
  }
142
151
 
143
152
  // Retrieve initial values and updateHomekit
153
+ this.debugLog('Retrieve initial values and update Homekit');
144
154
  this.refreshStatus();
145
155
 
146
- // Retrieve initial values and updateHomekit
147
- this.updateHomeKitCharacteristics();
148
-
149
156
  //regisiter webhook event handler
157
+ this.debugLog('Registering Webhook Event Handler');
150
158
  this.registerWebhook();
151
159
 
152
160
  // Start an update interval
@@ -157,21 +165,24 @@ export class MeterPlus extends deviceBase {
157
165
  });
158
166
  }
159
167
 
160
- async BLEparseStatus(serviceData: meterPlusServiceData): Promise<void> {
168
+ async BLEparseStatus(): Promise<void> {
161
169
  await this.debugLog('BLEparseStatus');
170
+ await this.debugLog(`(temperature, humidity) = BLE:(${this.serviceData.temperature.c}, ${this.serviceData.humidity}),`
171
+ + ` current:(${this.TemperatureSensor?.CurrentTemperature}, ${this.HumiditySensor?.CurrentRelativeHumidity})`);
172
+
162
173
  // CurrentRelativeHumidity
163
174
  if (!this.device.iosensor?.hide_humidity && this.HumiditySensor?.Service) {
164
- this.HumiditySensor.CurrentRelativeHumidity = serviceData.humidity;
175
+ this.HumiditySensor.CurrentRelativeHumidity = this.serviceData.humidity;
165
176
  await this.debugLog(`CurrentRelativeHumidity: ${this.HumiditySensor.CurrentRelativeHumidity}%`);
166
177
  }
167
178
  // Current Temperature
168
179
  if (!this.device.meter?.hide_temperature && this.TemperatureSensor?.Service) {
169
- const CELSIUS = serviceData.temperature.c < 0 ? 0 : serviceData.temperature.c > 100 ? 100 : serviceData.temperature.c;
180
+ const CELSIUS = this.serviceData.temperature.c < 0 ? 0 : this.serviceData.temperature.c > 100 ? 100 : this.serviceData.temperature.c;
170
181
  this.TemperatureSensor.CurrentTemperature = CELSIUS;
171
182
  await this.debugLog(`CurrentTemperature: ${this.TemperatureSensor.CurrentTemperature}°c`);
172
183
  }
173
184
  // BatteryLevel
174
- this.Battery.BatteryLevel = Number(serviceData.battery);
185
+ this.Battery.BatteryLevel = this.serviceData.battery;
175
186
  await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`);
176
187
  // StatusLowBattery
177
188
  this.Battery.StatusLowBattery = this.Battery.BatteryLevel < 10
@@ -179,29 +190,36 @@ export class MeterPlus extends deviceBase {
179
190
  await this.debugLog(`StatusLowBattery: ${this.Battery.StatusLowBattery}`);
180
191
  }
181
192
 
182
- async openAPIparseStatus(deviceStatus: meterPlusStatus): Promise<void> {
193
+ async openAPIparseStatus(): Promise<void> {
183
194
  await this.debugLog('openAPIparseStatus');
195
+ await this.debugLog(`(temperature, humidity) = OpenAPI:(${this.deviceStatus.temperature}, ${this.deviceStatus.humidity}),`
196
+ + ` current:(${this.TemperatureSensor?.CurrentTemperature}, ${this.HumiditySensor?.CurrentRelativeHumidity})`);
197
+
184
198
  // CurrentRelativeHumidity
185
199
  if (!this.device.meter?.hide_humidity && this.HumiditySensor?.Service) {
186
- this.HumiditySensor.CurrentRelativeHumidity = deviceStatus.humidity;
200
+ this.HumiditySensor.CurrentRelativeHumidity = this.deviceStatus.humidity;
187
201
  await this.debugLog(`CurrentRelativeHumidity: ${this.HumiditySensor.CurrentRelativeHumidity}%`);
188
202
  }
203
+
189
204
  // CurrentTemperature
190
205
  if (!this.device.meter?.hide_temperature && this.TemperatureSensor?.Service) {
191
- this.TemperatureSensor.CurrentTemperature = deviceStatus.temperature;
206
+ this.TemperatureSensor.CurrentTemperature = this.deviceStatus.temperature;
192
207
  await this.debugLog(`CurrentTemperature: ${this.TemperatureSensor.CurrentTemperature}°c`);
193
208
  }
209
+
194
210
  // BatteryLevel
195
- this.Battery.BatteryLevel = Number(deviceStatus.battery);
211
+ this.Battery.BatteryLevel = this.deviceStatus.battery;
196
212
  await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`);
213
+
197
214
  // StatusLowBattery
198
215
  this.Battery.StatusLowBattery = this.Battery.BatteryLevel < 10
199
216
  ? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW : this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
200
217
  await this.debugLog(`StatusLowBattery: ${this.Battery.StatusLowBattery}`);
218
+
201
219
  // FirmwareVersion
202
- const version = deviceStatus.version.toString();
203
- this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
204
- if (deviceStatus.version) {
220
+ if (this.deviceStatus.version) {
221
+ const version = this.deviceStatus.version.toString();
222
+ this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
205
223
  const deviceVersion = version.replace(/^V|-.*$/g, '') ?? '0.0.0';
206
224
  this.accessory
207
225
  .getService(this.hap.Service.AccessoryInformation)!
@@ -214,24 +232,27 @@ export class MeterPlus extends deviceBase {
214
232
  }
215
233
  }
216
234
 
217
- async parseStatusWebhook(context: meterPlusWebhookContext): Promise<void> {
235
+ async parseStatusWebhook(): Promise<void> {
218
236
  await this.debugLog('parseStatusWebhook');
219
- await this.debugLog(`(scale, temperature, humidity) = Webhook:(${context.scale}, ${convertUnits(context.temperature, context.scale,
220
- this.device.meter?.convertUnitTo)}, ${context.humidity}), current:(${this.TemperatureSensor?.CurrentTemperature},`
221
- + ` ${this.HumiditySensor?.CurrentRelativeHumidity})`);
237
+ await this.debugLog(`(scale, temperature, humidity) = Webhook:(${this.webhookContext.scale}, ${convertUnits(this.webhookContext.temperature,
238
+ this.webhookContext.scale, this.device.meter?.convertUnitTo)}, ${this.webhookContext.humidity}),`
239
+ + ` current:(${this.TemperatureSensor?.CurrentTemperature}, ${this.HumiditySensor?.CurrentRelativeHumidity})`);
222
240
  // Check if the scale is not CELSIUS
223
- if (context.scale !== 'CELSIUS' && this.device.hub?.convertUnitTo === undefined) {
224
- await this.warnLog(`received a non-CELSIUS Webhook scale: ${context.scale}, Use the *convertUnitsTo* config under Hub settings,`
241
+ if (this.webhookContext.scale !== 'CELSIUS' && this.device.hub?.convertUnitTo === undefined) {
242
+ await this.warnLog(`received a non-CELSIUS Webhook scale: ${this.webhookContext.scale}, Use the *convertUnitsTo* config under Hub settings,`
225
243
  + ' if displaying incorrectly in HomeKit.');
226
244
  }
245
+
227
246
  // CurrentRelativeHumidity
228
247
  if (!this.device.meter?.hide_humidity && this.HumiditySensor?.Service) {
229
- this.HumiditySensor.CurrentRelativeHumidity = context.humidity;
248
+ this.HumiditySensor.CurrentRelativeHumidity = this.webhookContext.humidity;
230
249
  await this.debugLog(`CurrentRelativeHumidity: ${this.HumiditySensor.CurrentRelativeHumidity}`);
231
250
  }
251
+
232
252
  // CurrentTemperature
233
253
  if (!this.device.meter?.hide_temperature && this.TemperatureSensor?.Service) {
234
- this.TemperatureSensor.CurrentTemperature = convertUnits(context.temperature, context.scale, this.device.hub?.convertUnitTo);
254
+ this.TemperatureSensor.CurrentTemperature = convertUnits(this.webhookContext.temperature, this.webhookContext.scale,
255
+ this.device.hub?.convertUnitTo);
235
256
  await this.debugLog(`CurrentTemperature: ${this.TemperatureSensor.CurrentTemperature}`);
236
257
  }
237
258
  }
@@ -265,7 +286,8 @@ export class MeterPlus extends deviceBase {
265
286
  const serviceData = await this.monitorAdvertisementPackets(switchbot) as meterPlusServiceData;
266
287
  // Update HomeKit
267
288
  if (serviceData.model === SwitchBotBLEModel.MeterPlus && serviceData.modelName === SwitchBotBLEModelName.MeterPlus) {
268
- await this.BLEparseStatus(serviceData);
289
+ this.serviceData = serviceData;
290
+ await this.BLEparseStatus();
269
291
  await this.updateHomeKitCharacteristics();
270
292
  } else {
271
293
  await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
@@ -283,7 +305,8 @@ export class MeterPlus extends deviceBase {
283
305
  await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);;
284
306
  if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
285
307
  await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
286
- await this.openAPIparseStatus(deviceStatus.body);
308
+ this.deviceStatus = deviceStatus.body;
309
+ await this.openAPIparseStatus();
287
310
  await this.updateHomeKitCharacteristics();
288
311
  } else {
289
312
  await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
@@ -298,10 +321,11 @@ export class MeterPlus extends deviceBase {
298
321
  async registerWebhook() {
299
322
  if (this.device.webhook) {
300
323
  await this.debugLog('is listening webhook.');
301
- this.platform.webhookEventHandler[this.device.deviceId] = async (context: meterPlusWebhookContext) => {
324
+ this.webhookEventHandler[this.device.deviceId] = async (context: meterPlusWebhookContext) => {
302
325
  try {
303
326
  await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
304
- await this.parseStatusWebhook(context);
327
+ this.webhookContext = context;
328
+ await this.parseStatusWebhook();
305
329
  await this.updateHomeKitCharacteristics();
306
330
  } catch (e: any) {
307
331
  await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
@@ -41,6 +41,15 @@ export class Motion extends deviceBase {
41
41
  CurrentAmbientLightLevel: CharacteristicValue;
42
42
  };
43
43
 
44
+ // OpenAPI
45
+ deviceStatus!: motionSensorStatus;
46
+
47
+ //Webhook
48
+ webhookContext!: motionSensorWebhookContext;
49
+
50
+ // BLE
51
+ serviceData!: motionSensorServiceData;
52
+
44
53
  // Updates
45
54
  motionUbpdateInProgress!: boolean;
46
55
  doMotionUpdate!: Subject<void>;
@@ -126,14 +135,12 @@ export class Motion extends deviceBase {
126
135
  });
127
136
  };
128
137
 
129
-
130
138
  // Retrieve initial values and updateHomekit
139
+ this.debugLog('Retrieve initial values and update Homekit');
131
140
  this.refreshStatus();
132
141
 
133
- // Retrieve initial values and updateHomekit
134
- this.updateHomeKitCharacteristics();
135
-
136
142
  //regisiter webhook event handler
143
+ this.debugLog('Registering Webhook Event Handler');
137
144
  this.registerWebhook();
138
145
 
139
146
  // Start an update interval
@@ -144,52 +151,62 @@ export class Motion extends deviceBase {
144
151
  });
145
152
  }
146
153
 
147
- async BLEparseStatus(serviceData: motionSensorServiceData): Promise<void> {
154
+ async BLEparseStatus(): Promise<void> {
148
155
  await this.debugLog('BLEparseStatus');
156
+ await this.debugLog(`(moveDetected) = BLE: (${this.serviceData.movement}), current: (${this.MotionSensor.MotionDetected})`);
157
+
149
158
  // Movement
150
- this.MotionSensor.MotionDetected = serviceData.movement;
159
+ this.MotionSensor.MotionDetected = this.serviceData.movement;
151
160
  await this.debugLog(`MotionDetected: ${this.MotionSensor.MotionDetected}`);
161
+
152
162
  // CurrentAmbientLightLevel
153
163
  if (!this.device.motion?.hide_lightsensor && this.LightSensor?.Service) {
154
164
  const set_minLux = this.device.blindTilt?.set_minLux ?? 1;
155
165
  const set_maxLux = this.device.blindTilt?.set_maxLux ?? 6001;
156
- const lightLevel = serviceData.lightLevel === 'bright' ? set_maxLux : set_minLux;
166
+ const lightLevel = this.serviceData.lightLevel === 'bright' ? set_maxLux : set_minLux;
157
167
  this.LightSensor.CurrentAmbientLightLevel = await this.getLightLevel(lightLevel, set_minLux, set_maxLux, 2);
158
- await this.debugLog(`LightLevel: ${serviceData.lightLevel}, CurrentAmbientLightLevel: ${this.LightSensor.CurrentAmbientLightLevel}`);
168
+ await this.debugLog(`LightLevel: ${this.serviceData.lightLevel}, CurrentAmbientLightLevel: ${this.LightSensor.CurrentAmbientLightLevel}`);
159
169
  }
170
+
160
171
  // BatteryLevel
161
- this.Battery.BatteryLevel = serviceData.battery;
172
+ this.Battery.BatteryLevel = this.serviceData.battery;
162
173
  await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`);
174
+
163
175
  // StatusLowBattery
164
176
  this.Battery.StatusLowBattery = this.Battery.BatteryLevel < 10
165
177
  ? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW : this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
166
178
  await this.debugLog(`StatusLowBattery: ${this.Battery.StatusLowBattery}`);
167
179
  }
168
180
 
169
- async openAPIparseStatus(deviceStatus: motionSensorStatus): Promise<void> {
181
+ async openAPIparseStatus(): Promise<void> {
170
182
  await this.debugLog('openAPIparseStatus');
183
+ await this.debugLog(`(moveDetected) = OpenAPI: (${this.deviceStatus.moveDetected}), current: (${this.MotionSensor.MotionDetected})`);
184
+
171
185
  // Motion State
172
- this.MotionSensor.MotionDetected = deviceStatus.moveDetected;
186
+ this.MotionSensor.MotionDetected = this.deviceStatus.moveDetected;
173
187
  await this.debugLog(`MotionDetected: ${this.MotionSensor.MotionDetected}`);
188
+
174
189
  // CurrentAmbientLightLevel
175
190
  if (!this.device.motion?.hide_lightsensor && this.LightSensor?.Service) {
176
191
  const set_minLux = this.device.blindTilt?.set_minLux ?? 1;
177
192
  const set_maxLux = this.device.blindTilt?.set_maxLux ?? 6001;
178
- const lightLevel = deviceStatus.brightness === 'bright' ? set_maxLux : set_minLux;
193
+ const lightLevel = this.deviceStatus.brightness === 'bright' ? set_maxLux : set_minLux;
179
194
  this.LightSensor.CurrentAmbientLightLevel = await this.getLightLevel(lightLevel, set_minLux, set_maxLux, 2);
180
- await this.debugLog(`LightLevel: ${deviceStatus.brightness}, CurrentAmbientLightLevel: ${this.LightSensor.CurrentAmbientLightLevel}`);
195
+ await this.debugLog(`LightLevel: ${this.deviceStatus.brightness}, CurrentAmbientLightLevel: ${this.LightSensor.CurrentAmbientLightLevel}`);
181
196
  }
182
197
  // BatteryLevel
183
- this.Battery.BatteryLevel = Number(deviceStatus.battery);
198
+ this.Battery.BatteryLevel = this.deviceStatus.battery;
184
199
  await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`);
200
+
185
201
  // StatusLowBattery
186
202
  this.Battery.StatusLowBattery = this.Battery.BatteryLevel < 10
187
203
  ? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW : this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
188
204
  await this.debugLog(`StatusLowBattery: ${this.Battery.StatusLowBattery}`);
205
+
189
206
  // FirmwareVersion
190
- const version = deviceStatus.version.toString();
191
- await this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
192
- if (deviceStatus.version) {
207
+ if (this.deviceStatus.version) {
208
+ const version = this.deviceStatus.version.toString();
209
+ await this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
193
210
  const deviceVersion = version.replace(/^V|-.*$/g, '') ?? '0.0.0';
194
211
  this.accessory
195
212
  .getService(this.hap.Service.AccessoryInformation)!
@@ -202,11 +219,12 @@ export class Motion extends deviceBase {
202
219
  }
203
220
  }
204
221
 
205
- async parseStatusWebhook(context: motionSensorWebhookContext): Promise<void> {
222
+ async parseStatusWebhook(): Promise<void> {
206
223
  await this.debugLog('parseStatusWebhook');
207
- await this.debugLog(`(detectionState) = Webhook: (${context.detectionState}), current: (${this.MotionSensor.MotionDetected})`);
224
+ await this.debugLog(`(detectionState) = Webhook: (${this.webhookContext.detectionState}), current: (${this.MotionSensor.MotionDetected})`);
225
+
208
226
  // MotionDetected
209
- this.MotionSensor.MotionDetected = context.detectionState === 'DETECTED' ? true : false;
227
+ this.MotionSensor.MotionDetected = this.webhookContext.detectionState === 'DETECTED' ? true : false;
210
228
  await this.debugLog(`MotionDetected: ${this.MotionSensor.MotionDetected}`);
211
229
  }
212
230
 
@@ -239,7 +257,8 @@ export class Motion extends deviceBase {
239
257
  const serviceData = await this.monitorAdvertisementPackets(switchbot) as motionSensorServiceData;
240
258
  // Update HomeKit
241
259
  if (serviceData.model === SwitchBotBLEModel.MotionSensor && serviceData.modelName === SwitchBotBLEModelName.MotionSensor) {
242
- await this.BLEparseStatus(serviceData);
260
+ this.serviceData = serviceData;
261
+ await this.BLEparseStatus();
243
262
  await this.updateHomeKitCharacteristics();
244
263
  } else {
245
264
  await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
@@ -257,7 +276,8 @@ export class Motion extends deviceBase {
257
276
  await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);;
258
277
  if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
259
278
  await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
260
- await this.openAPIparseStatus(deviceStatus.body);
279
+ this.deviceStatus = deviceStatus.body;
280
+ await this.openAPIparseStatus();
261
281
  await this.updateHomeKitCharacteristics();
262
282
  } else {
263
283
  await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
@@ -272,10 +292,11 @@ export class Motion extends deviceBase {
272
292
  async registerWebhook() {
273
293
  if (this.device.webhook) {
274
294
  await this.debugLog('is listening webhook.');
275
- this.platform.webhookEventHandler[this.device.deviceId] = async (context: motionSensorWebhookContext) => {
295
+ this.webhookEventHandler[this.device.deviceId] = async (context: motionSensorWebhookContext) => {
276
296
  try {
277
297
  await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
278
- await this.parseStatusWebhook(context);
298
+ this.webhookContext = context;
299
+ await this.parseStatusWebhook();
279
300
  await this.updateHomeKitCharacteristics();
280
301
  } catch (e: any) {
281
302
  await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
@@ -21,6 +21,15 @@ export class Plug extends deviceBase {
21
21
  On: CharacteristicValue;
22
22
  };
23
23
 
24
+ // OpenAPI
25
+ deviceStatus!: plugStatus | plugMiniStatus;
26
+
27
+ //Webhook
28
+ webhookContext!: plugWebhookContext | plugMiniUSWebhookContext | plugMiniJPWebhookContext;
29
+
30
+ // BLE
31
+ serviceData!: plugMiniUSServiceData | plugMiniJPServiceData;
32
+
24
33
  // Updates
25
34
  plugUpdateInProgress!: boolean;
26
35
  doPlugUpdate!: Subject<void>;
@@ -54,12 +63,11 @@ export class Plug extends deviceBase {
54
63
  .onSet(this.OnSet.bind(this));
55
64
 
56
65
  // Retrieve initial values and updateHomekit
66
+ this.debugLog('Retrieve initial values and update Homekit');
57
67
  this.refreshStatus();
58
68
 
59
- // Update Homekit
60
- this.updateHomeKitCharacteristics();
61
-
62
69
  //regisiter webhook event handler
70
+ this.debugLog('Registering Webhook Event Handler');
63
71
  this.registerWebhook();
64
72
 
65
73
  // Start an update interval
@@ -89,22 +97,27 @@ export class Plug extends deviceBase {
89
97
  });
90
98
  }
91
99
 
92
- async BLEparseStatus(serviceData: plugMiniUSServiceData | plugMiniJPServiceData): Promise<void> {
100
+ async BLEparseStatus(): Promise<void> {
93
101
  await this.debugLog('BLEparseStatus');
102
+ await this.debugLog(`(powerState) = BLE: (${this.serviceData.state}), current:(${this.Outlet.On})`);
103
+
94
104
  // On
95
- this.Outlet.On = serviceData.state === 'on' ? true : false;
105
+ this.Outlet.On = this.serviceData.state === 'on' ? true : false;
96
106
  await this.debugLog(`On: ${this.Outlet.On}`);
97
107
  }
98
108
 
99
- async openAPIparseStatus(deviceStatus: plugStatus | plugMiniStatus) {
109
+ async openAPIparseStatus() {
100
110
  await this.debugLog('openAPIparseStatus');
111
+ await this.debugLog(`(powerState) = OpenAPI: (${this.deviceStatus.power}), current:(${this.Outlet.On})`);
112
+
101
113
  // On
102
- this.Outlet.On = deviceStatus.power === 'on' ? true : false;
114
+ this.Outlet.On = this.deviceStatus.power === 'on' ? true : false;
103
115
  await this.debugLog(`On: ${this.Outlet.On}`);
116
+
104
117
  // Firmware Version
105
- const version = deviceStatus.version.toString();
106
- await this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
107
- if (deviceStatus.version) {
118
+ if (this.deviceStatus.version) {
119
+ const version = this.deviceStatus.version.toString();
120
+ await this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
108
121
  const deviceVersion = version.replace(/^V|-.*$/g, '') ?? '0.0.0';
109
122
  this.accessory
110
123
  .getService(this.hap.Service.AccessoryInformation)!
@@ -117,11 +130,12 @@ export class Plug extends deviceBase {
117
130
  }
118
131
  }
119
132
 
120
- async parseStatusWebhook(context: plugWebhookContext | plugMiniUSWebhookContext | plugMiniJPWebhookContext): Promise<void> {
133
+ async parseStatusWebhook(): Promise<void> {
121
134
  await this.debugLog('parseStatusWebhook');
122
- await this.debugLog(`(powerState) = Webhook: (${context.powerState}), current:(${this.Outlet.On})`);
135
+ await this.debugLog(`(powerState) = Webhook: (${this.webhookContext.powerState}), current:(${this.Outlet.On})`);
136
+
123
137
  // On
124
- this.Outlet.On = context.powerState === 'ON' ? true : false;
138
+ this.Outlet.On = this.webhookContext.powerState === 'ON' ? true : false;
125
139
  await this.debugLog(`On: ${this.Outlet.On}`);
126
140
  }
127
141
 
@@ -155,7 +169,8 @@ export class Plug extends deviceBase {
155
169
  // Update HomeKit
156
170
  if ((serviceData.model === SwitchBotBLEModel.PlugMiniUS || SwitchBotBLEModel.PlugMiniJP)
157
171
  && serviceData.modelName === (SwitchBotBLEModelName.PlugMini || SwitchBotBLEModelName.PlugMini)) {
158
- await this.BLEparseStatus(serviceData);
172
+ this.serviceData = serviceData;
173
+ await this.BLEparseStatus();
159
174
  await this.updateHomeKitCharacteristics();
160
175
  } else {
161
176
  await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
@@ -173,7 +188,8 @@ export class Plug extends deviceBase {
173
188
  await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);;
174
189
  if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
175
190
  await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
176
- await this.openAPIparseStatus(deviceStatus.body);
191
+ this.deviceStatus = deviceStatus.body;
192
+ await this.openAPIparseStatus();
177
193
  await this.updateHomeKitCharacteristics();
178
194
  } else {
179
195
  await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
@@ -188,11 +204,12 @@ export class Plug extends deviceBase {
188
204
  async registerWebhook() {
189
205
  if (this.device.webhook) {
190
206
  await this.debugLog('is listening webhook.');
191
- this.platform.webhookEventHandler[this.device.deviceId] = async (context: plugWebhookContext | plugMiniUSWebhookContext
207
+ this.webhookEventHandler[this.device.deviceId] = async (context: plugWebhookContext | plugMiniUSWebhookContext
192
208
  | plugMiniJPWebhookContext) => {
193
209
  try {
194
210
  await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
195
- await this.parseStatusWebhook(context);
211
+ this.webhookContext = context;
212
+ await this.parseStatusWebhook();
196
213
  await this.updateHomeKitCharacteristics();
197
214
  } catch (e: any) {
198
215
  await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);