@switchbot/homebridge-switchbot 2.14.0 → 3.0.0-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 (154) hide show
  1. package/.github/npm-version-script.cjs +81 -0
  2. package/.github/workflows/dependabot.yml +10 -11
  3. package/CHANGELOG.md +11 -2
  4. package/LICENSE +1 -1
  5. package/config.schema.json +1 -0
  6. package/dist/device/blindtilt.d.ts +9 -5
  7. package/dist/device/blindtilt.d.ts.map +1 -1
  8. package/dist/device/blindtilt.js +151 -91
  9. package/dist/device/blindtilt.js.map +1 -1
  10. package/dist/device/bot.d.ts +9 -5
  11. package/dist/device/bot.d.ts.map +1 -1
  12. package/dist/device/bot.js +234 -184
  13. package/dist/device/bot.js.map +1 -1
  14. package/dist/device/ceilinglight.d.ts +9 -5
  15. package/dist/device/ceilinglight.d.ts.map +1 -1
  16. package/dist/device/ceilinglight.js +110 -64
  17. package/dist/device/ceilinglight.js.map +1 -1
  18. package/dist/device/colorbulb.d.ts +9 -5
  19. package/dist/device/colorbulb.d.ts.map +1 -1
  20. package/dist/device/colorbulb.js +121 -69
  21. package/dist/device/colorbulb.js.map +1 -1
  22. package/dist/device/contact.d.ts +9 -5
  23. package/dist/device/contact.d.ts.map +1 -1
  24. package/dist/device/contact.js +111 -66
  25. package/dist/device/contact.js.map +1 -1
  26. package/dist/device/curtain.d.ts +9 -5
  27. package/dist/device/curtain.d.ts.map +1 -1
  28. package/dist/device/curtain.js +164 -107
  29. package/dist/device/curtain.js.map +1 -1
  30. package/dist/device/hub.d.ts +9 -5
  31. package/dist/device/hub.d.ts.map +1 -1
  32. package/dist/device/hub.js +87 -51
  33. package/dist/device/hub.js.map +1 -1
  34. package/dist/device/humidifier.d.ts +9 -5
  35. package/dist/device/humidifier.d.ts.map +1 -1
  36. package/dist/device/humidifier.js +133 -90
  37. package/dist/device/humidifier.js.map +1 -1
  38. package/dist/device/iosensor.d.ts +9 -5
  39. package/dist/device/iosensor.d.ts.map +1 -1
  40. package/dist/device/iosensor.js +108 -66
  41. package/dist/device/iosensor.js.map +1 -1
  42. package/dist/device/lightstrip.d.ts +9 -5
  43. package/dist/device/lightstrip.d.ts.map +1 -1
  44. package/dist/device/lightstrip.js +110 -65
  45. package/dist/device/lightstrip.js.map +1 -1
  46. package/dist/device/lock.d.ts +10 -6
  47. package/dist/device/lock.d.ts.map +1 -1
  48. package/dist/device/lock.js +125 -84
  49. package/dist/device/lock.js.map +1 -1
  50. package/dist/device/meter.d.ts +9 -5
  51. package/dist/device/meter.d.ts.map +1 -1
  52. package/dist/device/meter.js +102 -63
  53. package/dist/device/meter.js.map +1 -1
  54. package/dist/device/meterplus.d.ts +9 -5
  55. package/dist/device/meterplus.d.ts.map +1 -1
  56. package/dist/device/meterplus.js +102 -63
  57. package/dist/device/meterplus.js.map +1 -1
  58. package/dist/device/motion.d.ts +9 -5
  59. package/dist/device/motion.d.ts.map +1 -1
  60. package/dist/device/motion.js +95 -54
  61. package/dist/device/motion.js.map +1 -1
  62. package/dist/device/plug.d.ts +9 -5
  63. package/dist/device/plug.d.ts.map +1 -1
  64. package/dist/device/plug.js +68 -40
  65. package/dist/device/plug.js.map +1 -1
  66. package/dist/device/robotvacuumcleaner.d.ts +9 -5
  67. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  68. package/dist/device/robotvacuumcleaner.js +86 -52
  69. package/dist/device/robotvacuumcleaner.js.map +1 -1
  70. package/dist/homebridge-ui/server.d.ts +2 -0
  71. package/dist/homebridge-ui/server.d.ts.map +1 -0
  72. package/dist/homebridge-ui/server.js +44 -0
  73. package/dist/homebridge-ui/server.js.map +1 -0
  74. package/dist/index.d.ts +1 -4
  75. package/dist/index.d.ts.map +1 -1
  76. package/dist/index.js +9 -5
  77. package/dist/index.js.map +1 -1
  78. package/dist/irdevice/airconditioner.d.ts +8 -4
  79. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  80. package/dist/irdevice/airconditioner.js +105 -71
  81. package/dist/irdevice/airconditioner.js.map +1 -1
  82. package/dist/irdevice/airpurifier.d.ts +8 -4
  83. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  84. package/dist/irdevice/airpurifier.js +78 -46
  85. package/dist/irdevice/airpurifier.js.map +1 -1
  86. package/dist/irdevice/camera.d.ts +8 -4
  87. package/dist/irdevice/camera.d.ts.map +1 -1
  88. package/dist/irdevice/camera.js +41 -25
  89. package/dist/irdevice/camera.js.map +1 -1
  90. package/dist/irdevice/fan.d.ts +8 -4
  91. package/dist/irdevice/fan.d.ts.map +1 -1
  92. package/dist/irdevice/fan.js +62 -39
  93. package/dist/irdevice/fan.js.map +1 -1
  94. package/dist/irdevice/light.d.ts +8 -4
  95. package/dist/irdevice/light.d.ts.map +1 -1
  96. package/dist/irdevice/light.js +67 -45
  97. package/dist/irdevice/light.js.map +1 -1
  98. package/dist/irdevice/other.d.ts +8 -4
  99. package/dist/irdevice/other.d.ts.map +1 -1
  100. package/dist/irdevice/other.js +176 -150
  101. package/dist/irdevice/other.js.map +1 -1
  102. package/dist/irdevice/tv.d.ts +8 -4
  103. package/dist/irdevice/tv.d.ts.map +1 -1
  104. package/dist/irdevice/tv.js +81 -62
  105. package/dist/irdevice/tv.js.map +1 -1
  106. package/dist/irdevice/vacuumcleaner.d.ts +8 -4
  107. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  108. package/dist/irdevice/vacuumcleaner.js +41 -25
  109. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  110. package/dist/irdevice/waterheater.d.ts +8 -4
  111. package/dist/irdevice/waterheater.d.ts.map +1 -1
  112. package/dist/irdevice/waterheater.js +48 -32
  113. package/dist/irdevice/waterheater.js.map +1 -1
  114. package/dist/platform.d.ts +8 -9
  115. package/dist/platform.d.ts.map +1 -1
  116. package/dist/platform.js +147 -162
  117. package/dist/platform.js.map +1 -1
  118. package/dist/settings.js +7 -14
  119. package/dist/settings.js.map +1 -1
  120. package/dist/utils.js +1 -5
  121. package/dist/utils.js.map +1 -1
  122. package/package.json +28 -16
  123. package/src/device/blindtilt.ts +80 -69
  124. package/src/device/bot.ts +189 -178
  125. package/src/device/ceilinglight.ts +55 -42
  126. package/src/device/colorbulb.ts +51 -38
  127. package/src/device/contact.ts +69 -58
  128. package/src/device/curtain.ts +94 -83
  129. package/src/device/hub.ts +54 -43
  130. package/src/device/humidifier.ts +86 -75
  131. package/src/device/iosensor.ts +63 -52
  132. package/src/device/lightstrip.ts +54 -41
  133. package/src/device/lock.ts +89 -79
  134. package/src/device/meter.ts +59 -48
  135. package/src/device/meterplus.ts +63 -52
  136. package/src/device/motion.ts +56 -45
  137. package/src/device/plug.ts +36 -25
  138. package/src/device/robotvacuumcleaner.ts +47 -36
  139. package/src/homebridge-ui/public/index.html +240 -0
  140. package/src/homebridge-ui/server.ts +48 -0
  141. package/src/index.ts +10 -7
  142. package/src/irdevice/airconditioner.ts +70 -62
  143. package/src/irdevice/airpurifier.ts +47 -39
  144. package/src/irdevice/camera.ts +27 -19
  145. package/src/irdevice/fan.ts +41 -33
  146. package/src/irdevice/light.ts +47 -39
  147. package/src/irdevice/other.ts +163 -155
  148. package/src/irdevice/tv.ts +65 -57
  149. package/src/irdevice/vacuumcleaner.ts +27 -19
  150. package/src/irdevice/waterheater.ts +34 -26
  151. package/src/platform.ts +35 -21
  152. package/tsconfig.json +11 -13
  153. package/homebridge-ui/server.js +0 -50
  154. /package/{homebridge-ui → dist/homebridge-ui}/public/index.html +0 -0
