@switchbot/homebridge-switchbot 3.5.0 → 3.5.1-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (137) hide show
  1. package/.github/npm-version-script.cjs +81 -0
  2. package/config.schema.json +4 -4
  3. package/dist/device/blindtilt.d.ts.map +1 -1
  4. package/dist/device/blindtilt.js +64 -189
  5. package/dist/device/blindtilt.js.map +1 -1
  6. package/dist/device/bot.d.ts.map +1 -1
  7. package/dist/device/bot.js +34 -68
  8. package/dist/device/bot.js.map +1 -1
  9. package/dist/device/ceilinglight.d.ts +1 -1
  10. package/dist/device/ceilinglight.d.ts.map +1 -1
  11. package/dist/device/ceilinglight.js +65 -158
  12. package/dist/device/ceilinglight.js.map +1 -1
  13. package/dist/device/colorbulb.d.ts +1 -1
  14. package/dist/device/colorbulb.d.ts.map +1 -1
  15. package/dist/device/colorbulb.js +47 -159
  16. package/dist/device/colorbulb.js.map +1 -1
  17. package/dist/device/contact.d.ts.map +1 -1
  18. package/dist/device/contact.js +42 -105
  19. package/dist/device/contact.js.map +1 -1
  20. package/dist/device/curtain.d.ts +1 -0
  21. package/dist/device/curtain.d.ts.map +1 -1
  22. package/dist/device/curtain.js +61 -182
  23. package/dist/device/curtain.js.map +1 -1
  24. package/dist/device/device.d.ts +41 -2
  25. package/dist/device/device.d.ts.map +1 -1
  26. package/dist/device/device.js +182 -5
  27. package/dist/device/device.js.map +1 -1
  28. package/dist/device/fan.d.ts.map +1 -1
  29. package/dist/device/fan.js +76 -145
  30. package/dist/device/fan.js.map +1 -1
  31. package/dist/device/hub.d.ts +7 -1
  32. package/dist/device/hub.d.ts.map +1 -1
  33. package/dist/device/hub.js +54 -17
  34. package/dist/device/hub.js.map +1 -1
  35. package/dist/device/humidifier.d.ts.map +1 -1
  36. package/dist/device/humidifier.js +55 -104
  37. package/dist/device/humidifier.js.map +1 -1
  38. package/dist/device/iosensor.d.ts.map +1 -1
  39. package/dist/device/iosensor.js +27 -46
  40. package/dist/device/iosensor.js.map +1 -1
  41. package/dist/device/lightstrip.d.ts +1 -1
  42. package/dist/device/lightstrip.d.ts.map +1 -1
  43. package/dist/device/lightstrip.js +56 -120
  44. package/dist/device/lightstrip.js.map +1 -1
  45. package/dist/device/lock.d.ts +1 -1
  46. package/dist/device/lock.d.ts.map +1 -1
  47. package/dist/device/lock.js +36 -66
  48. package/dist/device/lock.js.map +1 -1
  49. package/dist/device/meter.d.ts.map +1 -1
  50. package/dist/device/meter.js +27 -46
  51. package/dist/device/meter.js.map +1 -1
  52. package/dist/device/meterplus.d.ts.map +1 -1
  53. package/dist/device/meterplus.js +26 -45
  54. package/dist/device/meterplus.js.map +1 -1
  55. package/dist/device/motion.d.ts.map +1 -1
  56. package/dist/device/motion.js +24 -43
  57. package/dist/device/motion.js.map +1 -1
  58. package/dist/device/plug.d.ts.map +1 -1
  59. package/dist/device/plug.js +32 -66
  60. package/dist/device/plug.js.map +1 -1
  61. package/dist/device/robotvacuumcleaner.d.ts +1 -1
  62. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  63. package/dist/device/robotvacuumcleaner.js +43 -88
  64. package/dist/device/robotvacuumcleaner.js.map +1 -1
  65. package/dist/device/waterdetector.d.ts.map +1 -1
  66. package/dist/device/waterdetector.js +23 -43
  67. package/dist/device/waterdetector.js.map +1 -1
  68. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  69. package/dist/irdevice/airconditioner.js +10 -28
  70. package/dist/irdevice/airconditioner.js.map +1 -1
  71. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  72. package/dist/irdevice/airpurifier.js +8 -20
  73. package/dist/irdevice/airpurifier.js.map +1 -1
  74. package/dist/irdevice/camera.d.ts.map +1 -1
  75. package/dist/irdevice/camera.js +8 -20
  76. package/dist/irdevice/camera.js.map +1 -1
  77. package/dist/irdevice/fan.d.ts.map +1 -1
  78. package/dist/irdevice/fan.js +8 -20
  79. package/dist/irdevice/fan.js.map +1 -1
  80. package/dist/irdevice/irdevice.d.ts +7 -0
  81. package/dist/irdevice/irdevice.d.ts.map +1 -1
  82. package/dist/irdevice/irdevice.js +28 -0
  83. package/dist/irdevice/irdevice.js.map +1 -1
  84. package/dist/irdevice/light.d.ts.map +1 -1
  85. package/dist/irdevice/light.js +8 -20
  86. package/dist/irdevice/light.js.map +1 -1
  87. package/dist/irdevice/other.d.ts.map +1 -1
  88. package/dist/irdevice/other.js +8 -20
  89. package/dist/irdevice/other.js.map +1 -1
  90. package/dist/irdevice/tv.d.ts.map +1 -1
  91. package/dist/irdevice/tv.js +8 -20
  92. package/dist/irdevice/tv.js.map +1 -1
  93. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  94. package/dist/irdevice/vacuumcleaner.js +8 -20
  95. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  96. package/dist/irdevice/waterheater.d.ts.map +1 -1
  97. package/dist/irdevice/waterheater.js +8 -20
  98. package/dist/irdevice/waterheater.js.map +1 -1
  99. package/dist/settings.d.ts +4 -2
  100. package/dist/settings.d.ts.map +1 -1
  101. package/dist/settings.js.map +1 -1
  102. package/dist/utils.d.ts +19 -1
  103. package/dist/utils.d.ts.map +1 -1
  104. package/dist/utils.js +20 -1
  105. package/dist/utils.js.map +1 -1
  106. package/package.json +8 -8
  107. package/src/device/blindtilt.ts +71 -186
  108. package/src/device/bot.ts +32 -68
  109. package/src/device/ceilinglight.ts +67 -153
  110. package/src/device/colorbulb.ts +51 -155
  111. package/src/device/contact.ts +49 -104
  112. package/src/device/curtain.ts +70 -177
  113. package/src/device/device.ts +207 -8
  114. package/src/device/fan.ts +75 -147
  115. package/src/device/hub.ts +55 -19
  116. package/src/device/humidifier.ts +52 -104
  117. package/src/device/iosensor.ts +25 -46
  118. package/src/device/lightstrip.ts +54 -121
  119. package/src/device/lock.ts +35 -67
  120. package/src/device/meter.ts +25 -46
  121. package/src/device/meterplus.ts +24 -45
  122. package/src/device/motion.ts +22 -43
  123. package/src/device/plug.ts +30 -66
  124. package/src/device/robotvacuumcleaner.ts +40 -89
  125. package/src/device/waterdetector.ts +22 -43
  126. package/src/irdevice/airconditioner.ts +10 -26
  127. package/src/irdevice/airpurifier.ts +8 -20
  128. package/src/irdevice/camera.ts +8 -20
  129. package/src/irdevice/fan.ts +8 -20
  130. package/src/irdevice/irdevice.ts +29 -0
  131. package/src/irdevice/light.ts +8 -20
  132. package/src/irdevice/other.ts +8 -20
  133. package/src/irdevice/tv.ts +8 -20
  134. package/src/irdevice/vacuumcleaner.ts +8 -20
  135. package/src/irdevice/waterheater.ts +8 -20
  136. package/src/settings.ts +4 -2
  137. package/src/utils.ts +20 -1
