@switchbot/homebridge-switchbot 2.14.1-beta.0 → 3.0.0-beta.1

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 (147) hide show
  1. package/.github/{npm-version-script.js → npm-version-script.cjs} +1 -1
  2. package/.github/workflows/dependabot.yml +10 -11
  3. package/CHANGELOG.md +11 -2
  4. package/LICENSE +1 -1
  5. package/dist/device/blindtilt.d.ts +10 -6
  6. package/dist/device/blindtilt.d.ts.map +1 -1
  7. package/dist/device/blindtilt.js +152 -91
  8. package/dist/device/blindtilt.js.map +1 -1
  9. package/dist/device/bot.d.ts +10 -6
  10. package/dist/device/bot.d.ts.map +1 -1
  11. package/dist/device/bot.js +234 -184
  12. package/dist/device/bot.js.map +1 -1
  13. package/dist/device/ceilinglight.d.ts +10 -6
  14. package/dist/device/ceilinglight.d.ts.map +1 -1
  15. package/dist/device/ceilinglight.js +110 -64
  16. package/dist/device/ceilinglight.js.map +1 -1
  17. package/dist/device/colorbulb.d.ts +10 -6
  18. package/dist/device/colorbulb.d.ts.map +1 -1
  19. package/dist/device/colorbulb.js +121 -69
  20. package/dist/device/colorbulb.js.map +1 -1
  21. package/dist/device/contact.d.ts +10 -6
  22. package/dist/device/contact.d.ts.map +1 -1
  23. package/dist/device/contact.js +111 -66
  24. package/dist/device/contact.js.map +1 -1
  25. package/dist/device/curtain.d.ts +10 -6
  26. package/dist/device/curtain.d.ts.map +1 -1
  27. package/dist/device/curtain.js +165 -107
  28. package/dist/device/curtain.js.map +1 -1
  29. package/dist/device/hub.d.ts +10 -6
  30. package/dist/device/hub.d.ts.map +1 -1
  31. package/dist/device/hub.js +88 -51
  32. package/dist/device/hub.js.map +1 -1
  33. package/dist/device/humidifier.d.ts +10 -6
  34. package/dist/device/humidifier.d.ts.map +1 -1
  35. package/dist/device/humidifier.js +133 -90
  36. package/dist/device/humidifier.js.map +1 -1
  37. package/dist/device/iosensor.d.ts +10 -6
  38. package/dist/device/iosensor.d.ts.map +1 -1
  39. package/dist/device/iosensor.js +109 -66
  40. package/dist/device/iosensor.js.map +1 -1
  41. package/dist/device/lightstrip.d.ts +10 -6
  42. package/dist/device/lightstrip.d.ts.map +1 -1
  43. package/dist/device/lightstrip.js +110 -65
  44. package/dist/device/lightstrip.js.map +1 -1
  45. package/dist/device/lock.d.ts +11 -7
  46. package/dist/device/lock.d.ts.map +1 -1
  47. package/dist/device/lock.js +125 -84
  48. package/dist/device/lock.js.map +1 -1
  49. package/dist/device/meter.d.ts +10 -6
  50. package/dist/device/meter.d.ts.map +1 -1
  51. package/dist/device/meter.js +103 -63
  52. package/dist/device/meter.js.map +1 -1
  53. package/dist/device/meterplus.d.ts +10 -6
  54. package/dist/device/meterplus.d.ts.map +1 -1
  55. package/dist/device/meterplus.js +103 -63
  56. package/dist/device/meterplus.js.map +1 -1
  57. package/dist/device/motion.d.ts +10 -6
  58. package/dist/device/motion.d.ts.map +1 -1
  59. package/dist/device/motion.js +95 -54
  60. package/dist/device/motion.js.map +1 -1
  61. package/dist/device/plug.d.ts +10 -6
  62. package/dist/device/plug.d.ts.map +1 -1
  63. package/dist/device/plug.js +68 -40
  64. package/dist/device/plug.js.map +1 -1
  65. package/dist/device/robotvacuumcleaner.d.ts +10 -6
  66. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  67. package/dist/device/robotvacuumcleaner.js +86 -52
  68. package/dist/device/robotvacuumcleaner.js.map +1 -1
  69. package/dist/homebridge-ui/server.js +5 -10
  70. package/dist/homebridge-ui/server.js.map +1 -1
  71. package/dist/index.d.ts +1 -4
  72. package/dist/index.d.ts.map +1 -1
  73. package/dist/index.js +9 -5
  74. package/dist/index.js.map +1 -1
  75. package/dist/irdevice/airconditioner.d.ts +9 -5
  76. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  77. package/dist/irdevice/airconditioner.js +105 -71
  78. package/dist/irdevice/airconditioner.js.map +1 -1
  79. package/dist/irdevice/airpurifier.d.ts +9 -5
  80. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  81. package/dist/irdevice/airpurifier.js +78 -46
  82. package/dist/irdevice/airpurifier.js.map +1 -1
  83. package/dist/irdevice/camera.d.ts +9 -5
  84. package/dist/irdevice/camera.d.ts.map +1 -1
  85. package/dist/irdevice/camera.js +41 -25
  86. package/dist/irdevice/camera.js.map +1 -1
  87. package/dist/irdevice/fan.d.ts +9 -5
  88. package/dist/irdevice/fan.d.ts.map +1 -1
  89. package/dist/irdevice/fan.js +62 -39
  90. package/dist/irdevice/fan.js.map +1 -1
  91. package/dist/irdevice/light.d.ts +9 -5
  92. package/dist/irdevice/light.d.ts.map +1 -1
  93. package/dist/irdevice/light.js +67 -45
  94. package/dist/irdevice/light.js.map +1 -1
  95. package/dist/irdevice/other.d.ts +9 -5
  96. package/dist/irdevice/other.d.ts.map +1 -1
  97. package/dist/irdevice/other.js +176 -150
  98. package/dist/irdevice/other.js.map +1 -1
  99. package/dist/irdevice/tv.d.ts +9 -5
  100. package/dist/irdevice/tv.d.ts.map +1 -1
  101. package/dist/irdevice/tv.js +81 -62
  102. package/dist/irdevice/tv.js.map +1 -1
  103. package/dist/irdevice/vacuumcleaner.d.ts +9 -5
  104. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  105. package/dist/irdevice/vacuumcleaner.js +41 -25
  106. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  107. package/dist/irdevice/waterheater.d.ts +9 -5
  108. package/dist/irdevice/waterheater.d.ts.map +1 -1
  109. package/dist/irdevice/waterheater.js +48 -32
  110. package/dist/irdevice/waterheater.js.map +1 -1
  111. package/dist/platform.d.ts +8 -9
  112. package/dist/platform.d.ts.map +1 -1
  113. package/dist/platform.js +151 -162
  114. package/dist/platform.js.map +1 -1
  115. package/dist/settings.js +7 -14
  116. package/dist/settings.js.map +1 -1
  117. package/dist/utils.js +1 -5
  118. package/dist/utils.js.map +1 -1
  119. package/package.json +20 -9
  120. package/src/device/blindtilt.ts +84 -72
  121. package/src/device/bot.ts +191 -180
  122. package/src/device/ceilinglight.ts +57 -44
  123. package/src/device/colorbulb.ts +53 -40
  124. package/src/device/contact.ts +71 -60
  125. package/src/device/curtain.ts +98 -86
  126. package/src/device/hub.ts +57 -45
  127. package/src/device/humidifier.ts +88 -77
  128. package/src/device/iosensor.ts +67 -55
  129. package/src/device/lightstrip.ts +56 -43
  130. package/src/device/lock.ts +91 -81
  131. package/src/device/meter.ts +63 -51
  132. package/src/device/meterplus.ts +67 -55
  133. package/src/device/motion.ts +58 -47
  134. package/src/device/plug.ts +38 -27
  135. package/src/device/robotvacuumcleaner.ts +49 -38
  136. package/src/index.ts +10 -7
  137. package/src/irdevice/airconditioner.ts +71 -63
  138. package/src/irdevice/airpurifier.ts +48 -40
  139. package/src/irdevice/camera.ts +28 -20
  140. package/src/irdevice/fan.ts +42 -34
  141. package/src/irdevice/light.ts +48 -40
  142. package/src/irdevice/other.ts +164 -156
  143. package/src/irdevice/tv.ts +66 -58
  144. package/src/irdevice/vacuumcleaner.ts +28 -20
  145. package/src/irdevice/waterheater.ts +35 -27
  146. package/src/platform.ts +65 -47
  147. package/tsconfig.json +11 -13
