@switchbot/homebridge-switchbot 2.2.0-beta.1 → 2.2.0-beta.3

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 (99) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/config.schema.json +0 -2
  3. package/dist/device/bot.d.ts +3 -0
  4. package/dist/device/bot.d.ts.map +1 -1
  5. package/dist/device/bot.js +13 -9
  6. package/dist/device/bot.js.map +1 -1
  7. package/dist/device/colorbulb.d.ts +11 -1
  8. package/dist/device/colorbulb.d.ts.map +1 -1
  9. package/dist/device/colorbulb.js +168 -16
  10. package/dist/device/colorbulb.js.map +1 -1
  11. package/dist/device/contact.d.ts +4 -1
  12. package/dist/device/contact.d.ts.map +1 -1
  13. package/dist/device/contact.js +16 -12
  14. package/dist/device/contact.js.map +1 -1
  15. package/dist/device/curtain.d.ts +2 -0
  16. package/dist/device/curtain.d.ts.map +1 -1
  17. package/dist/device/curtain.js +17 -11
  18. package/dist/device/curtain.js.map +1 -1
  19. package/dist/device/humidifier.d.ts +3 -0
  20. package/dist/device/humidifier.d.ts.map +1 -1
  21. package/dist/device/humidifier.js +13 -9
  22. package/dist/device/humidifier.js.map +1 -1
  23. package/dist/device/lock.d.ts +8 -1
  24. package/dist/device/lock.d.ts.map +1 -1
  25. package/dist/device/lock.js +83 -0
  26. package/dist/device/lock.js.map +1 -1
  27. package/dist/device/meter.d.ts +3 -0
  28. package/dist/device/meter.d.ts.map +1 -1
  29. package/dist/device/meter.js +13 -9
  30. package/dist/device/meter.js.map +1 -1
  31. package/dist/device/meterplus.d.ts +3 -0
  32. package/dist/device/meterplus.d.ts.map +1 -1
  33. package/dist/device/meterplus.js +13 -9
  34. package/dist/device/meterplus.js.map +1 -1
  35. package/dist/device/motion.d.ts +4 -1
  36. package/dist/device/motion.d.ts.map +1 -1
  37. package/dist/device/motion.js +20 -15
  38. package/dist/device/motion.js.map +1 -1
  39. package/dist/device/plug.d.ts +3 -0
  40. package/dist/device/plug.d.ts.map +1 -1
  41. package/dist/device/plug.js +13 -9
  42. package/dist/device/plug.js.map +1 -1
  43. package/dist/device/striplight.d.ts +3 -0
  44. package/dist/device/striplight.d.ts.map +1 -1
  45. package/dist/device/striplight.js +13 -9
  46. package/dist/device/striplight.js.map +1 -1
  47. package/dist/irdevice/airconditioner.d.ts +1 -4
  48. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  49. package/dist/irdevice/airconditioner.js +16 -28
  50. package/dist/irdevice/airconditioner.js.map +1 -1
  51. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  52. package/dist/irdevice/airpurifier.js +4 -5
  53. package/dist/irdevice/airpurifier.js.map +1 -1
  54. package/dist/irdevice/camera.d.ts.map +1 -1
  55. package/dist/irdevice/camera.js +2 -3
  56. package/dist/irdevice/camera.js.map +1 -1
  57. package/dist/irdevice/fan.d.ts.map +1 -1
  58. package/dist/irdevice/fan.js +11 -10
  59. package/dist/irdevice/fan.js.map +1 -1
  60. package/dist/irdevice/light.d.ts.map +1 -1
  61. package/dist/irdevice/light.js +2 -3
  62. package/dist/irdevice/light.js.map +1 -1
  63. package/dist/irdevice/other.d.ts.map +1 -1
  64. package/dist/irdevice/other.js +1 -1
  65. package/dist/irdevice/other.js.map +1 -1
  66. package/dist/irdevice/tv.d.ts.map +1 -1
  67. package/dist/irdevice/tv.js +11 -12
  68. package/dist/irdevice/tv.js.map +1 -1
  69. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  70. package/dist/irdevice/vacuumcleaner.js +2 -3
  71. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  72. package/dist/irdevice/waterheater.d.ts.map +1 -1
  73. package/dist/irdevice/waterheater.js +2 -3
  74. package/dist/irdevice/waterheater.js.map +1 -1
  75. package/dist/settings.d.ts +7 -1
  76. package/dist/settings.d.ts.map +1 -1
  77. package/dist/settings.js.map +1 -1
  78. package/package.json +4 -4
  79. package/src/device/bot.ts +13 -8
  80. package/src/device/colorbulb.ts +185 -20
  81. package/src/device/contact.ts +17 -12
  82. package/src/device/curtain.ts +18 -11
  83. package/src/device/humidifier.ts +13 -8
  84. package/src/device/lock.ts +92 -1
  85. package/src/device/meter.ts +13 -8
  86. package/src/device/meterplus.ts +13 -8
  87. package/src/device/motion.ts +21 -15
  88. package/src/device/plug.ts +13 -8
  89. package/src/device/striplight.ts +13 -8
  90. package/src/irdevice/airconditioner.ts +20 -37
  91. package/src/irdevice/airpurifier.ts +4 -5
  92. package/src/irdevice/camera.ts +3 -4
  93. package/src/irdevice/fan.ts +11 -10
  94. package/src/irdevice/light.ts +2 -3
  95. package/src/irdevice/other.ts +1 -1
  96. package/src/irdevice/tv.ts +11 -12
  97. package/src/irdevice/vacuumcleaner.ts +2 -3
  98. package/src/irdevice/waterheater.ts +2 -3
  99. package/src/settings.ts +14 -2
