@switchbot/homebridge-switchbot 5.0.0-beta.11 → 5.0.0-beta.111

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