@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
@@ -6,8 +6,8 @@ import { connectAsync } from 'async-mqtt';
6
6
  import { interval, Subject } from 'rxjs';
7
7
  import { skipWhile } from 'rxjs/operators';
8
8
  import { SwitchBotPlatform } from '../platform';
9
- import { Service, PlatformAccessory, Units, CharacteristicValue } from 'homebridge';
10
- import { device, devicesConfig, serviceData, ad, temperature, deviceStatus, Devices } from '../settings';
9
+ import { Service, PlatformAccessory, Units, CharacteristicValue, API, Logging, HAP } from 'homebridge';
10
+ import { device, devicesConfig, serviceData, ad, temperature, deviceStatus, Devices, SwitchBotPlatformConfig } from '../settings';
11
11
 
12
12
  /**
13
13
  * Platform Accessory
@@ -15,6 +15,10 @@ import { device, devicesConfig, serviceData, ad, temperature, deviceStatus, Devi
15
15
  * Each accessory may expose multiple services of different service types.
16
16
  */
17
17
  export class IOSensor {
18
+ public readonly api: API;
19
+ public readonly log: Logging;
20
+ public readonly config!: SwitchBotPlatformConfig;
21
+ protected readonly hap: HAP;
18
22
  // Services
19
23
  batteryService: Service;
20
24
  humidityservice?: Service;
@@ -59,22 +63,29 @@ export class IOSensor {
59
63
  doIOSensorUpdate: Subject<void>;
60
64
 
61
65
  // Connection
62
- private readonly BLE = this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI';
63
- private readonly OpenAPI = this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI';
66
+ private readonly OpenAPI: boolean;
67
+ private readonly BLE: boolean;
64
68
 
65
69
  constructor(
66
70
  private readonly platform: SwitchBotPlatform,
67
71
  private accessory: PlatformAccessory,
68
72
  public device: device & devicesConfig,
69
73
  ) {
74
+ this.api = this.platform.api;
75
+ this.log = this.platform.log;
76
+ this.config = this.platform.config;
77
+ this.hap = this.api.hap;
78
+ // Connection
79
+ this.BLE = this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI';
80
+ this.OpenAPI = this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI';
70
81
  // default placeholders
71
- this.logs(device);
82
+ this.deviceLogs(device);
72
83
  this.scan(device);
73
84
  this.refreshRate(device);
74
85
  this.context();
75
86
  this.setupHistoryService(device);
76
87
  this.setupMqtt(device);
77
- this.config(device);
88
+ this.deviceConfig(device);
78
89
 
79
90
  // this is subject we use to track when we need to POST changes to the SwitchBot API
80
91
  this.doIOSensorUpdate = new Subject();
@@ -85,29 +96,29 @@ export class IOSensor {
85
96
 
86
97
  // set accessory information
87
98
  accessory
88
- .getService(this.platform.Service.AccessoryInformation)!
89
- .setCharacteristic(this.platform.Characteristic.Manufacturer, 'SwitchBot')
90
- .setCharacteristic(this.platform.Characteristic.Model, 'WoIOSensor')
91
- .setCharacteristic(this.platform.Characteristic.SerialNumber, device.deviceId)
92
- .setCharacteristic(this.platform.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision);
99
+ .getService(this.hap.Service.AccessoryInformation)!
100
+ .setCharacteristic(this.hap.Characteristic.Manufacturer, 'SwitchBot')
101
+ .setCharacteristic(this.hap.Characteristic.Model, 'WoIOSensor')
102
+ .setCharacteristic(this.hap.Characteristic.SerialNumber, device.deviceId)
103
+ .setCharacteristic(this.hap.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision);
93
104
 
94
105
  // Temperature Sensor Service
95
106
  if (device.meter?.hide_temperature) {
96
107
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Temperature Sensor Service`);
97
- this.temperatureservice = this.accessory.getService(this.platform.Service.TemperatureSensor);
108
+ this.temperatureservice = this.accessory.getService(this.hap.Service.TemperatureSensor);
98
109
  accessory.removeService(this.temperatureservice!);
99
110
  } else if (!this.temperatureservice) {
100
111
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Temperature Sensor Service`);
101
112
  const temperatureservice = `${accessory.displayName} Temperature Sensor`;
102
- (this.temperatureservice = this.accessory.getService(this.platform.Service.TemperatureSensor)
103
- || this.accessory.addService(this.platform.Service.TemperatureSensor)), temperatureservice;
113
+ (this.temperatureservice = this.accessory.getService(this.hap.Service.TemperatureSensor)
114
+ || this.accessory.addService(this.hap.Service.TemperatureSensor)), temperatureservice;
104
115
 
105
- this.temperatureservice.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Temperature Sensor`);
106
- if (!this.temperatureservice.testCharacteristic(this.platform.Characteristic.ConfiguredName)) {
107
- this.temperatureservice.addCharacteristic(this.platform.Characteristic.ConfiguredName, `${accessory.displayName} Temperature Sensor`);
116
+ this.temperatureservice.setCharacteristic(this.hap.Characteristic.Name, `${accessory.displayName} Temperature Sensor`);
117
+ if (!this.temperatureservice.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
118
+ this.temperatureservice.addCharacteristic(this.hap.Characteristic.ConfiguredName, `${accessory.displayName} Temperature Sensor`);
108
119
  }
109
120
  this.temperatureservice
110
- .getCharacteristic(this.platform.Characteristic.CurrentTemperature)
121
+ .getCharacteristic(this.hap.Characteristic.CurrentTemperature)
111
122
  .setProps({
112
123
  unit: Units['CELSIUS'],
113
124
  validValueRanges: [-273.15, 100],
@@ -125,20 +136,20 @@ export class IOSensor {
125
136
  // Humidity Sensor Service
126
137
  if (device.meter?.hide_humidity) {
127
138
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Humidity Sensor Service`);
128
- this.humidityservice = this.accessory.getService(this.platform.Service.HumiditySensor);
139
+ this.humidityservice = this.accessory.getService(this.hap.Service.HumiditySensor);
129
140
  accessory.removeService(this.humidityservice!);
130
141
  } else if (!this.humidityservice) {
131
142
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Humidity Sensor Service`);
132
143
  const humidityservice = `${accessory.displayName} Humidity Sensor`;
133
- (this.humidityservice = this.accessory.getService(this.platform.Service.HumiditySensor)
134
- || this.accessory.addService(this.platform.Service.HumiditySensor)), humidityservice;
144
+ (this.humidityservice = this.accessory.getService(this.hap.Service.HumiditySensor)
145
+ || this.accessory.addService(this.hap.Service.HumiditySensor)), humidityservice;
135
146
 
136
- this.humidityservice.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Humidity Sensor`);
137
- if (!this.humidityservice.testCharacteristic(this.platform.Characteristic.ConfiguredName)) {
138
- this.humidityservice.addCharacteristic(this.platform.Characteristic.ConfiguredName, `${accessory.displayName} Humidity Sensor`);
147
+ this.humidityservice.setCharacteristic(this.hap.Characteristic.Name, `${accessory.displayName} Humidity Sensor`);
148
+ if (!this.humidityservice.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
149
+ this.humidityservice.addCharacteristic(this.hap.Characteristic.ConfiguredName, `${accessory.displayName} Humidity Sensor`);
139
150
  }
140
151
  this.humidityservice
141
- .getCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity)
152
+ .getCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity)
142
153
  .setProps({
143
154
  minStep: 0.1,
144
155
  })
@@ -151,14 +162,14 @@ export class IOSensor {
151
162
 
152
163
  // Battery Service
153
164
  const batteryService = `${accessory.displayName} Battery`;
154
- (this.batteryService = this.accessory.getService(this.platform.Service.Battery)
155
- || accessory.addService(this.platform.Service.Battery)), batteryService;
165
+ (this.batteryService = this.accessory.getService(this.hap.Service.Battery)
166
+ || accessory.addService(this.hap.Service.Battery)), batteryService;
156
167
 
157
- this.batteryService.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Battery`);
158
- if (!this.batteryService.testCharacteristic(this.platform.Characteristic.ConfiguredName)) {
159
- this.batteryService.addCharacteristic(this.platform.Characteristic.ConfiguredName, `${accessory.displayName} Battery`);
168
+ this.batteryService.setCharacteristic(this.hap.Characteristic.Name, `${accessory.displayName} Battery`);
169
+ if (!this.batteryService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
170
+ this.batteryService.addCharacteristic(this.hap.Characteristic.ConfiguredName, `${accessory.displayName} Battery`);
160
171
  }
161
- this.batteryService.setCharacteristic(this.platform.Characteristic.ChargingState, this.platform.Characteristic.ChargingState.NOT_CHARGEABLE);
172
+ this.batteryService.setCharacteristic(this.hap.Characteristic.ChargingState, this.hap.Characteristic.ChargingState.NOT_CHARGEABLE);
162
173
 
163
174
  // Retrieve initial values and updateHomekit
164
175
  this.updateHomeKitCharacteristics();
@@ -180,16 +191,16 @@ export class IOSensor {
180
191
  const { temperature, humidity } = context;
181
192
  const { CurrentTemperature, CurrentRelativeHumidity } = this;
182
193
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ` +
183
- '(temperature, humidity) = ' +
184
- `Webhook:(${temperature}, ${humidity}), ` +
185
- `current:(${CurrentTemperature}, ${CurrentRelativeHumidity})`);
194
+ '(temperature, humidity) = ' +
195
+ `Webhook:(${temperature}, ${humidity}), ` +
196
+ `current:(${CurrentTemperature}, ${CurrentRelativeHumidity})`);
186
197
  this.CurrentRelativeHumidity = humidity;
