@switchbot/homebridge-switchbot 2.8.0-beta.9 → 2.8.1-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (116) hide show
  1. package/CHANGELOG.md +4 -2
  2. package/README.md +1 -1
  3. package/dist/device/blindtilt.d.ts +21 -22
  4. package/dist/device/blindtilt.d.ts.map +1 -1
  5. package/dist/device/blindtilt.js +78 -77
  6. package/dist/device/blindtilt.js.map +1 -1
  7. package/dist/device/bot.d.ts +11 -13
  8. package/dist/device/bot.d.ts.map +1 -1
  9. package/dist/device/bot.js +93 -75
  10. package/dist/device/bot.js.map +1 -1
  11. package/dist/device/ceilinglight.d.ts +10 -13
  12. package/dist/device/ceilinglight.d.ts.map +1 -1
  13. package/dist/device/ceilinglight.js +40 -27
  14. package/dist/device/ceilinglight.js.map +1 -1
  15. package/dist/device/colorbulb.d.ts +16 -23
  16. package/dist/device/colorbulb.d.ts.map +1 -1
  17. package/dist/device/colorbulb.js +55 -46
  18. package/dist/device/colorbulb.js.map +1 -1
  19. package/dist/device/contact.d.ts +16 -22
  20. package/dist/device/contact.d.ts.map +1 -1
  21. package/dist/device/contact.js +82 -73
  22. package/dist/device/contact.js.map +1 -1
  23. package/dist/device/curtain.d.ts +21 -24
  24. package/dist/device/curtain.d.ts.map +1 -1
  25. package/dist/device/curtain.js +76 -77
  26. package/dist/device/curtain.js.map +1 -1
  27. package/dist/device/hub.d.ts +10 -8
  28. package/dist/device/hub.d.ts.map +1 -1
  29. package/dist/device/hub.js +50 -35
  30. package/dist/device/hub.js.map +1 -1
  31. package/dist/device/humidifier.d.ts +8 -10
  32. package/dist/device/humidifier.d.ts.map +1 -1
  33. package/dist/device/humidifier.js +34 -21
  34. package/dist/device/humidifier.js.map +1 -1
  35. package/dist/device/iosensor.d.ts +16 -20
  36. package/dist/device/iosensor.d.ts.map +1 -1
  37. package/dist/device/iosensor.js +42 -29
  38. package/dist/device/iosensor.js.map +1 -1
  39. package/dist/device/lightstrip.d.ts +2 -5
  40. package/dist/device/lightstrip.d.ts.map +1 -1
  41. package/dist/device/lightstrip.js +9 -8
  42. package/dist/device/lightstrip.js.map +1 -1
  43. package/dist/device/lock.d.ts +15 -19
  44. package/dist/device/lock.d.ts.map +1 -1
  45. package/dist/device/lock.js +71 -28
  46. package/dist/device/lock.js.map +1 -1
  47. package/dist/device/meter.d.ts +5 -9
  48. package/dist/device/meter.d.ts.map +1 -1
  49. package/dist/device/meter.js +16 -20
  50. package/dist/device/meter.js.map +1 -1
  51. package/dist/device/meterplus.d.ts +6 -10
  52. package/dist/device/meterplus.d.ts.map +1 -1
  53. package/dist/device/meterplus.js +16 -20
  54. package/dist/device/meterplus.js.map +1 -1
  55. package/dist/device/motion.d.ts +6 -8
  56. package/dist/device/motion.d.ts.map +1 -1
  57. package/dist/device/motion.js +49 -47
  58. package/dist/device/motion.js.map +1 -1
  59. package/dist/device/plug.d.ts +2 -5
  60. package/dist/device/plug.d.ts.map +1 -1
  61. package/dist/device/plug.js +9 -8
  62. package/dist/device/plug.js.map +1 -1
  63. package/dist/device/robotvacuumcleaner.d.ts +6 -8
  64. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  65. package/dist/device/robotvacuumcleaner.js +20 -4
  66. package/dist/device/robotvacuumcleaner.js.map +1 -1
  67. package/dist/irdevice/airconditioner.d.ts +2 -0
  68. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  69. package/dist/irdevice/airconditioner.js +67 -86
  70. package/dist/irdevice/airconditioner.js.map +1 -1
  71. package/dist/irdevice/airpurifier.js.map +1 -1
  72. package/dist/irdevice/camera.js.map +1 -1
  73. package/dist/irdevice/fan.d.ts +0 -1
  74. package/dist/irdevice/fan.d.ts.map +1 -1
  75. package/dist/irdevice/fan.js.map +1 -1
  76. package/dist/irdevice/light.js.map +1 -1
  77. package/dist/irdevice/other.js.map +1 -1
  78. package/dist/irdevice/tv.d.ts +0 -1
  79. package/dist/irdevice/tv.d.ts.map +1 -1
  80. package/dist/irdevice/tv.js.map +1 -1
  81. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  82. package/dist/irdevice/waterheater.js.map +1 -1
  83. package/dist/platform.d.ts.map +1 -1
  84. package/dist/platform.js +64 -63
  85. package/dist/platform.js.map +1 -1
  86. package/dist/settings.d.ts +3 -1
  87. package/dist/settings.d.ts.map +1 -1
  88. package/dist/settings.js.map +1 -1
  89. package/package.json +12 -12
  90. package/src/device/blindtilt.ts +103 -100
  91. package/src/device/bot.ts +105 -89
  92. package/src/device/ceilinglight.ts +56 -46
  93. package/src/device/colorbulb.ts +78 -76
  94. package/src/device/contact.ts +100 -94
  95. package/src/device/curtain.ts +101 -101
  96. package/src/device/hub.ts +61 -43
  97. package/src/device/humidifier.ts +42 -33
  98. package/src/device/iosensor.ts +59 -50
  99. package/src/device/lightstrip.ts +14 -17
  100. package/src/device/lock.ts +91 -50
  101. package/src/device/meter.ts +22 -30
  102. package/src/device/meterplus.ts +23 -32
  103. package/src/device/motion.ts +55 -53
  104. package/src/device/plug.ts +12 -15
  105. package/src/device/robotvacuumcleaner.ts +33 -16
  106. package/src/irdevice/airconditioner.ts +80 -94
  107. package/src/irdevice/airpurifier.ts +1 -1
  108. package/src/irdevice/camera.ts +1 -1
  109. package/src/irdevice/fan.ts +1 -4
  110. package/src/irdevice/light.ts +1 -1
  111. package/src/irdevice/other.ts +1 -1
  112. package/src/irdevice/tv.ts +1 -4
  113. package/src/irdevice/vacuumcleaner.ts +1 -1
  114. package/src/irdevice/waterheater.ts +1 -1
  115. package/src/platform.ts +73 -73
  116. package/src/settings.ts +6 -2
