@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
@@ -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)}`);
@@ -301,7 +324,8 @@ export class MeterPlus extends deviceBase {
301
324
  this.platform.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)}`);
@@ -275,7 +295,8 @@ export class Motion extends deviceBase {
275
295
  this.platform.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)}`);
@@ -192,7 +208,8 @@ export class Plug extends deviceBase {
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}`);
@@ -35,6 +35,15 @@ export class RobotVacuumCleaner extends deviceBase {
35
35
  ChargingState: CharacteristicValue;
36
36
  };
37
37
 
38
+ // OpenAPI
39
+ deviceStatus!: robotVacuumCleanerS1Status | robotVacuumCleanerS1PlusStatus | floorCleaningRobotS10Status;
40
+
41
+ //Webhook
42
+ webhookContext!: robotVacuumCleanerS1WebhookContext | robotVacuumCleanerS1PlusWebhookContext | floorCleaningRobotS10WebhookContext;
43
+
44
+ // BLE
45
+ serviceData!: robotVacuumCleanerServiceData;
46
+
38
47
  // Updates
39
48
  robotVacuumCleanerUpdateInProgress!: boolean;
40
49
  doRobotVacuumCleanerUpdate!: Subject<void>;
@@ -115,12 +124,11 @@ export class RobotVacuumCleaner extends deviceBase {
115
124
  });
116
125
 
117
126
  // Retrieve initial values and updateHomekit
127
+ this.debugLog('Retrieve initial values and update Homekit');
118
128
  this.refreshStatus();
119
129
 
120
- // Update Homekit
121
- this.updateHomeKitCharacteristics();
122
-
123
130
  //regisiter webhook event handler
131
+ this.debugLog('Registering Webhook Event Handler');
124
132
  this.registerWebhook();
125
133
 
126
134
  // Start an update interval
@@ -150,39 +158,51 @@ export class RobotVacuumCleaner extends deviceBase {
150
158
  });
151
159
  }
152
160
 