@@ -2,15 +2,13 @@
2
2
  *
3
3
  * blindtilt.ts: @switchbot/homebridge-switchbot.
4
4
  */
5
- import { request } from 'undici';
6
5
  import { deviceBase } from './device.js';
7
6
  import { interval, Subject } from 'rxjs';
8
- import { Devices } from '../settings.js';
9
7
  import { hs2rgb, rgb2hs, m2hs } from '../utils.js';
10
8
  import { debounceTime, skipWhile, take, tap } from 'rxjs/operators';
11
9
 
12
10
  import type { SwitchBotPlatform } from '../platform.js';
13
- import type { device, devicesConfig, deviceStatus, serviceData } from '../settings.js';
11
+ import type { device, devicesConfig, serviceData, deviceStatus } from '../settings.js';
14
12
  import type { Service, PlatformAccessory, CharacteristicValue, ControllerConstructor, Controller, ControllerServiceMap } from 'homebridge';
15
13
 
16
14
  /**
@@ -308,63 +306,43 @@ export class ColorBulb extends deviceBase {
308
306
 
309
307
  async BLERefreshStatus(): Promise<void> {
310
308
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLERefreshStatus`);
311
- const switchbot = await this.platform.connectBLE();
312
- // Convert to BLE Address
313
- this.device.bleMac = this.device
314
- .deviceId!.match(/.{1,2}/g)!
315
- .join(':')
316
- .toLowerCase();
317
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
318
- this.getCustomBLEAddress(switchbot);
309
+ const switchbot = await this.switchbotBLE();
310
+
311
+ if (switchbot === undefined) {
312
+ await this.BLERefreshConnection(switchbot);
313
+ } else {
319
314
  // Start to monitor advertisement packets
320
- (async () => {
315
+ (async () => {
321
316
  // Start to monitor advertisement packets
322
- await switchbot.startScan({ model: this.device.bleModel, id: this.device.bleMac });
323
- // Set an event handler
324
- switchbot.onadvertisement = (ad: any) => {
325
- if (this.device.bleMac === ad.address && ad.model === this.device.bleModel) {
326
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ${JSON.stringify(ad, null, ' ')}`);
327
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} address: ${ad.address}, model: ${ad.model}`);
328
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
317
+ const serviceData: serviceData = await this.monitorAdvertisementPackets(switchbot);
318
+ // Update HomeKit
319
+ if (serviceData.model !== '' && serviceData.modelName !== '') {
320
+ await this.BLEparseStatus(serviceData);
321
+ await this.updateHomeKitCharacteristics();
329
322
  } else {
330
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
323
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed to get serviceData, serviceData: ${serviceData}`);
324
+ await this.BLERefreshConnection(switchbot);
331
325
  }
332
- };
333
- // Wait 10 seconds
334
- await switchbot.wait(this.scanDuration * 1000);
335
- // Stop to monitor
336
- await switchbot.stopScan();
337
- // Update HomeKit
338
- await this.BLEparseStatus(switchbot.onadvertisement.serviceData);
339
- await this.updateHomeKitCharacteristics();
340
- })();
341
- if (switchbot === undefined) {
342
- await this.BLERefreshConnection(switchbot);
326
+ })();
343
327
  }
344
328
  }
345
329
 
346
330
  async openAPIRefreshStatus(): Promise<void> {
347
331
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
348
332
  try {
349
- const { body, statusCode } = await this.platform.retryRequest(this.deviceMaxRetries, this.deviceDelayBetweenRetries,
350
- `${Devices}/${this.device.deviceId}/status`, { headers: this.platform.generateHeaders() });
351
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
333
+ const { body, statusCode } = await this.deviceRefreshStatus();
352
334
  const deviceStatus: any = await body.json();
353
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
354
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
335
+ await this.refreshStatusCodes(statusCode, deviceStatus);;
355
336
  if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
356
- this.debugSuccessLog(`${this.device.deviceType}: ${this.accessory.displayName} `
357
- + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
358
- this.openAPIparseStatus(deviceStatus);
359
- this.updateHomeKitCharacteristics();
337
+ await this.successfulRefreshStatus(statusCode, deviceStatus);
338
+ await this.openAPIparseStatus(deviceStatus);
339
+ await this.updateHomeKitCharacteristics();
360
340
  } else {
361
- this.statusCode(statusCode);
362
- this.statusCode(deviceStatus.statusCode);
341
+ await this.statusCodes(statusCode, deviceStatus);
363
342
  }
364
343
  } catch (e: any) {
365
- this.apiError(e);
366
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}`
367
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
344
+ await this.apiError(e);
345
+ await this.openAPIRefreshError(e);
368
346
  }
369
347
  }
370
348
 
@@ -461,8 +439,7 @@ export class ColorBulb extends deviceBase {
461
439
  await this.openAPIpushChanges();
462
440
  } else {
463
441
  await this.offlineOff();
464
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
465
- + ` ${this.device.connectionType}, pushChanges will not happen.`);
442
+ await this.pushChangeDisabled();
466
443
  }
467
444
  // Refresh the status from the API
468
445
  interval(15000)
@@ -480,16 +457,9 @@ export class ColorBulb extends deviceBase {
480
457
  + ` On: ${this.LightBulb.On}, OnCached: ${this.accessory.context.On}`);
481
458
  const switchbot = await this.platform.connectBLE();
482
459
  // Convert to BLE Address
483
- this.device.bleMac = this.device
484
- .deviceId!.match(/.{1,2}/g)!
485
- .join(':')
486
- .toLowerCase();
487
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
460
+ await this.convertBLEAddress();
488
461
  switchbot
489
- .discover({
490
- model: 'u',
491
- id: this.device.bleMac,
492
- })
462
+ .discover({ model: this.device.bleModel, id: this.device.bleMac })
493
463
  .then(async (device_list: any) => {
494
464
  this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LightBulb.On}`);
495
465
  return await this.retryBLE({
@@ -527,6 +497,7 @@ export class ColorBulb extends deviceBase {
527
497
  if (this.LightBulb.On) {
528
498
  await this.BLEpushRGBChanges();
529
499
  }
500
+ await this.updateHomeKitCharacteristics();
530
501
  } else {
531
502
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushChanges, On: ${this.LightBulb.On},`
532
503
  + ` OnCached: ${this.accessory.context.On}`);
@@ -538,16 +509,9 @@ export class ColorBulb extends deviceBase {
538
509
  if (this.LightBulb.Brightness !== this.accessory.context.Brightness) {
539
510
  const switchbot = await this.platform.connectBLE();
540
511
  // Convert to BLE Address
541
- this.device.bleMac = this.device
542
- .deviceId!.match(/.{1,2}/g)!
543
- .join(':')
544
- .toLowerCase();
545
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
512
+ await this.convertBLEAddress();
546
513
  switchbot
547
- .discover({
548
- model: 'u',
549
- id: this.device.bleMac,
550
- })
514
+ .discover({ model: this.device.bleModel, id: this.device.bleMac })
551
515
  .then(async (device_list: any) => {
552
516
  this.infoLog(`${this.accessory.displayName} Target Brightness: ${this.LightBulb.Brightness}`);
553
517
  return await device_list[0].setBrightness(this.LightBulb.Brightness);
@@ -573,16 +537,9 @@ export class ColorBulb extends deviceBase {
573
537
  if (this.LightBulb.ColorTemperature !== this.accessory.context.ColorTemperature) {
574
538
  const switchbot = await this.platform.connectBLE();
575
539
  // Convert to BLE Address
576
- this.device.bleMac = this.device
577
- .deviceId!.match(/.{1,2}/g)!
578
- .join(':')
579
- .toLowerCase();
580
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
540
+ await this.convertBLEAddress();
581
541
  switchbot
582
- .discover({
583
- model: 'u',
584
- id: this.device.bleMac,
585
- })
542
+ .discover({ model: this.device.bleModel, id: this.device.bleMac })
586
543
  .then(async (device_list: any) => {
587
544
  this.infoLog(`${this.accessory.displayName} Target ColorTemperature: ${this.LightBulb.ColorTemperature}`);
588
545
  return await device_list[0].setColorTemperature(this.LightBulb.ColorTemperature);
@@ -614,16 +571,9 @@ export class ColorBulb extends deviceBase {
614
571
 
615
572
  const switchbot = await this.platform.connectBLE();
616
573
  // Convert to BLE Address
617
- this.device.bleMac = this.device
618
- .deviceId!.match(/.{1,2}/g)!
619
- .join(':')
620
- .toLowerCase();
621
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
574
+ await this.convertBLEAddress();
622
575
  switchbot
623
- .discover({
624
- model: 'u',
625
- id: this.device.bleMac,
626
- })
576
+ .discover({ model: this.device.bleModel, id: this.device.bleMac })
627
577
  .then(async (device_list: any) => {
628
578
  this.infoLog(`${this.accessory.displayName} Target RGB: ${(this.LightBulb.Brightness, red, green, blue)}`);
629
579
  return await device_list[0].setRGB(this.LightBulb.Brightness, red, green, blue);
@@ -660,16 +610,9 @@ export class ColorBulb extends deviceBase {
660
610
  });
661
611
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
662
612
  try {
663
- const { body, statusCode } = await request(`${Devices}/${this.device.deviceId}/commands`, {
664
- body: bodyChange,
665
- method: 'POST',
666
- headers: this.platform.generateHeaders(),
667
- });
668
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
613
+ const { body, statusCode } = await this.pushChangeRequest(bodyChange);
669
614
  const deviceStatus: any = await body.json();
670
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
671
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus body: ${JSON.stringify(deviceStatus.body)}`);
672
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
615
+ await this.pushStatusCodes(statusCode, deviceStatus);
673
616
  if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
674
617
  this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
675
618
  + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
@@ -716,16 +659,9 @@ export class ColorBulb extends deviceBase {
716
659
  });
717
660
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
718
661
  try {
719
- const { body, statusCode } = await request(`${Devices}/${this.device.deviceId}/commands`, {
720
- body: bodyChange,
721
- method: 'POST',
722
- headers: this.platform.generateHeaders(),
723
- });
724
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
662
+ const { body, statusCode } = await this.pushChangeRequest(bodyChange);
725
663
  const deviceStatus: any = await body.json();
726
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
727
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus body: ${JSON.stringify(deviceStatus.body)}`);
728
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
664
+ await this.pushStatusCodes(statusCode, deviceStatus);
729
665
  if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
730
666
  this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
731
667
  + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
@@ -758,16 +694,9 @@ export class ColorBulb extends deviceBase {
758
694
  });
759
695
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
760
696
  try {
761
- const { body, statusCode } = await request(`${Devices}/${this.device.deviceId}/commands`, {
762
- body: bodyChange,
763
- method: 'POST',
764
- headers: this.platform.generateHeaders(),
765
- });
766
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
697
+ const { body, statusCode } = await this.pushChangeRequest(bodyChange);
767
698
  const deviceStatus: any = await body.json();
768
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
769
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus body: ${JSON.stringify(deviceStatus.body)}`);
770
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
699
+ await this.pushStatusCodes(statusCode, deviceStatus);
771
700
  if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
772
701
  this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
773
702
  + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
@@ -798,16 +727,9 @@ export class ColorBulb extends deviceBase {
798
727
  });
799
728
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
800
729
  try {
801
- const { body, statusCode } = await request(`${Devices}/${this.device.deviceId}/commands`, {
802
- body: bodyChange,
803
- method: 'POST',
804
- headers: this.platform.generateHeaders(),
805
- });
806
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
730
+ const { body, statusCode } = await this.pushChangeRequest(bodyChange);
807
731
  const deviceStatus: any = await body.json();
808
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
809
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus body: ${JSON.stringify(deviceStatus.body)}`);
810
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
732
+ await this.pushStatusCodes(statusCode, deviceStatus);
811
733
  if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
812
734
  this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
813
735
  + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
@@ -929,46 +851,20 @@ export class ColorBulb extends deviceBase {
929
851
 
930
852
  async updateHomeKitCharacteristics(): Promise<void> {
931
853
  // On
932
- if (this.LightBulb.On === undefined) {
933
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LightBulb.On}`);
934
- } else {
935
- this.accessory.context.On = this.LightBulb.On;
936
- this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.On, this.LightBulb.On);
937
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic On: ${this.LightBulb.On}`);
938
- }
854
+ await this.updateCharacteristic(this.LightBulb.Service, this.hap.Characteristic.On,
855
+ this.LightBulb.On, 'On');
939
856
  // Brightness
940
- if (this.LightBulb.Brightness === undefined) {
941
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Brightness: ${this.LightBulb.Brightness}`);
942
- } else {
943
- this.accessory.context.Brightness = this.LightBulb.Brightness;
944
- this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Brightness, this.LightBulb.Brightness);
945
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Brightness: ${this.LightBulb.Brightness}`);
946
- }
857
+ await this.updateCharacteristic(this.LightBulb.Service, this.hap.Characteristic.Brightness,
858
+ this.LightBulb.Brightness, 'Brightness');
947
859
  // ColorTemperature
948
- if (this.LightBulb.ColorTemperature === undefined) {
949
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ColorTemperature: ${this.LightBulb.ColorTemperature}`);
950
- } else {
951
- this.accessory.context.ColorTemperature = this.LightBulb.ColorTemperature;
952
- this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.ColorTemperature, this.LightBulb.ColorTemperature);
953
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
954
- + ` ColorTemperature: ${this.LightBulb.ColorTemperature}`);
955
- }
860
+ await this.updateCharacteristic(this.LightBulb.Service, this.hap.Characteristic.ColorTemperature,
861
+ this.LightBulb.ColorTemperature, 'ColorTemperature');
956
862
  // Hue
957
- if (this.LightBulb.Hue === undefined) {
958
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${this.LightBulb.Hue}`);
959
- } else {
960
- this.accessory.context.Hue = this.LightBulb.Hue;
961
- this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Hue, this.LightBulb.Hue);
962
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Hue: ${this.LightBulb.Hue}`);
963
- }
863
+ await this.updateCharacteristic(this.LightBulb.Service, this.hap.Characteristic.Hue,
864
+ this.LightBulb.Hue, 'Hue');
964
865
  // Saturation
965
- if (this.LightBulb.Saturation === undefined) {
966
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${this.LightBulb.Saturation}`);
967
- } else {
968
- this.accessory.context.Saturation = this.LightBulb.Saturation;
969
- this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Saturation, this.LightBulb.Saturation);
970
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Saturation: ${this.LightBulb.Saturation}`);
971
- }
866
+ await this.updateCharacteristic(this.LightBulb.Service, this.hap.Characteristic.Saturation,
867
+ this.LightBulb.Saturation, 'Saturation');
972
868
  }
973
869
 
974
870
  async adaptiveLighting(device: device & devicesConfig): Promise<void> {
@@ -4,7 +4,6 @@
4
4
  */
