@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
package/src/device/fan.ts CHANGED
@@ -3,13 +3,10 @@
3
3
  * plug.ts: @switchbot/homebridge-switchbot.
4
4
  */
5
5
  import type { CharacteristicValue, PlatformAccessory, Service } from 'homebridge'
6
+ import type { batteryCirculatorFanServiceData, batteryCirculatorFanStatus, batteryCirculatorFanWebhookContext, bodyChange, device, SwitchbotDevice } from 'node-switchbot'
6
7
 
7
8
  import type { SwitchBotPlatform } from '../platform.js'
8
9
  import type { devicesConfig } from '../settings.js'
9
- import type { batteryCirculatorFanServiceData } from '../types/bledevicestatus.js'
10
- import type { device } from '../types/devicelist.js'
11
- import type { batteryCirculatorFanStatus } from '../types/devicestatus.js'
12
- import type { batteryCirculatorFanWebhookContext } from '../types/devicewebhookstatus.js'
13
10
 
14
11
  /*
15
12
  * For Testing Locally:
@@ -149,7 +146,7 @@ export class Fan 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 Fan 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 Fan 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
@@ -204,7 +201,8 @@ export class Fan extends deviceBase {
204
201
  await this.debugLog(`Active: ${this.Fan.Active}`)
205
202
 
206
203
  // RotationSpeed
207
- this.Fan.RotationSpeed = this.serviceData.fanSpeed
204
+ this.Fan.RotationSpeed = this.serviceData.fanSpeed // ?? 0
205
+ await this.debugLog(`RotationSpeed: ${this.Fan.RotationSpeed}`)
208
206
  }
209
207
 
210
208
  async openAPIparseStatus() {
@@ -222,7 +220,7 @@ export class Fan extends deviceBase {
222
220
  await this.debugLog(`SwingMode: ${this.Fan.SwingMode}`)
223
221
 
224
222
  // RotationSpeed
225
- this.Fan.RotationSpeed = this.deviceStatus.fanSpeed
223
+ this.Fan.RotationSpeed = this.deviceStatus.fanSpeed ?? 0
226
224
  await this.debugLog(`RotationSpeed: ${this.Fan.RotationSpeed}`)
227
225
 
228
226
  // ChargingState
@@ -271,7 +269,7 @@ export class Fan extends deviceBase {
271
269
  await this.debugLog(`SwingMode: ${this.Fan.SwingMode}`)
272
270
 
273
271
  // RotationSpeed
274
- this.Fan.RotationSpeed = this.webhookContext.fanSpeed
272
+ this.Fan.RotationSpeed = this.webhookContext.fanSpeed // ?? 0
275
273
  await this.debugLog(`RotationSpeed: ${this.Fan.RotationSpeed}`)
276
274
 
277
275
  // BatteryLevel
@@ -322,22 +320,22 @@ export class Fan extends deviceBase {
322
320
 
323
321
  async BLERefreshStatus(): Promise<void> {
324
322
  await this.debugLog('BLERefreshStatus')
325
- const switchbot = await this.switchbotBLE()
326
- if (switchbot === undefined) {
327
- await this.BLERefreshConnection(switchbot)
323
+ const switchBotBLE = await this.switchbotBLE()
324
+ if (switchBotBLE === undefined) {
325
+ await this.BLERefreshConnection(switchBotBLE)
328
326
  } else {
329
327
  // Start to monitor advertisement packets
330
328
  (async () => {
331
329
  // Start to monitor advertisement packets
332
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as batteryCirculatorFanServiceData
330
+ const serviceData = await this.monitorAdvertisementPackets(switchBotBLE) as batteryCirculatorFanServiceData
333
331
  // Update HomeKit
334
332
  if (serviceData.model === SwitchBotBLEModel.Unknown && SwitchBotBLEModelName.Unknown) {
335
333
  this.serviceData = serviceData
336
334
  await this.BLEparseStatus()
337
335
  await this.updateHomeKitCharacteristics()
338
336
  } else {
339
- await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`)
340
- await this.BLERefreshConnection(switchbot)
337
+ await this.errorLog(`failed to get serviceData, serviceData: ${JSON.stringify(serviceData)}`)
338
+ await this.BLERefreshConnection(switchBotBLE)
341
339
  }
342
340
  })()
343
341
  }
@@ -358,7 +356,7 @@ export class Fan extends deviceBase {
358
356
  await this.BLEparseStatus()
359
357
  await this.updateHomeKitCharacteristics()
360
358
  } catch (e: any) {
361
- await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`)
359
+ await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e.message ?? e}`)
362
360
  }
363
361
  }
364
362
  } catch (error) {
@@ -372,17 +370,17 @@ export class Fan extends deviceBase {
372
370
  async openAPIRefreshStatus(): Promise<void> {
373
371
  await this.debugLog('openAPIRefreshStatus')
374
372
  try {
375
- const { body, statusCode } = await this.deviceRefreshStatus()
376
- const deviceStatus: any = await body.json()
377
- await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
378
- if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
379
- await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
373
+ const { body } = await this.deviceRefreshStatus()
374
+ const deviceStatus: any = await body
375
+ await this.debugLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
376
+ if (await this.successfulStatusCodes(deviceStatus)) {
377
+ await this.debugSuccessLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
380
378
  this.deviceStatus = deviceStatus.body
381
379
  await this.openAPIparseStatus()
382
380
  await this.updateHomeKitCharacteristics()
383
381
  } else {
384
- await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
385
- await this.debugWarnLog(statusCode, deviceStatus)
382
+ await this.debugWarnLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
383
+ await this.debugWarnLog(deviceStatus)
386
384
  }
387
385
  } catch (e: any) {
388
386
  await this.apiError(e)
@@ -400,7 +398,7 @@ export class Fan extends deviceBase {
400
398
  await this.parseStatusWebhook()
401
399
  await this.updateHomeKitCharacteristics()
402
400
  } catch (e: any) {
403
- await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`)
401
+ await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e.message ?? e}`)
404
402
  }
405
403
  }
406
404
  } else {
@@ -453,22 +451,23 @@ export class Fan extends deviceBase {
453
451
  await this.debugLog('BLEpushChanges')
454
452
  if (this.Fan.Active !== this.accessory.context.Active) {
455
453
  await this.debugLog(`BLEpushChanges On: ${this.Fan.Active} OnCached: ${this.accessory.context.Active}`)
456
- const switchbot = await this.platform.connectBLE(this.accessory, this.device)
454
+ const switchBotBLE = await this.platform.connectBLE(this.accessory, this.device)
457
455
  try {
458
456
  const formattedDeviceId = formatDeviceIdAsMac(this.device.deviceId)
459
457
  this.device.bleMac = formattedDeviceId
460
458
  await this.debugLog(`bleMac: ${this.device.bleMac}`)
461
- if (switchbot !== false) {
462
- switchbot
459
+ if (switchBotBLE !== false) {
460
+ switchBotBLE
463
461
  .discover({ model: this.device.bleModel, id: this.device.bleMac })
464
- .then(async (device_list: any) => {
462
+ .then(async (device_list: SwitchbotDevice[]) => {
463
+ const deviceList = device_list as unknown as SwitchbotDevice[]
465
464
  return await this.retryBLE({
466
465
  max: await this.maxRetryBLE(),
467
466
  fn: async () => {
468
467
  if (this.Fan.Active) {
469
- return await device_list[0].turnOn({ id: this.device.bleMac })
468
+ return await deviceList[0].turnOn()
470
469
  } else {
471
- return await device_list[0].turnOff({ id: this.device.bleMac })
470
+ return await deviceList[0].turnOff()
472
471
  }
473
472
  },
474
473
  })
@@ -483,7 +482,7 @@ export class Fan extends deviceBase {
483
482
  await this.BLEPushConnection()
484
483
  })
485
484
  } else {
486
- await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`)
485
+ await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${JSON.stringify(switchBotBLE)}`)
487
486
  await this.BLEPushConnection()
488
487
  }
489
488
  } catch (error) {
@@ -498,21 +497,20 @@ export class Fan extends deviceBase {
498
497
  await this.debugLog('openAPIpushChanges')
499
498
  if (this.Fan.Active !== this.accessory.context.Active) {
500
499
  const command = this.Fan.Active ? 'turnOn' : 'turnOff'
501
- const bodyChange = JSON.stringify({
500
+ const bodyChange: bodyChange = {
502
501
  command: `${command}`,
503
502
  parameter: 'default',
504
503
  commandType: 'command',
505
- })
504
+ }
506
505
  await this.debugLog(`SwitchBot OpenAPI bodyChange: ${JSON.stringify(bodyChange)}`)
507
506
  try {
508
- const { body, statusCode } = await this.pushChangeRequest(bodyChange)
509
- const deviceStatus: any = await body.json()
510
- await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
511
- if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
512
- await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
507
+ const { body } = await this.pushChangeRequest(bodyChange)
508
+ const deviceStatus: any = await body
509
+ await this.debugLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
510
+ if (await this.successfulStatusCodes(deviceStatus)) {
511
+ await this.debugSuccessLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
513
512
  await this.updateHomeKitCharacteristics()
514
513
  } else {
515
- await this.statusCode(statusCode)
516
514
  await this.statusCode(deviceStatus.statusCode)
517
515
  }
518
516
  } catch (e: any) {
@@ -527,21 +525,20 @@ export class Fan extends deviceBase {
527
525
  async pushRotationSpeedChanges(): Promise<void> {
528
526
  await this.debugLog('pushRotationSpeedChanges')
529
527
  if (this.Fan.SwingMode !== this.accessory.context.SwingMode) {
530
- const bodyChange = JSON.stringify({
528
+ const bodyChange: bodyChange = {
531
529
  command: 'setWindSpeed',
532
530
  parameter: `${this.Fan.RotationSpeed}`,
533
531
  commandType: 'command',
534
- })
532
+ }
535
533
  await this.debugLog(`SwitchBot OpenAPI bodyChange: ${JSON.stringify(bodyChange)}`)
536
534
  try {
537
- const { body, statusCode } = await this.pushChangeRequest(bodyChange)
538
- const deviceStatus: any = await body.json()
539
- await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
540
- if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
541
- await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
535
+ const { body } = await this.pushChangeRequest(bodyChange)
536
+ const deviceStatus: any = await body
537
+ await this.debugLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
538
+ if (await this.successfulStatusCodes(deviceStatus)) {
539
+ await this.debugSuccessLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
542
540
  await this.updateHomeKitCharacteristics()
543
541
  } else {
544
- await this.statusCode(statusCode)
545
542
  await this.statusCode(deviceStatus.statusCode)
546
543
  }
547
544
  } catch (e: any) {
@@ -557,21 +554,20 @@ export class Fan extends deviceBase {
557
554
  await this.debugLog('pushSwingModeChanges')
558
555
  if (this.Fan.SwingMode !== this.accessory.context.SwingMode) {
559
556
  const parameter = this.Fan.SwingMode === this.hap.Characteristic.SwingMode.SWING_ENABLED ? 'on' : 'off'
560
- const bodyChange = JSON.stringify({
557
+ const bodyChange: bodyChange = {
561
558
  command: 'setOscillation',
562
559
  parameter: `${parameter}`,
563
560
  commandType: 'command',
564
- })
561
+ }
565
562
  await this.debugLog(`SwitchBot OpenAPI bodyChange: ${JSON.stringify(bodyChange)}`)
566
563
  try {
567
- const { body, statusCode } = await this.pushChangeRequest(bodyChange)
568
- const deviceStatus: any = await body.json()
569
- await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
570
- if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
571
- await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
564
+ const { body } = await this.pushChangeRequest(bodyChange)
565
+ const deviceStatus: any = await body
566
+ await this.debugLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
567
+ if (await this.successfulStatusCodes(deviceStatus)) {
568
+ await this.debugSuccessLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
572
569
  await this.updateHomeKitCharacteristics()
573
570
  } else {
574
- await this.statusCode(statusCode)
575
571
  await this.statusCode(deviceStatus.statusCode)
576
572
  }
577
573
  } catch (e: any) {
package/src/device/hub.ts CHANGED
@@ -3,13 +3,10 @@
3
3
  * hub.ts: @switchbot/homebridge-switchbot.
4
4
  */
