@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
  * lightstrip.ts: @switchbot/homebridge-switchbot.
4
4
  */
5
5
  import type { CharacteristicValue, Controller, ControllerConstructor, ControllerServiceMap, PlatformAccessory, Service } from 'homebridge'
6
+ import type { bodyChange, device, stripLightServiceData, stripLightStatus, stripLightWebhookContext, SwitchbotDevice, WoStrip } from 'node-switchbot'
6
7
 
7
8
  import type { SwitchBotPlatform } from '../platform.js'
8
- import type { devicesConfig } from '../settings.js'
9
- import type { stripLightServiceData } from '../types/bledevicestatus.js'
10
- import type { device } from '../types/devicelist.js'
11
- import type { stripLightStatus } from '../types/devicestatus.js'
12
- import type { stripLightWebhookContext } from '../types/devicewebhookstatus.js'
9
+ import type { devicesConfig, stripLightConfig } from '../settings.js'
13
10
 
14
11
  /*
15
12
  * For Testing Locally:
@@ -111,7 +108,7 @@ export class StripLight extends deviceBase {
111
108
 
112
109
  // Initialize LightBulb Brightness Characteristic
113
110
  this.LightBulb.Service.getCharacteristic(this.hap.Characteristic.Brightness).setProps({
114
- minStep: device.striplight?.set_minStep ?? 1,
111
+ minStep: (device as stripLightConfig).set_minStep ?? 1,
115
112
  minValue: 0,
116
113
  maxValue: 100,
117
114
  validValueRanges: [0, 100],
@@ -151,7 +148,7 @@ export class StripLight extends deviceBase {
151
148
  this.debugLog('Retrieve initial values and update Homekit')
152
149
  this.refreshStatus()
153
150
  } catch (e: any) {
154
- this.errorLog(`failed to retrieve initial values and update Homekit, Error: ${e}`)
151
+ this.errorLog(`failed to retrieve initial values and update Homekit, Error: ${e.message ?? e}`)
155
152
  }
156
153
 
157
154
  // regisiter webhook event handler if enabled
@@ -159,7 +156,7 @@ export class StripLight extends deviceBase {
159
156
  this.debugLog('Registering Webhook Event Handler')
160
157
  this.registerWebhook()
161
158
  } catch (e: any) {
162
- this.errorLog(`failed to registerWebhook, Error: ${e}`)
159
+ this.errorLog(`failed to registerWebhook, Error: ${e.message ?? e}`)
163
160
  }
164
161
 
165
162
  // regisiter platform BLE event handler if enabled
@@ -167,7 +164,7 @@ export class StripLight extends deviceBase {
167
164
  this.debugLog('Registering Platform BLE Event Handler')
168
165
  this.registerPlatformBLE()
169
166
  } catch (e: any) {
170
- this.errorLog(`failed to registerPlatformBLE, Error: ${e}`)
167
+ this.errorLog(`failed to registerPlatformBLE, Error: ${e.message ?? e}`)
171
168
  }
172
169
 
173
170
  // Start an update interval
@@ -312,22 +309,22 @@ export class StripLight extends deviceBase {
312
309
 
313
310
  async BLERefreshStatus(): Promise<void> {
314
311
  await this.debugLog('BLERefreshStatus')
315
- const switchbot = await this.switchbotBLE()
316
- if (switchbot === undefined) {
317
- await this.BLERefreshConnection(switchbot)
312
+ const switchBotBLE = await this.switchbotBLE()
313
+ if (switchBotBLE === undefined) {
314
+ await this.BLERefreshConnection(switchBotBLE)
318
315
  } else {
319
316
  // Start to monitor advertisement packets
320
317
  (async () => {
321
318
  // Start to monitor advertisement packets
322
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as stripLightServiceData
319
+ const serviceData = await this.monitorAdvertisementPackets(switchBotBLE) as stripLightServiceData
323
320
  // Update HomeKit
324
321
  if (serviceData.model === SwitchBotBLEModel.StripLight && serviceData.modelName === SwitchBotBLEModelName.StripLight) {
325
322
  this.serviceData = serviceData
326
323
  await this.BLEparseStatus()
327
324
  await this.updateHomeKitCharacteristics()
328
325
  } else {
329
- await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`)
330
- await this.BLERefreshConnection(switchbot)
326
+ await this.errorLog(`failed to get serviceData, serviceData: ${JSON.stringify(serviceData)}`)
327
+ await this.BLERefreshConnection(switchBotBLE)
331
328
  }
332
329
  })()
333
330
  }
@@ -348,7 +345,7 @@ export class StripLight extends deviceBase {
348
345
  await this.BLEparseStatus()
349
346
  await this.updateHomeKitCharacteristics()
350
347
  } catch (e: any) {
351
- await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`)
348
+ await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e.message ?? e}`)
352
349
  }
353
350
  }
354
351
  } catch (error) {
@@ -362,17 +359,17 @@ export class StripLight extends deviceBase {
362
359
  async openAPIRefreshStatus(): Promise<void> {
363
360
  await this.debugLog('openAPIRefreshStatus')
364
361
  try {
365
- const { body, statusCode } = await this.deviceRefreshStatus()
366
- const deviceStatus: any = await body.json()
367
- await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
368
- if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
369
- await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
362
+ const { body } = await this.deviceRefreshStatus()
363
+ const deviceStatus: any = await body
364
+ await this.debugLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
365
+ if (await this.successfulStatusCodes(deviceStatus)) {
366
+ await this.debugSuccessLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
370
367
  this.deviceStatus = deviceStatus.body
371
368
  await this.openAPIparseStatus()
372
369
  await this.updateHomeKitCharacteristics()
373
370
  } else {
374
- await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
375
- await this.debugWarnLog(statusCode, deviceStatus)
371
+ await this.debugWarnLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
372
+ await this.debugWarnLog(deviceStatus)
376
373
  }
377
374
  } catch (e: any) {
378
375
  await this.apiError(e)
@@ -390,7 +387,7 @@ export class StripLight extends deviceBase {
390
387
  await this.parseStatusWebhook()
391
388
  await this.updateHomeKitCharacteristics()
392
389
  } catch (e: any) {
393
- await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`)
390
+ await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e.message ?? e}`)
394
391
  }
395
392
  }
396
393
  } else {
@@ -459,23 +456,24 @@ export class StripLight extends deviceBase {
459
456
  await this.debugLog('BLEpushChanges')
460
457
  if (this.LightBulb.On !== this.accessory.context.On) {
461
458
  await this.debugLog(`BLEpushChanges On: ${this.LightBulb.On}, OnCached: ${this.accessory.context.On}`)
462
- const switchbot = await this.platform.connectBLE(this.accessory, this.device)
459
+ const switchBotBLE = await this.platform.connectBLE(this.accessory, this.device)
463
460
  try {
464
461
  const formattedDeviceId = formatDeviceIdAsMac(this.device.deviceId)
465
462
  this.device.bleMac = formattedDeviceId
466
463
  await this.debugLog(`bleMac: ${this.device.bleMac}`)
467
- if (switchbot !== false) {
468
- switchbot
464
+ if (switchBotBLE !== false) {
465
+ switchBotBLE
469
466
  .discover({ model: this.device.bleModel, id: this.device.bleMac })
470
- .then(async (device_list: any) => {
467
+ .then(async (device_list: SwitchbotDevice[]) => {
468
+ const deviceList = device_list as unknown as WoStrip[]
471
469
  await this.infoLog(`On: ${this.LightBulb.On}`)
472
470
  return await this.retryBLE({
473
471
  max: await this.maxRetryBLE(),
474
472
  fn: async () => {
475
473
  if (this.LightBulb.On) {
476
- return await device_list[0].turnOn({ id: this.device.bleMac })
474
+ return await deviceList[0].turnOn()
477
475
  } else {
478
- return await device_list[0].turnOff({ id: this.device.bleMac })
476
+ return await deviceList[0].turnOff()
479
477
  }
480
478
  },
481
479
  })
@@ -490,7 +488,7 @@ export class StripLight extends deviceBase {
490
488
  await this.BLEPushConnection()
491
489
  })
492
490
  } else {
493
- await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`)
491
+ await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${JSON.stringify(switchBotBLE)}`)
494
492
  await this.BLEPushConnection()
495
493
  }
496
494
  } catch (error) {
@@ -504,15 +502,15 @@ export class StripLight extends deviceBase {
504
502
  async BLEpushBrightnessChanges(): Promise<void> {
505
503
  await this.debugLog('BLEpushBrightnessChanges')
506
504
  if (this.LightBulb.Brightness !== this.accessory.context.Brightness) {
507
- const switchbot = await this.platform.connectBLE(this.accessory, this.device)
505
+ const switchBotBLE = await this.platform.connectBLE(this.accessory, this.device)
508
506
  try {
509
507
  const formattedDeviceId = formatDeviceIdAsMac(this.device.deviceId)
510
508
  this.device.bleMac = formattedDeviceId
511
509
  await this.debugLog(`bleMac: ${this.device.bleMac}`)
512
- if (switchbot !== false) {
513
- switchbot
510
+ if (switchBotBLE !== false) {
511
+ switchBotBLE
514
512
  .discover({ model: this.device.bleModel, id: this.device.bleMac })
515
- .then(async (device_list: any) => {
513
+ .then(async (device_list: SwitchbotDevice[]) => {
516
514
  await this.infoLog(`Brightness: ${this.LightBulb.Brightness}`)
517
515
  return await device_list[0].setBrightness(this.LightBulb.Brightness)
518
516
  })
@@ -526,7 +524,7 @@ export class StripLight extends deviceBase {
526
524
  await this.BLEPushConnection()
527
525
  })
528
526
  } else {
529
- await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`)
527
+ await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${JSON.stringify(switchBotBLE)}`)
530
528
  await this.BLEPushConnection()
531
529
  }
532
530
  } catch (error) {
@@ -543,15 +541,15 @@ export class StripLight extends deviceBase {
543
541
  await this.debugLog(`Hue: ${JSON.stringify(this.LightBulb.Hue)}, Saturation: ${JSON.stringify(this.LightBulb.Saturation)}`)
544
542
  const [red, green, blue] = hs2rgb(this.LightBulb.Hue, this.LightBulb.Saturation)
545
543
  await this.debugLog(`rgb: ${JSON.stringify([red, green, blue])}`)
546
- const switchbot = await this.platform.connectBLE(this.accessory, this.device)
544
+ const switchBotBLE = await this.platform.connectBLE(this.accessory, this.device)
547
545
  try {
548
546
  const formattedDeviceId = formatDeviceIdAsMac(this.device.deviceId)
549
547
  this.device.bleMac = formattedDeviceId
550
548
  await this.debugLog(`bleMac: ${this.device.bleMac}`)
551
- if (switchbot !== false) {
552
- switchbot
549
+ if (switchBotBLE !== false) {
550
+ switchBotBLE
553
551
  .discover({ model: this.device.bleModel, id: this.device.bleMac })
554
- .then(async (device_list: any) => {
552
+ .then(async (device_list: SwitchbotDevice[]) => {
555
553
  await this.infoLog(`RGB: ${(this.LightBulb.Brightness, red, green, blue)}`)
556
554
  return await device_list[0].setRGB(this.LightBulb.Brightness, red, green, blue)
557
555
  })
@@ -565,7 +563,7 @@ export class StripLight extends deviceBase {
565
563
  await this.BLEPushConnection()
566
564
  })
567
565
  } else {
568
- await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`)
566
+ await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${JSON.stringify(switchBotBLE)}`)
569
567
  await this.BLEPushConnection()
570
568
  }
571
569
  } catch (error) {
@@ -580,21 +578,20 @@ export class StripLight extends deviceBase {
580
578
  await this.debugLog('openAPIpushChanges')
581
579
  if (this.LightBulb.On !== this.accessory.context.On) {
582
580
  const command = this.LightBulb.On ? 'turnOn' : 'turnOff'
583
- const bodyChange = JSON.stringify({
581
+ const bodyChange: bodyChange = {
584
582
  command: `${command}`,
585
583
  parameter: 'default',
586
584
  commandType: 'command',
587
- })
585
+ }
588
586
  await this.debugLog(`SwitchBot OpenAPI bodyChange: ${JSON.stringify(bodyChange)}`)
589
587
  try {
590
- const { body, statusCode } = await this.pushChangeRequest(bodyChange)
591
- const deviceStatus: any = await body.json()
592
- await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
593
- if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
594
- await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
588
+ const { body } = await this.pushChangeRequest(bodyChange)
589
+ const deviceStatus: any = await body
590
+ await this.debugLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
591
+ if (await this.successfulStatusCodes(deviceStatus)) {
592
+ await this.debugSuccessLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
595
593
  await this.updateHomeKitCharacteristics()
596
594
  } else {
597
- await this.statusCode(statusCode)
598
595
  await this.statusCode(deviceStatus.statusCode)
599
596
  }
600
597
  } catch (e: any) {
@@ -612,21 +609,20 @@ export class StripLight extends deviceBase {
612
609
  await this.debugLog(`Hue: ${JSON.stringify(this.LightBulb.Hue)}, Saturation: ${JSON.stringify(this.LightBulb.Saturation)}`)
613
610
  const [red, green, blue] = hs2rgb(this.LightBulb.Hue, this.LightBulb.Saturation)
614
611
  await this.debugLog(`rgb: ${JSON.stringify([red, green, blue])}`)
615
- const bodyChange = JSON.stringify({
612
+ const bodyChange: bodyChange = {
616
613
  command: 'setColor',
617
614
  parameter: `${red}:${green}:${blue}`,
618
615
  commandType: 'command',
619
- })
616
+ }
620
617
  await this.debugLog(`SwitchBot OpenAPI bodyChange: ${JSON.stringify(bodyChange)}`)
621
618
  try {
622
- const { body, statusCode } = await this.pushChangeRequest(bodyChange)
623
- const deviceStatus: any = await body.json()
624
- await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
625
- if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
626
- await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
619
+ const { body } = await this.pushChangeRequest(bodyChange)
620
+ const deviceStatus: any = await body
621
+ await this.debugLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
622
+ if (await this.successfulStatusCodes(deviceStatus)) {
623
+ await this.debugSuccessLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
627
624
  await this.updateHomeKitCharacteristics()
628
625
  } else {
629
- await this.statusCode(statusCode)
630
626
  await this.statusCode(deviceStatus.statusCode)
631
627
  }
632
628
  } catch (e: any) {
@@ -641,21 +637,20 @@ export class StripLight extends deviceBase {
641
637
  async pushBrightnessChanges(): Promise<void> {
642
638
  await this.debugLog('pushBrightnessChanges')
643
639
  if (this.LightBulb.Brightness !== this.accessory.context.Brightness) {
644
- const bodyChange = JSON.stringify({
640
+ const bodyChange: bodyChange = {
645
641
  command: 'setBrightness',
646
642
  parameter: `${this.LightBulb.Brightness}`,
647
643
  commandType: 'command',
648
- })
644
+ }
649
645
  await this.debugLog(`SwitchBot OpenAPI bodyChange: ${JSON.stringify(bodyChange)}`)
650
646
  try {
651
- const { body, statusCode } = await this.pushChangeRequest(bodyChange)
652
- const deviceStatus: any = await body.json()
653
- await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
654
- if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
655
- await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
647
+ const { body } = await this.pushChangeRequest(bodyChange)
648
+ const deviceStatus: any = await body
649
+ await this.debugLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
650
+ if (await this.successfulStatusCodes(deviceStatus)) {
651
+ await this.debugSuccessLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
656
652
  await this.updateHomeKitCharacteristics()
657
653
  } else {
658
- await this.statusCode(statusCode)
659
654
  await this.statusCode(deviceStatus.statusCode)
660
655
  }
661
656
  } catch (e: any) {
@@ -791,13 +786,8 @@ export class StripLight extends deviceBase {
791
786
  this.adaptiveLighting = accessory.context.adaptiveLighting ?? true
792
787
  await this.debugLog(`adaptiveLighting: ${this.adaptiveLighting}`)
793
788
  // Adaptive Lighting Shift
794
- if (device.striplight?.adaptiveLightingShift) {
795
- this.adaptiveLightingShift = device.striplight.adaptiveLightingShift
796
- this.debugLog(`adaptiveLightingShift: ${this.adaptiveLightingShift}`)
797
- } else {
798
- this.adaptiveLightingShift = 0
799
- this.debugLog(`adaptiveLightingShift: ${this.adaptiveLightingShift}`)
800
- }
789
+ this.adaptiveLightingShift = (device as stripLightConfig).adaptiveLightingShift ?? 0
790
+ this.debugLog(`adaptiveLightingShift: ${this.adaptiveLightingShift}`)
801
791
  }
802
792
 
803
793
  async BLEPushConnection() {
@@ -3,13 +3,10 @@
3
3
  * lock.ts: @switchbot/homebridge-switchbot.
4
4
  */
