@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/bot.ts CHANGED
@@ -3,23 +3,21 @@
3
3
  * bot.ts: @switchbot/homebridge-switchbot.
4
4
  */
5
5
  import type { CharacteristicValue, PlatformAccessory, Service } from 'homebridge'
6
+ import type { bodyChange, botServiceData, botStatus, botWebhookContext, device, SwitchbotDevice, WoHand } from 'node-switchbot'
6
7
 
7
8
  import type { SwitchBotPlatform } from '../platform.js'
8
- import type { devicesConfig } from '../settings.js'
9
- import type { botServiceData } from '../types/bledevicestatus.js'
10
- import type { device } from '../types/devicelist.js'
11
- import type { botStatus } from '../types/devicestatus.js'
12
- import type { botWebhookContext } from '../types/devicewebhookstatus.js'
9
+ import type { botConfig, devicesConfig } from '../settings.js'
10
+
11
+ import { debounceTime, interval, skipWhile, Subject, take, tap } from 'rxjs'
12
+
13
+ import { formatDeviceIdAsMac } from '../utils.js'
14
+ import { deviceBase } from './device.js'
13
15
 
14
16
  /*
15
17
  * For Testing Locally:
16
18
  * import { SwitchBotBLEModel, SwitchBotBLEModelName } from '/Users/Shared/GitHub/OpenWonderLabs/node-switchbot/dist/index.js';
17
19
  */
18
20
  import { SwitchBotBLEModel, SwitchBotBLEModelName } from 'node-switchbot'
19
- import { debounceTime, interval, skipWhile, Subject, take, tap } from 'rxjs'
20
-
21
- import { formatDeviceIdAsMac } from '../utils.js'
22
- import { deviceBase } from './device.js'
23
21
 