@@ -41,7 +41,7 @@ export class Contact {
41
41
  address!: ad['address'];
42
42
  battery!: serviceData['battery'];
43
43
  movement!: serviceData['movement'];
44
- hallState!: any; //serviceData['doorState'];
44
+ doorState!: serviceData['doorState'];
45
45
  is_light!: any; //serviceData['lightLevel'];
46
46
  tested!: any;
47
47
  contact_open!: any;
@@ -183,7 +183,7 @@ export class Contact {
183
183
  async BLEparseStatus(): Promise<void> {
184
184
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
185
185
  // Door State
186
- switch (this.hallState) {
186
+ switch (this.doorState) {
187
187
  case 'open':
188
188
  case 1:
189
189
  this.ContactSensorState = this.platform.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED;
@@ -193,7 +193,7 @@ export class Contact {
193
193
  this.ContactSensorState = this.platform.Characteristic.ContactSensorState.CONTACT_DETECTED;
194
194
  break;
195
195
  default:
196
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} timeout no closed, doorstate: ${this.hallState}`);
196
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} timeout no closed, doorstate: ${this.doorState}`);
197
197
  }
198
198
  // Movement
199
199
  if (!this.device.contact?.hide_motionsensor) {
@@ -301,7 +301,7 @@ export class Contact {
301
301
  })
302
302
  .then(async () => {
303
303
  // Set an event hander
304
- switchbot.onadvertisement = (ad: any) => {
304
+ switchbot.onadvertisement = async (ad: any) => {
305
305
  this.address = ad.address;
306
306
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac},`
307
307
  + ` BLE Address Found: ${this.address}`);
@@ -313,7 +313,7 @@ export class Contact {
313
313
  this.contact_timeout = ad.serviceData.contact_timeout;
314
314
  this.is_light = ad.serviceData.is_light;
315
315
  this.button_count = ad.serviceData.button_count;
316
- this.hallState = ad.serviceData.hallState;
316
+ this.doorState = ad.serviceData.doorState;
317
317
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${superStringify(ad.serviceData)}`);
318
318
  this.debugLog(
319
319
  `${this.device.deviceType}: ${this.accessory.displayName} movement: ${ad.serviceData.movement}, doorState: ` +
@@ -323,6 +323,7 @@ export class Contact {
323
323
  if (this.serviceData) {
324
324
  this.connected = true;
325
325
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.connected}`);
326
+ await this.stopScanning(switchbot);
326
327
  } else {
327
328
  this.connected = false;
328
329
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.connected}`);
@@ -333,13 +334,7 @@ export class Contact {
333
334
  })
334
335
  .then(async () => {
335
336
  // Stop to monitor
336
- switchbot.stopScan();
337
- if (this.connected) {
338
- this.BLEparseStatus();
339
- this.updateHomeKitCharacteristics();
340
- } else {
341
- await this.BLERefreshConnection(switchbot);
342
- }
337
+ await this.stopScanning(switchbot);
343
338
  })
344
339
  .catch(async (e: any) => {
345
340
  this.apiError(e);
@@ -455,6 +450,16 @@ export class Contact {
455
450
  }
456
451
  }
457
452
 
453
+ async stopScanning({ switchbot }: { switchbot: any; }): Promise<void> {
454
+ switchbot.stopScan();
455
+ if (this.connected) {
456
+ await this.BLEparseStatus();
457
+ await this.updateHomeKitCharacteristics();
458
+ } else {
459
+ await this.BLERefreshConnection(switchbot);
460
+ }
461
+ }
462
+
458
463
  async getCustomBLEAddress(switchbot: any) {
459
464
  if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
460
465
  (async () => {
@@ -42,6 +42,7 @@ export class Curtain {
42
42
  calibration: serviceData['calibration'];
43
43
  battery: serviceData['battery'];
44
44
  position: serviceData['position'];
45
+ inMotion: serviceData['inMotion'];
45
46
  lightLevel: serviceData['lightLevel'];
46
47
 
47
48
  // Target
@@ -237,7 +238,7 @@ export class Curtain {
237
238
  this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Checking Status ...`);
238
239
  }
239
240
 
240
- if (this.setNewTarget) {
241
+ if (this.setNewTarget && this.inMotion) {
241
242
  await this.setMinMax();
242
243
  if (this.TargetPosition > this.CurrentPosition) {
243
244
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Closing, CurrentPosition: ${this.CurrentPosition}`);
@@ -421,24 +422,26 @@ export class Curtain {
421
422
  })
422
423
  .then(() => {
423
424
  // Set an event hander
424
- switchbot.onadvertisement = (ad: any) => {
425
+ switchbot.onadvertisement = async (ad: any) => {
425
426
  this.address = ad.address;
426
427
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac},`
427
428
  + ` BLE Address Found: ${this.address}`);
428
429
  this.serviceData = ad.serviceData;
429
430
  this.calibration = ad.serviceData.calibration;
430
431
  this.battery = ad.serviceData.battery;
432
+ this.inMotion = ad.serviceData.inMotion;
431
433
  this.position = ad.serviceData.position;
432
434
  this.lightLevel = ad.serviceData.lightLevel;
433
435
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${superStringify(ad.serviceData)}`);
434
436
  this.debugLog(
435
437
  `${this.device.deviceType}: ${this.accessory.displayName} calibration: ${ad.serviceData.calibration}, ` +
436
- `position: ${ad.serviceData.position}, lightLevel: ${ad.serviceData.lightLevel}, battery: ${ad.serviceData.battery}`,
437
- );
438
+ `position: ${ad.serviceData.position}, lightLevel: ${ad.serviceData.lightLevel}, battery: ${ad.serviceData.battery}, ` +
439
+ `inMotion: ${ad.serviceData.inMotion}`);
438
440
 
439
441
  if (this.serviceData) {
440
442
  this.connected = true;
441
443
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.connected}`);
444
+ await this.stopScanning(switchbot);
442
445
  } else {
443
446
  this.connected = false;
444
447
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.connected}`);
@@ -449,13 +452,7 @@ export class Curtain {
449
452
  })
450
453
  .then(async () => {
451
454
  // Stop to monitor
452
- switchbot.stopScan();
453
- if (this.connected) {
454
- this.BLEparseStatus();
455
- this.updateHomeKitCharacteristics();
456
- } else {
457
- await this.BLERefreshConnection(switchbot);
458
- }
455
+ await this.stopScanning(switchbot);
459
456
  })
460
457
  .catch(async (e: any) => {
461
458
  this.apiError(e);
@@ -809,6 +806,16 @@ export class Curtain {
809
806
  }
810
807
  }
811
808
 
809
+ async stopScanning(switchbot: any) {
810
+ switchbot.stopScan();
811
+ if (this.connected) {
812
+ await this.BLEparseStatus();
813
+ await this.updateHomeKitCharacteristics();
814
+ } else {
815
+ await this.BLERefreshConnection(switchbot);
816
+ }
817
+ }
818
+
812
819
  async getCustomBLEAddress(switchbot: any) {
813
820
  if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
814
821
  (async () => {
@@ -314,7 +314,7 @@ export class Humidifier {
314
314
  })
315
315
  .then(() => {
316
316
  // Set an event hander
317
- switchbot.onadvertisement = (ad: ad) => {
317
+ switchbot.onadvertisement = async (ad: ad) => {
318
318
  this.address = ad.address;
319
319
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac},`
320
320
  + ` BLE Address Found: ${this.address}`);
@@ -331,6 +331,7 @@ export class Humidifier {
331
331
  if (this.serviceData) {
332
332
  this.connected = true;
333
333
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.connected}`);
334
+ await this.stopScanning(switchbot);
334
335
  } else {
335
336
  this.connected = false;
336
337
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.connected}`);
@@ -341,13 +342,7 @@ export class Humidifier {
341
342
  })
342
343
  .then(async () => {
343
344
  // Stop to monitor
344
- switchbot.stopScan();
345
- if (this.connected) {
346
- this.BLEparseStatus();
347
- this.updateHomeKitCharacteristics();
348
- } else {
349
- await this.BLERefreshConnection(switchbot);
350
- }
345
+ await this.stopScanning(switchbot);
351
346
  })
352
347
  .catch(async (e: any) => {
353
348
  this.apiError(e);
@@ -770,6 +765,16 @@ export class Humidifier {
770
765
  }
771
766
  }
772
767
 
768
+ async stopScanning({ switchbot }: { switchbot: any; }): Promise<void> {
769
+ switchbot.stopScan();
770
+ if (this.connected) {
771
+ await this.BLEparseStatus();
772
+ await this.updateHomeKitCharacteristics();
773
+ } else {
774
+ await this.BLERefreshConnection(switchbot);
775
+ }
776
+ }
777
+
773
778
  async getCustomBLEAddress(switchbot: any) {
774
779
  if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
775
780
  (async () => {
@@ -7,7 +7,7 @@ import superStringify from 'super-stringify';
7
7
  import { SwitchBotPlatform } from '../platform';
8
8
  import { debounceTime, skipWhile, take, tap } from 'rxjs/operators';
9
9
  import { Service, PlatformAccessory, CharacteristicValue } from 'homebridge';
10
- import { device, devicesConfig, deviceStatus, HostDomain, DevicePath } from '../settings';
10
+ import { device, devicesConfig, deviceStatus, HostDomain, DevicePath, switchbot, ad, serviceData } from '../settings';
11
11
 
12
12
  export class Lock {
13
13
  // Services
@@ -24,6 +24,12 @@ export class Lock {
24
24
  lockState!: deviceStatus['lockState'];
25
25
  deviceStatus!: any; //deviceStatusResponse;
26
26
 
27
+ // BLE Others
28
+ connected?: boolean;
29
+ switchbot!: switchbot;
30
+ address!: ad['address'];
31
+ serviceData!: serviceData;
32
+
27
33
  // Config
28
34
  scanDuration!: number;
29
35
  deviceLogging!: string;
@@ -206,6 +212,63 @@ export class Lock {
206
212
  }
207
213
  }
208
214
 
215
+ async BLERefreshStatus(): Promise<void> {
216
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLERefreshStatus`);
217
+ const switchbot = await this.platform.connectBLE();
218
+ // Convert to BLE Address
219
+ this.device.bleMac = this.device
220
+ .deviceId!.match(/.{1,2}/g)!
221
+ .join(':')
222
+ .toLowerCase();
223
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
224
+ this.getCustomBLEAddress(switchbot);
225
+ // Start to monitor advertisement packets
226
+ if (switchbot !== false) {
227
+ switchbot
228
+ .startScan({
229
+ model: 'o',
230
+ id: this.device.bleMac,
231
+ })
232
+ .then(() => {
233
+ // Set an event hander
234
+ switchbot.onadvertisement = async (ad: any) => {
235
+ this.address = ad.address;
236
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac},`
237
+ + ` BLE Address Found: ${this.address}`);
238
+ this.serviceData = ad.serviceData;
239
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${superStringify(ad.serviceData)}`);
240
+ this.debugLog(
241
+ `${this.device.deviceType}: ${this.accessory.displayName} calibration: ${ad.serviceData.calibration}, ` +
242
+ `position: ${ad.serviceData.position}, lightLevel: ${ad.serviceData.lightLevel}, battery: ${ad.serviceData.battery}, ` +
243
+ `inMotion: ${ad.serviceData.inMotion}`);
244
+
245
+ if (this.serviceData) {
246
+ this.connected = true;
247
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.connected}`);
248
+ await this.stopScanning(switchbot);
249
+ } else {
250
+ this.connected = false;
251
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.connected}`);
252
+ }
253
+ };
254
+ // Wait 2 seconds
255
+ return switchbot.wait(this.scanDuration * 1000);
256
+ })
257
+ .then(async () => {
258
+ // Stop to monitor
259
+ await this.stopScanning(switchbot);
260
+ })
261
+ .catch(async (e: any) => {
262
+ this.apiError(e);
263
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLERefreshStatus with ${this.device.connectionType}`
264
+ + ` Connection, Error Message: ${superStringify(e.message)}`);
265
+ await this.BLERefreshConnection(switchbot);
266
+ });
267
+ } else {
268
+ await this.BLERefreshConnection(switchbot);
269
+ }
270
+ }
271
+
209
272
  async openAPIRefreshStatus(): Promise<void> {
210
273
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
211
274
  try {
@@ -427,6 +490,34 @@ export class Lock {
427
490
  }
428
491
  }
429
492
 
493
+ async stopScanning(switchbot: any) {
494
+ switchbot.stopScan();
495
+ if (this.connected) {
496
+ await this.BLEparseStatus();
497
+ await this.updateHomeKitCharacteristics();
498
+ } else {
499
+ await this.BLERefreshConnection(switchbot);
500
+ }
501
+ }
502
+
503
+ async getCustomBLEAddress(switchbot: any) {
504
+ if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
505
+ (async () => {
506
+ // Start to monitor advertisement packets
507
+ await switchbot.startScan({
508
+ model: 'c',
509
+ });
510
+ // Set an event handler
511
+ switchbot.onadvertisement = (ad: any) => {
512
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${superStringify(ad, null, ' ')}`);
513
+ };
514
+ await switchbot.wait(10000);
515
+ // Stop to monitor
516
+ switchbot.stopScan();
517
+ })();
518
+ }
519
+ }
520
+
430
521
  async BLEPushConnection() {
431
522
  if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
432
523
  this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection to Push Changes`);
@@ -268,7 +268,7 @@ export class Meter {
268
268
  })
269
269
  .then(() => {
270
270
  // Set an event hander
271
- switchbot.onadvertisement = (ad: ad) => {
271
+ switchbot.onadvertisement = async (ad: ad) => {
272
272
  this.address = ad.address;
273
273
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac},`
274
274
  + ` BLE Address Found: ${this.address}`);
@@ -291,6 +291,7 @@ export class Meter {
291
291
  if (this.serviceData) {
292
292
  this.connected = true;
293
293
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.connected}`);
294
+ await this.stopScanning(switchbot);
294
295
  } else {
295
296
  this.connected = false;
296
297
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.connected}`);
@@ -301,13 +302,7 @@ export class Meter {
301
302
  })
302
303
  .then(async () => {
303
304
  // Stop to monitor
304
- switchbot.stopScan();
305
- if (this.connected) {
306
- this.BLEparseStatus();
307
- this.updateHomeKitCharacteristics();
308
- } else {
309
- await this.BLERefreshConnection(switchbot);
310
- }
305
+ await this.stopScanning(switchbot);
311
306
  })
312
307
  .catch(async (e: any) => {
313
308
  this.apiError(e);
@@ -493,6 +488,16 @@ export class Meter {
493
488
  : null;
494
489
  }
495
490
 
491
+ async stopScanning({ switchbot }: { switchbot: any; }): Promise<void> {
492
+ switchbot.stopScan();
493
+ if (this.connected) {
494
+ await this.BLEparseStatus();
495
+ await this.updateHomeKitCharacteristics();
496
+ } else {
497
+ await this.BLERefreshConnection(switchbot);
498
+ }
499
+ }
500
+
496
501
  async getCustomBLEAddress(switchbot: any) {
497
502
  if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
498
503
  (async () => {
@@ -268,7 +268,7 @@ export class MeterPlus {
268
268
  })
269
269
  .then(() => {
270
270
  // Set an event hander
271
- switchbot.onadvertisement = (ad: ad) => {
271
+ switchbot.onadvertisement = async (ad: ad) => {
272
272
  this.address = ad.address;
273
273
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac},`
274
274
  + ` BLE Address Found: ${this.address}`);
@@ -291,6 +291,7 @@ export class MeterPlus {
291
291
  if (this.serviceData) {
292
292
  this.connected = true;
293
293
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.connected}`);
294
+ await this.stopScanning(switchbot);
294
295
  } else {
295
296
  this.connected = false;
296
297
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.connected}`);
@@ -301,13 +302,7 @@ export class MeterPlus {
301
302
  })
302
303
  .then(async () => {
303
304
  // Stop to monitor
304
- switchbot.stopScan();
305
- if (this.connected) {
306
- this.parseStatus();
307
- this.updateHomeKitCharacteristics();
308
- } else {
309
- await this.BLERefreshConnection(switchbot);
310
- }
305
+ await this.stopScanning(switchbot);
311
306
  })
312
307
  .catch(async (e: any) => {
313
308
  this.apiError(e);
@@ -492,6 +487,16 @@ export class MeterPlus {
492
487
  : null;
493
488
  }
494
489
 
490
+ async stopScanning({ switchbot }: { switchbot: any; }): Promise<void> {
491
+ switchbot.stopScan();
492
+ if (this.connected) {
493
+ await this.BLEparseStatus();
494
+ await this.updateHomeKitCharacteristics();
495
+ } else {
496
+ await this.BLERefreshConnection(switchbot);
497
+ }
498
+ }
499
+
495
500
  async getCustomBLEAddress(switchbot: any) {
496
501
  if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
497
502
  (async () => {
@@ -38,12 +38,12 @@ export class Motion {
38
38
  address!: ad['address'];
39
39
  battery!: serviceData['battery'];
40
40
  movement!: serviceData['movement'];
41
+ lightLevel!: serviceData['lightLevel'];
41
42
  is_light!: any;//serviceData['is_light'];
42
43
  tested!: any;
43
44
  led!: any;
44
45
  iot!: any;
45
46
  sense_distance!: any;
46
- light_intensity!: any;
47
47
 
48
48
  // Config
49
49
  set_minLux!: number;
@@ -169,15 +169,16 @@ export class Motion {
169
169
  if (!this.device.motion?.hide_lightsensor) {
170
170
  this.set_minLux = this.minLux();
171
171
  this.set_maxLux = this.maxLux();
172
- switch (this.is_light) {
173
- case true:
172
+ switch (this.lightLevel) {
173
+ case 'dark':
174
+ case 1:
174
175
  this.CurrentAmbientLightLevel = this.set_minLux;
175
176
  break;
176
177
  default:
177
178
  this.CurrentAmbientLightLevel = this.set_maxLux;
178
179
  }
179
180
  this.debugLog(
180
- `${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${this.is_light},` +
181
+ `${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${this.lightLevel},` +
181
182
  ` CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`,
182
183
  );
183
184
  }
@@ -254,7 +255,7 @@ export class Motion {
254
255
  })
255
256
  .then(async () => {
256
257
  // Set an event hander
257
- switchbot.onadvertisement = (ad: any) => {
258
+ switchbot.onadvertisement = async (ad: any) => {
258
259
  this.address = ad.address;
259
260
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac},`
260
261
  + ` BLE Address Found: ${this.address}`);
@@ -265,34 +266,29 @@ export class Motion {
265
266
  this.led = ad.serviceData.led;
266
267
  this.iot = ad.serviceData.led;
267
268
  this.sense_distance = ad.serviceData.sense_distance;
268
- this.light_intensity = ad.serviceData.light_intensity;
269
+ this.lightLevel = ad.serviceData.lightLevel;
269
270
  this.is_light = ad.serviceData.is_light;
270
271
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${superStringify(ad.serviceData)}`);
271
272
  this.debugLog(
272
273
  `${this.device.deviceType}: ${this.accessory.displayName} movement: ${ad.serviceData.movement}, is_light: ` +
273
- `${ad.serviceData.is_light}, battery: ${ad.serviceData.battery}`,
274
+ `${ad.serviceData.is_light}, lightLevel: ${ad.serviceData.lightLevel}, battery: ${ad.serviceData.battery}`,
274
275
  );
275
276
 
276
277
  if (this.serviceData) {
277
278
  this.connected = true;
278
279
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.connected}`);
280
+ await this.stopScanning(switchbot);
279
281
  } else {
280
282
  this.connected = false;
281
283
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.connected}`);
282
284
  }
283
285
  };
284
286
  // Wait 10 seconds
285
- return await switchbot.wait(this.scanDuration * 1000);
287
+ return await switchbot.wait(this.scanDuration * 2000);
286
288
  })
287
289
  .then(async () => {
288
290
  // Stop to monitor
289
- switchbot.stopScan();
290
- if (this.connected) {
291
- this.BLEparseStatus();
292
- this.updateHomeKitCharacteristics();
293
- } else {
294
- await this.BLERefreshConnection(switchbot);
295
- }
291
+ await this.stopScanning(switchbot);
296
292
  })
297
293
  .catch(async (e: any) => {
298
294
  this.apiError(e);
@@ -398,6 +394,16 @@ export class Motion {
398
394
  }
399
395
  }
400
396
 
397
+ async stopScanning({ switchbot }: { switchbot: any; }): Promise<void> {
398
+ switchbot.stopScan();
399
+ if (this.connected) {
400
+ await this.BLEparseStatus();
401
+ await this.updateHomeKitCharacteristics();
402
+ } else {
403
+ await this.BLERefreshConnection(switchbot);
404
+ }
405
+ }
406
+
401
407
  async getCustomBLEAddress(switchbot: any) {
402
408
  if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
403
409
  (async () => {
@@ -199,7 +199,7 @@ export class Plug {
199
199
  })
200
200
  .then(() => {
201
201
  // Set an event hander
202
- switchbot.onadvertisement = (ad: any) => {
202
+ switchbot.onadvertisement = async (ad: any) => {
203
203
  this.address = ad.address;
204
204
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac},`
205
205
  + ` BLE Address Found: ${this.address}`);
@@ -221,6 +221,7 @@ export class Plug {
221
221
  if (this.serviceData) {
222
222
  this.connected = true;
223
223
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.connected}`);
224
+ await this.stopScanning(switchbot);
224
225
  } else {
225
226
  this.connected = false;
226
227
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.connected}`);
@@ -231,13 +232,7 @@ export class Plug {
231
232
  })
232
233
  .then(async () => {
233
234
  // Stop to monitor
234
- switchbot.stopScan();
235
- if (this.connected) {
236
- this.BLEparseStatus();
237
- this.updateHomeKitCharacteristics();
238
- } else {
239
- await this.BLERefreshConnection(switchbot);
240
- }
235
+ await this.stopScanning(switchbot);
241
236
  })
242
237
  .catch(async (e: any) => {
243
238
  this.apiError(e);
@@ -463,6 +458,16 @@ export class Plug {
463
458
  }
464
459
  }
465
460
 
461
+ async stopScanning({ switchbot }: { switchbot: any; }): Promise<void> {
462
+ switchbot.stopScan();
463
+ if (this.connected) {
464
+ await this.BLEparseStatus();
465
+ await this.updateHomeKitCharacteristics();
466
+ } else {
467
+ await this.BLERefreshConnection(switchbot);
468
+ }
469
+ }
470
+
466
471
  BLEmodel(): 'g' | 'j' {
467
472
  if (this.device.deviceType === 'Plug Mini (US)') {
468
473
  return 'g';
@@ -311,7 +311,7 @@ export class StripLight {
311
311
  })
312
312
  .then(() => {
313
313
  // Set an event hander
314
- switchbot.onadvertisement = (ad: any) => {
314
+ switchbot.onadvertisement = async (ad: any) => {
315
315
  this.address = ad.address;
316
316
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac},`
317
317
  + ` BLE Address Found: ${this.address}`);
@@ -334,6 +334,7 @@ export class StripLight {
334
334
  if (this.serviceData) {
335
335
  this.connected = true;
336
336
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.connected}`);
337
+ await this.stopScanning(switchbot);
337
338
  } else {
338
339
  this.connected = false;
339
340
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.connected}`);
@@ -344,13 +345,7 @@ export class StripLight {
344
345
  })
345
346
  .then(async () => {
346
347
  // Stop to monitor
347
- switchbot.stopScan();
348
- if (this.connected) {
349
- this.BLEparseStatus();
350
- this.updateHomeKitCharacteristics();
351
- } else {
352
- await this.BLERefreshConnection(switchbot);
353
- }
348
+ await this.stopScanning(switchbot);
354
349
  })
355
350
  .catch(async (e: any) => {
356
351
  this.apiError(e);
@@ -814,6 +809,16 @@ export class StripLight {
814
809
  }
815
810
  }
816
811
 
812
+ async stopScanning({ switchbot }: { switchbot: any; }): Promise<void> {
813
+ switchbot.stopScan();
814
+ if (this.connected) {
815
+ await this.BLEparseStatus();
816
+ await this.updateHomeKitCharacteristics();
817
+ } else {
818
+ await this.BLERefreshConnection(switchbot);
819
+ }
820
+ }
821
+
817
822
  async getCustomBLEAddress(switchbot: any) {
818
823
  if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
819
824
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} customBLEaddress: ${this.device.customBLEaddress}`);