187
198
  this.CurrentTemperature = temperature;
188
199
  this.updateHomeKitCharacteristics();
189
200
  }
190
201
  } catch (e: any) {
191
202
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
192
- + `failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
203
+ + `failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
193
204
  }
194
205
  };
195
206
  }
@@ -219,9 +230,9 @@ export class IOSensor {
219
230
  // Battery
220
231
  this.BatteryLevel = Number(this.BLE_BatteryLevel);
221
232
  if (this.BatteryLevel < 15) {
222
- this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
233
+ this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
223
234
  } else {
224
- this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
235
+ this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
225
236
  }
226
237
  this.debugLog(`${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}, StatusLowBattery: ${this.StatusLowBattery}`);
227
238
 
@@ -244,9 +255,9 @@ export class IOSensor {
244
255
  // Battery
245
256
  this.BatteryLevel = Number(this.OpenAPI_BatteryLevel);
246
257
  if (this.BatteryLevel < 15) {
247
- this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
258
+ this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
248
259
  } else {
249
- this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
260
+ this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
250
261
  }
251
262
  this.debugLog(`${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}, StatusLowBattery: ${this.StatusLowBattery}`);
252
263
 
@@ -265,9 +276,9 @@ export class IOSensor {
265
276
  // BatteryLevel
266
277
  this.BatteryLevel = Number(this.OpenAPI_BatteryLevel);
267
278
  if (this.BatteryLevel < 10) {
268
- this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
279
+ this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
269
280
  } else {
270
- this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
281
+ this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
271
282
  }
272
283
  if (Number.isNaN(this.BatteryLevel)) {
273
284
  this.BatteryLevel = 100;
@@ -413,7 +424,7 @@ export class IOSensor {
413
424
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
414
425
  } else {
415
426
  this.accessory.context.CurrentRelativeHumidity = this.CurrentRelativeHumidity;
416
- this.humidityservice?.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, this.CurrentRelativeHumidity);
427
+ this.humidityservice?.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity, this.CurrentRelativeHumidity);
417
428
  this.debugLog(
418
429
  `${this.device.deviceType}: ${this.accessory.displayName}` +
419
430
  ` updateCharacteristic CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`,
@@ -437,7 +448,7 @@ export class IOSensor {
437
448
  entry['temp'] = this.CurrentTemperature;
438
449
  }
439
450
  this.accessory.context.CurrentTemperature = this.CurrentTemperature;
440
- this.temperatureservice?.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, this.CurrentTemperature);
451
+ this.temperatureservice?.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, this.CurrentTemperature);
441
452
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic CurrentTemperature: ${this.CurrentTemperature}`);
442
453
  }
443
454
  }
@@ -448,7 +459,7 @@ export class IOSensor {
448
459
  mqttmessage.push(`"battery": ${this.BatteryLevel}`);
449
460
  }
450
461
  this.accessory.context.BatteryLevel = this.BatteryLevel;
451
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, this.BatteryLevel);
462
+ this.batteryService?.updateCharacteristic(this.hap.Characteristic.BatteryLevel, this.BatteryLevel);
452
463
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
453
464
  }
454
465
  if (this.StatusLowBattery === undefined) {
@@ -458,7 +469,7 @@ export class IOSensor {
458
469
  mqttmessage.push(`"lowBattery": ${this.StatusLowBattery}`);
459
470
  }
460
471
  this.accessory.context.StatusLowBattery = this.StatusLowBattery;
461
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.StatusLowBattery);
472
+ this.batteryService?.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, this.StatusLowBattery);
462
473
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
463
474
  }
464
475
  if (this.device.mqttURL) {
@@ -622,13 +633,13 @@ export class IOSensor {
622
633
 
623
634
  async apiError(e: any): Promise<void> {
624
635
  if (!this.device.meter?.hide_humidity) {
625
- this.humidityservice?.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, e);
636
+ this.humidityservice?.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity, e);
626
637
  }
627
638
  if (!this.device.meter?.hide_temperature) {
628
- this.temperatureservice?.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, e);
639
+ this.temperatureservice?.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, e);
629
640
  }
630
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, e);
631
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, e);
641
+ this.batteryService?.updateCharacteristic(this.hap.Characteristic.BatteryLevel, e);
642
+ this.batteryService?.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, e);
632
643
  }
633
644
 
634
645
  async context() {
@@ -648,7 +659,7 @@ export class IOSensor {
648
659
  this.BatteryLevel = this.accessory.context.BatteryLevel;
649
660
  }
650
661
  if (this.StatusLowBattery === undefined) {
651
- this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
662
+ this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
652
663
  this.accessory.context.StatusLowBattery = this.StatusLowBattery;
653
664
  } else {
654
665
  this.StatusLowBattery = this.accessory.context.StatusLowBattery;
@@ -669,7 +680,7 @@ export class IOSensor {
669
680
  }
670
681
  }
671
682
 
672
- async config(device: device & devicesConfig): Promise<void> {
683
+ async deviceConfig(device: device & devicesConfig): Promise<void> {
673
684
  let config = {};
674
685
  if (device.meter) {
675
686
  config = device.meter;
@@ -697,7 +708,7 @@ export class IOSensor {
697
708
  }
698
709
  }
699
710
 
700
- async logs(device: device & devicesConfig): Promise<void> {
711
+ async deviceLogs(device: device & devicesConfig): Promise<void> {
701
712
  if (this.platform.debugMode) {
702
713
  this.deviceLogging = this.accessory.context.logging = 'debugMode';
703
714
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
@@ -3,8 +3,10 @@ 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, ControllerConstructor, Controller, ControllerServiceMap } from 'homebridge';
7
- import { device, devicesConfig, hs2rgb, rgb2hs, deviceStatus, ad, serviceData, m2hs, Devices } from '../settings';
6
+ import {
7
+ Service, PlatformAccessory, CharacteristicValue, ControllerConstructor, Controller, ControllerServiceMap, API, Logging, HAP,
8
+ } from 'homebridge';
9
+ import { device, devicesConfig, hs2rgb, rgb2hs, deviceStatus, ad, serviceData, m2hs, Devices, SwitchBotPlatformConfig } from '../settings';
8
10
 
9
11
  /**
10
12
  * Platform Accessory
@@ -12,6 +14,10 @@ import { device, devicesConfig, hs2rgb, rgb2hs, deviceStatus, ad, serviceData, m
12
14
  * Each accessory may expose multiple services of different service types.
13
15
  */
14
16
  export class StripLight {
17
+ public readonly api: API;
18
+ public readonly log: Logging;
19
+ public readonly config!: SwitchBotPlatformConfig;
20
+ protected readonly hap: HAP;
15
21
  // Services
16
22
  lightBulbService!: Service;
17
23
 
@@ -57,20 +63,27 @@ export class StripLight {
57
63
  doStripLightUpdate!: Subject<void>;
58
64
 
59
65
  // Connection
60
- private readonly BLE = this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI';
61
- private readonly OpenAPI = this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI';
66
+ private readonly OpenAPI: boolean;
67
+ private readonly BLE: boolean;
62
68
 
63
69
  constructor(
64
70
  private readonly platform: SwitchBotPlatform,
65
71
  private accessory: PlatformAccessory,
66
72
  public device: device & devicesConfig,
67
73
  ) {
74
+ this.api = this.platform.api;
75
+ this.log = this.platform.log;
76
+ this.config = this.platform.config;
77
+ this.hap = this.api.hap;
78
+ // Connection
79
+ this.BLE = this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI';
80
+ this.OpenAPI = this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI';
68
81
  // default placeholders
69
- this.logs(device);
82
+ this.deviceLogs(device);
70
83
  this.scan(device);
71
84
  this.refreshRate(device);
72
85
  this.context();
73
- this.config(device);
86
+ this.deviceConfig(device);
74
87
 
75
88
  // this is subject we use to track when we need to POST changes to the SwitchBot API
76
89
  this.doStripLightUpdate = new Subject();
@@ -81,35 +94,35 @@ export class StripLight {
81
94
 
82
95
  // set accessory information
83
96
  accessory
84
- .getService(this.platform.Service.AccessoryInformation)!
85
- .setCharacteristic(this.platform.Characteristic.Manufacturer, 'SwitchBot')
86
- .setCharacteristic(this.platform.Characteristic.Model, 'W1701100')
87
- .setCharacteristic(this.platform.Characteristic.SerialNumber, device.deviceId)
88
- .setCharacteristic(this.platform.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision);
97
+ .getService(this.hap.Service.AccessoryInformation)!
98
+ .setCharacteristic(this.hap.Characteristic.Manufacturer, 'SwitchBot')
99
+ .setCharacteristic(this.hap.Characteristic.Model, 'W1701100')
100
+ .setCharacteristic(this.hap.Characteristic.SerialNumber, device.deviceId)
101
+ .setCharacteristic(this.hap.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision);
89
102
 
90
103
  // get the Lightbulb service if it exists, otherwise create a new Lightbulb service
91
104
  // you can create multiple services for each accessory
92
105
  const lightBulbService = `${accessory.displayName} ${device.deviceType}`;
93
- (this.lightBulbService = accessory.getService(this.platform.Service.Lightbulb)
94
- || accessory.addService(this.platform.Service.Lightbulb)), lightBulbService;
106
+ (this.lightBulbService = accessory.getService(this.hap.Service.Lightbulb)
107
+ || accessory.addService(this.hap.Service.Lightbulb)), lightBulbService;
95
108
 
96
109
  if (this.adaptiveLightingShift === -1 && this.accessory.context.adaptiveLighting) {
97
110
  this.accessory.removeService(this.lightBulbService);
98
- this.lightBulbService = this.accessory.addService(this.platform.Service.Lightbulb);
111
+ this.lightBulbService = this.accessory.addService(this.hap.Service.Lightbulb);
99
112
  this.accessory.context.adaptiveLighting = false;
100
113
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} adaptiveLighting: ${this.accessory.context.adaptiveLighting}`);
101
114
  }
