@switchbot/homebridge-switchbot 3.5.1-beta.0 → 3.5.1-beta.10

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 (197) hide show
  1. package/README.md +16 -0
  2. package/dist/device/blindtilt.d.ts +15 -6
  3. package/dist/device/blindtilt.d.ts.map +1 -1
  4. package/dist/device/blindtilt.js +224 -370
  5. package/dist/device/blindtilt.js.map +1 -1
  6. package/dist/device/bot.d.ts +9 -4
  7. package/dist/device/bot.d.ts.map +1 -1
  8. package/dist/device/bot.js +197 -221
  9. package/dist/device/bot.js.map +1 -1
  10. package/dist/device/ceilinglight.d.ts +10 -5
  11. package/dist/device/ceilinglight.d.ts.map +1 -1
  12. package/dist/device/ceilinglight.js +214 -292
  13. package/dist/device/ceilinglight.js.map +1 -1
  14. package/dist/device/colorbulb.d.ts +10 -5
  15. package/dist/device/colorbulb.d.ts.map +1 -1
  16. package/dist/device/colorbulb.js +335 -421
  17. package/dist/device/colorbulb.js.map +1 -1
  18. package/dist/device/contact.d.ts +9 -4
  19. package/dist/device/contact.d.ts.map +1 -1
  20. package/dist/device/contact.js +121 -200
  21. package/dist/device/contact.js.map +1 -1
  22. package/dist/device/curtain.d.ts +20 -7
  23. package/dist/device/curtain.d.ts.map +1 -1
  24. package/dist/device/curtain.js +229 -403
  25. package/dist/device/curtain.js.map +1 -1
  26. package/dist/device/device.d.ts +41 -22
  27. package/dist/device/device.d.ts.map +1 -1
  28. package/dist/device/device.js +212 -181
  29. package/dist/device/device.js.map +1 -1
  30. package/dist/device/fan.d.ts +9 -4
  31. package/dist/device/fan.d.ts.map +1 -1
  32. package/dist/device/fan.js +204 -249
  33. package/dist/device/fan.js.map +1 -1
  34. package/dist/device/hub.d.ts +9 -5
  35. package/dist/device/hub.d.ts.map +1 -1
  36. package/dist/device/hub.js +107 -233
  37. package/dist/device/hub.js.map +1 -1
  38. package/dist/device/humidifier.d.ts +12 -4
  39. package/dist/device/humidifier.d.ts.map +1 -1
  40. package/dist/device/humidifier.js +193 -235
  41. package/dist/device/humidifier.js.map +1 -1
  42. package/dist/device/iosensor.d.ts +9 -4
  43. package/dist/device/iosensor.d.ts.map +1 -1
  44. package/dist/device/iosensor.js +92 -159
  45. package/dist/device/iosensor.js.map +1 -1
  46. package/dist/device/lightstrip.d.ts +10 -5
  47. package/dist/device/lightstrip.d.ts.map +1 -1
  48. package/dist/device/lightstrip.js +288 -370
  49. package/dist/device/lightstrip.js.map +1 -1
  50. package/dist/device/lock.d.ts +10 -5
  51. package/dist/device/lock.d.ts.map +1 -1
  52. package/dist/device/lock.js +172 -219
  53. package/dist/device/lock.js.map +1 -1
  54. package/dist/device/meter.d.ts +9 -4
  55. package/dist/device/meter.d.ts.map +1 -1
  56. package/dist/device/meter.js +94 -160
  57. package/dist/device/meter.js.map +1 -1
  58. package/dist/device/meterplus.d.ts +9 -4
  59. package/dist/device/meterplus.d.ts.map +1 -1
  60. package/dist/device/meterplus.js +95 -157
  61. package/dist/device/meterplus.js.map +1 -1
  62. package/dist/device/motion.d.ts +9 -4
  63. package/dist/device/motion.d.ts.map +1 -1
  64. package/dist/device/motion.js +85 -144
  65. package/dist/device/motion.js.map +1 -1
  66. package/dist/device/plug.d.ts +9 -4
  67. package/dist/device/plug.d.ts.map +1 -1
  68. package/dist/device/plug.js +100 -127
  69. package/dist/device/plug.js.map +1 -1
  70. package/dist/device/robotvacuumcleaner.d.ts +9 -6
  71. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  72. package/dist/device/robotvacuumcleaner.js +172 -259
  73. package/dist/device/robotvacuumcleaner.js.map +1 -1
  74. package/dist/device/waterdetector.d.ts +12 -4
  75. package/dist/device/waterdetector.d.ts.map +1 -1
  76. package/dist/device/waterdetector.js +106 -131
  77. package/dist/device/waterdetector.js.map +1 -1
  78. package/dist/irdevice/airconditioner.d.ts +2 -1
  79. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  80. package/dist/irdevice/airconditioner.js +2 -3
  81. package/dist/irdevice/airconditioner.js.map +1 -1
  82. package/dist/irdevice/airpurifier.d.ts +2 -1
  83. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  84. package/dist/irdevice/airpurifier.js +2 -3
  85. package/dist/irdevice/airpurifier.js.map +1 -1
  86. package/dist/irdevice/camera.d.ts +2 -1
  87. package/dist/irdevice/camera.d.ts.map +1 -1
  88. package/dist/irdevice/camera.js +2 -3
  89. package/dist/irdevice/camera.js.map +1 -1
  90. package/dist/irdevice/fan.d.ts +2 -1
  91. package/dist/irdevice/fan.d.ts.map +1 -1
  92. package/dist/irdevice/fan.js +2 -3
  93. package/dist/irdevice/fan.js.map +1 -1
  94. package/dist/irdevice/irdevice.d.ts +3 -1
  95. package/dist/irdevice/irdevice.d.ts.map +1 -1
  96. package/dist/irdevice/irdevice.js +5 -1
  97. package/dist/irdevice/irdevice.js.map +1 -1
  98. package/dist/irdevice/light.d.ts +2 -1
  99. package/dist/irdevice/light.d.ts.map +1 -1
  100. package/dist/irdevice/light.js +2 -3
  101. package/dist/irdevice/light.js.map +1 -1
  102. package/dist/irdevice/other.d.ts +2 -1
  103. package/dist/irdevice/other.d.ts.map +1 -1
  104. package/dist/irdevice/other.js +2 -3
  105. package/dist/irdevice/other.js.map +1 -1
  106. package/dist/irdevice/tv.d.ts +2 -1
  107. package/dist/irdevice/tv.d.ts.map +1 -1
  108. package/dist/irdevice/tv.js +2 -3
  109. package/dist/irdevice/tv.js.map +1 -1
  110. package/dist/irdevice/vacuumcleaner.d.ts +2 -1
  111. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  112. package/dist/irdevice/vacuumcleaner.js +2 -3
  113. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  114. package/dist/irdevice/waterheater.d.ts +2 -1
  115. package/dist/irdevice/waterheater.d.ts.map +1 -1
  116. package/dist/irdevice/waterheater.js +2 -3
  117. package/dist/irdevice/waterheater.js.map +1 -1
  118. package/dist/platform.d.ts +14 -14
  119. package/dist/platform.d.ts.map +1 -1
  120. package/dist/platform.js +102 -86
  121. package/dist/platform.js.map +1 -1
  122. package/dist/settings.d.ts +49 -190
  123. package/dist/settings.d.ts.map +1 -1
  124. package/dist/settings.js +19 -0
  125. package/dist/settings.js.map +1 -1
  126. package/dist/types/bledevicestatus.d.ts +279 -0
  127. package/dist/types/bledevicestatus.d.ts.map +1 -0
  128. package/dist/types/bledevicestatus.js +2 -0
  129. package/dist/types/bledevicestatus.js.map +1 -0
  130. package/dist/types/devicelist.d.ts +89 -0
  131. package/dist/types/devicelist.d.ts.map +1 -0
  132. package/dist/types/devicelist.js +6 -0
  133. package/dist/types/devicelist.js.map +1 -0
  134. package/dist/types/deviceresponse.d.ts +13 -0
  135. package/dist/types/deviceresponse.d.ts.map +1 -0
  136. package/dist/types/deviceresponse.js +2 -0
  137. package/dist/types/deviceresponse.js.map +1 -0
  138. package/dist/types/devicestatus.d.ts +149 -0
  139. package/dist/types/devicestatus.d.ts.map +1 -0
  140. package/dist/types/devicestatus.js +3 -0
  141. package/dist/types/devicestatus.js.map +1 -0
  142. package/dist/types/devicewebhookstatus.d.ts +156 -0
  143. package/dist/types/devicewebhookstatus.d.ts.map +1 -0
  144. package/dist/types/devicewebhookstatus.js +2 -0
  145. package/dist/types/devicewebhookstatus.js.map +1 -0
  146. package/dist/types/irdevicelist.d.ts +10 -0
  147. package/dist/types/irdevicelist.d.ts.map +1 -0
  148. package/dist/types/irdevicelist.js +2 -0
  149. package/dist/types/irdevicelist.js.map +1 -0
  150. package/dist/types/pushbody.d.ts +6 -0
  151. package/dist/types/pushbody.d.ts.map +1 -0
  152. package/dist/types/pushbody.js +2 -0
  153. package/dist/types/pushbody.js.map +1 -0
  154. package/dist/utils.d.ts +25 -0
  155. package/dist/utils.d.ts.map +1 -1
  156. package/dist/utils.js +28 -0
  157. package/dist/utils.js.map +1 -1
  158. package/package.json +11 -11
  159. package/src/device/blindtilt.ts +254 -376
  160. package/src/device/bot.ts +209 -224
  161. package/src/device/ceilinglight.ts +227 -297
  162. package/src/device/colorbulb.ts +345 -435
  163. package/src/device/contact.ts +139 -200
  164. package/src/device/curtain.ts +254 -414
  165. package/src/device/device.ts +221 -190
  166. package/src/device/fan.ts +222 -253
  167. package/src/device/hub.ts +127 -248
  168. package/src/device/humidifier.ts +217 -246
  169. package/src/device/iosensor.ts +115 -168
  170. package/src/device/lightstrip.ts +303 -380
  171. package/src/device/lock.ts +188 -224
  172. package/src/device/meter.ts +117 -179
  173. package/src/device/meterplus.ts +118 -176
  174. package/src/device/motion.ts +100 -146
  175. package/src/device/plug.ts +111 -131
  176. package/src/device/robotvacuumcleaner.ts +191 -258
  177. package/src/device/waterdetector.ts +124 -141
  178. package/src/irdevice/airconditioner.ts +4 -4
  179. package/src/irdevice/airpurifier.ts +4 -4
  180. package/src/irdevice/camera.ts +4 -4
  181. package/src/irdevice/fan.ts +4 -4
  182. package/src/irdevice/irdevice.ts +8 -2
  183. package/src/irdevice/light.ts +4 -4
  184. package/src/irdevice/other.ts +4 -4
  185. package/src/irdevice/tv.ts +4 -4
  186. package/src/irdevice/vacuumcleaner.ts +4 -4
  187. package/src/irdevice/waterheater.ts +4 -4
  188. package/src/platform.ts +104 -87
  189. package/src/settings.ts +30 -238
  190. package/src/types/bledevicestatus.ts +305 -0
  191. package/src/types/devicelist.ts +124 -0
  192. package/src/types/deviceresponse.ts +15 -0
  193. package/src/types/devicestatus.ts +178 -0
  194. package/src/types/devicewebhookstatus.ts +196 -0
  195. package/src/types/irdevicelist.ts +15 -0
  196. package/src/types/pushbody.ts +9 -0
  197. package/src/utils.ts +33 -0
