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

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 (57) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/config.schema.json +0 -2
  3. package/dist/device/bot.d.ts +1 -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 +173 -23
  10. package/dist/device/colorbulb.js.map +1 -1
  11. package/dist/device/contact.d.ts +2 -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/meter.d.ts +3 -0
  24. package/dist/device/meter.d.ts.map +1 -1
  25. package/dist/device/meter.js +13 -9
  26. package/dist/device/meter.js.map +1 -1
  27. package/dist/device/meterplus.d.ts +3 -0
  28. package/dist/device/meterplus.d.ts.map +1 -1
  29. package/dist/device/meterplus.js +13 -9
  30. package/dist/device/meterplus.js.map +1 -1
  31. package/dist/device/motion.d.ts +2 -1
  32. package/dist/device/motion.d.ts.map +1 -1
  33. package/dist/device/motion.js +20 -15
  34. package/dist/device/motion.js.map +1 -1
  35. package/dist/device/plug.d.ts +3 -0
  36. package/dist/device/plug.d.ts.map +1 -1
  37. package/dist/device/plug.js +13 -9
  38. package/dist/device/plug.js.map +1 -1
  39. package/dist/device/striplight.d.ts +3 -0
  40. package/dist/device/striplight.d.ts.map +1 -1
  41. package/dist/device/striplight.js +13 -9
  42. package/dist/device/striplight.js.map +1 -1
  43. package/dist/settings.d.ts +7 -1
  44. package/dist/settings.d.ts.map +1 -1
  45. package/dist/settings.js.map +1 -1
  46. package/package.json +4 -4
  47. package/src/device/bot.ts +13 -8
  48. package/src/device/colorbulb.ts +190 -25
  49. package/src/device/contact.ts +17 -12
  50. package/src/device/curtain.ts +18 -11
  51. package/src/device/humidifier.ts +13 -8
  52. package/src/device/meter.ts +13 -8
  53. package/src/device/meterplus.ts +13 -8
  54. package/src/device/motion.ts +21 -15
  55. package/src/device/plug.ts +13 -8
  56. package/src/device/striplight.ts +13 -8
  57. package/src/settings.ts +14 -2
@@ -37,11 +37,15 @@ export class ColorBulb {
37
37
  switchbot!: switchbot;
38
38
  address!: ad['address'];
39
39
  serviceData!: serviceData;
40
+ powerState: serviceData['power'];
40
41
  state: serviceData['state'];
42
+ red: serviceData['red'];
43
+ green: serviceData['green'];
44
+ blue: serviceData['blue'];
41
45
  delay: serviceData['delay'];
42
46
  wifiRssi: serviceData['wifiRssi'];
43
47
  brightnessBLE: serviceData['brightness'];
44
- sequence: any;//serviceData['sequence'];
48
+ color_temperature: serviceData['color_temperature'];
45
49
  preset: any;//serviceData['preset'];
46
50
  color_mode: any;//serviceData['color_mode'];
47
51
  speed: any;//serviceData['speed'];
@@ -221,9 +225,9 @@ export class ColorBulb {
221
225
  async parseStatus(): Promise<void> {
222
226
  if (!this.device.enableCloudService && this.OpenAPI) {
223
227
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} parseStatus enableCloudService: ${this.device.enableCloudService}`);
224
- } else/*if (this.BLE) {
228
+ } else if (this.BLE) {
225
229
  await this.BLEparseStatus();
226
- } else*/ if (this.OpenAPI && this.platform.config.credentials?.token) {
230
+ } else if (this.OpenAPI && this.platform.config.credentials?.token) {
227
231
  await this.openAPIparseStatus();
228
232
  } else {
229
233
  await this.offlineOff();
@@ -236,13 +240,44 @@ export class ColorBulb {
236
240
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
237
241
  // State
238
242
  switch (this.state) {
239
- case 'on':
243
+ case true:
240
244
  this.On = true;
241
245
  break;
242
246
  default:
243
247
  this.On = false;
244
248
  }
245
249
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
250
+
251
+
252
+ // Brightness
253
+ this.Brightness = Number(this.brightnessBLE);
254
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Brightness: ${this.Brightness}`);
255
+
256
+ // Color, Hue & Brightness
257
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} red: ${this.red}`);
258
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} green: ${this.green}`);
259
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} blue: ${this.blue}`);
260
+
261
+ const [hue, saturation] = rgb2hs(Number(this.red), Number(this.green), Number(this.blue));
262
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
263
+ + ` hs: ${superStringify(rgb2hs(Number(this.red), Number(this.green), Number(this.blue)))}`);
264
+
265
+ // Hue
266
+ this.Hue = hue;
267
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${this.Hue}`);
268
+
269
+ // Saturation
270
+ this.Saturation = saturation;
271
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${this.Saturation}`);
272
+
273
+ // ColorTemperature
274
+ if (this.color_temperature) {
275
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE ColorTemperature: ${this.color_temperature}`);
276
+ this.ColorTemperature = this.color_temperature!;
277
+
278
+ this.ColorTemperature = Math.max(Math.min(this.ColorTemperature, 500), 140);
279
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ColorTemperature: ${this.ColorTemperature}`);
280
+ }
246
281
  }
247
282
 
248
283
  async openAPIparseStatus() {
@@ -329,14 +364,18 @@ export class ColorBulb {
329
364
  })
330
365
  .then(() => {
331
366
  // Set an event hander
332
- switchbot.onadvertisement = (ad: any) => {
367
+ switchbot.onadvertisement = async (ad: any) => {
333
368
  this.address = ad.address;
334
369
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac},`
335
370
  + ` BLE Address Found: ${this.address}`);
