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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (134) 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 +32 -63
  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 +31 -63
  8. package/dist/device/bot.js.map +1 -1
  9. package/dist/device/ceilinglight.d.ts.map +1 -1
  10. package/dist/device/ceilinglight.js +51 -111
  11. package/dist/device/ceilinglight.js.map +1 -1
  12. package/dist/device/colorbulb.d.ts +1 -1
  13. package/dist/device/colorbulb.d.ts.map +1 -1
  14. package/dist/device/colorbulb.js +38 -113
  15. package/dist/device/colorbulb.js.map +1 -1
  16. package/dist/device/contact.d.ts.map +1 -1
  17. package/dist/device/contact.js +22 -49
  18. package/dist/device/contact.js.map +1 -1
  19. package/dist/device/curtain.d.ts.map +1 -1
  20. package/dist/device/curtain.js +53 -61
  21. package/dist/device/curtain.js.map +1 -1
  22. package/dist/device/device.d.ts +17 -1
  23. package/dist/device/device.d.ts.map +1 -1
  24. package/dist/device/device.js +112 -5
  25. package/dist/device/device.js.map +1 -1
  26. package/dist/device/fan.d.ts.map +1 -1
  27. package/dist/device/fan.js +42 -95
  28. package/dist/device/fan.js.map +1 -1
  29. package/dist/device/hub.d.ts +7 -1
  30. package/dist/device/hub.d.ts.map +1 -1
  31. package/dist/device/hub.js +52 -14
  32. package/dist/device/hub.js.map +1 -1
  33. package/dist/device/humidifier.d.ts.map +1 -1
  34. package/dist/device/humidifier.js +52 -99
  35. package/dist/device/humidifier.js.map +1 -1
  36. package/dist/device/iosensor.d.ts.map +1 -1
  37. package/dist/device/iosensor.js +25 -43
  38. package/dist/device/iosensor.js.map +1 -1
  39. package/dist/device/lightstrip.d.ts +1 -1
  40. package/dist/device/lightstrip.d.ts.map +1 -1
  41. package/dist/device/lightstrip.js +53 -115
  42. package/dist/device/lightstrip.js.map +1 -1
  43. package/dist/device/lock.d.ts +1 -1
  44. package/dist/device/lock.d.ts.map +1 -1
  45. package/dist/device/lock.js +33 -61
  46. package/dist/device/lock.js.map +1 -1
  47. package/dist/device/meter.d.ts.map +1 -1
  48. package/dist/device/meter.js +25 -43
  49. package/dist/device/meter.js.map +1 -1
  50. package/dist/device/meterplus.d.ts.map +1 -1
  51. package/dist/device/meterplus.js +24 -42
  52. package/dist/device/meterplus.js.map +1 -1
  53. package/dist/device/motion.d.ts.map +1 -1
  54. package/dist/device/motion.js +22 -40
  55. package/dist/device/motion.js.map +1 -1
  56. package/dist/device/plug.d.ts.map +1 -1
  57. package/dist/device/plug.js +29 -61
  58. package/dist/device/plug.js.map +1 -1
  59. package/dist/device/robotvacuumcleaner.d.ts +1 -1
  60. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  61. package/dist/device/robotvacuumcleaner.js +36 -77
  62. package/dist/device/robotvacuumcleaner.js.map +1 -1
  63. package/dist/device/waterdetector.d.ts.map +1 -1
  64. package/dist/device/waterdetector.js +21 -40
  65. package/dist/device/waterdetector.js.map +1 -1
  66. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  67. package/dist/irdevice/airconditioner.js +8 -25
  68. package/dist/irdevice/airconditioner.js.map +1 -1
  69. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  70. package/dist/irdevice/airpurifier.js +6 -17
  71. package/dist/irdevice/airpurifier.js.map +1 -1
  72. package/dist/irdevice/camera.d.ts.map +1 -1
  73. package/dist/irdevice/camera.js +6 -17
  74. package/dist/irdevice/camera.js.map +1 -1
  75. package/dist/irdevice/fan.d.ts.map +1 -1
  76. package/dist/irdevice/fan.js +6 -17
  77. package/dist/irdevice/fan.js.map +1 -1
  78. package/dist/irdevice/irdevice.d.ts +6 -0
  79. package/dist/irdevice/irdevice.d.ts.map +1 -1
  80. package/dist/irdevice/irdevice.js +24 -0
  81. package/dist/irdevice/irdevice.js.map +1 -1
  82. package/dist/irdevice/light.d.ts.map +1 -1
  83. package/dist/irdevice/light.js +6 -17
  84. package/dist/irdevice/light.js.map +1 -1
  85. package/dist/irdevice/other.d.ts.map +1 -1
  86. package/dist/irdevice/other.js +6 -17
  87. package/dist/irdevice/other.js.map +1 -1
  88. package/dist/irdevice/tv.d.ts.map +1 -1
  89. package/dist/irdevice/tv.js +6 -17
  90. package/dist/irdevice/tv.js.map +1 -1
  91. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  92. package/dist/irdevice/vacuumcleaner.js +6 -17
  93. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  94. package/dist/irdevice/waterheater.d.ts.map +1 -1
  95. package/dist/irdevice/waterheater.js +6 -17
  96. package/dist/irdevice/waterheater.js.map +1 -1
  97. package/dist/settings.d.ts +2 -1
  98. package/dist/settings.d.ts.map +1 -1
  99. package/dist/utils.d.ts +19 -1
  100. package/dist/utils.d.ts.map +1 -1
  101. package/dist/utils.js +20 -1
  102. package/dist/utils.js.map +1 -1
  103. package/package.json +8 -8
  104. package/src/device/blindtilt.ts +30 -63
  105. package/src/device/bot.ts +29 -63
  106. package/src/device/ceilinglight.ts +48 -111
  107. package/src/device/colorbulb.ts +37 -114
  108. package/src/device/contact.ts +20 -49
  109. package/src/device/curtain.ts +52 -62
  110. package/src/device/device.ts +124 -7
  111. package/src/device/fan.ts +40 -97
  112. package/src/device/hub.ts +53 -16
  113. package/src/device/humidifier.ts +49 -99
  114. package/src/device/iosensor.ts +23 -43
  115. package/src/device/lightstrip.ts +51 -116
  116. package/src/device/lock.ts +32 -62
  117. package/src/device/meter.ts +23 -43
  118. package/src/device/meterplus.ts +22 -42
  119. package/src/device/motion.ts +20 -40
  120. package/src/device/plug.ts +27 -61
  121. package/src/device/robotvacuumcleaner.ts +33 -78
  122. package/src/device/waterdetector.ts +20 -40
  123. package/src/irdevice/airconditioner.ts +8 -23
  124. package/src/irdevice/airpurifier.ts +6 -17
  125. package/src/irdevice/camera.ts +6 -17
  126. package/src/irdevice/fan.ts +6 -17
  127. package/src/irdevice/irdevice.ts +24 -0
  128. package/src/irdevice/light.ts +6 -17
  129. package/src/irdevice/other.ts +6 -17
  130. package/src/irdevice/tv.ts +6 -17
  131. package/src/irdevice/vacuumcleaner.ts +6 -17
  132. package/src/irdevice/waterheater.ts +6 -17
  133. package/src/settings.ts +2 -1
  134. package/src/utils.ts +20 -1
