@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
@@ -1,11 +1,11 @@
1
1
  import { connectAsync } from 'async-mqtt';
2
- import { CharacteristicValue, PlatformAccessory, Service, Units } from 'homebridge';
2
+ import { CharacteristicValue, PlatformAccessory, Service, Units, API, Logging, HAP } from 'homebridge';
3
3
  import { MqttClient } from 'mqtt';
4
4
  import { hostname } from 'os';
5
5
  import { interval } from 'rxjs';
6
6
  import { request } from 'undici';
7
7
  import { SwitchBotPlatform } from '../platform';
8
- import { Devices, ad, device, deviceStatus, devicesConfig, serviceData, temperature } from '../settings';
8
+ import { Devices, ad, device, deviceStatus, devicesConfig, serviceData, temperature, SwitchBotPlatformConfig } from '../settings';
9
9
  import { sleep } from '../utils';
10
10
 
11
11
  /**
@@ -14,6 +14,10 @@ import { sleep } from '../utils';
14
14
  * Each accessory may expose multiple services of different service types.
15
15
  */
16
16
  export class MeterPlus {
17
+ public readonly api: API;
18
+ public readonly log: Logging;
19
+ public readonly config!: SwitchBotPlatformConfig;
20
+ protected readonly hap: HAP;
17
21
  // Services
18
22
  batteryService: Service;
19
23
  humidityService?: Service;
@@ -54,51 +58,58 @@ export class MeterPlus {
54
58
  deviceRefreshRate!: number;
55
59
 
56
60
  // Connection
57
- private readonly BLE = this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI';
58
- private readonly OpenAPI = this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI';
61
+ private readonly OpenAPI: boolean;
62
+ private readonly BLE: boolean;
59
63
 
60
64
  constructor(
61
65
  private readonly platform: SwitchBotPlatform,
62
66
  private accessory: PlatformAccessory,
63
67
  public device: device & devicesConfig,
64
68
  ) {
69
+ this.api = this.platform.api;
70
+ this.log = this.platform.log;
71
+ this.config = this.platform.config;
72
+ this.hap = this.api.hap;
73
+ // Connection
74
+ this.BLE = this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI';
75
+ this.OpenAPI = this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI';
65
76
  // default placeholders
66
- this.logs(device);
77
+ this.deviceLogs(device);
67
78
  this.scan(device);
68
79
  this.refreshRate(device);
69
80
  this.context();
70
81
  this.setupHistoryService(device);
71
82
  this.setupMqtt(device);
72
- this.config(device);
83
+ this.deviceConfig(device);
73
84
 
74
85
  // Retrieve initial values and updateHomekit
75
86
  this.refreshStatus();
76
87
 
77
88
  // set accessory information
78
89
  accessory
79
- .getService(this.platform.Service.AccessoryInformation)!
80
- .setCharacteristic(this.platform.Characteristic.Manufacturer, 'SwitchBot')
81
- .setCharacteristic(this.platform.Characteristic.Model, this.model(device))
82
- .setCharacteristic(this.platform.Characteristic.SerialNumber, device.deviceId)
83
- .setCharacteristic(this.platform.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision);
90
+ .getService(this.hap.Service.AccessoryInformation)!
91
+ .setCharacteristic(this.hap.Characteristic.Manufacturer, 'SwitchBot')
92
+ .setCharacteristic(this.hap.Characteristic.Model, this.model(device))
93
+ .setCharacteristic(this.hap.Characteristic.SerialNumber, device.deviceId)
94
+ .setCharacteristic(this.hap.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision);
84
95
 
85
96
  // Temperature Sensor Service
86
97
  if (device.meter?.hide_temperature) {
87
98
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Temperature Sensor Service`);
88
- this.temperatureService = this.accessory.getService(this.platform.Service.TemperatureSensor);
99
+ this.temperatureService = this.accessory.getService(this.hap.Service.TemperatureSensor);
89
100
  accessory.removeService(this.temperatureService!);
90
101
  } else if (!this.temperatureService) {
91
102
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Temperature Sensor Service`);
92
103
  const temperatureService = `${accessory.displayName} Temperature Sensor`;
93
- (this.temperatureService = this.accessory.getService(this.platform.Service.TemperatureSensor)
94
- || this.accessory.addService(this.platform.Service.TemperatureSensor)), temperatureService;
104
+ (this.temperatureService = this.accessory.getService(this.hap.Service.TemperatureSensor)
105
+ || this.accessory.addService(this.hap.Service.TemperatureSensor)), temperatureService;
95
106
 
96
- this.temperatureService.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Temperature Sensor`);
97
- if (!this.temperatureService.testCharacteristic(this.platform.Characteristic.ConfiguredName)) {
98
- this.temperatureService.addCharacteristic(this.platform.Characteristic.ConfiguredName, `${accessory.displayName} Temperature Sensor`);
107
+ this.temperatureService.setCharacteristic(this.hap.Characteristic.Name, `${accessory.displayName} Temperature Sensor`);
108
+ if (!this.temperatureService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
109
+ this.temperatureService.addCharacteristic(this.hap.Characteristic.ConfiguredName, `${accessory.displayName} Temperature Sensor`);
99
110
  }
100
111
  this.temperatureService
101
- .getCharacteristic(this.platform.Characteristic.CurrentTemperature)
112
+ .getCharacteristic(this.hap.Characteristic.CurrentTemperature)
102
113
  .setProps({
103
114
  unit: Units['CELSIUS'],
104
115
  validValueRanges: [-273.15, 100],
@@ -116,20 +127,20 @@ export class MeterPlus {
116
127
  // Humidity Sensor Service
117
128
  if (device.meter?.hide_humidity) {
118
129
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Humidity Sensor Service`);
119
- this.humidityService = this.accessory.getService(this.platform.Service.HumiditySensor);
130
+ this.humidityService = this.accessory.getService(this.hap.Service.HumiditySensor);
120
131
  accessory.removeService(this.humidityService!);
121
132
  } else if (!this.humidityService) {
122
133
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Humidity Sensor Service`);
123
134
  const humidityService = `${accessory.displayName} Humidity Sensor`;
124
- (this.humidityService = this.accessory.getService(this.platform.Service.HumiditySensor)
125
- || this.accessory.addService(this.platform.Service.HumiditySensor)), humidityService;
135
+ (this.humidityService = this.accessory.getService(this.hap.Service.HumiditySensor)
136
+ || this.accessory.addService(this.hap.Service.HumiditySensor)), humidityService;
126
137
 
127
- this.humidityService.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Humidity Sensor`);
128
- if (!this.humidityService.testCharacteristic(this.platform.Characteristic.ConfiguredName)) {
129
- this.humidityService.addCharacteristic(this.platform.Characteristic.ConfiguredName, `${accessory.displayName} Humidity Sensor`);
138
+ this.humidityService.setCharacteristic(this.hap.Characteristic.Name, `${accessory.displayName} Humidity Sensor`);
139
+ if (!this.humidityService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
140
+ this.humidityService.addCharacteristic(this.hap.Characteristic.ConfiguredName, `${accessory.displayName} Humidity Sensor`);
130
141
  }
131
142
  this.humidityService
132
- .getCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity)
143
+ .getCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity)
133
144
  .setProps({
134
145
  minStep: 0.1,
135
146
  })