@@ -6,10 +6,15 @@ import { Units } from 'homebridge';
6
6
  import { deviceBase } from './device.js';
7
7
  import { convertUnits } from '../utils.js';
8
8
  import { Subject, interval, skipWhile } from 'rxjs';
9
+ import { SwitchBotBLEModel, SwitchBotBLEModelName } from 'node-switchbot';
9
10
 
11
+ import type { devicesConfig } from '../settings.js';
12
+ import type { device } from '../types/devicelist.js';
10
13
  import type { SwitchBotPlatform } from '../platform.js';
14
+ import type { outdoorMeterServiceData } from '../types/bledevicestatus.js';
15
+ import type { outdoorMeterStatus } from '../types/devicestatus.js';
16
+ import type { outdoorMeterWebhookContext } from '../types/devicewebhookstatus.js';
11
17
  import type { CharacteristicValue, PlatformAccessory, Service } from 'homebridge';
12
- import type { device, devicesConfig, serviceData, deviceStatus } from '../settings.js';
13
18
 
14
19
  /**
15
20
  * Platform Accessory
@@ -143,76 +148,72 @@ export class IOSensor extends deviceBase {
143
148
  // Retrieve initial values and updateHomekit
144
149
  this.updateHomeKitCharacteristics();
145
150
 
151
+ //regisiter webhook event handler
152
+ this.registerWebhook();
153
+
146
154
  // Start an update interval
147
155
  interval(this.deviceRefreshRate * 1000)
148
156
  .pipe(skipWhile(() => this.ioSensorUpdateInProgress))
149
157
  .subscribe(async () => {
150
158
  await this.refreshStatus();
151
159
  });
152
-
153
- //regisiter webhook event handler
154
- this.registerWebhook(accessory, device);
155
160
  }
156
161
 
157
- async BLEparseStatus(serviceData: serviceData): Promise<void> {
158
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
162
+ async BLEparseStatus(serviceData: outdoorMeterServiceData): Promise<void> {
163
+ await this.debugLog('BLEparseStatus');
159
164
 
160
- // Battery
165
+ // BatteryLevel
161
166
  this.Battery.BatteryLevel = Number(serviceData.battery);
162
- if (this.Battery.BatteryLevel < 10) {
163
- this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
164
- } else {
165
- this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
166
- }
167
- this.debugLog(`${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel}, StatusLowBattery: ${this.Battery.StatusLowBattery}`);
167
+ await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`);
168
+
169
+ // StatusLowBattery
170
+ this.Battery.StatusLowBattery = this.Battery.BatteryLevel < 10
171
+ ? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW : this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
172
+ await this.debugLog(`StatusLowBattery: ${this.Battery.StatusLowBattery}`);
168
173
 
169
- // Humidity
170
- if (!this.device.iosensor?.hide_humidity) {
171
- this.HumiditySensor!.CurrentRelativeHumidity = serviceData.humidity!;
172
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Humidity: ${this.HumiditySensor!.CurrentRelativeHumidity}%`);
174
+ // CurrentRelativeHumidity
175
+ if (!this.device.iosensor?.hide_humidity && this.HumiditySensor?.Service) {
176
+ this.HumiditySensor.CurrentRelativeHumidity = serviceData.humidity;
177
+ await this.debugLog(`CurrentRelativeHumidity: ${this.HumiditySensor.CurrentRelativeHumidity}%`);
173
178
  }
174
179
 
175
180
  // Current Temperature
176
- if (!this.device.meter?.hide_temperature) {
177
- const celcius = serviceData.temperature!.c < 0 ? 0 : serviceData.temperature!.c > 100 ? 100 : serviceData.temperature!.c;
178
- this.TemperatureSensor!.CurrentTemperature = celcius;
179
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Temperature: ${this.TemperatureSensor!.CurrentTemperature}°c`);
181
+ if (!this.device.meter?.hide_temperature && this.TemperatureSensor?.Service) {
182
+ const CELSIUS = serviceData.temperature.c < 0 ? 0 : serviceData.temperature.c > 100 ? 100 : serviceData.temperature.c;
183
+ this.TemperatureSensor.CurrentTemperature = CELSIUS;
184
+ await this.debugLog(`Temperature: ${this.TemperatureSensor.CurrentTemperature}°c`);
180
185
  }
181
186
  }
182
187
 
183
- async openAPIparseStatus(deviceStatus: deviceStatus): Promise<void> {
184
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
188
+ async openAPIparseStatus(deviceStatus: outdoorMeterStatus): Promise<void> {
189
+ await this.debugLog('openAPIparseStatus');
185
190
 
186
191
  // BatteryLevel
187
- this.Battery.BatteryLevel = Number(deviceStatus.body.battery);
188
- if (this.Battery.BatteryLevel < 10) {
189
- this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
190
- } else {
191
- this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
192
- }
193
- if (Number.isNaN(this.Battery.BatteryLevel)) {
194
- this.Battery.BatteryLevel = 100;
195
- }
196
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel},`
197
- + ` StatusLowBattery: ${this.Battery.StatusLowBattery}`);
192
+ this.Battery.BatteryLevel = Number(deviceStatus.battery);
193
+ await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`);
198
194
 
199
- // Current Relative Humidity
200
- if (!this.device.iosensor?.hide_humidity) {
201
- this.HumiditySensor!.CurrentRelativeHumidity = deviceStatus.body.humidity!;
202
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Humidity: ${this.HumiditySensor!.CurrentRelativeHumidity}%`);
195
+ // StatusLowBattery
196
+ this.Battery.StatusLowBattery = this.Battery.BatteryLevel < 10
197
+ ? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW : this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
198
+ await this.debugLog(`StatusLowBattery: ${this.Battery.StatusLowBattery}`);
199
+
200
+ // CurrentRelativeHumidity
201
+ if (!this.device.iosensor?.hide_humidity && this.HumiditySensor?.Service) {
202
+ this.HumiditySensor.CurrentRelativeHumidity = deviceStatus.humidity;
203
+ await this.debugLog(`CurrentRelativeHumidity: ${this.HumiditySensor.CurrentRelativeHumidity}%`);
203
204
  }
204
205
 
205
206
  // Current Temperature
206
- if (!this.device.iosensor?.hide_temperature) {
207
- this.TemperatureSensor!.CurrentTemperature = deviceStatus.body.temperature!;
208
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Temperature: ${this.TemperatureSensor!.CurrentTemperature}°c`);
207
+ if (!this.device.meter?.hide_temperature && this.TemperatureSensor?.Service) {
208
+ this.TemperatureSensor.CurrentTemperature = deviceStatus.temperature;
209
+ await this.debugLog(`CurrentTemperature: ${this.TemperatureSensor.CurrentTemperature}°c`);
209
210
  }
210
211
 
211
212
  // Firmware Version
212
- const version = deviceStatus.body.version?.toString();
213
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Firmware Version: ${version?.replace(/^V|-.*$/g, '')}`);
214
- if (deviceStatus.body.version) {
215
- const deviceVersion = version?.replace(/^V|-.*$/g, '') ?? '0.0.0';
213
+ const version = deviceStatus.version.toString();
214
+ await this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
215
+ if (deviceStatus.version) {
216
+ const deviceVersion = version.replace(/^V|-.*$/g, '') ?? '0.0.0';
216
217
  this.accessory
217
218
  .getService(this.hap.Service.AccessoryInformation)!
218
219
  .setCharacteristic(this.hap.Characteristic.HardwareRevision, deviceVersion)
@@ -220,7 +221,29 @@ export class IOSensor extends deviceBase {
220
221
  .getCharacteristic(this.hap.Characteristic.FirmwareRevision)
221
222
  .updateValue(deviceVersion);
222
223
  this.accessory.context.deviceVersion = deviceVersion;
223
- this.debugSuccessLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceVersion: ${this.accessory.context.deviceVersion}`);
224
+ await this.debugSuccessLog(`deviceVersion: ${this.accessory.context.deviceVersion}`);
225
+ }
226
+ }
227
+
228
+ async parseStatusWebhook(context: outdoorMeterWebhookContext): Promise<void> {
229
+ await this.debugLog('parseStatusWebhook');
230
+ await this.debugLog(`(scale, temperature, humidity) = Webhook:(${context.scale}, ${convertUnits(context.temperature, context.scale,
231
+ this.device.iosensor?.convertUnitTo)}, ${context.humidity}), current:(${this.TemperatureSensor?.CurrentTemperature},`
232
+ + ` ${this.HumiditySensor?.CurrentRelativeHumidity})`);
233
+
234
+ if (context.scale !== 'CELSIUS' && this.device.iosensor?.convertUnitTo === undefined) {
235
+ await this.warnLog(`received a non-CELSIUS Webhook scale: ${context.scale}, Use the *convertUnitsTo* config under Hub settings,`
236
+ + ' if displaying incorrectly in HomeKit.');
237
+ }
238
+ // CurrentRelativeHumidity
239
+ if (this.device.iosensor?.hide_humidity && this.HumiditySensor?.Service) {
240
+ this.HumiditySensor.CurrentRelativeHumidity = context.humidity;
241
+ await this.debugLog(`CurrentRelativeHumidity: ${this.HumiditySensor.CurrentRelativeHumidity}%`);
242
+ }
243
+ // CurrentTemperature
244
+ if (this.device.iosensor?.hide_temperature && this.TemperatureSensor?.Service) {
245
+ this.TemperatureSensor.CurrentTemperature = convertUnits(context.temperature, context.scale, this.device.iosensor?.convertUnitTo);
246
+ await this.debugLog(`CurrentTemperature: ${this.TemperatureSensor.CurrentTemperature}°c`);
224
247
  }
225
248
  }
226
249
 
@@ -229,20 +252,19 @@ export class IOSensor extends deviceBase {
229
252
  */