@@ -1,14 +1,18 @@
1
- import { connectAsync } from 'async-mqtt';
2
- import { CharacteristicValue, PlatformAccessory, Service, Units } from 'homebridge';
1
+ import asyncmqtt from 'async-mqtt';
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
- import { SwitchBotPlatform } from '../platform';
8
- import { Devices, ad, device, deviceStatus, devicesConfig, serviceData, temperature } from '../settings';
9
- import { sleep } from '../utils';
7
+ import { SwitchBotPlatform } from '../platform.js';
8
+ import { Devices, ad, device, deviceStatus, devicesConfig, serviceData, temperature, SwitchBotPlatformConfig } from '../settings.js';
9
+ import { sleep } from '../utils.js';
10
10
 
11
11
  export class Meter {
12
+ public readonly api: API;
13
+ public readonly log: Logging;
14
+ public readonly config!: SwitchBotPlatformConfig;
15
+ protected readonly hap: HAP;
12
16
  // Services
13
17
  batteryService: Service;
14
18
  humidityService?: Service;
@@ -49,22 +53,29 @@ export class Meter {
49
53
  deviceRefreshRate!: number;
50
54
 
51
55
  // Connection
52
- private readonly BLE = this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI';
53
- private readonly OpenAPI = this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI';
56
+ private readonly OpenAPI: boolean;
57
+ private readonly BLE: boolean;
54
58
 
55
59
  constructor(
56
60
  private readonly platform: SwitchBotPlatform,
57
61
  private accessory: PlatformAccessory,
58
62
  public device: device & devicesConfig,
59
63
  ) {
64
+ this.api = this.platform.api;
65
+ this.log = this.platform.log;
66
+ this.config = this.platform.config;
67
+ this.hap = this.api.hap;
68
+ // Connection
69
+ this.BLE = this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI';
70
+ this.OpenAPI = this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI';
60
71
  // default placeholders
61
- this.logs(device);
72
+ this.deviceLogs(device);
62
73
  this.scan(device);
63
74
  this.refreshRate(device);
64
75
  this.context();
65
76
  this.setupHistoryService(device);
66
77
  this.setupMqtt(device);
67
- this.config(device);
78
+ this.deviceConfig(device);
68
79
 
69
80
  this.CurrentRelativeHumidity = accessory.context.CurrentRelativeHumidity;
70
81
  this.CurrentTemperature = accessory.context.CurrentTemperature;
@@ -74,29 +85,29 @@ export class Meter {
74
85
 
75
86
  // set accessory information
76
87
  accessory
77
- .getService(this.platform.Service.AccessoryInformation)!
78
- .setCharacteristic(this.platform.Characteristic.Manufacturer, 'SwitchBot')
79
- .setCharacteristic(this.platform.Characteristic.Model, 'METERTH-S1')
80
- .setCharacteristic(this.platform.Characteristic.SerialNumber, device.deviceId)
81
- .setCharacteristic(this.platform.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision);
88
+ .getService(this.hap.Service.AccessoryInformation)!
89
+ .setCharacteristic(this.hap.Characteristic.Manufacturer, 'SwitchBot')
90
+ .setCharacteristic(this.hap.Characteristic.Model, 'METERTH-S1')
91
+ .setCharacteristic(this.hap.Characteristic.SerialNumber, device.deviceId)
92
+ .setCharacteristic(this.hap.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision);
82
93
 
83
94
  // Temperature Sensor Service
84
95
  if (device.meter?.hide_temperature) {
85
96
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Temperature Sensor Service`);
86
- this.temperatureService = this.accessory.getService(this.platform.Service.TemperatureSensor);
97
+ this.temperatureService = this.accessory.getService(this.hap.Service.TemperatureSensor);
87
98
  accessory.removeService(this.temperatureService!);
88
99
  } else if (!this.temperatureService) {
89
100
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Temperature Sensor Service`);
90
101
  const temperatureService = `${accessory.displayName} Temperature Sensor`;
91
- (this.temperatureService = this.accessory.getService(this.platform.Service.TemperatureSensor)
92
- || this.accessory.addService(this.platform.Service.TemperatureSensor)), temperatureService;
102
+ (this.temperatureService = this.accessory.getService(this.hap.Service.TemperatureSensor)
103
+ || this.accessory.addService(this.hap.Service.TemperatureSensor)), temperatureService;
93
104
 
94
- this.temperatureService.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Temperature Sensor`);
95
- if (!this.temperatureService.testCharacteristic(this.platform.Characteristic.ConfiguredName)) {
96
- this.temperatureService.addCharacteristic(this.platform.Characteristic.ConfiguredName, `${accessory.displayName} Temperature Sensor`);
105
+ this.temperatureService.setCharacteristic(this.hap.Characteristic.Name, `${accessory.displayName} Temperature Sensor`);
106
+ if (!this.temperatureService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
107
+ this.temperatureService.addCharacteristic(this.hap.Characteristic.ConfiguredName, `${accessory.displayName} Temperature Sensor`);
97
108
  }
98
109
  this.temperatureService
99
- .getCharacteristic(this.platform.Characteristic.CurrentTemperature)
110
+ .getCharacteristic(this.hap.Characteristic.CurrentTemperature)
100
111
  .setProps({
101
112
  unit: Units['CELSIUS'],
102
113
  validValueRanges: [-273.15, 100],
@@ -114,20 +125,20 @@ export class Meter {
114
125
  // Humidity Sensor Service
115
126
  if (device.meter?.hide_humidity) {
116
127
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Humidity Sensor Service`);
117
- this.humidityService = this.accessory.getService(this.platform.Service.HumiditySensor);
128
+ this.humidityService = this.accessory.getService(this.hap.Service.HumiditySensor);
118
129
  accessory.removeService(this.humidityService!);
119
130
  } else if (!this.humidityService) {
120
131
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Humidity Sensor Service`);
121
132
  const humidityService = `${accessory.displayName} Humidity Sensor`;
122
- (this.humidityService = this.accessory.getService(this.platform.Service.HumiditySensor)
123
- || this.accessory.addService(this.platform.Service.HumiditySensor)), humidityService;
133
+ (this.humidityService = this.accessory.getService(this.hap.Service.HumiditySensor)
134
+ || this.accessory.addService(this.hap.Service.HumiditySensor)), humidityService;
124
135
 
125
- this.humidityService.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Humidity Sensor`);
126
- if (!this.humidityService.testCharacteristic(this.platform.Characteristic.ConfiguredName)) {
127
- this.humidityService.addCharacteristic(this.platform.Characteristic.ConfiguredName, `${accessory.displayName} Humidity Sensor`);
136
+ this.humidityService.setCharacteristic(this.hap.Characteristic.Name, `${accessory.displayName} Humidity Sensor`);
137
+ if (!this.humidityService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
138
+ this.humidityService.addCharacteristic(this.hap.Characteristic.ConfiguredName, `${accessory.displayName} Humidity Sensor`);
128
139
  }
129
140
  this.humidityService
130
- .getCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity)
141
+ .getCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity)
131
142
  .setProps({
132
143
  minStep: 0.1,
133
144
  })
@@ -140,14 +151,14 @@ export class Meter {
140
151
 
141
152
  // Battery Service
142
153
  const batteryService = `${accessory.displayName} Battery`;
143
- (this.batteryService = this.accessory.getService(this.platform.Service.Battery)
144
- || accessory.addService(this.platform.Service.Battery)), batteryService;
154
+ (this.batteryService = this.accessory.getService(this.hap.Service.Battery)
155
+ || accessory.addService(this.hap.Service.Battery)), batteryService;
145
156
 
146
- this.batteryService.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Battery`);
147
- if (!this.batteryService.testCharacteristic(this.platform.Characteristic.ConfiguredName)) {
148
- this.batteryService.addCharacteristic(this.platform.Characteristic.ConfiguredName, `${accessory.displayName} Battery`);
157
+ this.batteryService.setCharacteristic(this.hap.Characteristic.Name, `${accessory.displayName} Battery`);
158
+ if (!this.batteryService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
159
+ this.batteryService.addCharacteristic(this.hap.Characteristic.ConfiguredName, `${accessory.displayName} Battery`);
149
160
  }
150
- this.batteryService.setCharacteristic(this.platform.Characteristic.ChargingState, this.platform.Characteristic.ChargingState.NOT_CHARGEABLE);
161
+ this.batteryService.setCharacteristic(this.hap.Characteristic.ChargingState, this.hap.Characteristic.ChargingState.NOT_CHARGEABLE);
151
162
 
152
163
  // Retrieve initial values and update Homekit
153
164
  this.updateHomeKitCharacteristics();
@@ -207,9 +218,9 @@ export class Meter {
207
218
  // BatteryLevel
208
219
  this.BatteryLevel = Number(this.BLE_BatteryLevel);
209
220
  if (this.BatteryLevel < 15) {
210
- this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
221
+ this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
211
222
  } else {
212
- this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
223
+ this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
213
224
  }
214
225
  this.debugLog(`${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}, StatusLowBattery: ${this.StatusLowBattery}`);
215
226
 
@@ -233,9 +244,9 @@ export class Meter {
233
244
  // BatteryLevel
234
245
  this.BatteryLevel = Number(this.OpenAPI_BatteryLevel);
235
246
  if (this.BatteryLevel < 15) {
236
- this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
247
+ this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
237
248
  } else {
238
- this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
249
+ this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
239
250
  }
240
251
  this.debugLog(`${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}, StatusLowBattery: ${this.StatusLowBattery}`);
241
252
 
@@ -254,9 +265,9 @@ export class Meter {
254
265
  // Battery
255
266
  this.BatteryLevel = Number(this.OpenAPI_BatteryLevel);
256
267
  if (this.BatteryLevel < 10) {
257
- this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
268
+ this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
258
269
  } else {
259
- this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
270
+ this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
260
271
  }
261
272
  if (Number.isNaN(this.BatteryLevel)) {
262
273
  this.BatteryLevel = 100;
@@ -410,7 +421,7 @@ export class Meter {
410
421
  entry['humidity'] = this.CurrentRelativeHumidity;
411
422
  }
412
423
  this.accessory.context.CurrentRelativeHumidity = this.CurrentRelativeHumidity;
413
- this.humidityService?.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, this.CurrentRelativeHumidity);
424
+ this.humidityService?.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity, this.CurrentRelativeHumidity);
414
425
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} `
415
426
  + `updateCharacteristic CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
416
427
  }
@@ -427,7 +438,7 @@ export class Meter {
427
438
  entry['temp'] = this.CurrentTemperature;
428
439
  }
429
440
  this.accessory.context.CurrentTemperature = this.CurrentTemperature;
430
- this.temperatureService?.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, this.CurrentTemperature);
441
+ this.temperatureService?.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, this.CurrentTemperature);
431
442
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic CurrentTemperature: ${this.CurrentTemperature}`);
432
443
  }
