@switchbot/homebridge-switchbot 5.0.0-beta.70 → 5.0.0-beta.72

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 (534) hide show
  1. package/.github/ISSUE_TEMPLATE/e2e-verification.md +36 -0
  2. package/.github/workflows/ci.yml +61 -0
  3. package/.github/workflows/manual-e2e.yml +103 -0
  4. package/CHANGELOG.md +20 -0
  5. package/E2E-VERIFICATION.md +121 -0
  6. package/MIGRATION.md +44 -0
  7. package/README.md +11 -0
  8. package/config.schema.json +99 -1940
  9. package/dist/deviceFactory.d.ts +13 -0
  10. package/dist/deviceFactory.d.ts.map +1 -0
  11. package/dist/deviceFactory.js +81 -0
  12. package/dist/deviceFactory.js.map +1 -0
  13. package/dist/devices/deviceBase.d.ts +50 -0
  14. package/dist/devices/deviceBase.d.ts.map +1 -0
  15. package/dist/devices/deviceBase.js +119 -0
  16. package/dist/devices/deviceBase.js.map +1 -0
  17. package/dist/devices/genericDevice.d.ts +283 -0
  18. package/dist/devices/genericDevice.d.ts.map +1 -0
  19. package/dist/devices/genericDevice.js +1035 -0
  20. package/dist/devices/genericDevice.js.map +1 -0
  21. package/dist/homebridge-ui/public/index.html +72 -440
  22. package/dist/homebridge-ui/server.d.ts +3 -1
  23. package/dist/homebridge-ui/server.d.ts.map +1 -1
  24. package/dist/homebridge-ui/server.js +47 -10
  25. package/dist/homebridge-ui/server.js.map +1 -1
  26. package/dist/index.d.ts.map +1 -1
  27. package/dist/index.js +1 -3
  28. package/dist/index.js.map +1 -1
  29. package/dist/platform.d.ts +27 -0
  30. package/dist/platform.d.ts.map +1 -0
  31. package/dist/platform.js +404 -0
  32. package/dist/platform.js.map +1 -0
  33. package/dist/settings.d.ts +10 -317
  34. package/dist/settings.d.ts.map +1 -1
  35. package/dist/settings.js +5 -30
  36. package/dist/settings.js.map +1 -1
  37. package/dist/switchbotClient.d.ts +32 -0
  38. package/dist/switchbotClient.d.ts.map +1 -0
  39. package/dist/switchbotClient.js +259 -0
  40. package/dist/switchbotClient.js.map +1 -0
  41. package/dist/utils.d.ts +36 -248
  42. package/dist/utils.d.ts.map +1 -1
  43. package/dist/utils.js +38 -1367
  44. package/dist/utils.js.map +1 -1
  45. package/docs/assets/icons.js +1 -1
  46. package/docs/assets/icons.svg +1 -1
  47. package/docs/assets/style.css +3 -3
  48. package/docs/index.html +50 -15
  49. package/docs/variables/default.html +1 -1
  50. package/package.json +19 -18
  51. package/scripts/e2e/README.md +25 -0
  52. package/scripts/e2e/curtain-e2e.sh +70 -0
  53. package/scripts/e2e/fan-e2e.sh +75 -0
  54. package/scripts/e2e/light-advanced-e2e.sh +97 -0
  55. package/scripts/e2e/light-e2e.sh +75 -0
  56. package/scripts/e2e/list-accessories.sh +19 -0
  57. package/scripts/e2e/lock-e2e.sh +65 -0
  58. package/scripts/generate-matter-maps.js +60 -0
  59. package/scripts/run-e2e-local.sh +14 -0
  60. package/src/deviceFactory.ts +122 -0
  61. package/src/devices/deviceBase.ts +141 -0
  62. package/src/devices/genericDevice.ts +965 -0
  63. package/src/homebridge-ui/public/index.html +72 -440
  64. package/src/homebridge-ui/server.ts +52 -10
  65. package/src/index.ts +1 -3
  66. package/src/platform.ts +395 -0
  67. package/src/settings.ts +12 -352
  68. package/src/switchbotClient.ts +266 -0
  69. package/src/utils.ts +47 -1456
  70. package/test/accessory-restore.spec.ts +73 -0
  71. package/test/device-mapping.spec.ts +37 -0
  72. package/test/deviceFactory.spec.ts +18 -0
  73. package/test/e2e/run-e2e.spec.ts +50 -0
  74. package/test/fan-swing.spec.ts +29 -0
  75. package/test/helpers/matter-harness.ts +53 -0
  76. package/test/lock-users.spec.ts +44 -0
  77. package/test/matter-childbridge.spec.ts +55 -0
  78. package/test/matter-descriptors.spec.ts +97 -0
  79. package/test/matter-device-state.spec.ts +101 -0
  80. package/test/matter-integration.spec.ts +70 -0
  81. package/test/platform.integration.spec.ts +55 -0
  82. package/test/switchbot-client-debounce.spec.ts +131 -0
  83. package/test/switchbot-client-openapi.spec.ts +56 -0
  84. package/test/switchbotClient.spec.ts +10 -0
  85. package/test/utils.spec.ts +20 -0
  86. package/vitest.config.ts +7 -0
  87. package/coverage/base.css +0 -224
  88. package/coverage/block-navigation.js +0 -87
  89. package/coverage/clover.xml +0 -15847
  90. package/coverage/coverage-final.json +0 -42
  91. package/coverage/docs/assets/dmt/dmt-component-data.js.html +0 -85
  92. package/coverage/docs/assets/dmt/dmt-components.js.html +0 -286
  93. package/coverage/docs/assets/dmt/index.html +0 -131
  94. package/coverage/docs/assets/hierarchy.js.html +0 -85
  95. package/coverage/docs/assets/icons.js.html +0 -136
  96. package/coverage/docs/assets/index.html +0 -146
  97. package/coverage/docs/assets/main.js.html +0 -265
  98. package/coverage/favicon.png +0 -0
  99. package/coverage/index.html +0 -191
  100. package/coverage/prettify.css +0 -1
  101. package/coverage/prettify.js +0 -2
  102. package/coverage/sort-arrow-sprite.png +0 -0
  103. package/coverage/sorter.js +0 -196
  104. package/coverage/src/device/blindtilt.ts.html +0 -3238
  105. package/coverage/src/device/bot.ts.html +0 -2803
  106. package/coverage/src/device/ceilinglight.ts.html +0 -2338
  107. package/coverage/src/device/colorbulb.ts.html +0 -2824
  108. package/coverage/src/device/contact.ts.html +0 -1465
  109. package/coverage/src/device/curtain.ts.html +0 -2869
  110. package/coverage/src/device/device.ts.html +0 -2500
  111. package/coverage/src/device/fan.ts.html +0 -2242
  112. package/coverage/src/device/hub.ts.html +0 -1408
  113. package/coverage/src/device/humidifier.ts.html +0 -2116
  114. package/coverage/src/device/index.html +0 -416
  115. package/coverage/src/device/iosensor.ts.html +0 -1375
  116. package/coverage/src/device/lightstrip.ts.html +0 -2617
  117. package/coverage/src/device/lock.ts.html +0 -1963
  118. package/coverage/src/device/meter.ts.html +0 -1372
  119. package/coverage/src/device/meterplus.ts.html +0 -1384
  120. package/coverage/src/device/meterpro.ts.html +0 -1618
  121. package/coverage/src/device/motion.ts.html +0 -1264
  122. package/coverage/src/device/plug.ts.html +0 -1372
  123. package/coverage/src/device/relayswitch.ts.html +0 -2284
  124. package/coverage/src/device/robotvacuumcleaner.ts.html +0 -1810
  125. package/coverage/src/device/waterdetector.ts.html +0 -1294
  126. package/coverage/src/homebridge-ui/index.html +0 -116
  127. package/coverage/src/homebridge-ui/server.ts.html +0 -229
  128. package/coverage/src/index.html +0 -161
  129. package/coverage/src/index.ts.html +0 -124
  130. package/coverage/src/irdevice/airconditioner.ts.html +0 -1687
  131. package/coverage/src/irdevice/airpurifier.ts.html +0 -844
  132. package/coverage/src/irdevice/camera.ts.html +0 -475
  133. package/coverage/src/irdevice/fan.ts.html +0 -766
  134. package/coverage/src/irdevice/index.html +0 -251
  135. package/coverage/src/irdevice/irdevice.ts.html +0 -1117
  136. package/coverage/src/irdevice/light.ts.html +0 -826
  137. package/coverage/src/irdevice/other.ts.html +0 -2458
  138. package/coverage/src/irdevice/tv.ts.html +0 -1222
  139. package/coverage/src/irdevice/vacuumcleaner.ts.html +0 -466
  140. package/coverage/src/irdevice/waterheater.ts.html +0 -469
  141. package/coverage/src/platform.ts.html +0 -8776
  142. package/coverage/src/settings.ts.html +0 -934
  143. package/coverage/src/utils.ts.html +0 -2092
  144. package/dist/devices-hap/airpurifier.d.ts +0 -54
  145. package/dist/devices-hap/airpurifier.d.ts.map +0 -1
  146. package/dist/devices-hap/airpurifier.js +0 -533
  147. package/dist/devices-hap/airpurifier.js.map +0 -1
  148. package/dist/devices-hap/blindtilt.d.ts +0 -90
  149. package/dist/devices-hap/blindtilt.d.ts.map +0 -1
  150. package/dist/devices-hap/blindtilt.js +0 -974
  151. package/dist/devices-hap/blindtilt.js.map +0 -1
  152. package/dist/devices-hap/bot.d.ts +0 -102
  153. package/dist/devices-hap/bot.d.ts.map +0 -1
  154. package/dist/devices-hap/bot.js +0 -822
  155. package/dist/devices-hap/bot.js.map +0 -1
  156. package/dist/devices-hap/ceilinglight.d.ts +0 -85
  157. package/dist/devices-hap/ceilinglight.d.ts.map +0 -1
  158. package/dist/devices-hap/ceilinglight.js +0 -707
  159. package/dist/devices-hap/ceilinglight.js.map +0 -1
  160. package/dist/devices-hap/colorbulb.d.ts +0 -88
  161. package/dist/devices-hap/colorbulb.d.ts.map +0 -1
  162. package/dist/devices-hap/colorbulb.js +0 -921
  163. package/dist/devices-hap/colorbulb.js.map +0 -1
  164. package/dist/devices-hap/contact.d.ts +0 -44
  165. package/dist/devices-hap/contact.d.ts.map +0 -1
  166. package/dist/devices-hap/contact.js +0 -409
  167. package/dist/devices-hap/contact.js.map +0 -1
  168. package/dist/devices-hap/curtain.d.ts +0 -73
  169. package/dist/devices-hap/curtain.d.ts.map +0 -1
  170. package/dist/devices-hap/curtain.js +0 -869
  171. package/dist/devices-hap/curtain.js.map +0 -1
  172. package/dist/devices-hap/device.d.ts +0 -108
  173. package/dist/devices-hap/device.d.ts.map +0 -1
  174. package/dist/devices-hap/device.js +0 -821
  175. package/dist/devices-hap/device.js.map +0 -1
  176. package/dist/devices-hap/fan.d.ts +0 -69
  177. package/dist/devices-hap/fan.d.ts.map +0 -1
  178. package/dist/devices-hap/fan.js +0 -655
  179. package/dist/devices-hap/fan.js.map +0 -1
  180. package/dist/devices-hap/hub.d.ts +0 -37
  181. package/dist/devices-hap/hub.d.ts.map +0 -1
  182. package/dist/devices-hap/hub.js +0 -393
  183. package/dist/devices-hap/hub.js.map +0 -1
  184. package/dist/devices-hap/humidifier.d.ts +0 -73
  185. package/dist/devices-hap/humidifier.d.ts.map +0 -1
  186. package/dist/devices-hap/humidifier.js +0 -716
  187. package/dist/devices-hap/humidifier.js.map +0 -1
  188. package/dist/devices-hap/iosensor.d.ts +0 -42
  189. package/dist/devices-hap/iosensor.d.ts.map +0 -1
  190. package/dist/devices-hap/iosensor.js +0 -397
  191. package/dist/devices-hap/iosensor.js.map +0 -1
  192. package/dist/devices-hap/lightstrip.d.ts +0 -79
  193. package/dist/devices-hap/lightstrip.d.ts.map +0 -1
  194. package/dist/devices-hap/lightstrip.js +0 -827
  195. package/dist/devices-hap/lightstrip.js.map +0 -1
  196. package/dist/devices-hap/lock.d.ts +0 -53
  197. package/dist/devices-hap/lock.d.ts.map +0 -1
  198. package/dist/devices-hap/lock.js +0 -569
  199. package/dist/devices-hap/lock.js.map +0 -1
  200. package/dist/devices-hap/meter.d.ts +0 -37
  201. package/dist/devices-hap/meter.d.ts.map +0 -1
  202. package/dist/devices-hap/meter.js +0 -380
  203. package/dist/devices-hap/meter.js.map +0 -1
  204. package/dist/devices-hap/meterplus.d.ts +0 -42
  205. package/dist/devices-hap/meterplus.d.ts.map +0 -1
  206. package/dist/devices-hap/meterplus.js +0 -385
  207. package/dist/devices-hap/meterplus.js.map +0 -1
  208. package/dist/devices-hap/meterpro.d.ts +0 -43
  209. package/dist/devices-hap/meterpro.d.ts.map +0 -1
  210. package/dist/devices-hap/meterpro.js +0 -469
  211. package/dist/devices-hap/meterpro.js.map +0 -1
  212. package/dist/devices-hap/motion.d.ts +0 -42
  213. package/dist/devices-hap/motion.d.ts.map +0 -1
  214. package/dist/devices-hap/motion.js +0 -345
  215. package/dist/devices-hap/motion.js.map +0 -1
  216. package/dist/devices-hap/plug.d.ts +0 -49
  217. package/dist/devices-hap/plug.d.ts.map +0 -1
  218. package/dist/devices-hap/plug.js +0 -400
  219. package/dist/devices-hap/plug.js.map +0 -1
  220. package/dist/devices-hap/relayswitch.d.ts +0 -96
  221. package/dist/devices-hap/relayswitch.d.ts.map +0 -1
  222. package/dist/devices-hap/relayswitch.js +0 -642
  223. package/dist/devices-hap/relayswitch.js.map +0 -1
  224. package/dist/devices-hap/robotvacuumcleaner.d.ts +0 -54
  225. package/dist/devices-hap/robotvacuumcleaner.d.ts.map +0 -1
  226. package/dist/devices-hap/robotvacuumcleaner.js +0 -530
  227. package/dist/devices-hap/robotvacuumcleaner.js.map +0 -1
  228. package/dist/devices-hap/waterdetector.d.ts +0 -41
  229. package/dist/devices-hap/waterdetector.d.ts.map +0 -1
  230. package/dist/devices-hap/waterdetector.js +0 -356
  231. package/dist/devices-hap/waterdetector.js.map +0 -1
  232. package/dist/devices-matter/BaseMatterAccessory.d.ts +0 -90
  233. package/dist/devices-matter/BaseMatterAccessory.d.ts.map +0 -1
  234. package/dist/devices-matter/BaseMatterAccessory.js +0 -264
  235. package/dist/devices-matter/BaseMatterAccessory.js.map +0 -1
  236. package/dist/devices-matter/ColorLightAccessory.d.ts +0 -20
  237. package/dist/devices-matter/ColorLightAccessory.d.ts.map +0 -1
  238. package/dist/devices-matter/ColorLightAccessory.js +0 -95
  239. package/dist/devices-matter/ColorLightAccessory.js.map +0 -1
  240. package/dist/devices-matter/ColorTemperatureLightAccessory.d.ts +0 -18
  241. package/dist/devices-matter/ColorTemperatureLightAccessory.d.ts.map +0 -1
  242. package/dist/devices-matter/ColorTemperatureLightAccessory.js +0 -76
  243. package/dist/devices-matter/ColorTemperatureLightAccessory.js.map +0 -1
  244. package/dist/devices-matter/ContactSensorAccessory.d.ts +0 -12
  245. package/dist/devices-matter/ContactSensorAccessory.d.ts.map +0 -1
  246. package/dist/devices-matter/ContactSensorAccessory.js +0 -34
  247. package/dist/devices-matter/ContactSensorAccessory.js.map +0 -1
  248. package/dist/devices-matter/DimmableLightAccessory.d.ts +0 -58
  249. package/dist/devices-matter/DimmableLightAccessory.d.ts.map +0 -1
  250. package/dist/devices-matter/DimmableLightAccessory.js +0 -167
  251. package/dist/devices-matter/DimmableLightAccessory.js.map +0 -1
  252. package/dist/devices-matter/DoorLockAccessory.d.ts +0 -14
  253. package/dist/devices-matter/DoorLockAccessory.d.ts.map +0 -1
  254. package/dist/devices-matter/DoorLockAccessory.js +0 -50
  255. package/dist/devices-matter/DoorLockAccessory.js.map +0 -1
  256. package/dist/devices-matter/ExtendedColorLightAccessory.d.ts +0 -21
  257. package/dist/devices-matter/ExtendedColorLightAccessory.d.ts.map +0 -1
  258. package/dist/devices-matter/ExtendedColorLightAccessory.js +0 -106
  259. package/dist/devices-matter/ExtendedColorLightAccessory.js.map +0 -1
  260. package/dist/devices-matter/FanAccessory.d.ts +0 -16
  261. package/dist/devices-matter/FanAccessory.d.ts.map +0 -1
  262. package/dist/devices-matter/FanAccessory.js +0 -81
  263. package/dist/devices-matter/FanAccessory.js.map +0 -1
  264. package/dist/devices-matter/HumiditySensorAccessory.d.ts +0 -12
  265. package/dist/devices-matter/HumiditySensorAccessory.d.ts.map +0 -1
  266. package/dist/devices-matter/HumiditySensorAccessory.js +0 -34
  267. package/dist/devices-matter/HumiditySensorAccessory.js.map +0 -1
  268. package/dist/devices-matter/LeakSensorAccessory.d.ts +0 -12
  269. package/dist/devices-matter/LeakSensorAccessory.d.ts.map +0 -1
  270. package/dist/devices-matter/LeakSensorAccessory.js +0 -33
  271. package/dist/devices-matter/LeakSensorAccessory.js.map +0 -1
  272. package/dist/devices-matter/LightSensorAccessory.d.ts +0 -12
  273. package/dist/devices-matter/LightSensorAccessory.d.ts.map +0 -1
  274. package/dist/devices-matter/LightSensorAccessory.js +0 -34
  275. package/dist/devices-matter/LightSensorAccessory.js.map +0 -1
  276. package/dist/devices-matter/OccupancySensorAccessory.d.ts +0 -12
  277. package/dist/devices-matter/OccupancySensorAccessory.d.ts.map +0 -1
  278. package/dist/devices-matter/OccupancySensorAccessory.js +0 -39
  279. package/dist/devices-matter/OccupancySensorAccessory.js.map +0 -1
  280. package/dist/devices-matter/OnOffLightAccessory.d.ts +0 -38
  281. package/dist/devices-matter/OnOffLightAccessory.d.ts.map +0 -1
  282. package/dist/devices-matter/OnOffLightAccessory.js +0 -110
  283. package/dist/devices-matter/OnOffLightAccessory.js.map +0 -1
  284. package/dist/devices-matter/OnOffOutletAccessory.d.ts +0 -14
  285. package/dist/devices-matter/OnOffOutletAccessory.d.ts.map +0 -1
  286. package/dist/devices-matter/OnOffOutletAccessory.js +0 -43
  287. package/dist/devices-matter/OnOffOutletAccessory.js.map +0 -1
  288. package/dist/devices-matter/OnOffSwitchAccessory.d.ts +0 -14
  289. package/dist/devices-matter/OnOffSwitchAccessory.d.ts.map +0 -1
  290. package/dist/devices-matter/OnOffSwitchAccessory.js +0 -42
  291. package/dist/devices-matter/OnOffSwitchAccessory.js.map +0 -1
  292. package/dist/devices-matter/RoboticVacuumAccessory.d.ts +0 -61
  293. package/dist/devices-matter/RoboticVacuumAccessory.d.ts.map +0 -1
  294. package/dist/devices-matter/RoboticVacuumAccessory.js +0 -544
  295. package/dist/devices-matter/RoboticVacuumAccessory.js.map +0 -1
  296. package/dist/devices-matter/SmokeCOAlarmAccessory.d.ts +0 -11
  297. package/dist/devices-matter/SmokeCOAlarmAccessory.d.ts.map +0 -1
  298. package/dist/devices-matter/SmokeCOAlarmAccessory.js +0 -49
  299. package/dist/devices-matter/SmokeCOAlarmAccessory.js.map +0 -1
  300. package/dist/devices-matter/TemperatureSensorAccessory.d.ts +0 -12
  301. package/dist/devices-matter/TemperatureSensorAccessory.d.ts.map +0 -1
  302. package/dist/devices-matter/TemperatureSensorAccessory.js +0 -36
  303. package/dist/devices-matter/TemperatureSensorAccessory.js.map +0 -1
  304. package/dist/devices-matter/ThermostatAccessory.d.ts +0 -19
  305. package/dist/devices-matter/ThermostatAccessory.d.ts.map +0 -1
  306. package/dist/devices-matter/ThermostatAccessory.js +0 -95
  307. package/dist/devices-matter/ThermostatAccessory.js.map +0 -1
  308. package/dist/devices-matter/VenetianBlindAccessory.d.ts +0 -19
  309. package/dist/devices-matter/VenetianBlindAccessory.d.ts.map +0 -1
  310. package/dist/devices-matter/VenetianBlindAccessory.js +0 -99
  311. package/dist/devices-matter/VenetianBlindAccessory.js.map +0 -1
  312. package/dist/devices-matter/WindowBlindAccessory.d.ts +0 -17
  313. package/dist/devices-matter/WindowBlindAccessory.d.ts.map +0 -1
  314. package/dist/devices-matter/WindowBlindAccessory.js +0 -131
  315. package/dist/devices-matter/WindowBlindAccessory.js.map +0 -1
  316. package/dist/devices-matter/custom/PowerStripAccessory.d.ts +0 -97
  317. package/dist/devices-matter/custom/PowerStripAccessory.d.ts.map +0 -1
  318. package/dist/devices-matter/custom/PowerStripAccessory.js +0 -265
  319. package/dist/devices-matter/custom/PowerStripAccessory.js.map +0 -1
  320. package/dist/devices-matter/custom/index.d.ts +0 -8
  321. package/dist/devices-matter/custom/index.d.ts.map +0 -1
  322. package/dist/devices-matter/custom/index.js +0 -8
  323. package/dist/devices-matter/custom/index.js.map +0 -1
  324. package/dist/devices-matter/index.d.ts +0 -29
  325. package/dist/devices-matter/index.d.ts.map +0 -1
  326. package/dist/devices-matter/index.js +0 -28
  327. package/dist/devices-matter/index.js.map +0 -1
  328. package/dist/irdevice/airconditioner.d.ts +0 -61
  329. package/dist/irdevice/airconditioner.d.ts.map +0 -1
  330. package/dist/irdevice/airconditioner.js +0 -472
  331. package/dist/irdevice/airconditioner.js.map +0 -1
  332. package/dist/irdevice/airpurifier.d.ts +0 -50
  333. package/dist/irdevice/airpurifier.d.ts.map +0 -1
  334. package/dist/irdevice/airpurifier.js +0 -213
  335. package/dist/irdevice/airpurifier.js.map +0 -1
  336. package/dist/irdevice/camera.d.ts +0 -32
  337. package/dist/irdevice/camera.d.ts.map +0 -1
  338. package/dist/irdevice/camera.js +0 -107
  339. package/dist/irdevice/camera.js.map +0 -1
  340. package/dist/irdevice/fan.d.ts +0 -36
  341. package/dist/irdevice/fan.d.ts.map +0 -1
  342. package/dist/irdevice/fan.js +0 -200
  343. package/dist/irdevice/fan.js.map +0 -1
  344. package/dist/irdevice/irdevice.d.ts +0 -69
  345. package/dist/irdevice/irdevice.d.ts.map +0 -1
  346. package/dist/irdevice/irdevice.js +0 -339
  347. package/dist/irdevice/irdevice.js.map +0 -1
  348. package/dist/irdevice/light.d.ts +0 -36
  349. package/dist/irdevice/light.d.ts.map +0 -1
  350. package/dist/irdevice/light.js +0 -206
  351. package/dist/irdevice/light.js.map +0 -1
  352. package/dist/irdevice/other.d.ts +0 -57
  353. package/dist/irdevice/other.d.ts.map +0 -1
  354. package/dist/irdevice/other.js +0 -778
  355. package/dist/irdevice/other.js.map +0 -1
  356. package/dist/irdevice/tv.d.ts +0 -45
  357. package/dist/irdevice/tv.d.ts.map +0 -1
  358. package/dist/irdevice/tv.js +0 -327
  359. package/dist/irdevice/tv.js.map +0 -1
  360. package/dist/irdevice/vacuumcleaner.d.ts +0 -28
  361. package/dist/irdevice/vacuumcleaner.d.ts.map +0 -1
  362. package/dist/irdevice/vacuumcleaner.js +0 -104
  363. package/dist/irdevice/vacuumcleaner.js.map +0 -1
  364. package/dist/irdevice/waterheater.d.ts +0 -30
  365. package/dist/irdevice/waterheater.d.ts.map +0 -1
  366. package/dist/irdevice/waterheater.js +0 -105
  367. package/dist/irdevice/waterheater.js.map +0 -1
  368. package/dist/platform-hap.d.ts +0 -160
  369. package/dist/platform-hap.d.ts.map +0 -1
  370. package/dist/platform-hap.js +0 -3041
  371. package/dist/platform-hap.js.map +0 -1
  372. package/dist/platform-matter.d.ts +0 -188
  373. package/dist/platform-matter.d.ts.map +0 -1
  374. package/dist/platform-matter.js +0 -2545
  375. package/dist/platform-matter.js.map +0 -1
  376. package/dist/test/apiRequestTracker.test.d.ts +0 -2
  377. package/dist/test/apiRequestTracker.test.d.ts.map +0 -1
  378. package/dist/test/apiRequestTracker.test.js +0 -392
  379. package/dist/test/apiRequestTracker.test.js.map +0 -1
  380. package/dist/test/hap/device-webhook-context.test.d.ts +0 -2
  381. package/dist/test/hap/device-webhook-context.test.d.ts.map +0 -1
  382. package/dist/test/hap/device-webhook-context.test.js +0 -128
  383. package/dist/test/hap/device-webhook-context.test.js.map +0 -1
  384. package/dist/test/hap/platform-hap.logging.test.d.ts +0 -2
  385. package/dist/test/hap/platform-hap.logging.test.d.ts.map +0 -1
  386. package/dist/test/hap/platform-hap.logging.test.js +0 -33
  387. package/dist/test/hap/platform-hap.logging.test.js.map +0 -1
  388. package/dist/test/hap/platform-hap.test.d.ts +0 -2
  389. package/dist/test/hap/platform-hap.test.d.ts.map +0 -1
  390. package/dist/test/hap/platform-hap.test.js +0 -62
  391. package/dist/test/hap/platform-hap.test.js.map +0 -1
  392. package/dist/test/helpers/platform-fixtures.d.ts +0 -9
  393. package/dist/test/helpers/platform-fixtures.d.ts.map +0 -1
  394. package/dist/test/helpers/platform-fixtures.js +0 -30
  395. package/dist/test/helpers/platform-fixtures.js.map +0 -1
  396. package/dist/test/homebridge-ui/server.test.d.ts +0 -2
  397. package/dist/test/homebridge-ui/server.test.d.ts.map +0 -1
  398. package/dist/test/homebridge-ui/server.test.js +0 -445
  399. package/dist/test/homebridge-ui/server.test.js.map +0 -1
  400. package/dist/test/index.test.d.ts +0 -2
  401. package/dist/test/index.test.d.ts.map +0 -1
  402. package/dist/test/index.test.js +0 -19
  403. package/dist/test/index.test.js.map +0 -1
  404. package/dist/test/matter/devices-matter/baseMatterAccessory.test.d.ts +0 -2
  405. package/dist/test/matter/devices-matter/baseMatterAccessory.test.d.ts.map +0 -1
  406. package/dist/test/matter/devices-matter/baseMatterAccessory.test.js +0 -71
  407. package/dist/test/matter/devices-matter/baseMatterAccessory.test.js.map +0 -1
  408. package/dist/test/matter/platform-matter.additional.test.d.ts +0 -2
  409. package/dist/test/matter/platform-matter.additional.test.d.ts.map +0 -1
  410. package/dist/test/matter/platform-matter.additional.test.js +0 -35
  411. package/dist/test/matter/platform-matter.additional.test.js.map +0 -1
  412. package/dist/test/matter/platform-matter.bleparse.test.d.ts +0 -2
  413. package/dist/test/matter/platform-matter.bleparse.test.d.ts.map +0 -1
  414. package/dist/test/matter/platform-matter.bleparse.test.js +0 -43
  415. package/dist/test/matter/platform-matter.bleparse.test.js.map +0 -1
  416. package/dist/test/matter/platform-matter.cleanup.test.d.ts +0 -2
  417. package/dist/test/matter/platform-matter.cleanup.test.d.ts.map +0 -1
  418. package/dist/test/matter/platform-matter.cleanup.test.js +0 -70
  419. package/dist/test/matter/platform-matter.cleanup.test.js.map +0 -1
  420. package/dist/test/matter/platform-matter.keepstale.test.d.ts +0 -2
  421. package/dist/test/matter/platform-matter.keepstale.test.d.ts.map +0 -1
  422. package/dist/test/matter/platform-matter.keepstale.test.js +0 -27
  423. package/dist/test/matter/platform-matter.keepstale.test.js.map +0 -1
  424. package/dist/test/matter/platform-matter.logging.test.d.ts +0 -2
  425. package/dist/test/matter/platform-matter.logging.test.d.ts.map +0 -1
  426. package/dist/test/matter/platform-matter.logging.test.js +0 -29
  427. package/dist/test/matter/platform-matter.logging.test.js.map +0 -1
  428. package/dist/test/matter/platform-matter.mapping.test.d.ts +0 -2
  429. package/dist/test/matter/platform-matter.mapping.test.d.ts.map +0 -1
  430. package/dist/test/matter/platform-matter.mapping.test.js +0 -43
  431. package/dist/test/matter/platform-matter.mapping.test.js.map +0 -1
  432. package/dist/test/matter/platform-matter.openapi-mapping.test.d.ts +0 -2
  433. package/dist/test/matter/platform-matter.openapi-mapping.test.d.ts.map +0 -1
  434. package/dist/test/matter/platform-matter.openapi-mapping.test.js +0 -84
  435. package/dist/test/matter/platform-matter.openapi-mapping.test.js.map +0 -1
  436. package/dist/test/matter/platform-matter.test.d.ts +0 -2
  437. package/dist/test/matter/platform-matter.test.d.ts.map +0 -1
  438. package/dist/test/matter/platform-matter.test.js +0 -117
  439. package/dist/test/matter/platform-matter.test.js.map +0 -1
  440. package/dist/test/matter/platform-matter.unregister.test.d.ts +0 -2
  441. package/dist/test/matter/platform-matter.unregister.test.d.ts.map +0 -1
  442. package/dist/test/matter/platform-matter.unregister.test.js +0 -30
  443. package/dist/test/matter/platform-matter.unregister.test.js.map +0 -1
  444. package/dist/test/matter/platform-matter.webhook.test.d.ts +0 -2
  445. package/dist/test/matter/platform-matter.webhook.test.d.ts.map +0 -1
  446. package/dist/test/matter/platform-matter.webhook.test.js +0 -46
  447. package/dist/test/matter/platform-matter.webhook.test.js.map +0 -1
  448. package/dist/test/utils.test.d.ts +0 -2
  449. package/dist/test/utils.test.d.ts.map +0 -1
  450. package/dist/test/utils.test.js +0 -95
  451. package/dist/test/utils.test.js.map +0 -1
  452. package/dist/test/verifyconfig.test.d.ts +0 -2
  453. package/dist/test/verifyconfig.test.d.ts.map +0 -1
  454. package/dist/test/verifyconfig.test.js +0 -167
  455. package/dist/test/verifyconfig.test.js.map +0 -1
  456. package/src/custom.d.ts +0 -7
  457. package/src/devices-hap/airpurifier.ts +0 -568
  458. package/src/devices-hap/blindtilt.ts +0 -1049
  459. package/src/devices-hap/bot.ts +0 -910
  460. package/src/devices-hap/ceilinglight.ts +0 -747
  461. package/src/devices-hap/colorbulb.ts +0 -940
  462. package/src/devices-hap/contact.ts +0 -457
  463. package/src/devices-hap/curtain.ts +0 -944
  464. package/src/devices-hap/device.ts +0 -890
  465. package/src/devices-hap/fan.ts +0 -716
  466. package/src/devices-hap/hub.ts +0 -440
  467. package/src/devices-hap/humidifier.ts +0 -762
  468. package/src/devices-hap/iosensor.ts +0 -442
  469. package/src/devices-hap/lightstrip.ts +0 -863
  470. package/src/devices-hap/lock.ts +0 -627
  471. package/src/devices-hap/meter.ts +0 -427
  472. package/src/devices-hap/meterplus.ts +0 -431
  473. package/src/devices-hap/meterpro.ts +0 -523
  474. package/src/devices-hap/motion.ts +0 -390
  475. package/src/devices-hap/plug.ts +0 -427
  476. package/src/devices-hap/relayswitch.ts +0 -727
  477. package/src/devices-hap/robotvacuumcleaner.ts +0 -574
  478. package/src/devices-hap/waterdetector.ts +0 -400
  479. package/src/devices-matter/BaseMatterAccessory.ts +0 -302
  480. package/src/devices-matter/ColorLightAccessory.ts +0 -110
  481. package/src/devices-matter/ColorTemperatureLightAccessory.ts +0 -90
  482. package/src/devices-matter/ContactSensorAccessory.ts +0 -41
  483. package/src/devices-matter/DimmableLightAccessory.ts +0 -192
  484. package/src/devices-matter/DoorLockAccessory.ts +0 -60
  485. package/src/devices-matter/ExtendedColorLightAccessory.ts +0 -122
  486. package/src/devices-matter/FanAccessory.ts +0 -95
  487. package/src/devices-matter/HumiditySensorAccessory.ts +0 -41
  488. package/src/devices-matter/LeakSensorAccessory.ts +0 -40
  489. package/src/devices-matter/LightSensorAccessory.ts +0 -41
  490. package/src/devices-matter/OccupancySensorAccessory.ts +0 -48
  491. package/src/devices-matter/OnOffLightAccessory.ts +0 -125
  492. package/src/devices-matter/OnOffOutletAccessory.ts +0 -51
  493. package/src/devices-matter/OnOffSwitchAccessory.ts +0 -51
  494. package/src/devices-matter/RoboticVacuumAccessory.ts +0 -621
  495. package/src/devices-matter/SmokeCOAlarmAccessory.ts +0 -59
  496. package/src/devices-matter/TemperatureSensorAccessory.ts +0 -43
  497. package/src/devices-matter/ThermostatAccessory.ts +0 -110
  498. package/src/devices-matter/VenetianBlindAccessory.ts +0 -115
  499. package/src/devices-matter/WindowBlindAccessory.ts +0 -135
  500. package/src/devices-matter/custom/PowerStripAccessory.ts +0 -309
  501. package/src/devices-matter/custom/index.ts +0 -8
  502. package/src/devices-matter/index.ts +0 -29
  503. package/src/irdevice/airconditioner.ts +0 -533
  504. package/src/irdevice/airpurifier.ts +0 -252
  505. package/src/irdevice/camera.ts +0 -129
  506. package/src/irdevice/fan.ts +0 -226
  507. package/src/irdevice/irdevice.ts +0 -383
  508. package/src/irdevice/light.ts +0 -246
  509. package/src/irdevice/other.ts +0 -790
  510. package/src/irdevice/tv.ts +0 -378
  511. package/src/irdevice/vacuumcleaner.ts +0 -126
  512. package/src/irdevice/waterheater.ts +0 -127
  513. package/src/platform-hap.ts +0 -3193
  514. package/src/platform-matter.ts +0 -2703
  515. package/src/test/apiRequestTracker.test.ts +0 -417
  516. package/src/test/hap/device-webhook-context.test.ts +0 -136
  517. package/src/test/hap/platform-hap.logging.test.ts +0 -36
  518. package/src/test/hap/platform-hap.test.ts +0 -70
  519. package/src/test/helpers/platform-fixtures.ts +0 -33
  520. package/src/test/homebridge-ui/server.test.ts +0 -486
  521. package/src/test/index.test.ts +0 -24
  522. package/src/test/matter/devices-matter/baseMatterAccessory.test.ts +0 -88
  523. package/src/test/matter/platform-matter.additional.test.ts +0 -44
  524. package/src/test/matter/platform-matter.bleparse.test.ts +0 -47
  525. package/src/test/matter/platform-matter.cleanup.test.ts +0 -86
  526. package/src/test/matter/platform-matter.keepstale.test.ts +0 -37
  527. package/src/test/matter/platform-matter.logging.test.ts +0 -33
  528. package/src/test/matter/platform-matter.mapping.test.ts +0 -57
  529. package/src/test/matter/platform-matter.openapi-mapping.test.ts +0 -109
  530. package/src/test/matter/platform-matter.test.ts +0 -144
  531. package/src/test/matter/platform-matter.unregister.test.ts +0 -39
  532. package/src/test/matter/platform-matter.webhook.test.ts +0 -54
  533. package/src/test/utils.test.ts +0 -96
  534. package/src/test/verifyconfig.test.ts +0 -198