5
5
  import { deviceBase } from './device.js';
6
6
  import { interval, Subject } from 'rxjs';
7
- import { Devices } from '../settings.js';
8
7
  import { skipWhile } from 'rxjs/operators';
9
8
 
10
9
  import type { SwitchBotPlatform } from '../platform.js';
@@ -219,9 +218,6 @@ export class Contact extends deviceBase {
219
218
  }
220
219
  }
221
220
  // Battery
222
- if (serviceData.battery === undefined) {
223
- serviceData.battery === 100;
224
- }
225
221
  this.Battery.BatteryLevel = Number(serviceData.battery);
226
222
  if (this.Battery.BatteryLevel < 10) {
227
223
  this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
@@ -265,9 +261,6 @@ export class Contact extends deviceBase {
265
261
  + ` CurrentAmbientLightLevel: ${this.LightSensor!.CurrentAmbientLightLevel}`);
266
262
  }
267
263
  // Battery
268
- if (deviceStatus.body.battery === undefined) {
269
- deviceStatus.body.battery === 100;
270
- }
271
264
  this.Battery.BatteryLevel = Number(deviceStatus.body.battery);
272
265
  if (this.Battery.BatteryLevel < 10) {
273
266
  this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
@@ -312,66 +305,43 @@ export class Contact extends deviceBase {
312
305
 
313
306
  async BLERefreshStatus(): Promise<void> {
314
307
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLERefreshStatus`);
315
- const switchbot = await this.platform.connectBLE();
316
- // Convert to BLE Address
317
- this.device.bleMac = this.device
318
- .deviceId!.match(/.{1,2}/g)!
319
- .join(':')
320
- .toLowerCase();
321
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
322
- this.getCustomBLEAddress(switchbot);
308
+ const switchbot = await this.switchbotBLE();
309
+
310
+ if (switchbot === undefined) {
311
+ await this.BLERefreshConnection(switchbot);
312
+ } else {
323
313
  // Start to monitor advertisement packets
324
- (async () => {
314
+ (async () => {
325
315
  // Start to monitor advertisement packets
326
- await switchbot.startScan({ model: this.device.bleModel, id: this.device.bleMac });
327
- // Set an event handler
328
- switchbot.onadvertisement = (ad: any) => {
329
- if (this.device.bleMac === ad.address && ad.model === this.device.bleModel) {
330
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ${JSON.stringify(ad, null, ' ')}`);
331
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} address: ${ad.address}, model: ${ad.model}`);
332
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
316
+ const serviceData: serviceData = await this.monitorAdvertisementPackets(switchbot);
317
+ // Update HomeKit
318
+ if (serviceData.model !== '' && serviceData.modelName !== '') {
319
+ await this.BLEparseStatus(serviceData);
320
+ await this.updateHomeKitCharacteristics();
333
321
  } else {
334
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
322
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed to get serviceData, serviceData: ${serviceData}`);
323
+ await this.BLERefreshConnection(switchbot);
335
324
  }