@@ -142,14 +153,14 @@ export class MeterPlus {
142
153
 
143
154
  // Battery Service
144
155
  const batteryService = `${accessory.displayName} Battery`;
145
- (this.batteryService = this.accessory.getService(this.platform.Service.Battery)
146
- || accessory.addService(this.platform.Service.Battery)), batteryService;
156
+ (this.batteryService = this.accessory.getService(this.hap.Service.Battery)
157
+ || accessory.addService(this.hap.Service.Battery)), batteryService;
147
158
 
148
- this.batteryService.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Battery`);
149
- if (!this.batteryService.testCharacteristic(this.platform.Characteristic.ConfiguredName)) {
150
- this.batteryService.addCharacteristic(this.platform.Characteristic.ConfiguredName, `${accessory.displayName} Battery`);
159
+ this.batteryService.setCharacteristic(this.hap.Characteristic.Name, `${accessory.displayName} Battery`);
160
+ if (!this.batteryService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
161
+ this.batteryService.addCharacteristic(this.hap.Characteristic.ConfiguredName, `${accessory.displayName} Battery`);
151
162
  }
152
- this.batteryService.setCharacteristic(this.platform.Characteristic.ChargingState, this.platform.Characteristic.ChargingState.NOT_CHARGEABLE);
163
+ this.batteryService.setCharacteristic(this.hap.Characteristic.ChargingState, this.hap.Characteristic.ChargingState.NOT_CHARGEABLE);
153
164
 
154
165
  // Retrieve initial values and updateHomekit
155
166
  this.updateHomeKitCharacteristics();
@@ -170,16 +181,16 @@ export class MeterPlus {
170
181
  const { temperature, humidity } = context;
171
182
  const { CurrentTemperature, CurrentRelativeHumidity } = this;
172
183
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ` +
173
- '(temperature, humidity) = ' +
174
- `Webhook:(${temperature}, ${humidity}), ` +
175
- `current:(${CurrentTemperature}, ${CurrentRelativeHumidity})`);
184
+ '(temperature, humidity) = ' +
185
+ `Webhook:(${temperature}, ${humidity}), ` +
186
+ `current:(${CurrentTemperature}, ${CurrentRelativeHumidity})`);
176
187
  this.CurrentRelativeHumidity = humidity;
