@switchbot/homebridge-switchbot 5.0.0-beta.99 → 5.0.0

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 (282) hide show
  1. package/.changeset/config.json +14 -0
  2. package/.github/copilot-instructions.md +39 -0
  3. package/.github/workflows/ci.yml +4 -1
  4. package/.github/workflows/manual-e2e.yml +6 -3
  5. package/.github/workflows/release.yml +64 -15
  6. package/.github/workflows/stale.yml +2 -4
  7. package/.husky/pre-push +15 -0
  8. package/CHANGELOG.md +126 -134
  9. package/MIGRATION.md +16 -6
  10. package/README.md +84 -3
  11. package/TODO.md +263 -0
  12. package/config.schema.json +229 -36
  13. package/dist/SwitchBotHAPPlatform.d.ts +133 -0
  14. package/dist/SwitchBotHAPPlatform.d.ts.map +1 -0
  15. package/dist/SwitchBotHAPPlatform.js +555 -0
  16. package/dist/SwitchBotHAPPlatform.js.map +1 -0
  17. package/dist/SwitchBotMatterPlatform.d.ts +141 -0
  18. package/dist/SwitchBotMatterPlatform.d.ts.map +1 -0
  19. package/dist/SwitchBotMatterPlatform.js +536 -0
  20. package/dist/SwitchBotMatterPlatform.js.map +1 -0
  21. package/dist/device-types.d.ts +31 -0
  22. package/dist/device-types.d.ts.map +1 -0
  23. package/dist/device-types.js +246 -0
  24. package/dist/device-types.js.map +1 -0
  25. package/dist/deviceCommandMapper.d.ts +10 -0
  26. package/dist/deviceCommandMapper.d.ts.map +1 -0
  27. package/dist/deviceCommandMapper.js +319 -0
  28. package/dist/deviceCommandMapper.js.map +1 -0
  29. package/dist/deviceFactory.d.ts +3 -2
  30. package/dist/deviceFactory.d.ts.map +1 -1
  31. package/dist/deviceFactory.js +107 -29
  32. package/dist/deviceFactory.js.map +1 -1
  33. package/dist/devices/genericDevice.d.ts +59 -37
  34. package/dist/devices/genericDevice.d.ts.map +1 -1
  35. package/dist/devices/genericDevice.js +376 -78
  36. package/dist/devices/genericDevice.js.map +1 -1
  37. package/dist/errors.d.ts +38 -0
  38. package/dist/errors.d.ts.map +1 -0
  39. package/dist/errors.js +32 -0
  40. package/dist/errors.js.map +1 -0
  41. package/dist/homebridge-ui/device-types.js +246 -0
  42. package/dist/homebridge-ui/device-types.js.map +1 -0
  43. package/dist/homebridge-ui/deviceCommandMapper.js +319 -0
  44. package/dist/homebridge-ui/deviceCommandMapper.js.map +1 -0
  45. package/dist/homebridge-ui/endpoints/config.d.ts +3 -0
  46. package/dist/homebridge-ui/endpoints/config.d.ts.map +1 -0
  47. package/dist/homebridge-ui/endpoints/config.js +90 -0
  48. package/dist/homebridge-ui/endpoints/config.js.map +1 -0
  49. package/dist/homebridge-ui/endpoints/devices.d.ts +6 -0
  50. package/dist/homebridge-ui/endpoints/devices.d.ts.map +1 -0
  51. package/dist/homebridge-ui/endpoints/devices.js +144 -0
  52. package/dist/homebridge-ui/endpoints/devices.js.map +1 -0
  53. package/dist/homebridge-ui/endpoints/discovery.d.ts +7 -0
  54. package/dist/homebridge-ui/endpoints/discovery.d.ts.map +1 -0
  55. package/dist/homebridge-ui/endpoints/discovery.js +219 -0
  56. package/dist/homebridge-ui/endpoints/discovery.js.map +1 -0
  57. package/dist/homebridge-ui/errors.js +32 -0
  58. package/dist/homebridge-ui/errors.js.map +1 -0
  59. package/dist/homebridge-ui/homebridge-ui/endpoints/config.js +90 -0
  60. package/dist/homebridge-ui/homebridge-ui/endpoints/config.js.map +1 -0
  61. package/dist/homebridge-ui/homebridge-ui/endpoints/devices.js +144 -0
  62. package/dist/homebridge-ui/homebridge-ui/endpoints/devices.js.map +1 -0
  63. package/dist/homebridge-ui/homebridge-ui/endpoints/discovery.js +219 -0
  64. package/dist/homebridge-ui/homebridge-ui/endpoints/discovery.js.map +1 -0
  65. package/dist/homebridge-ui/homebridge-ui/server.js +11 -0
  66. package/dist/homebridge-ui/homebridge-ui/server.js.map +1 -0
  67. package/dist/homebridge-ui/homebridge-ui/utils/config-parser.js +108 -0
  68. package/dist/homebridge-ui/homebridge-ui/utils/config-parser.js.map +1 -0
  69. package/dist/homebridge-ui/homebridge-ui/utils/device-migration.js +111 -0
  70. package/dist/homebridge-ui/homebridge-ui/utils/device-migration.js.map +1 -0
  71. package/dist/homebridge-ui/homebridge-ui/utils/logger.js +17 -0
  72. package/dist/homebridge-ui/homebridge-ui/utils/logger.js.map +1 -0
  73. package/dist/homebridge-ui/public/css/styles.css +483 -0
  74. package/dist/homebridge-ui/public/index.html +197 -621
  75. package/dist/homebridge-ui/public/js/advanced-settings.d.ts +3 -0
  76. package/dist/homebridge-ui/public/js/advanced-settings.d.ts.map +1 -0
  77. package/dist/homebridge-ui/public/js/advanced-settings.js +95 -0
  78. package/dist/homebridge-ui/public/js/advanced-settings.js.map +1 -0
  79. package/dist/homebridge-ui/public/js/advanced-settings.ts +94 -0
  80. package/dist/homebridge-ui/public/js/api.d.ts +66 -0
  81. package/dist/homebridge-ui/public/js/api.d.ts.map +1 -0
  82. package/dist/homebridge-ui/public/js/api.js +295 -0
  83. package/dist/homebridge-ui/public/js/api.js.map +1 -0
  84. package/dist/homebridge-ui/public/js/api.ts +355 -0
  85. package/dist/homebridge-ui/public/js/app.d.ts +2 -0
  86. package/dist/homebridge-ui/public/js/app.d.ts.map +1 -0
  87. package/dist/homebridge-ui/public/js/app.js +3722 -0
  88. package/dist/homebridge-ui/public/js/app.js.map +7 -0
  89. package/dist/homebridge-ui/public/js/app.ts +22 -0
  90. package/dist/homebridge-ui/public/js/constants.d.ts +2 -0
  91. package/dist/homebridge-ui/public/js/constants.d.ts.map +1 -0
  92. package/dist/homebridge-ui/public/js/constants.js +2 -0
  93. package/dist/homebridge-ui/public/js/constants.js.map +1 -0
  94. package/dist/homebridge-ui/public/js/constants.ts +1 -0
  95. package/dist/homebridge-ui/public/js/credentials.d.ts +3 -0
  96. package/dist/homebridge-ui/public/js/credentials.d.ts.map +1 -0
  97. package/dist/homebridge-ui/public/js/credentials.js +99 -0
  98. package/dist/homebridge-ui/public/js/credentials.js.map +1 -0
  99. package/dist/homebridge-ui/public/js/credentials.ts +105 -0
  100. package/dist/homebridge-ui/public/js/devices-delete.d.ts +3 -0
  101. package/dist/homebridge-ui/public/js/devices-delete.d.ts.map +1 -0
  102. package/dist/homebridge-ui/public/js/devices-delete.js +199 -0
  103. package/dist/homebridge-ui/public/js/devices-delete.js.map +1 -0
  104. package/dist/homebridge-ui/public/js/devices-delete.ts +227 -0
  105. package/dist/homebridge-ui/public/js/devices.d.ts +9 -0
  106. package/dist/homebridge-ui/public/js/devices.d.ts.map +1 -0
  107. package/dist/homebridge-ui/public/js/devices.js +98 -0
  108. package/dist/homebridge-ui/public/js/devices.js.map +1 -0
  109. package/dist/homebridge-ui/public/js/devices.ts +106 -0
  110. package/dist/homebridge-ui/public/js/discovery.d.ts +9 -0
  111. package/dist/homebridge-ui/public/js/discovery.d.ts.map +1 -0
  112. package/dist/homebridge-ui/public/js/discovery.js +1201 -0
  113. package/dist/homebridge-ui/public/js/discovery.js.map +1 -0
  114. package/dist/homebridge-ui/public/js/discovery.ts +1335 -0
  115. package/dist/homebridge-ui/public/js/logger.d.ts +7 -0
  116. package/dist/homebridge-ui/public/js/logger.d.ts.map +1 -0
  117. package/dist/homebridge-ui/public/js/logger.js +17 -0
  118. package/dist/homebridge-ui/public/js/logger.js.map +1 -0
  119. package/dist/homebridge-ui/public/js/logger.ts +17 -0
  120. package/dist/homebridge-ui/public/js/modal.d.ts +5 -0
  121. package/dist/homebridge-ui/public/js/modal.d.ts.map +1 -0
  122. package/dist/homebridge-ui/public/js/modal.js +35 -0
  123. package/dist/homebridge-ui/public/js/modal.js.map +1 -0
  124. package/dist/homebridge-ui/public/js/modal.ts +35 -0
  125. package/dist/homebridge-ui/public/js/modals.d.ts +15 -0
  126. package/dist/homebridge-ui/public/js/modals.d.ts.map +1 -0
  127. package/dist/homebridge-ui/public/js/modals.js +675 -0
  128. package/dist/homebridge-ui/public/js/modals.js.map +1 -0
  129. package/dist/homebridge-ui/public/js/modals.ts +765 -0
  130. package/dist/homebridge-ui/public/js/render.d.ts +71 -0
  131. package/dist/homebridge-ui/public/js/render.d.ts.map +1 -0
  132. package/dist/homebridge-ui/public/js/render.js +960 -0
  133. package/dist/homebridge-ui/public/js/render.js.map +1 -0
  134. package/dist/homebridge-ui/public/js/render.ts +1084 -0
  135. package/dist/homebridge-ui/public/js/toast.d.ts +6 -0
  136. package/dist/homebridge-ui/public/js/toast.d.ts.map +1 -0
  137. package/dist/homebridge-ui/public/js/toast.js +38 -0
  138. package/dist/homebridge-ui/public/js/toast.js.map +1 -0
  139. package/dist/homebridge-ui/public/js/toast.ts +44 -0
  140. package/dist/homebridge-ui/public/js/types.d.ts +23 -0
  141. package/dist/homebridge-ui/public/js/types.d.ts.map +1 -0
  142. package/dist/homebridge-ui/public/js/types.js +2 -0
  143. package/dist/homebridge-ui/public/js/types.js.map +1 -0
  144. package/dist/homebridge-ui/public/js/types.ts +26 -0
  145. package/dist/homebridge-ui/server.d.ts +1 -3
  146. package/dist/homebridge-ui/server.d.ts.map +1 -1
  147. package/dist/homebridge-ui/server.js +8 -471
  148. package/dist/homebridge-ui/server.js.map +1 -1
  149. package/dist/homebridge-ui/settings.js +8 -0
  150. package/dist/homebridge-ui/settings.js.map +1 -0
  151. package/dist/homebridge-ui/switchbotClient.js +247 -0
  152. package/dist/homebridge-ui/switchbotClient.js.map +1 -0
  153. package/dist/homebridge-ui/utils/config-parser.d.ts +39 -0
  154. package/dist/homebridge-ui/utils/config-parser.d.ts.map +1 -0
  155. package/dist/homebridge-ui/utils/config-parser.js +108 -0
  156. package/dist/homebridge-ui/utils/config-parser.js.map +1 -0
  157. package/dist/homebridge-ui/utils/device-migration.d.ts +35 -0
  158. package/dist/homebridge-ui/utils/device-migration.d.ts.map +1 -0
  159. package/dist/homebridge-ui/utils/device-migration.js +111 -0
  160. package/dist/homebridge-ui/utils/device-migration.js.map +1 -0
  161. package/dist/homebridge-ui/utils/logger.d.ts +7 -0
  162. package/dist/homebridge-ui/utils/logger.d.ts.map +1 -0
  163. package/dist/homebridge-ui/utils/logger.js +17 -0
  164. package/dist/homebridge-ui/utils/logger.js.map +1 -0
  165. package/dist/index.d.ts +10 -0
  166. package/dist/index.d.ts.map +1 -1
  167. package/dist/index.js +12 -2
  168. package/dist/index.js.map +1 -1
  169. package/dist/settings.d.ts +1 -0
  170. package/dist/settings.d.ts.map +1 -1
  171. package/dist/settings.js +1 -0
  172. package/dist/settings.js.map +1 -1
  173. package/dist/switchbotClient.d.ts +12 -10
  174. package/dist/switchbotClient.d.ts.map +1 -1
  175. package/dist/switchbotClient.js +156 -103
  176. package/dist/switchbotClient.js.map +1 -1
  177. package/dist/utils.d.ts +76 -1
  178. package/dist/utils.d.ts.map +1 -1
  179. package/dist/utils.js +1121 -4
  180. package/dist/utils.js.map +1 -1
  181. package/docs/assets/highlight.css +16 -2
  182. package/docs/assets/main.js +1 -1
  183. package/docs/index.html +82 -5
  184. package/docs/variables/default.html +3 -1
  185. package/eslint.config.js +9 -5
  186. package/nodemon.json +2 -2
  187. package/package.json +34 -21
  188. package/scripts/build-ui.js +37 -0
  189. package/scripts/free-dev-ports.mjs +105 -0
  190. package/scripts/generate-matter-maps.js +34 -17
  191. package/scripts/sync-device-types.mjs +31 -0
  192. package/src/SwitchBotHAPPlatform.ts +558 -0
  193. package/src/SwitchBotMatterPlatform.ts +538 -0
  194. package/src/device-types.js +246 -0
  195. package/src/device-types.js.map +1 -0
  196. package/src/device-types.ts +261 -0
  197. package/src/deviceCommandMapper.js +319 -0
  198. package/src/deviceCommandMapper.js.map +1 -0
  199. package/src/deviceCommandMapper.ts +333 -0
  200. package/src/deviceFactory.ts +125 -45
  201. package/src/devices/genericDevice.ts +411 -69
  202. package/src/errors.js +32 -0
  203. package/src/errors.js.map +1 -0
  204. package/src/errors.ts +35 -0
  205. package/src/homebridge-ui/endpoints/config.ts +110 -0
  206. package/src/homebridge-ui/endpoints/devices.ts +153 -0
  207. package/src/homebridge-ui/endpoints/discovery.ts +240 -0
  208. package/src/homebridge-ui/public/css/styles.css +483 -0
  209. package/src/homebridge-ui/public/index.html +197 -621
  210. package/src/homebridge-ui/public/js/advanced-settings.ts +94 -0
  211. package/src/homebridge-ui/public/js/api.ts +355 -0
  212. package/src/homebridge-ui/public/js/app.ts +22 -0
  213. package/src/homebridge-ui/public/js/constants.ts +1 -0
  214. package/src/homebridge-ui/public/js/credentials.ts +105 -0
  215. package/src/homebridge-ui/public/js/devices-delete.ts +227 -0
  216. package/src/homebridge-ui/public/js/devices.ts +106 -0
  217. package/src/homebridge-ui/public/js/discovery.ts +1335 -0
  218. package/src/homebridge-ui/public/js/logger.ts +17 -0
  219. package/src/homebridge-ui/public/js/modal.ts +35 -0
  220. package/src/homebridge-ui/public/js/modals.ts +765 -0
  221. package/src/homebridge-ui/public/js/render.ts +1084 -0
  222. package/src/homebridge-ui/public/js/toast.ts +44 -0
  223. package/src/homebridge-ui/public/js/types.ts +26 -0
  224. package/src/homebridge-ui/server.ts +9 -554
  225. package/src/homebridge-ui/utils/config-parser.ts +125 -0
  226. package/src/homebridge-ui/utils/device-migration.ts +144 -0
  227. package/src/homebridge-ui/utils/logger.ts +17 -0
  228. package/src/index.ts +12 -2
  229. package/src/settings.js +8 -0
  230. package/src/settings.js.map +1 -0
  231. package/src/settings.ts +2 -0
  232. package/src/switchbotClient.js +247 -0
  233. package/src/switchbotClient.js.map +1 -0
  234. package/src/switchbotClient.ts +177 -114
  235. package/src/utils.ts +1133 -5
  236. package/test/client/switchbot-client-debounce.spec.ts +35 -0
  237. package/test/client/switchbot-client-openapi.spec.ts +19 -0
  238. package/test/client/switchbotClient.spec.ts +64 -0
  239. package/test/device/device-mapping.spec.ts +23 -0
  240. package/test/device/deviceBase.spec.ts +26 -0
  241. package/test/device/deviceFactory-edge.spec.ts +15 -0
  242. package/test/device/deviceFactory.spec.ts +33 -0
  243. package/test/device/fan-swing.spec.ts +34 -0
  244. package/test/device/genericDevice-blepoll.spec.ts +47 -0
  245. package/test/device/irdevice.spec.ts +9 -0
  246. package/test/device/lock-users.spec.ts +35 -0
  247. package/test/device/matter-descriptors.spec.ts +22 -0
  248. package/test/device/matter-device-state.spec.ts +37 -0
  249. package/test/e2e/run-e2e.spec.ts +18 -19
  250. package/test/errors/errors.spec.ts +10 -0
  251. package/test/helpers/matter-harness.ts +20 -9
  252. package/test/homebridge-ui/server.spec.ts +9 -0
  253. package/test/platform/accessory-restore.spec.ts +37 -0
  254. package/test/platform/matter-childbridge.spec.ts +34 -0
  255. package/test/platform/matter-integration.spec.ts +33 -0
  256. package/test/platform/platform-edge.spec.ts +73 -0
  257. package/test/platform/platform.integration.spec.ts +34 -0
  258. package/test/utils/utils-extra.spec.ts +10 -0
  259. package/test/utils/utils.spec.ts +53 -0
  260. package/todo/TODO.md +80 -0
  261. package/tsconfig.ui.json +11 -0
  262. package/.github/npm-version-script-esm.js +0 -97
  263. package/.github/workflows/beta-release.yml +0 -52
  264. package/dist/platform.d.ts +0 -35
  265. package/dist/platform.d.ts.map +0 -1
  266. package/dist/platform.js +0 -945
  267. package/dist/platform.js.map +0 -1
  268. package/src/platform.ts +0 -963
  269. package/test/accessory-restore.spec.ts +0 -73
  270. package/test/device-mapping.spec.ts +0 -37
  271. package/test/deviceFactory.spec.ts +0 -18
  272. package/test/fan-swing.spec.ts +0 -29
  273. package/test/lock-users.spec.ts +0 -44
  274. package/test/matter-childbridge.spec.ts +0 -55
  275. package/test/matter-descriptors.spec.ts +0 -97
  276. package/test/matter-device-state.spec.ts +0 -101
  277. package/test/matter-integration.spec.ts +0 -70
  278. package/test/platform.integration.spec.ts +0 -55
  279. package/test/switchbot-client-debounce.spec.ts +0 -131
  280. package/test/switchbot-client-openapi.spec.ts +0 -56
  281. package/test/switchbotClient.spec.ts +0 -10
  282. package/test/utils.spec.ts +0 -20