5
5
  import type { CharacteristicValue, PlatformAccessory, Service } from 'homebridge'
6
+ import type { bodyChange, device, lockProServiceData, lockProStatus, lockProWebhookContext, lockServiceData, lockStatus, lockWebhookContext, SwitchbotDevice } from 'node-switchbot'
6
7
 
7
8
  import type { SwitchBotPlatform } from '../platform.js'
8
- import type { devicesConfig } from '../settings.js'
9
- import type { lockProServiceData, lockServiceData } from '../types/bledevicestatus.js'
10
- import type { device } from '../types/devicelist.js'
11
- import type { lockProStatus, lockStatus } from '../types/devicestatus.js'
12
- import type { lockProWebhookContext, lockWebhookContext } from '../types/devicewebhookstatus.js'
9
+ import type { devicesConfig, lockConfig } from '../settings.js'
13
10
 
14
11
  /*
15
12
  * For Testing Locally:
@@ -110,7 +107,7 @@ export class Lock extends deviceBase {
110
107
  })
111
108
 
112
109
  // Contact Sensor Service
113
- if (device.lock?.hide_contactsensor) {
110
+ if ((device as lockConfig).hide_contactsensor) {
114
111
  if (this.ContactSensor) {
115
112
  this.debugLog('Removing Contact Sensor Service')
116
113
  this.ContactSensor.Service = this.accessory.getService(this.hap.Service.ContactSensor) as Service
@@ -132,7 +129,7 @@ export class Lock extends deviceBase {
132
129
  }
133
130
 
134
131
  // Initialize Latch Button Service
135
- if (device.lock?.activate_latchbutton === false) {
132
+ if ((device as lockConfig).activate_latchbutton === false) {
136
133
  if (this.Switch) {
137
134
  this.debugLog('Removing Latch Button Service')
138
135
  this.Switch.Service = accessory.getService(this.hap.Service.Switch) as Service
@@ -158,7 +155,7 @@ export class Lock extends deviceBase {
158
155
  this.debugLog('Retrieve initial values and update Homekit')
159
156
  this.refreshStatus()
160
157
  } catch (e: any) {
161
- this.errorLog(`failed to retrieve initial values and update Homekit, Error: ${e}`)
158
+ this.errorLog(`failed to retrieve initial values and update Homekit, Error: ${e.message ?? e}`)
162
159
  }
163
160
 
164
161
  // regisiter webhook event handler if enabled
@@ -166,7 +163,7 @@ export class Lock extends deviceBase {
166
163
  this.debugLog('Registering Webhook Event Handler')
167
164
  this.registerWebhook()
168
165
  } catch (e: any) {
169
- this.errorLog(`failed to registerWebhook, Error: ${e}`)
166
+ this.errorLog(`failed to registerWebhook, Error: ${e.message ?? e}`)
170
167
  }
171
168
 
172
169
  // regisiter platform BLE event handler if enabled
@@ -174,7 +171,7 @@ export class Lock extends deviceBase {
174
171
  this.debugLog('Registering Platform BLE Event Handler')
175
172
  this.registerPlatformBLE()
176
173
  } catch (e: any) {
177
- this.errorLog(`failed to registerPlatformBLE, Error: ${e}`)
174
+ this.errorLog(`failed to registerPlatformBLE, Error: ${e.message ?? e}`)
178
175
  }
179
176
 
180
177
  // Start an update interval
@@ -221,8 +218,8 @@ export class Lock extends deviceBase {
221
218
  await this.debugLog(`LockTargetState: ${this.LockMechanism.LockTargetState}`)
222
219
 
223
220
  // Contact Sensor
224
- if (!this.device.lock?.hide_contactsensor && this.ContactSensor?.Service) {
225
- this.ContactSensor.ContactSensorState = this.serviceData.door_open === 'opened'
221
+ if (!(this.device as lockConfig).hide_contactsensor && this.ContactSensor?.Service) {
222
+ this.ContactSensor.ContactSensorState = this.serviceData.door_open
226
223
  ? this.hap.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED
227
224
  : this.hap.Characteristic.ContactSensorState.CONTACT_DETECTED
228
225
  await this.debugLog(`ContactSensorState: ${this.ContactSensor.ContactSensorState}`)
@@ -256,7 +253,7 @@ export class Lock extends deviceBase {
256
253
  await this.debugLog(`LockTargetState: ${this.LockMechanism.LockTargetState}`)
257
254
 
258
255
  // ContactSensorState
259
- if (!this.device.lock?.hide_contactsensor && this.ContactSensor?.Service) {
256
+ if (!(this.device as lockConfig).hide_contactsensor && this.ContactSensor?.Service) {
260
257
  this.ContactSensor.ContactSensorState = this.deviceStatus.doorState === 'opened'
261
258
  ? this.hap.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED
262
259
  : this.hap.Characteristic.ContactSensorState.CONTACT_DETECTED
@@ -324,14 +321,14 @@ export class Lock extends deviceBase {
324
321
 
325
322
  async BLERefreshStatus(): Promise<void> {
326
323
  await this.debugLog('BLERefreshStatus')
327
- const switchbot = await this.switchbotBLE()
328
- if (switchbot === undefined) {
329
- await this.BLERefreshConnection(switchbot)
324
+ const switchBotBLE = await this.switchbotBLE()
325
+ if (switchBotBLE === undefined) {
326
+ await this.BLERefreshConnection(switchBotBLE)
330
327
  } else {
331
328
  // Start to monitor advertisement packets
332
329
  (async () => {
333
330
  // Start to monitor advertisement packets
334
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as lockServiceData | lockProServiceData
331
+ const serviceData = await this.monitorAdvertisementPackets(switchBotBLE) as lockServiceData | lockProServiceData
335
332
  // Update HomeKit
336
333
  if ((serviceData.model === SwitchBotBLEModel.Lock || SwitchBotBLEModel.LockPro)
337
334
  && (serviceData.modelName === SwitchBotBLEModelName.Lock || SwitchBotBLEModelName.LockPro)) {
@@ -339,8 +336,8 @@ export class Lock extends deviceBase {
339
336
  await this.BLEparseStatus()
340
337
  await this.updateHomeKitCharacteristics()
341
338
  } else {
342
- await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`)
343
- await this.BLERefreshConnection(switchbot)
339
+ await this.errorLog(`failed to get serviceData, serviceData: ${JSON.stringify(serviceData)}`)
340
+ await this.BLERefreshConnection(switchBotBLE)
344
341
  }
345
342
  })()
346
343
  }
@@ -361,7 +358,7 @@ export class Lock extends deviceBase {
361
358
  await this.BLEparseStatus()
362
359
  await this.updateHomeKitCharacteristics()
363
360
  } catch (e: any) {
364
- await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`)
361
+ await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e.message ?? e}`)
365
362
  }
366
363
  }
367
364
  } catch (error) {
@@ -375,17 +372,17 @@ export class Lock extends deviceBase {
375
372
  async openAPIRefreshStatus(): Promise<void> {
376
373
  await this.debugLog('openAPIRefreshStatus')
377
374
  try {
378
- const { body, statusCode } = await this.deviceRefreshStatus()
379
- const deviceStatus: any = await body.json()
380
- await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
381
- if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
382
- await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
375
+ const { body } = await this.deviceRefreshStatus()
376
+ const deviceStatus: any = await body
377
+ await this.debugLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
378
+ if (await this.successfulStatusCodes(deviceStatus)) {
379
+ await this.debugSuccessLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
383
380
  this.deviceStatus = deviceStatus.body
384
381
  await this.openAPIparseStatus()
385
382
  await this.updateHomeKitCharacteristics()
386
383
  } else {
387
- await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
388
- await this.debugWarnLog(statusCode, deviceStatus)
384
+ await this.debugWarnLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
385
+ await this.debugWarnLog(deviceStatus)
389
386
  }
390
387
  } catch (e: any) {
391
388
  await this.apiError(e)
@@ -403,7 +400,7 @@ export class Lock extends deviceBase {
403
400
  await this.parseStatusWebhook()
404
401
  await this.updateHomeKitCharacteristics()
405
402
  } catch (e: any) {
406
- await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`)
403
+ await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e.message ?? e}`)
407
404
  }
408
405
  }
409
406
  } else {
@@ -440,15 +437,15 @@ export class Lock extends deviceBase {
440
437
  async BLEpushChanges(): Promise<void> {
441
438
  await this.debugLog('BLEpushChanges')
442
439
  if (this.LockMechanism.LockTargetState !== this.accessory.context.LockTargetState) {
443
- const switchbot = await this.platform.connectBLE(this.accessory, this.device)
440
+ const switchBotBLE = await this.platform.connectBLE(this.accessory, this.device)
444
441
  try {
445
442
  const formattedDeviceId = formatDeviceIdAsMac(this.device.deviceId)
446
443
  this.device.bleMac = formattedDeviceId
447
444
  await this.debugLog(`bleMac: ${this.device.bleMac}`)
448
- if (switchbot !== false) {
449
- switchbot
445
+ if (switchBotBLE !== false) {
446
+ switchBotBLE
450
447
  .discover({ model: this.device.bleModel, id: this.device.bleMac })
451
- .then(async (device_list: any) => {
448
+ .then(async (device_list: SwitchbotDevice[]) => {
452
449
  return await this.retryBLE({
453
450
  max: await this.maxRetryBLE(),
454
451
  fn: async () => {
@@ -470,7 +467,7 @@ export class Lock extends deviceBase {
470
467
  await this.BLEPushConnection()
471
468
  })
472
469
  } else {
473
- await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`)
470
+ await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${JSON.stringify(switchBotBLE)}`)
474
471
  await this.BLEPushConnection()
475
472
  }
476
473
  } catch (error) {
@@ -486,21 +483,20 @@ export class Lock extends deviceBase {
486
483
  if ((this.LockMechanism.LockTargetState !== this.accessory.context.LockTargetState) || LatchUnlock) {
487
484
  // Determine the command based on the LockTargetState or the forceUnlock parameter
488
485
  const command = LatchUnlock ? 'unlock' : this.LockMechanism.LockTargetState ? 'lock' : 'unlock'
489
- const bodyChange = JSON.stringify({
486
+ const bodyChange: bodyChange = {
490
487
  command: `${command}`,
491
488
  parameter: 'default',
492
489
  commandType: 'command',
493
- })
490
+ }
494
491
  await this.debugLog(`SwitchBot OpenAPI bodyChange: ${JSON.stringify(bodyChange)}`)
495
492
  try {
496
- const { body, statusCode } = await this.pushChangeRequest(bodyChange)
497
- const deviceStatus: any = await body.json()
498
- await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
499
- if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
500
- await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
493
+ const { body } = await this.pushChangeRequest(bodyChange)
494
+ const deviceStatus: any = await body
495
+ await this.debugLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
496
+ if (await this.successfulStatusCodes(deviceStatus)) {
497
+ await this.debugSuccessLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
501
498
  await this.updateHomeKitCharacteristics()
502
499
  } else {
503
- await this.statusCode(statusCode)
504
500
  await this.statusCode(deviceStatus.statusCode)
505
501
  }
506
502
  } catch (e: any) {
@@ -549,7 +545,7 @@ export class Lock extends deviceBase {
549
545
  }
550
546
  }).catch(async (e: any) => {
551
547
  // Log the error if the operation failed
552
- await this.debugLog(`Error opening latch: ${e}`)
548
+ await this.debugLog(`Error opening latch: ${e.message ?? e}`)
553
549
  // Ensure we turn the switch back off even in case of an error
554
550
  if (this.Switch?.Service) {
555
551
  this.Switch.Service.getCharacteristic(this.hap.Characteristic.On).updateValue(false)
@@ -570,7 +566,7 @@ export class Lock extends deviceBase {
570
566
  // LockCurrentState
571
567
  await this.updateCharacteristic(this.LockMechanism.Service, this.hap.Characteristic.LockCurrentState, this.LockMechanism.LockCurrentState, 'LockCurrentState')
572
568
  // ContactSensorState
573
- if (!this.device.lock?.hide_contactsensor && this.ContactSensor?.Service) {
569
+ if (!(this.device as lockConfig).hide_contactsensor && this.ContactSensor?.Service) {
574
570
  await this.updateCharacteristic(this.ContactSensor.Service, this.hap.Characteristic.ContactSensorState, this.ContactSensor.ContactSensorState, 'ContactSensorState')
575
571
  }
576
572
  // BatteryLevel
@@ -598,7 +594,7 @@ export class Lock extends deviceBase {
598
594
  if (this.device.offline) {
599
595
  this.LockMechanism.Service.updateCharacteristic(this.hap.Characteristic.LockTargetState, this.hap.Characteristic.LockTargetState.SECURED)
600
596
  this.LockMechanism.Service.updateCharacteristic(this.hap.Characteristic.LockCurrentState, this.hap.Characteristic.LockCurrentState.SECURED)
601
- if (!this.device.lock?.hide_contactsensor && this.ContactSensor?.Service) {
597
+ if (!(this.device as lockConfig).hide_contactsensor && this.ContactSensor?.Service) {
602
598
  this.ContactSensor.Service.updateCharacteristic(this.hap.Characteristic.ContactSensorState, this.hap.Characteristic.ContactSensorState.CONTACT_DETECTED)
603
599
  }
604
600
  }
@@ -607,7 +603,7 @@ export class Lock extends deviceBase {
607
603
  async apiError(e: any): Promise<void> {
608
604
  this.LockMechanism.Service.updateCharacteristic(this.hap.Characteristic.LockTargetState, e)
609
605
  this.LockMechanism.Service.updateCharacteristic(this.hap.Characteristic.LockCurrentState, e)
610
- if (!this.device.lock?.hide_contactsensor && this.ContactSensor?.Service) {
606
+ if (!(this.device as lockConfig).hide_contactsensor && this.ContactSensor?.Service) {
611
607
  this.ContactSensor.Service.updateCharacteristic(this.hap.Characteristic.ContactSensorState, e)
612
608
  }
613
609
  this.Battery.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, e)