177
188
  this.CurrentTemperature = temperature;
178
189
  this.updateHomeKitCharacteristics();
179
190
  }
180
191
  } catch (e: any) {
181
192
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
182
- + `failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
193
+ + `failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
183
194
  }
184
195
  };
185
196
  }
@@ -209,9 +220,9 @@ export class MeterPlus {
209
220
  // Battery
210
221
  this.BatteryLevel = Number(this.BLE_BatteryLevel);
211
222
  if (this.BatteryLevel < 15) {
212
- this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
223
+ this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
213
224
  } else {
214
- this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
225
+ this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
215
226
  }
216
227
  this.debugLog(`${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}, StatusLowBattery: ${this.StatusLowBattery}`);
217
228
 
@@ -235,9 +246,9 @@ export class MeterPlus {
235
246
  // BatteryLevel
236
247
  this.BatteryLevel = Number(this.OpenAPI_BatteryLevel);
237
248
  if (this.BatteryLevel < 15) {
238
- this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
249
+ this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
239
250
  } else {
240
- this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
251
+ this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
241
252
  }
242
253
  this.debugLog(`${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}, StatusLowBattery: ${this.StatusLowBattery}`);
243
254
 
@@ -256,9 +267,9 @@ export class MeterPlus {
256
267
  // Battery
257
268
  this.BatteryLevel = Number(this.OpenAPI_BatteryLevel);
258
269
  if (this.BatteryLevel < 10) {
259
- this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
270
+ this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
260
271
  } else {
261
- this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
272
+ this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
262
273
  }
263
274
  if (Number.isNaN(this.BatteryLevel)) {
264
275
  this.BatteryLevel = 100;
@@ -412,7 +423,7 @@ export class MeterPlus {
412
423
  entry['humidity'] = this.CurrentRelativeHumidity;
413
424
  }
414
425
  this.accessory.context.CurrentRelativeHumidity = this.CurrentRelativeHumidity;
415
- this.humidityService?.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, this.CurrentRelativeHumidity);
426
+ this.humidityService?.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity, this.CurrentRelativeHumidity);
416
427
  this.debugLog(
417
428
  `${this.device.deviceType}: ${this.accessory.displayName}` +
418
429
  ` updateCharacteristic CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`,
@@ -431,7 +442,7 @@ export class MeterPlus {
431
442
  entry['temp'] = this.CurrentTemperature;
432
443
  }
433
444
  this.accessory.context.CurrentTemperature = this.CurrentTemperature;
434
- this.temperatureService?.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, this.CurrentTemperature);
445
+ this.temperatureService?.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, this.CurrentTemperature);
435
446
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic CurrentTemperature: ${this.CurrentTemperature}`);
436
447
  }
437
448
  }
@@ -443,7 +454,7 @@ export class MeterPlus {
443
454
  mqttmessage.push(`"battery": ${this.BatteryLevel}`);
444
455
  }
445
456
  this.accessory.context.BatteryLevel = this.BatteryLevel;
446
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, this.BatteryLevel);
457
+ this.batteryService?.updateCharacteristic(this.hap.Characteristic.BatteryLevel, this.BatteryLevel);
447
458
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
448
459
  }
449
460
  // StatusLowBattery
@@ -454,7 +465,7 @@ export class MeterPlus {
454
465
  mqttmessage.push(`"lowBattery": ${this.StatusLowBattery}`);
455
466
  }
456
467
  this.accessory.context.StatusLowBattery = this.StatusLowBattery;
457
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.StatusLowBattery);
468
+ this.batteryService?.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, this.StatusLowBattery);
458
469
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
459
470
  }
460
471
 
@@ -630,13 +641,13 @@ export class MeterPlus {
630
641
 
631
642
  async apiError(e: any): Promise<void> {
632
643
  if (!this.device.meter?.hide_humidity) {
633
- this.humidityService?.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, e);
644
+ this.humidityService?.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity, e);
634
645
  }
635
646
  if (!this.device.meter?.hide_temperature) {
636
- this.temperatureService?.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, e);
647
+ this.temperatureService?.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, e);
637
648
  }
638
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, e);
639
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, e);
649
+ this.batteryService?.updateCharacteristic(this.hap.Characteristic.BatteryLevel, e);
650
+ this.batteryService?.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, e);
640
651
  }
641
652
 
642
653
  async context() {
@@ -656,7 +667,7 @@ export class MeterPlus {
656
667
  this.BatteryLevel = this.accessory.context.BatteryLevel;
657
668
  }
658
669
  if (this.StatusLowBattery === undefined) {
659
- this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
670
+ this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
660
671
  this.accessory.context.StatusLowBattery = this.StatusLowBattery;
661
672
  } else {
662
673
  this.StatusLowBattery = this.accessory.context.StatusLowBattery;
@@ -677,7 +688,7 @@ export class MeterPlus {
677
688
  }
678
689
  }
679
690
 
680
- async config(device: device & devicesConfig): Promise<void> {
691
+ async deviceConfig(device: device & devicesConfig): Promise<void> {
681
692
  let config = {};
682
693
  if (device.meter) {
683
694
  config = device.meter;
@@ -705,7 +716,7 @@ export class MeterPlus {
705
716
  }
706
717
  }
707
718
 
708
- async logs(device: device & devicesConfig): Promise<void> {
719
+ async deviceLogs(device: device & devicesConfig): Promise<void> {
709
720
  if (this.platform.debugMode) {
710
721
  this.deviceLogging = this.accessory.context.logging = 'debugMode';
711
722
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
@@ -3,8 +3,8 @@ import { sleep } from '../utils';
3
3
  import { interval, Subject } from 'rxjs';
4
4
  import { skipWhile } from 'rxjs/operators';
5
5
  import { SwitchBotPlatform } from '../platform';
6
- import { Service, PlatformAccessory, CharacteristicValue } from 'homebridge';
7
- import { device, devicesConfig, serviceData, deviceStatus, ad, Devices } from '../settings';
6
+ import { Service, PlatformAccessory, CharacteristicValue, API, Logging, HAP } from 'homebridge';
7
+ import { device, devicesConfig, serviceData, deviceStatus, ad, Devices, SwitchBotPlatformConfig } from '../settings';
8
8
 
9
9
  /**
10
10
  * Platform Accessory
@@ -12,6 +12,10 @@ import { device, devicesConfig, serviceData, deviceStatus, ad, Devices } from '.
12
12
  * Each accessory may expose multiple services of different service types.
13
13
  */
14
14
  export class Motion {
15
+ public readonly api: API;
16
+ public readonly log: Logging;
17
+ public readonly config!: SwitchBotPlatformConfig;
18
+ protected readonly hap: HAP;
15
19
  // Services
16
20
  batteryService: Service;
17
21
  motionSensorService!: Service;
@@ -51,20 +55,27 @@ export class Motion {
51
55
  doMotionUpdate!: Subject<void>;
52
56
 
53
57
  // Connection
54
- private readonly BLE = this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI';
55
- private readonly OpenAPI = this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI';
58
+ private readonly OpenAPI: boolean;
59
+ private readonly BLE: boolean;
56
60
 
57
61
  constructor(
58
62
  private readonly platform: SwitchBotPlatform,
59
63
  private accessory: PlatformAccessory,
60
64
  public device: device & devicesConfig,
61
65
  ) {
66
+ this.api = this.platform.api;
67
+ this.log = this.platform.log;
68
+ this.config = this.platform.config;
69
+ this.hap = this.api.hap;
70
+ // Connection
71
+ this.BLE = this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI';
72
+ this.OpenAPI = this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI';
62
73
  // default placeholders
63
- this.logs(device);
74
+ this.deviceLogs(device);
64
75
  this.scan(device);
65
76
  this.refreshRate(device);
66
77
  this.context();
67
- this.config(device);
78
+ this.deviceConfig(device);
68
79
 
69
80
  // this is subject we use to track when we need to POST changes to the SwitchBot API
70
81
  this.doMotionUpdate = new Subject();
@@ -75,21 +86,21 @@ export class Motion {
75
86
 
76
87
  // set accessory information
77
88
  accessory
78
- .getService(this.platform.Service.AccessoryInformation)!
79
- .setCharacteristic(this.platform.Characteristic.Manufacturer, 'SwitchBot')
80
- .setCharacteristic(this.platform.Characteristic.Model, 'W1101500')
81
- .setCharacteristic(this.platform.Characteristic.SerialNumber, device.deviceId)
82
- .setCharacteristic(this.platform.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision);
89
+ .getService(this.hap.Service.AccessoryInformation)!
90
+ .setCharacteristic(this.hap.Characteristic.Manufacturer, 'SwitchBot')
91
+ .setCharacteristic(this.hap.Characteristic.Model, 'W1101500')
92
+ .setCharacteristic(this.hap.Characteristic.SerialNumber, device.deviceId)
93
+ .setCharacteristic(this.hap.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision);
83
94
 
84
95
  // get the Battery service if it exists, otherwise create a new Motion service
85
96
  // you can create multiple services for each accessory
86
97
  const motionSensorService = `${accessory.displayName} Motion Sensor`;
87
- (this.motionSensorService = accessory.getService(this.platform.Service.MotionSensor)
88
- || accessory.addService(this.platform.Service.MotionSensor)), motionSensorService;
98
+ (this.motionSensorService = accessory.getService(this.hap.Service.MotionSensor)
99
+ || accessory.addService(this.hap.Service.MotionSensor)), motionSensorService;
89
100
 
90
- this.motionSensorService.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
91
- if (!this.motionSensorService.testCharacteristic(this.platform.Characteristic.ConfiguredName)) {
92
- this.motionSensorService.addCharacteristic(this.platform.Characteristic.ConfiguredName, accessory.displayName);
101
+ this.motionSensorService.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
102
+ if (!this.motionSensorService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
103
+ this.motionSensorService.addCharacteristic(this.hap.Characteristic.ConfiguredName, accessory.displayName);
93
104
  }
94
105
  // each service must implement at-minimum the "required characteristics" for the given service type
95
106
  // see https://developers.homebridge.io/#/service/MotionSensor
@@ -97,30 +108,30 @@ export class Motion {
97
108
  // Light Sensor Service
98
109
  if (device.motion?.hide_lightsensor) {
99
110
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Light Sensor Service`);
100
- this.lightSensorService = this.accessory.getService(this.platform.Service.LightSensor);
111
+ this.lightSensorService = this.accessory.getService(this.hap.Service.LightSensor);
101
112
  accessory.removeService(this.lightSensorService!);