336
- };
337
- // Wait 10 seconds
338
- await switchbot.wait(this.scanDuration * 1000);
339
- // Stop to monitor
340
- await switchbot.stopScan();
341
- // Update HomeKit
342
- await this.BLEparseStatus(switchbot.onadvertisement.serviceData);
343
- await this.updateHomeKitCharacteristics();
344
- })();
345
- if (switchbot === undefined) {
346
- await this.BLERefreshConnection(switchbot);
325
+ })();
347
326
  }
348
327
  }
349
328
 
350
329
  async openAPIRefreshStatus(): Promise<void> {
351
330
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
352
331
  try {
353
- const { body, statusCode } = await this.platform.retryRequest(this.deviceMaxRetries, this.deviceDelayBetweenRetries,
354
- `${Devices}/${this.device.deviceId}/status`, { headers: this.platform.generateHeaders() });
355
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
332
+ const { body, statusCode } = await this.deviceRefreshStatus();
356
333
  const deviceStatus: any = await body.json();
357
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
358
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
334
+ await this.refreshStatusCodes(statusCode, deviceStatus);;
359
335
  if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
360
- this.debugSuccessLog(`${this.device.deviceType}: ${this.accessory.displayName} `
361
- + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
362
- this.openAPIparseStatus(deviceStatus);
363
- this.updateHomeKitCharacteristics();
336
+ await this.successfulRefreshStatus(statusCode, deviceStatus);
337
+ await this.openAPIparseStatus(deviceStatus);
338
+ await this.updateHomeKitCharacteristics();
364
339
  } else {
365
- this.statusCode(statusCode);
366
- this.statusCode(deviceStatus.statusCode);
367
- if (deviceStatus.statusCode === 161 || deviceStatus.statusCode === 171) {
368
- this.offlineOff();
369
- }
340
+ await this.statusCodes(statusCode, deviceStatus);
370
341
  }
371
342
  } catch (e: any) {
372
- this.apiError(e);
373
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}`
374
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
343
+ await this.apiError(e);
344
+ await this.openAPIRefreshError(e);
375
345
  }
376
346
  }
377
347
 
@@ -410,50 +380,25 @@ export class Contact extends deviceBase {
410
380
  * Updates the status for each of the HomeKit Characteristics
411
381
  */
412
382
  async updateHomeKitCharacteristics(): Promise<void> {
413
- if (this.ContactSensor.ContactSensorState === undefined) {
414
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ContactSensorState: ${this.ContactSensor.ContactSensorState}`);
415
- } else {
416
- this.accessory.context.ContactSensorState = this.ContactSensor.ContactSensorState;
417
- this.ContactSensor.Service.updateCharacteristic(this.hap.Characteristic.ContactSensorState, this.ContactSensor.ContactSensorState);
418
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
419
- + ` ContactSensorState: ${this.ContactSensor.ContactSensorState}`);
383
+ // ContactSensorState
384
+ await this.updateCharacteristic(this.ContactSensor.Service, this.hap.Characteristic.ContactSensorState,
385
+ this.ContactSensor.ContactSensorState, 'ContactSensorState');
386
+ // MotionDetected
387
+ if (!this.device.contact?.hide_motionsensor && this.MotionSensor?.Service) {
388
+ await this.updateCharacteristic(this.MotionSensor.Service, this.hap.Characteristic.MotionDetected,
389
+ this.MotionSensor.MotionDetected, 'MotionDetected');
420
390
  }
