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

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