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

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