421
- if (!this.device.contact?.hide_motionsensor) {
422
- if (this.MotionSensor!.MotionDetected === undefined) {
423
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MotionDetected: ${this.MotionSensor!.MotionDetected}`);
424
- } else {
425
- this.accessory.context.MotionDetected = this.MotionSensor!.MotionDetected;
426
- this.MotionSensor!.Service.updateCharacteristic(this.hap.Characteristic.MotionDetected, this.MotionSensor!.MotionDetected);
427
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
428
- + ` MotionDetected: ${this.MotionSensor!.MotionDetected}`);
429
- }
430
- }
431
- if (!this.device.contact?.hide_lightsensor) {
432
- if (this.LightSensor?.CurrentAmbientLightLevel === undefined) {
433
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
434
- + ` CurrentAmbientLightLevel: ${this.LightSensor?.CurrentAmbientLightLevel}`);
435
- } else {
436
- this.accessory.context.CurrentAmbientLightLevel = this.LightSensor.CurrentAmbientLightLevel;
437
- this.LightSensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel, this.LightSensor.CurrentAmbientLightLevel);
438
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
439
- + ` updateCharacteristic CurrentAmbientLightLevel: ${this.LightSensor.CurrentAmbientLightLevel}`);
440
- }
441
- }
442
- if (this.Battery.BatteryLevel === undefined) {
443
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel}`);
444
- } else {
445
- this.accessory.context.BatteryLevel = this.Battery.BatteryLevel;
446
- this.Battery.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, this.Battery.BatteryLevel);
447
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.Battery.BatteryLevel}`);
448
- }
449
- if (this.Battery.StatusLowBattery === undefined) {
450
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.Battery.StatusLowBattery}`);
451
- } else {
452
- this.accessory.context.StatusLowBattery = this.Battery.StatusLowBattery;
453
- this.Battery.Service.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, this.Battery.StatusLowBattery);
454
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
455
- + ` StatusLowBattery: ${this.Battery.StatusLowBattery}`);
391
+ // CurrentAmbientLightLevel
392
+ if (!this.device.contact?.hide_lightsensor && this.LightSensor?.Service) {
393
+ await this.updateCharacteristic(this.LightSensor.Service, this.hap.Characteristic.CurrentAmbientLightLevel,
394
+ this.LightSensor.CurrentAmbientLightLevel, 'CurrentAmbientLightLevel');
456
395
  }