230
253
  async refreshStatus(): Promise<void> {
231
254
  if (!this.device.enableCloudService && this.OpenAPI) {
232
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} refreshStatus enableCloudService: ${this.device.enableCloudService}`);
255
+ await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
233
256
  } else if (this.BLE) {
234
257
  await this.BLERefreshStatus();
235
258
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
236
259
  await this.openAPIRefreshStatus();
237
260
  } else {
238
261
  await this.offlineOff();
239
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
240
- + ` ${this.device.connectionType}, refreshStatus will not happen.`);
262
+ await this.debugWarnLog(`Connection Type: ${this.device.connectionType}, refreshStatus will not happen.`);
241
263
  }
242
264
  }
243
265
 
244
266
  async BLERefreshStatus(): Promise<void> {
245
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLERefreshStatus`);
267
+ await this.debugLog('BLERefreshStatus');
246
268
  const switchbot = await this.switchbotBLE();
247
269
 
248
270
  if (switchbot === undefined) {
@@ -251,13 +273,13 @@ export class IOSensor extends deviceBase {
251
273
  // Start to monitor advertisement packets
252
274
  (async () => {
253
275
  // Start to monitor advertisement packets
254
- const serviceData: serviceData = await this.monitorAdvertisementPackets(switchbot);
276
+ const serviceData = await this.monitorAdvertisementPackets(switchbot) as unknown as outdoorMeterServiceData;
255
277
  // Update HomeKit
256
- if (serviceData.model !== '' && serviceData.modelName !== '') {
278
+ if (serviceData.model === SwitchBotBLEModel.OutdoorMeter && serviceData.modelName === SwitchBotBLEModelName.OutdoorMeter) {
257
279
  await this.BLEparseStatus(serviceData);
258
280
  await this.updateHomeKitCharacteristics();
259
281
  } else {
260
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed to get serviceData, serviceData: ${serviceData}`);
282
+ await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
261
283
  await this.BLERefreshConnection(switchbot);
262
284
  }
263
285
  })();
@@ -265,54 +287,39 @@ export class IOSensor extends deviceBase {
265
287
  }
266
288
 
267
289
  async openAPIRefreshStatus(): Promise<void> {
268
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
290
+ await this.debugLog('openAPIRefreshStatus');
269
291
  try {
270
292
  const { body, statusCode } = await this.deviceRefreshStatus();
271
293
  const deviceStatus: any = await body.json();
272
- await this.refreshStatusCodes(statusCode, deviceStatus);;
273
- if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
274
- await this.successfulRefreshStatus(statusCode, deviceStatus);
275
- await this.openAPIparseStatus(deviceStatus);
294
+ await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);;
295
+ if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
296
+ await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
297
+ await this.openAPIparseStatus(deviceStatus.body);
276
298
  await this.updateHomeKitCharacteristics();
277
299
  } else {
278
- await this.statusCodes(statusCode, deviceStatus);
300
+ await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
301
+ await this.debugWarnLog(statusCode, deviceStatus);
279
302
  }
280
303
  } catch (e: any) {
281
- this.apiError(e);
282
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}`
283
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
304
+ await this.apiError(e);
305
+ await this.errorLog(`failed openAPIRefreshStatus with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
284
306
  }
285
307
  }
286
308
 
287
- async registerWebhook(accessory: PlatformAccessory, device: device & devicesConfig) {
288
- if (device.webhook) {
289
- this.debugLog(`${device.deviceType}: ${accessory.displayName} is listening webhook.`);
290
- this.platform.webhookEventHandler[device.deviceId] = async (context) => {
309
+ async registerWebhook() {
310
+ if (this.device.webhook) {
311
+ await this.debugLog('is listening webhook.');
312
+ this.platform.webhookEventHandler[this.device.deviceId] = async (context: outdoorMeterWebhookContext) => {
291
313
  try {
292
- this.debugLog(`${device.deviceType}: ${accessory.displayName} received Webhook: ${JSON.stringify(context)}`);
293
- const { temperature, humidity } = context;
294
- const { CurrentTemperature } = this.TemperatureSensor || { CurrentTemperature: undefined };
295
- const { CurrentRelativeHumidity } = this.HumiditySensor || { CurrentRelativeHumidity: undefined };
296
- if (context.scale !== 'CELCIUS' && device.iosensor?.convertUnitTo === undefined) {
297
- this.warnLog(`${device.deviceType}: ${accessory.displayName} received a non-CELCIUS Webhook scale: `
298
- + `${context.scale}, Use the *convertUnitsTo* config under Indoor/Outdoor Sensor settings, if displaying incorrectly in HomeKit.`);
299
- }
300
- this.debugLog(`${device.deviceType}: ${accessory.displayName} (scale, temperature, humidity) = Webhook:(${context.scale},`
301
- + ` ${convertUnits(temperature, context.scale, device.iosensor?.convertUnitTo)}, ${humidity}), current:(${CurrentTemperature}, `
302
- + `${CurrentRelativeHumidity})`);
303
- if (device.iosensor?.hide_humidity) {
304
- this.HumiditySensor!.CurrentRelativeHumidity = humidity;
305
- }
306
- if (device.iosensor?.hide_temperature) {
307
- this.TemperatureSensor!.CurrentTemperature = convertUnits(temperature, context.scale, device.iosensor?.convertUnitTo);
308
- }
309
- this.updateHomeKitCharacteristics();
314
+ await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
315
+ await this.parseStatusWebhook(context);
316
+ await this.updateHomeKitCharacteristics();
310
317
  } catch (e: any) {
311
- this.errorLog(`${device.deviceType}: ${accessory.displayName} failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
318
+ await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
312
319
  }
313
320
  };
314
321
  } else {
315
- this.debugLog(`${device.deviceType}: ${accessory.displayName} is not listening webhook.`);
322
+ await this.debugLog('is not listening webhook.');
316
323
  }
317
324
  }
318
325
 
@@ -320,109 +327,49 @@ export class IOSensor extends deviceBase {
320
327
  * Updates the status for each of the HomeKit Characteristics
321
328
  */
322
329
  async updateHomeKitCharacteristics(): Promise<void> {
323
- const mqttmessage: string[] = [];
324
- const entry = { time: Math.round(new Date().valueOf() / 1000) };
325
-
326
330
  // CurrentRelativeHumidity
327
- if (!this.device.iosensor?.hide_humidity) {
328
- if (this.HumiditySensor!.CurrentRelativeHumidity === undefined) {
329
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
330
- + ` CurrentRelativeHumidity: ${this.HumiditySensor!.CurrentRelativeHumidity}`);
331
- } else {
332
- this.accessory.context.CurrentRelativeHumidity = this.HumiditySensor!.CurrentRelativeHumidity;
333
- this.HumiditySensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity,
334
- this.HumiditySensor!.CurrentRelativeHumidity);
335
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
336
- + ` CurrentRelativeHumidity: ${this.HumiditySensor!.CurrentRelativeHumidity}`);
337
- if (this.device.mqttURL) {
338
- mqttmessage.push(`"humidity": ${this.HumiditySensor!.CurrentRelativeHumidity}`);
339
- }
340
- if (this.device.history) {
341
- entry['humidity'] = this.HumiditySensor!.CurrentRelativeHumidity;
342
- }
343
- }
331
+ if (!this.device.iosensor?.hide_humidity && this.HumiditySensor?.Service) {
332
+ await this.updateCharacteristic(this.HumiditySensor.Service, this.hap.Characteristic.CurrentRelativeHumidity,
333
+ this.HumiditySensor.CurrentRelativeHumidity, 'CurrentRelativeHumidity');
344
334
  }
345
-
346
335
  // CurrentTemperature
347
- if (!this.device.iosensor?.hide_temperature) {
348
- if (this.TemperatureSensor!.CurrentTemperature === undefined) {
349
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentTemperature: ${this.TemperatureSensor!.CurrentTemperature}`);
350
- } else {
351
- this.accessory.context.CurrentTemperature = this.TemperatureSensor!.CurrentTemperature;
352
- this.TemperatureSensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, this.TemperatureSensor!.CurrentTemperature);
353
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
354
- + ` CurrentTemperature: ${this.TemperatureSensor!.CurrentTemperature}`);
355
- if (this.device.mqttURL) {
356
- mqttmessage.push(`"temperature": ${this.TemperatureSensor!.CurrentTemperature}`);
357
- }
358
- if (this.device.history) {
359
- entry['temp'] = this.TemperatureSensor!.CurrentTemperature;
360
- }
361
- }
362
- }
363
- if (this.Battery.BatteryLevel === undefined) {
364
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel}`);
365
- } else {
366
- this.accessory.context.BatteryLevel = this.Battery.BatteryLevel;
367
- this.Battery.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, this.Battery.BatteryLevel);
368
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.Battery.BatteryLevel}`);
369
- if (this.device.mqttURL) {
370
- mqttmessage.push(`"battery": ${this.Battery.BatteryLevel}`);
371
- }
372
- }
373
- if (this.Battery.StatusLowBattery === undefined) {
374
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.Battery.StatusLowBattery}`);
375
- } else {
376
- this.accessory.context.StatusLowBattery = this.Battery.StatusLowBattery;
377
- this.Battery.Service.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, this.Battery.StatusLowBattery);
378
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
379
- + ` StatusLowBattery: ${this.Battery.StatusLowBattery}`);
380
- if (this.device.mqttURL) {
381
- mqttmessage.push(`"lowBattery": ${this.Battery.StatusLowBattery}`);
382
- }
383
- }
384
-
385
- // MQTT Publish
386
- if (this.device.mqttURL) {
387
- this.mqttPublish(`{${mqttmessage.join(',')}}`);
388
- }
389
-
390
- // History Service
391
- if (!this.device.iosensor?.hide_humidity && (Number(this.HumiditySensor!.CurrentRelativeHumidity) > 0)) {
392
- // reject unreliable data
393
- if (this.device.history) {
394
- this.historyService?.addEntry(entry);
395
- }
336
+ if (!this.device.iosensor?.hide_temperature && this.TemperatureSensor?.Service) {
337
+ await this.updateCharacteristic(this.TemperatureSensor.Service, this.hap.Characteristic.CurrentTemperature,
338
+ this.TemperatureSensor.CurrentTemperature, 'CurrentTemperature');
396
339
  }
340
+ // BatteryLevel
341
+ await this.updateCharacteristic(this.Battery.Service, this.hap.Characteristic.BatteryLevel,
342
+ this.Battery.BatteryLevel, 'BatteryLevel');
343
+ // StatusLowBattery
344
+ await this.updateCharacteristic(this.Battery.Service, this.hap.Characteristic.StatusLowBattery,
345
+ this.Battery.StatusLowBattery, 'StatusLowBattery');
397
346
  }
