@switchbot/homebridge-switchbot 2.8.0 → 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 (94) hide show
  1. package/.github/npm-version-script.js +81 -0
  2. package/dist/device/blindtilt.d.ts +21 -23
  3. package/dist/device/blindtilt.d.ts.map +1 -1
  4. package/dist/device/blindtilt.js +71 -73
  5. package/dist/device/blindtilt.js.map +1 -1
  6. package/dist/device/bot.d.ts +11 -15
  7. package/dist/device/bot.d.ts.map +1 -1
  8. package/dist/device/bot.js +38 -34
  9. package/dist/device/bot.js.map +1 -1
  10. package/dist/device/ceilinglight.d.ts +10 -14
  11. package/dist/device/ceilinglight.d.ts.map +1 -1
  12. package/dist/device/ceilinglight.js +33 -23
  13. package/dist/device/ceilinglight.js.map +1 -1
  14. package/dist/device/colorbulb.d.ts +16 -24
  15. package/dist/device/colorbulb.d.ts.map +1 -1
  16. package/dist/device/colorbulb.js +49 -43
  17. package/dist/device/colorbulb.js.map +1 -1
  18. package/dist/device/contact.d.ts +16 -24
  19. package/dist/device/contact.d.ts.map +1 -1
  20. package/dist/device/contact.js +49 -51
  21. package/dist/device/contact.js.map +1 -1
  22. package/dist/device/curtain.d.ts +21 -26
  23. package/dist/device/curtain.d.ts.map +1 -1
  24. package/dist/device/curtain.js +49 -47
  25. package/dist/device/curtain.js.map +1 -1
  26. package/dist/device/hub.d.ts +9 -8
  27. package/dist/device/hub.d.ts.map +1 -1
  28. package/dist/device/hub.js +42 -30
  29. package/dist/device/hub.js.map +1 -1
  30. package/dist/device/humidifier.d.ts +8 -10
  31. package/dist/device/humidifier.d.ts.map +1 -1
  32. package/dist/device/humidifier.js +27 -17
  33. package/dist/device/humidifier.js.map +1 -1
  34. package/dist/device/iosensor.d.ts +16 -21
  35. package/dist/device/iosensor.d.ts.map +1 -1
  36. package/dist/device/iosensor.js +35 -26
  37. package/dist/device/iosensor.js.map +1 -1
  38. package/dist/device/lightstrip.d.ts +1 -5
  39. package/dist/device/lightstrip.d.ts.map +1 -1
  40. package/dist/device/lightstrip.js +2 -4
  41. package/dist/device/lightstrip.js.map +1 -1
  42. package/dist/device/lock.d.ts +15 -21
  43. package/dist/device/lock.d.ts.map +1 -1
  44. package/dist/device/lock.js +64 -26
  45. package/dist/device/lock.js.map +1 -1
  46. package/dist/device/meter.d.ts +4 -9
  47. package/dist/device/meter.d.ts.map +1 -1
  48. package/dist/device/meter.js +8 -16
  49. package/dist/device/meter.js.map +1 -1
  50. package/dist/device/meterplus.d.ts +5 -10
  51. package/dist/device/meterplus.d.ts.map +1 -1
  52. package/dist/device/meterplus.js +8 -16
  53. package/dist/device/meterplus.js.map +1 -1
  54. package/dist/device/motion.d.ts +4 -8
  55. package/dist/device/motion.d.ts.map +1 -1
  56. package/dist/device/motion.js +10 -23
  57. package/dist/device/motion.js.map +1 -1
  58. package/dist/device/plug.d.ts +1 -5
  59. package/dist/device/plug.d.ts.map +1 -1
  60. package/dist/device/plug.js +2 -4
  61. package/dist/device/plug.js.map +1 -1
  62. package/dist/device/robotvacuumcleaner.d.ts +4 -8
  63. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  64. package/dist/device/robotvacuumcleaner.js +7 -16
  65. package/dist/device/robotvacuumcleaner.js.map +1 -1
  66. package/dist/irdevice/fan.d.ts +0 -1
  67. package/dist/irdevice/fan.d.ts.map +1 -1
  68. package/dist/irdevice/fan.js.map +1 -1
  69. package/dist/irdevice/tv.d.ts +0 -1
  70. package/dist/irdevice/tv.d.ts.map +1 -1
  71. package/dist/irdevice/tv.js.map +1 -1
  72. package/dist/settings.d.ts +3 -1
  73. package/dist/settings.d.ts.map +1 -1
  74. package/dist/settings.js.map +1 -1
  75. package/package.json +2 -2
  76. package/src/device/blindtilt.ts +96 -95
  77. package/src/device/bot.ts +54 -54
  78. package/src/device/ceilinglight.ts +46 -39
  79. package/src/device/colorbulb.ts +68 -69
  80. package/src/device/contact.ts +69 -77
  81. package/src/device/curtain.ts +75 -74
  82. package/src/device/hub.ts +53 -38
  83. package/src/device/humidifier.ts +33 -26
  84. package/src/device/iosensor.ts +53 -48
  85. package/src/device/lightstrip.ts +4 -10
  86. package/src/device/lock.ts +84 -49
  87. package/src/device/meter.ts +13 -25
  88. package/src/device/meterplus.ts +14 -27
  89. package/src/device/motion.ts +16 -32
  90. package/src/device/plug.ts +4 -10
  91. package/src/device/robotvacuumcleaner.ts +13 -25
  92. package/src/irdevice/fan.ts +0 -3
  93. package/src/irdevice/tv.ts +0 -3
  94. package/src/settings.ts +5 -1
