@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
@@ -3,10 +3,8 @@
3
3
  * curtain.ts: @switchbot/homebridge-switchbot.
4
4
  */
5
5
  import { hostname } from 'os';
6
- import { request } from 'undici';
7
6
  import { interval, Subject } from 'rxjs';
8
7
  import { deviceBase } from './device.js';
9
- import { Devices } from '../settings.js';
10
8
  import { debounceTime, skipWhile, take, tap } from 'rxjs/operators';
11
9
 
12
10
  import type { SwitchBotPlatform } from '../platform.js';
@@ -296,8 +294,9 @@ export class Curtain extends deviceBase {
296
294
 
297
295
  async BLEparseStatus(serviceData: serviceData): Promise<void> {
298
296
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
297
+ this.debugSuccessLog(`${this.device.deviceType}: ${this.accessory.displayName} ${JSON.stringify(serviceData)}`);
299
298
  // CurrentPosition
300
- this.WindowCovering.CurrentPosition = 100 - Number(serviceData.position);
299
+ this.WindowCovering.CurrentPosition = 100 - Number(serviceData.position!);
301
300
  await this.setMinMax();
302
301
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentPosition ${this.WindowCovering.CurrentPosition}`);
303
302
  if (this.setNewTarget) {
@@ -337,21 +336,35 @@ export class Curtain extends deviceBase {
337
336
  const set_maxLux = this.device.curtain?.set_maxLux ?? 6001;
338
337
  const spaceBetweenLevels = 9;
339
338
 
339
+ serviceData.lightLevel = 1;
340
340
  // Brightness
341
341
  switch (serviceData.lightLevel) {
342
342
  case 1:
343
+ this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * (serviceData.lightLevel === 1
344
+ ? set_minLux : 2);
345
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} 1 LightLevel: ${serviceData.lightLevel},`
346
+ + ` CurrentAmbientLightLevel: ${this.LightSensor!.CurrentAmbientLightLevel}`);
343
347
  this.LightSensor!.CurrentAmbientLightLevel = set_minLux;
344
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${serviceData.lightLevel}`);
348
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} 2 LightLevel: ${serviceData.lightLevel},`
349
+ + ` CurrentAmbientLightLevel: ${this.LightSensor!.CurrentAmbientLightLevel}`);
345
350
  break;
346
351
  case 2:
352
+ this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * (Number(serviceData.lightLevel) - 1);
353
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} 1 LightLevel: ${serviceData.lightLevel},`
354
+ + ` CurrentAmbientLightLevel: ${this.LightSensor!.CurrentAmbientLightLevel}`);
347
355
  this.LightSensor!.CurrentAmbientLightLevel = (set_maxLux - set_minLux) / spaceBetweenLevels;
348
- this.debugLog(
349
- `${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${serviceData.lightLevel},`
356
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} 2 LightLevel: ${serviceData.lightLevel},`
357
+ + ` CurrentAmbientLightLevel: ${this.LightSensor!.CurrentAmbientLightLevel}`);
358
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} 3 LightLevel: ${serviceData.lightLevel},`
350
359
  + ` Calculation: ${(set_maxLux - set_minLux) / spaceBetweenLevels}`);
351
360
  break;
352
361
  case 3:
362
+ this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * (Number(serviceData.lightLevel) - 1);
363
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} 1 LightLevel: ${serviceData.lightLevel},`
364
+ + ` CurrentAmbientLightLevel: ${this.LightSensor!.CurrentAmbientLightLevel}`);
353
365
  this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 2;
354
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${serviceData.lightLevel}`);
366
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} 2 LightLevel: ${serviceData.lightLevel},`
367
+ + ` CurrentAmbientLightLevel: ${this.LightSensor!.CurrentAmbientLightLevel}`);
355
368
  this.Battery.ChargingState = this.hap.Characteristic.ChargingState.CHARGING;
356
369
  break;
357
370
  case 4:
@@ -386,7 +399,12 @@ export class Curtain extends deviceBase {
386
399
  break;
387
400
  case 10:
388
401
  default:
402
+ this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * (Number(serviceData.lightLevel) - 1);
403
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} 1 LightLevel: ${serviceData.lightLevel},`
404
+ + ` CurrentAmbientLightLevel: ${this.LightSensor!.CurrentAmbientLightLevel}`);
389
405
  this.LightSensor!.CurrentAmbientLightLevel = set_maxLux;
406
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} 2 LightLevel: ${serviceData.lightLevel},`
407
+ + ` CurrentAmbientLightLevel: ${this.LightSensor!.CurrentAmbientLightLevel}`);
390
408
  this.debugLog();