396
+ // BatteryLevel
397
+ await this.updateCharacteristic(this.Battery.Service, this.hap.Characteristic.BatteryLevel,
398
+ this.Battery.BatteryLevel, 'BatteryLevel');
399
+ // StatusLowBattery
400
+ await this.updateCharacteristic(this.Battery.Service, this.hap.Characteristic.StatusLowBattery,
401
+ this.Battery.StatusLowBattery, 'StatusLowBattery');
457
402
  }
458
403
 
459
404
  async BLERefreshConnection(switchbot: any): Promise<void> {
@@ -469,11 +414,11 @@ export class Contact extends deviceBase {
469
414
  if (this.device.offline) {
470
415
  this.ContactSensor.Service.updateCharacteristic(this.hap.Characteristic.ContactSensorState,
471
416
  this.hap.Characteristic.ContactSensorState.CONTACT_DETECTED);
472
- if (!this.device.contact?.hide_motionsensor) {
473
- this.MotionSensor!.Service.updateCharacteristic(this.hap.Characteristic.MotionDetected, false);
417
+ if (!this.device.contact?.hide_motionsensor && this.MotionSensor?.Service) {
418
+ this.MotionSensor.Service.updateCharacteristic(this.hap.Characteristic.MotionDetected, false);
474
419
  }
475
- if (!this.device.contact?.hide_lightsensor) {
476
- this.LightSensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel, 100);
420
+ if (!this.device.contact?.hide_lightsensor && this.LightSensor?.Service) {
421
+ this.LightSensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel, 100);
477
422
  }
478
423
  }
479
424
  }
@@ -481,13 +426,13 @@ export class Contact extends deviceBase {
481
426
  async apiError(e: any): Promise<void> {
482
427
  this.ContactSensor.Service.updateCharacteristic(this.hap.Characteristic.ContactSensorState, e);
483
428
  this.ContactSensor.Service.updateCharacteristic(this.hap.Characteristic.StatusActive, e);
484
- if (!this.device.contact?.hide_motionsensor) {
485
- this.MotionSensor!.Service.updateCharacteristic(this.hap.Characteristic.MotionDetected, e);
486
- this.MotionSensor!.Service.updateCharacteristic(this.hap.Characteristic.StatusActive, e);
429
+ if (!this.device.contact?.hide_motionsensor && this.MotionSensor?.Service) {
430
+ this.MotionSensor.Service.updateCharacteristic(this.hap.Characteristic.MotionDetected, e);
431
+ this.MotionSensor.Service.updateCharacteristic(this.hap.Characteristic.StatusActive, e);
487
432
  }
488
- if (!this.device.contact?.hide_lightsensor) {
489
- this.LightSensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel, e);
490
- this.LightSensor!.Service.updateCharacteristic(this.hap.Characteristic.StatusActive, e);
433
+ if (!this.device.contact?.hide_lightsensor && this.LightSensor?.Service) {
434
+ this.LightSensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel, e);
435
+ this.LightSensor.Service.updateCharacteristic(this.hap.Characteristic.StatusActive, e);
491
436
  }
492
437
  this.Battery.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, e);
493
438
  this.Battery.Service.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, e);