@@ -3,10 +3,14 @@ import { sleep } from '../utils';
3
3
  import { interval, Subject } from 'rxjs';
4
4
  import { SwitchBotPlatform } from '../platform';
5
5
  import { debounceTime, skipWhile, take, tap } from 'rxjs/operators';
6
- import { Service, PlatformAccessory, CharacteristicValue } from 'homebridge';
7
- import { device, devicesConfig, deviceStatus, ad, serviceData, Devices } from '../settings';
6
+ import { Service, PlatformAccessory, CharacteristicValue, API, Logging, HAP } from 'homebridge';
7
+ import { device, devicesConfig, deviceStatus, ad, serviceData, Devices, SwitchBotPlatformConfig } from '../settings';
8
8
 
9
9
  export class Plug {
10
+ public readonly api: API;
11
+ public readonly log: Logging;
12
+ public readonly config!: SwitchBotPlatformConfig;
13
+ protected readonly hap: HAP;
10
14
  // Services
11
15
  outletService: Service;
12
16
 
@@ -32,20 +36,27 @@ export class Plug {
32
36
  doPlugUpdate!: Subject<void>;
33
37
 
34
38
  // Connection
35
- private readonly BLE = this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI';
36
- private readonly OpenAPI = this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI';
39
+ private readonly OpenAPI: boolean;
40
+ private readonly BLE: boolean;
37
41
 
38
42
  constructor(
39
43
  private readonly platform: SwitchBotPlatform,
40
44
  private accessory: PlatformAccessory,
41
45
  public device: device & devicesConfig,
42
46
  ) {
47
+ this.api = this.platform.api;
48
+ this.log = this.platform.log;
49
+ this.config = this.platform.config;
50
+ this.hap = this.api.hap;
51
+ // Connection
52
+ this.BLE = this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI';
53
+ this.OpenAPI = this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI';
43
54
  // default placeholders
44
- this.logs(device);
55
+ this.deviceLogs(device);
45
56
  this.scan(device);
46
57
  this.refreshRate(device);
47
58
  this.context();
48
- this.config(device);
59
+ this.deviceConfig(device);
49
60
 
50
61
  // this is subject we use to track when we need to POST changes to the SwitchBot API
51
62
  this.doPlugUpdate = new Subject();
@@ -56,27 +67,27 @@ export class Plug {
56
67
 
57
68
  // set accessory information
58
69
  accessory
59
- .getService(this.platform.Service.AccessoryInformation)!
60
- .setCharacteristic(this.platform.Characteristic.Manufacturer, 'SwitchBot')
61
- .setCharacteristic(this.platform.Characteristic.Model, this.model(device))
62
- .setCharacteristic(this.platform.Characteristic.SerialNumber, device.deviceId)
63
- .setCharacteristic(this.platform.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision);
70
+ .getService(this.hap.Service.AccessoryInformation)!
71
+ .setCharacteristic(this.hap.Characteristic.Manufacturer, 'SwitchBot')
72
+ .setCharacteristic(this.hap.Characteristic.Model, this.model(device))
73
+ .setCharacteristic(this.hap.Characteristic.SerialNumber, device.deviceId)
74
+ .setCharacteristic(this.hap.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision);
64
75
 
65
76
  // get the Outlet service if it exists, otherwise create a new Outlet service
66
77
  // you can create multiple services for each accessory
67
78
  const outletService = `${accessory.displayName} ${device.deviceType}`;
68
- (this.outletService = accessory.getService(this.platform.Service.Outlet)
69
- || accessory.addService(this.platform.Service.Outlet)), outletService;
79
+ (this.outletService = accessory.getService(this.hap.Service.Outlet)
80
+ || accessory.addService(this.hap.Service.Outlet)), outletService;
70
81
 
71
- this.outletService.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
72
- if (!this.outletService.testCharacteristic(this.platform.Characteristic.ConfiguredName)) {
73
- this.outletService.addCharacteristic(this.platform.Characteristic.ConfiguredName, accessory.displayName);
82
+ this.outletService.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
83
+ if (!this.outletService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
84
+ this.outletService.addCharacteristic(this.hap.Characteristic.ConfiguredName, accessory.displayName);
74
85
  }
75
86
  // each service must implement at-minimum the "required characteristics" for the given service type
76
87
  // see https://developers.homebridge.io/#/service/Outlet
77
88
 
78
89
  // create handlers for required characteristics
79
- this.outletService.getCharacteristic(this.platform.Characteristic.On).onSet(this.OnSet.bind(this));
90
+ this.outletService.getCharacteristic(this.hap.Characteristic.On).onSet(this.OnSet.bind(this));
80
91
 
81
92
  // Update Homekit
82
93
  this.updateHomeKitCharacteristics();
@@ -97,14 +108,14 @@ export class Plug {
97
108
  const { powerState } = context;
98
109
  const { On } = this;
99
110
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ` +
100
- '(powerState) = ' +
101
- `Webhook:(${powerState}), ` +
102
- `current:(${On})`);
111
+ '(powerState) = ' +
112
+ `Webhook:(${powerState}), ` +
113
+ `current:(${On})`);
103
114
  this.On = powerState === 'ON' ? true : false;
104
115
  this.updateHomeKitCharacteristics();
105
116
  } catch (e: any) {
106
117
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
107
- + `failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
118
+ + `failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
108
119
  }
109
120
  };
110
121
  }
@@ -442,7 +453,7 @@ export class Plug {
442
453
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
443
454
  } else {
444
455
  this.accessory.context.On = this.On;
445
- this.outletService.updateCharacteristic(this.platform.Characteristic.On, this.On);
456
+ this.outletService.updateCharacteristic(this.hap.Characteristic.On, this.On);
446
457
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
447
458
  }
448
459
  }
@@ -595,7 +606,7 @@ export class Plug {
595
606
  }
596
607
 
597
608
  async apiError(e: any): Promise<void> {
598
- this.outletService.updateCharacteristic(this.platform.Characteristic.On, e);
609
+ this.outletService.updateCharacteristic(this.hap.Characteristic.On, e);
599
610
  }
600
611
 
601
612
  async context() {
@@ -616,7 +627,7 @@ export class Plug {
616
627
  }
617
628
  }
618
629
 
619
- async config(device: device & devicesConfig): Promise<void> {
630
+ async deviceConfig(device: device & devicesConfig): Promise<void> {
620
631
  let config = {};
621
632
  if (device.plug) {
622
633
  config = device.plug;
@@ -647,7 +658,7 @@ export class Plug {
647
658
  }
648
659
  }
649
660
 
650
- async logs(device: device & devicesConfig): Promise<void> {
661
+ async deviceLogs(device: device & devicesConfig): Promise<void> {
651
662
  if (this.platform.debugMode) {
652
663
  this.deviceLogging = this.accessory.context.logging = 'debugMode';
653
664
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
@@ -3,10 +3,14 @@ import { sleep } from '../utils';
3
3
  import { interval, Subject } from 'rxjs';
4
4
  import { SwitchBotPlatform } from '../platform';
5
5
  import { debounceTime, skipWhile, take, tap } from 'rxjs/operators';
6
- import { Service, PlatformAccessory, CharacteristicValue } from 'homebridge';
7
- import { device, devicesConfig, deviceStatus, ad, serviceData, Devices } from '../settings';
6
+ import { Service, PlatformAccessory, CharacteristicValue, API, Logging, HAP } from 'homebridge';
7
+ import { device, devicesConfig, deviceStatus, ad, serviceData, Devices, SwitchBotPlatformConfig } from '../settings';
8
8
 
9
9
  export class RobotVacuumCleaner {
10
+ public readonly api: API;
11
+ public readonly log: Logging;
12
+ public readonly config!: SwitchBotPlatformConfig;
13
+ protected readonly hap: HAP;
10
14
  // Services
11
15
  batteryService: Service;
12
16
  robotVacuumCleanerService: Service;
@@ -39,20 +43,27 @@ export class RobotVacuumCleaner {
39
43
  doRobotVacuumCleanerUpdate!: Subject<void>;
40
44
 
41
45
  // Connection
42
- private readonly BLE = this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI';
43
- private readonly OpenAPI = this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI';
46
+ private readonly OpenAPI: boolean;
47
+ private readonly BLE: boolean;
44
48
 
45
49
  constructor(
46
50
  private readonly platform: SwitchBotPlatform,
47
51
  private accessory: PlatformAccessory,
48
52
  public device: device & devicesConfig,
49
53
  ) {
54
+ this.api = this.platform.api;
55
+ this.log = this.platform.log;
56
+ this.config = this.platform.config;
57
+ this.hap = this.api.hap;
58
+ // Connection
59
+ this.BLE = this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI';
60
+ this.OpenAPI = this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI';
50
61
  // default placeholders
51
- this.logs(device);
62
+ this.deviceLogs(device);
52
63
  this.scan(device);
53
64
  this.refreshRate(device);
54
65
  this.context();
55
- this.config(device);
66
+ this.deviceConfig(device);
56
67
 
57
68
  // this is subject we use to track when we need to POST changes to the SwitchBot API
58
69
  this.doRobotVacuumCleanerUpdate = new Subject();
@@ -63,31 +74,31 @@ export class RobotVacuumCleaner {
63
74
 
64
75
  // set accessory information
65
76
  accessory
66
- .getService(this.platform.Service.AccessoryInformation)!
67
- .setCharacteristic(this.platform.Characteristic.Manufacturer, 'SwitchBot')
68
- .setCharacteristic(this.platform.Characteristic.Model, this.model(device))
69
- .setCharacteristic(this.platform.Characteristic.SerialNumber, device.deviceId)
70
- .setCharacteristic(this.platform.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision);
77
+ .getService(this.hap.Service.AccessoryInformation)!
78
+ .setCharacteristic(this.hap.Characteristic.Manufacturer, 'SwitchBot')
79
+ .setCharacteristic(this.hap.Characteristic.Model, this.model(device))
80
+ .setCharacteristic(this.hap.Characteristic.SerialNumber, device.deviceId)
81
+ .setCharacteristic(this.hap.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision);
71
82
 
72
83
  // get the Lightbulb service if it exists, otherwise create a new Lightbulb service
73
84
  // you can create multiple services for each accessory
74
85
  const robotVacuumCleanerService = `${accessory.displayName} ${device.deviceType}`;
75
- (this.robotVacuumCleanerService = accessory.getService(this.platform.Service.Lightbulb)
76
- || accessory.addService(this.platform.Service.Lightbulb)), robotVacuumCleanerService;
86
+ (this.robotVacuumCleanerService = accessory.getService(this.hap.Service.Lightbulb)
87
+ || accessory.addService(this.hap.Service.Lightbulb)), robotVacuumCleanerService;
77
88
 
78
- this.robotVacuumCleanerService.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
79
- if (!this.robotVacuumCleanerService.testCharacteristic(this.platform.Characteristic.ConfiguredName)) {
80
- this.robotVacuumCleanerService.addCharacteristic(this.platform.Characteristic.ConfiguredName, accessory.displayName);
89
+ this.robotVacuumCleanerService.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
90
+ if (!this.robotVacuumCleanerService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
91
+ this.robotVacuumCleanerService.addCharacteristic(this.hap.Characteristic.ConfiguredName, accessory.displayName);
81
92
  }
82
93
  // each service must implement at-minimum the "required characteristics" for the given service type
83
94
  // see https://developers.homebridge.io/#/service/Lightbulb
84
95
 
85
96
  // create handlers for required characteristics
86
- this.robotVacuumCleanerService.getCharacteristic(this.platform.Characteristic.On).onSet(this.OnSet.bind(this));
97
+ this.robotVacuumCleanerService.getCharacteristic(this.hap.Characteristic.On).onSet(this.OnSet.bind(this));
87
98
 
88
99
  // handle Brightness events using the Brightness characteristic
89
100
  this.robotVacuumCleanerService
90
- .getCharacteristic(this.platform.Characteristic.Brightness)
101
+ .getCharacteristic(this.hap.Characteristic.Brightness)
91
102
  .setProps({
92
103
  minStep: 25,
93
104
  minValue: 0,
@@ -102,12 +113,12 @@ export class RobotVacuumCleaner {
102
113
 
103
114
  // Battery Service
104
115
  const batteryService = `${accessory.displayName} Battery`;
105
- (this.batteryService = this.accessory.getService(this.platform.Service.Battery)
106
- || accessory.addService(this.platform.Service.Battery)), batteryService;
116
+ (this.batteryService = this.accessory.getService(this.hap.Service.Battery)
117
+ || accessory.addService(this.hap.Service.Battery)), batteryService;
107
118
 
108
- this.batteryService.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Battery`);
109
- if (!this.batteryService.testCharacteristic(this.platform.Characteristic.ConfiguredName)) {
110
- this.batteryService.addCharacteristic(this.platform.Characteristic.ConfiguredName, `${accessory.displayName} Battery`);
119
+ this.batteryService.setCharacteristic(this.hap.Characteristic.Name, `${accessory.displayName} Battery`);
120
+ if (!this.batteryService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
121
+ this.batteryService.addCharacteristic(this.hap.Characteristic.ConfiguredName, `${accessory.displayName} Battery`);
111
122
  }
112
123
 
113
124
  // Update Homekit
@@ -129,15 +140,15 @@ export class RobotVacuumCleaner {
129
140
  const { onlineStatus, battery } = context;
130
141
  const { On, BatteryLevel } = this;
131
142
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ` +
132
- '(onlineStatus, battery) = ' +
133
- `Webhook:(${onlineStatus}, ${battery}), ` +
134
- `current:(${On}, ${BatteryLevel})`);
143
+ '(onlineStatus, battery) = ' +
144
+ `Webhook:(${onlineStatus}, ${battery}), ` +
145
+ `current:(${On}, ${BatteryLevel})`);
135
146
  this.On = onlineStatus === 'online' ? true : false;
136
147
  this.BatteryLevel = battery;
137
148
  this.updateHomeKitCharacteristics();
138
149
  } catch (e: any) {
139
150
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
140
- + `failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
151
+ + `failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
141
152
  }
142
153
  };
143
154
  }
@@ -215,9 +226,9 @@ export class RobotVacuumCleaner {
215
226
  // Battery
216
227
  this.BatteryLevel = Number(this.OpenAPI_BatteryLevel);
217
228
  if (this.BatteryLevel < 15) {
218
- this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
229
+ this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
219
230
  } else {
220
- this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
231
+ this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
221
232
  }
222
233
  this.debugLog(`${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}, StatusLowBattery: ${this.StatusLowBattery}`);
223
234
 
@@ -583,7 +594,7 @@ export class RobotVacuumCleaner {
583
594
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
584
595
  } else {
585
596
  this.accessory.context.On = this.On;
586
- this.robotVacuumCleanerService.updateCharacteristic(this.platform.Characteristic.On, this.On);
597
+ this.robotVacuumCleanerService.updateCharacteristic(this.hap.Characteristic.On, this.On);
587
598
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
588
599
  }
589
600
  // Brightness
@@ -591,7 +602,7 @@ export class RobotVacuumCleaner {
591
602
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Brightness: ${this.Brightness}`);
592
603
  } else {
593
604
  this.accessory.context.Brightness = this.Brightness;
594
- this.robotVacuumCleanerService.updateCharacteristic(this.platform.Characteristic.Brightness, this.Brightness);
605
+ this.robotVacuumCleanerService.updateCharacteristic(this.hap.Characteristic.Brightness, this.Brightness);
595
606
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Brightness: ${this.Brightness}`);
596
607
  }
597
608
  // BatteryLevel
@@ -599,7 +610,7 @@ export class RobotVacuumCleaner {
599
610
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
600
611
  } else {
601
612
  this.accessory.context.BatteryLevel = this.BatteryLevel;
602
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, this.BatteryLevel);
613
+ this.batteryService?.updateCharacteristic(this.hap.Characteristic.BatteryLevel, this.BatteryLevel);
603
614
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
604
615
  }
605
616
  // StatusLowBattery
@@ -607,7 +618,7 @@ export class RobotVacuumCleaner {
607
618
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
608
619
  } else {
609
620
  this.accessory.context.StatusLowBattery = this.StatusLowBattery;
610
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.StatusLowBattery);
621
+ this.batteryService?.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, this.StatusLowBattery);
611
622
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
612
623
  }
613
624
  }
@@ -759,7 +770,7 @@ export class RobotVacuumCleaner {
759
770
  }
760
771
 
761
772
  async apiError(e: any): Promise<void> {
762
- this.robotVacuumCleanerService.updateCharacteristic(this.platform.Characteristic.On, e);
773
+ this.robotVacuumCleanerService.updateCharacteristic(this.hap.Characteristic.On, e);
763
774
  }
764
775
 
765
776
  async context() {
@@ -785,7 +796,7 @@ export class RobotVacuumCleaner {
785
796
  }
786
797
  }
787
798
 
788
- async config(device: device & devicesConfig): Promise<void> {
799
+ async deviceConfig(device: device & devicesConfig): Promise<void> {
789
800
  let config = {};
790
801
  if (device.plug) {
791
802
  config = device.plug;
@@ -816,7 +827,7 @@ export class RobotVacuumCleaner {
816
827
  }
817
828
  }
818
829
 
819
- async logs(device: device & devicesConfig): Promise<void> {
830
+ async deviceLogs(device: device & devicesConfig): Promise<void> {
820
831
  if (this.platform.debugMode) {
821
832
  this.deviceLogging = this.accessory.context.logging = 'debugMode';
822
833
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
@@ -0,0 +1,240 @@
1
+ <p class="text-center">
2
+ <img
3
+ src="https://raw.githubusercontent.com/OpenWonderLabs/homebridge-switchbot/latest/branding/Homebridge_x_SwitchBot.svg"
4
+ alt="homebridge-switchbot logo"
5
+ style="width: 50%" />
6
+ </p>
7
+ <div id="pageIntro" style="display: none">
8
+ <p class="lead text-center">Thank you for installing <strong>homebridge-switchbot</strong></p>
9
+ <p class="lead text-center">Before continuing:</p>
10
+ <ol>
11
+ <li class="mb-3">
12
+ Download the SwitchBot App from either the
13
+ <a href="https://apps.apple.com/us/app/switchbot/id1087374760" target="_blank" rel="noreferrer noopener">App Store</a> or the
14
+ <a href="https://play.google.com/store/apps/details?id=com.theswitchbot.switchbot&hl=en_US&gl=US" target="_blank" rel="noreferrer noopener"
15
+ >Play Store</a
16
+ >.
17
+ </li>
18
+ <li class="mb-3">Select <strong>Profile</strong>.</li>
19
+ <li class="mb-3">Select <strong>Preferences</strong>.</li>
20
+ <li class="mb-3">Tap the <strong>App version</strong> 10 times, this will enable Developer Options.</li>
21
+ <li class="mb-3">Select <strong>Developer Options</strong>.</li>
22
+ <li class="mb-3">Copy your token by hitting <strong>Copy to Clipboard</strong>.</li>
23
+ <li class="mb-3">Paste the Token into the Token parameter on the next page.</li>
24
+ </ol>
25
+ <div class="text-center">
26
+ <button type="button" class="btn btn-primary" id="introLink">Continue &rarr;</button>
27
+ </div>
28
+ </div>
29
+ <div id="menuWrapper" class="btn-group w-100 mb-0" role="group" aria-label="UI Menu" style="display: none">
30
+ <button type="button" class="btn btn-primary" id="menuSettings">Settings</button>
31
+ <button type="button" class="btn btn-primary" id="menuDevices">Devices</button>
32
+ <button type="button" class="btn btn-primary mr-0" id="menuHome">Support</button>
33
+ </div>
34
+ <div id="disabledBanner" class="alert alert-secondary mb-0 mt-3" role="alert" style="display: none">
35
+ Plugin is currently disabled
36
+ <button id="disabledEnable" type="button" class="btn btn-link p-0 m-0 float-right">Enable</button>
37
+ </div>
38
+ <div id="pageDevices" class="mt-4" style="display: none">
39
+ <div id="deviceInfo">
40
+ <form>
41
+ <div class="form-group">
42
+ <select class="form-control" id="deviceSelect"></select>
43
+ </div>
44
+ </form>
45
+ <table class="table w-100" id="deviceTable" style="display: none">
46
+ <thead>
47
+ <tr class="table-active">
48
+ <th scope="col" style="width: 40%">Device Name</th>
49
+ <th scope="col" style="width: 60%" id="displayName"></th>
50
+ </tr>
51
+ </thead>
52
+ <tbody>
53
+ <tr>
54
+ <th scope="row">Device ID</th>
55
+ <td id="deviceID"></td>
56
+ </tr>
57
+ <tr>
58
+ <th scope="row">Model</th>
59
+ <td id="model"></td>
60
+ </tr>
61
+ <tr>
62
+ <th scope="row">Firmware Version</th>
63
+ <td id="firmwareRevision"></td>
64
+ </tr>
65
+ <tr>
66
+ <th scope="row">Device Type</th>
67
+ <td id="deviceType"></td>
68
+ </tr>
69
+ <tr>
70
+ <th scope="row">Connection Type</th>
71
+ <td id="connectionType"></td>
72
+ </tr>
73
+ </tbody>
74
+ </table>
75
+ <p class="text-center">External Accessories Will Not Display Here.</p>
76
+ </div>
77
+ </div>
78
+ <div id="pageSupport" class="mt-4" style="display: none">
79
+ <p class="text-center lead">Thank you for using <strong>homebridge-switchbot</strong></p>
80
+ <p class="text-center">The links below will take you to our GitHub wiki</p>
81
+ <h5>Setup</h5>
82
+ <ul>
83
+ <li>
84
+ <a href="https://github.com/OpenWonderLabs/homebridge-switchbot/wiki" target="_blank">Wiki Home</a>
85
+ </li>
86
+ <li>
87
+ <a href="https://github.com/OpenWonderLabs/homebridge-switchbot/wiki/Configuration" target="_blank">Configuration</a>
88
+ </li>
89
+ <li>
90
+ <a href="https://github.com/OpenWonderLabs/homebridge-switchbot/wiki/Beta-Version" target="_blank">Beta Version</a>
91
+ </li>
92
+ <li>
93
+ <a href="https://github.com/OpenWonderLabs/homebridge-switchbot/wiki/Node-Version" target="_blank">Node Version</a>
94
+ </li>
95
+ <li>
96
+ <a href="https://github.com/OpenWonderLabs/homebridge-switchbot/wiki/Uninstallation" target="_blank">Uninstallation</a>
97
+ </li>
98
+ </ul>
99
+ <h5>Features</h5>
100
+ <ul>
101
+ <li>
102
+ <a href="https://github.com/OpenWonderLabs/homebridge-switchbot/wiki/Supported-Devices" target="_blank">Supported Devices</a>
103
+ </li>
104
+ </ul>
105
+ <h5>Help/About</h5>
106
+ <ul>
107
+ <li>
108
+ <a href="https://github.com/OpenWonderLabs/homebridge-switchbot/issues/new/choose" target="_blank">Support Request</a>
109
+ </li>
110
+ <li>
111
+ <a href="https://github.com/OpenWonderLabs/homebridge-switchbot/blob/latest/CHANGELOG.md" target="_blank">Changelog</a>
112
+ </li>
113
+ </ul>
114
+ </div>
115
+ <script>
116
+ (async () => {
117
+ try {
118
+ const currentConfig = await homebridge.getPluginConfig();
119
+ showIntro = () => {
120
+ const introLink = document.getElementById('introLink');
121
+ introLink.addEventListener('click', () => {
122
+ homebridge.showSpinner();
123
+ document.getElementById('pageIntro').style.display = 'none';
124
+ document.getElementById('menuWrapper').style.display = 'inline-flex';
125
+ showSettings();
126
+ homebridge.hideSpinner();
127
+ });
128
+ document.getElementById('pageIntro').style.display = 'block';
129
+ };
130
+ showDevices = async () => {
131
+ homebridge.showSpinner();
132
+ homebridge.hideSchemaForm();
133
+ document.getElementById('menuHome').classList.remove('btn-elegant');
134
+ document.getElementById('menuHome').classList.add('btn-primary');
135
+ document.getElementById('menuDevices').classList.add('btn-elegant');
136
+ document.getElementById('menuDevices').classList.remove('btn-primary');
137
+ document.getElementById('menuSettings').classList.remove('btn-elegant');
138
+ document.getElementById('menuSettings').classList.add('btn-primary');
139
+ document.getElementById('pageSupport').style.display = 'none';
140
+ document.getElementById('pageDevices').style.display = 'block';
141
+ const cachedAccessories =
142
+ typeof homebridge.getCachedAccessories === 'function'
143
+ ? await homebridge.getCachedAccessories()
144
+ : await homebridge.request('/getCachedAccessories');
145
+ if (cachedAccessories.length > 0) {
146
+ cachedAccessories.sort((a, b) => {
147
+ return a.displayName.toLowerCase() > b.displayName.toLowerCase() ? 1 : b.displayName.toLowerCase() > a.displayName.toLowerCase() ? -1 : 0;
148
+ });
149
+ }
150
+ const deviceSelect = document.getElementById('deviceSelect');
151
+ deviceSelect.innerHTML = '';
152
+ cachedAccessories.forEach((a) => {
153
+ const option = document.createElement('option');
154
+ option.text = a.displayName;
155
+ option.value = a.UUID;
156
+ deviceSelect.add(option);
157
+ });
158
+ showDeviceInfo = async (UUID) => {
159
+ homebridge.showSpinner();
160
+ const thisAcc = cachedAccessories.find((x) => x.UUID === UUID);
161
+ const context = thisAcc.context;
162
+ document.getElementById('displayName').innerHTML = thisAcc.displayName;
163
+ document.getElementById('deviceID').innerHTML = context.deviceID;
164
+ document.getElementById('model').innerHTML = context.model;
165
+ document.getElementById('firmwareRevision').innerHTML = context.firmwareRevision || 'N/A';
166
+ document.getElementById('deviceType').innerHTML = context.deviceType;
167
+ document.getElementById('connectionType').innerHTML = context.connectionType;
168
+ document.getElementById('deviceTable').style.display = 'inline-table';
169
+ homebridge.hideSpinner();
170
+ };
171
+ deviceSelect.addEventListener('change', (event) => showDeviceInfo(event.target.value));
172
+ if (cachedAccessories.length > 0) {
173
+ showDeviceInfo(cachedAccessories[0].UUID);
174
+ } else {
175
+ const option = document.createElement('option');
176
+ option.text = 'No Devices';
177
+ deviceSelect.add(option);
178
+ deviceSelect.disabled = true;
179
+ }
180
+ homebridge.hideSpinner();
181
+ };
182
+ showSupport = () => {
183
+ homebridge.showSpinner();
184
+ homebridge.hideSchemaForm();
185
+ document.getElementById('menuHome').classList.add('btn-elegant');
186
+ document.getElementById('menuHome').classList.remove('btn-primary');
187
+ document.getElementById('menuDevices').classList.remove('btn-elegant');
188
+ document.getElementById('menuDevices').classList.add('btn-primary');
189
+ document.getElementById('menuSettings').classList.remove('btn-elegant');
190
+ document.getElementById('menuSettings').classList.add('btn-primary');
191
+ document.getElementById('pageSupport').style.display = 'block';
192
+ document.getElementById('pageDevices').style.display = 'none';
193
+ homebridge.hideSpinner();
194
+ };
195
+ showSettings = () => {
196
+ homebridge.showSpinner();
197
+ document.getElementById('menuHome').classList.remove('btn-elegant');
198
+ document.getElementById('menuHome').classList.add('btn-primary');
199
+ document.getElementById('menuDevices').classList.remove('btn-elegant');
200
+ document.getElementById('menuDevices').classList.add('btn-primary');
201
+ document.getElementById('menuSettings').classList.add('btn-elegant');
202
+ document.getElementById('menuSettings').classList.remove('btn-primary');
203
+ document.getElementById('pageSupport').style.display = 'none';
204
+ document.getElementById('pageDevices').style.display = 'none';
205
+ homebridge.showSchemaForm();
206
+ homebridge.hideSpinner();
207
+ };
208
+ showDisabledBanner = () => {
209
+ document.getElementById('disabledBanner').style.display = 'block';
210
+ };
211
+ enablePlugin = async () => {
212
+ homebridge.showSpinner();
213
+ document.getElementById('disabledBanner').style.display = 'none';
214
+ currentConfig[0].disablePlugin = false;
215
+ await homebridge.updatePluginConfig(currentConfig);
216
+ await homebridge.savePluginConfig();
217
+ homebridge.hideSpinner();
218
+ };
219
+ menuHome.addEventListener('click', () => showSupport());
220
+ menuDevices.addEventListener('click', () => showDevices());
221
+ menuSettings.addEventListener('click', () => showSettings());
222
+ disabledEnable.addEventListener('click', () => enablePlugin());
223
+ if (currentConfig.length) {
224
+ document.getElementById('menuWrapper').style.display = 'inline-flex';
225
+ showSettings();
226
+ if (currentConfig[0].disablePlugin) {
227
+ showDisabledBanner();
228
+ }
229
+ } else {
230
+ currentConfig.push({ name: 'SwitchBot' });
231
+ await homebridge.updatePluginConfig(currentConfig);
232
+ showIntro();
233
+ }
234
+ } catch (err) {
235
+ homebridge.toast.error(err.message, 'Error');
236
+ } finally {
237
+ homebridge.hideSpinner();
238
+ }
239
+ })();
240
+ </script>
@@ -0,0 +1,48 @@
1
+ /* eslint-disable no-console */
2
+ import { HomebridgePluginUiServer } from '@homebridge/plugin-ui-utils';
3
+ import fs from 'fs';
4
+
5
+ class PluginUiServer extends HomebridgePluginUiServer {
6
+ constructor() {
7
+ super();
8
+ /*
9
+ A native method getCachedAccessories() was introduced in config-ui-x v4.37.0
10
+ The following is for users who have a lower version of config-ui-x
11
+ */
12
+ this.onRequest('getCachedAccessories', () => {
13
+ try {
14
+ const plugin = 'homebridge-switchbot';
15
+ const devicesToReturn = [];
16
+
17
+ // The path and file of the cached accessories
18
+ const accFile = this.homebridgeStoragePath + '/accessories/cachedAccessories';
19
+
20
+ // Check the file exists
21
+ if (fs.existsSync(accFile)) {
22
+ // read the cached accessories file
23
+ const cachedAccessories: any[] = JSON.parse(fs.readFileSync(accFile, 'utf8'));
24
+
25
+ cachedAccessories.forEach((accessory: any) => {
26
+ // Check the accessory is from this plugin
27
+ if (accessory.plugin === plugin) {
28
+ // Add the cached accessory to the array
29
+ devicesToReturn.push(accessory.accessory as never);
30
+ }
31
+ });
32
+ }
33
+ // Return the array
34
+ return devicesToReturn;
35
+ } catch (err) {
36
+ // Just return an empty accessory list in case of any errors
37
+ return [];
38
+ }
39
+ });
40
+ this.ready();
41
+ }
42
+ }
43
+
44
+ function startPluginUiServer(): PluginUiServer {
45
+ return new PluginUiServer();
46
+ }
47
+
48
+ startPluginUiServer();
package/src/index.ts CHANGED
@@ -1,10 +1,13 @@
1
+ /* Copyright(C) 2021-2023, SwitchBot (https://github.com/SwitchBot). All rights reserved.
2
+ *
3
+ * index.ts: @switchbot/homebridge-switchbot plugin registration.
4
+ */
5
+ import { PLATFORM_NAME, PLUGIN_NAME } from './settings.js';
1
6
  import { API } from 'homebridge';
2
- import { PLATFORM_NAME } from './settings';
3
- import { SwitchBotPlatform } from './platform';
7
+ import { SwitchBotPlatform } from './platform.js';
4
8
 
5
- /**
6
- * This method registers the platform with Homebridge
7
- */
8
- export = (api: API): void => {
9
- api.registerPlatform(PLATFORM_NAME, SwitchBotPlatform);
9
+ // Register our platform with homebridge.
10
+ export default (api: API): void => {
11
+
12
+ api.registerPlatform(PLUGIN_NAME, PLATFORM_NAME, SwitchBotPlatform);
10
13
  };