102
115
 
103
- this.lightBulbService.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
104
- if (!this.lightBulbService.testCharacteristic(this.platform.Characteristic.ConfiguredName)) {
105
- this.lightBulbService.addCharacteristic(this.platform.Characteristic.ConfiguredName, accessory.displayName);
116
+ this.lightBulbService.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
117
+ if (!this.lightBulbService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
118
+ this.lightBulbService.addCharacteristic(this.hap.Characteristic.ConfiguredName, accessory.displayName);
106
119
  }
107
120
  // handle on / off events using the On characteristic
108
- this.lightBulbService.getCharacteristic(this.platform.Characteristic.On).onSet(this.OnSet.bind(this));
121
+ this.lightBulbService.getCharacteristic(this.hap.Characteristic.On).onSet(this.OnSet.bind(this));
109
122
 
110
123
  // handle Brightness events using the Brightness characteristic
111
124
  this.lightBulbService
112
- .getCharacteristic(this.platform.Characteristic.Brightness)
125
+ .getCharacteristic(this.hap.Characteristic.Brightness)
113
126
  .setProps({
114
127
  minStep: this.minStep(device),
115
128
  minValue: 0,
@@ -123,7 +136,7 @@ export class StripLight {
123
136
 
124
137
  // handle ColorTemperature events using the ColorTemperature characteristic
125
138
  this.lightBulbService
126
- .getCharacteristic(this.platform.Characteristic.ColorTemperature)
139
+ .getCharacteristic(this.hap.Characteristic.ColorTemperature)
127
140
  .setProps({
128
141
  minValue: 140,
129
142
  maxValue: 500,
@@ -136,7 +149,7 @@ export class StripLight {
136
149
 
137
150
  // handle Hue events using the Hue characteristic
138
151
  this.lightBulbService
139
- .getCharacteristic(this.platform.Characteristic.Hue)
152
+ .getCharacteristic(this.hap.Characteristic.Hue)
140
153
  .setProps({
141
154
  minValue: 0,
142
155
  maxValue: 360,
@@ -149,7 +162,7 @@ export class StripLight {
149
162
 
150
163
  // handle Hue events using the Hue characteristic
151
164
  this.lightBulbService
152
- .getCharacteristic(this.platform.Characteristic.Saturation)
165
+ .getCharacteristic(this.hap.Characteristic.Saturation)
153
166
  .setProps({
154
167
  minValue: 0,
155
168
  maxValue: 100,
@@ -192,9 +205,9 @@ export class StripLight {
192
205
  const { powerState, brightness, color, colorTemperature } = context;
193
206
  const { On, Brightness, Hue, Saturation, ColorTemperature } = this;
194
207
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ` +
195
- '(powerState, brightness, color, colorTemperature) = ' +
196
- `Webhook:(${powerState}, ${brightness}, ${color}, ${colorTemperature}), ` +
197
- `current:(${On}, ${Brightness}, ${Hue}, ${Saturation}, ${ColorTemperature})`);
208
+ '(powerState, brightness, color, colorTemperature) = ' +
209
+ `Webhook:(${powerState}, ${brightness}, ${color}, ${colorTemperature}), ` +
210
+ `current:(${On}, ${Brightness}, ${Hue}, ${Saturation}, ${ColorTemperature})`);
198
211
  this.On = powerState === 'ON' ? true : false;
199
212
  this.Brightness = brightness;
200
213
 
@@ -220,7 +233,7 @@ export class StripLight {
220
233
  this.updateHomeKitCharacteristics();
221
234
  } catch (e: any) {
222
235
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
223
- + `failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
236
+ + `failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
224
237
  }
225
238
  };
226
239
  }
@@ -816,8 +829,8 @@ export class StripLight {
816
829
 
817
830
  // Updating the hue/sat to the corresponding values mimics native adaptive lighting
818
831
  const hs = m2hs(value);
819
- this.lightBulbService.updateCharacteristic(this.platform.Characteristic.Hue, hs[0]);
820
- this.lightBulbService.updateCharacteristic(this.platform.Characteristic.Saturation, hs[1]);
832
+ this.lightBulbService.updateCharacteristic(this.hap.Characteristic.Hue, hs[0]);
833
+ this.lightBulbService.updateCharacteristic(this.hap.Characteristic.Saturation, hs[1]);
821
834
 
822
835
  this.ColorTemperature = value;
823
836
  this.doStripLightUpdate.next();
@@ -835,7 +848,7 @@ export class StripLight {
835
848
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Hue: ${value}`);
836
849
  }
837
850
 
838
- this.lightBulbService.updateCharacteristic(this.platform.Characteristic.ColorTemperature, 140);
851
+ this.lightBulbService.updateCharacteristic(this.hap.Characteristic.ColorTemperature, 140);
839
852
 
840
853
  this.Hue = value;
841
854
  this.doStripLightUpdate.next();
@@ -853,7 +866,7 @@ export class StripLight {
853
866
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Saturation: ${value}`);
854
867
  }
855
868
 
856
- this.lightBulbService.updateCharacteristic(this.platform.Characteristic.ColorTemperature, 140);
869
+ this.lightBulbService.updateCharacteristic(this.hap.Characteristic.ColorTemperature, 140);
857
870
 
858
871
  this.Saturation = value;
859
872
  this.doStripLightUpdate.next();
@@ -865,7 +878,7 @@ export class StripLight {
865
878
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
866
879
  } else {
867
880
  this.accessory.context.On = this.On;
868
- this.lightBulbService.updateCharacteristic(this.platform.Characteristic.On, this.On);
881
+ this.lightBulbService.updateCharacteristic(this.hap.Characteristic.On, this.On);
869
882
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
870
883
  }
871
884
  // Brightness
@@ -873,7 +886,7 @@ export class StripLight {
873
886
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Brightness: ${this.Brightness}`);
874
887
  } else {
875
888
  this.accessory.context.Brightness = this.Brightness;
876
- this.lightBulbService.updateCharacteristic(this.platform.Characteristic.Brightness, this.Brightness);
889
+ this.lightBulbService.updateCharacteristic(this.hap.Characteristic.Brightness, this.Brightness);
877
890
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Brightness: ${this.Brightness}`);
878
891
  }
879
892
  // ColorTemperature
@@ -881,7 +894,7 @@ export class StripLight {
881
894
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ColorTemperature: ${this.ColorTemperature}`);
882
895
  } else {
883
896
  this.accessory.context.ColorTemperature = this.ColorTemperature;
884
- this.lightBulbService.updateCharacteristic(this.platform.Characteristic.ColorTemperature, this.ColorTemperature);
897
+ this.lightBulbService.updateCharacteristic(this.hap.Characteristic.ColorTemperature, this.ColorTemperature);
885
898
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic ColorTemperature: ${this.ColorTemperature}`);
886
899
  }
887
900
  // Hue
@@ -889,7 +902,7 @@ export class StripLight {
889
902
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${this.Hue}`);
890
903
  } else {
891
904
  this.accessory.context.Hue = this.Hue;
892
- this.lightBulbService.updateCharacteristic(this.platform.Characteristic.Hue, this.Hue);
905
+ this.lightBulbService.updateCharacteristic(this.hap.Characteristic.Hue, this.Hue);
893
906
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Hue: ${this.Hue}`);
894
907
  }
895
908
  // Saturation
@@ -897,7 +910,7 @@ export class StripLight {
897
910
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${this.Saturation}`);
898
911
  } else {
899
912
  this.accessory.context.Saturation = this.Saturation;
900
- this.lightBulbService.updateCharacteristic(this.platform.Characteristic.Saturation, this.Saturation);
913
+ this.lightBulbService.updateCharacteristic(this.hap.Characteristic.Saturation, this.Saturation);
901
914
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Saturation: ${this.Saturation}`);
902
915
  }
903
916
  }
@@ -1049,10 +1062,10 @@ export class StripLight {
1049
1062
  }
1050
1063
 
1051
1064
  apiError(e: any): void {
1052
- this.lightBulbService.updateCharacteristic(this.platform.Characteristic.On, e);
1053
- this.lightBulbService.updateCharacteristic(this.platform.Characteristic.Hue, e);
1054
- this.lightBulbService.updateCharacteristic(this.platform.Characteristic.Brightness, e);
1055
- this.lightBulbService.updateCharacteristic(this.platform.Characteristic.Saturation, e);
1065
+ this.lightBulbService.updateCharacteristic(this.hap.Characteristic.On, e);
1066
+ this.lightBulbService.updateCharacteristic(this.hap.Characteristic.Hue, e);
1067
+ this.lightBulbService.updateCharacteristic(this.hap.Characteristic.Brightness, e);
1068
+ this.lightBulbService.updateCharacteristic(this.hap.Characteristic.Saturation, e);
1056
1069
  }
1057
1070
 
1058
1071
  async context() {
@@ -1099,7 +1112,7 @@ export class StripLight {
1099
1112
  }
1100
1113
  }
1101
1114
 
1102
- async config(device: device & devicesConfig): Promise<void> {
1115
+ async deviceConfig(device: device & devicesConfig): Promise<void> {
1103
1116
  let config = {};
1104
1117
  if (device.striplight) {
1105
1118
  config = device.striplight;
@@ -1130,7 +1143,7 @@ export class StripLight {
1130
1143
  }
1131
1144
  }
1132
1145
 
1133
- async logs(device: device & devicesConfig): Promise<void> {
1146
+ async deviceLogs(device: device & devicesConfig): Promise<void> {
1134
1147
  if (this.platform.debugMode) {
1135
1148
  this.deviceLogging = this.accessory.context.logging = 'debugMode';
1136
1149
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);