102
113
  } else if (!this.lightSensorService) {
103
114
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Light Sensor Service`);
104
115
  const lightSensorService = `${accessory.displayName} Light Sensor`;
105
- (this.lightSensorService = this.accessory.getService(this.platform.Service.LightSensor)
106
- || this.accessory.addService(this.platform.Service.LightSensor)), lightSensorService;
116
+ (this.lightSensorService = this.accessory.getService(this.hap.Service.LightSensor)
117
+ || this.accessory.addService(this.hap.Service.LightSensor)), lightSensorService;
107
118
 
108
- this.lightSensorService.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Light Sensor`);
109
- this.lightSensorService.setCharacteristic(this.platform.Characteristic.ConfiguredName, `${accessory.displayName} Light Sensor`);
119
+ this.lightSensorService.setCharacteristic(this.hap.Characteristic.Name, `${accessory.displayName} Light Sensor`);
120
+ this.lightSensorService.setCharacteristic(this.hap.Characteristic.ConfiguredName, `${accessory.displayName} Light Sensor`);
110
121
  } else {
111
122
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Light Sensor Service Not Added`);
112
123
  }
113
124
 
114
125
  // Battery Service
115
126
  const batteryService = `${accessory.displayName} Battery`;
116
- (this.batteryService = this.accessory.getService(this.platform.Service.Battery)
117
- || accessory.addService(this.platform.Service.Battery)), batteryService;
127
+ (this.batteryService = this.accessory.getService(this.hap.Service.Battery)
128
+ || accessory.addService(this.hap.Service.Battery)), batteryService;
118
129
 
119
- this.batteryService.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Battery`);
120
- if (!this.batteryService.testCharacteristic(this.platform.Characteristic.ConfiguredName)) {
121
- this.batteryService.addCharacteristic(this.platform.Characteristic.ConfiguredName, `${accessory.displayName} Battery`);
130
+ this.batteryService.setCharacteristic(this.hap.Characteristic.Name, `${accessory.displayName} Battery`);
131
+ if (!this.batteryService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
132
+ this.batteryService.addCharacteristic(this.hap.Characteristic.ConfiguredName, `${accessory.displayName} Battery`);
122
133
  }
123
- this.batteryService.setCharacteristic(this.platform.Characteristic.ChargingState, this.platform.Characteristic.ChargingState.NOT_CHARGEABLE);
134
+ this.batteryService.setCharacteristic(this.hap.Characteristic.ChargingState, this.hap.Characteristic.ChargingState.NOT_CHARGEABLE);
124
135
 
125
136
  // Retrieve initial values and updateHomekit
126
137
  this.updateHomeKitCharacteristics();
@@ -141,14 +152,14 @@ export class Motion {
141
152
  const { detectionState } = context;
142
153
  const { MotionDetected } = this;
143
154
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ` +
144
- '(detectionState) = ' +
145
- `Webhook:(${detectionState}), ` +
146
- `current:(${MotionDetected})`);
155
+ '(detectionState) = ' +
156
+ `Webhook:(${detectionState}), ` +
157
+ `current:(${MotionDetected})`);
147
158
  this.MotionDetected = detectionState === 'DETECTED' ? true : false;
148
159
  this.updateHomeKitCharacteristics();
149
160
  } catch (e: any) {
150
161
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
151
- + `failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
162
+ + `failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
152
163
  }
153
164
  };
154
165
  }
@@ -206,9 +217,9 @@ export class Motion {
206
217
  }
207
218
  this.BatteryLevel = this.BLE_BatteryLevel!;
208
219
  if (this.BatteryLevel < 10) {
209
- this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
220
+ this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
210
221
  } else {
211
- this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
222
+ this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
212
223
  }
213
224
  this.debugLog(
214
225
  `${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel},` + ` StatusLowBattery: ${this.StatusLowBattery}`,
@@ -238,9 +249,9 @@ export class Motion {
238
249
  // Battery
239
250
  this.BatteryLevel = Number(this.OpenAPI_BatteryLevel);
240
251
  if (this.BatteryLevel < 10) {
241
- this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
252
+ this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
242
253
  } else {
243
- this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
254
+ this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
244
255
  }
245
256
  if (Number.isNaN(this.BatteryLevel)) {
246
257
  this.BatteryLevel = 100;
@@ -394,21 +405,21 @@ export class Motion {
394
405
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
395
406
  } else {
396
407
  this.accessory.context.MotionDetected = this.MotionDetected;
397
- this.motionSensorService.updateCharacteristic(this.platform.Characteristic.MotionDetected, this.MotionDetected);
408
+ this.motionSensorService.updateCharacteristic(this.hap.Characteristic.MotionDetected, this.MotionDetected);
398
409
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic MotionDetected: ${this.MotionDetected}`);
399
410
  }