153
- async BLEparseStatus(serviceData: robotVacuumCleanerServiceData): Promise<void> {
161
+ async BLEparseStatus(): Promise<void> {
154
162
  await this.debugLog('BLEparseStatus');
163
+ await this.debugLog(`(state, battery) = BLE: (${this.serviceData.state}, ${this.serviceData.battery}),`
164
+ + ` current: (${this.LightBulb.On}, ${this.Battery.BatteryLevel})`);
165
+
155
166
  // On
156
- this.LightBulb.On = serviceData.state === 'on' ? true : false;
167
+ this.LightBulb.On = this.serviceData.state === 'on' ? true : false;
157
168
  await this.debugLog(`On: ${this.LightBulb.On}`);
169
+
158
170
  // BatteryLevel
159
- this.Battery.BatteryLevel = Number(serviceData.battery);
171
+ this.Battery.BatteryLevel = this.serviceData.battery;
160
172
  await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`);
173
+
161
174
  // StatusLowBattery
162
175
  this.Battery.StatusLowBattery = this.Battery.BatteryLevel < 10
163
176
  ? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW : this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
164
177
  await this.debugLog(`StatusLowBattery: ${this.Battery.StatusLowBattery}`);
165
178
  }
166
179
 
167
- async openAPIparseStatus(deviceStatus: robotVacuumCleanerS1Status | robotVacuumCleanerS1PlusStatus | floorCleaningRobotS10Status) {
180
+ async openAPIparseStatus() {
168
181
  await this.debugLog('openAPIparseStatus');
182
+ await this.debugLog(`(onlineStatus, battery, workingStatus) = API: (${this.deviceStatus.onlineStatus}, ${this.deviceStatus.battery},`
183
+ + ` ${this.deviceStatus.workingStatus}), current: (${this.LightBulb.On}, ${this.Battery.BatteryLevel}, ${this.Battery.ChargingState})`);
184
+
169
185
  // On
170
- this.LightBulb.On = deviceStatus.onlineStatus === 'online' ? true : false;
186
+ this.LightBulb.On = this.deviceStatus.onlineStatus === 'online' ? true : false;
171
187
  await this.debugLog(`On: ${this.LightBulb.On}`);
188
+
172
189
  // BatteryLevel
173
- this.Battery.BatteryLevel = Number(deviceStatus.battery);
190
+ this.Battery.BatteryLevel = this.deviceStatus.battery;
174
191
  await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`);
192
+
175
193
  // StatusLowBattery
176
194
  this.Battery.StatusLowBattery = this.Battery.BatteryLevel < 10
177
195
  ? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW : this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
178
196
  await this.debugLog(`StatusLowBattery: ${this.Battery.StatusLowBattery}`);
197
+
179
198
  // ChargingState
180
- this.Battery.ChargingState = deviceStatus.workingStatus === 'Charging'
199
+ this.Battery.ChargingState = this.deviceStatus.workingStatus === 'Charging'
181
200
  ? this.hap.Characteristic.ChargingState.CHARGING : this.hap.Characteristic.ChargingState.NOT_CHARGING;
182
201
  await this.debugLog(`ChargingState: ${this.Battery.ChargingState}`);
202
+
183
203
  // Firmware Version
184
- if (deviceStatus.version) {
185
- const version = deviceStatus.version.toString();
204
+ if (this.deviceStatus.version) {
205
+ const version = this.deviceStatus.version.toString();
186
206
  await this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
187
207
  const deviceVersion = version.replace(/^V|-.*$/g, '') ?? '0.0.0';
188
208
  this.accessory
@@ -196,23 +216,26 @@ export class RobotVacuumCleaner extends deviceBase {
196
216
  }
197
217
  }
198
218
 
199
- async parseStatusWebhook(context: robotVacuumCleanerS1WebhookContext | robotVacuumCleanerS1PlusWebhookContext
200
- | floorCleaningRobotS10WebhookContext): Promise<void> {
219
+ async parseStatusWebhook(): Promise<void> {
201
220
  await this.debugLog('parseStatusWebhook');
202
- await this.debugLog(`(onlineStatus, battery, workingStatus) = Webhook: (${context.onlineStatus}, ${context.battery}, ${context.workingStatus}),`
203
- + ` current: (${this.LightBulb.On}, ${this.Battery.BatteryLevel}, ${this.Battery.ChargingState})`);
221
+ await this.debugLog(`(onlineStatus, battery, workingStatus) = Webhook: (${this.webhookContext.onlineStatus}, ${this.webhookContext.battery},`
222
+ + ` ${this.webhookContext.workingStatus}), current: (${this.LightBulb.On}, ${this.Battery.BatteryLevel}, ${this.Battery.ChargingState})`);
223
+
204
224
  // On
205
- this.LightBulb.On = context.onlineStatus === 'online' ? true : false;
225
+ this.LightBulb.On = this.webhookContext.onlineStatus === 'online' ? true : false;
206
226
  await this.debugLog(`On: ${this.LightBulb.On}`);
227
+
207
228
  // BatteryLevel
208
- this.Battery.BatteryLevel = Number(context.battery);
229
+ this.Battery.BatteryLevel = this.webhookContext.battery;
209
230
  await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`);
231
+
210
232
  // StatusLowBattery
211
233
  this.Battery.StatusLowBattery = this.Battery.BatteryLevel < 10
212
234
  ? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW : this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
213
235
  await this.debugLog(`StatusLowBattery: ${this.Battery.StatusLowBattery}`);
236
+
214
237
  // ChargingState
215
- this.Battery.ChargingState = context.workingStatus === 'Charging'
238
+ this.Battery.ChargingState = this.webhookContext.workingStatus === 'Charging'
216
239
  ? this.hap.Characteristic.ChargingState.CHARGING : this.hap.Characteristic.ChargingState.NOT_CHARGING;
217
240
  await this.debugLog(`ChargingState: ${this.Battery.ChargingState}`);
218
241
  }
@@ -246,7 +269,8 @@ export class RobotVacuumCleaner extends deviceBase {
246
269
  const serviceData = await this.monitorAdvertisementPackets(switchbot) as unknown as robotVacuumCleanerServiceData;
247
270
  // Update HomeKit
248
271
  if (serviceData.model === SwitchBotBLEModel.Unknown && serviceData.modelName === SwitchBotBLEModelName.Unknown) {
249
- await this.BLEparseStatus(serviceData);
272
+ this.serviceData = serviceData;
273
+ await this.BLEparseStatus();
250
274
  await this.updateHomeKitCharacteristics();
251
275
  } else {
252
276
  await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
@@ -264,7 +288,8 @@ export class RobotVacuumCleaner extends deviceBase {
264
288
  await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);;
265
289
  if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
266
290
  await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
267
- await this.openAPIparseStatus(deviceStatus.body);
291
+ this.deviceStatus = deviceStatus.body;
292
+ await this.openAPIparseStatus();
268
293
  await this.updateHomeKitCharacteristics();
269
294
  } else {
270
295
  await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
@@ -283,7 +308,8 @@ export class RobotVacuumCleaner extends deviceBase {
283
308
  | robotVacuumCleanerS1PlusWebhookContext | floorCleaningRobotS10WebhookContext) => {
284
309
  try {
285
310
  await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
286
- await this.parseStatusWebhook(context);
311
+ this.webhookContext = context;
312
+ await this.parseStatusWebhook();
287
313
  await this.updateHomeKitCharacteristics();
288
314
  } catch (e: any) {
289
315
  await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);