@@ -6,30 +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
- Version: deviceStatus['version'];
23
- Battery: deviceStatus['battery'];
24
- doorState!: deviceStatus['doorState'];
25
- lockState!: deviceStatus['lockState'];
26
- 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'];
27
35
 
28
36
  // BLE Others
29
- connected?: boolean;
30
- switchbot!: switchbot;
31
- address!: ad['address'];
32
- serviceData!: serviceData;
37
+ BLE_IsConnected?: boolean;
33
38
 
34
39
  // Config
35
40
  scanDuration!: number;
@@ -43,15 +48,6 @@ export class Lock {
43
48
  // Connection
44
49
  private readonly BLE = this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI';
45
50
  private readonly OpenAPI = this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI';
46
- battery: any;
47
- calibration: any;
48
- status: any;
49
- update_from_secondary_lock: any;
50
- door_open: any;
51
- double_lock_mode: any;
52
- unclosed_alarm: any;
53
- unlocked_alarm: any;
54
- auto_lock_paused: any;
55
51
 
56
52
  constructor(
57
53
  private readonly platform: SwitchBotPlatform,
@@ -123,6 +119,16 @@ export class Lock {
123
119
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Contact Sensor Service Not Added`);
124
120
  }
125
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
+
126
132
  // Update Homekit
127
133
  this.updateHomeKitCharacteristics();
128
134
 
@@ -176,7 +182,7 @@ export class Lock {
176
182
 
177
183
  async BLEparseStatus(): Promise<void> {
178
184
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
179
- switch (this.lockState) {
185
+ switch (this.BLE_LockCurrentState) {
180
186
  case 'locked':
181
187
  this.LockCurrentState = this.platform.Characteristic.LockCurrentState.SECURED;
182
188
  this.LockTargetState = this.platform.Characteristic.LockTargetState.SECURED;
@@ -185,7 +191,7 @@ export class Lock {
185
191
  this.LockCurrentState = this.platform.Characteristic.LockCurrentState.UNSECURED;
186
192
  this.LockTargetState = this.platform.Characteristic.LockTargetState.UNSECURED;
187
193
  }
188
- switch (this.doorState) {
194
+ switch (this.BLE_ContactSensorState) {
189
195
  case 'opened':
190
196
  this.ContactSensorState = this.platform.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED;
191
197
  break;
@@ -193,11 +199,21 @@ export class Lock {
193
199
  this.ContactSensorState = this.platform.Characteristic.ContactSensorState.CONTACT_DETECTED;
194
200
  }
195
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
+ );
196
212
  }
197
213
 
198
214
  async openAPIparseStatus(): Promise<void> {
199
215
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
200
- switch (this.lockState) {
216
+ switch (this.OpenAPI_LockCurrentState) {
201
217
  case 'locked':
202
218
  this.LockCurrentState = this.platform.Characteristic.LockCurrentState.SECURED;
203
219
  this.LockTargetState = this.platform.Characteristic.LockTargetState.SECURED;
@@ -206,7 +222,7 @@ export class Lock {
206
222
  this.LockCurrentState = this.platform.Characteristic.LockCurrentState.UNSECURED;
207
223
  this.LockTargetState = this.platform.Characteristic.LockTargetState.UNSECURED;
208
224
  }
209
- switch (this.doorState) {
225
+ switch (this.OpenAPI_ContactSensorState) {
210
226
  case 'opened':
211
227
  this.ContactSensorState = this.platform.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED;
212
228
  break;
@@ -214,6 +230,18 @@ export class Lock {
214
230
  this.ContactSensorState = this.platform.Characteristic.ContactSensorState.CONTACT_DETECTED;
215
231
  }
216
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}`);
217
245
  }
218
246
 
219
247
  /**
@@ -254,22 +282,15 @@ export class Lock {
254
282
  })
255
283
  .then(async () => {
256
284
  // Set an event hander
257
- switchbot.onadvertisement = async (ad: any) => {
258
- this.address = ad.address;
285
+ switchbot.onadvertisement = async (ad: ad) => {
259
286
  this.debugLog(
260
287
  `${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac},` +
261
- ` BLE Address Found: ${this.address}`,
288
+ ` BLE Address Found: ${ad.address}`,
262
289
  );
263
- this.serviceData = ad.serviceData;
264
- this.battery = ad.serviceData.battery;
265
- this.calibration = ad.serviceData.calibration;
266
- this.status = ad.serviceData.status;
267
- this.update_from_secondary_lock = ad.serviceData.update_from_secondary_lock;
268
- this.door_open = ad.serviceData.door_open;
269
- this.double_lock_mode = ad.serviceData.double_lock_mode;
270
- this.unclosed_alarm = ad.serviceData.unclosed_alarm;
271
- this.unlocked_alarm = ad.serviceData.unlocked_alarm;
272
- 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;
273
294
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
274
295
  this.debugLog(
275
296
  `${this.device.deviceType}: ${this.accessory.displayName} battery: ${ad.serviceData.battery}, ` +
@@ -277,13 +298,13 @@ export class Lock {
277
298
  `door_open: ${ad.serviceData.door_open}`,
278
299
  );
279
300
 
280
- if (this.serviceData) {
281
- this.connected = true;
282
- 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}`);
283
304
  await this.stopScanning(switchbot);
284
305
  } else {
285
- this.connected = false;
286
- 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}`);
287
308
  }
288
309
  };
289
310
  // Wait
@@ -317,10 +338,10 @@ export class Lock {
317
338
  this.statusCode(statusCode);
318
339
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Headers: ${JSON.stringify(headers)}`);
319
340
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} refreshStatus: ${JSON.stringify(deviceStatus)}`);
320
- this.lockState = deviceStatus.body.lockState;
321
- this.doorState = deviceStatus.body.doorState;
322
- this.Battery = deviceStatus.body.battery;
323
- this.Version = deviceStatus.body.version;
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;
324
345
  this.openAPIparseStatus();
325
346
  this.updateHomeKitCharacteristics();
326
347
  } catch (e: any) {
@@ -479,11 +500,25 @@ export class Lock {
479
500
  this.lockService.updateCharacteristic(this.platform.Characteristic.LockCurrentState, this.LockCurrentState);
480
501
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic LockCurrentState: ${this.LockCurrentState}`);
481
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
+ }
482
517
  }
483
518
 
484
519
  async stopScanning(switchbot: any) {
485
520
  await switchbot.stopScan();
486
- if (this.connected) {
521
+ if (this.BLE_IsConnected) {
487
522
  await this.BLEparseStatus();
488
523
  await this.updateHomeKitCharacteristics();
489
524
  } else {
@@ -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,29 +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
32
31
  Version: deviceStatus['version'];
33
32
  Battery: deviceStatus['battery'];
34
33
  Temperature: deviceStatus['temperature'];
35
34
  Humidity: deviceStatus['humidity'];
36
- deviceStatus!: any; //deviceStatusResponse;
37
35
 
38
36
  // BLE Others
39
37
  connected?: boolean;
40
- switchbot!: switchbot;
41
- serviceData!: serviceData;
42
- address!: ad['address'];
43
38
  temperature!: serviceData['temperature'];
44
39
  celsius!: temperature['c'];
45
40
  fahrenheit!: temperature['f'];
@@ -155,19 +150,14 @@ export class Meter {
155
150
  }
156
151
 
157
152
  // Battery Service
158
- if (!this.batteryService) {
159
- this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Battery Service`);
160
- (this.batteryService = this.accessory.getService(this.platform.Service.Battery) || this.accessory.addService(this.platform.Service.Battery)),
161
- `${accessory.displayName} Battery`;
162
-
163
- this.batteryService.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Battery`);
164
- if (!this.batteryService.testCharacteristic(this.platform.Characteristic.ConfiguredName)) {
165
- this.batteryService.addCharacteristic(this.platform.Characteristic.ConfiguredName, `${accessory.displayName} Battery`);
166
- }
167
- this.batteryService.setCharacteristic(this.platform.Characteristic.ChargingState, this.platform.Characteristic.ChargingState.NOT_CHARGEABLE);
168
- } else {
169
- 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`);
170
159
  }
160
+ this.batteryService.setCharacteristic(this.platform.Characteristic.ChargingState, this.platform.Characteristic.ChargingState.NOT_CHARGEABLE);
171
161
 
172
162
  // Retrieve initial values and updateHomekit
173
163
  this.updateHomeKitCharacteristics();
@@ -288,16 +278,14 @@ export class Meter {
288
278
  .then(async () => {
289
279
  // Set an event hander
290
280
  switchbot.onadvertisement = async (ad: ad) => {
291
- this.address = ad.address;
292
281
  this.debugLog(
293
282
  `${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac},` +
294
- ` BLE Address Found: ${this.address}`,
283
+ ` BLE Address Found: ${ad.address}`,
295
284
  );
296
285
  if (ad.serviceData.humidity! > 0) {
297
286
  // reject unreliable data
298
287
  this.humidity = ad.serviceData.humidity;
299
288
  }
300
- this.serviceData = ad.serviceData;
301
289
  this.temperature = ad.serviceData.temperature;
302
290
  this.celsius = ad.serviceData.temperature!.c;
303
291
  this.fahrenheit = ad.serviceData.temperature!.f;
@@ -309,7 +297,7 @@ export class Meter {
309
297
  `battery: ${ad.serviceData.battery}`,
310
298
  );
311
299
 
312
- if (this.serviceData) {
300
+ if (ad.serviceData) {
313
301
  this.connected = true;
314
302
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.connected}`);
315
303
  await this.stopScanning(switchbot);
@@ -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,29 +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
32
31
  Version: deviceStatus['version'];
33
32
  Battery: deviceStatus['battery'];
34
33
  Temperature: deviceStatus['temperature'];
35
34
  Humidity: deviceStatus['humidity'];
36
- deviceStatus!: any; //deviceStatusResponse;
37
35
 
38
36
  // BLE Others
39
37
  connected?: boolean;
40
- switchbot!: switchbot;
41
- serviceData!: serviceData;
42
- address!: ad['address'];
43
38
  temperature!: serviceData['temperature'];
44
39
  celsius!: temperature['c'];
45
40
  fahrenheit!: temperature['f'];
@@ -155,19 +150,14 @@ export class MeterPlus {
155
150
  }
156
151
 
157
152
  // Battery Service
158
- if (!this.batteryService) {
159
- this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Battery Service`);
160
- (this.batteryService = this.accessory.getService(this.platform.Service.Battery) || this.accessory.addService(this.platform.Service.Battery)),
161
- `${accessory.displayName} Battery`;
162
-
163
- this.batteryService.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Battery`);
164
- if (!this.batteryService.testCharacteristic(this.platform.Characteristic.ConfiguredName)) {
165
- this.batteryService.addCharacteristic(this.platform.Characteristic.ConfiguredName, `${accessory.displayName} Battery`);
166
- }
167
- this.batteryService.setCharacteristic(this.platform.Characteristic.ChargingState, this.platform.Characteristic.ChargingState.NOT_CHARGEABLE);
168
- } else {
169
- 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`);
170
159
  }
160
+ this.batteryService.setCharacteristic(this.platform.Characteristic.ChargingState, this.platform.Characteristic.ChargingState.NOT_CHARGEABLE);
171
161
 
172
162
  // Retrieve initial values and updateHomekit
173
163
  this.updateHomeKitCharacteristics();
@@ -288,16 +278,14 @@ export class MeterPlus {
288
278
  .then(async () => {
289
279
  // Set an event hander
290
280
  switchbot.onadvertisement = async (ad: ad) => {
291
- this.address = ad.address;
292
281
  this.debugLog(
293
282
  `${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac},` +
294
- ` BLE Address Found: ${this.address}`,
283
+ ` BLE Address Found: ${ad.address}`,
295
284
  );
296
285
  if (ad.serviceData.humidity! > 0) {
297
286
  // reject unreliable data
298
287
  this.humidity = ad.serviceData.humidity;
299
288
  }
300
- this.serviceData = ad.serviceData;
301
289
  this.temperature = ad.serviceData.temperature;
302
290
  this.celsius = ad.serviceData.temperature!.c;
303
291
  this.fahrenheit = ad.serviceData.temperature!.f;
@@ -309,7 +297,7 @@ export class MeterPlus {
309
297
  `battery: ${ad.serviceData.battery}`,
310
298
  );
311
299
 
312
- if (this.serviceData) {
300
+ if (ad.serviceData) {
313
301
  this.connected = true;
314
302
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.connected}`);
315
303
  await this.stopScanning(switchbot);
@@ -425,7 +413,6 @@ export class MeterPlus {
425
413
  this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.StatusLowBattery);
426
414
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
427
415
  }
428
-
429
416
  if (this.device.mqttURL) {
430
417
  this.mqttPublish(`{${mqttmessage.join(',')}}`);
431
418
  }
@@ -5,7 +5,7 @@ import { interval, Subject } from 'rxjs';
5
5
  import { skipWhile } from 'rxjs/operators';
6
6
  import { SwitchBotPlatform } from '../platform';
7
7
  import { Service, PlatformAccessory, CharacteristicValue } from 'homebridge';
8
- import { device, devicesConfig, serviceData, switchbot, deviceStatus, ad, Devices } from '../settings';
8
+ import { device, devicesConfig, serviceData, deviceStatus, ad, Devices } from '../settings';
9
9
 
10
10
  /**
11
11
  * Platform Accessory
@@ -16,27 +16,23 @@ export class Motion {
16
16
  // Services
17
17
  motionSensorService: Service;
18
18
  lightSensorService?: Service;
19
- batteryService?: Service;
19
+ batteryService: Service;
20
20
 
21
21
  // Characteristic Values
22
22
  MotionDetected!: CharacteristicValue;
23
23
  CurrentAmbientLightLevel!: CharacteristicValue;
24
- BatteryLevel?: CharacteristicValue;
25
- StatusLowBattery?: CharacteristicValue;
24
+ BatteryLevel!: CharacteristicValue;
25
+ StatusLowBattery!: CharacteristicValue;
26
26
 
27
27
  // OpenAPI Others
28
28
  Version: deviceStatus['version'];
29
29
  Battery: deviceStatus['battery'];
30
- deviceStatus!: any; //deviceStatusResponse;
31
30
  moveDetected: deviceStatus['moveDetected'];
32
31
  brightness: deviceStatus['brightness'];
33
32
 
34
33
  // BLE Others
35
34
  scanning!: boolean;
36
35
  connected?: boolean;
37
- switchbot!: switchbot;
38
- serviceData!: serviceData;
39
- address!: ad['address'];
40
36
  battery!: serviceData['battery'];
41
37
  movement!: serviceData['movement'];
42
38
  lightLevel!: serviceData['lightLevel'];
@@ -126,19 +122,14 @@ export class Motion {
126
122
  }
127
123
 
128
124
  // Battery Service
129
- if (!this.batteryService) {
130
- this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Battery Service`);
131
- (this.batteryService = this.accessory.getService(this.platform.Service.Battery) || this.accessory.addService(this.platform.Service.Battery)),
132
- `${accessory.displayName} Battery`;
133
-
134
- this.batteryService.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Battery`);
135
- if (!this.batteryService.testCharacteristic(this.platform.Characteristic.ConfiguredName)) {
136
- this.batteryService.addCharacteristic(this.platform.Characteristic.ConfiguredName, `${accessory.displayName} Battery`);
137
- }
138
- this.batteryService.setCharacteristic(this.platform.Characteristic.ChargingState, this.platform.Characteristic.ChargingState.NOT_CHARGEABLE);
139
- } else {
140
- this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Battery Service Not Added`);
125
+ (this.batteryService = this.accessory.getService(this.platform.Service.Battery) || accessory.addService(this.platform.Service.Battery)),
126
+ `${accessory.displayName} Battery`;
127
+
128
+ this.batteryService.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Battery`);
129
+ if (!this.batteryService.testCharacteristic(this.platform.Characteristic.ConfiguredName)) {
130
+ this.batteryService.addCharacteristic(this.platform.Characteristic.ConfiguredName, `${accessory.displayName} Battery`);
141
131
  }
132
+ this.batteryService.setCharacteristic(this.platform.Characteristic.ChargingState, this.platform.Characteristic.ChargingState.NOT_CHARGEABLE);
142
133
 
143
134
  // Retrieve initial values and updateHomekit
144
135
  this.updateHomeKitCharacteristics();
@@ -290,28 +281,21 @@ export class Motion {
290
281
  fn: async () => {
291
282
  // Set an event hander
292
283
  this.scanning = true;
293
- switchbot.onadvertisement = async (ad: any) => {
294
- this.address = ad.address;
284
+ switchbot.onadvertisement = async (ad: ad) => {
295
285
  this.debugLog(
296
286
  `${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac},` +
297
- ` BLE Address Found: ${this.address}`,
287
+ ` BLE Address Found: ${ad.address}`,
298
288
  );
299
- this.serviceData = ad.serviceData;
300
289
  this.movement = ad.serviceData.movement;
301
- this.tested = ad.serviceData.tested;
302
290
  this.battery = ad.serviceData.battery;
303
- this.led = ad.serviceData.led;
304
- this.iot = ad.serviceData.led;
305
- this.sense_distance = ad.serviceData.sense_distance;
306
291
  this.lightLevel = ad.serviceData.lightLevel;
307
- this.is_light = ad.serviceData.is_light;
308
292
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
309
293
  this.debugLog(
310
- `${this.device.deviceType}: ${this.accessory.displayName} movement: ${ad.serviceData.movement}, is_light: ` +
311
- `${ad.serviceData.is_light}, lightLevel: ${ad.serviceData.lightLevel}, battery: ${ad.serviceData.battery}`,
294
+ `${this.device.deviceType}: ${this.accessory.displayName} movement: ${ad.serviceData.movement},`
295
+ + ` lightLevel: ${ad.serviceData.lightLevel}, battery: ${ad.serviceData.battery}`,
312
296
  );
313
297
 
314
- if (this.serviceData) {
298
+ if (ad.serviceData) {
315
299
  this.connected = true;
316
300
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.connected}`);
317
301
  this.debugErrorLog('1');
@@ -5,7 +5,7 @@ import { interval, Subject } from 'rxjs';
5
5
  import { SwitchBotPlatform } from '../platform';
6
6
  import { debounceTime, skipWhile, take, tap } from 'rxjs/operators';
7
7
  import { Service, PlatformAccessory, CharacteristicValue } from 'homebridge';
8
- import { device, devicesConfig, deviceStatus, ad, serviceData, switchbot, Devices } from '../settings';
8
+ import { device, devicesConfig, deviceStatus, ad, serviceData, Devices } from '../settings';
9
9
 
10
10
  export class Plug {
11
11
  // Services
@@ -17,13 +17,9 @@ export class Plug {
17
17
  // OpenAPI Others
18
18
  Version: deviceStatus['version'];
19
19
  power: deviceStatus['power'];
20
- deviceStatus!: any; //deviceStatusResponse;
21
20
 
22
21
  // BLE Others
23
22
  connected?: boolean;
24
- switchbot!: switchbot;
25
- address!: ad['address'];
26
- serviceData!: serviceData;
27
23
  state: serviceData['state'];
28
24
  delay: serviceData['delay'];
29
25
  timer: serviceData['timer'];
@@ -209,13 +205,11 @@ export class Plug {
209
205
  })
210
206
  .then(async () => {
211
207
  // Set an event hander
212
- switchbot.onadvertisement = async (ad: any) => {
213
- this.address = ad.address;
208
+ switchbot.onadvertisement = async (ad: ad) => {
214
209
  this.debugLog(
215
210
  `${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac},` +
216
- ` BLE Address Found: ${this.address}`,
211
+ ` BLE Address Found: ${ad.address}`,
217
212
  );
218
- this.serviceData = ad.serviceData;
219
213
  this.state = ad.serviceData.state;
220
214
  this.delay = ad.serviceData.delay;
221
215
  this.timer = ad.serviceData.timer;
@@ -230,7 +224,7 @@ export class Plug {
230
224
  `wifiRssi: ${ad.serviceData.wifiRssi}, overload: ${ad.serviceData.overload}, currentPower: ${ad.serviceData.currentPower}`,
231
225
  );
232
226
 
233
- if (this.serviceData) {
227
+ if (ad.serviceData) {
234
228
  this.connected = true;
235
229
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.connected}`);
236
230
  await this.stopScanning(switchbot);