@@ -2,10 +2,8 @@
2
2
  *
3
3
  * ceilinglight.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
 
@@ -282,58 +280,38 @@ export class CeilingLight extends deviceBase {
282
280
 
283
281
  async BLERefreshStatus(): Promise<void> {
284
282
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLERefreshStatus`);
285
- const switchbot = await this.platform.connectBLE();
286
- // Convert to BLE Address
287
- this.device.bleMac = this.device
288
- .deviceId!.match(/.{1,2}/g)!
289
- .join(':')
290
- .toLowerCase();
291
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
292
- this.getCustomBLEAddress(switchbot);
293
- // Start to monitor advertisement packets
294
- (async () => {
283
+ const switchbot = await this.switchbotBLE();
284
+
285
+ if (switchbot === undefined) {
286
+ await this.BLERefreshConnection(switchbot);
287
+ } else {
288
+ (async () => {
295
289
  // Start to monitor advertisement packets
296
- await switchbot.startScan({ model: this.device.bleModel, id: this.device.bleMac });
297
- // Set an event handler
298
- switchbot.onadvertisement = (ad: any) => {
299
- if (this.device.bleMac === ad.address && ad.model === this.device.bleModel) {
300
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ${JSON.stringify(ad, null, ' ')}`);
301
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} address: ${ad.address}, model: ${ad.model}`);
302
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
290
+ const serviceData: serviceData = await this.monitorAdvertisementPackets(switchbot);
291
+ // Update HomeKit
292
+ if (serviceData.model !== '' && serviceData.modelName !== '') {
293
+ await this.BLEparseStatus(serviceData);
294
+ await this.updateHomeKitCharacteristics();
303
295
  } else {
304
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
296
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed to get serviceData, serviceData: ${serviceData}`);
297
+ await this.BLERefreshConnection(switchbot);
305
298
  }