@@ -6,28 +6,35 @@ import { interval, Subject } from 'rxjs';
6
6
  import { SwitchBotPlatform } from '../platform';
7
7
  import { debounceTime, skipWhile, take, tap } from 'rxjs/operators';
8
8
  import { Service, PlatformAccessory, CharacteristicValue } from 'homebridge';
9
- import { device, devicesConfig, deviceStatus, switchbot, ad, serviceData, Devices } from '../settings';
9
+ import { device, devicesConfig, deviceStatus, ad, Devices, serviceData } from '../settings';
10
10
 
11
11
  export class Lock {
12
12
  // Services
13
13
  lockService: Service;
14
+ batteryService: Service;
14
15
  contactSensorService?: Service;
15
16
 
16
17
  // Characteristic Values
17
- ContactSensorState!: CharacteristicValue;
18
- LockCurrentState!: CharacteristicValue;
18
+ BatteryLevel!: CharacteristicValue;
19
19
  LockTargetState!: CharacteristicValue;
20
+ LockCurrentState!: CharacteristicValue;
21
+ StatusLowBattery!: CharacteristicValue;
22
+ ContactSensorState!: CharacteristicValue;
23
+
24
+ // OpenAPI Status
25
+ OpenAPI_Version: deviceStatus['version'];
26
+ OpenAPI_Battery: deviceStatus['battery'];
27
+ OpenAPI_LockCurrentState!: deviceStatus['lockState'];
28
+ OpenAPI_ContactSensorState!: deviceStatus['doorState'];
20
29
 
21
- // OpenAPI Others
22
- doorState!: deviceStatus['doorState'];
23
- lockState!: deviceStatus['lockState'];
24
- deviceStatus!: any; //deviceStatusResponse;
30
+ // BLE Status
31
+ BLE_Battery: serviceData['battery'];
32
+ BLE_LockCurrentState: serviceData['state'];
33
+ BLE_Calibration: serviceData['calibration'];
34
+ BLE_ContactSensorState: serviceData['door_open'];
25
35
 
26
36
  // BLE Others
27
- connected?: boolean;
28
- switchbot!: switchbot;
29
- address!: ad['address'];
30
- serviceData!: serviceData;
37
+ BLE_IsConnected?: boolean;
31
38
 
32
39
  // Config
33
40
  scanDuration!: number;
@@ -41,15 +48,6 @@ export class Lock {
41
48
  // Connection
42
49
  private readonly BLE = this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI';
43
50
  private readonly OpenAPI = this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI';
44
- battery: any;
45
- calibration: any;
46
- status: any;
47
- update_from_secondary_lock: any;
48
- door_open: any;
49
- double_lock_mode: any;
50
- unclosed_alarm: any;
51
- unlocked_alarm: any;
52
- auto_lock_paused: any;
53
51
 
54
52
  constructor(
55
53
  private readonly platform: SwitchBotPlatform,
@@ -121,6 +119,16 @@ export class Lock {
121
119
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Contact Sensor Service Not Added`);
122
120
  }
123
121
 
122
+ // Battery Service
123
+ (this.batteryService = this.accessory.getService(this.platform.Service.Battery) || accessory.addService(this.platform.Service.Battery)),
124
+ `${accessory.displayName} Battery`;
125
+
126
+ this.batteryService.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Battery`);
127
+ if (!this.batteryService.testCharacteristic(this.platform.Characteristic.ConfiguredName)) {
128
+ this.batteryService.addCharacteristic(this.platform.Characteristic.ConfiguredName, `${accessory.displayName} Battery`);
129
+ }
130
+ this.batteryService.setCharacteristic(this.platform.Characteristic.ChargingState, this.platform.Characteristic.ChargingState.NOT_CHARGEABLE);
131
+
124
132
  // Update Homekit
125
133
  this.updateHomeKitCharacteristics();
126
134
 
@@ -174,7 +182,7 @@ export class Lock {
174
182
 
175
183
  async BLEparseStatus(): Promise<void> {
176
184
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
177
- switch (this.lockState) {
185
+ switch (this.BLE_LockCurrentState) {
178
186
  case 'locked':
179
187
  this.LockCurrentState = this.platform.Characteristic.LockCurrentState.SECURED;
180
188
  this.LockTargetState = this.platform.Characteristic.LockTargetState.SECURED;
@@ -183,7 +191,7 @@ export class Lock {
183
191
  this.LockCurrentState = this.platform.Characteristic.LockCurrentState.UNSECURED;
184
192
  this.LockTargetState = this.platform.Characteristic.LockTargetState.UNSECURED;
185
193
  }
186
- switch (this.doorState) {
194
+ switch (this.BLE_ContactSensorState) {
187
195
  case 'opened':
188
196
  this.ContactSensorState = this.platform.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED;
189
197
  break;
@@ -191,11 +199,21 @@ export class Lock {
191
199
  this.ContactSensorState = this.platform.Characteristic.ContactSensorState.CONTACT_DETECTED;
192
200
  }
193
201
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LockTargetState}`);
202
+ // Battery
203
+ this.BatteryLevel = Number(this.BLE_Battery);
204
+ if (this.BatteryLevel < 10) {
205
+ this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
206
+ } else {
207
+ this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
208
+ }
209
+ this.debugLog(
210
+ `${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel},` + ` StatusLowBattery: ${this.StatusLowBattery}`,
211
+ );
194
212
  }
195
213
 
196
214
  async openAPIparseStatus(): Promise<void> {
197
215
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
198
- switch (this.lockState) {
216
+ switch (this.OpenAPI_LockCurrentState) {
199
217
  case 'locked':
200
218
  this.LockCurrentState = this.platform.Characteristic.LockCurrentState.SECURED;
201
219
  this.LockTargetState = this.platform.Characteristic.LockTargetState.SECURED;
@@ -204,7 +222,7 @@ export class Lock {
204
222
  this.LockCurrentState = this.platform.Characteristic.LockCurrentState.UNSECURED;
205
223
  this.LockTargetState = this.platform.Characteristic.LockTargetState.UNSECURED;
206
224
  }
207
- switch (this.doorState) {
225
+ switch (this.OpenAPI_ContactSensorState) {
208
226
  case 'opened':
209
227
  this.ContactSensorState = this.platform.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED;
210
228
  break;
@@ -212,6 +230,18 @@ export class Lock {
212
230
  this.ContactSensorState = this.platform.Characteristic.ContactSensorState.CONTACT_DETECTED;
213
231
  }
214
232
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LockTargetState}`);
233
+
234
+ // Battery
235
+ this.BatteryLevel = Number(this.OpenAPI_Battery);
236
+ if (this.BatteryLevel < 10) {
237
+ this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
238
+ } else {
239
+ this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
240
+ }
241
+ if (Number.isNaN(this.BatteryLevel)) {
242
+ this.BatteryLevel = 100;
243
+ }
244
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
215
245
  }
216
246
 
217
247
  /**
@@ -252,22 +282,15 @@ export class Lock {
252
282
  })
253
283
  .then(async () => {
254
284
  // Set an event hander
255
- switchbot.onadvertisement = async (ad: any) => {
256
- this.address = ad.address;
285
+ switchbot.onadvertisement = async (ad: ad) => {
257
286
  this.debugLog(
258
287
  `${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac},` +
259
- ` BLE Address Found: ${this.address}`,
288
+ ` BLE Address Found: ${ad.address}`,
260
289
  );
261
- this.serviceData = ad.serviceData;
262
- this.battery = ad.serviceData.battery;
263
- this.calibration = ad.serviceData.calibration;
264
- this.status = ad.serviceData.status;
265
- this.update_from_secondary_lock = ad.serviceData.update_from_secondary_lock;
266
- this.door_open = ad.serviceData.door_open;
267
- this.double_lock_mode = ad.serviceData.double_lock_mode;
268
- this.unclosed_alarm = ad.serviceData.unclosed_alarm;
269
- this.unlocked_alarm = ad.serviceData.unlocked_alarm;
270
- this.auto_lock_paused = ad.serviceData.auto_lock_paused;
290
+ this.BLE_Battery = ad.serviceData.battery;
291
+ this.BLE_Calibration = ad.serviceData.calibration;
292
+ this.BLE_LockCurrentState = ad.serviceData.status;
293
+ this.BLE_ContactSensorState = ad.serviceData.door_open;
271
294
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
272
295
  this.debugLog(
273
296
  `${this.device.deviceType}: ${this.accessory.displayName} battery: ${ad.serviceData.battery}, ` +
@@ -275,13 +298,13 @@ export class Lock {
275
298
  `door_open: ${ad.serviceData.door_open}`,
276
299
  );
277
300
 
278
- if (this.serviceData) {
279
- this.connected = true;
280
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.connected}`);
301
+ if (ad.serviceData) {
302
+ this.BLE_IsConnected = true;
303
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.BLE_IsConnected}`);
281
304
  await this.stopScanning(switchbot);
282
305
  } else {
283
- this.connected = false;
284
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.connected}`);
306
+ this.BLE_IsConnected = false;
307
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.BLE_IsConnected}`);
285
308
  }
286
309
  };
287
310
  // Wait
@@ -310,13 +333,15 @@ export class Lock {
310
333
  const { body, statusCode, headers } = await request(`${Devices}/${this.device.deviceId}/status`, {
311
334
  headers: this.platform.generateHeaders(),
312
335
  });
313
- const deviceStatus = await body.json();
336
+ const deviceStatus: any = await body.json();
314
337
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Devices: ${JSON.stringify(deviceStatus.body)}`);
315
338
  this.statusCode(statusCode);
316
339
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Headers: ${JSON.stringify(headers)}`);
317
340
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} refreshStatus: ${JSON.stringify(deviceStatus)}`);
318
- this.lockState = deviceStatus.body.lockState;
319
- this.doorState = deviceStatus.body.doorState;
341
+ this.OpenAPI_LockCurrentState = deviceStatus.body.lockState;
342
+ this.OpenAPI_ContactSensorState = deviceStatus.body.doorState;
343
+ this.OpenAPI_Battery = deviceStatus.body.battery;
344
+ this.OpenAPI_Version = deviceStatus.body.version;
320
345
  this.openAPIparseStatus();
321
346
  this.updateHomeKitCharacteristics();
322
347
  } catch (e: any) {
@@ -417,7 +442,7 @@ export class Lock {
417
442
  method: 'POST',
418
443
  headers: this.platform.generateHeaders(),
419
444
  });
420
- const deviceStatus = await body.json();
445
+ const deviceStatus: any = await body.json();
421
446
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Devices: ${JSON.stringify(deviceStatus.body)}`);
422
447
  this.statusCode(statusCode);
423
448
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Headers: ${JSON.stringify(headers)}`);
@@ -475,11 +500,25 @@ export class Lock {
475
500
  this.lockService.updateCharacteristic(this.platform.Characteristic.LockCurrentState, this.LockCurrentState);
476
501
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic LockCurrentState: ${this.LockCurrentState}`);
477
502
  }
503
+ if (this.BatteryLevel === undefined) {
504
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
505
+ } else {
506
+ this.accessory.context.BatteryLevel = this.BatteryLevel;
507
+ this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, this.BatteryLevel);
508
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
509
+ }
510
+ if (this.StatusLowBattery === undefined) {
511
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
512
+ } else {
513
+ this.accessory.context.StatusLowBattery = this.StatusLowBattery;
514
+ this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.StatusLowBattery);
515
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
516
+ }
478
517
  }
479
518
 
480
519
  async stopScanning(switchbot: any) {
481
520
  await switchbot.stopScan();
482
- if (this.connected) {
521
+ if (this.BLE_IsConnected) {
483
522
  await this.BLEparseStatus();
484
523
  await this.updateHomeKitCharacteristics();
485
524
  } else {
@@ -598,10 +637,12 @@ export class Lock {
598
637
  );
599
638
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} device.firmware: ${device.firmware}`);
600
639
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} this.platform.version: ${this.platform.version}`);
601
- if (accessory.context.FirmwareRevision) {
602
- FirmwareRevision = accessory.context.FirmwareRevision;
603
- } else if (device.firmware) {
640
+ if (device.firmware) {
604
641
  FirmwareRevision = device.firmware;
642
+ } else if (device.version) {
643
+ FirmwareRevision = JSON.stringify(device.version);
644
+ } else if (accessory.context.FirmwareRevision) {
645
+ FirmwareRevision = accessory.context.FirmwareRevision;
605
646
  } else {
606
647
  FirmwareRevision = this.platform.version;
607
648
  }
@@ -7,7 +7,7 @@ import { skipWhile } from 'rxjs/operators';
7
7
  import { request } from 'undici';
8
8
  import { Context } from 'vm';
9
9
  import { SwitchBotPlatform } from '../platform';
10
- import { Devices, ad, device, deviceStatus, devicesConfig, serviceData, switchbot, temperature } from '../settings';
10
+ import { Devices, ad, device, deviceStatus, devicesConfig, serviceData, temperature } from '../settings';
11
11
  import { sleep } from '../utils';
12
12
 
13
13
  /**
@@ -17,28 +17,24 @@ import { sleep } from '../utils';
17
17
  */
18
18
  export class Meter {
19
19
  // Services
20
- batteryService?: Service;
20
+ batteryService: Service;
21
21
  temperatureservice?: Service;
22
22
  humidityservice?: Service;
23
23
 
24
24
  // Characteristic Values
25
25
  CurrentRelativeHumidity!: CharacteristicValue;
26
26
  CurrentTemperature?: CharacteristicValue;
27
- BatteryLevel?: CharacteristicValue;
28
- ChargingState?: CharacteristicValue;
29
- StatusLowBattery?: CharacteristicValue;
27
+ BatteryLevel!: CharacteristicValue;
28
+ StatusLowBattery!: CharacteristicValue;
30
29
 
31
30
  // OpenAPI Others
31
+ Version: deviceStatus['version'];
32
32
  Battery: deviceStatus['battery'];
33
33
  Temperature: deviceStatus['temperature'];
34
34
  Humidity: deviceStatus['humidity'];
35
- deviceStatus!: any; //deviceStatusResponse;
36
35
 
37
36
  // BLE Others
38
37
  connected?: boolean;
39
- switchbot!: switchbot;
40
- serviceData!: serviceData;
41
- address!: ad['address'];
42
38
  temperature!: serviceData['temperature'];
43
39
  celsius!: temperature['c'];
44
40
  fahrenheit!: temperature['f'];
@@ -154,19 +150,14 @@ export class Meter {
154
150
  }
155
151
 
156
152
  // Battery Service
157
- if (!this.batteryService) {
158
- this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Battery Service`);
159
- (this.batteryService = this.accessory.getService(this.platform.Service.Battery) || this.accessory.addService(this.platform.Service.Battery)),
160
- `${accessory.displayName} Battery`;
161
-
162
- this.batteryService.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Battery`);
163
- if (!this.batteryService.testCharacteristic(this.platform.Characteristic.ConfiguredName)) {
164
- this.batteryService.addCharacteristic(this.platform.Characteristic.ConfiguredName, `${accessory.displayName} Battery`);
165
- }
166
- this.batteryService.setCharacteristic(this.platform.Characteristic.ChargingState, this.platform.Characteristic.ChargingState.NOT_CHARGEABLE);
167
- } else {
168
- this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Battery Service Not Added`);
153
+ (this.batteryService = this.accessory.getService(this.platform.Service.Battery) || accessory.addService(this.platform.Service.Battery)),
154
+ `${accessory.displayName} Battery`;
155
+
156
+ this.batteryService.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Battery`);
157
+ if (!this.batteryService.testCharacteristic(this.platform.Characteristic.ConfiguredName)) {
158
+ this.batteryService.addCharacteristic(this.platform.Characteristic.ConfiguredName, `${accessory.displayName} Battery`);
169
159
  }
160
+ this.batteryService.setCharacteristic(this.platform.Characteristic.ChargingState, this.platform.Characteristic.ChargingState.NOT_CHARGEABLE);
170
161
 
171
162
  // Retrieve initial values and updateHomekit
172
163
  this.updateHomeKitCharacteristics();
@@ -287,16 +278,14 @@ export class Meter {
287
278
  .then(async () => {
288
279
  // Set an event hander
289
280
  switchbot.onadvertisement = async (ad: ad) => {
290
- this.address = ad.address;
291
281
  this.debugLog(
292
282
  `${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac},` +
293
- ` BLE Address Found: ${this.address}`,
283
+ ` BLE Address Found: ${ad.address}`,
294
284
  );
295
285
  if (ad.serviceData.humidity! > 0) {
296
286
  // reject unreliable data
297
287
  this.humidity = ad.serviceData.humidity;
298
288
  }
299
- this.serviceData = ad.serviceData;
300
289
  this.temperature = ad.serviceData.temperature;
301
290
  this.celsius = ad.serviceData.temperature!.c;
302
291
  this.fahrenheit = ad.serviceData.temperature!.f;
@@ -308,7 +297,7 @@ export class Meter {
308
297
  `battery: ${ad.serviceData.battery}`,
309
298
  );
310
299
 
311
- if (this.serviceData) {
300
+ if (ad.serviceData) {
312
301
  this.connected = true;
313
302
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.connected}`);
314
303
  await this.stopScanning(switchbot);
@@ -343,14 +332,15 @@ export class Meter {
343
332
  const { body, statusCode, headers } = await request(`${Devices}/${this.device.deviceId}/status`, {
344
333
  headers: this.platform.generateHeaders(),
345
334
  });
346
- const deviceStatus = await body.json();
335
+ const deviceStatus: any = await body.json();
347
336
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Devices: ${JSON.stringify(deviceStatus.body)}`);
348
337
  this.statusCode(statusCode);
349
338
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Headers: ${JSON.stringify(headers)}`);
350
339
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} refreshStatus: ${JSON.stringify(deviceStatus)}`);
351
340
  this.Humidity = deviceStatus.body.humidity!;
352
341
  this.Temperature = deviceStatus.body.temperature!;
353
- this.Battery = deviceStatus.body.battery!;
342
+ this.Battery = deviceStatus.body.battery;
343
+ this.Version = deviceStatus.body.version;
354
344
  this.openAPIparseStatus();
355
345
  this.updateHomeKitCharacteristics();
356
346
  } catch (e: any) {
@@ -600,10 +590,12 @@ export class Meter {
600
590
  );
601
591
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} device.firmware: ${device.firmware}`);
602
592
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} this.platform.version: ${this.platform.version}`);
603
- if (accessory.context.FirmwareRevision) {
604
- FirmwareRevision = accessory.context.FirmwareRevision;
605
- } else if (device.firmware) {
593
+ if (device.firmware) {
606
594
  FirmwareRevision = device.firmware;
595
+ } else if (device.version) {
596
+ FirmwareRevision = JSON.stringify(device.version);
597
+ } else if (accessory.context.FirmwareRevision) {
598
+ FirmwareRevision = accessory.context.FirmwareRevision;
607
599
  } else {
608
600
  FirmwareRevision = this.platform.version;
609
601
  }
@@ -7,7 +7,7 @@ import { skipWhile } from 'rxjs/operators';
7
7
  import { request } from 'undici';
8
8
  import { Context } from 'vm';
9
9
  import { SwitchBotPlatform } from '../platform';
10
- import { Devices, ad, device, deviceStatus, devicesConfig, serviceData, switchbot, temperature } from '../settings';
10
+ import { Devices, ad, device, deviceStatus, devicesConfig, serviceData, temperature } from '../settings';
11
11
  import { sleep } from '../utils';
12
12
 
13
13
  /**
@@ -17,28 +17,24 @@ import { sleep } from '../utils';
17
17
  */
18
18
  export class MeterPlus {
19
19
  // Services
20
- batteryService?: Service;
21
- temperatureservice?: Service;
20
+ batteryService: Service;
22
21
  humidityservice?: Service;
22
+ temperatureservice?: Service;
23
23
 
24
24
  // Characteristic Values
25
25
  CurrentRelativeHumidity!: CharacteristicValue;
26
26
  CurrentTemperature?: CharacteristicValue;
27
- BatteryLevel?: CharacteristicValue;
28
- ChargingState?: CharacteristicValue;
29
- StatusLowBattery?: CharacteristicValue;
27
+ BatteryLevel!: CharacteristicValue;
28
+ StatusLowBattery!: CharacteristicValue;
30
29
 
31
30
  // OpenAPI Others
31
+ Version: deviceStatus['version'];
32
32
  Battery: deviceStatus['battery'];
33
33
  Temperature: deviceStatus['temperature'];
34
34
  Humidity: deviceStatus['humidity'];
35
- deviceStatus!: any; //deviceStatusResponse;
36
35
 
37
36
  // BLE Others
38
37
  connected?: boolean;
39
- switchbot!: switchbot;
40
- serviceData!: serviceData;
41
- address!: ad['address'];
42
38
  temperature!: serviceData['temperature'];
43
39
  celsius!: temperature['c'];
44
40
  fahrenheit!: temperature['f'];
@@ -154,19 +150,14 @@ export class MeterPlus {
154
150
  }
155
151
 
156
152
  // Battery Service
157
- if (!this.batteryService) {
158
- this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Battery Service`);
159
- (this.batteryService = this.accessory.getService(this.platform.Service.Battery) || this.accessory.addService(this.platform.Service.Battery)),
160
- `${accessory.displayName} Battery`;
161
-
162
- this.batteryService.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Battery`);
163
- if (!this.batteryService.testCharacteristic(this.platform.Characteristic.ConfiguredName)) {
164
- this.batteryService.addCharacteristic(this.platform.Characteristic.ConfiguredName, `${accessory.displayName} Battery`);
165
- }
166
- this.batteryService.setCharacteristic(this.platform.Characteristic.ChargingState, this.platform.Characteristic.ChargingState.NOT_CHARGEABLE);
167
- } else {
168
- this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Battery Service Not Added`);
153
+ (this.batteryService = this.accessory.getService(this.platform.Service.Battery) || accessory.addService(this.platform.Service.Battery)),
154
+ `${accessory.displayName} Battery`;
155
+
156
+ this.batteryService.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Battery`);
157
+ if (!this.batteryService.testCharacteristic(this.platform.Characteristic.ConfiguredName)) {
158
+ this.batteryService.addCharacteristic(this.platform.Characteristic.ConfiguredName, `${accessory.displayName} Battery`);
169
159
  }
160
+ this.batteryService.setCharacteristic(this.platform.Characteristic.ChargingState, this.platform.Characteristic.ChargingState.NOT_CHARGEABLE);
170
161
 
171
162
  // Retrieve initial values and updateHomekit
172
163
  this.updateHomeKitCharacteristics();
@@ -287,16 +278,14 @@ export class MeterPlus {
287
278
  .then(async () => {
288
279
  // Set an event hander
289
280
  switchbot.onadvertisement = async (ad: ad) => {
290
- this.address = ad.address;
291
281
  this.debugLog(
292
282
  `${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac},` +
293
- ` BLE Address Found: ${this.address}`,
283
+ ` BLE Address Found: ${ad.address}`,
294
284
  );
295
285
  if (ad.serviceData.humidity! > 0) {
296
286
  // reject unreliable data
297
287
  this.humidity = ad.serviceData.humidity;
298
288
  }
299
- this.serviceData = ad.serviceData;
300
289
  this.temperature = ad.serviceData.temperature;
301
290
  this.celsius = ad.serviceData.temperature!.c;
302
291
  this.fahrenheit = ad.serviceData.temperature!.f;
@@ -308,7 +297,7 @@ export class MeterPlus {
308
297
  `battery: ${ad.serviceData.battery}`,
309
298
  );
310
299
 
311
- if (this.serviceData) {
300
+ if (ad.serviceData) {
312
301
  this.connected = true;
313
302
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.connected}`);
314
303
  await this.stopScanning(switchbot);
@@ -343,14 +332,15 @@ export class MeterPlus {
343
332
  const { body, statusCode, headers } = await request(`${Devices}/${this.device.deviceId}/status`, {
344
333
  headers: this.platform.generateHeaders(),
345
334
  });
346
- const deviceStatus = await body.json();
335
+ const deviceStatus: any = await body.json();
347
336
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Devices: ${JSON.stringify(deviceStatus.body)}`);
348
337
  this.statusCode(statusCode);
349
338
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Headers: ${JSON.stringify(headers)}`);
350
339
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} refreshStatus: ${JSON.stringify(deviceStatus)}`);
351
340
  this.Humidity = deviceStatus.body.humidity!;
352
341
  this.Temperature = deviceStatus.body.temperature!;
353
- this.Battery = deviceStatus.body.battery!;
342
+ this.Battery = deviceStatus.body.battery;
343
+ this.Version = deviceStatus.body.version;
354
344
  this.openAPIparseStatus();
355
345
  this.updateHomeKitCharacteristics();
356
346
  } catch (e: any) {
@@ -423,7 +413,6 @@ export class MeterPlus {
423
413
  this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.StatusLowBattery);
424
414
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
425
415
  }
426
-
427
416
  if (this.device.mqttURL) {
428
417
  this.mqttPublish(`{${mqttmessage.join(',')}}`);
429
418
  }
@@ -611,10 +600,12 @@ export class MeterPlus {
611
600
  );
612
601
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} device.firmware: ${device.firmware}`);
613
602
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} this.platform.version: ${this.platform.version}`);
614
- if (accessory.context.FirmwareRevision) {
615
- FirmwareRevision = accessory.context.FirmwareRevision;
616
- } else if (device.firmware) {
603
+ if (device.firmware) {
617
604
  FirmwareRevision = device.firmware;
605
+ } else if (device.version) {
606
+ FirmwareRevision = JSON.stringify(device.version);
607
+ } else if (accessory.context.FirmwareRevision) {
608
+ FirmwareRevision = accessory.context.FirmwareRevision;
618
609
  } else {
619
610
  FirmwareRevision = this.platform.version;
620
611
  }