@switchbot/homebridge-switchbot 5.0.0-beta.13 → 5.0.0-beta.131

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