306
- };
307
- // Wait 10 seconds
308
- await switchbot.wait(this.scanDuration * 1000);
309
- // Stop to monitor
310
- await switchbot.stopScan();
311
- // Update HomeKit
312
- await this.BLEparseStatus(switchbot.onadvertisement.serviceData);
313
- await this.updateHomeKitCharacteristics();
314
- })();
315
- if (switchbot === undefined) {
316
- await this.BLERefreshConnection(switchbot);
299
+ })();
317
300
  }
318
301
  }
319
302
 
320
303
  async openAPIRefreshStatus(): Promise<void> {
321
304
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
322
305
  try {
323
- const { body, statusCode } = await this.platform.retryRequest(this.deviceMaxRetries, this.deviceDelayBetweenRetries,
324
- `${Devices}/${this.device.deviceId}/status`, { headers: this.platform.generateHeaders() });
325
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
306
+ const { body, statusCode } = await this.deviceRefreshStatus();
326
307
  const deviceStatus: any = await body.json();
327
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
328
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
308
+ await this.refreshStatusCodes(statusCode, deviceStatus);;
329
309
  if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
330
- this.debugSuccessLog(`${this.device.deviceType}: ${this.accessory.displayName} `
331
- + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
332
- this.openAPIparseStatus(deviceStatus);
333
- this.updateHomeKitCharacteristics();
310
+ await this.successfulRefreshStatus(statusCode, deviceStatus);
311
+ await this.openAPIparseStatus(deviceStatus);
312
+ await this.updateHomeKitCharacteristics();
334
313
  } else {
335
- this.statusCode(statusCode);
336
- this.statusCode(deviceStatus.statusCode);
314
+ await this.statusCodes(statusCode, deviceStatus);
337
315
  }
338
316
  } catch (e: any) {
339
317
  this.apiError(e);
@@ -403,8 +381,8 @@ export class CeilingLight extends deviceBase {
403
381
  async pushChanges(): Promise<void> {
404
382
  if (!this.device.enableCloudService && this.OpenAPI) {
405
383
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} pushChanges enableCloudService: ${this.device.enableCloudService}`);
406
- /*} else if (this.BLE) {
407
- await this.BLEpushChanges();*/
384
+ } else if (this.BLE) {
385
+ await this.BLEpushChanges();
408
386
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
409
387
  await this.openAPIpushChanges();
410
388
  } else {
@@ -428,16 +406,9 @@ export class CeilingLight extends deviceBase {
428
406
  + ` BLEpushChanges On: ${this.LightBulb.On} OnCached: ${this.accessory.context.On}`);
429
407
  const switchbot = await this.platform.connectBLE();
430
408
  // Convert to BLE Address
431
- this.device.bleMac = this.device
432
- .deviceId!.match(/.{1,2}/g)!
433
- .join(':')
434
- .toLowerCase();
435
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
409
+ await this.convertBLEAddress();
436
410
  switchbot
437
- .discover({
438
- model: 'u',
439
- id: this.device.bleMac,
440
- })
411
+ .discover({ model: this.device.bleModel, id: this.device.bleMac })
441
412
  .then(async (device_list: any) => {
442
413
  this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LightBulb.On}`);
443
414
  return await this.retryBLE({
@@ -486,24 +457,15 @@ export class CeilingLight extends deviceBase {
486
457
  });
487
458
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
488
459
  try {
489
- const { body, statusCode } = await request(`${Devices}/${this.device.deviceId}/commands`, {
490
- body: bodyChange,
491
- method: 'POST',
492
- headers: this.platform.generateHeaders(),
493
- });
494
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
460
+ const { body, statusCode } = await this.pushChangeRequest(bodyChange);
495
461
  const deviceStatus: any = await body.json();
496
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
497
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus body: ${JSON.stringify(deviceStatus.body)}`);
498
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
462
+ await this.pushStatusCodes(statusCode, deviceStatus);
499
463
  if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
500
- this.debugSuccessLog(`${this.device.deviceType}: ${this.accessory.displayName} `
501
- + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
502
- this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
503
- + `request to SwitchBot API, body: ${JSON.stringify(JSON.parse(bodyChange))} sent successfully`);
464
+ await this.successfulPushChange(statusCode, deviceStatus, bodyChange);
465
+ await this.updateHomeKitCharacteristics();
504
466
  } else {
505
- this.statusCode(statusCode);
506
- this.statusCode(deviceStatus.statusCode);
467
+ await this.statusCode(statusCode);
468
+ await this.statusCode(deviceStatus.statusCode);
507
469
  }
508
470
  } catch (e: any) {
509
471
  this.apiError(e);
@@ -543,24 +505,15 @@ export class CeilingLight extends deviceBase {
543
505
  });
544
506
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
545
507
  try {
546
- const { body, statusCode } = await request(`${Devices}/${this.device.deviceId}/commands`, {
547
- body: bodyChange,
548
- method: 'POST',
549
- headers: this.platform.generateHeaders(),
550
- });
551
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
508
+ const { body, statusCode } = await this.pushChangeRequest(bodyChange);
552
509
  const deviceStatus: any = await body.json();
553
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
554
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus body: ${JSON.stringify(deviceStatus.body)}`);
555
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
510
+ await this.pushStatusCodes(statusCode, deviceStatus);
556
511
  if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
557
- this.debugSuccessLog(`${this.device.deviceType}: ${this.accessory.displayName} `
558
- + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
559
- this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
560
- + `request to SwitchBot API, body: ${JSON.stringify(JSON.parse(bodyChange))} sent successfully`);
512
+ await this.successfulPushChange(statusCode, deviceStatus, bodyChange);
513
+ await this.updateHomeKitCharacteristics();
561
514
  } else {
562
- this.statusCode(statusCode);
563
- this.statusCode(deviceStatus.statusCode);
515
+ await this.statusCode(statusCode);
516
+ await this.statusCode(deviceStatus.statusCode);
564
517
  }
565
518
  } catch (e: any) {
566
519
  this.apiError(e);
@@ -585,22 +538,15 @@ export class CeilingLight extends deviceBase {
585
538
  });
586
539
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
587
540
  try {
588
- const { body, statusCode } = await request(`${Devices}/${this.device.deviceId}/commands`, {
589
- body: bodyChange,
590
- method: 'POST',
591
- headers: this.platform.generateHeaders(),
592
- });
593
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
541
+ const { body, statusCode } = await this.pushChangeRequest(bodyChange);
594
542
  const deviceStatus: any = await body.json();
595
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
596
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus body: ${JSON.stringify(deviceStatus.body)}`);
597
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
543
+ await this.pushStatusCodes(statusCode, deviceStatus);
598
544
  if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
599
- this.debugSuccessLog(`${this.device.deviceType}: ${this.accessory.displayName} `
600
- + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
545
+ await this.successfulPushChange(statusCode, deviceStatus, bodyChange);
546
+ await this.updateHomeKitCharacteristics();
601
547
  } else {
602
- this.statusCode(statusCode);
603
- this.statusCode(deviceStatus.statusCode);
548
+ await this.statusCode(statusCode);
549
+ await this.statusCode(deviceStatus.statusCode);
604
550
  }
605
551
  } catch (e: any) {
606
552
  this.apiError(e);
@@ -623,24 +569,15 @@ export class CeilingLight extends deviceBase {
623
569
  });
624
570
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
625
571
  try {
626
- const { body, statusCode } = await request(`${Devices}/${this.device.deviceId}/commands`, {
627
- body: bodyChange,
628
- method: 'POST',
629
- headers: this.platform.generateHeaders(),
630
- });
631
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
572
+ const { body, statusCode } = await this.pushChangeRequest(bodyChange);
632
573
  const deviceStatus: any = await body.json();
633
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
634
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus body: ${JSON.stringify(deviceStatus.body)}`);
635
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
574
+ await this.pushStatusCodes(statusCode, deviceStatus);
636
575
  if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
637
- this.debugSuccessLog(`${this.device.deviceType}: ${this.accessory.displayName} `
638
- + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
639
- this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
640
- + `request to SwitchBot API, body: ${JSON.stringify(JSON.parse(bodyChange))} sent successfully`);
576
+ await this.successfulPushChange(statusCode, deviceStatus, bodyChange);
577
+ await this.updateHomeKitCharacteristics();
641
578
  } else {
642
- this.statusCode(statusCode);
643
- this.statusCode(deviceStatus.statusCode);
579
+ await this.statusCode(statusCode);
580
+ await this.statusCode(deviceStatus.statusCode);
644
581
  }
645
582
  } catch (e: any) {
646
583
  this.apiError(e);
@@ -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,58 +306,39 @@ 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
344
  this.apiError(e);
@@ -480,16 +459,9 @@ export class ColorBulb extends deviceBase {
480
459
  + ` On: ${this.LightBulb.On}, OnCached: ${this.accessory.context.On}`);
481
460
  const switchbot = await this.platform.connectBLE();
482
461
  // 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}`);
462
+ await this.convertBLEAddress();
488
463
  switchbot
489
- .discover({
490
- model: 'u',
491
- id: this.device.bleMac,
492
- })
464
+ .discover({ model: this.device.bleModel, id: this.device.bleMac })
493
465
  .then(async (device_list: any) => {
494
466
  this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LightBulb.On}`);
495
467
  return await this.retryBLE({
@@ -538,16 +510,9 @@ export class ColorBulb extends deviceBase {
538
510
  if (this.LightBulb.Brightness !== this.accessory.context.Brightness) {
539
511
  const switchbot = await this.platform.connectBLE();
540
512
  // 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}`);
513
+ await this.convertBLEAddress();
546
514
  switchbot
547
- .discover({
548
- model: 'u',
549
- id: this.device.bleMac,
550
- })
515
+ .discover({ model: this.device.bleModel, id: this.device.bleMac })
551
516
  .then(async (device_list: any) => {
552
517
  this.infoLog(`${this.accessory.displayName} Target Brightness: ${this.LightBulb.Brightness}`);
553
518
  return await device_list[0].setBrightness(this.LightBulb.Brightness);
@@ -573,16 +538,9 @@ export class ColorBulb extends deviceBase {
573
538
  if (this.LightBulb.ColorTemperature !== this.accessory.context.ColorTemperature) {
574
539
  const switchbot = await this.platform.connectBLE();
575
540
  // 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}`);
541
+ await this.convertBLEAddress();
581
542
  switchbot
582
- .discover({
583
- model: 'u',
584
- id: this.device.bleMac,
585
- })
543
+ .discover({ model: this.device.bleModel, id: this.device.bleMac })
586
544
  .then(async (device_list: any) => {
587
545
  this.infoLog(`${this.accessory.displayName} Target ColorTemperature: ${this.LightBulb.ColorTemperature}`);
588
546
  return await device_list[0].setColorTemperature(this.LightBulb.ColorTemperature);
@@ -614,16 +572,9 @@ export class ColorBulb extends deviceBase {
614
572
 
615
573
  const switchbot = await this.platform.connectBLE();
616
574
  // 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}`);
575
+ await this.convertBLEAddress();
622
576
  switchbot
623
- .discover({
624
- model: 'u',
625
- id: this.device.bleMac,
626
- })
577
+ .discover({ model: this.device.bleModel, id: this.device.bleMac })
627
578
  .then(async (device_list: any) => {
628
579
  this.infoLog(`${this.accessory.displayName} Target RGB: ${(this.LightBulb.Brightness, red, green, blue)}`);
629
580
  return await device_list[0].setRGB(this.LightBulb.Brightness, red, green, blue);
@@ -660,16 +611,9 @@ export class ColorBulb extends deviceBase {
660
611
  });
661
612
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
662
613
  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}`);
614
+ const { body, statusCode } = await this.pushChangeRequest(bodyChange);
669
615
  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}`);
616
+ await this.pushStatusCodes(statusCode, deviceStatus);
673
617
  if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
674
618
  this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
675
619
  + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
@@ -716,16 +660,9 @@ export class ColorBulb extends deviceBase {
716
660
  });
717
661
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
718
662
  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}`);
663
+ const { body, statusCode } = await this.pushChangeRequest(bodyChange);
725
664
  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}`);
665
+ await this.pushStatusCodes(statusCode, deviceStatus);
729
666
  if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
730
667
  this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
731
668
  + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
@@ -758,16 +695,9 @@ export class ColorBulb extends deviceBase {
758
695
  });
759
696
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
760
697
  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}`);
698
+ const { body, statusCode } = await this.pushChangeRequest(bodyChange);
767
699
  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}`);
700
+ await this.pushStatusCodes(statusCode, deviceStatus);
771
701
  if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
772
702
  this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
773
703
  + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
@@ -798,16 +728,9 @@ export class ColorBulb extends deviceBase {
798
728
  });
799
729
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
800
730
  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}`);
731
+ const { body, statusCode } = await this.pushChangeRequest(bodyChange);
807
732
  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}`);
733
+ await this.pushStatusCodes(statusCode, deviceStatus);
811
734
  if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
812
735
  this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
813
736
  + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
@@ -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,61 +305,39 @@ 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
343
  this.apiError(e);