@switchbot/homebridge-switchbot 5.0.0-beta.12 → 5.0.0-beta.121

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