@switchbot/homebridge-switchbot 3.8.3 → 4.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 (205) hide show
  1. package/.github/npm-version-script.cjs +81 -0
  2. package/.github/workflows/beta-release.yml +12 -4
  3. package/CHANGELOG.md +15 -1
  4. package/config.schema.json +583 -937
  5. package/dist/device/blindtilt.d.ts +1 -4
  6. package/dist/device/blindtilt.d.ts.map +1 -1
  7. package/dist/device/blindtilt.js +68 -66
  8. package/dist/device/blindtilt.js.map +1 -1
  9. package/dist/device/bot.d.ts +1 -4
  10. package/dist/device/bot.d.ts.map +1 -1
  11. package/dist/device/bot.js +63 -59
  12. package/dist/device/bot.js.map +1 -1
  13. package/dist/device/ceilinglight.d.ts +1 -4
  14. package/dist/device/ceilinglight.d.ts.map +1 -1
  15. package/dist/device/ceilinglight.js +56 -65
  16. package/dist/device/ceilinglight.js.map +1 -1
  17. package/dist/device/colorbulb.d.ts +1 -4
  18. package/dist/device/colorbulb.d.ts.map +1 -1
  19. package/dist/device/colorbulb.js +73 -80
  20. package/dist/device/colorbulb.js.map +1 -1
  21. package/dist/device/contact.d.ts +1 -4
  22. package/dist/device/contact.d.ts.map +1 -1
  23. package/dist/device/contact.js +38 -38
  24. package/dist/device/contact.js.map +1 -1
  25. package/dist/device/curtain.d.ts +1 -4
  26. package/dist/device/curtain.d.ts.map +1 -1
  27. package/dist/device/curtain.js +60 -60
  28. package/dist/device/curtain.js.map +1 -1
  29. package/dist/device/device.d.ts +6 -6
  30. package/dist/device/device.d.ts.map +1 -1
  31. package/dist/device/device.js +86 -20
  32. package/dist/device/device.js.map +1 -1
  33. package/dist/device/fan.d.ts +1 -4
  34. package/dist/device/fan.d.ts.map +1 -1
  35. package/dist/device/fan.js +50 -51
  36. package/dist/device/fan.js.map +1 -1
  37. package/dist/device/hub.d.ts +1 -4
  38. package/dist/device/hub.d.ts.map +1 -1
  39. package/dist/device/hub.js +48 -48
  40. package/dist/device/hub.js.map +1 -1
  41. package/dist/device/humidifier.d.ts +1 -4
  42. package/dist/device/humidifier.d.ts.map +1 -1
  43. package/dist/device/humidifier.js +53 -55
  44. package/dist/device/humidifier.js.map +1 -1
  45. package/dist/device/iosensor.d.ts +1 -4
  46. package/dist/device/iosensor.d.ts.map +1 -1
  47. package/dist/device/iosensor.js +35 -35
  48. package/dist/device/iosensor.js.map +1 -1
  49. package/dist/device/lightstrip.d.ts +1 -4
  50. package/dist/device/lightstrip.d.ts.map +1 -1
  51. package/dist/device/lightstrip.js +57 -65
  52. package/dist/device/lightstrip.js.map +1 -1
  53. package/dist/device/lock.d.ts +1 -4
  54. package/dist/device/lock.d.ts.map +1 -1
  55. package/dist/device/lock.js +38 -39
  56. package/dist/device/lock.js.map +1 -1
  57. package/dist/device/meter.d.ts +1 -4
  58. package/dist/device/meter.d.ts.map +1 -1
  59. package/dist/device/meter.js +35 -35
  60. package/dist/device/meter.js.map +1 -1
  61. package/dist/device/meterplus.d.ts +1 -4
  62. package/dist/device/meterplus.d.ts.map +1 -1
  63. package/dist/device/meterplus.js +35 -35
  64. package/dist/device/meterplus.js.map +1 -1
  65. package/dist/device/motion.d.ts +1 -4
  66. package/dist/device/motion.d.ts.map +1 -1
  67. package/dist/device/motion.js +27 -27
  68. package/dist/device/motion.js.map +1 -1
  69. package/dist/device/plug.d.ts +1 -4
  70. package/dist/device/plug.d.ts.map +1 -1
  71. package/dist/device/plug.js +32 -32
  72. package/dist/device/plug.js.map +1 -1
  73. package/dist/device/robotvacuumcleaner.d.ts +1 -4
  74. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  75. package/dist/device/robotvacuumcleaner.js +39 -40
  76. package/dist/device/robotvacuumcleaner.js.map +1 -1
  77. package/dist/device/waterdetector.d.ts +1 -4
  78. package/dist/device/waterdetector.d.ts.map +1 -1
  79. package/dist/device/waterdetector.js +28 -28
  80. package/dist/device/waterdetector.js.map +1 -1
  81. package/dist/irdevice/airconditioner.d.ts +1 -1
  82. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  83. package/dist/irdevice/airconditioner.js +31 -32
  84. package/dist/irdevice/airconditioner.js.map +1 -1
  85. package/dist/irdevice/airpurifier.d.ts +1 -1
  86. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  87. package/dist/irdevice/airpurifier.js +11 -12
  88. package/dist/irdevice/airpurifier.js.map +1 -1
  89. package/dist/irdevice/camera.d.ts +1 -1
  90. package/dist/irdevice/camera.d.ts.map +1 -1
  91. package/dist/irdevice/camera.js +9 -10
  92. package/dist/irdevice/camera.js.map +1 -1
  93. package/dist/irdevice/fan.d.ts +1 -1
  94. package/dist/irdevice/fan.d.ts.map +1 -1
  95. package/dist/irdevice/fan.js +22 -23
  96. package/dist/irdevice/fan.js.map +1 -1
  97. package/dist/irdevice/irdevice.d.ts +6 -6
  98. package/dist/irdevice/irdevice.d.ts.map +1 -1
  99. package/dist/irdevice/irdevice.js +28 -14
  100. package/dist/irdevice/irdevice.js.map +1 -1
  101. package/dist/irdevice/light.d.ts +1 -1
  102. package/dist/irdevice/light.d.ts.map +1 -1
  103. package/dist/irdevice/light.js +12 -13
  104. package/dist/irdevice/light.js.map +1 -1
  105. package/dist/irdevice/other.d.ts +1 -1
  106. package/dist/irdevice/other.d.ts.map +1 -1
  107. package/dist/irdevice/other.js +12 -15
  108. package/dist/irdevice/other.js.map +1 -1
  109. package/dist/irdevice/tv.d.ts +1 -1
  110. package/dist/irdevice/tv.d.ts.map +1 -1
  111. package/dist/irdevice/tv.js +27 -28
  112. package/dist/irdevice/tv.js.map +1 -1
  113. package/dist/irdevice/vacuumcleaner.d.ts +1 -1
  114. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  115. package/dist/irdevice/vacuumcleaner.js +9 -10
  116. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  117. package/dist/irdevice/waterheater.d.ts +1 -1
  118. package/dist/irdevice/waterheater.d.ts.map +1 -1
  119. package/dist/irdevice/waterheater.js +9 -10
  120. package/dist/irdevice/waterheater.js.map +1 -1
  121. package/dist/platform.d.ts +9 -22
  122. package/dist/platform.d.ts.map +1 -1
  123. package/dist/platform.js +154 -260
  124. package/dist/platform.js.map +1 -1
  125. package/dist/settings.d.ts +64 -82
  126. package/dist/settings.d.ts.map +1 -1
  127. package/dist/settings.js +0 -20
  128. package/dist/settings.js.map +1 -1
  129. package/dist/utils.d.ts +1 -102
  130. package/dist/utils.d.ts.map +1 -1
  131. package/dist/utils.js +0 -105
  132. package/dist/utils.js.map +1 -1
  133. package/docs/assets/highlight.css +42 -0
  134. package/docs/assets/main.js +1 -1
  135. package/docs/functions/default.html +1 -1
  136. package/docs/index.html +61 -50
  137. package/docs/media/security-privacy-bluetooth.png +0 -0
  138. package/package.json +5 -7
  139. package/src/device/blindtilt.ts +74 -74
  140. package/src/device/bot.ts +68 -67
  141. package/src/device/ceilinglight.ts +59 -70
  142. package/src/device/colorbulb.ts +79 -88
  143. package/src/device/contact.ts +40 -43
  144. package/src/device/curtain.ts +66 -68
  145. package/src/device/device.ts +86 -40
  146. package/src/device/fan.ts +52 -56
  147. package/src/device/hub.ts +50 -53
  148. package/src/device/humidifier.ts +56 -61
  149. package/src/device/iosensor.ts +37 -40
  150. package/src/device/lightstrip.ts +62 -72
  151. package/src/device/lock.ts +41 -45
  152. package/src/device/meter.ts +37 -40
  153. package/src/device/meterplus.ts +37 -40
  154. package/src/device/motion.ts +29 -32
  155. package/src/device/plug.ts +34 -37
  156. package/src/device/robotvacuumcleaner.ts +41 -49
  157. package/src/device/waterdetector.ts +30 -33
  158. package/src/irdevice/airconditioner.ts +33 -35
  159. package/src/irdevice/airpurifier.ts +12 -13
  160. package/src/irdevice/camera.ts +10 -11
  161. package/src/irdevice/fan.ts +24 -25
  162. package/src/irdevice/irdevice.ts +31 -27
  163. package/src/irdevice/light.ts +14 -15
  164. package/src/irdevice/other.ts +14 -17
  165. package/src/irdevice/tv.ts +28 -29
  166. package/src/irdevice/vacuumcleaner.ts +10 -11
  167. package/src/irdevice/waterheater.ts +10 -11
  168. package/src/platform.ts +178 -287
  169. package/src/settings.ts +67 -88
  170. package/src/utils.ts +2 -106
  171. package/dist/types/bledevicestatus.d.ts +0 -281
  172. package/dist/types/bledevicestatus.d.ts.map +0 -1
  173. package/dist/types/bledevicestatus.js +0 -2
  174. package/dist/types/bledevicestatus.js.map +0 -1
  175. package/dist/types/devicelist.d.ts +0 -89
  176. package/dist/types/devicelist.d.ts.map +0 -1
  177. package/dist/types/devicelist.js +0 -6
  178. package/dist/types/devicelist.js.map +0 -1
  179. package/dist/types/deviceresponse.d.ts +0 -13
  180. package/dist/types/deviceresponse.d.ts.map +0 -1
  181. package/dist/types/deviceresponse.js +0 -2
  182. package/dist/types/deviceresponse.js.map +0 -1
  183. package/dist/types/devicestatus.d.ts +0 -149
  184. package/dist/types/devicestatus.d.ts.map +0 -1
  185. package/dist/types/devicestatus.js +0 -3
  186. package/dist/types/devicestatus.js.map +0 -1
  187. package/dist/types/devicewebhookstatus.d.ts +0 -156
  188. package/dist/types/devicewebhookstatus.d.ts.map +0 -1
  189. package/dist/types/devicewebhookstatus.js +0 -2
  190. package/dist/types/devicewebhookstatus.js.map +0 -1
  191. package/dist/types/irdevicelist.d.ts +0 -10
  192. package/dist/types/irdevicelist.d.ts.map +0 -1
  193. package/dist/types/irdevicelist.js +0 -2
  194. package/dist/types/irdevicelist.js.map +0 -1
  195. package/dist/types/pushbody.d.ts +0 -6
  196. package/dist/types/pushbody.d.ts.map +0 -1
  197. package/dist/types/pushbody.js +0 -2
  198. package/dist/types/pushbody.js.map +0 -1
  199. package/src/types/bledevicestatus.ts +0 -303
  200. package/src/types/devicelist.ts +0 -124
  201. package/src/types/deviceresponse.ts +0 -14
  202. package/src/types/devicestatus.ts +0 -178
  203. package/src/types/devicewebhookstatus.ts +0 -191
  204. package/src/types/irdevicelist.ts +0 -15
  205. package/src/types/pushbody.ts +0 -9
