@switchbot/homebridge-switchbot 5.0.0-beta.1 → 5.0.0-beta.100

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 (448) hide show
  1. package/.github/ISSUE_TEMPLATE/e2e-verification.md +36 -0
  2. package/.github/workflows/ci.yml +32 -0
  3. package/.github/workflows/manual-e2e.yml +115 -0
  4. package/.github/workflows/release.yml +0 -4
  5. package/CHANGELOG.md +35 -0
  6. package/E2E-VERIFICATION.md +121 -0
  7. package/MIGRATION.md +44 -0
  8. package/README.md +56 -3
  9. package/config.schema.json +91 -14787
  10. package/dist/deviceFactory.d.ts +13 -0
  11. package/dist/deviceFactory.d.ts.map +1 -0
  12. package/dist/deviceFactory.js +81 -0
  13. package/dist/deviceFactory.js.map +1 -0
  14. package/dist/devices/deviceBase.d.ts +50 -0
  15. package/dist/devices/deviceBase.d.ts.map +1 -0
  16. package/dist/devices/deviceBase.js +119 -0
  17. package/dist/devices/deviceBase.js.map +1 -0
  18. package/dist/devices/genericDevice.d.ts +298 -0
  19. package/dist/devices/genericDevice.d.ts.map +1 -0
  20. package/dist/devices/genericDevice.js +1065 -0
  21. package/dist/devices/genericDevice.js.map +1 -0
  22. package/dist/homebridge-ui/public/index.html +636 -246
  23. package/dist/homebridge-ui/server.d.ts +3 -1
  24. package/dist/homebridge-ui/server.d.ts.map +1 -1
  25. package/dist/homebridge-ui/server.js +467 -36
  26. package/dist/homebridge-ui/server.js.map +1 -1
  27. package/dist/index.d.ts.map +1 -1
  28. package/dist/index.js +4 -7
  29. package/dist/index.js.map +1 -1
  30. package/dist/platform.d.ts +35 -0
  31. package/dist/platform.d.ts.map +1 -0
  32. package/dist/platform.js +1562 -0
  33. package/dist/platform.js.map +1 -0
  34. package/dist/settings.d.ts +10 -249
  35. package/dist/settings.d.ts.map +1 -1
  36. package/dist/settings.js +5 -30
  37. package/dist/settings.js.map +1 -1
  38. package/dist/switchbotClient.d.ts +32 -0
  39. package/dist/switchbotClient.d.ts.map +1 -0
  40. package/dist/switchbotClient.js +194 -0
  41. package/dist/switchbotClient.js.map +1 -0
  42. package/dist/utils.d.ts +39 -50
  43. package/dist/utils.d.ts.map +1 -1
  44. package/dist/utils.js +39 -688
  45. package/dist/utils.js.map +1 -1
  46. package/docs/assets/highlight.css +14 -0
  47. package/docs/assets/icons.js +1 -1
  48. package/docs/assets/icons.svg +1 -1
  49. package/docs/assets/main.js +2 -2
  50. package/docs/assets/style.css +3 -3
  51. package/docs/index.html +77 -13
  52. package/docs/variables/default.html +1 -1
  53. package/eslint.config.js +2 -8
  54. package/package.json +21 -28
  55. package/scripts/e2e/README.md +25 -0
  56. package/scripts/e2e/curtain-e2e.sh +70 -0
  57. package/scripts/e2e/fan-e2e.sh +75 -0
  58. package/scripts/e2e/light-advanced-e2e.sh +97 -0
  59. package/scripts/e2e/light-e2e.sh +75 -0
  60. package/scripts/e2e/list-accessories.sh +19 -0
  61. package/scripts/e2e/lock-e2e.sh +65 -0
  62. package/scripts/generate-matter-maps.js +60 -0
  63. package/scripts/run-e2e-local.sh +14 -0
  64. package/src/deviceFactory.ts +122 -0
  65. package/src/devices/deviceBase.ts +141 -0
  66. package/src/devices/genericDevice.ts +995 -0
  67. package/src/homebridge-ui/public/index.html +636 -246
  68. package/src/homebridge-ui/server.ts +552 -41
  69. package/src/index.ts +4 -7
  70. package/src/platform.ts +1561 -0
  71. package/src/settings.ts +12 -277
  72. package/src/switchbotClient.ts +203 -0
  73. package/src/utils.ts +45 -713
  74. package/test/accessory-restore.spec.ts +73 -0
  75. package/test/device-mapping.spec.ts +37 -0
  76. package/test/deviceFactory.spec.ts +18 -0
  77. package/test/e2e/run-e2e.spec.ts +50 -0
  78. package/test/fan-swing.spec.ts +29 -0
  79. package/test/helpers/matter-harness.ts +53 -0
  80. package/test/lock-users.spec.ts +44 -0
  81. package/test/matter-childbridge.spec.ts +55 -0
  82. package/test/matter-descriptors.spec.ts +97 -0
  83. package/test/matter-device-state.spec.ts +101 -0
  84. package/test/matter-integration.spec.ts +70 -0
  85. package/test/platform.integration.spec.ts +55 -0
  86. package/test/switchbot-client-debounce.spec.ts +131 -0
  87. package/test/switchbot-client-openapi.spec.ts +56 -0
  88. package/test/switchbotClient.spec.ts +10 -0
  89. package/test/utils.spec.ts +20 -0
  90. package/vitest.config.ts +7 -0
  91. package/coverage/base.css +0 -224
  92. package/coverage/block-navigation.js +0 -87
  93. package/coverage/clover.xml +0 -15847
  94. package/coverage/coverage-final.json +0 -42
  95. package/coverage/docs/assets/dmt/dmt-component-data.js.html +0 -85
  96. package/coverage/docs/assets/dmt/dmt-components.js.html +0 -286
  97. package/coverage/docs/assets/dmt/index.html +0 -131
  98. package/coverage/docs/assets/hierarchy.js.html +0 -85
  99. package/coverage/docs/assets/icons.js.html +0 -136
  100. package/coverage/docs/assets/index.html +0 -146
  101. package/coverage/docs/assets/main.js.html +0 -265
  102. package/coverage/favicon.png +0 -0
  103. package/coverage/index.html +0 -191
  104. package/coverage/prettify.css +0 -1
  105. package/coverage/prettify.js +0 -2
  106. package/coverage/sort-arrow-sprite.png +0 -0
  107. package/coverage/sorter.js +0 -196
  108. package/coverage/src/device/blindtilt.ts.html +0 -3238
  109. package/coverage/src/device/bot.ts.html +0 -2803
  110. package/coverage/src/device/ceilinglight.ts.html +0 -2338
  111. package/coverage/src/device/colorbulb.ts.html +0 -2824
  112. package/coverage/src/device/contact.ts.html +0 -1465
  113. package/coverage/src/device/curtain.ts.html +0 -2869
  114. package/coverage/src/device/device.ts.html +0 -2500
  115. package/coverage/src/device/fan.ts.html +0 -2242
  116. package/coverage/src/device/hub.ts.html +0 -1408
  117. package/coverage/src/device/humidifier.ts.html +0 -2116
  118. package/coverage/src/device/index.html +0 -416
  119. package/coverage/src/device/iosensor.ts.html +0 -1375
  120. package/coverage/src/device/lightstrip.ts.html +0 -2617
  121. package/coverage/src/device/lock.ts.html +0 -1963
  122. package/coverage/src/device/meter.ts.html +0 -1372
  123. package/coverage/src/device/meterplus.ts.html +0 -1384
  124. package/coverage/src/device/meterpro.ts.html +0 -1618
  125. package/coverage/src/device/motion.ts.html +0 -1264
  126. package/coverage/src/device/plug.ts.html +0 -1372
  127. package/coverage/src/device/relayswitch.ts.html +0 -2284
  128. package/coverage/src/device/robotvacuumcleaner.ts.html +0 -1810
  129. package/coverage/src/device/waterdetector.ts.html +0 -1294
  130. package/coverage/src/homebridge-ui/index.html +0 -116
  131. package/coverage/src/homebridge-ui/server.ts.html +0 -229
  132. package/coverage/src/index.html +0 -161
  133. package/coverage/src/index.ts.html +0 -124
  134. package/coverage/src/irdevice/airconditioner.ts.html +0 -1687
  135. package/coverage/src/irdevice/airpurifier.ts.html +0 -844
  136. package/coverage/src/irdevice/camera.ts.html +0 -475
  137. package/coverage/src/irdevice/fan.ts.html +0 -766
  138. package/coverage/src/irdevice/index.html +0 -251
  139. package/coverage/src/irdevice/irdevice.ts.html +0 -1117
  140. package/coverage/src/irdevice/light.ts.html +0 -826
  141. package/coverage/src/irdevice/other.ts.html +0 -2458
  142. package/coverage/src/irdevice/tv.ts.html +0 -1222
  143. package/coverage/src/irdevice/vacuumcleaner.ts.html +0 -466
  144. package/coverage/src/irdevice/waterheater.ts.html +0 -469
  145. package/coverage/src/platform.ts.html +0 -8776
  146. package/coverage/src/settings.ts.html +0 -934
  147. package/coverage/src/utils.ts.html +0 -2092
  148. package/dist/devices-hap/airpurifier.d.ts +0 -54
  149. package/dist/devices-hap/airpurifier.d.ts.map +0 -1
  150. package/dist/devices-hap/airpurifier.js +0 -527
  151. package/dist/devices-hap/airpurifier.js.map +0 -1
  152. package/dist/devices-hap/blindtilt.d.ts +0 -90
  153. package/dist/devices-hap/blindtilt.d.ts.map +0 -1
  154. package/dist/devices-hap/blindtilt.js +0 -974
  155. package/dist/devices-hap/blindtilt.js.map +0 -1
  156. package/dist/devices-hap/bot.d.ts +0 -102
  157. package/dist/devices-hap/bot.d.ts.map +0 -1
  158. package/dist/devices-hap/bot.js +0 -811
  159. package/dist/devices-hap/bot.js.map +0 -1
  160. package/dist/devices-hap/ceilinglight.d.ts +0 -85
  161. package/dist/devices-hap/ceilinglight.d.ts.map +0 -1
  162. package/dist/devices-hap/ceilinglight.js +0 -701
  163. package/dist/devices-hap/ceilinglight.js.map +0 -1
  164. package/dist/devices-hap/colorbulb.d.ts +0 -88
  165. package/dist/devices-hap/colorbulb.d.ts.map +0 -1
  166. package/dist/devices-hap/colorbulb.js +0 -881
  167. package/dist/devices-hap/colorbulb.js.map +0 -1
  168. package/dist/devices-hap/contact.d.ts +0 -44
  169. package/dist/devices-hap/contact.d.ts.map +0 -1
  170. package/dist/devices-hap/contact.js +0 -409
  171. package/dist/devices-hap/contact.js.map +0 -1
  172. package/dist/devices-hap/curtain.d.ts +0 -73
  173. package/dist/devices-hap/curtain.d.ts.map +0 -1
  174. package/dist/devices-hap/curtain.js +0 -869
  175. package/dist/devices-hap/curtain.js.map +0 -1
  176. package/dist/devices-hap/device.d.ts +0 -98
  177. package/dist/devices-hap/device.d.ts.map +0 -1
  178. package/dist/devices-hap/device.js +0 -749
  179. package/dist/devices-hap/device.js.map +0 -1
  180. package/dist/devices-hap/fan.d.ts +0 -69
  181. package/dist/devices-hap/fan.d.ts.map +0 -1
  182. package/dist/devices-hap/fan.js +0 -649
  183. package/dist/devices-hap/fan.js.map +0 -1
  184. package/dist/devices-hap/hub.d.ts +0 -37
  185. package/dist/devices-hap/hub.d.ts.map +0 -1
  186. package/dist/devices-hap/hub.js +0 -392
  187. package/dist/devices-hap/hub.js.map +0 -1
  188. package/dist/devices-hap/humidifier.d.ts +0 -68
  189. package/dist/devices-hap/humidifier.d.ts.map +0 -1
  190. package/dist/devices-hap/humidifier.js +0 -628
  191. package/dist/devices-hap/humidifier.js.map +0 -1
  192. package/dist/devices-hap/iosensor.d.ts +0 -42
  193. package/dist/devices-hap/iosensor.d.ts.map +0 -1
  194. package/dist/devices-hap/iosensor.js +0 -382
  195. package/dist/devices-hap/iosensor.js.map +0 -1
  196. package/dist/devices-hap/lightstrip.d.ts +0 -79
  197. package/dist/devices-hap/lightstrip.d.ts.map +0 -1
  198. package/dist/devices-hap/lightstrip.js +0 -797
  199. package/dist/devices-hap/lightstrip.js.map +0 -1
  200. package/dist/devices-hap/lock.d.ts +0 -53
  201. package/dist/devices-hap/lock.d.ts.map +0 -1
  202. package/dist/devices-hap/lock.js +0 -561
  203. package/dist/devices-hap/lock.js.map +0 -1
  204. package/dist/devices-hap/meter.d.ts +0 -37
  205. package/dist/devices-hap/meter.d.ts.map +0 -1
  206. package/dist/devices-hap/meter.js +0 -379
  207. package/dist/devices-hap/meter.js.map +0 -1
  208. package/dist/devices-hap/meterplus.d.ts +0 -42
  209. package/dist/devices-hap/meterplus.d.ts.map +0 -1
  210. package/dist/devices-hap/meterplus.js +0 -384
  211. package/dist/devices-hap/meterplus.js.map +0 -1
  212. package/dist/devices-hap/meterpro.d.ts +0 -43
  213. package/dist/devices-hap/meterpro.d.ts.map +0 -1
  214. package/dist/devices-hap/meterpro.js +0 -468
  215. package/dist/devices-hap/meterpro.js.map +0 -1
  216. package/dist/devices-hap/motion.d.ts +0 -42
  217. package/dist/devices-hap/motion.d.ts.map +0 -1
  218. package/dist/devices-hap/motion.js +0 -345
  219. package/dist/devices-hap/motion.js.map +0 -1
  220. package/dist/devices-hap/plug.d.ts +0 -49
  221. package/dist/devices-hap/plug.d.ts.map +0 -1
  222. package/dist/devices-hap/plug.js +0 -395
  223. package/dist/devices-hap/plug.js.map +0 -1
  224. package/dist/devices-hap/relayswitch.d.ts +0 -96
  225. package/dist/devices-hap/relayswitch.d.ts.map +0 -1
  226. package/dist/devices-hap/relayswitch.js +0 -642
  227. package/dist/devices-hap/relayswitch.js.map +0 -1
  228. package/dist/devices-hap/robotvacuumcleaner.d.ts +0 -54
  229. package/dist/devices-hap/robotvacuumcleaner.d.ts.map +0 -1
  230. package/dist/devices-hap/robotvacuumcleaner.js +0 -523
  231. package/dist/devices-hap/robotvacuumcleaner.js.map +0 -1
  232. package/dist/devices-hap/waterdetector.d.ts +0 -41
  233. package/dist/devices-hap/waterdetector.d.ts.map +0 -1
  234. package/dist/devices-hap/waterdetector.js +0 -356
  235. package/dist/devices-hap/waterdetector.js.map +0 -1
  236. package/dist/devices-matter/BaseMatterAccessory.d.ts +0 -63
  237. package/dist/devices-matter/BaseMatterAccessory.d.ts.map +0 -1
  238. package/dist/devices-matter/BaseMatterAccessory.js +0 -100
  239. package/dist/devices-matter/BaseMatterAccessory.js.map +0 -1
  240. package/dist/devices-matter/ColorLightAccessory.d.ts +0 -18
  241. package/dist/devices-matter/ColorLightAccessory.d.ts.map +0 -1
  242. package/dist/devices-matter/ColorLightAccessory.js +0 -96
  243. package/dist/devices-matter/ColorLightAccessory.js.map +0 -1
  244. package/dist/devices-matter/ColorTemperatureLightAccessory.d.ts +0 -16
  245. package/dist/devices-matter/ColorTemperatureLightAccessory.d.ts.map +0 -1
  246. package/dist/devices-matter/ColorTemperatureLightAccessory.js +0 -83
  247. package/dist/devices-matter/ColorTemperatureLightAccessory.js.map +0 -1
  248. package/dist/devices-matter/ContactSensorAccessory.d.ts +0 -10
  249. package/dist/devices-matter/ContactSensorAccessory.d.ts.map +0 -1
  250. package/dist/devices-matter/ContactSensorAccessory.js +0 -31
  251. package/dist/devices-matter/ContactSensorAccessory.js.map +0 -1
  252. package/dist/devices-matter/DimmableLightAccessory.d.ts +0 -56
  253. package/dist/devices-matter/DimmableLightAccessory.d.ts.map +0 -1
  254. package/dist/devices-matter/DimmableLightAccessory.js +0 -170
  255. package/dist/devices-matter/DimmableLightAccessory.js.map +0 -1
  256. package/dist/devices-matter/DoorLockAccessory.d.ts +0 -12
  257. package/dist/devices-matter/DoorLockAccessory.d.ts.map +0 -1
  258. package/dist/devices-matter/DoorLockAccessory.js +0 -47
  259. package/dist/devices-matter/DoorLockAccessory.js.map +0 -1
  260. package/dist/devices-matter/ExtendedColorLightAccessory.d.ts +0 -19
  261. package/dist/devices-matter/ExtendedColorLightAccessory.d.ts.map +0 -1
  262. package/dist/devices-matter/ExtendedColorLightAccessory.js +0 -108
  263. package/dist/devices-matter/ExtendedColorLightAccessory.js.map +0 -1
  264. package/dist/devices-matter/FanAccessory.d.ts +0 -14
  265. package/dist/devices-matter/FanAccessory.d.ts.map +0 -1
  266. package/dist/devices-matter/FanAccessory.js +0 -73
  267. package/dist/devices-matter/FanAccessory.js.map +0 -1
  268. package/dist/devices-matter/HumiditySensorAccessory.d.ts +0 -10
  269. package/dist/devices-matter/HumiditySensorAccessory.d.ts.map +0 -1
  270. package/dist/devices-matter/HumiditySensorAccessory.js +0 -33
  271. package/dist/devices-matter/HumiditySensorAccessory.js.map +0 -1
  272. package/dist/devices-matter/LeakSensorAccessory.d.ts +0 -10
  273. package/dist/devices-matter/LeakSensorAccessory.d.ts.map +0 -1
  274. package/dist/devices-matter/LeakSensorAccessory.js +0 -30
  275. package/dist/devices-matter/LeakSensorAccessory.js.map +0 -1
  276. package/dist/devices-matter/LightSensorAccessory.d.ts +0 -10
  277. package/dist/devices-matter/LightSensorAccessory.d.ts.map +0 -1
  278. package/dist/devices-matter/LightSensorAccessory.js +0 -33
  279. package/dist/devices-matter/LightSensorAccessory.js.map +0 -1
  280. package/dist/devices-matter/OccupancySensorAccessory.d.ts +0 -10
  281. package/dist/devices-matter/OccupancySensorAccessory.d.ts.map +0 -1
  282. package/dist/devices-matter/OccupancySensorAccessory.js +0 -43
  283. package/dist/devices-matter/OccupancySensorAccessory.js.map +0 -1
  284. package/dist/devices-matter/OnOffLightAccessory.d.ts +0 -36
  285. package/dist/devices-matter/OnOffLightAccessory.d.ts.map +0 -1
  286. package/dist/devices-matter/OnOffLightAccessory.js +0 -117
  287. package/dist/devices-matter/OnOffLightAccessory.js.map +0 -1
  288. package/dist/devices-matter/OnOffOutletAccessory.d.ts +0 -12
  289. package/dist/devices-matter/OnOffOutletAccessory.d.ts.map +0 -1
  290. package/dist/devices-matter/OnOffOutletAccessory.js +0 -43
  291. package/dist/devices-matter/OnOffOutletAccessory.js.map +0 -1
  292. package/dist/devices-matter/OnOffSwitchAccessory.d.ts +0 -12
  293. package/dist/devices-matter/OnOffSwitchAccessory.d.ts.map +0 -1
  294. package/dist/devices-matter/OnOffSwitchAccessory.js +0 -43
  295. package/dist/devices-matter/OnOffSwitchAccessory.js.map +0 -1
  296. package/dist/devices-matter/RoboticVacuumAccessory.d.ts +0 -66
  297. package/dist/devices-matter/RoboticVacuumAccessory.d.ts.map +0 -1
  298. package/dist/devices-matter/RoboticVacuumAccessory.js +0 -402
  299. package/dist/devices-matter/RoboticVacuumAccessory.js.map +0 -1
  300. package/dist/devices-matter/SmokeCOAlarmAccessory.d.ts +0 -11
  301. package/dist/devices-matter/SmokeCOAlarmAccessory.d.ts.map +0 -1
  302. package/dist/devices-matter/SmokeCOAlarmAccessory.js +0 -49
  303. package/dist/devices-matter/SmokeCOAlarmAccessory.js.map +0 -1
  304. package/dist/devices-matter/TemperatureSensorAccessory.d.ts +0 -10
  305. package/dist/devices-matter/TemperatureSensorAccessory.d.ts.map +0 -1
  306. package/dist/devices-matter/TemperatureSensorAccessory.js +0 -33
  307. package/dist/devices-matter/TemperatureSensorAccessory.js.map +0 -1
  308. package/dist/devices-matter/ThermostatAccessory.d.ts +0 -17
  309. package/dist/devices-matter/ThermostatAccessory.d.ts.map +0 -1
  310. package/dist/devices-matter/ThermostatAccessory.js +0 -87
  311. package/dist/devices-matter/ThermostatAccessory.js.map +0 -1
  312. package/dist/devices-matter/VenetianBlindAccessory.d.ts +0 -17
  313. package/dist/devices-matter/VenetianBlindAccessory.d.ts.map +0 -1
  314. package/dist/devices-matter/VenetianBlindAccessory.js +0 -100
  315. package/dist/devices-matter/VenetianBlindAccessory.js.map +0 -1
  316. package/dist/devices-matter/WindowBlindAccessory.d.ts +0 -15
  317. package/dist/devices-matter/WindowBlindAccessory.d.ts.map +0 -1
  318. package/dist/devices-matter/WindowBlindAccessory.js +0 -81
  319. package/dist/devices-matter/WindowBlindAccessory.js.map +0 -1
  320. package/dist/devices-matter/custom/PowerStripAccessory.d.ts +0 -97
  321. package/dist/devices-matter/custom/PowerStripAccessory.d.ts.map +0 -1
  322. package/dist/devices-matter/custom/PowerStripAccessory.js +0 -265
  323. package/dist/devices-matter/custom/PowerStripAccessory.js.map +0 -1
  324. package/dist/devices-matter/custom/index.d.ts +0 -8
  325. package/dist/devices-matter/custom/index.d.ts.map +0 -1
  326. package/dist/devices-matter/custom/index.js +0 -8
  327. package/dist/devices-matter/custom/index.js.map +0 -1
  328. package/dist/devices-matter/index.d.ts +0 -29
  329. package/dist/devices-matter/index.d.ts.map +0 -1
  330. package/dist/devices-matter/index.js +0 -28
  331. package/dist/devices-matter/index.js.map +0 -1
  332. package/dist/index.test.d.ts +0 -2
  333. package/dist/index.test.d.ts.map +0 -1
  334. package/dist/index.test.js +0 -14
  335. package/dist/index.test.js.map +0 -1
  336. package/dist/irdevice/airconditioner.d.ts +0 -61
  337. package/dist/irdevice/airconditioner.d.ts.map +0 -1
  338. package/dist/irdevice/airconditioner.js +0 -472
  339. package/dist/irdevice/airconditioner.js.map +0 -1
  340. package/dist/irdevice/airpurifier.d.ts +0 -50
  341. package/dist/irdevice/airpurifier.d.ts.map +0 -1
  342. package/dist/irdevice/airpurifier.js +0 -213
  343. package/dist/irdevice/airpurifier.js.map +0 -1
  344. package/dist/irdevice/camera.d.ts +0 -32
  345. package/dist/irdevice/camera.d.ts.map +0 -1
  346. package/dist/irdevice/camera.js +0 -107
  347. package/dist/irdevice/camera.js.map +0 -1
  348. package/dist/irdevice/fan.d.ts +0 -36
  349. package/dist/irdevice/fan.d.ts.map +0 -1
  350. package/dist/irdevice/fan.js +0 -200
  351. package/dist/irdevice/fan.js.map +0 -1
  352. package/dist/irdevice/irdevice.d.ts +0 -68
  353. package/dist/irdevice/irdevice.d.ts.map +0 -1
  354. package/dist/irdevice/irdevice.js +0 -298
  355. package/dist/irdevice/irdevice.js.map +0 -1
  356. package/dist/irdevice/light.d.ts +0 -36
  357. package/dist/irdevice/light.d.ts.map +0 -1
  358. package/dist/irdevice/light.js +0 -206
  359. package/dist/irdevice/light.js.map +0 -1
  360. package/dist/irdevice/other.d.ts +0 -57
  361. package/dist/irdevice/other.d.ts.map +0 -1
  362. package/dist/irdevice/other.js +0 -778
  363. package/dist/irdevice/other.js.map +0 -1
  364. package/dist/irdevice/tv.d.ts +0 -45
  365. package/dist/irdevice/tv.d.ts.map +0 -1
  366. package/dist/irdevice/tv.js +0 -327
  367. package/dist/irdevice/tv.js.map +0 -1
  368. package/dist/irdevice/vacuumcleaner.d.ts +0 -28
  369. package/dist/irdevice/vacuumcleaner.d.ts.map +0 -1
  370. package/dist/irdevice/vacuumcleaner.js +0 -104
  371. package/dist/irdevice/vacuumcleaner.js.map +0 -1
  372. package/dist/irdevice/waterheater.d.ts +0 -30
  373. package/dist/irdevice/waterheater.d.ts.map +0 -1
  374. package/dist/irdevice/waterheater.js +0 -105
  375. package/dist/irdevice/waterheater.js.map +0 -1
  376. package/dist/platform-hap.d.ts +0 -149
  377. package/dist/platform-hap.d.ts.map +0 -1
  378. package/dist/platform-hap.js +0 -2849
  379. package/dist/platform-hap.js.map +0 -1
  380. package/dist/platform-matter.d.ts +0 -75
  381. package/dist/platform-matter.d.ts.map +0 -1
  382. package/dist/platform-matter.js +0 -379
  383. package/dist/platform-matter.js.map +0 -1
  384. package/dist/verifyconfig.test.d.ts +0 -2
  385. package/dist/verifyconfig.test.d.ts.map +0 -1
  386. package/dist/verifyconfig.test.js +0 -167
  387. package/dist/verifyconfig.test.js.map +0 -1
  388. package/src/custom.d.ts +0 -7
  389. package/src/devices-hap/airpurifier.ts +0 -563
  390. package/src/devices-hap/blindtilt.ts +0 -1049
  391. package/src/devices-hap/bot.ts +0 -900
  392. package/src/devices-hap/ceilinglight.ts +0 -742
  393. package/src/devices-hap/colorbulb.ts +0 -904
  394. package/src/devices-hap/contact.ts +0 -457
  395. package/src/devices-hap/curtain.ts +0 -944
  396. package/src/devices-hap/device.ts +0 -811
  397. package/src/devices-hap/fan.ts +0 -711
  398. package/src/devices-hap/hub.ts +0 -439
  399. package/src/devices-hap/humidifier.ts +0 -669
  400. package/src/devices-hap/iosensor.ts +0 -427
  401. package/src/devices-hap/lightstrip.ts +0 -836
  402. package/src/devices-hap/lock.ts +0 -620
  403. package/src/devices-hap/meter.ts +0 -426
  404. package/src/devices-hap/meterplus.ts +0 -430
  405. package/src/devices-hap/meterpro.ts +0 -522
  406. package/src/devices-hap/motion.ts +0 -390
  407. package/src/devices-hap/plug.ts +0 -423
  408. package/src/devices-hap/relayswitch.ts +0 -727
  409. package/src/devices-hap/robotvacuumcleaner.ts +0 -568
  410. package/src/devices-hap/waterdetector.ts +0 -400
  411. package/src/devices-matter/BaseMatterAccessory.ts +0 -131
  412. package/src/devices-matter/ColorLightAccessory.ts +0 -113
  413. package/src/devices-matter/ColorTemperatureLightAccessory.ts +0 -98
  414. package/src/devices-matter/ContactSensorAccessory.ts +0 -37
  415. package/src/devices-matter/DimmableLightAccessory.ts +0 -196
  416. package/src/devices-matter/DoorLockAccessory.ts +0 -56
  417. package/src/devices-matter/ExtendedColorLightAccessory.ts +0 -127
  418. package/src/devices-matter/FanAccessory.ts +0 -87
  419. package/src/devices-matter/HumiditySensorAccessory.ts +0 -39
  420. package/src/devices-matter/LeakSensorAccessory.ts +0 -36
  421. package/src/devices-matter/LightSensorAccessory.ts +0 -39
  422. package/src/devices-matter/OccupancySensorAccessory.ts +0 -52
  423. package/src/devices-matter/OnOffLightAccessory.ts +0 -133
  424. package/src/devices-matter/OnOffOutletAccessory.ts +0 -52
  425. package/src/devices-matter/OnOffSwitchAccessory.ts +0 -52
  426. package/src/devices-matter/RoboticVacuumAccessory.ts +0 -484
  427. package/src/devices-matter/SmokeCOAlarmAccessory.ts +0 -59
  428. package/src/devices-matter/TemperatureSensorAccessory.ts +0 -39
  429. package/src/devices-matter/ThermostatAccessory.ts +0 -105
  430. package/src/devices-matter/VenetianBlindAccessory.ts +0 -118
  431. package/src/devices-matter/WindowBlindAccessory.ts +0 -94
  432. package/src/devices-matter/custom/PowerStripAccessory.ts +0 -309
  433. package/src/devices-matter/custom/index.ts +0 -8
  434. package/src/devices-matter/index.ts +0 -29
  435. package/src/index.test.ts +0 -19
  436. package/src/irdevice/airconditioner.ts +0 -533
  437. package/src/irdevice/airpurifier.ts +0 -252
  438. package/src/irdevice/camera.ts +0 -129
  439. package/src/irdevice/fan.ts +0 -226
  440. package/src/irdevice/irdevice.ts +0 -344
  441. package/src/irdevice/light.ts +0 -246
  442. package/src/irdevice/other.ts +0 -790
  443. package/src/irdevice/tv.ts +0 -378
  444. package/src/irdevice/vacuumcleaner.ts +0 -126
  445. package/src/irdevice/waterheater.ts +0 -127
  446. package/src/platform-hap.ts +0 -2986
  447. package/src/platform-matter.ts +0 -465
  448. package/src/verifyconfig.test.ts +0 -197