24
22
  /**
25
23
  * Platform Accessory
@@ -394,7 +392,7 @@ export class Bot extends deviceBase {
394
392
  this.debugLog('Retrieve initial values and update Homekit')
395
393
  this.refreshStatus()
396
394
  } catch (e: any) {
397
- this.errorLog(`failed to retrieve initial values and update Homekit, Error: ${e}`)
395
+ this.errorLog(`failed to retrieve initial values and update Homekit, Error: ${e.message ?? e}`)
398
396
  }
399
397
 
400
398
  // regisiter webhook event handler if enabled
@@ -402,7 +400,7 @@ export class Bot extends deviceBase {
402
400
  this.debugLog('Registering Webhook Event Handler')
403
401
  this.registerWebhook()
404
402
  } catch (e: any) {
405
- this.errorLog(`failed to registerWebhook, Error: ${e}`)
403
+ this.errorLog(`failed to registerWebhook, Error: ${e.message ?? e}`)
406
404
  }
407
405
 
408
406
  // regisiter platform BLE event handler if enabled
@@ -410,7 +408,7 @@ export class Bot extends deviceBase {
410
408
  this.debugLog('Registering Platform BLE Event Handler')
411
409
  this.registerPlatformBLE()
412
410
  } catch (e: any) {
413
- this.errorLog(`failed to registerPlatformBLE, Error: ${e}`)
411
+ this.errorLog(`failed to registerPlatformBLE, Error: ${e.message ?? e}`)
414
412
  }
415
413
 
416
414
  // Start an update interval
@@ -482,7 +480,7 @@ export class Bot extends deviceBase {
482
480
  // On
483
481
  this.On = this.botMode === 'press' ? false : this.deviceStatus.power === 'on'
484
482
  this.accessory.context.On = this.On
485
- await this.debugLog(`On: ${this.accessory.context.On}`)
483
+ await this.debugLog(`openAPI On: ${this.accessory.context.On}`)
486
484
 
487
485
  // Battery Level
488
486
  this.Battery.BatteryLevel = this.deviceStatus.battery
@@ -551,22 +549,22 @@ export class Bot extends deviceBase {
551
549
 
552
550
  async BLERefreshStatus(): Promise<void> {
553
551
  await this.debugLog('BLERefreshStatus')
554
- const switchbot = await this.switchbotBLE()
555
- if (switchbot === undefined) {
556
- await this.BLERefreshConnection(switchbot)
552
+ const switchBotBLE = await this.switchbotBLE()
553
+ if (switchBotBLE === undefined) {
554
+ await this.BLERefreshConnection(switchBotBLE)
557
555
  } else {
558
556
  // Start to monitor advertisement packets
559
557
  (async () => {
560
558
  // Start to monitor advertisement packets
561
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as botServiceData
559
+ const serviceData = await this.monitorAdvertisementPackets(switchBotBLE) as botServiceData
562
560
  // Update HomeKit
563
561
  if (serviceData.model === SwitchBotBLEModel.Bot && serviceData.modelName === SwitchBotBLEModelName.Bot) {
564
562
  this.serviceData = serviceData
565
563
  await this.BLEparseStatus()
566
564
  await this.updateHomeKitCharacteristics()
567
565
  } else {
568
- await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`)
569
- await this.BLERefreshConnection(switchbot)
566
+ await this.errorLog(`failed to get serviceData, serviceData: ${JSON.stringify(serviceData)}`)
567
+ await this.BLERefreshConnection(switchBotBLE)
570
568
  }
571
569
  })()
572
570
  }
@@ -587,7 +585,7 @@ export class Bot extends deviceBase {
587
585
  await this.BLEparseStatus()
588
586
  await this.updateHomeKitCharacteristics()
589
587
  } catch (e: any) {
590
- await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`)
588
+ await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e.message ?? e}`)
591
589
  }
592
590
  }
593
591
  } catch (error) {
@@ -601,17 +599,17 @@ export class Bot extends deviceBase {
601
599
  async openAPIRefreshStatus(): Promise<void> {
602
600
  await this.debugLog('openAPIRefreshStatus')
603
601
  try {
604
- const { body, statusCode } = await this.deviceRefreshStatus()
605
- const deviceStatus: any = await body.json()
606
- await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
607
- if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
608
- await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
602
+ const { body } = await this.deviceRefreshStatus()
603
+ const deviceStatus: any = await body
604
+ await this.debugLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
605
+ if (await this.successfulStatusCodes(deviceStatus)) {
606
+ await this.debugSuccessLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
609
607
  this.deviceStatus = deviceStatus.body
610
608
  await this.openAPIparseStatus()
611
609
  await this.updateHomeKitCharacteristics()
612
610
  } else {
613
- await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
614
- await this.debugWarnLog(statusCode, deviceStatus)
611
+ await this.debugWarnLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
612
+ await this.debugWarnLog(deviceStatus)
615
613
  }
616
614
  } catch (e: any) {
617
615
  await this.apiError(e)
@@ -629,7 +627,7 @@ export class Bot extends deviceBase {
629
627
  await this.parseStatusWebhook()
630
628
  await this.updateHomeKitCharacteristics()
631
629
  } catch (e: any) {
632
- await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`)
630
+ await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e.message ?? e}`)
633
631
  }
634
632
  }
635
633
  } else {
@@ -668,19 +666,20 @@ export class Bot extends deviceBase {
668
666
  await this.debugLog('BLEpushChanges')
669
667
  if (this.On !== this.accessory.context.On || this.allowPush) {
670
668
  await this.debugLog(`BLEpushChanges On: ${this.On} OnCached: ${this.accessory.context.On}`)
671
- const switchbot = await this.platform.connectBLE(this.accessory, this.device)
669
+ const switchBotBLE = this.platform.switchBotBLE
672
670
  try {
673
671
  const formattedDeviceId = formatDeviceIdAsMac(this.device.deviceId)
674
672
  this.device.bleMac = formattedDeviceId
675
673
  await this.debugLog(`bleMac: ${this.device.bleMac}`)
676
- // if (switchbot !== false) {
674
+ // if (switchBotBLE !== false) {
677
675
  await this.debugLog(`Bot Mode: ${this.botMode}`)
678
676
  if (this.botMode === 'press') {
679
- switchbot
677
+ switchBotBLE
680
678
  .discover({ model: 'H', quick: true, id: this.device.bleMac })
681
- .then(async (device_list: { press: (arg0: { id: string | undefined }) => any }[]) => {
679
+ .then(async (device_list: SwitchbotDevice[]) => {
680
+ const deviceList = device_list as unknown as WoHand[]
682
681
  await this.infoLog(`On: ${this.On}`)
683
- return await device_list[0].press({ id: this.device.bleMac })
682
+ return await deviceList[0].press()
684
683
  })
685
684
  .then(async () => {
686
685
  await this.successLog(`On: ${this.On} sent over SwitchBot BLE, sent successfully`)
@@ -697,17 +696,23 @@ export class Bot extends deviceBase {
697
696
  await this.BLEPushConnection()
698
697
  })
699
698
  } else if (this.botMode === 'switch') {
700
- switchbot
699
+ switchBotBLE
701
700
  .discover({ model: this.device.bleModel, quick: true, id: this.device.bleMac })
702
- .then(async (device_list: any) => {
701
+ .then(async (device_list: SwitchbotDevice[]) => {
702
+ const deviceList = device_list as unknown as WoHand[]
703
703
  this.infoLog(`On: ${this.On}`)
704
+ this.warnLog(`device_list: ${JSON.stringify(device_list)}`)
704
705
  return await this.retryBLE({
705
706
  max: await this.maxRetryBLE(),
706
707
  fn: async () => {
707
- if (this.On) {
708
- return await device_list[0].turnOn({ id: this.device.bleMac })
708
+ if (deviceList.length > 0) {
709
+ if (this.On) {
710
+ return await deviceList[0].turnOn()
711
+ } else {
712
+ return await deviceList[0].turnOff()
713
+ }
709
714
  } else {
710
- return await device_list[0].turnOff({ id: this.device.bleMac })
715
+ throw new Error('No device found')
711
716
  }
712
717
  },
713
718
  })
@@ -749,24 +754,23 @@ export class Bot extends deviceBase {
749
754
  } else {
750
755
  throw new Error('Device Parameters not set for this Bot.')
751
756
  }
752
- const bodyChange = JSON.stringify({
757
+ const bodyChange: bodyChange = {
753
758
  command: `${command}`,
754
759
  parameter: 'default',
755
760
  commandType: 'command',
756
- })
757
- this.debugLog(`Sending request to SwitchBot API, body: ${bodyChange},`)
761
+ }
762
+ this.debugLog(`Sending request to SwitchBot API, body: ${JSON.stringify(bodyChange)},`)
758
763
  try {
759
- const { body, statusCode } = await this.pushChangeRequest(bodyChange)
760
- const deviceStatus: any = await body.json()
761
- await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
762
- if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
763
- await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
764
+ const { body } = await this.pushChangeRequest(bodyChange)
765
+ const deviceStatus: any = await body
766
+ await this.debugLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
767
+ if (await this.successfulStatusCodes(deviceStatus)) {
768
+ await this.debugSuccessLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
764
769
  await this.updateHomeKitCharacteristics()
765
770
  } else {
766
- await this.statusCode(statusCode)
767
771
  await this.statusCode(deviceStatus.statusCode)
768
772
  }
769
- if (this.device.bot?.mode === 'multipress') {
773
+ if ((this.device as botConfig).mode === 'multipress') {
770
774
  this.multiPressCount--
771
775
  if (this.multiPressCount > 0) {
772
776
  await this.debugLog(`multiPressCount: ${this.multiPressCount}`)
@@ -843,7 +847,7 @@ export class Bot extends deviceBase {
843
847
  await this.debugLog(`Set On: ${value}`)
844
848
  this.On = value !== false
845
849
  }
846
- if (this.device.bot?.mode === 'multipress' && this.On) {
850
+ if ((this.device as botConfig).mode === 'multipress' && this.On) {
847
851
  this.multiPressCount++
848
852
  await this.debugLog(`multiPressCount: ${this.multiPressCount}`)
849
853
  }
@@ -1140,45 +1144,42 @@ export class Bot extends deviceBase {
1140
1144
 
1141
1145
  async getBotConfigSettings(device: device & devicesConfig) {
1142
1146
  // Bot Device Type
1143
- this.botDeviceType = device.bot?.deviceType ?? 'outlet'
1144
- const botDeviceType = device.bot?.deviceType
1145
- ? `Using Device Type: ${this.botDeviceType}`
1146
- : `No Device Type Set, deviceType: ${this.device.bot?.deviceType}, Using default deviceType: ${this.botDeviceType}`
1147
- await this.debugWarnLog(botDeviceType)
1148
- this.accessory.context.botDeviceType = this.botDeviceType
1147
+ this.botDeviceType = (device as botConfig).type ?? 'outlet'
1148
+ const botDeviceType = (device as botConfig).type ? 'Device Config' : 'Default'
1149
+ await this.debugWarnLog(`Use ${botDeviceType} Device Type: ${this.botDeviceType}`)
1149
1150
  // Bot Mode
1150
- this.botMode = device.bot?.mode ?? 'switch'
1151
- if (!device.bot?.mode) {
1151
+ this.botMode = (device as botConfig).mode ?? 'switch'
1152
+ if (!(device as botConfig).mode) {
1152
1153
  this.botMode = 'switch'
1153
1154
  this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} does not have bot mode set in the Plugin's SwitchBot Device Settings, defaulting to "${this.botMode}" mode. You may experience issues.`)
1154
- } else if (['switch', 'press', 'multipress'].includes(device.bot.mode)) {
1155
- this.botMode = device.bot.mode
1155
+ } else if (['switch', 'press', 'multipress'].includes((device as botConfig).mode!)) {
1156
+ this.botMode = (device as botConfig).mode!
1156
1157
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Bot Mode: ${this.botMode}`)
1157
1158
  } else {
1158
- throw new Error(`${this.device.deviceType}: ${this.accessory.displayName} Invalid Bot Mode: ${device.bot.mode}`)
1159
+ throw new Error(`${this.device.deviceType}: ${this.accessory.displayName} Invalid Bot Mode: ${(device as botConfig).mode}`)
1159
1160
  }
1160
- const botModeLog = device.bot?.mode
1161
+ const botModeLog = (device as botConfig).mode
1161
1162
  ? `Using Bot Mode: ${this.botMode}`
1162
1163
  : `No Bot Mode Set, Using default Bot Mode: ${this.botMode}`
1163
1164
  await this.debugWarnLog(botModeLog)
1164
1165
  this.accessory.context.botMode = this.botMode
1165
1166
  // Bot Double Press
1166
- this.doublePress = device.bot?.doublePress ?? 1
1167
- const doublePress = device.bot?.doublePress
1167
+ this.doublePress = (device as botConfig).doublePress ?? 1
1168
+ const doublePress = (device as botConfig).doublePress
1168
1169
  ? `Using Double Press: ${this.doublePress}`
1169
1170
  : `No Double Press Set, Using default Double Press: ${this.doublePress}`
1170
1171
  await this.debugWarnLog(doublePress)
1171
1172
  this.accessory.context.doublePress = this.doublePress
1172
1173
  // Bot Press PushRate
1173
- this.pushRatePress = device.bot?.pushRatePress ?? 15
1174
- const pushRatePress = device.bot?.pushRatePress
1174
+ this.pushRatePress = (device as botConfig).pushRatePress ?? 15
1175
+ const pushRatePress = (device as botConfig).pushRatePress
1175
1176
  ? `Using Bot Push Rate Press: ${this.pushRatePress}`
1176
1177
  : `No Push Rate Press Set, Using default Push Rate Press: ${this.pushRatePress}`
1177
1178
  await this.debugWarnLog(pushRatePress)
1178
1179
  this.accessory.context.pushRatePress = this.pushRatePress
1179
1180
  // Bot Allow Push
1180
- this.allowPush = device.bot?.allowPush ?? false
1181
- const allowPush = device.bot?.allowPush
1181
+ this.allowPush = (device as botConfig).allowPush ?? false
1182
+ const allowPush = (device as botConfig).allowPush
1182
1183
  ? `Using Allow Push: ${this.allowPush}`
1183
1184
  : `No Allow Push Set, Using default Allow Push: ${this.allowPush}`
1184
1185
  await this.debugWarnLog(allowPush)
@@ -3,13 +3,10 @@
3
3
  * ceilinglight.ts: @switchbot/homebridge-switchbot.
4
4
  */