@@ -3,13 +3,10 @@
3
3
  * meter.ts: @switchbot/homebridge-switchbot.
4
4
  */
5
5
  import type { CharacteristicValue, PlatformAccessory, Service } from 'homebridge'
6
+ import type { device, meterServiceData, meterStatus, meterWebhookContext } from 'node-switchbot'
6
7
 
7
8
  import type { SwitchBotPlatform } from '../platform.js'
8
- import type { devicesConfig } from '../settings.js'
9
- import type { meterServiceData } from '../types/bledevicestatus.js'
10
- import type { device } from '../types/devicelist.js'
11
- import type { meterStatus } from '../types/devicestatus.js'
12
- import type { meterWebhookContext } from '../types/devicewebhookstatus.js'
9
+ import type { devicesConfig, meterConfig } from '../settings.js'
13
10
 
14
11
  import { Units } from 'homebridge'
15
12
  /*
@@ -89,7 +86,7 @@ export class Meter extends deviceBase {
89
86
  })
90
87
 
91
88
  // Initialize Temperature Sensor Service
92
- if (device.meter?.hide_temperature) {
89
+ if ((device as meterConfig).hide_temperature) {
93
90
  if (this.TemperatureSensor) {
94
91
  this.debugLog('Removing Temperature Sensor Service')
95
92
  this.TemperatureSensor.Service = this.accessory.getService(this.hap.Service.TemperatureSensor) as Service
@@ -116,7 +113,7 @@ export class Meter extends deviceBase {
116
113
  })
117
114
  }
118
115
  // Initialize Humidity Sensor Service
119
- if (device.meter?.hide_humidity) {
116
+ if ((device as meterConfig).hide_humidity) {
120
117
  if (this.HumiditySensor) {
121
118
  this.debugLog('Removing Humidity Sensor Service')
122
119
  this.HumiditySensor.Service = this.accessory.getService(this.hap.Service.HumiditySensor) as Service
@@ -144,7 +141,7 @@ export class Meter extends deviceBase {
144
141
  this.debugLog('Retrieve initial values and update Homekit')
145
142
  this.refreshStatus()
146
143
  } catch (e: any) {
147
- this.errorLog(`failed to retrieve initial values and update Homekit, Error: ${e}`)
144
+ this.errorLog(`failed to retrieve initial values and update Homekit, Error: ${e.message ?? e}`)
148
145
  }
149
146
 
150
147
  // regisiter webhook event handler if enabled
@@ -152,7 +149,7 @@ export class Meter extends deviceBase {
152
149
  this.debugLog('Registering Webhook Event Handler')
153
150
  this.registerWebhook()
154
151
  } catch (e: any) {
155
- this.errorLog(`failed to registerWebhook, Error: ${e}`)
152
+ this.errorLog(`failed to registerWebhook, Error: ${e.message ?? e}`)
156
153
  }
157
154
 
158
155
  // regisiter platform BLE event handler if enabled
@@ -160,7 +157,7 @@ export class Meter extends deviceBase {
160
157
  this.debugLog('Registering Platform BLE Event Handler')
161
158
  this.registerPlatformBLE()
162
159
  } catch (e: any) {
163
- this.errorLog(`failed to registerPlatformBLE, Error: ${e}`)
160
+ this.errorLog(`failed to registerPlatformBLE, Error: ${e.message ?? e}`)
164
161
  }
165
162
 
166
163
  // Start an update interval
@@ -176,13 +173,13 @@ export class Meter extends deviceBase {
176
173
  await this.debugLog(`(scale, temperature, humidity) = BLE:(${this.serviceData.fahrenheit}, ${this.serviceData.celsius}, ${this.serviceData.humidity}), current:(${this.TemperatureSensor?.CurrentTemperature}, ${this.HumiditySensor?.CurrentRelativeHumidity})`)
177
174
 
178
175
  // CurrentRelativeHumidity
179
- if (!this.device.iosensor?.hide_humidity && this.HumiditySensor?.Service) {
176
+ if (!(this.device as meterConfig).hide_humidity && this.HumiditySensor?.Service) {
180
177
  this.HumiditySensor.CurrentRelativeHumidity = validHumidity(this.serviceData.humidity, 0, 100)
181
178
  await this.debugLog(`CurrentRelativeHumidity: ${this.HumiditySensor.CurrentRelativeHumidity}%`)
182
179
  }
183
180
 
184
181
  // Current Temperature
185
- if (!this.device.meter?.hide_temperature && this.TemperatureSensor?.Service) {
182
+ if (!(this.device as meterConfig).hide_temperature && this.TemperatureSensor?.Service) {
186
183
  const CELSIUS = this.serviceData.celsius < 0 ? 0 : this.serviceData.celsius > 100 ? 100 : this.serviceData.celsius
187
184
  this.TemperatureSensor.CurrentTemperature = CELSIUS
188
185
  await this.debugLog(`CurrentTemperature: ${this.TemperatureSensor.CurrentTemperature}°c`)
@@ -204,13 +201,13 @@ export class Meter extends deviceBase {
204
201
  await this.debugLog(`(battery, temperature, humidity) = OpenAPI:(${this.deviceStatus.battery}, ${this.deviceStatus.temperature}, ${this.deviceStatus.humidity}), current:(${this.Battery?.BatteryLevel}, ${this.TemperatureSensor?.CurrentTemperature}, ${this.HumiditySensor?.CurrentRelativeHumidity})`)
205
202
 
206
203
  // CurrentRelativeHumidity
207
- if (!this.device.meter?.hide_humidity && this.HumiditySensor?.Service) {
204
+ if (!(this.device as meterConfig).hide_humidity && this.HumiditySensor?.Service) {
208
205
  this.HumiditySensor.CurrentRelativeHumidity = this.deviceStatus.humidity
209
206
  await this.debugLog(`CurrentRelativeHumidity: ${this.HumiditySensor.CurrentRelativeHumidity}%`)
210
207
  }
211
208
 
212
209
  // CurrentTemperature
213
- if (!this.device.meter?.hide_temperature && this.TemperatureSensor?.Service) {
210
+ if (!(this.device as meterConfig).hide_temperature && this.TemperatureSensor?.Service) {
214
211
  this.TemperatureSensor.CurrentTemperature = this.deviceStatus.temperature
215
212
  await this.debugLog(`CurrentTemperature: ${this.TemperatureSensor.CurrentTemperature}°c`)
216
213
  }
@@ -243,21 +240,21 @@ export class Meter extends deviceBase {
243
240
 
244
241
  async parseStatusWebhook(): Promise<void> {
245
242
  await this.debugLog('parseStatusWebhook')
246
- await this.debugLog(`(scale, temperature, humidity) = Webhook:(${this.webhookContext.scale}, ${convertUnits(this.webhookContext.temperature, this.webhookContext.scale, this.device.meter?.convertUnitTo)}, ${this.webhookContext.humidity}), current:(${this.TemperatureSensor?.CurrentTemperature}, ${this.HumiditySensor?.CurrentRelativeHumidity})`)
243
+ await this.debugLog(`(scale, temperature, humidity) = Webhook:(${this.webhookContext.scale}, ${convertUnits(this.webhookContext.temperature, this.webhookContext.scale, (this.device as meterConfig).convertUnitTo)}, ${this.webhookContext.humidity}), current:(${this.TemperatureSensor?.CurrentTemperature}, ${this.HumiditySensor?.CurrentRelativeHumidity})`)
247
244
  // Check if the scale is not CELSIUS
248
- if (this.webhookContext.scale !== 'CELSIUS' && this.device.hub?.convertUnitTo === undefined) {
245
+ if (this.webhookContext.scale !== 'CELSIUS' && (this.device as meterConfig).convertUnitTo === undefined) {
249
246
  await this.warnLog(`received a non-CELSIUS Webhook scale: ${this.webhookContext.scale}, Use the *convertUnitsTo* config under Hub settings, if displaying incorrectly in HomeKit.`)
250
247
  }
251
248
 
252
249
  // CurrentRelativeHumidity
253
- if (!this.device.meter?.hide_humidity && this.HumiditySensor?.Service) {
250
+ if (!(this.device as meterConfig).hide_humidity && this.HumiditySensor?.Service) {
254
251
  this.HumiditySensor.CurrentRelativeHumidity = this.webhookContext.humidity
255
252
  await this.debugLog(`CurrentRelativeHumidity: ${this.HumiditySensor.CurrentRelativeHumidity}`)
256
253
  }
257
254
 
258
255
  // CurrentTemperature
259
- if (!this.device.meter?.hide_temperature && this.TemperatureSensor?.Service) {
260
- this.TemperatureSensor.CurrentTemperature = convertUnits(this.webhookContext.temperature, this.webhookContext.scale, this.device.hub?.convertUnitTo)
256
+ if (!(this.device as meterConfig).hide_temperature && this.TemperatureSensor?.Service) {
257
+ this.TemperatureSensor.CurrentTemperature = convertUnits(this.webhookContext.temperature, this.webhookContext.scale, (this.device as meterConfig).convertUnitTo)
261
258
  await this.debugLog(`CurrentTemperature: ${this.TemperatureSensor.CurrentTemperature}`)
262
259
  }
263
260
  }
@@ -280,22 +277,22 @@ export class Meter extends deviceBase {
280
277
 
281
278
  async BLERefreshStatus(): Promise<void> {
282
279
  await this.debugLog('BLERefreshStatus')
283
- const switchbot = await this.switchbotBLE()
284
- if (switchbot === undefined) {
285
- await this.BLERefreshConnection(switchbot)
280
+ const switchBotBLE = await this.switchbotBLE()
281
+ if (switchBotBLE === undefined) {
282
+ await this.BLERefreshConnection(switchBotBLE)
286
283
  } else {
287
284
  // Start to monitor advertisement packets
288
285
  (async () => {
289
286
  // Start to monitor advertisement packets
290
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as meterServiceData
287
+ const serviceData = await this.monitorAdvertisementPackets(switchBotBLE) as meterServiceData
291
288
  // Update HomeKit
292
289
  if (serviceData.model === SwitchBotBLEModel.Meter && serviceData.modelName === SwitchBotBLEModelName.Meter) {
293
290
  this.serviceData = serviceData
294
291
  await this.BLEparseStatus()
295
292
  await this.updateHomeKitCharacteristics()
296
293
  } else {
297
- await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`)
298
- await this.BLERefreshConnection(switchbot)
294
+ await this.errorLog(`failed to get serviceData, serviceData: ${JSON.stringify(serviceData)}`)
295
+ await this.BLERefreshConnection(switchBotBLE)
299
296
  }
300
297
  })()
301
298
  }
@@ -316,7 +313,7 @@ export class Meter extends deviceBase {
316
313
  await this.BLEparseStatus()
317
314
  await this.updateHomeKitCharacteristics()
318
315
  } catch (e: any) {
319
- await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`)
316
+ await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e.message ?? e}`)
320
317
  }
321
318
  }
322
319
  } catch (error) {
@@ -330,17 +327,17 @@ export class Meter extends deviceBase {
330
327
  async openAPIRefreshStatus(): Promise<void> {
331
328
  await this.debugLog('openAPIRefreshStatus')
332
329
  try {
333
- const { body, statusCode } = await this.deviceRefreshStatus()
334
- const deviceStatus: any = await body.json()
335
- await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
336
- if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
337
- await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
330
+ const { body } = await this.deviceRefreshStatus()
331
+ const deviceStatus: any = await body
332
+ await this.debugLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
333
+ if (await this.successfulStatusCodes(deviceStatus)) {
334
+ await this.debugSuccessLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
338
335
  this.deviceStatus = deviceStatus.body
339
336
  await this.openAPIparseStatus()
340
337
  await this.updateHomeKitCharacteristics()
341
338
  } else {
342
- await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
343
- await this.debugWarnLog(statusCode, deviceStatus)
339
+ await this.debugWarnLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
340
+ await this.debugWarnLog(deviceStatus)
344
341
  }
345
342
  } catch (e: any) {
346
343
  await this.apiError(e)
@@ -358,7 +355,7 @@ export class Meter extends deviceBase {
358
355
  await this.parseStatusWebhook()
359
356
  await this.updateHomeKitCharacteristics()
360
357
  } catch (e: any) {
361
- await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`)
358
+ await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e.message ?? e}`)
362
359
  }
363
360
  }
364
361
  } else {
@@ -371,11 +368,11 @@ export class Meter extends deviceBase {
371
368
  */