391
409
  }
392
410
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${serviceData.lightLevel},`
@@ -506,58 +524,39 @@ export class Curtain extends deviceBase {
506
524
 
507
525
  async BLERefreshStatus(): Promise<void> {
508
526
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLERefreshStatus`);
509
- const switchbot = await this.platform.connectBLE();
510
- // Convert to BLE Address
511
- this.device.bleMac = this.device
512
- .deviceId!.match(/.{1,2}/g)!
513
- .join(':')
514
- .toLowerCase();
515
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
516
- this.getCustomBLEAddress(switchbot);
527
+ const switchbot = await this.switchbotBLE();
528
+
529
+ if (switchbot === undefined) {
530
+ await this.BLERefreshConnection(switchbot);
531
+ } else {
517
532
  // Start to monitor advertisement packets
518
- (async () => {
533
+ (async () => {
519
534
  // Start to monitor advertisement packets
520
- await switchbot.startScan({ model: this.device.bleModel, id: this.device.bleMac });
521
- // Set an event handler
522
- switchbot.onadvertisement = (ad: any) => {
523
- if (this.device.bleMac === ad.address && ad.model === this.device.bleModel) {
524
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ${JSON.stringify(ad, null, ' ')}`);
525
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} address: ${ad.address}, model: ${ad.model}`);
526
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
535
+ const serviceData: serviceData = await this.monitorAdvertisementPackets(switchbot);
536
+ // Update HomeKit
537
+ if (serviceData.model !== '' && serviceData.modelName !== '') {
538
+ await this.BLEparseStatus(serviceData);
539
+ await this.updateHomeKitCharacteristics();
527
540
  } else {
528
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
541
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed to get serviceData, serviceData: ${serviceData}`);
542
+ await this.BLERefreshConnection(switchbot);
529
543
  }
530
- };
531
- // Wait 10 seconds
532
- await switchbot.wait(this.scanDuration * 1000);
533
- // Stop to monitor
534
- await switchbot.stopScan();
535
- // Update HomeKit
536
- await this.BLEparseStatus(switchbot.onadvertisement.serviceData);
537
- await this.updateHomeKitCharacteristics();
538
- })();
539
- if (switchbot === undefined) {
540
- await this.BLERefreshConnection(switchbot);
544
+ })();
541
545
  }
542
546
  }
543
547
 
544
548
  async openAPIRefreshStatus(): Promise<void> {
545
549
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
546
550
  try {
547
- const { body, statusCode } = await this.platform.retryRequest(this.deviceMaxRetries, this.deviceDelayBetweenRetries,
548
- `${Devices}/${this.device.deviceId}/status`, { headers: this.platform.generateHeaders() });
549
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
551
+ const { body, statusCode } = await this.deviceRefreshStatus();
550
552
  const deviceStatus: any = await body.json();
551
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
552
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
553
+ await this.refreshStatusCodes(statusCode, deviceStatus);
553
554
  if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
554
- this.debugSuccessLog(`${this.device.deviceType}: ${this.accessory.displayName} `
555
- + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
556
- this.openAPIparseStatus(deviceStatus);
557
- this.updateHomeKitCharacteristics();
555
+ await this.successfulRefreshStatus(statusCode, deviceStatus);
556
+ await this.openAPIparseStatus(deviceStatus);
557
+ await this.updateHomeKitCharacteristics();
558
558
  } else {
559
- this.statusCode(statusCode);
560
- this.statusCode(deviceStatus.statusCode);
559
+ await this.statusCodes(statusCode, deviceStatus);
561
560
  }
562
561
  } catch (e: any) {
563
562
  this.apiError(e);
@@ -614,17 +613,13 @@ export class Curtain extends deviceBase {
614
613
  if (this.WindowCovering.TargetPosition !== this.WindowCovering.CurrentPosition) {
615
614
  const switchbot = await this.platform.connectBLE();
616
615
  // 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}`);
616
+ await this.convertBLEAddress();
622
617
  const { setPositionMode, Mode }: { setPositionMode: number; Mode: string; } = await this.setPerformance();
623
618
  const adjustedMode = setPositionMode === 1 ? 0x01 : 0xff;
624
619
  this.debugLog(`${this.accessory.displayName} Mode: ${Mode}`);
625
620
  if (switchbot !== false) {
626
621
  try {
627
- const device_list = await switchbot.discover({ model: 'c', quick: true, id: this.device.bleMac });
622
+ const device_list = await switchbot.discover({ model: this.device.bleModel, quick: true, id: this.device.bleMac });
628
623
  this.infoLog(`${this.accessory.displayName} Target Position: ${this.WindowCovering.TargetPosition}`);
629
624
 
630
625
  await this.retryBLE({
@@ -681,16 +676,9 @@ export class Curtain extends deviceBase {
681
676
  });
682
677
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
683
678
  try {
684
- const { body, statusCode } = await request(`${Devices}/${this.device.deviceId}/commands`, {
685
- body: bodyChange,
686
- method: 'POST',
687
- headers: this.platform.generateHeaders(),
688
- });
689
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
679
+ const { body, statusCode } = await this.pushChangeRequest(bodyChange);
690
680
  const deviceStatus: any = await body.json();
691
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
692
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus body: ${JSON.stringify(deviceStatus.body)}`);
693
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
681
+ await this.pushStatusCodes(statusCode, deviceStatus);
694
682
  if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
695
683
  this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
696
684
  + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
@@ -883,8 +871,10 @@ export class Curtain extends deviceBase {
883
871
  }
884
872
 
885
873
  async BLERefreshConnection(switchbot: any): Promise<void> {
886
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot:`
874
+ if (switchbot !== undefined) {
875
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot:`
887
876
  + ` ${JSON.stringify(switchbot)}`);
877
+ }
888
878
  if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
889
879
  this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection to Refresh Status`);
890
880
  await this.openAPIRefreshStatus();
@@ -4,13 +4,15 @@
4
4
  */
5
5
 
6
6
  import { hostname } from 'os';
7
+ import { request } from 'undici';
7
8
  import asyncmqtt from 'async-mqtt';
8
- import { BlindTiltMappingMode, SwitchBotModel, SwitchBotBLEModel, sleep } from '../utils.js';
9
+ import { Devices } from '../settings.js';
10
+ import { BlindTiltMappingMode, SwitchBotModel, SwitchBotBLEModel, SwitchBotBLEModelName, sleep } from '../utils.js';
9
11
 
10
12
  import type { MqttClient } from 'mqtt';
11
13
  import type { SwitchBotPlatform } from '../platform.js';
12
14
  import type { API, HAP, Logging, PlatformAccessory } from 'homebridge';
13
- import type { SwitchBotPlatformConfig, device, devicesConfig } from '../settings.js';
15
+ import type { ad, device, serviceData, SwitchBotPlatformConfig, devicesConfig, deviceStatus } from '../settings.js';
14
16
 
15
17
  export abstract class deviceBase {
16
18
  public readonly api: API;
@@ -365,14 +367,53 @@ export abstract class deviceBase {
365
367
  : null;
366
368
  }
367
369
 
368
- async getCustomBLEAddress(switchbot: any) {
370
+ async switchbotBLE() {
371
+ const switchbot = await this.platform.connectBLE();
372
+ // Convert to BLE Address
373
+ await this.convertBLEAddress();
374
+ await this.getCustomBLEAddress(switchbot);
375
+ return switchbot;
376
+ }
377
+
378
+ async convertBLEAddress() {
379
+ this.device.bleMac = this.device
380
+ .deviceId!.match(/.{1,2}/g)!
381
+ .join(':')
382
+ .toLowerCase();
383
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
384
+ }
385
+
386
+ async monitorAdvertisementPackets(switchbot: any) {
387
+ await switchbot.startScan({ model: this.device.bleModel, id: this.device.bleMac });
388
+ // Set an event handler
389
+ let serviceData: serviceData = { model: this.device.bleModel, modelName: this.device.bleModelName } as serviceData;
390
+ switchbot.onadvertisement = async (ad: ad) => {
391
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${JSON.stringify(ad, null, ' ')}`);
392
+ if (this.device.bleMac === ad.address && ad.serviceData.model === this.device.bleModel) {
393
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ${JSON.stringify(ad, null, ' ')}`);
394
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} address: ${ad.address}, model: ${ad.serviceData.model}`);
395
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
396
+ serviceData = ad.serviceData;
397
+ } else {
398
+ serviceData = { model: '', modelName: '' } as serviceData;
399
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
400
+ }
401
+ };
402
+ // Wait 10 seconds
403
+ await switchbot.wait(this.scanDuration * 1000);
404
+ // Stop to monitor
405
+ await switchbot.stopScan();
406
+ return serviceData;
407
+ }
408
+
409
+ async getCustomBLEAddress(switchbot: any): Promise<void> {
369
410
  if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
370
411
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} customBLEaddress: ${this.device.customBLEaddress}`);
371
412
  (async () => {
372
413
  // Start to monitor advertisement packets
373
414
  await switchbot.startScan({ model: this.device.bleModel });
374
415
  // Set an event handler
375
- switchbot.onadvertisement = (ad: any) => {
416
+ switchbot.onadvertisement = async (ad: ad) => {
376
417
  this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${JSON.stringify(ad, null, ' ')}`);
377
418
  };
378
419
  await sleep(10000);
@@ -382,27 +423,74 @@ export abstract class deviceBase {
382
423
  }
383
424
  }
384
425
 
426
+ async pushChangeRequest(bodyChange: string): Promise<{ body: any; statusCode: any; }> {
427
+ return await request(`${Devices}/${this.device.deviceId}/commands`, {
428
+ body: bodyChange,
429
+ method: 'POST',
430
+ headers: this.platform.generateHeaders(),
431
+ });
432
+ }
433
+
434
+ async successfulPushChange(statusCode: any, deviceStatus: any, bodyChange: string) {
435
+ this.debugSuccessLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode} & deviceStatus`
436
+ + ` StatusCode: ${deviceStatus.statusCode}`);
437
+ this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} request to SwitchBot API,`
438
+ + ` body: ${JSON.stringify(JSON.parse(bodyChange))} sent successfully`);
439
+ }
440
+
441
+ async successfulRefreshStatus(statusCode: any, deviceStatus: any) {
442
+ this.debugSuccessLog(`${this.device.deviceType}: ${this.accessory.displayName} `
443
+ + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
444
+ }
445
+
446
+ async statusCodes(statusCode: any, deviceStatus: any) {
447
+ this.statusCode(statusCode);
448
+ this.statusCode(deviceStatus.statusCode);
449
+ }
450
+
451
+ async refreshStatusCodes(statusCode: number, deviceStatus: deviceStatus): Promise<void> {
452
+ this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
453
+ this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
454
+ this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
455
+ }
456
+
457
+ async pushStatusCodes(statusCode: number, deviceStatus: any) {
458
+ this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
459
+ this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
460
+ this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus body: ${JSON.stringify(deviceStatus.body)}`);
461
+ this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
462
+ }
463
+
464
+ async deviceRefreshStatus(): Promise<{ body: any; statusCode: any; }> {
465
+ return await this.platform.retryRequest(this.deviceMaxRetries, this.deviceDelayBetweenRetries,
466
+ `${Devices}/${this.device.deviceId}/status`, { headers: this.platform.generateHeaders() });
467
+ }
468
+
385
469
  async getDeviceContext(accessory: PlatformAccessory, device: device & devicesConfig): Promise<void> {
386
470
  // Set the accessory context
387
471
  switch (device.deviceType) {
388
472
  case 'Humidifier':
389
473
  device.model = SwitchBotModel.Humidifier;
390
474
  device.bleModel = SwitchBotBLEModel.Humidifier;
475
+ device.bleModelName = SwitchBotBLEModelName.Humidifier;
391
476
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
392
477
  break;
393
478
  case 'Hub Mini':
394
479
  device.model = SwitchBotModel.HubMini;
395
480
  device.bleModel = SwitchBotBLEModel.Unknown;
481
+ device.bleModelName = SwitchBotBLEModelName.Unknown;
396
482
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
397
483
  break;
398
484
  case 'Hub Plus':
399
485
  device.model = SwitchBotModel.HubPlus;
400
486
  device.bleModel = SwitchBotBLEModel.Unknown;
487
+ device.bleModelName = SwitchBotBLEModelName.Unknown;
401
488
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
402
489
  break;
403
490
  case 'Hub 2':
404
491
  device.model = SwitchBotModel.Hub2;
405
- device.bleModel = SwitchBotBLEModel.Unknown;
492
+ device.bleModel = SwitchBotBLEModel.Hub2;
493
+ device.bleModelName = SwitchBotBLEModelName.Hub2;
406
494
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
407
495
  break;
408
496
  case 'Bot':
@@ -413,96 +501,115 @@ export abstract class deviceBase {
413
501
  case 'Meter':
414
502
  device.model = SwitchBotModel.Meter;
415
503
  device.bleModel = SwitchBotBLEModel.Meter;
504
+ device.bleModelName = SwitchBotBLEModelName.Meter;
416
505
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
417
506
  break;
418
507
  case 'MeterPlus':
419
508
  device.model = SwitchBotModel.MeterPlusUS;
420
509
  device.bleModel = SwitchBotBLEModel.MeterPlus;
510
+ device.bleModelName = SwitchBotBLEModelName.MeterPlus;
421
511
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
422
512
  break;
423
513
  case 'Meter Plus (JP)':
424
514
  device.model = SwitchBotModel.MeterPlusJP;
425
515
  device.bleModel = SwitchBotBLEModel.MeterPlus;
516
+ device.bleModelName = SwitchBotBLEModelName.MeterPlus;
426
517
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
427
518
  break;
428
519
  case 'WoIOSensor':
429
520
  device.model = SwitchBotModel.OutdoorMeter;
430
521
  device.bleModel = SwitchBotBLEModel.OutdoorMeter;
522
+ device.bleModelName = SwitchBotBLEModelName.OutdoorMeter;
431
523
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
432
524
  break;
433
525
  case 'Water Detector':
434
526
  device.model = SwitchBotModel.WaterDetector;
435
527
  device.bleModel = SwitchBotBLEModel.Unknown;
528
+ device.bleModelName = SwitchBotBLEModelName.Unknown;
436
529
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
437
530
  break;
438
531
  case 'Motion Sensor':
439
532
  device.model = SwitchBotModel.MotionSensor;
440
533
  device.bleModel = SwitchBotBLEModel.MotionSensor;
534
+ device.bleModelName = SwitchBotBLEModelName.MotionSensor;
441
535
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
442
536
  break;
443
537
  case 'Contact Sensor':
444
538
  device.model = SwitchBotModel.ContactSensor;
445
539
  device.bleModel = SwitchBotBLEModel.ContactSensor;
540
+ device.bleModelName = SwitchBotBLEModelName.ContactSensor;
446
541
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
447
542
  break;
448
543
  case 'Curtain':
449
544
  device.model = SwitchBotModel.Curtain;
450
545
  device.bleModel = SwitchBotBLEModel.Curtain;
546
+ device.bleModelName = SwitchBotBLEModelName.Curtain;
451
547
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
452
548
  break;
453
549
  case 'Curtain3':
454
550
  device.model = SwitchBotModel.Curtain3;
455
551
  device.bleModel = SwitchBotBLEModel.Curtain3;
552
+ device.bleModelName = SwitchBotBLEModelName.Curtain3;
456
553
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
457
554
  break;
458
555
  case 'Blind Tilt':
459
556
  device.model = SwitchBotModel.BlindTilt;
460
557
  device.bleModel = SwitchBotBLEModel.BlindTilt;
558
+ device.bleModelName = SwitchBotBLEModelName.BlindTilt;
461
559
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
462
560
  break;
463
561
  case 'Plug':
464
562
  device.model = SwitchBotModel.Plug;
465
563
  device.bleModel = SwitchBotBLEModel.PlugMiniUS;
564
+ device.bleModelName = SwitchBotBLEModelName.PlugMini;
466
565
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
467
566
  break;
468
567
  case 'Plug Mini (US)':
469
568
  device.model = SwitchBotModel.PlugMiniUS;
470
569
  device.bleModel = SwitchBotBLEModel.PlugMiniUS;
570
+ device.bleModelName = SwitchBotBLEModelName.PlugMini;
471
571
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
472
572
  break;
473
573
  case 'Plug Mini (JP)':
474
574
  device.model = SwitchBotModel.PlugMiniJP;
475
575
  device.bleModel = SwitchBotBLEModel.PlugMiniJP;
576
+ device.bleModelName = SwitchBotBLEModelName.PlugMini;
476
577
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
477
578
  break;
478
579
  case 'Smart Lock':
479
580
  device.model = SwitchBotModel.Lock;
480
581
  device.bleModel = SwitchBotBLEModel.Lock;
582
+ device.bleModelName = SwitchBotBLEModelName.Lock;
481
583
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
482
584
  break;
483
585
  case 'Smart Lock Pro':
484
586
  device.model = SwitchBotModel.LockPro;
485
- device.bleModel = SwitchBotBLEModel.Unknown;
587
+ device.bleModel = SwitchBotBLEModel.Lock;
588
+ device.bleModelName = SwitchBotBLEModelName.Lock;
486
589
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
487
590
  break;
488
591
  case 'Color Bulb':
489
592
  device.model = SwitchBotModel.ColorBulb;
490
593
  device.bleModel = SwitchBotBLEModel.ColorBulb;
594
+ device.bleModelName = SwitchBotBLEModelName.ColorBulb;
491
595
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
492
596
  break;
493
597
  case 'K10+':
494
598
  device.model = SwitchBotModel.K10;
495
599
  device.bleModel = SwitchBotBLEModel.Unknown;
600
+ device.bleModelName = SwitchBotBLEModelName.Unknown;
496
601
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
497
602
  break;
498
603
  case 'WoSweeper':
499
604
  device.model = SwitchBotModel.WoSweeper;
500
605
  device.bleModel = SwitchBotBLEModel.Unknown;
606
+ device.bleModelName = SwitchBotBLEModelName.Unknown;
501
607
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
502
608
  break;
503
609
  case 'WoSweeperMini':
504
610
  device.model = SwitchBotModel.WoSweeperMini;
505
611
  device.bleModel = SwitchBotBLEModel.Unknown;
612
+ device.bleModelName = SwitchBotBLEModelName.Unknown;
506
613
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
507
614
  break;
508
615
  case 'Robot Vacuum Cleaner S1':
@@ -513,51 +620,61 @@ export abstract class deviceBase {
513
620
  case 'Robot Vacuum Cleaner S1 Plus':
514
621
  device.model = SwitchBotModel.RobotVacuumCleanerS1Plus;
515
622
  device.bleModel = SwitchBotBLEModel.Unknown;
623
+ device.bleModelName = SwitchBotBLEModelName.Unknown;
516
624
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
517
625
  break;
518
626
  case 'Robot Vacuum Cleaner S10':
519
627
  device.model = SwitchBotModel.RobotVacuumCleanerS10;
520
628
  device.bleModel = SwitchBotBLEModel.Unknown;
629
+ device.bleModelName = SwitchBotBLEModelName.Unknown;
521
630
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
522
631
  break;
523
632
  case 'Ceiling Light':
524
633
  device.model = SwitchBotModel.CeilingLight;
525
634
  device.bleModel = SwitchBotBLEModel.CeilingLight;
635
+ device.bleModelName = SwitchBotBLEModelName.Unknown;
526
636
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
527
637
  break;
528
638
  case 'Ceiling Light Pro':
529
639
  device.model = SwitchBotModel.CeilingLightPro;
530
640
  device.bleModel = SwitchBotBLEModel.CeilingLightPro;
641
+ device.bleModelName = SwitchBotBLEModelName.Unknown;
531
642
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
532
643
  break;
533
644
  case 'Strip Light':
534
645
  device.model = SwitchBotModel.StripLight;
535
646
  device.bleModel = SwitchBotBLEModel.StripLight;
647
+ device.bleModelName = SwitchBotBLEModelName.StripLight;
536
648
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
537
649
  break;
538
650
  case 'Indoor Cam':
539
651
  device.model = SwitchBotModel.IndoorCam;
540
652
  device.bleModel = SwitchBotBLEModel.Unknown;
653
+ device.bleModelName = SwitchBotBLEModelName.Unknown;
541
654
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
542
655
  break;
543
656
  case 'Remote':
544
657
  device.model = SwitchBotModel.Remote;
545
- device.bleModel = SwitchBotBLEModel.Remote;
658
+ device.bleModel = SwitchBotBLEModel.Unknown;
659
+ device.bleModelName = SwitchBotBLEModelName.Unknown;
546
660
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
547
661
  break;
548
662
  case 'remote with screen+':
549
663
  device.model = SwitchBotModel.UniversalRemote;
550
664
  device.bleModel = SwitchBotBLEModel.Unknown;
665
+ device.bleModelName = SwitchBotBLEModelName.Unknown;
551
666
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
552
667
  break;
553
668
  case 'Battery Circulator Fan':
554
669
  device.model = SwitchBotModel.BatteryCirculatorFan;
555
670
  device.bleModel = SwitchBotBLEModel.Unknown;
671
+ device.bleModelName = SwitchBotBLEModelName.Unknown;
556
672
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
557
673
  break;
558
674
  default:
559
675
  device.model = SwitchBotModel.Unknown;
560
676
  device.bleModel = SwitchBotBLEModel.Unknown;
677
+ device.bleModelName = SwitchBotBLEModelName.Unknown;
561
678
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
562
679
  }
563
680
  accessory.context.model = device.model;