5
5
  import type { CharacteristicValue, PlatformAccessory, Service } from 'homebridge'
6
+ import type { device, hub2ServiceData, hub2Status, hub2WebhookContext } from 'node-switchbot'
6
7
 
7
8
  import type { SwitchBotPlatform } from '../platform.js'
8
- import type { devicesConfig } from '../settings.js'
9
- import type { hub2ServiceData } from '../types/bledevicestatus.js'
10
- import type { device } from '../types/devicelist.js'
11
- import type { hub2Status } from '../types/devicestatus.js'
12
- import type { hub2WebhookContext } from '../types/devicewebhookstatus.js'
9
+ import type { devicesConfig, hubConfig } from '../settings.js'
13
10
 
14
11
  import { Units } from 'homebridge'
15
12
  /*
@@ -69,7 +66,7 @@ export class Hub extends deviceBase {
69
66
  this.hubUpdateInProgress = false
70
67
 
71
68
  // Initialize Temperature Sensor Service
72
- if (device.hub?.hide_temperature) {
69
+ if ((device as hubConfig).hide_temperature) {
73
70
  if (this.TemperatureSensor) {
74
71
  this.debugLog('Removing Temperature Sensor Service')
75
72
  this.TemperatureSensor.Service = this.accessory.getService(this.hap.Service.TemperatureSensor) as Service
@@ -97,7 +94,7 @@ export class Hub extends deviceBase {
97
94
  }
98
95
 
99
96
  // Initialize Humidity Sensor Service
100
- if (device.hub?.hide_humidity) {
97
+ if ((device as hubConfig).hide_humidity) {
101
98
  if (this.HumiditySensor) {
102
99
  this.debugLog('Removing Humidity Sensor Service')
103
100
  this.HumiditySensor.Service = this.accessory.getService(this.hap.Service.HumiditySensor) as Service
@@ -121,7 +118,7 @@ export class Hub extends deviceBase {
121
118
  }
122
119
 
123
120
  // Initialize Light Sensor Service
124
- if (device.hub?.hide_lightsensor) {
121
+ if ((device as hubConfig).hide_lightsensor) {
125
122
  if (this.LightSensor) {
126
123
  this.debugLog('Removing Light Sensor Service')
127
124
  this.LightSensor.Service = this.accessory.getService(this.hap.Service.LightSensor) as Service
@@ -149,7 +146,7 @@ export class Hub 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 Hub 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 Hub 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,21 +178,21 @@ export class Hub extends deviceBase {
181
178
  await this.debugLog(`(temperature, humidity, lightLevel) = BLE:(${this.serviceData.celsius}, ${this.serviceData.humidity}, ${this.serviceData.lightLevel}), current:(${this.TemperatureSensor?.CurrentTemperature}, ${this.HumiditySensor?.CurrentRelativeHumidity}, ${this.LightSensor?.CurrentAmbientLightLevel})`)
182
179
 
183
180
  // CurrentTemperature
184
- if (!this.device.hub?.hide_temperature && this.TemperatureSensor?.Service) {
181
+ if (!(this.device as hubConfig).hide_temperature && this.TemperatureSensor?.Service) {
185
182
  this.TemperatureSensor.CurrentTemperature = this.serviceData.celsius
186
183
  await this.debugLog(`CurrentTemperature: ${this.TemperatureSensor.CurrentTemperature}°c`)
187
184
  }
188
185
 
189
186
  // CurrentRelativeHumidity
190
- if (!this.device.hub?.hide_humidity && this.HumiditySensor?.Service) {
187
+ if (!(this.device as hubConfig).hide_humidity && this.HumiditySensor?.Service) {
191
188
  this.HumiditySensor!.CurrentRelativeHumidity = validHumidity(this.serviceData.humidity, 0, 100)
192
189
  await this.debugLog(`CurrentRelativeHumidity: ${this.HumiditySensor.CurrentRelativeHumidity}%`)
193
190
  }
194
191
 
195
192
  // CurrentAmbientLightLevel
196
- if (!this.device.hub?.hide_lightsensor && this.LightSensor?.Service) {
197
- const set_minLux = this.device.blindTilt?.set_minLux ?? 1
198
- const set_maxLux = this.device.blindTilt?.set_maxLux ?? 6001
193
+ if (!(this.device as hubConfig).hide_lightsensor && this.LightSensor?.Service) {
194
+ const set_minLux = (this.device as hubConfig).set_minLux ?? 1
195
+ const set_maxLux = (this.device as hubConfig).set_maxLux ?? 6001
199
196
  const lightLevel = this.serviceData.lightLevel
200
197
  this.LightSensor.CurrentAmbientLightLevel = await this.getLightLevel(lightLevel, set_minLux, set_maxLux, 19)
201
198
  await this.debugLog(`LightLevel: ${this.serviceData.lightLevel}, CurrentAmbientLightLevel: ${this.LightSensor.CurrentAmbientLightLevel}`)
@@ -207,21 +204,21 @@ export class Hub extends deviceBase {
207
204
  await this.debugLog(`(temperature, humidity, lightLevel) = OpenAPI:(${this.deviceStatus.temperature}, ${this.deviceStatus.humidity}, ${this.deviceStatus.lightLevel}), current:(${this.TemperatureSensor?.CurrentTemperature}, ${this.HumiditySensor?.CurrentRelativeHumidity}, ${this.LightSensor?.CurrentAmbientLightLevel})`)
208
205
 
209
206
  // CurrentRelativeHumidity
210
- if (!this.device.hub?.hide_humidity && this.HumiditySensor?.Service) {
207
+ if (!(this.device as hubConfig).hide_humidity && this.HumiditySensor?.Service) {
211
208
  this.HumiditySensor.CurrentRelativeHumidity = this.deviceStatus.humidity
212
209
  await this.debugLog(`CurrentRelativeHumidity: ${this.HumiditySensor.CurrentRelativeHumidity}%`)
213
210
  }
214
211
 
215
212
  // CurrentTemperature
216
- if (!this.device.hub?.hide_temperature && this.TemperatureSensor?.Service) {
213
+ if (!(this.device as hubConfig).hide_temperature && this.TemperatureSensor?.Service) {
217
214
  this.TemperatureSensor.CurrentTemperature = this.deviceStatus.temperature
218
215
  await this.debugLog(`CurrentTemperature: ${this.TemperatureSensor.CurrentTemperature}°c`)
219
216
  }
220
217
 
221
218
  // LightSensor
222
- if (!this.device.hub?.hide_lightsensor && this.LightSensor?.Service) {
223
- const set_minLux = this.device.blindTilt?.set_minLux ?? 1
224
- const set_maxLux = this.device.blindTilt?.set_maxLux ?? 6001
219
+ if (!(this.device as hubConfig).hide_lightsensor && this.LightSensor?.Service) {
220
+ const set_minLux = (this.device as hubConfig).set_minLux ?? 1
221
+ const set_maxLux = (this.device as hubConfig).set_maxLux ?? 6001
225
222
  const lightLevel = this.deviceStatus.lightLevel
226
223
  this.LightSensor.CurrentAmbientLightLevel = await this.getLightLevel(lightLevel, set_minLux, set_maxLux, 19)
227
224
  await this.debugLog(`LightLevel: ${this.deviceStatus.lightLevel}, CurrentAmbientLightLevel: ${this.LightSensor!.CurrentAmbientLightLevel}`)
@@ -245,28 +242,28 @@ export class Hub extends deviceBase {
245
242
 
246
243
  async parseStatusWebhook(): Promise<void> {
247
244
  await this.debugLog('parseStatusWebhook')
248
- await this.debugLog(`(scale, temperature, humidity, lightLevel) = Webhook:(${this.webhookContext.scale}, ${convertUnits(this.webhookContext.temperature, this.webhookContext.scale, this.device.hub?.convertUnitTo)}, ${this.webhookContext.humidity}, ${this.webhookContext.lightLevel}), current:(${this.TemperatureSensor?.CurrentTemperature}, ${this.HumiditySensor?.CurrentRelativeHumidity}, ${this.LightSensor?.CurrentAmbientLightLevel})`)
245
+ await this.debugLog(`(scale, temperature, humidity, lightLevel) = Webhook:(${this.webhookContext.scale}, ${convertUnits(this.webhookContext.temperature, this.webhookContext.scale, (this.device as hubConfig).convertUnitTo)}, ${this.webhookContext.humidity}, ${this.webhookContext.lightLevel}), current:(${this.TemperatureSensor?.CurrentTemperature}, ${this.HumiditySensor?.CurrentRelativeHumidity}, ${this.LightSensor?.CurrentAmbientLightLevel})`)
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 hubConfig).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.hub?.hide_humidity && this.HumiditySensor?.Service) {
252
+ if (!(this.device as hubConfig).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.hub?.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 hubConfig).hide_temperature && this.TemperatureSensor?.Service) {
259
+ this.TemperatureSensor.CurrentTemperature = convertUnits(this.webhookContext.temperature, this.webhookContext.scale, (this.device as hubConfig).convertUnitTo)
263
260
  await this.debugLog(`CurrentTemperature: ${this.TemperatureSensor.CurrentTemperature}`)
264
261
  }
265
262
 
266
263
  // CurrentAmbientLightLevel
267
- if (!this.device.hub?.hide_lightsensor && this.LightSensor?.Service) {
268
- const set_minLux = this.device.blindTilt?.set_minLux ?? 1
269
- const set_maxLux = this.device.blindTilt?.set_maxLux ?? 6001
264
+ if (!(this.device as hubConfig).hide_lightsensor && this.LightSensor?.Service) {
265
+ const set_minLux = (this.device as hubConfig).set_minLux ?? 1
266
+ const set_maxLux = (this.device as hubConfig).set_maxLux ?? 6001
270
267
  this.LightSensor.CurrentAmbientLightLevel = await this.getLightLevel(this.webhookContext.lightLevel, set_minLux, set_maxLux, 19)
271
268
  await this.debugLog(`CurrentAmbientLightLevel: ${this.LightSensor.CurrentAmbientLightLevel}`)
272
269
  }
@@ -290,22 +287,22 @@ export class Hub extends deviceBase {
290
287
 
291
288
  async BLERefreshStatus(): Promise<void> {
292
289
  await this.debugLog('BLERefreshStatus')
293
- const switchbot = await this.switchbotBLE()
294
- if (switchbot === undefined) {
295
- await this.BLERefreshConnection(switchbot)
290
+ const switchBotBLE = await this.switchbotBLE()
291
+ if (switchBotBLE === undefined) {
292
+ await this.BLERefreshConnection(switchBotBLE)
296
293
  } else {
297
294
  // Start to monitor advertisement packets
298
295
  (async () => {
299
296
  // Start to monitor advertisement packets
300
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as hub2ServiceData
297
+ const serviceData = await this.monitorAdvertisementPackets(switchBotBLE) as hub2ServiceData
301
298
  // Update HomeKit
302
299
  if (serviceData.model === SwitchBotBLEModel.Hub2 && serviceData.modelName === SwitchBotBLEModelName.Hub2) {
303
300
  this.serviceData = serviceData
304
301
  await this.BLEparseStatus()
305
302
  await this.updateHomeKitCharacteristics()
306
303
  } else {
307
- await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`)
308
- await this.BLERefreshConnection(switchbot)
304
+ await this.errorLog(`failed to get serviceData, serviceData: ${JSON.stringify(serviceData)}`)
305
+ await this.BLERefreshConnection(switchBotBLE)
309
306
  }
310
307
  })()
311
308
  }
@@ -326,7 +323,7 @@ export class Hub extends deviceBase {
326
323
  await this.BLEparseStatus()
327
324
  await this.updateHomeKitCharacteristics()
328
325
  } catch (e: any) {
329
- await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`)
326
+ await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e.message ?? e}`)
330
327
  }
331
328
  }
332
329
  } catch (error) {
@@ -340,17 +337,17 @@ export class Hub extends deviceBase {
340
337
  async openAPIRefreshStatus(): Promise<void> {
341
338
  await this.debugLog('openAPIRefreshStatus')
342
339
  try {
343
- const { body, statusCode } = await this.deviceRefreshStatus()
344
- const deviceStatus: any = await body.json()
345
- await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
346
- if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
347
- await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
340
+ const { body } = await this.deviceRefreshStatus()
341
+ const deviceStatus: any = await body
342
+ await this.debugLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
343
+ if (await this.successfulStatusCodes(deviceStatus)) {
344
+ await this.debugSuccessLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
348
345
  this.deviceStatus = deviceStatus.body
349
346
  await this.openAPIparseStatus()
350
347
  await this.updateHomeKitCharacteristics()
351
348
  } else {
352
- await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
353
- await this.debugWarnLog(statusCode, deviceStatus)
349
+ await this.debugWarnLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
350
+ await this.debugWarnLog(deviceStatus)
354
351
  }
355
352
  } catch (e: any) {
356
353
  await this.apiError(e)
@@ -368,7 +365,7 @@ export class Hub extends deviceBase {
368
365
  await this.parseStatusWebhook()
369
366
  await this.updateHomeKitCharacteristics()
370
367
  } catch (e: any) {
371
- await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`)
368
+ await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e.message ?? e}`)
372
369
  }
373
370
  }
374
371
  } else {
@@ -382,15 +379,15 @@ export class Hub extends deviceBase {
382
379
 
383
380
  async updateHomeKitCharacteristics(): Promise<void> {
384
381
  // CurrentRelativeHumidity
385
- if (!this.device.hub?.hide_humidity && this.HumiditySensor?.Service) {
382
+ if (!(this.device as hubConfig).hide_humidity && this.HumiditySensor?.Service) {
386
383
  await this.updateCharacteristic(this.HumiditySensor.Service, this.hap.Characteristic.CurrentRelativeHumidity, this.HumiditySensor.CurrentRelativeHumidity, 'CurrentRelativeHumidity')
387
384
  }
388
385
  // CurrentTemperature
389
- if (!this.device.hub?.hide_temperature && this.TemperatureSensor?.Service) {
386
+ if (!(this.device as hubConfig).hide_temperature && this.TemperatureSensor?.Service) {
390
387
  await this.updateCharacteristic(this.TemperatureSensor.Service, this.hap.Characteristic.CurrentTemperature, this.TemperatureSensor.CurrentTemperature, 'CurrentTemperature')
391
388
  }
392
389
  // CurrentAmbientLightLevel
393
- if (!this.device.hub?.hide_lightsensor && this.LightSensor?.Service) {
390
+ if (!(this.device as hubConfig).hide_lightsensor && this.LightSensor?.Service) {
394
391
  await this.updateCharacteristic(this.LightSensor.Service, this.hap.Characteristic.CurrentAmbientLightLevel, this.LightSensor.CurrentAmbientLightLevel, 'CurrentAmbientLightLevel')
395
392
  }
396
393
  }
@@ -405,26 +402,26 @@ export class Hub extends deviceBase {
405
402
 
406
403
  async offlineOff(): Promise<void> {
407
404
  if (this.device.offline) {
408
- if (!this.device.hub?.hide_temperature && this.TemperatureSensor?.Service) {
405
+ if (!(this.device as hubConfig).hide_temperature && this.TemperatureSensor?.Service) {
409
406
  this.TemperatureSensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, this.accessory.context.CurrentTemperature)
410
407
  }
411
- if (!this.device.hub?.hide_humidity && this.HumiditySensor?.Service) {
408
+ if (!(this.device as hubConfig).hide_humidity && this.HumiditySensor?.Service) {
412
409
  this.HumiditySensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity, this.accessory.context.CurrentRelativeHumidity)
413
410
  }
414
- if (!this.device.hub?.hide_lightsensor && this.LightSensor?.Service) {
411
+ if (!(this.device as hubConfig).hide_lightsensor && this.LightSensor?.Service) {
415
412
  this.LightSensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel, this.accessory.context.CurrentAmbientLightLevel)
416
413
  }
417
414
  }
418
415
  }
419
416
 
420
417
  async apiError(e: any): Promise<void> {
421
- if (!this.device.hub?.hide_temperature && this.TemperatureSensor?.Service) {
418
+ if (!(this.device as hubConfig).hide_temperature && this.TemperatureSensor?.Service) {
422
419
  this.TemperatureSensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, e)
423
420
  }
424
- if (!this.device.hub?.hide_humidity && this.HumiditySensor?.Service) {
421
+ if (!(this.device as hubConfig).hide_humidity && this.HumiditySensor?.Service) {
425
422
  this.HumiditySensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity, e)
426
423
  }
427
- if (!this.device.hub?.hide_lightsensor && this.LightSensor?.Service) {
424
+ if (!(this.device as hubConfig).hide_lightsensor && this.LightSensor?.Service) {
428
425
  this.LightSensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel, e)
429
426
  }
430
427
  }