372
369
  async updateHomeKitCharacteristics(): Promise<void> {
373
370
  // CurrentRelativeHumidity
374
- if (!this.device.meter?.hide_humidity && this.HumiditySensor?.Service) {
371
+ if (!(this.device as meterConfig).hide_humidity && this.HumiditySensor?.Service) {
375
372
  await this.updateCharacteristic(this.HumiditySensor.Service, this.hap.Characteristic.CurrentRelativeHumidity, this.HumiditySensor.CurrentRelativeHumidity, 'CurrentRelativeHumidity')
376
373
  }
377
374
  // CurrentTemperature
378
- if (!this.device.meter?.hide_temperature && this.TemperatureSensor?.Service) {
375
+ if (!(this.device as meterConfig).hide_temperature && this.TemperatureSensor?.Service) {
379
376
  await this.updateCharacteristic(this.TemperatureSensor.Service, this.hap.Characteristic.CurrentTemperature, this.TemperatureSensor.CurrentTemperature, 'CurrentTemperature')
380
377
  }
381
378
  // BatteryLevel
@@ -394,10 +391,10 @@ export class Meter extends deviceBase {
394
391
 
395
392
  async offlineOff(): Promise<void> {
396
393
  if (this.device.offline) {
397
- if (!this.device.meter?.hide_humidity && this.HumiditySensor?.Service) {
394
+ if (!(this.device as meterConfig).hide_humidity && this.HumiditySensor?.Service) {
398
395
  this.HumiditySensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity, 50)
399
396
  }
400
- if (!this.device.meter?.hide_temperature && this.TemperatureSensor?.Service) {
397
+ if (!(this.device as meterConfig).hide_temperature && this.TemperatureSensor?.Service) {
401
398
  this.TemperatureSensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, 30)
402
399
  }
403
400
  this.Battery.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, 100)
@@ -406,10 +403,10 @@ export class Meter extends deviceBase {
406
403
  }
407
404
 
408
405
  async apiError(e: any): Promise<void> {
409
- if (!this.device.meter?.hide_humidity && this.HumiditySensor?.Service) {
406
+ if (!(this.device as meterConfig).hide_humidity && this.HumiditySensor?.Service) {
410
407
  this.HumiditySensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity, e)
411
408
  }
412
- if (!this.device.meter?.hide_temperature && this.TemperatureSensor?.Service) {
409
+ if (!(this.device as meterConfig).hide_temperature && this.TemperatureSensor?.Service) {
413
410
  this.TemperatureSensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, e)
414
411
  }
415
412
  this.Battery.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, e)
@@ -3,13 +3,10 @@
3
3
  * meterplus.ts: @switchbot/homebridge-switchbot.
4
4
  */
5
5
  import type { CharacteristicValue, PlatformAccessory, Service } from 'homebridge'
6
+ import type { device, meterPlusServiceData, meterPlusStatus, meterPlusWebhookContext } from 'node-switchbot'
6
7
 
7
8
  import type { SwitchBotPlatform } from '../platform.js'
8
- import type { devicesConfig } from '../settings.js'
9
- import type { meterPlusServiceData } from '../types/bledevicestatus.js'
10
- import type { device } from '../types/devicelist.js'
11
- import type { meterPlusStatus } from '../types/devicestatus.js'
12
- import type { meterPlusWebhookContext } from '../types/devicewebhookstatus.js'
9
+ import type { devicesConfig, meterConfig } from '../settings.js'
13
10
 
14
11
  import { Units } from 'homebridge'
15
12
  /*
@@ -94,7 +91,7 @@ export class MeterPlus extends deviceBase {
94
91
  })
95
92
 
96
93
  // Initialize Temperature Sensor Service
97
- if (device.meter?.hide_temperature) {
94
+ if ((device as meterConfig).hide_temperature) {
98
95
  if (this.TemperatureSensor) {
99
96
  this.debugLog('Removing Temperature Sensor Service')
100
97
  this.TemperatureSensor.Service = this.accessory.getService(this.hap.Service.TemperatureSensor) as Service
@@ -121,7 +118,7 @@ export class MeterPlus extends deviceBase {
121
118
  })
122
119
  }
123
120
  // Initialize Humidity Sensor Service
124
- if (device.meter?.hide_humidity) {
121
+ if ((device as meterConfig).hide_humidity) {
125
122
  if (this.HumiditySensor) {
126
123
  this.debugLog('Removing Humidity Sensor Service')
127
124
  this.HumiditySensor.Service = this.accessory.getService(this.hap.Service.HumiditySensor) as Service
@@ -149,7 +146,7 @@ export class MeterPlus extends deviceBase {
149
146
  this.debugLog('Retrieve initial values and update Homekit')
150
147
  this.refreshStatus()
151
148
  } catch (e: any) {
152
- this.errorLog(`failed to retrieve initial values and update Homekit, Error: ${e}`)
149
+ this.errorLog(`failed to retrieve initial values and update Homekit, Error: ${e.message ?? e}`)
153
150
  }
154
151
 
155
152
  // regisiter webhook event handler if enabled
@@ -157,7 +154,7 @@ export class MeterPlus extends deviceBase {
157
154
  this.debugLog('Registering Webhook Event Handler')
158
155
  this.registerWebhook()
159
156
  } catch (e: any) {
160
- this.errorLog(`failed to registerWebhook, Error: ${e}`)
157
+ this.errorLog(`failed to registerWebhook, Error: ${e.message ?? e}`)
161
158
  }
162
159
 
163
160
  // regisiter platform BLE event handler if enabled
@@ -165,7 +162,7 @@ export class MeterPlus extends deviceBase {
165
162
  this.debugLog('Registering Platform BLE Event Handler')
166
163
  this.registerPlatformBLE()
167
164
  } catch (e: any) {
168
- this.errorLog(`failed to registerPlatformBLE, Error: ${e}`)
165
+ this.errorLog(`failed to registerPlatformBLE, Error: ${e.message ?? e}`)
169
166
  }
170
167
 
171
168
  // Start an update interval
@@ -181,12 +178,12 @@ export class MeterPlus extends deviceBase {
181
178
  await this.debugLog(`(temperature, humidity) = BLE:(${this.serviceData.celsius}, ${this.serviceData.humidity}), current:(${this.TemperatureSensor?.CurrentTemperature}, ${this.HumiditySensor?.CurrentRelativeHumidity})`)
182
179
 
183
180
  // CurrentRelativeHumidity
184
- if (!this.device.iosensor?.hide_humidity && this.HumiditySensor?.Service) {
181
+ if (!(this.device as meterConfig).hide_humidity && this.HumiditySensor?.Service) {
185
182
  this.HumiditySensor.CurrentRelativeHumidity = validHumidity(this.serviceData.humidity, 0, 100)
186
183
  await this.debugLog(`CurrentRelativeHumidity: ${this.HumiditySensor.CurrentRelativeHumidity}%`)
187
184
  }
188
185
  // Current Temperature
189
- if (!this.device.meter?.hide_temperature && this.TemperatureSensor?.Service) {
186
+ if (!(this.device as meterConfig).hide_temperature && this.TemperatureSensor?.Service) {
190
187
  const CELSIUS = this.serviceData.celsius < 0 ? 0 : this.serviceData.celsius > 100 ? 100 : this.serviceData.celsius
191
188
  this.TemperatureSensor.CurrentTemperature = CELSIUS
192
189
  await this.debugLog(`CurrentTemperature: ${this.TemperatureSensor.CurrentTemperature}°c`)
@@ -206,13 +203,13 @@ export class MeterPlus extends deviceBase {
206
203
  await this.debugLog(`(temperature, humidity) = OpenAPI:(${this.deviceStatus.temperature}, ${this.deviceStatus.humidity}), current:(${this.TemperatureSensor?.CurrentTemperature}, ${this.HumiditySensor?.CurrentRelativeHumidity})`)
207
204
 
208
205
  // CurrentRelativeHumidity
209
- if (!this.device.meter?.hide_humidity && this.HumiditySensor?.Service) {
206
+ if (!(this.device as meterConfig).hide_humidity && this.HumiditySensor?.Service) {
210
207
  this.HumiditySensor.CurrentRelativeHumidity = this.deviceStatus.humidity
211
208
  await this.debugLog(`CurrentRelativeHumidity: ${this.HumiditySensor.CurrentRelativeHumidity}%`)
212
209
  }
213
210
 
214
211
  // CurrentTemperature
215
- if (!this.device.meter?.hide_temperature && this.TemperatureSensor?.Service) {
212
+ if (!(this.device as meterConfig).hide_temperature && this.TemperatureSensor?.Service) {
216
213
  this.TemperatureSensor.CurrentTemperature = this.deviceStatus.temperature
217
214
  await this.debugLog(`CurrentTemperature: ${this.TemperatureSensor.CurrentTemperature}°c`)
218
215
  }
@@ -245,21 +242,21 @@ export class MeterPlus extends deviceBase {
245
242
 
246
243
  async parseStatusWebhook(): Promise<void> {
247
244
  await this.debugLog('parseStatusWebhook')
248
- await this.debugLog(`(scale, temperature, humidity) = Webhook:(${this.webhookContext.scale}, ${convertUnits(this.webhookContext.temperature, this.webhookContext.scale, this.device.meter?.convertUnitTo)}, ${this.webhookContext.humidity}) current:(${this.TemperatureSensor?.CurrentTemperature}, ${this.HumiditySensor?.CurrentRelativeHumidity})`)
245
+ await this.debugLog(`(scale, temperature, humidity) = Webhook:(${this.webhookContext.scale}, ${convertUnits(this.webhookContext.temperature, this.webhookContext.scale, (this.device as meterConfig).convertUnitTo)}, ${this.webhookContext.humidity}) current:(${this.TemperatureSensor?.CurrentTemperature}, ${this.HumiditySensor?.CurrentRelativeHumidity})`)
249
246
  // Check if the scale is not CELSIUS
250
- if (this.webhookContext.scale !== 'CELSIUS' && this.device.hub?.convertUnitTo === undefined) {
247
+ if (this.webhookContext.scale !== 'CELSIUS' && (this.device as meterConfig).convertUnitTo === undefined) {
251
248
  await this.warnLog(`received a non-CELSIUS Webhook scale: ${this.webhookContext.scale}, Use the *convertUnitsTo* config under Hub settings, if displaying incorrectly in HomeKit.`)
252
249
  }
253
250
 
254
251
  // CurrentRelativeHumidity
255
- if (!this.device.meter?.hide_humidity && this.HumiditySensor?.Service) {
252
+ if (!(this.device as meterConfig).hide_humidity && this.HumiditySensor?.Service) {
256
253
  this.HumiditySensor.CurrentRelativeHumidity = this.webhookContext.humidity
257
254
  await this.debugLog(`CurrentRelativeHumidity: ${this.HumiditySensor.CurrentRelativeHumidity}`)
258
255
  }
259
256
 
260
257
  // CurrentTemperature
261
- if (!this.device.meter?.hide_temperature && this.TemperatureSensor?.Service) {
262
- this.TemperatureSensor.CurrentTemperature = convertUnits(this.webhookContext.temperature, this.webhookContext.scale, this.device.hub?.convertUnitTo)
258
+ if (!(this.device as meterConfig).hide_temperature && this.TemperatureSensor?.Service) {
259
+ this.TemperatureSensor.CurrentTemperature = convertUnits(this.webhookContext.temperature, this.webhookContext.scale, (this.device as meterConfig).convertUnitTo)
263
260
  await this.debugLog(`CurrentTemperature: ${this.TemperatureSensor.CurrentTemperature}`)
264
261
  }
265
262
  }
@@ -282,22 +279,22 @@ export class MeterPlus extends deviceBase {
282
279
 
283
280
  async BLERefreshStatus(): Promise<void> {
284
281
  await this.debugLog('BLERefreshStatus')
285
- const switchbot = await this.switchbotBLE()
286
- if (switchbot === undefined) {
287
- await this.BLERefreshConnection(switchbot)
282
+ const switchBotBLE = await this.switchbotBLE()
283
+ if (switchBotBLE === undefined) {
284
+ await this.BLERefreshConnection(switchBotBLE)
288
285
  } else {
289
286
  // Start to monitor advertisement packets
290
287
  (async () => {
291
288
  // Start to monitor advertisement packets
292
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as meterPlusServiceData
289
+ const serviceData = await this.monitorAdvertisementPackets(switchBotBLE) as meterPlusServiceData
293
290
  // Update HomeKit
294
291
  if (serviceData.model === SwitchBotBLEModel.MeterPlus && serviceData.modelName === SwitchBotBLEModelName.MeterPlus) {
295
292
  this.serviceData = serviceData
296
293
  await this.BLEparseStatus()
297
294
  await this.updateHomeKitCharacteristics()
298
295
  } else {
299
- await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`)
300
- await this.BLERefreshConnection(switchbot)
296
+ await this.errorLog(`failed to get serviceData, serviceData: ${JSON.stringify(serviceData)}`)
297
+ await this.BLERefreshConnection(switchBotBLE)
301
298
  }
302
299
  })()
303
300
  }
@@ -318,7 +315,7 @@ export class MeterPlus extends deviceBase {
318
315
  await this.BLEparseStatus()
319
316
  await this.updateHomeKitCharacteristics()
320
317
  } catch (e: any) {
321
- await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`)
318
+ await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e.message ?? e}`)
322
319
  }
323
320
  }
324
321
  } catch (error) {
@@ -332,17 +329,17 @@ export class MeterPlus extends deviceBase {
332
329
  async openAPIRefreshStatus(): Promise<void> {
333
330
  await this.debugLog('openAPIRefreshStatus')
334
331
  try {
335
- const { body, statusCode } = await this.deviceRefreshStatus()
336
- const deviceStatus: any = await body.json()
337
- await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
338
- if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
339
- await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
332
+ const { body } = await this.deviceRefreshStatus()
333
+ const deviceStatus: any = await body
334
+ await this.debugLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
335
+ if (await this.successfulStatusCodes(deviceStatus)) {
336
+ await this.debugSuccessLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
340
337
  this.deviceStatus = deviceStatus.body
341
338
  await this.openAPIparseStatus()
342
339
  await this.updateHomeKitCharacteristics()
343
340
  } else {
344
- await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
345
- await this.debugWarnLog(statusCode, deviceStatus)
341
+ await this.debugWarnLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
342
+ await this.debugWarnLog(deviceStatus)
346
343
  }
347
344
  } catch (e: any) {
348
345
  await this.apiError(e)
@@ -360,7 +357,7 @@ export class MeterPlus extends deviceBase {
360
357
  await this.parseStatusWebhook()
361
358
  await this.updateHomeKitCharacteristics()
362
359
  } catch (e: any) {
363
- await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`)
360
+ await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e.message ?? e}`)
364
361
  }
365
362
  }
366
363
  } else {
@@ -373,11 +370,11 @@ export class MeterPlus extends deviceBase {
373
370
  */
374
371
  async updateHomeKitCharacteristics(): Promise<void> {
375
372
  // CurrentRelativeHumidity
376
- if (!this.device.meter?.hide_humidity && this.HumiditySensor?.Service) {
373
+ if (!(this.device as meterConfig).hide_humidity && this.HumiditySensor?.Service) {
377
374
  await this.updateCharacteristic(this.HumiditySensor.Service, this.hap.Characteristic.CurrentRelativeHumidity, this.HumiditySensor.CurrentRelativeHumidity, 'CurrentRelativeHumidity')
378
375
  }
379
376
  // CurrentTemperature
380
- if (!this.device.meter?.hide_temperature && this.TemperatureSensor?.Service) {
377
+ if (!(this.device as meterConfig).hide_temperature && this.TemperatureSensor?.Service) {
381
378
  await this.updateCharacteristic(this.TemperatureSensor.Service, this.hap.Characteristic.CurrentTemperature, this.TemperatureSensor.CurrentTemperature, 'CurrentTemperature')
382
379
  }
383
380
  // BatteryLevel
@@ -396,10 +393,10 @@ export class MeterPlus extends deviceBase {
396
393
 
397
394
  async offlineOff(): Promise<void> {
398
395
  if (this.device.offline) {
399
- if (!this.device.meter?.hide_humidity && this.HumiditySensor?.Service) {
396
+ if (!(this.device as meterConfig).hide_humidity && this.HumiditySensor?.Service) {
400
397
  this.HumiditySensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity, 50)
401
398
  }
402
- if (!this.device.meter?.hide_temperature && this.TemperatureSensor?.Service) {
399
+ if (!(this.device as meterConfig).hide_temperature && this.TemperatureSensor?.Service) {
403
400
  this.TemperatureSensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, 30)
404
401
  }
405
402
  this.Battery.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, 100)
@@ -408,10 +405,10 @@ export class MeterPlus extends deviceBase {
408
405
  }
409
406
 
410
407
  async apiError(e: any): Promise<void> {
411
- if (!this.device.meter?.hide_humidity && this.HumiditySensor?.Service) {
408
+ if (!(this.device as meterConfig).hide_humidity && this.HumiditySensor?.Service) {
412
409
  this.HumiditySensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity, e)
413
410
  }
414
- if (!this.device.meter?.hide_temperature && this.TemperatureSensor?.Service) {
411
+ if (!(this.device as meterConfig).hide_temperature && this.TemperatureSensor?.Service) {
415
412
  this.TemperatureSensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, e)
416
413
  }
417
414
  this.Battery.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, e)
@@ -3,13 +3,10 @@
3
3
  * motion.ts: @switchbot/homebridge-switchbot.
4
4
  */
5
5
  import type { CharacteristicValue, PlatformAccessory, Service } from 'homebridge'
6
+ import type { device, motionSensorServiceData, motionSensorStatus, motionSensorWebhookContext } from 'node-switchbot'
6
7
 
7
8
  import type { SwitchBotPlatform } from '../platform.js'
8
- import type { devicesConfig } from '../settings.js'
9
- import type { motionSensorServiceData } from '../types/bledevicestatus.js'
10
- import type { device } from '../types/devicelist.js'
11
- import type { motionSensorStatus } from '../types/devicestatus.js'
12
- import type { motionSensorWebhookContext } from '../types/devicewebhookstatus.js'
9
+ import type { devicesConfig, motionConfig } from '../settings.js'
13
10
 
14
11
  /*
15
12
  * For Testing Locally:
@@ -107,7 +104,7 @@ export class Motion extends deviceBase {
107
104
  })
108
105
 
109
106
  // Initialize Light Sensor Service
110
- if (device.motion?.hide_lightsensor) {
107
+ if ((device as motionConfig).hide_lightsensor) {
111
108
  if (this.LightSensor) {
112
109
  this.debugLog('Removing Light Sensor Service')
113
110
  this.LightSensor.Service = this.accessory.getService(this.hap.Service.LightSensor) as Service
@@ -133,7 +130,7 @@ export class Motion extends deviceBase {
133
130
  this.debugLog('Retrieve initial values and update Homekit')
134
131
  this.refreshStatus()
135
132
  } catch (e: any) {
136
- this.errorLog(`failed to retrieve initial values and update Homekit, Error: ${e}`)
133
+ this.errorLog(`failed to retrieve initial values and update Homekit, Error: ${e.message ?? e}`)
137
134
  }
138
135
 
139
136
  // regisiter webhook event handler if enabled
@@ -141,7 +138,7 @@ export class Motion extends deviceBase {
141
138
  this.debugLog('Registering Webhook Event Handler')
142
139
  this.registerWebhook()
143
140
  } catch (e: any) {
144
- this.errorLog(`failed to registerWebhook, Error: ${e}`)
141
+ this.errorLog(`failed to registerWebhook, Error: ${e.message ?? e}`)
145
142
  }
146
143
 
147
144
  // regisiter platform BLE event handler if enabled
@@ -149,7 +146,7 @@ export class Motion extends deviceBase {
149
146
  this.debugLog('Registering Platform BLE Event Handler')
150
147
  this.registerPlatformBLE()
151
148
  } catch (e: any) {
152
- this.errorLog(`failed to registerPlatformBLE, Error: ${e}`)
149
+ this.errorLog(`failed to registerPlatformBLE, Error: ${e.message ?? e}`)
153
150
  }
154
151
 
155
152
  // Start an update interval
@@ -169,9 +166,9 @@ export class Motion extends deviceBase {
169
166
  await this.debugLog(`MotionDetected: ${this.MotionSensor.MotionDetected}`)
170
167
 
171
168
  // CurrentAmbientLightLevel
172
- if (!this.device.motion?.hide_lightsensor && this.LightSensor?.Service) {
173
- const set_minLux = this.device.blindTilt?.set_minLux ?? 1
174
- const set_maxLux = this.device.blindTilt?.set_maxLux ?? 6001
169
+ if (!(this.device as motionConfig).hide_lightsensor && this.LightSensor?.Service) {
170
+ const set_minLux = (this.device as motionConfig).set_minLux ?? 1
171
+ const set_maxLux = (this.device as motionConfig).set_maxLux ?? 6001
175
172
  const lightLevel = this.serviceData.lightLevel === 'bright' ? set_maxLux : set_minLux
176
173
  this.LightSensor.CurrentAmbientLightLevel = await this.getLightLevel(lightLevel, set_minLux, set_maxLux, 2)
177
174
  await this.debugLog(`LightLevel: ${this.serviceData.lightLevel}, CurrentAmbientLightLevel: ${this.LightSensor.CurrentAmbientLightLevel}`)
@@ -197,9 +194,9 @@ export class Motion extends deviceBase {
197
194
  await this.debugLog(`MotionDetected: ${this.MotionSensor.MotionDetected}`)
198
195
 
199
196
  // CurrentAmbientLightLevel
200
- if (!this.device.motion?.hide_lightsensor && this.LightSensor?.Service) {
201
- const set_minLux = this.device.blindTilt?.set_minLux ?? 1
202
- const set_maxLux = this.device.blindTilt?.set_maxLux ?? 6001
197
+ if (!(this.device as motionConfig).hide_lightsensor && this.LightSensor?.Service) {
198
+ const set_minLux = (this.device as motionConfig).set_minLux ?? 1
199
+ const set_maxLux = (this.device as motionConfig).set_maxLux ?? 6001
203
200
  const lightLevel = this.deviceStatus.brightness === 'bright' ? set_maxLux : set_minLux
204
201
  this.LightSensor.CurrentAmbientLightLevel = await this.getLightLevel(lightLevel, set_minLux, set_maxLux, 2)
205
202
  await this.debugLog(`LightLevel: ${this.deviceStatus.brightness}, CurrentAmbientLightLevel: ${this.LightSensor.CurrentAmbientLightLevel}`)
@@ -257,22 +254,22 @@ export class Motion extends deviceBase {
257
254
 
258
255
  async BLERefreshStatus(): Promise<void> {
259
256
  await this.debugLog('BLERefreshStatus')
260
- const switchbot = await this.switchbotBLE()
261
- if (switchbot === undefined) {
262
- await this.BLERefreshConnection(switchbot)
257
+ const switchBotBLE = await this.switchbotBLE()
258
+ if (switchBotBLE === undefined) {
259
+ await this.BLERefreshConnection(switchBotBLE)
263
260
  } else {
264
261
  // Start to monitor advertisement packets
265
262
  (async () => {
266
263
  // Start to monitor advertisement packets
267
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as motionSensorServiceData
264
+ const serviceData = await this.monitorAdvertisementPackets(switchBotBLE) as motionSensorServiceData
268
265
  // Update HomeKit
269
266
  if (serviceData.model === SwitchBotBLEModel.MotionSensor && serviceData.modelName === SwitchBotBLEModelName.MotionSensor) {
270
267
  this.serviceData = serviceData
271
268
  await this.BLEparseStatus()
272
269
  await this.updateHomeKitCharacteristics()
273
270
  } else {
274
- await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`)
275
- await this.BLERefreshConnection(switchbot)
271
+ await this.errorLog(`failed to get serviceData, serviceData: ${JSON.stringify(serviceData)}`)
272
+ await this.BLERefreshConnection(switchBotBLE)
276
273
  }
277
274
  })()
278
275
  }
@@ -293,7 +290,7 @@ export class Motion extends deviceBase {
293
290
  await this.BLEparseStatus()
294
291
  await this.updateHomeKitCharacteristics()
295
292
  } catch (e: any) {
296
- await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`)
293
+ await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e.message ?? e}`)
297
294
  }
298
295
  }
299
296
  } catch (error) {
@@ -307,17 +304,17 @@ export class Motion extends deviceBase {
307
304
  async openAPIRefreshStatus(): Promise<void> {
308
305
  await this.debugLog('openAPIRefreshStatus')
309
306
  try {
310
- const { body, statusCode } = await this.deviceRefreshStatus()
311
- const deviceStatus: any = await body.json()
312
- await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
313
- if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
314
- await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
307
+ const { body } = await this.deviceRefreshStatus()
308
+ const deviceStatus: any = await body
309
+ await this.debugLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
310
+ if (await this.successfulStatusCodes(deviceStatus)) {
311
+ await this.debugSuccessLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
315
312
  this.deviceStatus = deviceStatus.body
316
313
  await this.openAPIparseStatus()
317
314
  await this.updateHomeKitCharacteristics()
318
315
  } else {
319
- await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
320
- await this.debugWarnLog(statusCode, deviceStatus)
316
+ await this.debugWarnLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
317
+ await this.debugWarnLog(deviceStatus)
321
318
  }
322
319
  } catch (e: any) {
323
320
  await this.apiError(e)
@@ -335,7 +332,7 @@ export class Motion extends deviceBase {
335
332
  await this.parseStatusWebhook()
336
333
  await this.updateHomeKitCharacteristics()
337
334
  } catch (e: any) {
338
- await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`)
335
+ await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e.message ?? e}`)
339
336
  }
340
337
  }
341
338
  } else {
@@ -354,7 +351,7 @@ export class Motion extends deviceBase {
354
351
  // StatusLowBattery
355
352
  await this.updateCharacteristic(this.Battery.Service, this.hap.Characteristic.StatusLowBattery, this.Battery.StatusLowBattery, 'StatusLowBattery')
356
353
  // CurrentAmbientLightLevel
357
- if (!this.device.motion?.hide_lightsensor && this.LightSensor?.Service) {
354
+ if (!(this.device as motionConfig).hide_lightsensor && this.LightSensor?.Service) {
358
355
  await this.updateCharacteristic(this.LightSensor.Service, this.hap.Characteristic.CurrentAmbientLightLevel, this.LightSensor.CurrentAmbientLightLevel, 'CurrentAmbientLightLevel')
359
356
  }
360
357
  }
@@ -377,7 +374,7 @@ export class Motion extends deviceBase {
377
374
  this.MotionSensor.Service.updateCharacteristic(this.hap.Characteristic.MotionDetected, e)
378
375
  this.Battery.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, e)
379
376
  this.Battery.Service.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, e)
380
- if (!this.device.motion?.hide_lightsensor && this.LightSensor?.Service) {
377
+ if (!(this.device as motionConfig).hide_lightsensor && this.LightSensor?.Service) {
381
378
  this.LightSensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel, e)
382
379
  }
383
380
  }