5
5
  import type { CharacteristicValue, Controller, ControllerConstructor, ControllerServiceMap, PlatformAccessory, Service } from 'homebridge'
6
+ import type { bodyChange, ceilingLightProServiceData, ceilingLightProStatus, ceilingLightProWebhookContext, ceilingLightServiceData, ceilingLightStatus, ceilingLightWebhookContext, device, SwitchbotDevice, WoCeilingLight } from 'node-switchbot'
6
7
 
7
8
  import type { SwitchBotPlatform } from '../platform.js'
8
- import type { devicesConfig } from '../settings.js'
9
- import type { ceilingLightProServiceData, ceilingLightServiceData } from '../types/bledevicestatus.js'
10
- import type { device } from '../types/devicelist.js'
11
- import type { ceilingLightProStatus, ceilingLightStatus } from '../types/devicestatus.js'
12
- import type { ceilingLightProWebhookContext, ceilingLightWebhookContext } from '../types/devicewebhookstatus.js'
9
+ import type { ceilingLightConfig, devicesConfig } from '../settings.js'
13
10
 
14
11
  /*
15
12
  * For Testing Locally:
@@ -111,7 +108,7 @@ export class CeilingLight extends deviceBase {
111
108
 
112
109
  // Initialize LightBulb Brightness
113
110
  this.LightBulb.Service.getCharacteristic(this.hap.Characteristic.Brightness).setProps({
114
- minStep: device.ceilinglight?.set_minStep ?? 1,
111
+ minStep: (device as ceilingLightConfig).set_minStep ?? 1,
115
112
  minValue: 0,
116
113
  maxValue: 100,
117
114
  validValueRanges: [0, 100],
@@ -151,7 +148,7 @@ export class CeilingLight 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 CeilingLight 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 CeilingLight 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
@@ -286,21 +283,21 @@ export class CeilingLight extends deviceBase {
286
283
 
287
284
  async BLERefreshStatus(): Promise<void> {
288
285
  await this.debugLog('BLERefreshStatus')
289
- const switchbot = await this.switchbotBLE()
290
- if (switchbot === undefined) {
291
- await this.BLERefreshConnection(switchbot)
286
+ const switchBotBLE = await this.switchbotBLE()
287
+ if (switchBotBLE === undefined) {
288
+ await this.BLERefreshConnection(switchBotBLE)
292
289
  } else {
293
290
  (async () => {
294
291
  // Start to monitor advertisement packets
295
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as unknown as ceilingLightServiceData
292
+ const serviceData = await this.monitorAdvertisementPackets(switchBotBLE) as unknown as ceilingLightServiceData
296
293
  // Update HomeKit
297
294
  if ((serviceData.model === SwitchBotBLEModel.CeilingLight || SwitchBotBLEModel.CeilingLightPro) && (serviceData.modelName === SwitchBotBLEModelName.CeilingLight || SwitchBotBLEModelName.CeilingLightPro)) {
298
295
  this.serviceData = serviceData
299
296
  await this.BLEparseStatus()
300
297
  await this.updateHomeKitCharacteristics()
301
298
  } else {
302
- await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`)
303
- await this.BLERefreshConnection(switchbot)
299
+ await this.errorLog(`failed to get serviceData, serviceData: ${JSON.stringify(serviceData)}`)
300
+ await this.BLERefreshConnection(switchBotBLE)
304
301
  }
305
302
  })()
306
303
  }
@@ -321,7 +318,7 @@ export class CeilingLight extends deviceBase {
321
318
  await this.BLEparseStatus()
322
319
  await this.updateHomeKitCharacteristics()
323
320
  } catch (e: any) {
324
- await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`)
321
+ await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e.message ?? e}`)
325
322
  }
326
323
  }
327
324
  } catch (error) {
@@ -335,17 +332,17 @@ export class CeilingLight extends deviceBase {
335
332
  async openAPIRefreshStatus(): Promise<void> {
336
333
  await this.debugLog('openAPIRefreshStatus')
337
334
  try {
338
- const { body, statusCode } = await this.deviceRefreshStatus()
339
- const deviceStatus: any = await body.json()
340
- await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
341
- if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
342
- await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
335
+ const { body } = await this.deviceRefreshStatus()
336
+ const deviceStatus: any = await body
337
+ await this.debugLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
338
+ if (await this.successfulStatusCodes(deviceStatus)) {
339
+ await this.debugSuccessLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
343
340
  this.deviceStatus = deviceStatus.body
344
341
  await this.openAPIparseStatus()
345
342
  await this.updateHomeKitCharacteristics()
346
343
  } else {
347
- await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
348
- await this.debugWarnLog(statusCode, deviceStatus)
344
+ await this.debugWarnLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
345
+ await this.debugWarnLog(deviceStatus)
349
346
  }
350
347
  } catch (e: any) {
351
348
  await this.apiError(e)
@@ -363,7 +360,7 @@ export class CeilingLight extends deviceBase {
363
360
  await this.parseStatusWebhook()
364
361
  await this.updateHomeKitCharacteristics()
365
362
  } catch (e: any) {
366
- await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`)
363
+ await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e.message ?? e}`)
367
364
  }
368
365
  }
369
366
  } else {
@@ -420,23 +417,24 @@ export class CeilingLight extends deviceBase {
420
417
  await this.debugLog('BLEpushChanges')
421
418
  if (this.LightBulb.On !== this.accessory.context.On) {
422
419
  await this.debugLog(`BLEpushChanges On: ${this.LightBulb.On} OnCached: ${this.accessory.context.On}`)
423
- const switchbot = await this.platform.connectBLE(this.accessory, this.device)
420
+ const switchBotBLE = await this.platform.connectBLE(this.accessory, this.device)
424
421
  try {
425
422
  const formattedDeviceId = formatDeviceIdAsMac(this.device.deviceId)
426
423
  this.device.bleMac = formattedDeviceId
427
424
  await this.debugLog(`bleMac: ${this.device.bleMac}`)
428
- if (switchbot !== false) {
429
- switchbot
425
+ if (switchBotBLE !== false) {
426
+ switchBotBLE
430
427
  .discover({ model: this.device.bleModel, id: this.device.bleMac })
431
- .then(async (device_list: any) => {
428
+ .then(async (device_list: SwitchbotDevice[]) => {
429
+ const deviceList = device_list[0] as unknown as WoCeilingLight
432
430
  this.infoLog(`On: ${this.LightBulb.On}`)
433
431
  return await this.retryBLE({
434
432
  max: await this.maxRetryBLE(),
435
433
  fn: async () => {
436
434
  if (this.LightBulb.On) {
437
- return await device_list[0].turnOn({ id: this.device.bleMac })
435
+ return await deviceList[0].turnOn()
438
436
  } else {
439
- return await device_list[0].turnOff({ id: this.device.bleMac })
437
+ return await deviceList[0].turnOff()
440
438
  }
441
439
  },
442
440
  })
@@ -451,7 +449,7 @@ export class CeilingLight extends deviceBase {
451
449
  await this.BLEPushConnection()
452
450
  })
453
451
  } else {
454
- await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`)
452
+ await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${JSON.stringify(switchBotBLE)}`)
455
453
  await this.BLEPushConnection()
456
454
  }
457
455
  } catch (error) {
@@ -466,21 +464,20 @@ export class CeilingLight extends deviceBase {
466
464
  await this.debugLog('openAPIpushChanges')
467
465
  if (this.LightBulb.On !== this.accessory.context.On) {
468
466
  const command = this.LightBulb.On ? 'turnOn' : 'turnOff'
469
- const bodyChange = JSON.stringify({
467
+ const bodyChange: bodyChange = {
470
468
  command: `${command}`,
471
469
  parameter: 'default',
472
470
  commandType: 'command',
473
- })
471
+ }
474
472
  await this.debugLog(`SwitchBot OpenAPI bodyChange: ${JSON.stringify(bodyChange)}`)
475
473
  try {
476
- const { body, statusCode } = await this.pushChangeRequest(bodyChange)
477
- const deviceStatus: any = await body.json()
478
- await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
479
- if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
480
- await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
474
+ const { body } = await this.pushChangeRequest(bodyChange)
475
+ const deviceStatus: any = await body
476
+ await this.debugLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
477
+ if (await this.successfulStatusCodes(deviceStatus)) {
478
+ await this.debugSuccessLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
481
479
  await this.updateHomeKitCharacteristics()
482
480
  } else {
483
- await this.statusCode(statusCode)
484
481
  await this.statusCode(deviceStatus.statusCode)
485
482
  }
486
483
  } catch (e: any) {
@@ -499,21 +496,20 @@ export class CeilingLight extends deviceBase {
499
496
  await this.debugLog(`Saturation: ${JSON.stringify(this.LightBulb.Saturation)}`)
500
497
  const [red, green, blue] = hs2rgb(Number(this.LightBulb.Hue), Number(this.LightBulb.Saturation))
501
498
  await this.debugLog(`rgb: ${JSON.stringify([red, green, blue])}`)
502
- const bodyChange = JSON.stringify({
499
+ const bodyChange: bodyChange = {
503
500
  command: 'setColor',
504
501
  parameter: `${red}:${green}:${blue}`,
505
502
  commandType: 'command',
506
- })
503
+ }
507
504
  await this.debugLog(`(pushHueSaturationChanges) SwitchBot OpenAPI bodyChange: ${JSON.stringify(bodyChange)}`)
508
505
  try {
509
- const { body, statusCode } = await this.pushChangeRequest(bodyChange)
510
- const deviceStatus: any = await body.json()
511
- await this.debugLog(`(pushHueSaturationChanges) statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
512
- if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
513
- await this.debugSuccessLog(`(pushHueSaturationChanges) statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
506
+ const { body } = await this.pushChangeRequest(bodyChange)
507
+ const deviceStatus: any = await body
508
+ await this.debugLog(`(pushHueSaturationChanges) statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
509
+ if (await this.successfulStatusCodes(deviceStatus)) {
510
+ await this.debugSuccessLog(`(pushHueSaturationChanges) statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
514
511
  await this.updateHomeKitCharacteristics()
515
512
  } else {
516
- await this.statusCode(statusCode)
517
513
  await this.statusCode(deviceStatus.statusCode)
518
514
  }
519
515
  } catch (e: any) {
@@ -530,21 +526,20 @@ export class CeilingLight extends deviceBase {
530
526
  if (this.LightBulb.ColorTemperature !== this.accessory.context.ColorTemperature) {
531
527
  const kelvin = Math.round(1000000 * Number(this.LightBulb.ColorTemperature))
532
528
  this.accessory.context.kelvin = kelvin
533
- const bodyChange = JSON.stringify({
529
+ const bodyChange: bodyChange = {
534
530
  command: 'setColorTemperature',
535
531
  parameter: `${kelvin}`,
536
532
  commandType: 'command',
537
- })
533
+ }
538
534
  await this.debugLog(`(pushColorTemperatureChanges) SwitchBot OpenAPI bodyChange: ${JSON.stringify(bodyChange)}`)
539
535
  try {
540
- const { body, statusCode } = await this.pushChangeRequest(bodyChange)
541
- const deviceStatus: any = await body.json()
542
- await this.debugLog(`(pushColorTemperatureChanges) statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
543
- if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
544
- await this.debugSuccessLog(`(pushColorTemperatureChanges) statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
536
+ const { body } = await this.pushChangeRequest(bodyChange)
537
+ const deviceStatus: any = await body
538
+ await this.debugLog(`(pushColorTemperatureChanges) statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
539
+ if (await this.successfulStatusCodes(deviceStatus)) {
540
+ await this.debugSuccessLog(`(pushColorTemperatureChanges) statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
545
541
  await this.updateHomeKitCharacteristics()
546
542
  } else {
547
- await this.statusCode(statusCode)
548
543
  await this.statusCode(deviceStatus.statusCode)
549
544
  }
550
545
  } catch (e: any) {
@@ -559,21 +554,20 @@ export class CeilingLight extends deviceBase {
559
554
  async pushBrightnessChanges(): Promise<void> {
560
555
  await this.debugLog('pushBrightnessChanges')
561
556
  if (this.LightBulb.Brightness !== this.accessory.context.Brightness) {
562
- const bodyChange = JSON.stringify({
557
+ const bodyChange: bodyChange = {
563
558
  command: 'setBrightness',
564
559
  parameter: `${this.LightBulb.Brightness}`,
565
560
  commandType: 'command',
566
- })
561
+ }
567
562
  await this.debugLog(`(pushBrightnessChanges) SwitchBot OpenAPI bodyChange: ${JSON.stringify(bodyChange)}`)
568
563
  try {
569
- const { body, statusCode } = await this.pushChangeRequest(bodyChange)
570
- const deviceStatus: any = await body.json()
571
- await this.debugLog(`(pushBrightnessChanges) statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
572
- if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
573
- await this.debugSuccessLog(`(pushBrightnessChanges) statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
564
+ const { body } = await this.pushChangeRequest(bodyChange)
565
+ const deviceStatus: any = await body
566
+ await this.debugLog(`(pushBrightnessChanges) statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
567
+ if (await this.successfulStatusCodes(deviceStatus)) {
568
+ await this.debugSuccessLog(`(pushBrightnessChanges) statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
574
569
  await this.updateHomeKitCharacteristics()
575
570
  } else {
576
- await this.statusCode(statusCode)
577
571
  await this.statusCode(deviceStatus.statusCode)
578
572
  }
579
573
  } catch (e: any) {
@@ -709,13 +703,8 @@ export class CeilingLight extends deviceBase {
709
703
  this.adaptiveLighting = accessory.context.adaptiveLighting ?? true
710
704
  await this.debugLog(`adaptiveLighting: ${this.adaptiveLighting}`)
711
705
  // Adaptive Lighting Shift
712
- if (device.ceilinglight?.adaptiveLightingShift) {
713
- this.adaptiveLightingShift = device.ceilinglight.adaptiveLightingShift
714
- this.debugLog(`adaptiveLightingShift: ${this.adaptiveLightingShift}`)
715
- } else {
716
- this.adaptiveLightingShift = 0
717
- this.debugLog(`adaptiveLightingShift: ${this.adaptiveLightingShift}`)
718
- }
706
+ this.adaptiveLightingShift = (device as ceilingLightConfig).adaptiveLightingShift ?? 0
707
+ this.debugLog(`adaptiveLightingShift: ${this.adaptiveLightingShift}`)
719
708
  }
720
709
 
721
710
  async BLEPushConnection() {