package/dist/utils.js CHANGED
@@ -1,4 +1,1016 @@
1
- // Canonical Matter cluster ID mapping (from matter.js clusters)
1
+ /**
2
+ * Indicates which device types should prefer Matter if available.
3
+ * Based on HAP service mappings: device implementations use specific HomeKit services
4
+ * that map to corresponding Matter clusters when Matter is enabled.
5
+ *
6
+ * @property {boolean} [deviceType] - True if the device type supports Matter, false otherwise.
7
+ * @example
8
+ * DEVICE_MATTER_SUPPORTED['bot'] // true
9
+ */
10
+ /**
11
+ * Factory function to create Matter handlers with Homebridge logger integration.
12
+ * Returns handler objects for supported device types, mapping Matter cluster actions to SwitchBot API calls.
13
+ *
14
+ * @param log - Homebridge logger instance
15
+ * @param deviceId - SwitchBot device ID
16
+ * @param type - Device type string
17
+ * @param client - SwitchBot client instance
18
+ * @returns Handler object for Matter clusters
19
+ */
20
+ export const DEVICE_MATTER_SUPPORTED = {
21
+ // Core devices
22
+ 'bot': true, // Switch → OnOff
23
+ 'curtain': true, // WindowCovering → WindowCovering
24
+ 'fan': true, // Fan → FanControl
25
+ 'light': true, // Lightbulb → OnOff + LevelControl
26
+ 'lightstrip': true, // Lightbulb (color) → OnOff + LevelControl + ColorControl
27
+ 'motion': true, // MotionSensor → OccupancySensing
28
+ 'contact': true, // ContactSensor → BooleanState
29
+ 'vacuum': true, // Switch → RobotVacuumCleaner
30
+ 'lock': true, // LockMechanism → DoorLock
31
+ 'humidifier': true, // Fan + Humidity → OnOff + FanControl + RelativeHumidityMeasurement
32
+ 'temperature': true, // TemperatureSensor → TemperatureMeasurement
33
+ // Switch devices
34
+ 'relay': true, // Switch → OnOff
35
+ 'relay switch 1': true, // Switch → OnOff
36
+ 'relay switch 1pm': true, // Switch → OnOff
37
+ 'plug': true, // Outlet → OnOff
38
+ 'plug mini (jp)': true, // Outlet → OnOff
39
+ 'plug mini (us)': true, // Outlet → OnOff
40
+ // Window covering variants
41
+ 'blindtilt': true, // WindowCovering → WindowCovering
42
+ 'blind tilt': true, // WindowCovering → WindowCovering
43
+ 'curtain3': true, // WindowCovering → WindowCovering
44
+ 'rollershade': true, // WindowCovering → WindowCovering
45
+ 'roller shade': true, // WindowCovering → WindowCovering
46
+ 'worollershade': true, // WindowCovering → WindowCovering
47
+ 'wo rollershade': true, // WindowCovering → WindowCovering
48
+ // Vacuum variants (normalized to 'vacuum' before lookup)
49
+ 'wosweeper': true, // VacuumDevice → RobotVacuumCleaner
50
+ 'wosweepermini': true, // VacuumDevice → RobotVacuumCleaner
51
+ 'wosweeperminipro': true, // VacuumDevice → RobotVacuumCleaner
52
+ 'k10+': true, // VacuumDevice → RobotVacuumCleaner
53
+ 'k10+ pro': true, // VacuumDevice → RobotVacuumCleaner
54
+ // Sensors
55
+ 'meter': true, // TemperatureSensor + HumiditySensor → TemperatureMeasurement + RelativeHumidityMeasurement
56
+ 'meterplus': true, // TemperatureSensor + HumiditySensor → TemperatureMeasurement + RelativeHumidityMeasurement
57
+ 'meter plus (jp)': true, // TemperatureSensor + HumiditySensor → TemperatureMeasurement + RelativeHumidityMeasurement
58
+ 'meterpro': true, // TemperatureSensor + HumiditySensor → TemperatureMeasurement + RelativeHumidityMeasurement
59
+ 'meterpro(co2)': true, // TemperatureSensor + HumiditySensor → TemperatureMeasurement + RelativeHumidityMeasurement
60
+ 'waterdetector': true, // LeakSensor → BooleanState
61
+ 'water detector': true, // LeakSensor → BooleanState
62
+ // Other devices
63
+ 'smart fan': true, // Fan → FanControl
64
+ 'strip light': true, // Lightbulb (color) → OnOff + LevelControl + ColorControl
65
+ 'hub 2': false, // Hub device - not exposed as accessory
66
+ 'walletfinder': false, // Button device - Matter support TBD
67
+ };
68
+ /**
69
+ * Default Matter cluster configurations by device type.
70
+ * Maps device types to their Matter cluster states (used when device doesn't provide clusters).
71
+ * Note: wosweeper/curtain/plug variants are normalized before cluster lookup (see loadDevices).
72
+ *
73
+ * @property {object} [deviceType] - The default cluster state for the device type.
74
+ * @example
75
+ * DEVICE_MATTER_CLUSTERS['bot'] // { onOff: { onOff: false } }
76
+ */
77
+ export const DEVICE_MATTER_CLUSTERS = {
78
+ // Core devices - aligned with HAP service implementations
79
+ bot: { onOff: { onOff: false } }, // Switch → OnOff
80
+ vacuum: {
81
+ rvcRunMode: {
82
+ supportedModes: [
83
+ { label: 'Idle', mode: 0, modeTags: [{ value: 16384 }] },
84
+ { label: 'Cleaning', mode: 1, modeTags: [{ value: 16385 }] },
85
+ ],
86
+ currentMode: 0,
87
+ },
88
+ rvcCleanMode: {
89
+ supportedModes: [
90
+ { label: 'Vacuum', mode: 0, modeTags: [{ value: 16385 }] },
91
+ ],
92
+ currentMode: 0,
93
+ },
94
+ rvcOperationalState: {
95
+ operationalStateList: [
96
+ { operationalStateId: 0 }, // Stopped
97
+ { operationalStateId: 1 }, // Running
98
+ { operationalStateId: 2 }, // Paused
99
+ { operationalStateId: 3 }, // Error (required)
100
+ { operationalStateId: 64 }, // Seeking charger
101
+ { operationalStateId: 65 }, // Charging
102
+ { operationalStateId: 66 }, // Docked
103
+ ],
104
+ operationalState: 66,
105
+ },
106
+ }, // Switch in HAP, RobotVacuumCleaner in Matter
107
+ curtain: {
108
+ windowCovering: {
109
+ currentPositionLiftPercent100ths: 0,
110
+ targetPositionLiftPercent100ths: 0,
111
+ operationalStatus: {
112
+ global: 0,
113
+ lift: 0,
114
+ tilt: 0,
115
+ },
116
+ endProductType: 0,
117
+ configStatus: {
118
+ operational: true,
119
+ onlineReserved: true,
120
+ liftMovementReversed: false,
121
+ liftPositionAware: true,
122
+ tiltPositionAware: false,
123
+ liftEncoderControlled: true,
124
+ tiltEncoderControlled: false,
125
+ },
126
+ },
127
+ }, // WindowCovering → WindowCovering (includes curtain3, rollershade variants via normalization)
128
+ blindtilt: {
129
+ windowCovering: {
130
+ currentPositionLiftPercent100ths: 0,
131
+ targetPositionLiftPercent100ths: 0,
132
+ currentPositionTiltPercent100ths: 0,
133
+ targetPositionTiltPercent100ths: 0,
134
+ operationalStatus: {
135
+ global: 0,
136
+ lift: 0,
137
+ tilt: 0,
138
+ },
139
+ endProductType: 8,
140
+ configStatus: {
141
+ operational: true,
142
+ onlineReserved: true,
143
+ liftMovementReversed: false,
144
+ liftPositionAware: true,
145
+ tiltPositionAware: true,
146
+ liftEncoderControlled: true,
147
+ tiltEncoderControlled: true,
148
+ },
149
+ },
150
+ }, // WindowCovering with tilt → WindowCovering
151
+ fan: {
152
+ onOff: { onOff: false },
153
+ fanControl: {
154
+ fanMode: 0,
155
+ percentCurrent: 0,
156
+ percentSetting: 0,
157
+ speedCurrent: 0,
158
+ speedMax: 100,
159
+ },
160
+ }, // Fan → OnOff + FanControl
161
+ light: {
162
+ onOff: { onOff: false },
163
+ levelControl: {
164
+ currentLevel: 0,
165
+ minLevel: 0,
166
+ maxLevel: 254,
167
+ },
168
+ }, // Lightbulb → OnOff + LevelControl
169
+ lightstrip: {
170
+ onOff: { onOff: false },
171
+ levelControl: {
172
+ currentLevel: 0,
173
+ minLevel: 0,
174
+ maxLevel: 254,
175
+ },
176
+ colorControl: {
177
+ colorMode: 0,
178
+ },
179
+ }, // Lightbulb with color → OnOff + LevelControl + ColorControl
180
+ lock: {
181
+ doorLock: {
182
+ lockState: 0,
183
+ lockType: 0,
184
+ actuatorEnabled: true,
185
+ operatingMode: 0,
186
+ },
187
+ }, // LockMechanism → DoorLock
188
+ motion: {
189
+ occupancySensing: {
190
+ occupancy: 0,
191
+ occupancySensorType: 0,
192
+ },
193
+ }, // MotionSensor → OccupancySensing
194
+ contact: {
195
+ booleanState: {
196
+ stateValue: false,
197
+ },
198
+ }, // ContactSensor → BooleanState
199
+ humidifier: {
200
+ onOff: { onOff: false },
201
+ fanControl: {
202
+ fanMode: 0,
203
+ percentCurrent: 0,
204
+ },
205
+ relativeHumidityMeasurement: {
206
+ measuredValue: 0,
207
+ minMeasuredValue: 0,
208
+ maxMeasuredValue: 100,
209
+ },
210
+ }, // HumidifierDehumidifier → OnOff + FanControl + RelativeHumidityMeasurement
211
+ temperature: {
212
+ temperatureMeasurement: {
213
+ measuredValue: 0,
214
+ minMeasuredValue: -27315,
215
+ maxMeasuredValue: 32767,
216
+ },
217
+ }, // TemperatureSensor → TemperatureMeasurement
218
+ // Switch/Outlet devices
219
+ relay: { onOff: { onOff: false } }, // Switch → OnOff
220
+ plug: {
221
+ onOff: { onOff: false },
222
+ electricalMeasurement: {
223
+ activePower: 0,
224
+ rmsCurrent: 0,
225
+ rmsVoltage: 0,
226
+ },
227
+ }, // Outlet → OnOff + ElectricalMeasurement (for PM models)
228
+ // Sensors
229
+ meter: {
230
+ temperatureMeasurement: {
231
+ measuredValue: 0,
232
+ minMeasuredValue: -27315,
233
+ maxMeasuredValue: 32767,
234
+ },
235
+ relativeHumidityMeasurement: {
236
+ measuredValue: 0,
237
+ minMeasuredValue: 0,
238
+ maxMeasuredValue: 100,
239
+ },
240
+ }, // TemperatureSensor + HumiditySensor → TemperatureMeasurement + RelativeHumidityMeasurement
241
+ waterdetector: {
242
+ booleanState: {
243
+ stateValue: false,
244
+ },
245
+ }, // LeakSensor → BooleanState
246
+ };
247
+ export function createMatterHandlers(log, deviceId, type, client) {
248
+ const lowerType = type.toLowerCase();
249
+ switch (lowerType) {
250
+ case 'vacuum':
251
+ return {
252
+ rvcRunMode: {
253
+ changeToMode: async (request) => {
254
+ const modeNames = ['Idle', 'Cleaning', 'Mapping'];
255
+ const modeName = modeNames[request?.newMode] || `Unknown (${request?.newMode})`;
256
+ log.info(`[${deviceId}] RVC run mode change requested: ${modeName}`);
257
+ if (!client) {
258
+ log.warn(`[${deviceId}] No SwitchBot client available`);
259
+ return { success: false };
260
+ }
261
+ try {
262
+ // For K10+ family: use 'start' to begin cleaning (mode 1 = Cleaning)
263
+ // For older K10+: only supports start/stop/dock
264
+ // For newer K20+/S10/S20: supports startClean with more parameters
265
+ // Map Matter mode to SwitchBot command
266
+ const switchBotCommand = request?.newMode === 1 ? 'start' : 'stop';
267
+ const body = {
268
+ command: switchBotCommand,
269
+ parameter: 'default',
270
+ commandType: 'command',
271
+ };
272
+ log.debug(`[${deviceId}] Sending RVC mode change request:`, JSON.stringify(body));
273
+ const result = await client.setDeviceState(deviceId, body);
274
+ log.debug(`[${deviceId}] RVC mode change API response:`, JSON.stringify(result));
275
+ log.info(`[${deviceId}] RVC mode changed successfully to ${switchBotCommand}`);
276
+ return { success: true, result };
277
+ }
278
+ catch (e) {
279
+ log.error(`[${deviceId}] Failed to change RVC mode:`, e);
280
+ return { success: false, error: e };
281
+ }
282
+ },
283
+ },
284
+ rvcCleanMode: {
285
+ changeToMode: async (request) => {
286
+ const modeName = request?.newMode !== undefined ? `Mode ${request.newMode}` : 'Unknown';
287
+ log.info(`[${deviceId}] RVC clean mode change requested: ${modeName}`);
288
+ if (!client) {
289
+ log.warn(`[${deviceId}] No SwitchBot client available`);
290
+ return { success: false };
291
+ }
292
+ try {
293
+ // Clean mode (vacuum/mop/etc) not directly supported via Matter for K10+
294
+ // K20+ Pro and newer models support via startClean action parameter
295
+ log.info(`[${deviceId}] Clean mode change requires startClean command (not yet implemented for Matter)`);
296
+ return { success: true };
297
+ }
298
+ catch (e) {
299
+ log.error(`[${deviceId}] Failed to change RVC clean mode:`, e);
300
+ return { success: false, error: e };
301
+ }
302
+ },
303
+ },
304
+ rvcOperationalState: {
305
+ pause: async () => {
306
+ log.info(`[${deviceId}] RVC pause command received`);
307
+ if (!client) {
308
+ log.warn(`[${deviceId}] No SwitchBot client available`);
309
+ return { success: false };
310
+ }
311
+ try {
312
+ const body = {
313
+ command: 'stop',
314
+ parameter: 'default',
315
+ commandType: 'command',
316
+ };
317
+ log.debug(`[${deviceId}] Sending RVC pause request:`, JSON.stringify(body));
318
+ const result = await client.setDeviceState(deviceId, body);
319
+ log.debug(`[${deviceId}] RVC pause API response:`, JSON.stringify(result));
320
+ log.info(`[${deviceId}] RVC paused successfully`);
321
+ return { success: true, result };
322
+ }
323
+ catch (e) {
324
+ log.error(`[${deviceId}] Failed to pause RVC:`, e);
325
+ return { success: false, error: e };
326
+ }
327
+ },
328
+ resume: async () => {
329
+ log.info(`[${deviceId}] RVC resume command received`);
330
+ if (!client) {
331
+ log.warn(`[${deviceId}] No SwitchBot client available`);
332
+ return { success: false };
333
+ }
334
+ try {
335
+ const body = {
336
+ command: 'start',
337
+ parameter: 'default',
338
+ commandType: 'command',
339
+ };
340
+ log.debug(`[${deviceId}] Sending RVC resume request:`, JSON.stringify(body));
341
+ const result = await client.setDeviceState(deviceId, body);
342
+ log.debug(`[${deviceId}] RVC resume API response:`, JSON.stringify(result));
343
+ log.info(`[${deviceId}] RVC resumed successfully`);
344
+ return { success: true, result };
345
+ }
346
+ catch (e) {
347
+ log.error(`[${deviceId}] Failed to resume RVC:`, e);
348
+ return { success: false, error: e };
349
+ }
350
+ },
351
+ goHome: async () => {
352
+ log.info(`[${deviceId}] RVC goHome command received`);
353
+ if (!client) {
354
+ log.warn(`[${deviceId}] No SwitchBot client available`);
355
+ return { success: false };
356
+ }
357
+ try {
358
+ const body = {
359
+ command: 'dock',
360
+ parameter: 'default',
361
+ commandType: 'command',
362
+ };
363
+ log.debug(`[${deviceId}] Sending RVC goHome request:`, JSON.stringify(body));
364
+ const result = await client.setDeviceState(deviceId, body);
365
+ log.debug(`[${deviceId}] RVC goHome API response:`, JSON.stringify(result));
366
+ log.info(`[${deviceId}] RVC sent to dock successfully`);
367
+ return { success: true, result };
368
+ }
369
+ catch (e) {
370
+ log.error(`[${deviceId}] Failed to send goHome command:`, e);
371
+ return { success: false, error: e };
372
+ }
373
+ },
374
+ },
375
+ };
376
+ case 'bot':
377
+ return {
378
+ onOff: {
379
+ on: async () => {
380
+ log.info(`[${deviceId}] Bot ON command received`);
381
+ if (!client) {
382
+ log.warn(`[${deviceId}] No SwitchBot client available`);
383
+ return { success: false };
384
+ }
385
+ try {
386
+ const result = await client.setDeviceState(deviceId, {
387
+ command: 'turnOn',
388
+ parameter: 'default',
389
+ commandType: 'command',
390
+ });
391
+ log.info(`[${deviceId}] Bot turned on successfully`);
392
+ return { success: true, result };
393
+ }
394
+ catch (e) {
395
+ log.error(`[${deviceId}] Failed to turn on Bot:`, e);
396
+ return { success: false, error: e };
397
+ }
398
+ },
399
+ off: async () => {
400
+ log.info(`[${deviceId}] Bot OFF command received`);
401
+ if (!client) {
402
+ log.warn(`[${deviceId}] No SwitchBot client available`);
403
+ return { success: false };
404
+ }
405
+ try {
406
+ const result = await client.setDeviceState(deviceId, {
407
+ command: 'turnOff',
408
+ parameter: 'default',
409
+ commandType: 'command',
410
+ });
411
+ log.info(`[${deviceId}] Bot turned off successfully`);
412
+ return { success: true, result };
413
+ }
414
+ catch (e) {
415
+ log.error(`[${deviceId}] Failed to turn off Bot:`, e);
416
+ return { success: false, error: e };
417
+ }
418
+ },
419
+ },
420
+ };
421
+ case 'curtain':
422
+ case 'blindtilt':
423
+ return {
424
+ windowCovering: {
425
+ goToLiftPercentage: async (request) => {
426
+ const percentage = request?.liftPercent100thsValue;
427
+ log.info(`[${deviceId}] Curtain position change requested: ${percentage}`);
428
+ if (!client) {
429
+ log.warn(`[${deviceId}] No SwitchBot client available`);
430
+ return { success: false };
431
+ }
432
+ try {
433
+ // Convert Matter percentage (0-10000) to SwitchBot (0-100)
434
+ const position = Math.max(0, Math.min(100, Math.round((percentage || 0) / 100)));
435
+ const result = await client.setDeviceState(deviceId, {
436
+ command: 'setPosition',
437
+ parameter: String(position),
438
+ commandType: 'command',
439
+ });
440
+ log.info(`[${deviceId}] Curtain position set to ${position}% successfully`);
441
+ return { success: true, result };
442
+ }
443
+ catch (e) {
444
+ log.error(`[${deviceId}] Failed to set curtain position:`, e);
445
+ return { success: false, error: e };
446
+ }
447
+ },
448
+ upOrOpen: async () => {
449
+ log.info(`[${deviceId}] Curtain open command received`);
450
+ if (!client) {
451
+ log.warn(`[${deviceId}] No SwitchBot client available`);
452
+ return { success: false };
453
+ }
454
+ try {
455
+ const result = await client.setDeviceState(deviceId, {
456
+ command: 'open',
457
+ parameter: 'default',
458
+ commandType: 'command',
459
+ });
460
+ log.info(`[${deviceId}] Curtain opened successfully`);
461
+ return { success: true, result };
462
+ }
463
+ catch (e) {
464
+ log.error(`[${deviceId}] Failed to open curtain:`, e);
465
+ return { success: false, error: e };
466
+ }
467
+ },
468
+ downOrClose: async () => {
469
+ log.info(`[${deviceId}] Curtain close command received`);
470
+ if (!client) {
471
+ log.warn(`[${deviceId}] No SwitchBot client available`);
472
+ return { success: false };
473
+ }
474
+ try {
475
+ const result = await client.setDeviceState(deviceId, {
476
+ command: 'close',
477
+ parameter: 'default',
478
+ commandType: 'command',
479
+ });
480
+ log.info(`[${deviceId}] Curtain closed successfully`);
481
+ return { success: true, result };
482
+ }
483
+ catch (e) {
484
+ log.error(`[${deviceId}] Failed to close curtain:`, e);
485
+ return { success: false, error: e };
486
+ }
487
+ },
488
+ stopMotion: async () => {
489
+ log.info(`[${deviceId}] Curtain stop command received`);
490
+ if (!client) {
491
+ log.warn(`[${deviceId}] No SwitchBot client available`);
492
+ return { success: false };
493
+ }
494
+ try {
495
+ const result = await client.setDeviceState(deviceId, {
496
+ command: 'pause',
497
+ parameter: 'default',
498
+ commandType: 'command',
499
+ });
500
+ log.info(`[${deviceId}] Curtain motion stopped successfully`);
501
+ return { success: true, result };
502
+ }
503
+ catch (e) {
504
+ log.error(`[${deviceId}] Failed to stop curtain:`, e);
505
+ return { success: false, error: e };
506
+ }
507
+ },
508
+ },
509
+ };
510
+ case 'plug':
511
+ return {
512
+ onOff: {
513
+ on: async () => {
514
+ log.info(`[${deviceId}] Plug ON command received`);
515
+ if (!client) {
516
+ log.warn(`[${deviceId}] No SwitchBot client available`);
517
+ return { success: false };
518
+ }
519
+ try {
520
+ const result = await client.setDeviceState(deviceId, {
521
+ command: 'turnOn',
522
+ parameter: 'default',
523
+ commandType: 'command',
524
+ });
525
+ log.info(`[${deviceId}] Plug turned on successfully`);
526
+ return { success: true, result };
527
+ }
528
+ catch (e) {
529
+ log.error(`[${deviceId}] Failed to turn on plug:`, e);
530
+ return { success: false, error: e };
531
+ }
532
+ },
533
+ off: async () => {
534
+ log.info(`[${deviceId}] Plug OFF command received`);
535
+ if (!client) {
536
+ log.warn(`[${deviceId}] No SwitchBot client available`);
537
+ return { success: false };
538
+ }
539
+ try {
540
+ const result = await client.setDeviceState(deviceId, {
541
+ command: 'turnOff',
542
+ parameter: 'default',
543
+ commandType: 'command',
544
+ });
545
+ log.info(`[${deviceId}] Plug turned off successfully`);
546
+ return { success: true, result };
547
+ }
548
+ catch (e) {
549
+ log.error(`[${deviceId}] Failed to turn off plug:`, e);
550
+ return { success: false, error: e };
551
+ }
552
+ },
553
+ },
554
+ };
555
+ case 'lock':
556
+ return {
557
+ doorLock: {
558
+ setLockState: async (request) => {
559
+ const state = request?.lockState === 1 ? 'LOCKED' : 'UNLOCKED';
560
+ log.info(`[${deviceId}] Lock state change requested: ${state}`);
561
+ if (!client) {
562
+ log.warn(`[${deviceId}] No SwitchBot client available`);
563
+ return { success: false };
564
+ }
565
+ try {
566
+ const command = request?.lockState === 1 ? 'lock' : 'unlock';
567
+ const result = await client.setDeviceState(deviceId, {
568
+ command,
569
+ parameter: 'default',
570
+ commandType: 'command',
571
+ });
572
+ log.info(`[${deviceId}] Lock ${state} successfully`);
573
+ return { success: true, result };
574
+ }
575
+ catch (e) {
576
+ log.error(`[${deviceId}] Failed to change lock state:`, e);
577
+ return { success: false, error: e };
578
+ }
579
+ },
580
+ },
581
+ };
582
+ case 'fan':
583
+ return {
584
+ onOff: {
585
+ on: async () => {
586
+ log.info(`[${deviceId}] Fan ON command received`);
587
+ if (!client) {
588
+ log.warn(`[${deviceId}] No SwitchBot client available`);
589
+ return { success: false };
590
+ }
591
+ try {
592
+ const result = await client.setDeviceState(deviceId, {
593
+ command: 'turnOn',
594
+ parameter: 'default',
595
+ commandType: 'command',
596
+ });
597
+ log.info(`[${deviceId}] Fan turned on successfully`);
598
+ return { success: true, result };
599
+ }
600
+ catch (e) {
601
+ log.error(`[${deviceId}] Failed to turn on fan:`, e);
602
+ return { success: false, error: e };
603
+ }
604
+ },
605
+ off: async () => {
606
+ log.info(`[${deviceId}] Fan OFF command received`);
607
+ if (!client) {
608
+ log.warn(`[${deviceId}] No SwitchBot client available`);
609
+ return { success: false };
610
+ }
611
+ try {
612
+ const result = await client.setDeviceState(deviceId, {
613
+ command: 'turnOff',
614
+ parameter: 'default',
615
+ commandType: 'command',
616
+ });
617
+ log.info(`[${deviceId}] Fan turned off successfully`);
618
+ return { success: true, result };
619
+ }
620
+ catch (e) {
621
+ log.error(`[${deviceId}] Failed to turn off fan:`, e);
622
+ return { success: false, error: e };
623
+ }
624
+ },
625
+ },
626
+ fanControl: {
627
+ setFanSpeed: async (request) => {
628
+ const speed = request?.percentSetting || 0;
629
+ log.info(`[${deviceId}] Fan speed change requested: ${speed}%`);
630
+ if (!client) {
631
+ log.warn(`[${deviceId}] No SwitchBot client available`);
632
+ return { success: false };
633
+ }
634
+ try {
635
+ // Convert percentage to SwitchBot fan speed parameter
636
+ const speedParam = Math.max(1, Math.min(100, speed));
637
+ const result = await client.setDeviceState(deviceId, {
638
+ command: 'setFanSpeed',
639
+ parameter: String(speedParam),
640
+ commandType: 'command',
641
+ });
642
+ log.info(`[${deviceId}] Fan speed set to ${speedParam}% successfully`);
643
+ return { success: true, result };
644
+ }
645
+ catch (e) {
646
+ log.error(`[${deviceId}] Failed to set fan speed:`, e);
647
+ return { success: false, error: e };
648
+ }
649
+ },
650
+ },
651
+ };
652
+ case 'light':
653
+ return {
654
+ onOff: {
655
+ on: async () => {
656
+ log.info(`[${deviceId}] Light ON command received`);
657
+ if (!client) {
658
+ log.warn(`[${deviceId}] No SwitchBot client available`);
659
+ return { success: false };
660
+ }
661
+ try {
662
+ const result = await client.setDeviceState(deviceId, {
663
+ command: 'turnOn',
664
+ parameter: 'default',
665
+ commandType: 'command',
666
+ });
667
+ log.info(`[${deviceId}] Light turned on successfully`);
668
+ return { success: true, result };
669
+ }
670
+ catch (e) {
671
+ log.error(`[${deviceId}] Failed to turn on light:`, e);
672
+ return { success: false, error: e };
673
+ }
674
+ },
675
+ off: async () => {
676
+ log.info(`[${deviceId}] Light OFF command received`);
677
+ if (!client) {
678
+ log.warn(`[${deviceId}] No SwitchBot client available`);
679
+ return { success: false };
680
+ }
681
+ try {
682
+ const result = await client.setDeviceState(deviceId, {
683
+ command: 'turnOff',
684
+ parameter: 'default',
685
+ commandType: 'command',
686
+ });
687
+ log.info(`[${deviceId}] Light turned off successfully`);
688
+ return { success: true, result };
689
+ }
690
+ catch (e) {
691
+ log.error(`[${deviceId}] Failed to turn off light:`, e);
692
+ return { success: false, error: e };
693
+ }
694
+ },
695
+ },
696
+ levelControl: {
697
+ moveToLevel: async (request) => {
698
+ const level = request?.level || 0;
699
+ // Convert from 0-254 to 0-100
700
+ const brightness = Math.round((level / 254) * 100);
701
+ log.info(`[${deviceId}] Light brightness change requested: ${brightness}%`);
702
+ if (!client) {
703
+ log.warn(`[${deviceId}] No SwitchBot client available`);
704
+ return { success: false };
705
+ }
706
+ try {
707
+ const param = Math.max(0, Math.min(100, brightness));
708
+ const result = await client.setDeviceState(deviceId, {
709
+ command: 'setBrightness',
710
+ parameter: String(param),
711
+ commandType: 'command',
712
+ });
713
+ log.info(`[${deviceId}] Light brightness set to ${param}% successfully`);
714
+ return { success: true, result };
715
+ }
716
+ catch (e) {
717
+ log.error(`[${deviceId}] Failed to set light brightness:`, e);
718
+ return { success: false, error: e };
719
+ }
720
+ },
721
+ },
722
+ };
723
+ case 'lightstrip':
724
+ return {
725
+ onOff: {
726
+ on: async () => {
727
+ log.info(`[${deviceId}] Lightstrip ON command received`);
728
+ if (!client) {
729
+ log.warn(`[${deviceId}] No SwitchBot client available`);
730
+ return { success: false };
731
+ }
732
+ try {
733
+ const result = await client.setDeviceState(deviceId, {
734
+ command: 'turnOn',
735
+ parameter: 'default',
736
+ commandType: 'command',
737
+ });
738
+ log.info(`[${deviceId}] Lightstrip turned on successfully`);
739
+ return { success: true, result };
740
+ }
741
+ catch (e) {
742
+ log.error(`[${deviceId}] Failed to turn on lightstrip:`, e);
743
+ return { success: false, error: e };
744
+ }
745
+ },
746
+ off: async () => {
747
+ log.info(`[${deviceId}] Lightstrip OFF command received`);
748
+ if (!client) {
749
+ log.warn(`[${deviceId}] No SwitchBot client available`);
750
+ return { success: false };
751
+ }
752
+ try {
753
+ const result = await client.setDeviceState(deviceId, {
754
+ command: 'turnOff',
755
+ parameter: 'default',
756
+ commandType: 'command',
757
+ });
758
+ log.info(`[${deviceId}] Lightstrip turned off successfully`);
759
+ return { success: true, result };
760
+ }
761
+ catch (e) {
762
+ log.error(`[${deviceId}] Failed to turn off lightstrip:`, e);
763
+ return { success: false, error: e };
764
+ }
765
+ },
766
+ },
767
+ levelControl: {
768
+ moveToLevel: async (request) => {
769
+ const level = request?.level || 0;
770
+ // Convert from 0-254 to 0-100
771
+ const brightness = Math.round((level / 254) * 100);
772
+ log.info(`[${deviceId}] Lightstrip brightness change requested: ${brightness}%`);
773
+ if (!client) {
774
+ log.warn(`[${deviceId}] No SwitchBot client available`);
775
+ return { success: false };
776
+ }
777
+ try {
778
+ const param = Math.max(0, Math.min(100, brightness));
779
+ const result = await client.setDeviceState(deviceId, {
780
+ command: 'setBrightness',
781
+ parameter: String(param),
782
+ commandType: 'command',
783
+ });
784
+ log.info(`[${deviceId}] Lightstrip brightness set to ${param}% successfully`);
785
+ return { success: true, result };
786
+ }
787
+ catch (e) {
788
+ log.error(`[${deviceId}] Failed to set lightstrip brightness:`, e);
789
+ return { success: false, error: e };
790
+ }
791
+ },
792
+ },
793
+ colorControl: {
794
+ moveToHueAndSaturation: async (request) => {
795
+ const hue = request?.hue || 0;
796
+ const saturation = request?.saturation || 0;
797
+ log.info(`[${deviceId}] Lightstrip color change requested: hue=${hue}, sat=${saturation}`);
798
+ if (!client) {
799
+ log.warn(`[${deviceId}] No SwitchBot client available`);
800
+ return { success: false };
801
+ }
802
+ try {
803
+ // Convert hue (0-254) and saturation (0-254) to combined color parameter
804
+ // SwitchBot typically expects RGB or HSV format as parameter
805
+ const colorParam = `${Math.round(hue)},${Math.round(saturation)}`;
806
+ const result = await client.setDeviceState(deviceId, {
807
+ command: 'setColor',
808
+ parameter: colorParam,
809
+ commandType: 'command',
810
+ });
811
+ log.info(`[${deviceId}] Lightstrip color set successfully`);
812
+ return { success: true, result };
813
+ }
814
+ catch (e) {
815
+ log.error(`[${deviceId}] Failed to set lightstrip color:`, e);
816
+ return { success: false, error: e };
817
+ }
818
+ },
819
+ moveToColorTemperature: async (request) => {
820
+ const mireds = request?.colorTemperatureMireds || 400;
821
+ // Convert mireds (158-500 typical range) to Kelvin: K = 1000000 / mireds
822
+ const kelvin = Math.round(1000000 / mireds);
823
+ log.info(`[${deviceId}] Lightstrip color temperature change requested: ${mireds} mireds (${kelvin}K)`);
824
+ if (!client) {
825
+ log.warn(`[${deviceId}] No SwitchBot client available`);
826
+ return { success: false };
827
+ }
828
+ try {
829
+ // Map Kelvin to SwitchBot color temperature parameter (typically 0-100 or specific values)
830
+ // Normalize to 0-100 scale where 0=warm (2700K) and 100=cool (6500K)
831
+ const colorTempParam = Math.max(0, Math.min(100, Math.round(((kelvin - 2700) / 3800) * 100)));
832
+ const result = await client.setDeviceState(deviceId, {
833
+ command: 'setColorTemperature',
834
+ parameter: String(colorTempParam),
835
+ commandType: 'command',
836
+ });
837
+ log.info(`[${deviceId}] Lightstrip color temperature set to ${kelvin}K successfully`);
838
+ return { success: true, result };
839
+ }
840
+ catch (e) {
841
+ log.error(`[${deviceId}] Failed to set lightstrip color temperature:`, e);
842
+ return { success: false, error: e };
843
+ }
844
+ },
845
+ },
846
+ };
847
+ case 'humidifier':
848
+ return {
849
+ onOff: {
850
+ on: async () => {
851
+ log.info(`[${deviceId}] Humidifier ON command received`);
852
+ if (!client) {
853
+ log.warn(`[${deviceId}] No SwitchBot client available`);
854
+ return { success: false };
855
+ }
856
+ try {
857
+ const result = await client.setDeviceState(deviceId, {
858
+ command: 'turnOn',
859
+ parameter: 'default',
860
+ commandType: 'command',
861
+ });
862
+ log.info(`[${deviceId}] Humidifier turned on successfully`);
863
+ return { success: true, result };
864
+ }
865
+ catch (e) {
866
+ log.error(`[${deviceId}] Failed to turn on humidifier:`, e);
867
+ return { success: false, error: e };
868
+ }
869
+ },
870
+ off: async () => {
871
+ log.info(`[${deviceId}] Humidifier OFF command received`);
872
+ if (!client) {
873
+ log.warn(`[${deviceId}] No SwitchBot client available`);
874
+ return { success: false };
875
+ }
876
+ try {
877
+ const result = await client.setDeviceState(deviceId, {
878
+ command: 'turnOff',
879
+ parameter: 'default',
880
+ commandType: 'command',
881
+ });
882
+ log.info(`[${deviceId}] Humidifier turned off successfully`);
883
+ return { success: true, result };
884
+ }
885
+ catch (e) {
886
+ log.error(`[${deviceId}] Failed to turn off humidifier:`, e);
887
+ return { success: false, error: e };
888
+ }
889
+ },
890
+ },
891
+ fanControl: {
892
+ setFanSpeed: async (request) => {
893
+ const speed = request?.percentSetting || 0;
894
+ log.info(`[${deviceId}] Humidifier speed change requested: ${speed}%`);
895
+ if (!client) {
896
+ log.warn(`[${deviceId}] No SwitchBot client available`);
897
+ return { success: false };
898
+ }
899
+ try {
900
+ // Convert percentage to SwitchBot humidifier speed parameter
901
+ const speedParam = Math.max(1, Math.min(100, speed));
902
+ const result = await client.setDeviceState(deviceId, {
903
+ command: 'setFanSpeed',
904
+ parameter: String(speedParam),
905
+ commandType: 'command',
906
+ });
907
+ log.info(`[${deviceId}] Humidifier speed set to ${speedParam}% successfully`);
908
+ return { success: true, result };
909
+ }
910
+ catch (e) {
911
+ log.error(`[${deviceId}] Failed to set humidifier speed:`, e);
912
+ return { success: false, error: e };
913
+ }
914
+ },
915
+ },
916
+ };
917
+ case 'relay':
918
+ return {
919
+ onOff: {
920
+ on: async () => {
921
+ log.info(`[${deviceId}] Relay ON command received`);
922
+ if (!client) {
923
+ log.warn(`[${deviceId}] No SwitchBot client available`);
924
+ return { success: false };
925
+ }
926
+ try {
927
+ const result = await client.setDeviceState(deviceId, {
928
+ command: 'turnOn',
929
+ parameter: 'default',
930
+ commandType: 'command',
931
+ });
932
+ log.info(`[${deviceId}] Relay turned on successfully`);
933
+ return { success: true, result };
934
+ }
935
+ catch (e) {
936
+ log.error(`[${deviceId}] Failed to turn on relay:`, e);
937
+ return { success: false, error: e };
938
+ }
939
+ },
940
+ off: async () => {
941
+ log.info(`[${deviceId}] Relay OFF command received`);
942
+ if (!client) {
943
+ log.warn(`[${deviceId}] No SwitchBot client available`);
944
+ return { success: false };
945
+ }
946
+ try {
947
+ const result = await client.setDeviceState(deviceId, {
948
+ command: 'turnOff',
949
+ parameter: 'default',
950
+ commandType: 'command',
951
+ });
952
+ log.info(`[${deviceId}] Relay turned off successfully`);
953
+ return { success: true, result };
954
+ }
955
+ catch (e) {
956
+ log.error(`[${deviceId}] Failed to turn off relay:`, e);
957
+ return { success: false, error: e };
958
+ }
959
+ },
960
+ },
961
+ };
962
+ default:
963
+ return undefined;
964
+ }
965
+ }
966
+ /**
967
+ * Resolves the Matter device type for a given device, using the Matter API and device type mappings.
968
+ * Used to map normalized device types to Matter device type definitions.
969
+ *
970
+ * @param matterApi - The Matter API object containing deviceTypes
971
+ * @param type - The normalized device type string
972
+ * @param createdDeviceType - Optionally, an already created device type object
973
+ * @param clusters - Optionally, the clusters object for the device
974
+ * @returns The resolved Matter device type object
975
+ */
976
+ const DEVICE_MATTER_DEVICE_TYPE_KEYS = {
977
+ bot: 'OnOffSwitch',
978
+ vacuum: 'RoboticVacuumCleaner',
979
+ curtain: 'WindowCovering',
980
+ blindtilt: 'WindowCovering',
981
+ fan: 'Fan',
982
+ light: 'DimmableLight',
983
+ lightstrip: 'ExtendedColorLight',
984
+ lock: 'DoorLock',
985
+ motion: 'MotionSensor',
986
+ contact: 'ContactSensor',
987
+ humidifier: 'Fan',
988
+ temperature: 'TemperatureSensor',
989
+ relay: 'OnOffSwitch',
990
+ plug: 'OnOffOutlet',
991
+ meter: 'TemperatureSensor',
992
+ waterdetector: 'LeakSensor',
993
+ };
994
+ export function resolveMatterDeviceType(matterApi, type, createdDeviceType, clusters) {
995
+ if (createdDeviceType && typeof createdDeviceType === 'object' && typeof createdDeviceType.with === 'function') {
996
+ return createdDeviceType;
997
+ }
998
+ const lowerType = (typeof createdDeviceType === 'string' && createdDeviceType) ? createdDeviceType.toLowerCase() : (type || '').toLowerCase();
999
+ // Cluster-based upgrade for color lights if descriptor omitted device type.
1000
+ const hasColorControl = !!clusters?.colorControl;
1001
+ const inferredType = hasColorControl && lowerType === 'light'
1002
+ ? 'lightstrip'
1003
+ : lowerType;
1004
+ const mappedKey = DEVICE_MATTER_DEVICE_TYPE_KEYS[inferredType] || 'OnOffSwitch';
1005
+ return matterApi?.deviceTypes?.[mappedKey] || matterApi?.deviceTypes?.OnOffSwitch;
1006
+ }
1007
+ /**
1008
+ * Canonical Matter cluster ID mapping (from matter.js clusters).
1009
+ * Maps cluster names to their numeric cluster IDs.
1010
+ *
1011
+ * @example
1012
+ * MATTER_CLUSTER_IDS.OnOff // 0x0006
1013
+ */
2
1014
  export const MATTER_CLUSTER_IDS = {
3
1015
  OnOff: 0x0006,
4
1016
  LevelControl: 0x0008,
@@ -8,7 +1020,13 @@ export const MATTER_CLUSTER_IDS = {
8
1020
  FanControl: 0x0202,
9
1021
  RelativeHumidityMeasurement: 0x0405,
10
1022
  };
11
- // Common Matter attribute IDs grouped by cluster
1023
+ /**
1024
+ * Common Matter attribute IDs grouped by cluster.
1025
+ * Maps cluster names to objects mapping attribute names to their numeric attribute IDs.
1026
+ *
1027
+ * @example
1028
+ * MATTER_ATTRIBUTE_IDS.OnOff.OnOff // 0x0000
1029
+ */
12
1030
  export const MATTER_ATTRIBUTE_IDS = {
13
1031
  OnOff: { OnOff: 0x0000 },
14
1032
  LevelControl: { CurrentLevel: 0x0000 },
@@ -18,20 +1036,119 @@ export const MATTER_ATTRIBUTE_IDS = {
18
1036
  DoorLock: { LockState: 0x0000 },
19
1037
  RelativeHumidityMeasurement: { MeasuredValue: 0x0000 },
20
1038
  };
1039
+ /**
1040
+ * Normalizes a device type string for Matter integration.
1041
+ * Maps various device type aliases and variants to canonical Matter device types.
1042
+ *
1043
+ * @param {string | undefined | null} typeValue - The device type string to normalize.
1044
+ * @returns {string} The normalized device type string for Matter.
1045
+ *
1046
+ * @example
1047
+ * normalizeTypeForMatter('wosweeper') // 'vacuum'
1048
+ * normalizeTypeForMatter('curtain3') // 'curtain'
1049
+ * normalizeTypeForMatter('plug mini (us)') // 'plug'
1050
+ */
1051
+ export function normalizeTypeForMatter(typeValue) {
1052
+ const raw = String(typeValue || '').trim().toLowerCase();
1053
+ if (!raw) {
1054
+ return 'unknown';
1055
+ }
1056
+ // Vacuum variants
1057
+ if (['wosweeper', 'wosweepermini', 'wosweeperminipro', 'k10+', 'k10+ pro'].includes(raw)) {
1058
+ return 'vacuum';
1059
+ }
1060
+ // Window covering variants
1061
+ if (['curtain', 'curtain3', 'rollershade', 'roller shade', 'worollershade', 'wo rollershade'].includes(raw)) {
1062
+ return 'curtain';
1063
+ }
1064
+ // Blind tilt variants (normalized to 'blindtilt' for Matter since it uses tilt-capable cluster)
1065
+ if (['blindtilt', 'blind tilt'].includes(raw)) {
1066
+ return 'blindtilt';
1067
+ }
1068
+ // Plug variants
1069
+ if (['plug mini (jp)', 'plug mini (us)', 'plug mini (eu)'].includes(raw)) {
1070
+ return 'plug';
1071
+ }
1072
+ // Meter variants
1073
+ if (['meterplus', 'meter plus', 'meter plus (jp)', 'meterpro', 'meter pro', 'meterpro(co2)', 'meter pro (co2)'].includes(raw)) {
1074
+ return 'meter';
1075
+ }
1076
+ // Relay switch variants
1077
+ if (['relay switch 1', 'relay switch 1pm'].includes(raw)) {
1078
+ return 'relay';
1079
+ }
1080
+ // Water detector variants
1081
+ if (['water detector', 'waterdetector'].includes(raw)) {
1082
+ return 'waterdetector';
1083
+ }
1084
+ // Fan variants
1085
+ if (['smart fan', 'circulator fan', 'battery circulator fan', 'standing circulator fan'].includes(raw)) {
1086
+ return 'fan';
1087
+ }
1088
+ // Light variants
1089
+ if (['strip light', 'strip light 3', 'rgbic neon rope light', 'rgbic neon wire rope light', 'rgbicww floor lamp', 'rgbicww strip light'].includes(raw)) {
1090
+ return 'lightstrip';
1091
+ }
1092
+ if (['color bulb', 'ceiling light', 'ceiling light pro', 'candle warmer lamp', 'floor lamp'].includes(raw)) {
1093
+ return 'light';
1094
+ }
1095
+ // Sensor variants
1096
+ if (raw === 'motion sensor') {
1097
+ return 'motion';
1098
+ }
1099
+ if (['contact sensor', 'presence sensor'].includes(raw)) {
1100
+ return 'contact';
1101
+ }
1102
+ // Lock variants
1103
+ if (['smart lock', 'smart lock pro', 'smart lock ultra', 'lock lite', 'keypad', 'keypad touch', 'keypad vision', 'keypad vision pro', 'lock vision pro'].includes(raw)) {
1104
+ return 'lock';
1105
+ }
1106
+ // Climate variant
1107
+ if (raw === 'humidifier2') {
1108
+ return 'humidifier';
1109
+ }
1110
+ return raw;
1111
+ }
1112
+ /**
1113
+ * Normalizes a Homebridge PlatformConfig object to a SwitchBotPluginConfig.
1114
+ *
1115
+ * @param raw The raw Homebridge platform config object.
1116
+ * @returns The normalized plugin config object.
1117
+ */
21
1118
  export function normalizeConfig(raw) {
22
- if (!raw)
1119
+ if (!raw) {
23
1120
  return {};
1121
+ }
24
1122
  return { ...raw };
25
1123
  }
26
1124
  // Create a Proxy constructor that instantiates the right platform implementation at runtime.
1125
+ /**
1126
+ * Creates a proxy class that instantiates the correct platform implementation (HAP or Matter) at runtime.
1127
+ *
1128
+ * @param HAPPlatform The HAP platform class constructor.
1129
+ * @param MatterPlatform The Matter platform class constructor.
1130
+ * @returns A proxy class that delegates to the correct platform implementation.
1131
+ *
1132
+ * @class SwitchBotPlatformProxy
1133
+ * @property impl The instantiated platform implementation (HAP or Matter).
1134
+ */
27
1135
  export function createPlatformProxy(HAPPlatform, MatterPlatform) {
28
1136
  return class SwitchBotPlatformProxy {
1137
+ /** The instantiated platform implementation (HAP or Matter) */
29
1138
  impl;
1139
+ /**
1140
+ * Constructs the proxy and instantiates the correct platform implementation.
1141
+ * @param log Logger instance
1142
+ * @param config Platform config
1143
+ * @param api Homebridge API instance
1144
+ * @returns The instantiated platform implementation
1145
+ */
30
1146
  constructor(log, config, api) {
31
1147
  const cfg = normalizeConfig(config);
32
1148
  const preferMatter = cfg.preferMatter ?? true;
33
1149
  const enableMatter = cfg.enableMatter ?? true;
34
- if (enableMatter && preferMatter && MatterPlatform) {
1150
+ const matterAvailable = !!(api?.isMatterAvailable?.() && api?.isMatterEnabled?.());
1151
+ if (enableMatter && preferMatter && MatterPlatform && matterAvailable) {
35
1152
  this.impl = new MatterPlatform(log, cfg, api);
36
1153
  return this.impl;
37
1154
  }