@@ -0,0 +1,1562 @@
1
+ import { createDevice } from './deviceFactory.js';
2
+ import { PLATFORM_NAME, PLUGIN_NAME } from './settings.js';
3
+ import { SwitchBotClient } from './switchbotClient.js';
4
+ // Which device types should prefer Matter if available
5
+ // Based on HAP service mappings: device implementations use specific HomeKit services
6
+ // that map to corresponding Matter clusters when Matter is enabled
7
+ const DEVICE_MATTER_SUPPORTED = {
8
+ // Core devices
9
+ 'bot': true, // Switch → OnOff
10
+ 'curtain': true, // WindowCovering → WindowCovering
11
+ 'fan': true, // Fan → FanControl
12
+ 'light': true, // Lightbulb → OnOff + LevelControl
13
+ 'lightstrip': true, // Lightbulb (color) → OnOff + LevelControl + ColorControl
14
+ 'motion': true, // MotionSensor → OccupancySensing
15
+ 'contact': true, // ContactSensor → BooleanState
16
+ 'vacuum': true, // Switch → RobotVacuumCleaner
17
+ 'lock': true, // LockMechanism → DoorLock
18
+ 'humidifier': true, // Fan + Humidity → OnOff + FanControl + RelativeHumidityMeasurement
19
+ 'temperature': true, // TemperatureSensor → TemperatureMeasurement
20
+ // Switch devices
21
+ 'relay': true, // Switch → OnOff
22
+ 'relay switch 1': true, // Switch → OnOff
23
+ 'relay switch 1pm': true, // Switch → OnOff
24
+ 'plug': true, // Outlet → OnOff
25
+ 'plug mini (jp)': true, // Outlet → OnOff
26
+ 'plug mini (us)': true, // Outlet → OnOff
27
+ // Window covering variants
28
+ 'blindtilt': true, // WindowCovering → WindowCovering
29
+ 'blind tilt': true, // WindowCovering → WindowCovering
30
+ 'curtain3': true, // WindowCovering → WindowCovering
31
+ 'rollershade': true, // WindowCovering → WindowCovering
32
+ 'roller shade': true, // WindowCovering → WindowCovering
33
+ 'worollershade': true, // WindowCovering → WindowCovering
34
+ 'wo rollershade': true, // WindowCovering → WindowCovering
35
+ // Vacuum variants (normalized to 'vacuum' before lookup)
36
+ 'wosweeper': true, // VacuumDevice → RobotVacuumCleaner
37
+ 'wosweepermini': true, // VacuumDevice → RobotVacuumCleaner
38
+ 'wosweeperminipro': true, // VacuumDevice → RobotVacuumCleaner
39
+ 'k10+': true, // VacuumDevice → RobotVacuumCleaner
40
+ 'k10+ pro': true, // VacuumDevice → RobotVacuumCleaner
41
+ // Sensors
42
+ 'meter': true, // TemperatureSensor + HumiditySensor → TemperatureMeasurement + RelativeHumidityMeasurement
43
+ 'meterplus': true, // TemperatureSensor + HumiditySensor → TemperatureMeasurement + RelativeHumidityMeasurement
44
+ 'meter plus (jp)': true, // TemperatureSensor + HumiditySensor → TemperatureMeasurement + RelativeHumidityMeasurement
45
+ 'meterpro': true, // TemperatureSensor + HumiditySensor → TemperatureMeasurement + RelativeHumidityMeasurement
46
+ 'meterpro(co2)': true, // TemperatureSensor + HumiditySensor → TemperatureMeasurement + RelativeHumidityMeasurement
47
+ 'waterdetector': true, // LeakSensor → BooleanState
48
+ 'water detector': true, // LeakSensor → BooleanState
49
+ // Other devices
50
+ 'smart fan': true, // Fan → FanControl
51
+ 'strip light': true, // Lightbulb (color) → OnOff + LevelControl + ColorControl
52
+ 'hub 2': false, // Hub device - not exposed as accessory
53
+ 'walletfinder': false, // Button device - Matter support TBD
54
+ };
55
+ // Default Matter cluster configurations by device type
56
+ // Maps device types to their Matter cluster states (used when device doesn't provide clusters)
57
+ // Note: wosweeper/curtain/plug variants are normalized before cluster lookup (see loadDevices)
58
+ const DEVICE_MATTER_CLUSTERS = {
59
+ // Core devices - aligned with HAP service implementations
60
+ bot: { onOff: { onOff: false } }, // Switch → OnOff
61
+ vacuum: {
62
+ rvcRunMode: {
63
+ supportedModes: [
64
+ { label: 'Idle', mode: 0, modeTags: [{ value: 16384 }] },
65
+ { label: 'Cleaning', mode: 1, modeTags: [{ value: 16385 }] },
66
+ ],
67
+ currentMode: 0,
68
+ },
69
+ rvcCleanMode: {
70
+ supportedModes: [
71
+ { label: 'Vacuum', mode: 0, modeTags: [{ value: 16385 }] },
72
+ ],
73
+ currentMode: 0,
74
+ },
75
+ rvcOperationalState: {
76
+ operationalStateList: [
77
+ { operationalStateId: 0 }, // Stopped
78
+ { operationalStateId: 1 }, // Running
79
+ { operationalStateId: 2 }, // Paused
80
+ { operationalStateId: 3 }, // Error (required)
81
+ { operationalStateId: 64 }, // Seeking charger
82
+ { operationalStateId: 65 }, // Charging
83
+ { operationalStateId: 66 }, // Docked
84
+ ],
85
+ operationalState: 66,
86
+ },
87
+ }, // Switch in HAP, RobotVacuumCleaner in Matter
88
+ curtain: {
89
+ windowCovering: {
90
+ currentPositionLiftPercent100ths: 0,
91
+ targetPositionLiftPercent100ths: 0,
92
+ operationalStatus: 0,
93
+ },
94
+ }, // WindowCovering → WindowCovering (includes curtain3, rollershade variants via normalization)
95
+ blindtilt: {
96
+ windowCovering: {
97
+ currentPositionLiftPercent100ths: 0,
98
+ targetPositionLiftPercent100ths: 0,
99
+ currentPositionTiltPercent100ths: 0,
100
+ targetPositionTiltPercent100ths: 0,
101
+ operationalStatus: 0,
102
+ },
103
+ }, // WindowCovering with tilt → WindowCovering
104
+ fan: {
105
+ onOff: { onOff: false },
106
+ fanControl: {
107
+ fanMode: 0,
108
+ percentCurrent: 0,
109
+ percentSetting: 0,
110
+ speedCurrent: 0,
111
+ speedMax: 100,
112
+ },
113
+ }, // Fan → OnOff + FanControl
114
+ light: {
115
+ onOff: { onOff: false },
116
+ levelControl: {
117
+ currentLevel: 0,
118
+ minLevel: 0,
119
+ maxLevel: 254,
120
+ },
121
+ }, // Lightbulb → OnOff + LevelControl
122
+ lightstrip: {
123
+ onOff: { onOff: false },
124
+ levelControl: {
125
+ currentLevel: 0,
126
+ minLevel: 0,
127
+ maxLevel: 254,
128
+ },
129
+ colorControl: {
130
+ currentHue: 0,
131
+ currentSaturation: 0,
132
+ colorTemperatureMireds: 400,
133
+ colorMode: 0,
134
+ },
135
+ }, // Lightbulb with color → OnOff + LevelControl + ColorControl
136
+ lock: {
137
+ doorLock: {
138
+ lockState: 0,
139
+ lockType: 0,
140
+ actuatorEnabled: true,
141
+ operatingMode: 0,
142
+ },
143
+ }, // LockMechanism → DoorLock
144
+ motion: {
145
+ occupancySensing: {
146
+ occupancy: 0,
147
+ occupancySensorType: 0,
148
+ },
149
+ }, // MotionSensor → OccupancySensing
150
+ contact: {
151
+ booleanState: {
152
+ stateValue: false,
153
+ },
154
+ }, // ContactSensor → BooleanState
155
+ humidifier: {
156
+ onOff: { onOff: false },
157
+ fanControl: {
158
+ fanMode: 0,
159
+ percentCurrent: 0,
160
+ },
161
+ relativeHumidityMeasurement: {
162
+ measuredValue: 0,
163
+ minMeasuredValue: 0,
164
+ maxMeasuredValue: 100,
165
+ },
166
+ }, // HumidifierDehumidifier → OnOff + FanControl + RelativeHumidityMeasurement
167
+ temperature: {
168
+ temperatureMeasurement: {
169
+ measuredValue: 0,
170
+ minMeasuredValue: -27315,
171
+ maxMeasuredValue: 32767,
172
+ },
173
+ }, // TemperatureSensor → TemperatureMeasurement
174
+ // Switch/Outlet devices
175
+ relay: { onOff: { onOff: false } }, // Switch → OnOff
176
+ plug: {
177
+ onOff: { onOff: false },
178
+ electricalMeasurement: {
179
+ activePower: 0,
180
+ rmsCurrent: 0,
181
+ rmsVoltage: 0,
182
+ },
183
+ }, // Outlet → OnOff + ElectricalMeasurement (for PM models)
184
+ // Sensors
185
+ meter: {
186
+ temperatureMeasurement: {
187
+ measuredValue: 0,
188
+ minMeasuredValue: -27315,
189
+ maxMeasuredValue: 32767,
190
+ },
191
+ relativeHumidityMeasurement: {
192
+ measuredValue: 0,
193
+ minMeasuredValue: 0,
194
+ maxMeasuredValue: 100,
195
+ },
196
+ }, // TemperatureSensor + HumiditySensor → TemperatureMeasurement + RelativeHumidityMeasurement
197
+ waterdetector: {
198
+ booleanState: {
199
+ stateValue: false,
200
+ },
201
+ }, // LeakSensor → BooleanState
202
+ };
203
+ const DEVICE_MATTER_DEVICE_TYPE_KEYS = {
204
+ bot: 'OnOffSwitch',
205
+ vacuum: 'RoboticVacuumCleaner',
206
+ curtain: 'WindowCovering',
207
+ blindtilt: 'WindowCovering',
208
+ fan: 'Fan',
209
+ light: 'DimmableLight',
210
+ lightstrip: 'ExtendedColorLight',
211
+ lock: 'DoorLock',
212
+ motion: 'MotionSensor',
213
+ contact: 'ContactSensor',
214
+ humidifier: 'Fan',
215
+ temperature: 'TemperatureSensor',
216
+ relay: 'OnOffSwitch',
217
+ plug: 'OnOffOutlet',
218
+ meter: 'TemperatureSensor',
219
+ waterdetector: 'LeakSensor',
220
+ };
221
+ // Factory function to create Matter handlers with Homebridge logger integration
222
+ function createMatterHandlers(log, deviceId, type, client) {
223
+ const lowerType = type.toLowerCase();
224
+ switch (lowerType) {
225
+ case 'vacuum':
226
+ return {
227
+ rvcRunMode: {
228
+ changeToMode: async (request) => {
229
+ const modeNames = ['Idle', 'Cleaning', 'Mapping'];
230
+ const modeName = modeNames[request?.newMode] || `Unknown (${request?.newMode})`;
231
+ log.info(`[${deviceId}] RVC run mode change requested: ${modeName}`);
232
+ if (!client) {
233
+ log.warn(`[${deviceId}] No SwitchBot client available`);
234
+ return { success: false };
235
+ }
236
+ try {
237
+ // Map Matter mode to SwitchBot command
238
+ const modeMap = {
239
+ 0: 'standby', // Idle
240
+ 1: 'start', // Cleaning
241
+ 2: 'mapping', // Mapping
242
+ };
243
+ const switchBotMode = modeMap[request?.newMode] || 'start';
244
+ const result = await client.setDeviceState(deviceId, {
245
+ command: switchBotMode,
246
+ parameter: 'default',
247
+ commandType: 'command',
248
+ });
249
+ log.info(`[${deviceId}] RVC mode changed successfully to ${switchBotMode}`);
250
+ return { success: true, result };
251
+ }
252
+ catch (e) {
253
+ log.error(`[${deviceId}] Failed to change RVC mode:`, e);
254
+ return { success: false, error: e };
255
+ }
256
+ },
257
+ },
258
+ rvcCleanMode: {
259
+ changeToMode: async (request) => {
260
+ const modeName = request?.newMode !== undefined ? `Mode ${request.newMode}` : 'Unknown';
261
+ log.info(`[${deviceId}] RVC clean mode change requested: ${modeName}`);
262
+ if (!client) {
263
+ log.warn(`[${deviceId}] No SwitchBot client available`);
264
+ return { success: false };
265
+ }
266
+ try {
267
+ // For vacuum, clean mode might not be directly supported; document for future API expansion
268
+ log.info(`[${deviceId}] Clean mode change not directly supported yet for vacuum`);
269
+ return { success: true };
270
+ }
271
+ catch (e) {
272
+ log.error(`[${deviceId}] Failed to change RVC clean mode:`, e);
273
+ return { success: false, error: e };
274
+ }
275
+ },
276
+ },
277
+ rvcOperationalState: {
278
+ pause: async () => {
279
+ log.info(`[${deviceId}] RVC pause command received`);
280
+ if (!client) {
281
+ log.warn(`[${deviceId}] No SwitchBot client available`);
282
+ return { success: false };
283
+ }
284
+ try {
285
+ const result = await client.setDeviceState(deviceId, {
286
+ command: 'pause',
287
+ parameter: 'default',
288
+ commandType: 'command',
289
+ });
290
+ log.info(`[${deviceId}] RVC paused successfully`);
291
+ return { success: true, result };
292
+ }
293
+ catch (e) {
294
+ log.error(`[${deviceId}] Failed to pause RVC:`, e);
295
+ return { success: false, error: e };
296
+ }
297
+ },
298
+ resume: async () => {
299
+ log.info(`[${deviceId}] RVC resume command received`);
300
+ if (!client) {
301
+ log.warn(`[${deviceId}] No SwitchBot client available`);
302
+ return { success: false };
303
+ }
304
+ try {
305
+ const result = await client.setDeviceState(deviceId, {
306
+ command: 'resume',
307
+ parameter: 'default',
308
+ commandType: 'command',
309
+ });
310
+ log.info(`[${deviceId}] RVC resumed successfully`);
311
+ return { success: true, result };
312
+ }
313
+ catch (e) {
314
+ log.error(`[${deviceId}] Failed to resume RVC:`, e);
315
+ return { success: false, error: e };
316
+ }
317
+ },
318
+ goHome: async () => {
319
+ log.info(`[${deviceId}] RVC goHome command received`);
320
+ if (!client) {
321
+ log.warn(`[${deviceId}] No SwitchBot client available`);
322
+ return { success: false };
323
+ }
324
+ try {
325
+ const result = await client.setDeviceState(deviceId, {
326
+ command: 'home',
327
+ parameter: 'default',
328
+ commandType: 'command',
329
+ });
330
+ log.info(`[${deviceId}] RVC sent to home successfully`);
331
+ return { success: true, result };
332
+ }
333
+ catch (e) {
334
+ log.error(`[${deviceId}] Failed to send goHome command:`, e);
335
+ return { success: false, error: e };
336
+ }
337
+ },
338
+ },
339
+ };
340
+ case 'bot':
341
+ return {
342
+ onOff: {
343
+ on: async () => {
344
+ log.info(`[${deviceId}] Bot ON command received`);
345
+ if (!client) {
346
+ log.warn(`[${deviceId}] No SwitchBot client available`);
347
+ return { success: false };
348
+ }
349
+ try {
350
+ const result = await client.setDeviceState(deviceId, {
351
+ command: 'turnOn',
352
+ parameter: 'default',
353
+ commandType: 'command',
354
+ });
355
+ log.info(`[${deviceId}] Bot turned on successfully`);
356
+ return { success: true, result };
357
+ }
358
+ catch (e) {
359
+ log.error(`[${deviceId}] Failed to turn on Bot:`, e);
360
+ return { success: false, error: e };
361
+ }
362
+ },
363
+ off: async () => {
364
+ log.info(`[${deviceId}] Bot OFF command received`);
365
+ if (!client) {
366
+ log.warn(`[${deviceId}] No SwitchBot client available`);
367
+ return { success: false };
368
+ }
369
+ try {
370
+ const result = await client.setDeviceState(deviceId, {
371
+ command: 'turnOff',
372
+ parameter: 'default',
373
+ commandType: 'command',
374
+ });
375
+ log.info(`[${deviceId}] Bot turned off successfully`);
376
+ return { success: true, result };
377
+ }
378
+ catch (e) {
379
+ log.error(`[${deviceId}] Failed to turn off Bot:`, e);
380
+ return { success: false, error: e };
381
+ }
382
+ },
383
+ },
384
+ };
385
+ case 'curtain':
386
+ case 'blindtilt':
387
+ return {
388
+ windowCovering: {
389
+ goToLiftPercentage: async (request) => {
390
+ const percentage = request?.liftPercent100thsValue;
391
+ log.info(`[${deviceId}] Curtain position change requested: ${percentage}`);
392
+ if (!client) {
393
+ log.warn(`[${deviceId}] No SwitchBot client available`);
394
+ return { success: false };
395
+ }
396
+ try {
397
+ // Convert Matter percentage (0-10000) to SwitchBot (0-100)
398
+ const position = Math.max(0, Math.min(100, Math.round((percentage || 0) / 100)));
399
+ const result = await client.setDeviceState(deviceId, {
400
+ command: 'setPosition',
401
+ parameter: String(position),
402
+ commandType: 'command',
403
+ });
404
+ log.info(`[${deviceId}] Curtain position set to ${position}% successfully`);
405
+ return { success: true, result };
406
+ }
407
+ catch (e) {
408
+ log.error(`[${deviceId}] Failed to set curtain position:`, e);
409
+ return { success: false, error: e };
410
+ }
411
+ },
412
+ upOrOpen: async () => {
413
+ log.info(`[${deviceId}] Curtain open command received`);
414
+ if (!client) {
415
+ log.warn(`[${deviceId}] No SwitchBot client available`);
416
+ return { success: false };
417
+ }
418
+ try {
419
+ const result = await client.setDeviceState(deviceId, {
420
+ command: 'open',
421
+ parameter: 'default',
422
+ commandType: 'command',
423
+ });
424
+ log.info(`[${deviceId}] Curtain opened successfully`);
425
+ return { success: true, result };
426
+ }
427
+ catch (e) {
428
+ log.error(`[${deviceId}] Failed to open curtain:`, e);
429
+ return { success: false, error: e };
430
+ }
431
+ },
432
+ downOrClose: async () => {
433
+ log.info(`[${deviceId}] Curtain close command received`);
434
+ if (!client) {
435
+ log.warn(`[${deviceId}] No SwitchBot client available`);
436
+ return { success: false };
437
+ }
438
+ try {
439
+ const result = await client.setDeviceState(deviceId, {
440
+ command: 'close',
441
+ parameter: 'default',
442
+ commandType: 'command',
443
+ });
444
+ log.info(`[${deviceId}] Curtain closed successfully`);
445
+ return { success: true, result };
446
+ }
447
+ catch (e) {
448
+ log.error(`[${deviceId}] Failed to close curtain:`, e);
449
+ return { success: false, error: e };
450
+ }
451
+ },
452
+ stopMotion: async () => {
453
+ log.info(`[${deviceId}] Curtain stop command received`);
454
+ if (!client) {
455
+ log.warn(`[${deviceId}] No SwitchBot client available`);
456
+ return { success: false };
457
+ }
458
+ try {
459
+ const result = await client.setDeviceState(deviceId, {
460
+ command: 'pause',
461
+ parameter: 'default',
462
+ commandType: 'command',
463
+ });
464
+ log.info(`[${deviceId}] Curtain motion stopped successfully`);
465
+ return { success: true, result };
466
+ }
467
+ catch (e) {
468
+ log.error(`[${deviceId}] Failed to stop curtain:`, e);
469
+ return { success: false, error: e };
470
+ }
471
+ },
472
+ },
473
+ };
474
+ case 'plug':
475
+ return {
476
+ onOff: {
477
+ on: async () => {
478
+ log.info(`[${deviceId}] Plug ON command received`);
479
+ if (!client) {
480
+ log.warn(`[${deviceId}] No SwitchBot client available`);
481
+ return { success: false };
482
+ }
483
+ try {
484
+ const result = await client.setDeviceState(deviceId, {
485
+ command: 'turnOn',
486
+ parameter: 'default',
487
+ commandType: 'command',
488
+ });
489
+ log.info(`[${deviceId}] Plug turned on successfully`);
490
+ return { success: true, result };
491
+ }
492
+ catch (e) {
493
+ log.error(`[${deviceId}] Failed to turn on plug:`, e);
494
+ return { success: false, error: e };
495
+ }
496
+ },
497
+ off: async () => {
498
+ log.info(`[${deviceId}] Plug OFF command received`);
499
+ if (!client) {
500
+ log.warn(`[${deviceId}] No SwitchBot client available`);
501
+ return { success: false };
502
+ }
503
+ try {
504
+ const result = await client.setDeviceState(deviceId, {
505
+ command: 'turnOff',
506
+ parameter: 'default',
507
+ commandType: 'command',
508
+ });
509
+ log.info(`[${deviceId}] Plug turned off successfully`);
510
+ return { success: true, result };
511
+ }
512
+ catch (e) {
513
+ log.error(`[${deviceId}] Failed to turn off plug:`, e);
514
+ return { success: false, error: e };
515
+ }
516
+ },
517
+ },
518
+ };
519
+ case 'lock':
520
+ return {
521
+ doorLock: {
522
+ setLockState: async (request) => {
523
+ const state = request?.lockState === 1 ? 'LOCKED' : 'UNLOCKED';
524
+ log.info(`[${deviceId}] Lock state change requested: ${state}`);
525
+ if (!client) {
526
+ log.warn(`[${deviceId}] No SwitchBot client available`);
527
+ return { success: false };
528
+ }
529
+ try {
530
+ const command = request?.lockState === 1 ? 'lock' : 'unlock';
531
+ const result = await client.setDeviceState(deviceId, {
532
+ command,
533
+ parameter: 'default',
534
+ commandType: 'command',
535
+ });
536
+ log.info(`[${deviceId}] Lock ${state} successfully`);
537
+ return { success: true, result };
538
+ }
539
+ catch (e) {
540
+ log.error(`[${deviceId}] Failed to change lock state:`, e);
541
+ return { success: false, error: e };
542
+ }
543
+ },
544
+ },
545
+ };
546
+ case 'fan':
547
+ return {
548
+ onOff: {
549
+ on: async () => {
550
+ log.info(`[${deviceId}] Fan ON command received`);
551
+ if (!client) {
552
+ log.warn(`[${deviceId}] No SwitchBot client available`);
553
+ return { success: false };
554
+ }
555
+ try {
556
+ const result = await client.setDeviceState(deviceId, {
557
+ command: 'turnOn',
558
+ parameter: 'default',
559
+ commandType: 'command',
560
+ });
561
+ log.info(`[${deviceId}] Fan turned on successfully`);
562
+ return { success: true, result };
563
+ }
564
+ catch (e) {
565
+ log.error(`[${deviceId}] Failed to turn on fan:`, e);
566
+ return { success: false, error: e };
567
+ }
568
+ },
569
+ off: async () => {
570
+ log.info(`[${deviceId}] Fan OFF command received`);
571
+ if (!client) {
572
+ log.warn(`[${deviceId}] No SwitchBot client available`);
573
+ return { success: false };
574
+ }
575
+ try {
576
+ const result = await client.setDeviceState(deviceId, {
577
+ command: 'turnOff',
578
+ parameter: 'default',
579
+ commandType: 'command',
580
+ });
581
+ log.info(`[${deviceId}] Fan turned off successfully`);
582
+ return { success: true, result };
583
+ }
584
+ catch (e) {
585
+ log.error(`[${deviceId}] Failed to turn off fan:`, e);
586
+ return { success: false, error: e };
587
+ }
588
+ },
589
+ },
590
+ fanControl: {
591
+ setFanSpeed: async (request) => {
592
+ const speed = request?.percentSetting || 0;
593
+ log.info(`[${deviceId}] Fan speed change requested: ${speed}%`);
594
+ if (!client) {
595
+ log.warn(`[${deviceId}] No SwitchBot client available`);
596
+ return { success: false };
597
+ }
598
+ try {
599
+ // Convert percentage to SwitchBot fan speed parameter
600
+ const speedParam = Math.max(1, Math.min(100, speed));
601
+ const result = await client.setDeviceState(deviceId, {
602
+ command: 'setFanSpeed',
603
+ parameter: String(speedParam),
604
+ commandType: 'command',
605
+ });
606
+ log.info(`[${deviceId}] Fan speed set to ${speedParam}% successfully`);
607
+ return { success: true, result };
608
+ }
609
+ catch (e) {
610
+ log.error(`[${deviceId}] Failed to set fan speed:`, e);
611
+ return { success: false, error: e };
612
+ }
613
+ },
614
+ },
615
+ };
616
+ case 'light':
617
+ return {
618
+ onOff: {
619
+ on: async () => {
620
+ log.info(`[${deviceId}] Light ON command received`);
621
+ if (!client) {
622
+ log.warn(`[${deviceId}] No SwitchBot client available`);
623
+ return { success: false };
624
+ }
625
+ try {
626
+ const result = await client.setDeviceState(deviceId, {
627
+ command: 'turnOn',
628
+ parameter: 'default',
629
+ commandType: 'command',
630
+ });
631
+ log.info(`[${deviceId}] Light turned on successfully`);
632
+ return { success: true, result };
633
+ }
634
+ catch (e) {
635
+ log.error(`[${deviceId}] Failed to turn on light:`, e);
636
+ return { success: false, error: e };
637
+ }
638
+ },
639
+ off: async () => {
640
+ log.info(`[${deviceId}] Light OFF command received`);
641
+ if (!client) {
642
+ log.warn(`[${deviceId}] No SwitchBot client available`);
643
+ return { success: false };
644
+ }
645
+ try {
646
+ const result = await client.setDeviceState(deviceId, {
647
+ command: 'turnOff',
648
+ parameter: 'default',
649
+ commandType: 'command',
650
+ });
651
+ log.info(`[${deviceId}] Light turned off successfully`);
652
+ return { success: true, result };
653
+ }
654
+ catch (e) {
655
+ log.error(`[${deviceId}] Failed to turn off light:`, e);
656
+ return { success: false, error: e };
657
+ }
658
+ },
659
+ },
660
+ levelControl: {
661
+ moveToLevel: async (request) => {
662
+ const level = request?.level || 0;
663
+ // Convert from 0-254 to 0-100
664
+ const brightness = Math.round((level / 254) * 100);
665
+ log.info(`[${deviceId}] Light brightness change requested: ${brightness}%`);
666
+ if (!client) {
667
+ log.warn(`[${deviceId}] No SwitchBot client available`);
668
+ return { success: false };
669
+ }
670
+ try {
671
+ const param = Math.max(0, Math.min(100, brightness));
672
+ const result = await client.setDeviceState(deviceId, {
673
+ command: 'setBrightness',
674
+ parameter: String(param),
675
+ commandType: 'command',
676
+ });
677
+ log.info(`[${deviceId}] Light brightness set to ${param}% successfully`);
678
+ return { success: true, result };
679
+ }
680
+ catch (e) {
681
+ log.error(`[${deviceId}] Failed to set light brightness:`, e);
682
+ return { success: false, error: e };
683
+ }
684
+ },
685
+ },
686
+ };
687
+ case 'lightstrip':
688
+ return {
689
+ onOff: {
690
+ on: async () => {
691
+ log.info(`[${deviceId}] Lightstrip ON command received`);
692
+ if (!client) {
693
+ log.warn(`[${deviceId}] No SwitchBot client available`);
694
+ return { success: false };
695
+ }
696
+ try {
697
+ const result = await client.setDeviceState(deviceId, {
698
+ command: 'turnOn',
699
+ parameter: 'default',
700
+ commandType: 'command',
701
+ });
702
+ log.info(`[${deviceId}] Lightstrip turned on successfully`);
703
+ return { success: true, result };
704
+ }
705
+ catch (e) {
706
+ log.error(`[${deviceId}] Failed to turn on lightstrip:`, e);
707
+ return { success: false, error: e };
708
+ }
709
+ },
710
+ off: async () => {
711
+ log.info(`[${deviceId}] Lightstrip OFF command received`);
712
+ if (!client) {
713
+ log.warn(`[${deviceId}] No SwitchBot client available`);
714
+ return { success: false };
715
+ }
716
+ try {
717
+ const result = await client.setDeviceState(deviceId, {
718
+ command: 'turnOff',
719
+ parameter: 'default',
720
+ commandType: 'command',
721
+ });
722
+ log.info(`[${deviceId}] Lightstrip turned off successfully`);
723
+ return { success: true, result };
724
+ }
725
+ catch (e) {
726
+ log.error(`[${deviceId}] Failed to turn off lightstrip:`, e);
727
+ return { success: false, error: e };
728
+ }
729
+ },
730
+ },
731
+ levelControl: {
732
+ moveToLevel: async (request) => {
733
+ const level = request?.level || 0;
734
+ // Convert from 0-254 to 0-100
735
+ const brightness = Math.round((level / 254) * 100);
736
+ log.info(`[${deviceId}] Lightstrip brightness change requested: ${brightness}%`);
737
+ if (!client) {
738
+ log.warn(`[${deviceId}] No SwitchBot client available`);
739
+ return { success: false };
740
+ }
741
+ try {
742
+ const param = Math.max(0, Math.min(100, brightness));
743
+ const result = await client.setDeviceState(deviceId, {
744
+ command: 'setBrightness',
745
+ parameter: String(param),
746
+ commandType: 'command',
747
+ });
748
+ log.info(`[${deviceId}] Lightstrip brightness set to ${param}% successfully`);
749
+ return { success: true, result };
750
+ }
751
+ catch (e) {
752
+ log.error(`[${deviceId}] Failed to set lightstrip brightness:`, e);
753
+ return { success: false, error: e };
754
+ }
755
+ },
756
+ },
757
+ colorControl: {
758
+ moveToHueAndSaturation: async (request) => {
759
+ const hue = request?.hue || 0;
760
+ const saturation = request?.saturation || 0;
761
+ log.info(`[${deviceId}] Lightstrip color change requested: hue=${hue}, sat=${saturation}`);
762
+ if (!client) {
763
+ log.warn(`[${deviceId}] No SwitchBot client available`);
764
+ return { success: false };
765
+ }
766
+ try {
767
+ // Convert hue (0-254) and saturation (0-254) to combined color parameter
768
+ // SwitchBot typically expects RGB or HSV format as parameter
769
+ const colorParam = `${Math.round(hue)},${Math.round(saturation)}`;
770
+ const result = await client.setDeviceState(deviceId, {
771
+ command: 'setColor',
772
+ parameter: colorParam,
773
+ commandType: 'command',
774
+ });
775
+ log.info(`[${deviceId}] Lightstrip color set successfully`);
776
+ return { success: true, result };
777
+ }
778
+ catch (e) {
779
+ log.error(`[${deviceId}] Failed to set lightstrip color:`, e);
780
+ return { success: false, error: e };
781
+ }
782
+ },
783
+ moveToColorTemperature: async (request) => {
784
+ const mireds = request?.colorTemperatureMireds || 400;
785
+ // Convert mireds (158-500 typical range) to Kelvin: K = 1000000 / mireds
786
+ const kelvin = Math.round(1000000 / mireds);
787
+ log.info(`[${deviceId}] Lightstrip color temperature change requested: ${mireds} mireds (${kelvin}K)`);
788
+ if (!client) {
789
+ log.warn(`[${deviceId}] No SwitchBot client available`);
790
+ return { success: false };
791
+ }
792
+ try {
793
+ // Map Kelvin to SwitchBot color temperature parameter (typically 0-100 or specific values)
794
+ // Normalize to 0-100 scale where 0=warm (2700K) and 100=cool (6500K)
795
+ const colorTempParam = Math.max(0, Math.min(100, Math.round(((kelvin - 2700) / 3800) * 100)));
796
+ const result = await client.setDeviceState(deviceId, {
797
+ command: 'setColorTemperature',
798
+ parameter: String(colorTempParam),
799
+ commandType: 'command',
800
+ });
801
+ log.info(`[${deviceId}] Lightstrip color temperature set to ${kelvin}K successfully`);
802
+ return { success: true, result };
803
+ }
804
+ catch (e) {
805
+ log.error(`[${deviceId}] Failed to set lightstrip color temperature:`, e);
806
+ return { success: false, error: e };
807
+ }
808
+ },
809
+ },
810
+ };
811
+ case 'humidifier':
812
+ return {
813
+ onOff: {
814
+ on: async () => {
815
+ log.info(`[${deviceId}] Humidifier ON command received`);
816
+ if (!client) {
817
+ log.warn(`[${deviceId}] No SwitchBot client available`);
818
+ return { success: false };
819
+ }
820
+ try {
821
+ const result = await client.setDeviceState(deviceId, {
822
+ command: 'turnOn',
823
+ parameter: 'default',
824
+ commandType: 'command',
825
+ });
826
+ log.info(`[${deviceId}] Humidifier turned on successfully`);
827
+ return { success: true, result };
828
+ }
829
+ catch (e) {
830
+ log.error(`[${deviceId}] Failed to turn on humidifier:`, e);
831
+ return { success: false, error: e };
832
+ }
833
+ },
834
+ off: async () => {
835
+ log.info(`[${deviceId}] Humidifier OFF command received`);
836
+ if (!client) {
837
+ log.warn(`[${deviceId}] No SwitchBot client available`);
838
+ return { success: false };
839
+ }
840
+ try {
841
+ const result = await client.setDeviceState(deviceId, {
842
+ command: 'turnOff',
843
+ parameter: 'default',
844
+ commandType: 'command',
845
+ });
846
+ log.info(`[${deviceId}] Humidifier turned off successfully`);
847
+ return { success: true, result };
848
+ }
849
+ catch (e) {
850
+ log.error(`[${deviceId}] Failed to turn off humidifier:`, e);
851
+ return { success: false, error: e };
852
+ }
853
+ },
854
+ },
855
+ fanControl: {
856
+ setFanSpeed: async (request) => {
857
+ const speed = request?.percentSetting || 0;
858
+ log.info(`[${deviceId}] Humidifier speed change requested: ${speed}%`);
859
+ if (!client) {
860
+ log.warn(`[${deviceId}] No SwitchBot client available`);
861
+ return { success: false };
862
+ }
863
+ try {
864
+ // Convert percentage to SwitchBot humidifier speed parameter
865
+ const speedParam = Math.max(1, Math.min(100, speed));
866
+ const result = await client.setDeviceState(deviceId, {
867
+ command: 'setFanSpeed',
868
+ parameter: String(speedParam),
869
+ commandType: 'command',
870
+ });
871
+ log.info(`[${deviceId}] Humidifier speed set to ${speedParam}% successfully`);
872
+ return { success: true, result };
873
+ }
874
+ catch (e) {
875
+ log.error(`[${deviceId}] Failed to set humidifier speed:`, e);
876
+ return { success: false, error: e };
877
+ }
878
+ },
879
+ },
880
+ };
881
+ case 'relay':
882
+ return {
883
+ onOff: {
884
+ on: async () => {
885
+ log.info(`[${deviceId}] Relay ON command received`);
886
+ if (!client) {
887
+ log.warn(`[${deviceId}] No SwitchBot client available`);
888
+ return { success: false };
889
+ }
890
+ try {
891
+ const result = await client.setDeviceState(deviceId, {
892
+ command: 'turnOn',
893
+ parameter: 'default',
894
+ commandType: 'command',
895
+ });
896
+ log.info(`[${deviceId}] Relay turned on successfully`);
897
+ return { success: true, result };
898
+ }
899
+ catch (e) {
900
+ log.error(`[${deviceId}] Failed to turn on relay:`, e);
901
+ return { success: false, error: e };
902
+ }
903
+ },
904
+ off: async () => {
905
+ log.info(`[${deviceId}] Relay OFF command received`);
906
+ if (!client) {
907
+ log.warn(`[${deviceId}] No SwitchBot client available`);
908
+ return { success: false };
909
+ }
910
+ try {
911
+ const result = await client.setDeviceState(deviceId, {
912
+ command: 'turnOff',
913
+ parameter: 'default',
914
+ commandType: 'command',
915
+ });
916
+ log.info(`[${deviceId}] Relay turned off successfully`);
917
+ return { success: true, result };
918
+ }
919
+ catch (e) {
920
+ log.error(`[${deviceId}] Failed to turn off relay:`, e);
921
+ return { success: false, error: e };
922
+ }
923
+ },
924
+ },
925
+ };
926
+ default:
927
+ return undefined;
928
+ }
929
+ }
930
+ function resolveMatterDeviceType(matterApi, type, createdDeviceType, clusters) {
931
+ if (createdDeviceType && typeof createdDeviceType === 'object' && typeof createdDeviceType.with === 'function') {
932
+ return createdDeviceType;
933
+ }
934
+ const lowerType = (typeof createdDeviceType === 'string' && createdDeviceType) ? createdDeviceType.toLowerCase() : (type || '').toLowerCase();
935
+ // Cluster-based upgrade for color lights if descriptor omitted device type.
936
+ const hasColorControl = !!clusters?.colorControl;
937
+ const inferredType = hasColorControl && lowerType === 'light'
938
+ ? 'lightstrip'
939
+ : lowerType;
940
+ const mappedKey = DEVICE_MATTER_DEVICE_TYPE_KEYS[inferredType] || 'OnOffSwitch';
941
+ return matterApi?.deviceTypes?.[mappedKey] || matterApi?.deviceTypes?.OnOffSwitch;
942
+ }
943
+ export class SwitchBotHAPPlatform {
944
+ api;
945
+ log;
946
+ config;
947
+ devices = [];
948
+ // cached accessories restored by Homebridge
949
+ accessories;
950
+ // Track last loaded config to detect changes
951
+ lastConfigHash = '';
952
+ configReloadInterval = null;
953
+ constructor(log, config, api) {
954
+ this.log = log;
955
+ this.config = config ?? {};
956
+ this.api = api;
957
+ this.accessories = new Map();
958
+ this.log.info('SwitchBot HAP platform initialized');
959
+ // Create/shared SwitchBot client and attach to config so child devices reuse it.
960
+ try {
961
+ const client = new SwitchBotClient(this.config);
962
+ void client.init();
963
+ this.config._client = client;
964
+ }
965
+ catch (e) {
966
+ this.log.debug('Failed to create shared SwitchBot client', e);
967
+ }
968
+ // Create/shared SwitchBot client and attach to config so child devices reuse it.
969
+ try {
970
+ const client = new SwitchBotClient(this.config);
971
+ void client.init();
972
+ this.config._client = client;
973
+ }
974
+ catch (e) {
975
+ this.log.debug('Failed to create shared SwitchBot client', e);
976
+ }
977
+ // Wait for Homebridge to finish launching to create/register accessories
978
+ if (this.api && typeof this.api.on === 'function') {
979
+ ;
980
+ this.api.on('didFinishLaunching', () => {
981
+ void this.loadDevices();
982
+ // Start periodic config reload to pick up UI changes
983
+ this.configReloadInterval = setInterval(() => {
984
+ void this.checkAndReloadDevices();
985
+ }, 10000); // Check every 10 seconds
986
+ });
987
+ }
988
+ else {
989
+ void this.loadDevices();
990
+ // Start periodic config reload to pick up UI changes
991
+ this.configReloadInterval = setInterval(() => {
992
+ void this.checkAndReloadDevices();
993
+ }, 10000); // Check every 10 seconds
994
+ }
995
+ }
996
+ getConfigHash() {
997
+ // Create a simple hash of current device config to detect changes
998
+ const devices = this.config?.devices ?? [];
999
+ return JSON.stringify(devices.map((d) => ({
1000
+ id: d.deviceId ?? d.id,
1001
+ type: d.configDeviceType ?? d.type,
1002
+ name: d.configDeviceName ?? d.name,
1003
+ })));
1004
+ }
1005
+ async checkAndReloadDevices() {
1006
+ const currentHash = this.getConfigHash();
1007
+ if (currentHash !== this.lastConfigHash) {
1008
+ this.log.info('[SwitchBot] Detected config changes, reloading devices...');
1009
+ // Clear existing devices
1010
+ this.devices = [];
1011
+ await this.loadDevices();
1012
+ }
1013
+ }
1014
+ async loadDevices() {
1015
+ const devices = this.config?.devices ?? [];
1016
+ for (const raw of devices) {
1017
+ // Normalize config keys from UI schema to internal shape
1018
+ const d = {
1019
+ id: raw.deviceId ?? raw.id,
1020
+ name: raw.configDeviceName ?? raw.name,
1021
+ type: raw.configDeviceType ?? raw.type ?? raw.deviceType ?? 'unknown',
1022
+ _raw: raw,
1023
+ };
1024
+ let type = d.type;
1025
+ // Normalize device type variants for consistent Matter cluster lookup
1026
+ // Vacuum variants
1027
+ if (['wosweeper', 'wosweepermini', 'wosweeperminipro', 'k10+', 'k10+ pro'].includes((type || '').toLowerCase())) {
1028
+ type = 'vacuum';
1029
+ }
1030
+ // Window covering variants
1031
+ if (['blindtilt', 'blind tilt', 'curtain3', 'rollershade', 'roller shade', 'worollershade', 'wo rollershade'].includes((type || '').toLowerCase())) {
1032
+ type = 'curtain';
1033
+ }
1034
+ // Plug variants
1035
+ if (['plug mini (jp)', 'plug mini (us)'].includes((type || '').toLowerCase())) {
1036
+ type = 'plug';
1037
+ }
1038
+ // Meter variants
1039
+ if (['meterplus', 'meter plus (jp)', 'meterpro', 'meterpro(co2)'].includes((type || '').toLowerCase())) {
1040
+ type = 'meter';
1041
+ }
1042
+ // Relay switch variants
1043
+ if (['relay switch 1', 'relay switch 1pm'].includes((type || '').toLowerCase())) {
1044
+ type = 'relay';
1045
+ }
1046
+ // Water detector variants
1047
+ if (['water detector'].includes((type || '').toLowerCase())) {
1048
+ type = 'waterdetector';
1049
+ }
1050
+ // Fan variants
1051
+ if (['smart fan'].includes((type || '').toLowerCase())) {
1052
+ type = 'fan';
1053
+ }
1054
+ // Light variants
1055
+ if (['strip light'].includes((type || '').toLowerCase())) {
1056
+ type = 'lightstrip';
1057
+ }
1058
+ const matterSupported = !!DEVICE_MATTER_SUPPORTED[(type || '').toLowerCase()];
1059
+ // Auto-detect Matter from Homebridge API, allow manual override via config
1060
+ const matterAvailable = !!(this.api?.isMatterAvailable?.() && this.api?.isMatterEnabled?.());
1061
+ const matterEnabled = matterAvailable || !!this.config.enableMatter;
1062
+ const useMatter = !!(matterEnabled && matterSupported && (!!this.config.preferMatter || matterAvailable));
1063
+ try {
1064
+ const created = await createDevice({ id: d.id, type, name: d.name }, this.config, useMatter);
1065
+ this.devices.push(created);
1066
+ // Prefer Matter: try registering to the Matter child bridge first.
1067
+ let matterRegistered = false;
1068
+ if (useMatter) {
1069
+ this.log.info(`Attempting Matter registration for ${d.id} (${type})`);
1070
+ // If Homebridge Matter APIs are available, register the accessory
1071
+ try {
1072
+ const matterApi = this.api?.matter;
1073
+ if (this.api?.isMatterAvailable?.() && this.api?.isMatterEnabled?.() && matterApi && typeof matterApi.registerPlatformAccessories === 'function') {
1074
+ const createdDesc = created.createAccessory?.(this.api) || { id: d.id, name: d.name || type };
1075
+ const uuid = matterApi.uuid.generate(`${d.id}`);
1076
+ const defaultClusters = DEVICE_MATTER_CLUSTERS[type.toLowerCase()] || { onOff: { onOff: false } };
1077
+ const clusters = createdDesc.clusters || defaultClusters;
1078
+ const deviceType = resolveMatterDeviceType(matterApi, type, createdDesc.deviceType, clusters);
1079
+ const accessory = {
1080
+ UUID: uuid,
1081
+ displayName: createdDesc.name || d.name || type,
1082
+ deviceType,
1083
+ manufacturer: createdDesc.manufacturer || 'SwitchBot',
1084
+ model: createdDesc.model || type,
1085
+ serialNumber: createdDesc.serialNumber || d.id,
1086
+ reachable: createdDesc.reachable !== false,
1087
+ firmwareRevision: createdDesc.firmwareRevision || '1.0.0',
1088
+ hardwareRevision: createdDesc.hardwareRevision || '',
1089
+ clusters,
1090
+ handlers: createdDesc.handlers || createMatterHandlers(this.log, d.id, type, this.config?._client) || undefined,
1091
+ context: { deviceId: d.id, type, _created: true },
1092
+ };
1093
+ try {
1094
+ await matterApi.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
1095
+ this.accessories.set(uuid, accessory);
1096
+ matterRegistered = true;
1097
+ this.log.info(`Registered Matter accessory ${d.id} (${type}) with uuid=${uuid}`);
1098
+ }
1099
+ catch (e) {
1100
+ this.log.warn('Failed to register Matter accessory with Homebridge matter API', e);
1101
+ }
1102
+ }
1103
+ else {
1104
+ this.log.info('Homebridge Matter API not available or not enabled; will fallback to HAP');
1105
+ }
1106
+ }
1107
+ catch (e) {
1108
+ this.log.warn('Matter registration attempt failed', e);
1109
+ }
1110
+ }
1111
+ // If Matter wasn't registered (either not supported, API missing, or registration failed), fall back to HAP registration.
1112
+ if (!matterRegistered && this.api && this.api.hap) {
1113
+ // Basic HAP accessory creation using homebridge API when available
1114
+ try {
1115
+ const hap = this.api.hap;
1116
+ const uuid = hap.uuid.generate(`${d.id}`);
1117
+ // Reuse cached accessory if available by uuid
1118
+ let accessory = this.accessories.get(uuid);
1119
+ // If not found by uuid, attempt to find by stored deviceId in accessory.context
1120
+ if (!accessory) {
1121
+ for (const [, a] of Array.from(this.accessories.entries())) {
1122
+ try {
1123
+ if (a && a.context && a.context.deviceId === d.id) {
1124
+ accessory = a;
1125
+ break;
1126
+ }
1127
+ }
1128
+ catch (e) {
1129
+ // ignore
1130
+ }
1131
+ }
1132
+ }
1133
+ if (!accessory) {
1134
+ accessory = new this.api.platformAccessory(d.name || type, uuid);
1135
+ // Store device metadata on accessory.context for persistence across restarts
1136
+ try {
1137
+ accessory.context = accessory.context || {};
1138
+ accessory.context.deviceId = d.id;
1139
+ accessory.context.type = type;
1140
+ }
1141
+ catch (e) {
1142
+ // ignore context failures
1143
+ }
1144
+ // Register new accessory with Homebridge so it's cached
1145
+ try {
1146
+ ;
1147
+ this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
1148
+ }
1149
+ catch (e) {
1150
+ // older API variations may require different registration; ignore if unavailable
1151
+ }
1152
+ this.accessories.set(uuid, accessory);
1153
+ }
1154
+ else {
1155
+ // ensure context includes deviceId (in case restored accessory lacked it)
1156
+ try {
1157
+ accessory.context = accessory.context || {};
1158
+ accessory.context.deviceId = accessory.context.deviceId || d.id;
1159
+ accessory.context.type = accessory.context.type || type;
1160
+ }
1161
+ catch (e) {
1162
+ // ignore
1163
+ }
1164
+ }
1165
+ // Add basic service descriptor from device
1166
+ const accDesc = created.createAccessory?.(this.api);
1167
+ if (accDesc && accDesc.services) {
1168
+ for (const s of accDesc.services) {
1169
+ const Service = hap.Service[s.type] || hap.Service[s.type];
1170
+ if (!Service) {
1171
+ continue;
1172
+ }
1173
+ const service = accessory.getService(Service) || accessory.addService(Service);
1174
+ for (const [charName, getterSetterRaw] of Object.entries(s.characteristics || {})) {
1175
+ const getterSetter = getterSetterRaw;
1176
+ const Characteristic = hap.Characteristic[charName];
1177
+ if (!Characteristic) {
1178
+ continue;
1179
+ }
1180
+ // Apply characteristic props if provided (min/max/step)
1181
+ if (getterSetter && getterSetter.props) {
1182
+ try {
1183
+ service.getCharacteristic(Characteristic).setProps(getterSetter.props);
1184
+ }
1185
+ catch (e) {
1186
+ // ignore setProps failures on older HAP implementations
1187
+ }
1188
+ }
1189
+ // Wire simple get/set handlers if provided
1190
+ if (getterSetter && typeof getterSetter.get === 'function') {
1191
+ service.getCharacteristic(Characteristic).onGet(getterSetter.get);
1192
+ }
1193
+ if (getterSetter && typeof getterSetter.set === 'function') {
1194
+ service.getCharacteristic(Characteristic).onSet(getterSetter.set);
1195
+ }
1196
+ }
1197
+ }
1198
+ }
1199
+ this.log.info(`Created/updated HAP accessory ${d.id} (${type})`);
1200
+ }
1201
+ catch (e) {
1202
+ this.log.warn('HAP accessory creation failed', e);
1203
+ }
1204
+ }
1205
+ else if (!matterRegistered) {
1206
+ this.log.info(`Created HAP descriptor for ${d.id} (${type}) (API not available to register)`);
1207
+ }
1208
+ }
1209
+ catch (e) {
1210
+ this.log.error(`Failed to create device ${d.id}:`, e);
1211
+ }
1212
+ }
1213
+ // Update hash after successfully loading devices
1214
+ this.lastConfigHash = this.getConfigHash();
1215
+ }
1216
+ // Example lifecycle method called by Homebridge
1217
+ async configureAccessory(accessory) {
1218
+ // Homebridge calls this for restored cached accessories — keep a reference.
1219
+ try {
1220
+ const uuid = accessory.UUID || accessory.UUID;
1221
+ this.accessories.set(uuid, accessory);
1222
+ this.log.info(`Restored cached accessory ${accessory.displayName || uuid}`);
1223
+ }
1224
+ catch (e) {
1225
+ this.log.warn('configureAccessory failed to restore accessory', e);
1226
+ }
1227
+ }
1228
+ // Called by Homebridge when a cached Matter accessory is restored
1229
+ configureMatterAccessory(accessory) {
1230
+ try {
1231
+ const uuid = accessory.uuid || accessory.UUID || accessory.uuid;
1232
+ this.accessories.set(uuid, accessory);
1233
+ this.log.info(`Restored cached Matter accessory ${accessory.displayName || uuid}`);
1234
+ }
1235
+ catch (e) {
1236
+ this.log.warn('configureMatterAccessory failed to restore accessory', e);
1237
+ }
1238
+ }
1239
+ }
1240
+ // Matter platform implementation (placeholder)
1241
+ export class SwitchBotMatterPlatform {
1242
+ api;
1243
+ log;
1244
+ config;
1245
+ devices = [];
1246
+ accessories;
1247
+ // Track last loaded config to detect changes
1248
+ lastConfigHash = '';
1249
+ configReloadInterval = null;
1250
+ constructor(log, config, api) {
1251
+ this.log = log;
1252
+ this.config = config ?? {};
1253
+ this.api = api;
1254
+ this.accessories = new Map();
1255
+ this.log.info('SwitchBot Matter platform initialized');
1256
+ if (this.api && typeof this.api.on === 'function') {
1257
+ ;
1258
+ this.api.on('didFinishLaunching', () => {
1259
+ ;
1260
+ (async () => {
1261
+ // After launch, perform discovery (if any) and register Matter accessories
1262
+ try {
1263
+ await this.loadDevices();
1264
+ if (this.api.isMatterAvailable?.() && this.api.isMatterEnabled?.() && this.api.matter && typeof this.api.matter.registerPlatformAccessories === 'function') {
1265
+ try {
1266
+ await this.registerMatterAccessories?.();
1267
+ }
1268
+ catch (e) {
1269
+ this.log.warn('registerMatterAccessories failed', e);
1270
+ }
1271
+ }
1272
+ }
1273
+ catch (e) {
1274
+ this.log.warn('Error during Matter platform startup', e);
1275
+ }
1276
+ })();
1277
+ // Start periodic config reload to pick up UI changes
1278
+ this.configReloadInterval = setInterval(() => {
1279
+ void this.checkAndReloadDevices();
1280
+ }, 10000); // Check every 10 seconds
1281
+ });
1282
+ }
1283
+ else {
1284
+ void this.loadDevices();
1285
+ // Start periodic config reload to pick up UI changes
1286
+ this.configReloadInterval = setInterval(() => {
1287
+ void this.checkAndReloadDevices();
1288
+ }, 10000); // Check every 10 seconds
1289
+ }
1290
+ // Create/shared SwitchBot client and attach to config so child devices reuse it.
1291
+ try {
1292
+ const client = new SwitchBotClient(this.config);
1293
+ void client.init();
1294
+ this.config._client = client;
1295
+ }
1296
+ catch (e) {
1297
+ this.log.debug('Failed to create shared SwitchBot client', e);
1298
+ }
1299
+ }
1300
+ async loadDevices() {
1301
+ const devices = this.config?.devices ?? [];
1302
+ for (const raw of devices) {
1303
+ // Normalize config keys produced by the UI schema
1304
+ const d = {
1305
+ id: raw.deviceId ?? raw.id,
1306
+ name: raw.configDeviceName ?? raw.name,
1307
+ type: raw.configDeviceType ?? raw.type ?? raw.deviceType ?? 'unknown',
1308
+ _raw: raw,
1309
+ };
1310
+ let type = d.type;
1311
+ // Normalize device type variants for consistent Matter cluster lookup
1312
+ // Vacuum variants
1313
+ if (['wosweeper', 'wosweepermini', 'wosweeperminipro', 'k10+', 'k10+ pro'].includes((type || '').toLowerCase())) {
1314
+ type = 'vacuum';
1315
+ }
1316
+ // Window covering variants
1317
+ if (['blindtilt', 'blind tilt', 'curtain3', 'rollershade', 'roller shade', 'worollershade', 'wo rollershade'].includes((type || '').toLowerCase())) {
1318
+ type = 'curtain';
1319
+ }
1320
+ // Plug variants
1321
+ if (['plug mini (jp)', 'plug mini (us)'].includes((type || '').toLowerCase())) {
1322
+ type = 'plug';
1323
+ }
1324
+ // Meter variants
1325
+ if (['meterplus', 'meter plus (jp)', 'meterpro', 'meterpro(co2)'].includes((type || '').toLowerCase())) {
1326
+ type = 'meter';
1327
+ }
1328
+ // Relay switch variants
1329
+ if (['relay switch 1', 'relay switch 1pm'].includes((type || '').toLowerCase())) {
1330
+ type = 'relay';
1331
+ }
1332
+ // Water detector variants
1333
+ if (['water detector'].includes((type || '').toLowerCase())) {
1334
+ type = 'waterdetector';
1335
+ }
1336
+ // Fan variants
1337
+ if (['smart fan'].includes((type || '').toLowerCase())) {
1338
+ type = 'fan';
1339
+ }
1340
+ // Light variants
1341
+ if (['strip light'].includes((type || '').toLowerCase())) {
1342
+ type = 'lightstrip';
1343
+ }
1344
+ const matterSupported = !!DEVICE_MATTER_SUPPORTED[(type || '').toLowerCase()];
1345
+ // Auto-detect Matter from Homebridge API, allow manual override via config
1346
+ const matterAvailable = this.api?.isMatterAvailable?.() && this.api?.isMatterEnabled?.();
1347
+ const matterEnabled = matterAvailable || !!this.config.enableMatter;
1348
+ const useMatter = matterEnabled && matterSupported;
1349
+ try {
1350
+ const created = await createDevice({ id: d.id, type, name: d.name }, this.config, useMatter);
1351
+ this.devices.push(created);
1352
+ if (useMatter) {
1353
+ this.log.info(`Prepared Matter accessory for ${d.id} (${type})${matterAvailable ? ' (auto-detected)' : ' (manually enabled)'}`);
1354
+ }
1355
+ else {
1356
+ if (!matterEnabled) {
1357
+ this.log.info(`Skipping Matter for ${d.id} (${type}) - Matter not available on this bridge`);
1358
+ }
1359
+ else if (!matterSupported) {
1360
+ this.log.info(`Skipping Matter for ${d.id} (${type}) - device type not supported`);
1361
+ }
1362
+ else {
1363
+ this.log.info(`Skipping Matter for ${d.id} (${type}) - not supported`);
1364
+ }
1365
+ }
1366
+ }
1367
+ catch (e) {
1368
+ this.log.error(`Failed to create Matter device ${d.id}:`, e);
1369
+ }
1370
+ }
1371
+ // Update hash after successfully loading devices
1372
+ this.lastConfigHash = this.getConfigHash();
1373
+ }
1374
+ getConfigHash() {
1375
+ // Create a simple hash of current device config to detect changes
1376
+ const devices = this.config?.devices ?? [];
1377
+ return JSON.stringify(devices.map((d) => ({
1378
+ id: d.deviceId ?? d.id,
1379
+ type: d.configDeviceType ?? d.type,
1380
+ name: d.configDeviceName ?? d.name,
1381
+ })));
1382
+ }
1383
+ async checkAndReloadDevices() {
1384
+ const currentHash = this.getConfigHash();
1385
+ if (currentHash !== this.lastConfigHash) {
1386
+ this.log.info('[SwitchBot] Detected config changes, reloading devices...');
1387
+ // Clear existing devices
1388
+ this.devices = [];
1389
+ await this.loadDevices();
1390
+ }
1391
+ }
1392
+ async configureAccessory(accessory) {
1393
+ try {
1394
+ const uuid = accessory.UUID || accessory.UUID;
1395
+ this.accessories.set(uuid, accessory);
1396
+ this.log.info(`Restored cached Matter accessory ${accessory.displayName || uuid}`);
1397
+ }
1398
+ catch (e) {
1399
+ this.log.warn('configureAccessory failed to restore Matter accessory', e);
1400
+ }
1401
+ }
1402
+ // Homebridge calls this when restoring cached Matter accessories
1403
+ configureMatterAccessory(accessory) {
1404
+ try {
1405
+ const uuid = accessory.uuid || accessory.UUID || accessory.uuid;
1406
+ this.accessories.set(uuid, accessory);
1407
+ this.log.info(`Restored cached Matter accessory ${accessory.displayName || uuid}`);
1408
+ }
1409
+ catch (e) {
1410
+ this.log.warn('configureMatterAccessory failed to restore Matter accessory', e);
1411
+ }
1412
+ }
1413
+ // Register serialized Matter accessories via Homebridge Matter API
1414
+ async registerMatterAccessories() {
1415
+ if (!this.api) {
1416
+ return;
1417
+ }
1418
+ const matterApi = this.api.matter;
1419
+ if (!matterApi || typeof matterApi.registerPlatformAccessories !== 'function') {
1420
+ this.log.info('Homebridge Matter API not available; skipping Matter accessory registration');
1421
+ return;
1422
+ }
1423
+ const devices = this.config?.devices ?? [];
1424
+ const accessoriesToRegister = [];
1425
+ // Auto-detect Matter from Homebridge API
1426
+ const matterAvailable = this.api?.isMatterAvailable?.() && this.api?.isMatterEnabled?.();
1427
+ const matterEnabled = matterAvailable || !!this.config.enableMatter;
1428
+ for (const raw of devices) {
1429
+ const d = {
1430
+ id: raw.deviceId ?? raw.id,
1431
+ name: raw.configDeviceName ?? raw.name,
1432
+ type: raw.configDeviceType ?? raw.type ?? raw.deviceType ?? 'unknown',
1433
+ };
1434
+ if (!d.id) {
1435
+ continue;
1436
+ }
1437
+ let type = d.type;
1438
+ // Normalize device type variants for consistent Matter cluster lookup
1439
+ // Vacuum variants
1440
+ if (['wosweeper', 'wosweepermini', 'wosweeperminipro', 'k10+', 'k10+ pro'].includes((type || '').toLowerCase())) {
1441
+ type = 'vacuum';
1442
+ }
1443
+ // Window covering variants
1444
+ if (['blindtilt', 'blind tilt', 'curtain3', 'rollershade', 'roller shade', 'worollershade', 'wo rollershade'].includes((type || '').toLowerCase())) {
1445
+ type = 'curtain';
1446
+ }
1447
+ // Plug variants
1448
+ if (['plug mini (jp)', 'plug mini (us)'].includes((type || '').toLowerCase())) {
1449
+ type = 'plug';
1450
+ }
1451
+ // Meter variants
1452
+ if (['meterplus', 'meter plus (jp)', 'meterpro', 'meterpro(co2)'].includes((type || '').toLowerCase())) {
1453
+ type = 'meter';
1454
+ }
1455
+ // Relay switch variants
1456
+ if (['relay switch 1', 'relay switch 1pm'].includes((type || '').toLowerCase())) {
1457
+ type = 'relay';
1458
+ }
1459
+ // Water detector variants
1460
+ if (['water detector'].includes((type || '').toLowerCase())) {
1461
+ type = 'waterdetector';
1462
+ }
1463
+ // Fan variants
1464
+ if (['smart fan'].includes((type || '').toLowerCase())) {
1465
+ type = 'fan';
1466
+ }
1467
+ // Light variants
1468
+ if (['strip light'].includes((type || '').toLowerCase())) {
1469
+ type = 'lightstrip';
1470
+ }
1471
+ const matterSupported = !!DEVICE_MATTER_SUPPORTED[(type || '').toLowerCase()];
1472
+ const useMatter = matterEnabled && matterSupported;
1473
+ if (!useMatter) {
1474
+ continue;
1475
+ }
1476
+ try {
1477
+ const created = await createDevice({ id: d.id, type, name: d.name }, this.config, true);
1478
+ const createdDesc = created.createAccessory?.(this.api) || { id: d.id, name: d.name || type };
1479
+ const uuid = matterApi.uuid.generate(`${d.id}`);
1480
+ // Try to find existing restored accessory by deviceId
1481
+ let existing;
1482
+ for (const [, a] of Array.from(this.accessories.entries())) {
1483
+ try {
1484
+ if (a && a.context && a.context.deviceId === d.id) {
1485
+ existing = a;
1486
+ break;
1487
+ }
1488
+ }
1489
+ catch (e) {
1490
+ // ignore
1491
+ }
1492
+ }
1493
+ if (existing) {
1494
+ // Ensure context and displayName are up to date
1495
+ // Prioritize device-specific Matter clusters (e.g., RVC for vacuum) over generic HAP-derived clusters
1496
+ let clusters = DEVICE_MATTER_CLUSTERS[type.toLowerCase()];
1497
+ if (!clusters) {
1498
+ clusters = existing.clusters || createdDesc.clusters || { onOff: { onOff: false } };
1499
+ }
1500
+ const deviceType = resolveMatterDeviceType(matterApi, type, existing.deviceType || createdDesc.deviceType, clusters);
1501
+ existing.context = existing.context || {};
1502
+ existing.context.deviceId = existing.context.deviceId || d.id;
1503
+ existing.context.type = existing.context.type || type;
1504
+ existing.deviceType = deviceType;
1505
+ existing.manufacturer = existing.manufacturer || createdDesc.manufacturer || 'SwitchBot';
1506
+ existing.model = existing.model || createdDesc.model || type;
1507
+ existing.serialNumber = existing.serialNumber || createdDesc.serialNumber || d.id;
1508
+ existing.reachable = existing.reachable !== false;
1509
+ existing.firmwareRevision = existing.firmwareRevision || createdDesc.firmwareRevision || '1.0.0';
1510
+ existing.hardwareRevision = existing.hardwareRevision || createdDesc.hardwareRevision || '';
1511
+ existing.clusters = clusters;
1512
+ existing.handlers = createdDesc.handlers || createMatterHandlers(this.log, d.id, type, this.config?._client) || undefined;
1513
+ existing.displayName = createdDesc.name || d.name || type;
1514
+ existing.UUID = existing.UUID || existing.uuid || uuid;
1515
+ accessoriesToRegister.push(existing);
1516
+ this.accessories.set(existing.UUID || uuid, existing);
1517
+ }
1518
+ else {
1519
+ // Prioritize device-specific Matter clusters (e.g., RVC for vacuum) over generic HAP-derived clusters
1520
+ let clusters = DEVICE_MATTER_CLUSTERS[type.toLowerCase()];
1521
+ if (!clusters) {
1522
+ clusters = createdDesc.clusters || { onOff: { onOff: false } };
1523
+ }
1524
+ const deviceType = resolveMatterDeviceType(matterApi, type, createdDesc.deviceType, clusters);
1525
+ const serialized = {
1526
+ UUID: uuid,
1527
+ displayName: createdDesc.name || d.name || type,
1528
+ deviceType,
1529
+ manufacturer: createdDesc.manufacturer || 'SwitchBot',
1530
+ model: createdDesc.model || type,
1531
+ serialNumber: createdDesc.serialNumber || d.id,
1532
+ reachable: createdDesc.reachable !== false,
1533
+ firmwareRevision: createdDesc.firmwareRevision || '1.0.0',
1534
+ hardwareRevision: createdDesc.hardwareRevision || '',
1535
+ clusters,
1536
+ handlers: createdDesc.handlers || createMatterHandlers(this.log, d.id, type, this.config?._client) || undefined,
1537
+ context: { deviceId: d.id, type, created: true },
1538
+ };
1539
+ accessoriesToRegister.push(serialized);
1540
+ this.accessories.set(uuid, serialized);
1541
+ }
1542
+ }
1543
+ catch (e) {
1544
+ this.log.warn(`Failed to prepare Matter accessory for ${d.id} (${type})`, e);
1545
+ }
1546
+ }
1547
+ if (accessoriesToRegister.length > 0) {
1548
+ try {
1549
+ await matterApi.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, accessoriesToRegister);
1550
+ this.log.info(`Registered ${accessoriesToRegister.length} Matter accessory(ies) with Homebridge`);
1551
+ }
1552
+ catch (e) {
1553
+ this.log.warn('Failed to register Matter accessories', e);
1554
+ }
1555
+ }
1556
+ else {
1557
+ this.log.info('No Matter accessories to register');
1558
+ }
1559
+ }
1560
+ }
1561
+ export default SwitchBotHAPPlatform;
1562
+ //# sourceMappingURL=platform.js.map