package/src/utils.ts CHANGED
@@ -1,1459 +1,50 @@
1
- import type { API, Logging } from 'homebridge'
2
- import type { blindTilt, curtain, curtain3, device } from 'node-switchbot'
3
-
4
- import type { devicesConfig } from './settings.js'
5
-
6
- /* Copyright(C) 2017-2024, donavanbecker (https://github.com/donavanbecker). All rights reserved.
7
- *
8
- * util.ts: @switchbot/homebridge-switchbot platform class.
9
- */
10
- import { existsSync, readFileSync, writeFileSync } from 'node:fs'
11
- import { join } from 'node:path'
12
-
13
- export enum BlindTiltMappingMode {
14
- OnlyUp = 'only_up',
15
- OnlyDown = 'only_down',
16
- DownAndUp = 'down_and_up',
17
- UpAndDown = 'up_and_down',
18
- UseTiltForDirection = 'use_tilt_for_direction',
19
- }
20
-
21
- export function isCurtainDevice(device: device & devicesConfig): device is (curtain | curtain3) & devicesConfig {
22
- return device.deviceType === 'Curtain' || device.deviceType === 'Curtain3'
23
- }
24
-
25
- export function isBlindTiltDevice(device: device & devicesConfig): device is blindTilt & devicesConfig {
26
- return device.deviceType === 'Blind Tilt'
27
- }
28
-
29
- export function sleep(ms: number): Promise<void> {
30
- return new Promise(resolve => setTimeout(resolve, ms))
31
- }
32
- /**
33
- * Check if the humidity is within the min and max range
34
- * @param humidity - The humidity value
35
- * @param min - The minimum humidity value
36
- * @param max - The maximum humidity value
37
- * @returns The humidity value
38
- */
39
- export function validHumidity(humidity: number, min?: number, max?: number): number {
40
- if (humidity < (min || 0)) {
41
- return min ?? 0
42
- } else if (humidity > (max || 100)) {
43
- return max ?? 100
44
- }
45
- return humidity
46
- }
47
-
48
- /**
49
- * Converts the value to celsius if the temperature units are in Fahrenheit
50
- */
51
- export function convertUnits(value: number, unit: string, convert?: string): number {
52
- // Convert only when source unit differs from target unit.
53
- // Supported values for unit/convert: 'CELSIUS' | 'FAHRENHEIT'
54
- if (!convert || unit === convert) {
55
- return value
56
- }
57
-
58
- if (unit === 'CELSIUS' && convert === 'FAHRENHEIT') {
59
- return Math.round((value * 9) / 5 + 32)
60
- }
61
-
62
- if (unit === 'FAHRENHEIT' && convert === 'CELSIUS') {
63
- // Celsius should be to the nearest 0.5 degree
64
- return Math.round(((value - 32) * 5) / 9 * 2) / 2
65
- }
66
-
67
- // Unknown unit combination: return as-is
68
- return value
69
- }
70
-
71
- /**
72
- * Safely serializes an object to a JSON string, handling circular references.
73
- *
74
- * @param obj - The object to be serialized.
75
- * @returns The JSON string representation of the object.
76
- */
77
- export function safeStringify(obj: any) {
78
- const seen = new WeakSet()
79
- return JSON.stringify(obj, (_key, value) => {
80
- if (typeof value === 'object' && value !== null) {
81
- if (seen.has(value)) {
82
- return
83
- }
84
- seen.add(value)
85
- }
86
- return value
87
- }, ' ')
88
- }
89
-
90
- /**
91
- * Formats a device ID as a MAC address.
92
- * Ensures the device ID does not already contain colons.
93
- *
94
- * @param deviceId - The device ID to format.
95
- * @param cassSensative - If the MAC address should be case sensitive. Default is false, which will return the MAC address in lowercase.
96
- * @returns The formatted MAC address.
97
- * @throws Will throw an error if the device ID is not a valid MAC address or a 12-character hexadecimal string.
98
- */
99
- export function formatDeviceIdAsMac(deviceId: string, cassSensative?: boolean): string {
100
- if (typeof deviceId !== 'string') {
101
- throw new TypeError('Invalid device ID format. Device ID must be a string.')
102
- }
103
-
104
- deviceId = deviceId.trim()
105
-
106
- const macAddressRegex = /^(?:[0-9a-f]{2}:){5}[0-9a-f]{2}$/i
107
- const hexRegex = /^[0-9a-f]{12}$/i
108
- const vacuumFormatRegex = /^[a-z0-9]{12,18}$/i
109
-
110
- // Check if the deviceId is already in a valid MAC address format
111
- if (macAddressRegex.test(deviceId)) {
112
- return cassSensative ? deviceId : deviceId.toLowerCase()
113
- }
114
-
115
- // Check if the deviceId is a valid 12-character hexadecimal string
116
- if (hexRegex.test(deviceId)) {
117
- const formattedDeviceId = deviceId.match(/.{1,2}/g)!.join(':')
118
- return cassSensative ? formattedDeviceId : formattedDeviceId.toLowerCase()
119
- }
120
-
121
- // Check if the deviceId matches the custom format
122
- if (vacuumFormatRegex.test(deviceId)) {
123
- const formattedDeviceId = deviceId.slice(-12).match(/.{1,2}/g)!.join(':')
124
- return cassSensative ? formattedDeviceId : formattedDeviceId.toLowerCase()
125
- }
126
-
127
- throw new Error(`Invalid device ID format. Must be a valid MAC address, a 12-character hexadecimal string, or a 12 to 18-character alphanumeric string. Device ID: ${deviceId}`)
128
- }
129
-
130
- export function rgb2hs(r: any, g: any, b: any) {
131
- /**
132
- * Credit:
133
- * https://github.com/WickyNilliams/pure-color
134
- */
135
- r = Number.parseInt(r)
136
- g = Number.parseInt(g)
137
- b = Number.parseInt(b)
138
- const min = Math.min(r, g, b)
139
- const max = Math.max(r, g, b)
140
- const delta = max - min
141
- let h
142
- let s
143
- if (max === 0) {
144
- s = 0
145
- } else {
146
- s = (delta / max) * 100
147
- }
148
- if (max === min) {
149
- h = 0
150
- } else if (r === max) {
151
- h = (g - b) / delta
152
- } else if (g === max) {
153
- h = 2 + (b - r) / delta
154
- } else if (b === max) {
155
- h = 4 + (r - g) / delta
156
- }
157
- h = Math.min(h * 60, 360)
158
-
159
- if (h < 0) {
160
- h += 360
161
- }
162
- return [Math.round(h), Math.round(s)]
163
- }
164
-
165
- export function hs2rgb(h: any, s: any) {
166
- /*
167
- Credit:
168
- https://github.com/WickyNilliams/pure-color
169
- */
170
- h = Number.parseInt(h) / 60
171
- s = Number.parseInt(s) / 100
172
- const f = h - Math.floor(h)
173
- const p = 255 * (1 - s)
174
- const q = 255 * (1 - s * f)
175
- const t = 255 * (1 - s * (1 - f))
176
- let rgb
177
- switch (Math.floor(h) % 6) {
178
- case 0:
179
- rgb = [255, t, p]
180
- break
181
- case 1:
182
- rgb = [q, 255, p]
183
- break
184
- case 2:
185
- rgb = [p, 255, t]
186
- break
187
- case 3:
188
- rgb = [p, q, 255]
189
- break
190
- case 4:
191
- rgb = [t, p, 255]
192
- break
193
- case 5:
194
- rgb = [255, p, q]
195
- break
196
- }
197
- if (rgb[0] === 255) {
198
- rgb[1] *= 0.8
199
- rgb[2] *= 0.8
200
- if (rgb[1] <= 25 && rgb[2] <= 25) {
201
- rgb[1] = 0
202
- rgb[2] = 0
1
+ import type { PlatformConfig } from 'homebridge'
2
+ import type { SwitchBotPluginConfig } from './settings.js'
3
+
4
+ // Canonical Matter cluster ID mapping (from matter.js clusters)
5
+ export const MATTER_CLUSTER_IDS = {
6
+ OnOff: 0x0006,
7
+ LevelControl: 0x0008,
8
+ ColorControl: 0x0300,
9
+ WindowCovering: 0x0102,
10
+ DoorLock: 0x0101,
11
+ FanControl: 0x0202,
12
+ RelativeHumidityMeasurement: 0x0405,
13
+ } as const
14
+
15
+ // Common Matter attribute IDs grouped by cluster
16
+ export const MATTER_ATTRIBUTE_IDS = {
17
+ OnOff: { OnOff: 0x0000 },
18
+ LevelControl: { CurrentLevel: 0x0000 },
19
+ ColorControl: { CurrentHue: 0x0000, CurrentSaturation: 0x0001, ColorTemperatureMireds: 0x0002 },
20
+ WindowCovering: { CurrentPosition: 0x0000, TargetPosition: 0x0001 },
21
+ FanControl: { SpeedCurrent: 0x0000 },
22
+ DoorLock: { LockState: 0x0000 },
23
+ RelativeHumidityMeasurement: { MeasuredValue: 0x0000 },
24
+ } as const
25
+
26
+ export function normalizeConfig(raw?: PlatformConfig): SwitchBotPluginConfig {
27
+ if (!raw) return {}
28
+ return { ...(raw as any) } as SwitchBotPluginConfig
29
+ }
30
+
31
+ // Create a Proxy constructor that instantiates the right platform implementation at runtime.
32
+ export function createPlatformProxy(HAPPlatform: any, MatterPlatform: any): any {
33
+ return class SwitchBotPlatformProxy {
34
+ private impl: any
35
+ constructor(log: any, config: PlatformConfig, api: any) {
36
+ const cfg = normalizeConfig(config)
37
+ const preferMatter = cfg.preferMatter ?? true
38
+ const enableMatter = cfg.enableMatter ?? true
39
+
40
+ if (enableMatter && preferMatter && MatterPlatform) {
41
+ this.impl = new MatterPlatform(log, cfg, api)
42
+ return this.impl
43
+ }
44
+
45
+ // Fallback to HAP
46
+ this.impl = new HAPPlatform(log, cfg, api)
47
+ return this.impl
203
48
  }
204
49
  }
205
- return [Math.round(rgb[0]), Math.round(rgb[1]), Math.round(rgb[2])]
206
- }
207
-
208
- export function k2rgb(k: number) {
209
- // Set kelvin to nearest 100, between 2000 and 7100
210
- k = Math.round(k / 100) * 100
211
- k = Math.max(Math.min(k, 7100), 2000)
212
-
213
- // k should now appear in our table of kelvin to rgb
214
- const values = {
215
- 2000: [255, 141, 11],
216
- 2100: [255, 146, 29],
217
- 2200: [255, 147, 44],
218
- 2300: [255, 152, 54],
219
- 2400: [255, 157, 63],
220
- 2500: [255, 166, 69],
221
- 2600: [255, 170, 77],
222
- 2700: [255, 174, 84],
223
- 2800: [255, 173, 94],
224
- 2900: [255, 177, 101],
225
- 3000: [255, 180, 107],
226
- 3100: [255, 189, 111],
227
- 3200: [255, 187, 120],
228
- 3300: [255, 195, 124],
229
- 3400: [255, 198, 130],
230
- 3500: [255, 201, 135],
231
- 3600: [255, 203, 141],
232
- 3700: [255, 206, 146],
233
- 3800: [255, 204, 153],
234
- 3900: [255, 206, 159],
235
- 4000: [255, 213, 161],
236
- 4100: [255, 215, 166],
237
- 4200: [255, 217, 171],
238
- 4300: [255, 219, 175],
239
- 4400: [255, 221, 180],
240
- 4500: [255, 223, 184],
241
- 4600: [255, 225, 188],
242
- 4700: [255, 226, 192],
243
- 4800: [255, 228, 196],
244
- 4900: [255, 229, 200],
245
- 5000: [255, 231, 204],
246
- 5100: [255, 230, 210],
247
- 5200: [255, 234, 211],
248
- 5300: [255, 235, 215],
249
- 5400: [255, 237, 218],
250
- 5500: [255, 236, 224],
251
- 5700: [255, 240, 228],
252
- 5800: [255, 241, 231],
253
- 5900: [255, 243, 234],
254
- 6000: [255, 244, 237],
255
- 6100: [255, 245, 240],
256
- 6200: [255, 246, 243],
257
- 6300: [255, 247, 247],
258
- 6400: [255, 248, 251],
259
- 6500: [255, 249, 253],
260
- 6600: [254, 249, 255],
261
- 6700: [252, 247, 255],
262
- 6800: [249, 246, 255],
263
- 6900: [247, 245, 255],
264
- 7000: [245, 243, 255],
265
- 7100: [243, 242, 255],
266
- }
267
-
268
- // Return the value
269
- return values[k]
270
- }
271
-
272
- export function m2hs(m) {
273
- /*
274
- Credit:
275
- https://github.com/homebridge/HAP-NodeJS
276
- */
277
- const table = {
278
- 100: [19, 222.1],
279
- 101: [18.7, 222.2],
280
- 102: [18.4, 222.3],
281
- 103: [18.2, 222.3],
282
- 104: [17.9, 222.4],
283
- 105: [17.6, 222.5],
284
- 106: [17.3, 222.7],
285
- 107: [17, 222.8],
286
- 108: [16.7, 222.9],
287
- 109: [16.4, 223],
288
- 110: [16.1, 223.2],
289
- 111: [15.8, 223.3],
290
- 112: [15.4, 223.4],
291
- 113: [15.2, 223.6],
292
- 114: [14.9, 223.8],
293
- 115: [14.7, 223.9],
294
- 116: [14.3, 224.1],
295
- 117: [14.1, 224.2],
296
- 118: [13.8, 224.4],
297
- 119: [13.5, 224.6],
298
- 120: [13.2, 224.8],
299
- 121: [12.9, 225],
300
- 122: [12.5, 225.3],
301
- 123: [12.2, 225.6],
302
- 124: [11.8, 225.9],
303
- 125: [11.4, 226.3],
304
- 126: [11.1, 226.7],
305
- 127: [10.7, 227.1],
306
- 128: [10.3, 227.6],
307
- 129: [9.9, 228],
308
- 130: [9.6, 228.5],
309
- 131: [9.3, 229.1],
310
- 132: [8.9, 229.6],
311
- 133: [8.5, 230.2],
312
- 134: [8.2, 230.9],
313
- 135: [7.8, 231.6],
314
- 136: [7.5, 232.5],
315
- 137: [7.1, 233.5],
316
- 138: [6.7, 234.6],
317
- 139: [6.3, 235.8],
318
- 140: [6, 237.1],
319
- 141: [5.6, 238.9],
320
- 142: [5.2, 240.9],
321
- 143: [5, 242.9],
322
- 144: [4.8, 244.9],
323
- 145: [4.6, 246.9],
324
- 146: [4.4, 249.3],
325
- 147: [4.3, 251.9],
326
- 148: [4.1, 254.9],
327
- 149: [3.9, 258],
328
- 150: [3.7, 261.8],
329
- 151: [3.4, 265.9],
330
- 152: [3.2, 271],
331
- 153: [3, 276.4],
332
- 154: [2.8, 283.6],
333
- 155: [2.6, 290.4],
334
- 156: [2.3, 295.3],
335
- 157: [2.1, 300],
336
- 158: [1.9, 300],
337
- 159: [1.6, 300],
338
- 160: [1.4, 195.8],
339
- 161: [1.2, 84.3],
340
- 162: [1.3, 58.2],
341
- 163: [1.5, 55.9],
342
- 164: [1.7, 53.2],
343
- 165: [1.9, 50.2],
344
- 166: [2.1, 47.1],
345
- 167: [2.4, 44.5],
346
- 168: [2.6, 42.6],
347
- 169: [2.9, 40.9],
348
- 170: [3.1, 39.5],
349
- 171: [3.4, 38.3],
350
- 172: [3.7, 37.3],
351
- 173: [3.9, 36.5],
352
- 174: [4.2, 35.7],
353
- 175: [4.4, 35.1],
354
- 176: [4.6, 34.5],
355
- 177: [4.9, 34],
356
- 178: [5.1, 33.5],
357
- 179: [5.3, 33],
358
- 180: [5.6, 32.7],
359
- 181: [5.8, 32.3],
360
- 182: [6, 32],
361
- 183: [6.3, 31.7],
362
- 184: [6.5, 31.4],
363
- 185: [6.7, 31.2],
364
- 186: [7, 30.9],
365
- 187: [7.2, 30.7],
366
- 188: [7.4, 30.5],
367
- 189: [7.6, 30.3],
368
- 190: [7.9, 30.1],
369
- 191: [8.1, 29.9],
370
- 192: [8.4, 29.7],
371
- 193: [8.6, 29.6],
372
- 194: [8.9, 29.5],
373
- 195: [9.1, 29.3],
374
- 196: [9.4, 29.2],
375
- 197: [9.6, 29.1],
376
- 198: [9.8, 29],
377
- 199: [10, 28.9],
378
- 200: [10.2, 28.7],
379
- 201: [10.5, 28.7],
380
- 202: [10.7, 28.6],
381
- 203: [11, 28.5],
382
- 204: [11.2, 28.4],
383
- 205: [11.4, 28.3],
384
- 206: [11.6, 28.3],
385
- 207: [11.8, 28.2],
386
- 208: [12.1, 28.1],
387
- 209: [12.3, 28.1],
388
- 210: [12.5, 28],
389
- 211: [12.7, 28],
390
- 212: [12.9, 27.9],
391
- 213: [13.2, 27.8],
392
- 214: [13.4, 27.8],
393
- 215: [13.6, 27.7],
394
- 216: [13.8, 27.7],
395
- 217: [14, 27.7],
396
- 218: [14.3, 27.6],
397
- 219: [14.5, 27.6],
398
- 220: [14.7, 27.5],
399
- 221: [14.9, 27.5],
400
- 222: [15.1, 27.5],
401
- 223: [15.3, 27.4],
402
- 224: [15.5, 27.4],
403
- 225: [15.8, 27.4],
404
- 226: [16, 27.3],
405
- 227: [16.2, 27.3],
406
- 228: [16.4, 27.3],
407
- 229: [16.6, 27.3],
408
- 230: [16.8, 27.2],
409
- 231: [17, 27.2],
410
- 232: [17.2, 27.2],
411
- 233: [17.4, 27.2],
412
- 234: [17.6, 27.2],
413
- 235: [17.8, 27.1],
414
- 236: [18, 27.1],
415
- 237: [18.2, 27.1],
416
- 238: [18.4, 27.1],
417
- 239: [18.7, 27.1],
418
- 240: [18.8, 27],
419
- 241: [19, 27],
420
- 242: [19.2, 27],
421
- 243: [19.4, 27],
422
- 244: [19.6, 27],
423
- 245: [19.8, 27],
424
- 246: [20, 27],
425
- 247: [20.3, 26.9],
426
- 248: [20.5, 26.9],
427
- 249: [20.6, 26.9],
428
- 250: [20.8, 26.9],
429
- 251: [21, 26.9],
430
- 252: [21.3, 26.9],
431
- 253: [21.5, 26.9],
432
- 254: [21.6, 26.9],
433
- 255: [21.8, 26.8],
434
- 256: [22, 26.8],
435
- 257: [22.2, 26.8],
436
- 258: [22.4, 26.8],
437
- 259: [22.6, 26.8],
438
- 260: [22.8, 26.8],
439
- 261: [23, 26.8],
440
- 262: [23.2, 26.8],
441
- 263: [23.4, 26.8],
442
- 264: [23.6, 26.8],
443
- 265: [23.8, 26.8],
444
- 266: [24, 26.8],
445
- 267: [24.1, 26.8],
446
- 268: [24.3, 26.8],
447
- 269: [24.5, 26.8],
448
- 270: [24.7, 26.8],
449
- 271: [24.8, 26.8],
450
- 272: [25.1, 26.7],
451
- 273: [25.3, 26.7],
452
- 274: [25.4, 26.7],
453
- 275: [25.6, 26.7],
454
- 276: [25.8, 26.7],
455
- 277: [26, 26.7],
456
- 278: [26.1, 26.7],
457
- 279: [26.3, 26.7],
458
- 280: [26.5, 26.7],
459
- 281: [26.7, 26.7],
460
- 282: [26.9, 26.7],
461
- 283: [27.1, 26.7],
462
- 284: [27.3, 26.7],
463
- 285: [27.5, 26.7],
464
- 286: [27.7, 26.7],
465
- 287: [27.8, 26.7],
466
- 288: [28, 26.7],
467
- 289: [28.2, 26.7],
468
- 290: [28.4, 26.7],
469
- 291: [28.6, 26.7],
470
- 292: [28.8, 26.7],
471
- 293: [28.9, 26.7],
472
- 294: [29.1, 26.7],
473
- 295: [29.3, 26.7],
474
- 296: [29.5, 26.7],
475
- 297: [29.6, 26.7],
476
- 298: [29.8, 26.7],
477
- 299: [30, 26.7],
478
- 300: [30.2, 26.7],
479
- 301: [30.4, 26.7],
480
- 302: [30.5, 26.7],
481
- 303: [30.7, 26.7],
482
- 304: [30.9, 26.7],
483
- 305: [31.1, 26.7],
484
- 306: [31.2, 26.7],
485
- 307: [31.4, 26.7],
486
- 308: [31.6, 26.7],
487
- 309: [31.8, 26.8],
488
- 310: [31.9, 26.8],
489
- 311: [32.1, 26.8],
490
- 312: [32.3, 26.8],
491
- 313: [32.5, 26.8],
492
- 314: [32.6, 26.8],
493
- 315: [32.8, 26.8],
494
- 316: [33, 26.8],
495
- 317: [33.2, 26.8],
496
- 318: [33.3, 26.8],
497
- 319: [33.5, 26.8],
498
- 320: [33.7, 26.8],
499
- 321: [33.8, 26.8],
500
- 322: [34, 26.8],
501
- 323: [34.2, 26.8],
502
- 324: [34.4, 26.8],
503
- 325: [34.5, 26.8],
504
- 326: [34.7, 26.8],
505
- 327: [34.9, 26.8],
506
- 328: [35.1, 26.8],
507
- 329: [35.2, 26.8],
508
- 330: [35.4, 26.8],
509
- 331: [35.5, 26.8],
510
- 332: [35.7, 26.8],
511
- 333: [35.9, 26.8],
512
- 334: [36.1, 26.8],
513
- 335: [36.3, 26.9],
514
- 336: [36.5, 26.9],
515
- 337: [36.7, 26.9],
516
- 338: [36.9, 26.9],
517
- 339: [37.1, 26.9],
518
- 340: [37.2, 26.9],
519
- 341: [37.4, 26.9],
520
- 342: [37.5, 26.9],
521
- 343: [37.7, 26.9],
522
- 344: [37.9, 26.9],
523
- 345: [38.1, 26.9],
524
- 346: [38.3, 26.9],
525
- 347: [38.5, 26.9],
526
- 348: [38.7, 26.9],
527
- 349: [38.9, 26.9],
528
- 350: [39, 26.9],
529
- 351: [39.2, 26.9],
530
- 352: [39.3, 27],
531
- 353: [39.5, 27],
532
- 354: [39.7, 27],
533
- 355: [39.9, 27],
534
- 356: [40.1, 27],
535
- 357: [40.2, 27],
536
- 358: [40.4, 27],
537
- 359: [40.6, 27],
538
- 360: [40.8, 27],
539
- 361: [40.9, 27],
540
- 362: [41.1, 27],
541
- 363: [41.2, 27],
542
- 364: [41.4, 27],
543
- 365: [41.6, 27],
544
- 366: [41.8, 27],
545
- 367: [42, 27],
546
- 368: [42.1, 27.1],
547
- 369: [42.3, 27.1],
548
- 370: [42.4, 27.1],
549
- 371: [42.6, 27.1],
550
- 372: [42.8, 27.1],
551
- 373: [43, 27.1],
552
- 374: [43.1, 27.1],
553
- 375: [43.2, 27.1],
554
- 376: [43.4, 27.1],
555
- 377: [43.6, 27.1],
556
- 378: [43.8, 27.1],
557
- 379: [43.9, 27.1],
558
- 380: [44.1, 27.1],
559
- 381: [44.3, 27.2],
560
- 382: [44.4, 27.2],
561
- 383: [44.6, 27.2],
562
- 384: [44.7, 27.2],
563
- 385: [44.9, 27.2],
564
- 386: [45.1, 27.2],
565
- 387: [45.3, 27.2],
566
- 388: [45.5, 27.2],
567
- 389: [45.6, 27.2],
568
- 390: [45.8, 27.2],
569
- 391: [46, 27.2],
570
- 392: [46.2, 27.3],
571
- 393: [46.4, 27.3],
572
- 394: [46.5, 27.3],
573
- 395: [46.7, 27.3],
574
- 396: [46.9, 27.3],
575
- 397: [47.1, 27.3],
576
- 398: [47.2, 27.3],
577
- 399: [47.4, 27.3],
578
- 400: [47.6, 27.3],
579
- 401: [47.7, 27.3],
580
- 402: [47.9, 27.3],
581
- 403: [48.1, 27.3],
582
- 404: [48.3, 27.3],
583
- 405: [48.5, 27.4],
584
- 406: [48.7, 27.4],
585
- 407: [48.8, 27.4],
586
- 408: [49, 27.4],
587
- 409: [49.2, 27.4],
588
- 410: [49.4, 27.4],
589
- 411: [49.6, 27.4],
590
- 412: [49.7, 27.4],
591
- 413: [49.9, 27.4],
592
- 414: [50.1, 27.4],
593
- 415: [50.2, 27.4],
594
- 416: [50.4, 27.4],
595
- 417: [50.6, 27.5],
596
- 418: [50.7, 27.5],
597
- 419: [50.9, 27.5],
598
- 420: [51.1, 27.5],
599
- 421: [51.2, 27.5],
600
- 422: [51.4, 27.5],
601
- 423: [51.6, 27.5],
602
- 424: [51.7, 27.5],
603
- 425: [51.9, 27.5],
604
- 426: [52.1, 27.5],
605
- 427: [51.2, 27.6],
606
- 428: [52.4, 27.6],
607
- 429: [52.5, 27.6],
608
- 430: [52.7, 27.6],
609
- 431: [52.9, 27.6],
610
- 432: [53.1, 27.6],
611
- 433: [53.2, 27.6],
612
- 434: [53.4, 27.6],
613
- 435: [53.6, 27.6],
614
- 436: [53.7, 27.6],
615
- 437: [53.9, 27.6],
616
- 438: [54.1, 27.7],
617
- 439: [54.2, 27.7],
618
- 440: [54.3, 27.7],
619
- 441: [54.5, 27.7],
620
- 442: [54.7, 27.7],
621
- 443: [54.8, 27.7],
622
- 444: [55, 27.7],
623
- 445: [55.2, 27.7],
624
- 446: [55.3, 27.7],
625
- 447: [55.5, 27.7],
626
- 448: [55.7, 27.7],
627
- 449: [55.8, 27.8],
628
- 450: [56, 27.8],
629
- 451: [56.2, 27.8],
630
- 452: [56.3, 27.8],
631
- 453: [56.5, 27.8],
632
- 454: [56.7, 27.8],
633
- 455: [56.8, 27.8],
634
- 456: [57, 27.8],
635
- 457: [57.2, 27.8],
636
- 458: [57.3, 27.9],
637
- 459: [57.4, 27.9],
638
- 460: [57.6, 27.9],
639
- 461: [57.8, 27.9],
640
- 462: [57.9, 27.9],
641
- 463: [58.1, 27.9],
642
- 464: [58.3, 27.9],
643
- 465: [58.4, 27.9],
644
- 466: [58.6, 27.9],
645
- 467: [58.8, 27.9],
646
- 468: [59, 28],
647
- 469: [59.1, 28],
648
- 470: [59.2, 28],
649
- 471: [59.4, 28],
650
- 472: [59.6, 28],
651
- 473: [59.7, 28],
652
- 474: [60, 28],
653
- 475: [60.1, 28],
654
- 476: [60.2, 28],
655
- 477: [60.4, 28],
656
- 478: [60.6, 28.1],
657
- 479: [60.7, 28.1],
658
- 480: [60.9, 28.1],
659
- 481: [60.1, 28.1],
660
- 482: [60.3, 28.1],
661
- 483: [61.4, 28.1],
662
- 484: [61.5, 28.1],
663
- 485: [61.7, 28.1],
664
- 486: [61.9, 28.1],
665
- 487: [62, 28.2],
666
- 488: [62.2, 28.2],
667
- 489: [62.3, 28.2],
668
- 490: [62.5, 28.2],
669
- 491: [62.7, 28.2],
670
- 492: [62.8, 28.2],
671
- 493: [63, 28.2],
672
- 494: [63.2, 28.2],
673
- 495: [63.3, 28.2],
674
- 496: [63.4, 28.2],
675
- 497: [63.6, 28.2],
676
- 498: [63.8, 28.3],
677
- 499: [63.9, 28.3],
678
- 500: [64.1, 28.3],
679
- }
680
- const input = Math.min(Math.max(Math.round(m), 140), 500)
681
- const toReturn = table[input]
682
- return [Math.round(toReturn[1]), Math.round(toReturn[0])]
683
- }
684
-
685
- /**
686
- * Factory that returns a function to send OpenAPI commands using a retry wrapper.
687
- *
688
- * @param retryCommandFunc - bound function that calls platform.retryCommand(device, bodyChange, maxRetries, delay)
689
- * @param deviceObj - the device object to operate on
690
- * @param opts - optional overrides for maxRetries and delayBetweenRetries
691
- * @param opts.maxRetries - override for maxRetries
692
- * @param opts.delayBetweenRetries - override for delayBetweenRetries
693
- */
694
- export function makeOpenAPISender(retryCommandFunc: any, deviceObj: any, opts?: { maxRetries?: number, delayBetweenRetries?: number }) {
695
- return async (command: string, parameter = 'default') => {
696
- const bodyChange: any = { command, parameter, commandType: 'command' }
697
- return retryCommandFunc(deviceObj, bodyChange, opts?.maxRetries, opts?.delayBetweenRetries)
698
- }
699
- }
700
-
701
- /**
702
- * Factory that returns a function to perform BLE actions using a SwitchBotBLE client.
703
- * Handles discovery retries and method invocation on the discovered device instance.
704
- *
705
- * @param switchBotBLE - instance of SwitchBotBLE (may be undefined)
706
- * @param deviceObj - the device object (used to obtain bleModel/deviceId)
707
- * @param opts - optional retry settings
708
- * @param opts.bleRetries - number of BLE discovery retries
709
- * @param opts.bleRetryDelay - delay between BLE retries in ms
710
- */
711
- export function makeBLESender(switchBotBLE: any, deviceObj: any, opts?: { bleRetries?: number, bleRetryDelay?: number }) {
712
- return async (methodName: string, ...args: any[]) => {
713
- if (!switchBotBLE) {
714
- throw new Error('Platform BLE not available')
715
- }
716
- const id = formatDeviceIdAsMac(deviceObj.deviceId)
717
- const maxRetries = opts?.bleRetries ?? 2
718
- const retryDelay = opts?.bleRetryDelay ?? 500
719
- let attempt = 0
720
- while (attempt < maxRetries) {
721
- try {
722
- const list = await switchBotBLE.discover({ model: (deviceObj as any).bleModel, id })
723
- if (!Array.isArray(list) || list.length === 0) {
724
- throw new Error('BLE device not found')
725
- }
726
- const deviceInst: any = list[0]
727
- if (typeof deviceInst[methodName] !== 'function') {
728
- throw new TypeError(`BLE method ${methodName} not available on device`)
729
- }
730
- return await deviceInst[methodName](...args)
731
- } catch (e: any) {
732
- attempt++
733
- if (attempt >= maxRetries) {
734
- throw e
735
- }
736
- await sleep(retryDelay)
737
- }
738
- }
739
- throw new Error('BLE operation failed')
740
- }
741
- }
742
-
743
- /**
744
- * Decide effective connection type for a device given platform options.
745
- * Mirrors the logic previously in platform-matter.
746
- */
747
- export function chooseConnectionType(platformOptions: any, deviceObj: any): 'BLE' | 'OpenAPI' {
748
- if (deviceObj?.connectionType) {
749
- return deviceObj.connectionType === 'BLE' ? 'BLE' : 'OpenAPI'
750
- }
751
- if (platformOptions?.BLE && (deviceObj?.bleModel || (typeof deviceObj?.deviceId === 'string' && deviceObj.deviceId.length > 0))) {
752
- return 'BLE'
753
- }
754
- return 'OpenAPI'
755
- }
756
-
757
- /**
758
- * Detect whether Matter is enabled/available on the provided Homebridge API object.
759
- * This encapsulates the multi-fallback detection used across the project.
760
- */
761
- /**
762
- * Detect whether Matter is enabled on the provided Homebridge API object.
763
- * Returns an object with an `enabled` boolean and an optional `reason` string
764
- * describing which check matched (useful for diagnostics).
765
- */
766
- export function detectMatter(apiObj: API): { enabled: boolean, reason?: string } {
767
- try {
768
- const maybe = (apiObj as any).isMatterEnabled
769
- if (typeof maybe === 'function') {
770
- return { enabled: Boolean(maybe.call(apiObj)), reason: 'api.isMatterEnabled() returned truthy' }
771
- }
772
- if (typeof maybe !== 'undefined') {
773
- return { enabled: Boolean(maybe), reason: 'api.isMatterEnabled property present' }
774
- }
775
-
776
- const server = (apiObj as any).server ?? (apiObj as any).homebridgeServer ?? (apiObj as any).homebridge_server
777
- const serverMaybe = server?.isMatterEnabled
778
- if (typeof serverMaybe === 'function') {
779
- return { enabled: Boolean(serverMaybe.call(server)), reason: 'server.isMatterEnabled() returned truthy' }
780
- }
781
- if (typeof server?.isMatterEnabled !== 'undefined') {
782
- return { enabled: Boolean(server.isMatterEnabled), reason: 'server.isMatterEnabled property present' }
783
- }
784
- } catch (e: any) {
785
- return { enabled: false, reason: `error during detection: ${String(e?.message ?? e)}` }
786
- }
787
- return { enabled: false, reason: 'no isMatterEnabled API or server fallback detected' }
788
- }
789
-
790
- /**
791
- * Backwards-compatible boolean wrapper for detectMatter.
792
- */
793
- export function detectMatterEnabled(apiObj: API): boolean {
794
- return detectMatter(apiObj).enabled
795
- }
796
-
797
- /**
798
- * Create platform logging helpers used by both HAP and Matter platforms.
799
- *
800
- * getPlatformLogging may be either a synchronous string-returning function or an
801
- * async function that resolves to the current platform logging setting. The
802
- * returned helpers mirror the instance methods previously implemented on the
803
- * HAP platform (infoLog, warnLog, errorLog, debugLog, etc.).
804
- */
805
- export function createPlatformLogger(getPlatformLogging: () => string | Promise<string | undefined>, log: Logging) {
806
- const getPL = async () => {
807
- try {
808
- return await getPlatformLogging()
809
- } catch {
810
- return undefined
811
- }
812
- }
813
-
814
- const loggingIsDebug = async () => {
815
- const pl = await getPL()
816
- return pl === 'debugMode' || pl === 'debug'
817
- }
818
-
819
- const enablingPlatformLogging = async () => {
820
- const pl = await getPL()
821
- return pl === 'debugMode' || pl === 'debug' || pl === 'standard'
822
- }
823
-
824
- const formatArgs = (args: any[]): string => {
825
- return args
826
- .map((a: any) => {
827
- if (typeof a === 'string') {
828
- return a
829
- }
830
- try {
831
- return JSON.stringify(a)
832
- } catch {
833
- return String(a)
834
- }
835
- })
836
- .join(' ')
837
- }
838
-
839
- return {
840
- // Format arbitrary arguments into a single string to ensure values are not dropped
841
- // when loggers only accept a single message parameter.
842
- // Prefer readable JSON for objects, fall back to String() on errors.
843
- // Example: infoLog('Loaded', accessory.displayName) => "Loaded My Light"
844
- infoLog: async (...args: any[]) => {
845
- if (await enablingPlatformLogging()) {
846
- const msg = formatArgs(args)
847
- log.info(msg)
848
- }
849
- },
850
- successLog: async (...args: any[]) => {
851
- if (await enablingPlatformLogging()) {
852
- const msg = formatArgs(args)
853
- // Some Logging implementations expose `success` — call if present
854
- ;(log as any).success?.(msg) ?? log.info(msg)
855
- }
856
- },
857
- debugSuccessLog: async (...args: any[]) => {
858
- if (await enablingPlatformLogging()) {
859
- if (await loggingIsDebug()) {
860
- const msg = formatArgs(args)
861
- ;(log as any).success?.(`[DEBUG] ${msg}`) ?? log.info(`[DEBUG] ${msg}`)
862
- }
863
- }
864
- },
865
- warnLog: async (...args: any[]) => {
866
- if (await enablingPlatformLogging()) {
867
- const msg = formatArgs(args)
868
- log.warn(msg)
869
- }
870
- },
871
- debugWarnLog: async (...args: any[]) => {
872
- if (await enablingPlatformLogging()) {
873
- if (await loggingIsDebug()) {
874
- const msg = formatArgs(args)
875
- log.warn(`[DEBUG] ${msg}`)
876
- }
877
- }
878
- },
879
- errorLog: async (...args: any[]) => {
880
- if (await enablingPlatformLogging()) {
881
- const msg = formatArgs(args)
882
- log.error(msg)
883
- }
884
- },
885
- debugErrorLog: async (...args: any[]) => {
886
- if (await enablingPlatformLogging()) {
887
- if (await loggingIsDebug()) {
888
- const msg = formatArgs(args)
889
- log.error(`[DEBUG] ${msg}`)
890
- }
891
- }
892
- },
893
- debugLog: async (...args: any[]) => {
894
- if (await enablingPlatformLogging()) {
895
- const pl = await getPL()
896
- if (pl === 'debug') {
897
- const msg = formatArgs(args)
898
- log.info(`[DEBUG] ${msg}`)
899
- } else if (pl === 'debugMode') {
900
- const msg = formatArgs(args)
901
- log.debug(msg)
902
- }
903
- }
904
- },
905
- loggingIsDebug,
906
- enablingPlatformLogging,
907
- }
908
- }
909
-
910
- /**
911
- * Create a Platform proxy class that selects between two platform constructors
912
- * (HAP vs Matter) at runtime using `detectMatter`. Returns a class suitable
913
- * for passing to `api.registerPlatform`.
914
- */
915
- export function createPlatformProxy(HAPCtor: any, MatterCtor: any) {
916
- return class PlatformProxy {
917
- delegate: any
918
-
919
- constructor(public readonly log: any, public readonly config: any, public readonly api: API) {
920
- const matterInfo = detectMatter(this.api)
921
- const isMatter = matterInfo.enabled
922
- const reason = matterInfo.reason ? ` Reason: ${matterInfo.reason}` : ''
923
- this.log.info?.(`Homebridge SwitchBot Plugin initializing in ${isMatter ? 'Matter' : 'HAP'} mode.`)
924
- this.log.debug?.(`Homebridge SwitchBot Plugin initializing in ${isMatter ? 'Matter' : 'HAP'} mode.${reason}`)
925
- const PlatformCtor = isMatter ? MatterCtor : HAPCtor
926
- this.delegate = new PlatformCtor(this.log, this.config, this.api)
927
- }
928
-
929
- configureAccessory(accessory: any): void {
930
- try {
931
- if (this.delegate && typeof this.delegate.configureAccessory === 'function') {
932
- return this.delegate.configureAccessory(accessory)
933
- }
934
- } catch (e) {
935
- // swallow — preserve previous behaviour where delegate errors don't bubble here
936
- }
937
- }
938
-
939
- configureMatterAccessory?(accessory: any): void {
940
- try {
941
- if (this.delegate && typeof this.delegate.configureMatterAccessory === 'function') {
942
- return this.delegate.configureMatterAccessory(accessory)
943
- }
944
- } catch (e) {
945
- // swallow — delegate may not implement this or may throw
946
- }
947
- }
948
-
949
- get accessories(): any {
950
- try {
951
- return this.delegate?.accessories
952
- } catch (e) {
953
- return undefined
954
- }
955
- }
956
-
957
- get matterAccessories(): any {
958
- try {
959
- return this.delegate?.matterAccessories
960
- } catch (e) {
961
- return undefined
962
- }
963
- }
964
- }
965
- }
966
-
967
- /**
968
- * API Request Tracker - Persistent tracking of SwitchBot API calls
969
- * Tracks requests per day with automatic midnight rollover
970
- */
971
- export class ApiRequestTracker {
972
- private count = 0
973
- private date = ''
974
- private statsFile = ''
975
- private hourlyTimer?: NodeJS.Timeout
976
- private midnightTimer?: NodeJS.Timeout
977
- private log: Logging
978
- // Daily limits
979
- private dailyLimit: number
980
- private reserveForCommands: number
981
- private lastWarn: Record<string, number> = {}
982
- private pausePollingAtReserve = false
983
- private resetAtLocalMidnight = false
984
-
985
- constructor(api: API, log: Logging, pluginName = 'SwitchBot', limits?: { dailyLimit?: number, reserveForCommands?: number, pausePollingAtReserve?: boolean, resetAtLocalMidnight?: boolean }) {
986
- this.log = log
987
- this.statsFile = join(api.user.storagePath(), `${pluginName.toLowerCase()}-api-stats.json`)
988
- this.dailyLimit = Math.max(0, Number(limits?.dailyLimit ?? 10000))
989
- this.reserveForCommands = Math.max(0, Number(limits?.reserveForCommands ?? 1000))
990
- this.pausePollingAtReserve = Boolean(limits?.pausePollingAtReserve ?? false)
991
- this.resetAtLocalMidnight = Boolean(limits?.resetAtLocalMidnight ?? false)
992
- this.load()
993
- }
994
-
995
- /**
996
- * Return date key string (YYYY-MM-DD) based on reset mode
997
- * - UTC (default): uses UTC date
998
- * - Local: uses local timezone date
999
- */
1000
- private dateKey(now: Date = new Date()): string {
1001
- if (!this.resetAtLocalMidnight) {
1002
- return now.toISOString().split('T')[0]
1003
- }
1004
- const y = now.getFullYear()
1005
- const m = (now.getMonth() + 1).toString().padStart(2, '0')
1006
- const d = now.getDate().toString().padStart(2, '0')
1007
- return `${y}-${m}-${d}`
1008
- }
1009
-
1010
- /**
1011
- * Load API request statistics from persistent storage
1012
- */
1013
- private load(): void {
1014
- try {
1015
- const today = this.dateKey()
1016
-
1017
- if (existsSync(this.statsFile)) {
1018
- const data = JSON.parse(readFileSync(this.statsFile, 'utf8'))
1019
-
1020
- // If it's a new day, reset the counter
1021
- if (data.date === today) {
1022
- this.count = data.count || 0
1023
- this.date = data.date
1024
- this.log.warn?.(`[API Stats] Loaded: ${this.count} requests today (${today})`)
1025
- } else {
1026
- this.log.error?.(`[API Stats] New day detected (${this.resetAtLocalMidnight ? 'local' : 'UTC'}). Previous: ${data.count || 0} requests on ${data.date}`)
1027
- this.count = 0
1028
- this.date = today
1029
- this.save()
1030
- }
1031
- } else {
1032
- this.log.debug?.('[API Stats] No existing stats file, starting fresh')
1033
- this.count = 0
1034
- this.date = today
1035
- this.save()
1036
- }
1037
- } catch (e: any) {
1038
- this.log.error?.(`[API Stats] Failed to load stats: ${e?.message ?? e}`)
1039
- this.count = 0
1040
- this.date = this.dateKey()
1041
- }
1042
- }
1043
-
1044
- /**
1045
- * Save API request statistics to persistent storage
1046
- */
1047
- private save(): void {
1048
- try {
1049
- const data = {
1050
- date: this.date,
1051
- count: this.count,
1052
- lastUpdated: new Date().toISOString(),
1053
- }
1054
- writeFileSync(this.statsFile, JSON.stringify(data, null, 2), 'utf8')
1055
- } catch (e: any) {
1056
- this.log.debug?.(`[API Stats] Failed to save stats: ${e?.message ?? e}`)
1057
- }
1058
- }
1059
-
1060
- /**
1061
- * Increment API request counter and save
1062
- */
1063
- public track(): void {
1064
- const today = this.dateKey()
1065
-
1066
- // Reset counter if it's a new day
1067
- if (this.date !== today) {
1068
- this.log.debug?.(`[API Stats] Day rollover: ${this.count} requests on ${this.date}`)
1069
- this.count = 0
1070
- this.date = today
1071
- }
1072
-
1073
- this.count++
1074
- this.save()
1075
- }
1076
-
1077
- /**
1078
- * Attempt to spend from the daily budget for a request of a given kind.
1079
- * Kinds: 'command' (user actions), 'poll' (status refresh), 'discovery'.
1080
- * Returns true if allowed (and increments the counter), false if blocked.
1081
- */
1082
- public trySpend(kind: 'command' | 'poll' | 'discovery', n = 1): boolean {
1083
- const today = this.dateKey()
1084
- if (this.date !== today) {
1085
- // Day rollover
1086
- this.log.debug?.(`[API Stats] Day rollover: ${this.count} requests on ${this.date}`)
1087
- this.count = 0
1088
- this.date = today
1089
- this.save()
1090
- }
1091
-
1092
- const softCap = Math.max(0, this.dailyLimit - this.reserveForCommands)
1093
- const projected = this.count + n
1094
- const now = Date.now()
1095
- const overHardCap = projected > this.dailyLimit
1096
- const overSoftCap = projected > softCap
1097
- const shouldRateLimit = (kind === 'command')
1098
- ? overHardCap
1099
- : (this.pausePollingAtReserve ? overSoftCap : overHardCap)
1100
-
1101
- if (shouldRateLimit) {
1102
- const warnKey = kind === 'command' ? 'hardcap' : 'softcap'
1103
- const last = this.lastWarn[warnKey] ?? 0
1104
- if (now - last > 10 * 60 * 1000) { // warn at most every 10 minutes
1105
- if (kind === 'command') {
1106
- this.log.error?.(`[API Stats] Daily limit (${this.dailyLimit}) reached. Blocking command requests until reset.`)
1107
- } else {
1108
- const remainingForCommands = Math.max(0, this.dailyLimit - this.count)
1109
- this.log.warn?.(`[API Stats] Near daily limit. Pausing ${kind} requests to reserve ~${this.reserveForCommands} calls for commands. Remaining today: ${remainingForCommands}`)
1110
- }
1111
- this.lastWarn[warnKey] = now
1112
- }
1113
- return false
1114
- }
1115
-
1116
- this.count += n
1117
- this.save()
1118
- return true
1119
- }
1120
-
1121
- /**
1122
- * Start hourly logging of API request count
1123
- */
1124
- public startHourlyLogging(): void {
1125
- // Log immediately on startup
1126
- const softCap = Math.max(0, this.dailyLimit - this.reserveForCommands)
1127
- const remaining = Math.max(0, this.dailyLimit - this.count)
1128
- const percentUsed = Math.round((this.count / this.dailyLimit) * 100)
1129
-
1130
- this.log.info?.(`[API Stats] Today (${this.date}): ${this.count}/${this.dailyLimit} API requests (${percentUsed}%), ${remaining} remaining`)
1131
- this.log.info?.(`[API Stats] Reset schedule: ${this.resetAtLocalMidnight ? 'local midnight' : 'UTC midnight'}`)
1132
-
1133
- if (this.count >= this.dailyLimit) {
1134
- this.log.warn?.('[API Stats] ⚠️ DAILY LIMIT REACHED - All API requests blocked until reset')
1135
- } else if (this.count >= softCap) {
1136
- this.log.warn?.(`[API Stats] ⚠️ NEAR LIMIT - Background polling paused, ${remaining} requests reserved for commands`)
1137
- }
1138
-
1139
- // Then log every hour
1140
- this.hourlyTimer = setInterval(() => {
1141
- const today = this.dateKey()
1142
- if (this.date !== today) {
1143
- // Day rollover
1144
- this.log.info?.(`[API Stats] Day rollover - Previous day (${this.date}): ${this.count} API requests`)
1145
- this.count = 0
1146
- this.date = today
1147
- this.save()
1148
- this.log.info?.('[API Stats] ✅ Polling resumed after daily reset')
1149
- }
1150
-
1151
- const remaining = Math.max(0, this.dailyLimit - this.count)
1152
- const percentUsed = Math.round((this.count / this.dailyLimit) * 100)
1153
- const softCap = Math.max(0, this.dailyLimit - this.reserveForCommands)
1154
-
1155
- // Provide context-aware status message
1156
- if (this.count >= this.dailyLimit) {
1157
- this.log.warn?.(`[API Stats] Today (${this.date}): ${this.count}/${this.dailyLimit} requests (${percentUsed}%) - ⚠️ LIMIT REACHED, all requests blocked`)
1158
- } else if (this.count >= softCap) {
1159
- this.log.warn?.(`[API Stats] Today (${this.date}): ${this.count}/${this.dailyLimit} requests (${percentUsed}%), ${remaining} remaining - polling paused`)
1160
- } else {
1161
- this.log.info?.(`[API Stats] Today (${this.date}): ${this.count}/${this.dailyLimit} requests (${percentUsed}%), ${remaining} remaining`)
1162
- }
1163
- }, 60 * 60 * 1000) // Every hour
1164
-
1165
- // Schedule an exact midnight rollover log/reset
1166
- this.scheduleMidnightRollover()
1167
- }
1168
-
1169
- /**
1170
- * Stop hourly logging
1171
- */
1172
- public stopHourlyLogging(): void {
1173
- if (this.hourlyTimer) {
1174
- clearInterval(this.hourlyTimer)
1175
- this.hourlyTimer = undefined
1176
- }
1177
- if (this.midnightTimer) {
1178
- clearTimeout(this.midnightTimer)
1179
- this.midnightTimer = undefined
1180
- }
1181
- }
1182
-
1183
- /**
1184
- * Get current count
1185
- */
1186
- public getCount(): number {
1187
- return this.count
1188
- }
1189
-
1190
- /**
1191
- * Get current date
1192
- */
1193
- public getDate(): string {
1194
- return this.date
1195
- }
1196
-
1197
- /** Schedule a precise log/reset at the next UTC midnight */
1198
- private scheduleMidnightRollover(): void {
1199
- try {
1200
- // Clear any previous timer
1201
- if (this.midnightTimer) {
1202
- clearTimeout(this.midnightTimer)
1203
- this.midnightTimer = undefined
1204
- }
1205
- const now = new Date()
1206
- let delay = 0
1207
- if (this.resetAtLocalMidnight) {
1208
- // Next local midnight
1209
- const next = new Date(now)
1210
- next.setHours(24, 0, 0, 0) // rolls to next day at 00:00 local
1211
- delay = Math.max(1000, next.getTime() - now.getTime())
1212
- } else {
1213
- // Next UTC midnight
1214
- const nextUtcMidnightMs = Date.UTC(
1215
- now.getUTCFullYear(),
1216
- now.getUTCMonth(),
1217
- now.getUTCDate() + 1,
1218
- 0,
1219
- 0,
1220
- 0,
1221
- 0,
1222
- )
1223
- delay = Math.max(1000, nextUtcMidnightMs - now.getTime())
1224
- }
1225
- this.midnightTimer = setTimeout(() => {
1226
- try {
1227
- const today = this.dateKey()
1228
- if (this.date !== today) {
1229
- this.log.info?.(`[API Stats] 🌙 Day rollover - Previous day (${this.date}): ${this.count} API requests`)
1230
- this.count = 0
1231
- this.date = today
1232
- this.save()
1233
- }
1234
- // Emit the precise resume line and a fresh today counter line
1235
- this.log.info?.('[API Stats] ✅ Daily API counter reset - Polling resumed')
1236
- this.log.info?.(`[API Stats] Today (${this.date}): ${this.count}/${this.dailyLimit} API requests`)
1237
- } catch {}
1238
- // Reschedule for the next midnight
1239
- this.scheduleMidnightRollover()
1240
- }, delay)
1241
- } catch {}
1242
- }
1243
- }
1244
-
1245
- /**
1246
- * Normalize a deviceId for matching (uppercase alphanumerics only)
1247
- */
1248
- export function normalizeDeviceId(deviceId: string): string {
1249
- return (deviceId ?? '').toUpperCase().replace(/[^A-Z0-9]+/g, '')
1250
- }
1251
-
1252
- /**
1253
- * Merge two arrays by deviceId. For each item in a1 (user-provided devices list),
1254
- * find matching item in a2 (discovered devices) and merge them with user overrides last.
1255
- */
1256
- export function mergeByDeviceId(a1: { deviceId: string }[], a2: any[], allowConfigOnly = false): any[] {
1257
- const result: any[] = []
1258
- for (const itm of (a1 || [])) {
1259
- const matchingItem = (a2 || []).find(item => normalizeDeviceId(item.deviceId) === normalizeDeviceId(itm.deviceId))
1260
- if (matchingItem) {
1261
- result.push({ ...matchingItem, ...itm })
1262
- } else if (allowConfigOnly) {
1263
- result.push(itm)
1264
- }
1265
- }
1266
- return result
1267
- }
1268
-
1269
- /**
1270
- * Apply device-type or remote-type templates to an array of devices.
1271
- * Templates are config entries with applyToAllDevicesOfType=true.
1272
- *
1273
- * @param devices - Array of devices to apply templates to
1274
- * @param configDevices - User config array that may contain template entries
1275
- * @param typeKey - Property name to match device types ('deviceType' for devices, 'remoteType' for IR devices)
1276
- * @param debugLog - Optional debug logging function
1277
- * @returns Array of devices with templates applied
1278
- */
1279
- export function applyDeviceTypeTemplates(
1280
- devices: any[],
1281
- configDevices: any[],
1282
- typeKey: string,
1283
- debugLog?: (message: string) => void,
1284
- ): any[] {
1285
- // Build a map of device-type templates from config devices with applyToAllDevicesOfType=true
1286
- const typeTemplates = new Map<string, any>()
1287
-
1288
- for (const configDevice of configDevices || []) {
1289
- if (configDevice.applyToAllDevicesOfType) {
1290
- // Get the type value from multiple possible sources
1291
- const deviceType = configDevice[typeKey] || (configDevice as any).configDeviceType || (configDevice as any).configRemoteType
1292
- if (!deviceType) {
1293
- continue
1294
- }
1295
-
1296
- // Store all config properties except deviceId and applyToAllDevicesOfType flag
1297
- const template: any = { ...configDevice }
1298
- delete template.deviceId
1299
- delete template.applyToAllDevicesOfType
1300
-
1301
- typeTemplates.set(deviceType, template)
1302
- if (debugLog) {
1303
- debugLog(`Device type template found for '${deviceType}': ${JSON.stringify(template)}`)
1304
- }
1305
- }
1306
- }
1307
-
1308
- // If no templates found, return original array
1309
- if (typeTemplates.size === 0) {
1310
- return devices
1311
- }
1312
-
1313
- // Apply templates to devices
1314
- return devices.map((device) => {
1315
- const deviceType = device[typeKey] || (device as any).configDeviceType || (device as any).configRemoteType
1316
- const template = typeTemplates.get(deviceType)
1317
-
1318
- if (template) {
1319
- if (debugLog) {
1320
- debugLog(`Applying device type template to ${device.deviceId} (${deviceType})`)
1321
- }
1322
- // Template settings go first, then device data (device data takes precedence)
1323
- return Object.assign({}, template, device)
1324
- }
1325
-
1326
- return device
1327
- })
1328
- }
1329
-
1330
- /**
1331
- * Check if an API status code indicates success
1332
- */
1333
- export function isSuccessfulStatusCode(statusCode: number): boolean {
1334
- return statusCode === 200 || statusCode === 100
1335
- }
1336
-
1337
- /**
1338
- * Log status code messages with appropriate log level
1339
- */
1340
- export async function logStatusCode(statusCode: number, log: {
1341
- debugLog: (...args: any[]) => void | Promise<void>
1342
- errorLog: (...args: any[]) => void | Promise<void>
1343
- }): Promise<void> {
1344
- const messages: { [key: number]: string } = {
1345
- 151: `Command not supported by this device type, statusCode: ${statusCode}, Submit Feature Request Here:
1346
- https://tinyurl.com/SwitchBotFeatureRequest`,
1347
- 152: `Device not found, statusCode: ${statusCode}`,
1348
- 160: `Command is not supported, statusCode: ${statusCode}, Submit Bugs Here: https://tinyurl.com/SwitchBotBug`,
1349
- 161: `Device is offline, statusCode: ${statusCode}`,
1350
- 171: `is offline, statusCode: ${statusCode}`,
1351
- 190: `Requests reached the daily limit, statusCode: ${statusCode}`,
1352
- 100: `Command successfully sent, statusCode: ${statusCode}`,
1353
- 200: `Request successful, statusCode: ${statusCode}`,
1354
- 400: `Bad Request, The client has issued an invalid request. This is commonly used to specify validation errors in a request payload,
1355
- statusCode: ${statusCode}`,
1356
- 401: `Unauthorized, Authorization for the API is required, but the request has not been authenticated, statusCode: ${statusCode}`,
1357
- 403: `Forbidden, The request has been authenticated but does not have appropriate permissions, or a requested resource is not found,
1358
- statusCode: ${statusCode}`,
1359
- 404: `Not Found, Specifies the requested path does not exist, statusCode: ${statusCode}`,
1360
- 406: `Not Acceptable, The client has requested a MIME type via the Accept header for a value not supported by the server,
1361
- statusCode: ${statusCode}`,
1362
- 415: `Unsupported Media Type, The client has defined a contentType header that is not supported by the server, statusCode: ${statusCode}`,
1363
- 422: `Unprocessable Entity, The client has made a valid request, but the server cannot process it. This is often used for APIs for which
1364
- certain limits have been exceeded, statusCode: ${statusCode}`,
1365
- 429: `Too Many Requests, The client has exceeded the number of requests allowed for a given time window, statusCode: ${statusCode}`,
1366
- 500: `Internal Server Error, An unexpected error on the SmartThings servers has occurred. These errors should be rare,
1367
- statusCode: ${statusCode}`,
1368
- }
1369
-
1370
- const message = messages[statusCode] ?? `Unknown statusCode, statusCode: ${statusCode}, Submit Bugs Here: https://tinyurl.com/SwitchBotBug`
1371
-
1372
- if ([100, 200].includes(statusCode)) {
1373
- await log.debugLog(message)
1374
- } else {
1375
- await log.errorLog(message)
1376
- }
1377
- }
1378
-
1379
- /**
1380
- * Shared device logging helpers
1381
- */
1382
-
1383
- /**
1384
- * Check if device logging is in debug mode
1385
- */
1386
- export function deviceLoggingIsDebug(deviceLogging?: string): boolean {
1387
- return deviceLogging === 'debugMode' || deviceLogging === 'debug'
1388
- }
1389
-
1390
- /**
1391
- * Check if device logging is enabled
1392
- */
1393
- export function deviceLoggingEnabled(deviceLogging?: string, platformLogging?: string): boolean {
1394
- // If deviceLogging isn't provided, fall back to platform-wide flag
1395
- if (deviceLogging === undefined || deviceLogging === '') {
1396
- return platformLogging === 'debugMode' || platformLogging === 'debug' || platformLogging === 'standard'
1397
- }
1398
- return deviceLogging === 'debugMode' || deviceLogging === 'debug' || deviceLogging === 'standard'
1399
- }
1400
-
1401
- /**
1402
- * Device status code handler with comprehensive messages
1403
- */
1404
- export interface DeviceStatusCodeLogger {
1405
- debugLog: (...args: any[]) => void | Promise<void>
1406
- debugErrorLog?: (...args: any[]) => void | Promise<void>
1407
- errorLog: (...args: any[]) => void | Promise<void>
1408
- infoLog?: (...args: any[]) => void | Promise<void>
1409
- }
1410
-
1411
- export async function logDeviceStatusCode(
1412
- statusCode: number,
1413
- log: DeviceStatusCodeLogger,
1414
- deviceId?: string,
1415
- hubDeviceId?: string,
1416
- ): Promise<void> {
1417
- let adjustedStatusCode = statusCode
1418
-
1419
- // Handle special case where device is its own hub
1420
- if (statusCode === 171 && hubDeviceId && deviceId && (hubDeviceId === deviceId || hubDeviceId === '000000000000')) {
1421
- if (log.debugErrorLog) {
1422
- log.debugErrorLog(`statusCode 171 changed to 161: hubDeviceId ${hubDeviceId} matches deviceId ${deviceId}, device is its own hub.`)
1423
- }
1424
- adjustedStatusCode = 161
1425
- }
1426
-
1427
- const statusMessages: { [key: number]: string } = {
1428
- 151: 'Command not supported by this device type',
1429
- 152: 'Device not found',
1430
- 160: 'Command is not supported',
1431
- 161: 'Device is offline',
1432
- 171: hubDeviceId ? `Hub Device is offline. Hub: ${hubDeviceId}` : 'Hub Device is offline',
1433
- 190: 'Device internal error due to device states not synchronized with server, or command format is invalid',
1434
- 100: 'Command successfully sent',
1435
- 200: 'Request successful',
1436
- 400: 'Bad Request, an invalid payload request',
1437
- 401: 'Unauthorized, Authorization for the API is required, but the request has not been authenticated',
1438
- 403: 'Forbidden, The request has been authenticated but does not have appropriate permissions, or a requested resource is not found',
1439
- 404: 'Not Found, Specifies the requested path does not exist',
1440
- 406: 'Not Acceptable, a MIME type has been requested via the Accept header for a value not supported by the server',
1441
- 415: 'Unsupported Media Type, a contentType header has been defined that is not supported by the server',
1442
- 422: 'Unprocessable Entity: The server cannot process the request, often due to exceeded API limits.',
1443
- 429: 'Too Many Requests, exceeded the number of requests allowed for a given time window',
1444
- 500: 'Internal Server Error, An unexpected error occurred. These errors should be rare',
1445
- }
1446
-
1447
- const logMessage = statusMessages[adjustedStatusCode] || `Unknown statusCode: ${adjustedStatusCode}, Submit Bugs Here: https://tinyurl.com/SwitchBotBug`
1448
- const fullMessage = `${logMessage}, statusCode: ${adjustedStatusCode}`
1449
-
1450
- if ([100, 200].includes(adjustedStatusCode)) {
1451
- await log.debugLog(fullMessage)
1452
- } else if (statusMessages[adjustedStatusCode]) {
1453
- await log.errorLog(fullMessage)
1454
- } else if (log.infoLog) {
1455
- await log.infoLog(fullMessage)
1456
- } else {
1457
- await log.errorLog(fullMessage)
1458
- }
1459
50
  }