398
347
 
399
348
  async BLERefreshConnection(switchbot: any): Promise<void> {
400
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot:`
401
- + ` ${JSON.stringify(switchbot)}`);
349
+ await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${JSON.stringify(switchbot)}`);
402
350
  if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
403
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection to Refresh Status`);
351
+ await this.warnLog('Using OpenAPI Connection to Refresh Status');
404
352
  await this.openAPIRefreshStatus();
405
353
  }
406
354
  }
407
355
 
408
356
  async offlineOff(): Promise<void> {
409
357
  if (this.device.offline) {
410
- if (!this.device.iosensor?.hide_humidity) {
411
- this.HumiditySensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity, 50);
358
+ if (!this.device.iosensor?.hide_humidity && this.HumiditySensor?.Service) {
359
+ this.HumiditySensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity, 50);
412
360
  }
413
- if (!this.device.iosensor?.hide_temperature) {
414
- this.TemperatureSensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, 30);
361
+ if (!this.device.iosensor?.hide_temperature && this.TemperatureSensor?.Service) {
362
+ this.TemperatureSensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, 30);
415
363
  }
416
- this.Battery.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, 100);
417
364
  }
418
365
  }
419
366
 
420
367
  async apiError(e: any): Promise<void> {
421
- if (!this.device.iosensor?.hide_humidity) {
422
- this.HumiditySensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity, e);
368
+ if (!this.device.iosensor?.hide_humidity && this.HumiditySensor?.Service) {
369
+ this.HumiditySensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity, e);
423
370
  }
424
- if (!this.device.iosensor?.hide_temperature) {
425
- this.TemperatureSensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, e);
371
+ if (!this.device.iosensor?.hide_temperature && this.TemperatureSensor?.Service) {
372
+ this.TemperatureSensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, e);
426
373
  }
427
374
  this.Battery.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, e);
428
375
  this.Battery.Service.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, e);