400
411
  if (this.BatteryLevel === undefined) {
401
412
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
402
413
  } else {
403
414
  this.accessory.context.BatteryLevel = this.BatteryLevel;
404
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, this.BatteryLevel);
415
+ this.batteryService?.updateCharacteristic(this.hap.Characteristic.BatteryLevel, this.BatteryLevel);
405
416
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
406
417
  }
407
418
  if (this.StatusLowBattery === undefined) {
408
419
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
409
420
  } else {
410
421
  this.accessory.context.StatusLowBattery = this.StatusLowBattery;
411
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.StatusLowBattery);
422
+ this.batteryService?.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, this.StatusLowBattery);
412
423
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
413
424
  }
414
425
  if (this.BLE) {
@@ -416,7 +427,7 @@ export class Motion {
416
427
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
417
428
  } else {
418
429
  this.accessory.context.CurrentAmbientLightLevel = this.CurrentAmbientLightLevel;
419
- this.lightSensorService?.updateCharacteristic(this.platform.Characteristic.CurrentAmbientLightLevel, this.CurrentAmbientLightLevel);
430
+ this.lightSensorService?.updateCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel, this.CurrentAmbientLightLevel);
420
431
  this.debugLog(
421
432
  `${this.device.deviceType}: ${this.accessory.displayName}` +
422
433
  ` updateCharacteristic CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`,
@@ -563,11 +574,11 @@ export class Motion {
563
574
  }
564
575
 
565
576
  async apiError(e: any): Promise<void> {
566
- this.motionSensorService.updateCharacteristic(this.platform.Characteristic.MotionDetected, e);
567
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, e);
568
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, e);
577
+ this.motionSensorService.updateCharacteristic(this.hap.Characteristic.MotionDetected, e);
578
+ this.batteryService?.updateCharacteristic(this.hap.Characteristic.BatteryLevel, e);
579
+ this.batteryService?.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, e);
569
580
  if (this.BLE) {
570
- this.lightSensorService?.updateCharacteristic(this.platform.Characteristic.CurrentAmbientLightLevel, e);
581
+ this.lightSensorService?.updateCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel, e);
571
582
  }
572
583
  }
573
584
 
@@ -589,7 +600,7 @@ export class Motion {
589
600
  }
590
601
  }
591
602
 
592
- async config(device: device & devicesConfig): Promise<void> {
603
+ async deviceConfig(device: device & devicesConfig): Promise<void> {
593
604
  let config = {};
594
605
  if (device.motion) {
595
606
  config = device.motion;
@@ -617,7 +628,7 @@ export class Motion {
617
628
  }
618
629
  }
619
630
 
620
- async logs(device: device & devicesConfig): Promise<void> {
631
+ async deviceLogs(device: device & devicesConfig): Promise<void> {
621
632
  if (this.platform.debugMode) {
622
633
  this.deviceLogging = this.accessory.context.logging = 'debugMode';
623
634
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);