433
444
  }
@@ -439,7 +450,7 @@ export class Meter {
439
450
  mqttmessage.push(`"battery": ${this.BatteryLevel}`);
440
451
  }
441
452
  this.accessory.context.BatteryLevel = this.BatteryLevel;
442
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, this.BatteryLevel);
453
+ this.batteryService?.updateCharacteristic(this.hap.Characteristic.BatteryLevel, this.BatteryLevel);
443
454
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
444
455
  }
445
456
  if (this.StatusLowBattery === undefined) {
@@ -449,7 +460,7 @@ export class Meter {
449
460
  mqttmessage.push(`"lowBattery": ${this.StatusLowBattery}`);
450
461
  }
451
462
  this.accessory.context.StatusLowBattery = this.StatusLowBattery;
452
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.StatusLowBattery);
463
+ this.batteryService?.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, this.StatusLowBattery);
453
464
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
454
465
  }
455
466
 
@@ -485,6 +496,7 @@ export class Meter {
485
496
  async setupMqtt(device: device & devicesConfig): Promise<void> {
486
497
  if (device.mqttURL) {
487
498
  try {
499
+ const { connectAsync } = asyncmqtt;
488
500
  this.mqttClient = await connectAsync(device.mqttURL, device.mqttOptions || {});
489
501
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MQTT connection has been established successfully.`);
490
502
  this.mqttClient.on('error', (e: Error) => {
@@ -615,13 +627,13 @@ export class Meter {
615
627
 
616
628
  async apiError(e: any): Promise<void> {
617
629
  if (!this.device.meter?.hide_humidity) {
618
- this.humidityService?.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, e);
630
+ this.humidityService?.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity, e);
619
631
  }
620
632
  if (!this.device.meter?.hide_temperature) {
621
- this.temperatureService?.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, e);
633
+ this.temperatureService?.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, e);
622
634
  }
623
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, e);
624
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, e);
635
+ this.batteryService?.updateCharacteristic(this.hap.Characteristic.BatteryLevel, e);
636
+ this.batteryService?.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, e);
625
637
  }
626
638
 
627
639
  async context() {
@@ -641,7 +653,7 @@ export class Meter {
641
653
  this.BatteryLevel = this.accessory.context.BatteryLevel;
642
654
  }
643
655
  if (this.StatusLowBattery === undefined) {
644
- this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
656
+ this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
645
657
  this.accessory.context.StatusLowBattery = this.StatusLowBattery;
646
658
  } else {
647
659
  this.StatusLowBattery = this.accessory.context.StatusLowBattery;
@@ -662,7 +674,7 @@ export class Meter {
662
674
  }
663
675
  }
664
676
 
665
- async config(device: device & devicesConfig): Promise<void> {
677
+ async deviceConfig(device: device & devicesConfig): Promise<void> {
666
678
  let config = {};
667
679
  if (device.meter) {
668
680
  config = device.meter;
@@ -690,7 +702,7 @@ export class Meter {
690
702
  }
691
703
  }
692
704
 
693
- async logs(device: device & devicesConfig): Promise<void> {
705
+ async deviceLogs(device: device & devicesConfig): Promise<void> {
694
706
  if (this.platform.debugMode) {
695
707
  this.deviceLogging = this.accessory.context.logging = 'debugMode';
696
708
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
@@ -1,12 +1,12 @@
1
- import { connectAsync } from 'async-mqtt';
2
- import { CharacteristicValue, PlatformAccessory, Service, Units } from 'homebridge';
1
+ import asyncmqtt from 'async-mqtt';
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
- import { SwitchBotPlatform } from '../platform';
8
- import { Devices, ad, device, deviceStatus, devicesConfig, serviceData, temperature } from '../settings';
9
- import { sleep } from '../utils';
7
+ import { SwitchBotPlatform } from '../platform.js';
8
+ import { Devices, ad, device, deviceStatus, devicesConfig, serviceData, temperature, SwitchBotPlatformConfig } from '../settings.js';
9
+ import { sleep } from '../utils.js';
10
10
 
11
11
  /**
12
12
  * Platform Accessory
@@ -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
 
@@ -490,6 +501,7 @@ export class MeterPlus {
490
501
  async setupMqtt(device: device & devicesConfig): Promise<void> {
491
502
  if (device.mqttURL) {
492
503
  try {
504
+ const { connectAsync } = asyncmqtt;
493
505
  this.mqttClient = await connectAsync(device.mqttURL, device.mqttOptions || {});
494
506
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MQTT connection has been established successfully.`);
495
507
  this.mqttClient.on('error', (e: Error) => {
@@ -630,13 +642,13 @@ export class MeterPlus {
630
642
 
631
643
  async apiError(e: any): Promise<void> {
632
644
  if (!this.device.meter?.hide_humidity) {
633
- this.humidityService?.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, e);
645
+ this.humidityService?.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity, e);
634
646
  }
635
647
  if (!this.device.meter?.hide_temperature) {
636
- this.temperatureService?.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, e);
648
+ this.temperatureService?.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, e);
637
649
  }
638
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, e);
639
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, e);
650
+ this.batteryService?.updateCharacteristic(this.hap.Characteristic.BatteryLevel, e);
651
+ this.batteryService?.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, e);
640
652
  }
641
653
 
642
654
  async context() {
@@ -656,7 +668,7 @@ export class MeterPlus {
656
668
  this.BatteryLevel = this.accessory.context.BatteryLevel;
657
669
  }
658
670
  if (this.StatusLowBattery === undefined) {
659
- this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
671
+ this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
660
672
  this.accessory.context.StatusLowBattery = this.StatusLowBattery;
661
673
  } else {
662
674
  this.StatusLowBattery = this.accessory.context.StatusLowBattery;
@@ -677,7 +689,7 @@ export class MeterPlus {
677
689
  }
678
690
  }
679
691
 
680
- async config(device: device & devicesConfig): Promise<void> {
692
+ async deviceConfig(device: device & devicesConfig): Promise<void> {
681
693
  let config = {};
682
694
  if (device.meter) {
683
695
  config = device.meter;
@@ -705,7 +717,7 @@ export class MeterPlus {
705
717
  }
706
718
  }
707
719
 
708
- async logs(device: device & devicesConfig): Promise<void> {
720
+ async deviceLogs(device: device & devicesConfig): Promise<void> {
709
721
  if (this.platform.debugMode) {
710
722
  this.deviceLogging = this.accessory.context.logging = 'debugMode';
711
723
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);