336
371
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${superStringify(ad.serviceData)}`);
337
372
  this.serviceData = ad.serviceData;
373
+ this.powerState = ad.serviceData.power;
338
374
  this.state = ad.serviceData.state;
339
- this.sequence = ad.serviceData.sequence_number;
375
+ this.red = ad.serviceData.red;
376
+ this.green = ad.serviceData.green;
377
+ this.blue = ad.serviceData.blue;
378
+ this.color_temperature = ad.serviceData.color_temperature;
340
379
  this.brightnessBLE = ad.serviceData.brightness;
341
380
  this.delay = ad.serviceData.delay;
342
381
  this.preset = ad.serviceData.preset;
@@ -353,6 +392,7 @@ export class ColorBulb {
353
392
  if (this.serviceData) {
354
393
  this.connected = true;
355
394
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.connected}`);
395
+ await this.stopScanning({ switchbot });
356
396
  } else {
357
397
  this.connected = false;
358
398
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.connected}`);
@@ -363,13 +403,7 @@ export class ColorBulb {
363
403
  })
364
404
  .then(async () => {
365
405
  // Stop to monitor
366
- switchbot.stopScan();
367
- if (this.connected) {
368
- this.BLEparseStatus();
369
- this.updateHomeKitCharacteristics();
370
- } else {
371
- await this.BLERefreshConnection(switchbot);
372
- }
406
+ await this.stopScanning({ switchbot });
373
407
  })
374
408
  .catch(async (e: any) => {
375
409
  this.apiError(e);
@@ -452,10 +486,9 @@ export class ColorBulb {
452
486
  async pushChanges(): Promise<void> {
453
487
  if (!this.device.enableCloudService && this.OpenAPI) {
454
488
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} pushChanges enableCloudService: ${this.device.enableCloudService}`);
455
- } else
456
- /*if (this.BLE) {
489
+ } else/* if (this.BLE) {
457
490
  await this.BLEpushChanges();
458
- } else*/ if (this.OpenAPI && this.platform.config.credentials?.token) {
491
+ } else*/if (this.OpenAPI && this.platform.config.credentials?.token) {
459
492
  await this.openAPIpushChanges();
460
493
  } else {
461
494
  await this.offlineOff();
@@ -473,8 +506,132 @@ export class ColorBulb {
473
506
 
474
507
  async BLEpushChanges(): Promise<void> {
475
508
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`);
476
- if (this.On !== this.accessory.context.On) {
477
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges On: ${this.On} OnCached: ${this.accessory.context.On}`);
509
+ //if (this.On !== this.accessory.context.On) {
510
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges On: ${this.On} OnCached: ${this.accessory.context.On}`);
511
+ const switchbot = await this.platform.connectBLE();
512
+ // Convert to BLE Address
513
+ this.device.bleMac = this.device
514
+ .deviceId!.match(/.{1,2}/g)!
515
+ .join(':')
516
+ .toLowerCase();
517
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
518
+ switchbot
519
+ .discover({
520
+ model: 'u',
521
+ id: this.device.bleMac,
522
+ })
523
+ .then((device_list: any) => {
524
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
525
+ return this.turnOnOff(device_list);
526
+ })
527
+ .then(() => {
528
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
529
+ this.On = false;
530
+ })
531
+ .catch(async (e: any) => {
532
+ this.apiError(e);
533
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushChanges with ${this.device.connectionType}`
534
+ + ` Connection, Error Message: ${superStringify(e.message)}`);
535
+ await this.BLEPushConnection();
536
+ });
537
+ // Push Brightness Update
538
+ if (this.On) {
539
+ await this.BLEpushBrightnessChanges();
540
+ }
541
+ // Push ColorTemperature Update
542
+ if (this.On) {
543
+ await this.BLEpushColorTemperatureChanges();
544
+ }
545
+ // Push Hue & Saturation Update
546
+ if (this.On) {
547
+ await this.BLEpushRGBChanges();
548
+ }
549
+ // } else {
550
+ // this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushChanges.` + `On: ${this.On}, `
551
+ // +`OnCached: ${this.accessory.context.On}`);
552
+ // }
553
+ }
554
+
555
+ async BLEpushBrightnessChanges(): Promise<void> {
556
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushBrightnessChanges`);
557
+ //if (this.Brightness !== this.accessory.context.Brightness) {
558
+ const switchbot = await this.platform.connectBLE();
559
+ // Convert to BLE Address
560
+ this.device.bleMac = this.device
561
+ .deviceId!.match(/.{1,2}/g)!
562
+ .join(':')
563
+ .toLowerCase();
564
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
565
+ switchbot
566
+ .discover({
567
+ model: 'u',
568
+ id: this.device.bleMac,
569
+ })
570
+ .then((device_list: any) => {
571
+ this.infoLog(`${this.accessory.displayName} Target Brightness: ${this.Brightness}`);
572
+ return device_list[0].setBrightness(this.Brightness);
573
+ })
574
+ .then(() => {
575
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
576
+ this.On = false;
577
+ })
578
+ .catch(async (e: any) => {
579
+ this.apiError(e);
580
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushBrightnessChanges with ${this.device.connectionType}`
581
+ + ` Connection, Error Message: ${superStringify(e.message)}`);
582
+ await this.BLEPushConnection();
583
+ });
584
+ // } else {
585
+ // this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushBrightnessChanges.` + `Brightness: ${this.Brightness}, `
586
+ // +`BrightnessCached: ${this.accessory.context.Brightness}`);
587
+ //}
588
+ }
589
+
590
+ async BLEpushColorTemperatureChanges(): Promise<void> {
591
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushColorTemperatureChanges`);
592
+ if (this.ColorTemperature !== this.accessory.context.ColorTemperature) {
593
+ const switchbot = await this.platform.connectBLE();
594
+ // Convert to BLE Address
595
+ this.device.bleMac = this.device
596
+ .deviceId!.match(/.{1,2}/g)!
597
+ .join(':')
598
+ .toLowerCase();
599
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
600
+ switchbot
601
+ .discover({
602
+ model: 'u',
603
+ id: this.device.bleMac,
604
+ })
605
+ .then((device_list: any) => {
606
+ this.infoLog(`${this.accessory.displayName} Target ColorTemperature: ${this.ColorTemperature}`);
607
+ return device_list[0].setColorTemperature(this.ColorTemperature);
608
+ })
609
+ .then(() => {
610
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
611
+ this.On = false;
612
+ })
613
+ .catch(async (e: any) => {
614
+ this.apiError(e);
615
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushColorTemperatureChanges with `
616
+ + `${this.device.connectionType} Connection, Error Message: ${superStringify(e.message)}`);
617
+ await this.BLEPushConnection();
618
+ });
619
+ } else {
620
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushColorTemperatureChanges.` +
621
+ `ColorTemperature: ${this.ColorTemperature}, ColorTemperatureCached: ${this.accessory.context.ColorTemperature}`);
622
+
623
+ }
624
+ }
625
+
626
+ async BLEpushRGBChanges(): Promise<void> {
627
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushRGBChanges`);
628
+ if (this.Hue !== this.accessory.context.Hue || this.Saturation !== this.accessory.context.Saturation) {
629
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${superStringify(this.Hue)}`);
630
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${superStringify(this.Saturation)}`);
631
+
632
+ const [red, green, blue] = hs2rgb(Number(this.Hue), Number(this.Saturation));
633
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} rgb: ${superStringify([red, green, blue])}`);
634
+
478
635
  const switchbot = await this.platform.connectBLE();
479
636
  // Convert to BLE Address
480
637
  this.device.bleMac = this.device
@@ -488,8 +645,8 @@ export class ColorBulb {
488
645
  id: this.device.bleMac,
489
646
  })
490
647
  .then((device_list: any) => {
491
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
492
- return this.turnOnOff(device_list);
648
+ this.infoLog(`${this.accessory.displayName} Target RGB: ${this.Brightness, red, green, blue}`);
649
+ return device_list[0].setRGB(this.Brightness, red, green, blue);
493
650
  })
494
651
  .then(() => {
495
652
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
@@ -497,15 +654,13 @@ export class ColorBulb {
497
654
  })
498
655
  .catch(async (e: any) => {
499
656
  this.apiError(e);
500
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushChanges with ${this.device.connectionType}`
657
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushRGBChanges with ${this.device.connectionType}`
501
658
  + ` Connection, Error Message: ${superStringify(e.message)}`);
502
659
  await this.BLEPushConnection();
503
660
  });
504
661
  } else {
505
- this.debugLog(
506
- `${this.device.deviceType}: ${this.accessory.displayName} No BLEpushChanges.` + `On: ${this.On}, `
507
- +`OnCached: ${this.accessory.context.On}`,
508
- );
662
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushRGBChanges. Hue: ${this.Hue}, `
663
+ + `HueCached: ${this.accessory.context.Hue}, Saturation: ${this.Saturation}, SaturationCached: ${this.accessory.context.Saturation}`);
509
664
  }
510
665
  }
511
666
 
@@ -910,6 +1065,16 @@ export class ColorBulb {
910
1065
  }
911
1066
  }
912
1067
 
1068
+ async stopScanning({ switchbot }: { switchbot: any; }): Promise<void> {
1069
+ switchbot.stopScan();
1070
+ if (this.connected) {
1071
+ this.BLEparseStatus();
1072
+ this.updateHomeKitCharacteristics();
1073
+ } else {
1074
+ await this.BLERefreshConnection(switchbot);
1075
+ }
1076
+ }
1077
+
913
1078
  async getCustomBLEAddress(switchbot: any) {
914
1079
  if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
915
1080
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} customBLEaddress: ${this.device.customBLEaddress}`);
@@ -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: any) {
454
+ switchbot.stopScan();
455
+ if (this.connected) {
456
+ this.BLEparseStatus();
457
+ 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
+ this.BLEparseStatus();
813
+ 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
+ this.BLEparseStatus();
772
+ 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 () => {
@@ -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
+ this.BLEparseStatus();
495
+ 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
+ this.BLEparseStatus();
494
+ 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 () => {