@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
@@ -1,73 +0,0 @@
1
- import { describe, it, expect } from 'vitest'
2
- import { SwitchBotHAPPlatform, SwitchBotMatterPlatform } from '../src/platform'
3
-
4
- const log = { info: () => {}, warn: () => {}, error: () => {}, debug: () => {} }
5
-
6
- describe('accessory restore', () => {
7
- it('reuses restored HAP accessory by context.deviceId and avoids re-register', async () => {
8
- const registered: any[] = []
9
- const hap = { uuid: { generate: (s: string) => `uuid-${s}` }, Service: {}, Characteristic: {} }
10
-
11
- function PlatformAccessory(name: string, uuid: string) {
12
- this.displayName = name
13
- this.UUID = uuid
14
- this.context = {}
15
- this.getService = () => undefined
16
- this.addService = () => undefined
17
- }
18
-
19
- const api: any = {
20
- hap,
21
- platformAccessory: PlatformAccessory as any,
22
- registerPlatformAccessories: (_p: string, _n: string, accs: any[]) => registered.push(...accs),
23
- on: (_ev: string, cb: Function) => {},
24
- }
25
-
26
- const platform = new SwitchBotHAPPlatform(log as any, { devices: [{ id: 'dev1', type: 'light', name: 'NewName' }] }, api)
27
-
28
- // Simulate restored accessory with different uuid but correct deviceId in context
29
- const restored = new (api.platformAccessory)('OldName', 'old-uuid')
30
- restored.context = { deviceId: 'dev1', type: 'light' }
31
- platform.configureAccessory(restored)
32
-
33
- // Load devices should reuse restored accessory by context.deviceId
34
- await (platform as any).loadDevices()
35
-
36
- // No new registration for this device because restored accessory was reused
37
- expect(registered.length).toBe(0)
38
- // The restored accessory should remain in the platform's accessory map
39
- const keys = Array.from((platform as any).accessories.keys())
40
- expect(keys).toContain('old-uuid')
41
- const acc = (platform as any).accessories.get('old-uuid')
42
- expect(acc.context.deviceId).toBe('dev1')
43
- })
44
-
45
- it('reuses restored Matter accessory by context.deviceId when registering', async () => {
46
- const captured: any[] = []
47
- const matter = { uuid: { generate: (s: string) => `uuid-${s}` }, clusterNames: {} }
48
- const api: any = {
49
- matter,
50
- isMatterAvailable: () => true,
51
- isMatterEnabled: () => true,
52
- on: (_ev: string, cb: Function) => {},
53
- }
54
-
55
- const platform = new SwitchBotMatterPlatform(log as any, { devices: [{ id: 'm1', type: 'light', name: 'M1' }] }, api)
56
-
57
- // Simulate restored Matter accessory with a different uuid but matching deviceId
58
- const restored = { uuid: 'restored-uuid', displayName: 'OldM', context: { deviceId: 'm1', type: 'light' } }
59
- ;(platform as any).accessories.set(restored.uuid, restored)
60
-
61
- // stub matter.registerPlatformAccessories to capture what is registered
62
- api.matter.registerPlatformAccessories = async (_p: string, _n: string, accs: any[]) => captured.push(...accs)
63
-
64
- // call registerMatterAccessories
65
- await (platform as any).registerMatterAccessories()
66
-
67
- // The restored accessory should still be present and associated with deviceId
68
- const keys = Array.from((platform as any).accessories.keys())
69
- expect(keys).toContain(restored.uuid)
70
- const acc = (platform as any).accessories.get(restored.uuid)
71
- expect(acc.context.deviceId).toBe('m1')
72
- })
73
- })
@@ -1,37 +0,0 @@
1
- import { describe, it, expect } from 'vitest'
2
- import { createDevice } from '../src/deviceFactory'
3
-
4
- const DEVICE_TYPES = [
5
- 'bot',
6
- 'curtain',
7
- 'fan',
8
- 'light',
9
- 'lightstrip',
10
- 'motion',
11
- 'contact',
12
- 'vacuum',
13
- 'lock',
14
- 'humidifier',
15
- 'temperature',
16
- ]
17
-
18
- describe('device mapping', () => {
19
- it('maps known device types to device descriptors (HAP)', async () => {
20
- for (const t of DEVICE_TYPES) {
21
- const created = await createDevice({ id: `dev-${t}`, type: t, name: `Name-${t}` }, {}, false)
22
- expect(created).toHaveProperty('instance')
23
- expect(created).toHaveProperty('createAccessory')
24
- const acc = created.createAccessory?.(undefined)
25
- expect(acc).toBeDefined()
26
- // descriptor should include services for HAP devices
27
- if (created.protocol === 'hap') {
28
- expect(acc).toHaveProperty('services')
29
- }
30
- }
31
- })
32
-
33
- it('returns matter protocol when requested', async () => {
34
- const created = await createDevice({ id: 'm1', type: 'light', name: 'M1' }, { preferMatter: true, enableMatter: true }, true)
35
- expect(created.protocol).toBe('matter')
36
- })
37
- })
@@ -1,18 +0,0 @@
1
- import { describe, it, expect } from 'vitest'
2
- import { createDevice } from '../src/deviceFactory'
3
-
4
- describe('deviceFactory', () => {
5
- it('creates HAP device descriptor for light', async () => {
6
- const created = await createDevice({ id: 'dev1', type: 'light', name: 'L1' }, {}, false)
7
- expect(created).toHaveProperty('instance')
8
- expect(created).toHaveProperty('createAccessory')
9
- expect(created.protocol).toBe('hap')
10
- const acc = created.createAccessory?.(undefined)
11
- expect(acc).toHaveProperty('services')
12
- })
13
-
14
- it('creates Matter descriptor when useMatter=true', async () => {
15
- const created = await createDevice({ id: 'dev2', type: 'light', name: 'L2' }, { preferMatter: true, enableMatter: true }, true)
16
- expect(created.protocol).toBe('matter')
17
- })
18
- })
@@ -1,29 +0,0 @@
1
- import { describe, it, expect, vi } from 'vitest'
2
- import { FanDevice } from '../src/devices/genericDevice'
3
-
4
- describe('Fan swing commands', () => {
5
- it('sends setSwing on boolean swing', async () => {
6
- const mockClient = { setDeviceState: vi.fn(async (id: string, body: any) => ({ id, body })) }
7
- const d = new FanDevice({ id: 'fan1', type: 'fan', name: 'F' }, { _client: mockClient } as any)
8
- await d.init()
9
- const res = await d.setState({ swing: true })
10
- expect(mockClient.setDeviceState).toHaveBeenCalled()
11
- expect((mockClient.setDeviceState as any).mock.calls[0][1]).toEqual({ command: 'setSwing', parameter: 'on', commandType: 'command' })
12
- })
13
-
14
- it('sends setSwing with angle', async () => {
15
- const mockClient = { setDeviceState: vi.fn(async (id: string, body: any) => ({ id, body })) }
16
- const d = new FanDevice({ id: 'fan2', type: 'fan', name: 'F2' }, { _client: mockClient } as any)
17
- await d.init()
18
- await d.setState({ swingAngle: 45 })
19
- expect((mockClient.setDeviceState as any).mock.calls[0][1]).toEqual({ command: 'setSwing', parameter: '45', commandType: 'command' })
20
- })
21
-
22
- it('sends setSwing with mode', async () => {
23
- const mockClient = { setDeviceState: vi.fn(async (id: string, body: any) => ({ id, body })) }
24
- const d = new FanDevice({ id: 'fan3', type: 'fan', name: 'F3' }, { _client: mockClient } as any)
25
- await d.init()
26
- await d.setState({ swingMode: 'vertical' })
27
- expect((mockClient.setDeviceState as any).mock.calls[0][1]).toEqual({ command: 'setSwing', parameter: 'vertical', commandType: 'command' })
28
- })
29
- })
@@ -1,44 +0,0 @@
1
- import { describe, it, expect, vi } from 'vitest'
2
- import { LockDevice } from '../src/devices/genericDevice'
3
-
4
- describe('Lock user management', () => {
5
- it('sets lock pin via pin field', async () => {
6
- const mockClient = { setDeviceState: vi.fn(async (id: string, body: any) => ({ id, body })) }
7
- const d = new LockDevice({ id: 'lock1', type: 'lock', name: 'L1' }, { _client: mockClient } as any)
8
- await d.init()
9
- await d.setState({ pin: '1234' })
10
- expect((mockClient.setDeviceState as any).mock.calls[0][1]).toEqual({ command: 'setLockPin', parameter: '1234', commandType: 'command' })
11
- })
12
-
13
- it('adds a user code with addUser action', async () => {
14
- const mockClient = { setDeviceState: vi.fn(async (id: string, body: any) => ({ id, body })) }
15
- const d = new LockDevice({ id: 'lock2', type: 'lock', name: 'L2' }, { _client: mockClient } as any)
16
- await d.init()
17
- await d.setState({ action: 'addUser', user: 'user1', pin: '9999' })
18
- expect((mockClient.setDeviceState as any).mock.calls[0][1]).toEqual({ command: 'addUserCode', parameter: 'user1:9999', commandType: 'command' })
19
- })
20
-
21
- it('removes a user code with removeUser action', async () => {
22
- const mockClient = { setDeviceState: vi.fn(async (id: string, body: any) => ({ id, body })) }
23
- const d = new LockDevice({ id: 'lock3', type: 'lock', name: 'L3' }, { _client: mockClient } as any)
24
- await d.init()
25
- await d.setState({ action: 'removeUser', user: 'user1' })
26
- expect((mockClient.setDeviceState as any).mock.calls[0][1]).toEqual({ command: 'removeUserCode', parameter: 'user1', commandType: 'command' })
27
- })
28
-
29
- it('lists users with listUsers action', async () => {
30
- const mockClient = { setDeviceState: vi.fn(async (id: string, body: any) => ({ id, body })) }
31
- const d = new LockDevice({ id: 'lock4', type: 'lock', name: 'L4' }, { _client: mockClient } as any)
32
- await d.init()
33
- await d.setState({ action: 'listUsers' })
34
- expect((mockClient.setDeviceState as any).mock.calls[0][1]).toEqual({ command: 'listUsers', parameter: 'default', commandType: 'command' })
35
- })
36
-
37
- it('unlocks with pin using unlockWithPin action', async () => {
38
- const mockClient = { setDeviceState: vi.fn(async (id: string, body: any) => ({ id, body })) }
39
- const d = new LockDevice({ id: 'lock5', type: 'lock', name: 'L5' }, { _client: mockClient } as any)
40
- await d.init()
41
- await d.setState({ action: 'unlockWithPin', pin: '5555' })
42
- expect((mockClient.setDeviceState as any).mock.calls[0][1]).toEqual({ command: 'unlockWithPin', parameter: '5555', commandType: 'command' })
43
- })
44
- })
@@ -1,55 +0,0 @@
1
- import { describe, it, expect, beforeEach } from 'vitest'
2
- import SwitchBotPlatform from '../src/platform'
3
- import { createPlatformProxy } from '../src/utils'
4
-
5
- // This is a lightweight integration-style unit test that verifies the
6
- // platform will attempt Matter registration when MatterPlatform is
7
- // provided by the proxy and will still restore cached accessories.
8
-
9
- describe('platform integration (Matter)', () => {
10
- it('creates Matter descriptors when useMatter=true and restores accessories', async () => {
11
- // Create a fake MatterPlatform class that records register calls
12
- class FakeMatterPlatform {
13
- public registered: any[] = []
14
- constructor(public log: any, public cfg: any, public api: any) {}
15
- async registerMatterAccessories(accessories: any[]) {
16
- this.registered.push(...accessories)
17
- return
18
- }
19
- // simulate configureAccessory hook for restored matter accessories
20
- configureMatterAccessory(accessory: any) {
21
- // no-op
22
- }
23
- // lightweight loadDevices implementation that uses an injected discover hook if present
24
- async loadDevices() {
25
- const devices = (this.cfg?.devices) || (this as any)._discoverDevices ? await (this as any)._discoverDevices() : []
26
- // Simulate preparing descriptors and immediately registering them
27
- const serialized = devices.map((d: any) => ({ id: d.id ?? d.deviceId ?? d, protocol: 'matter', name: d.name }))
28
- if (serialized.length > 0) await this.registerMatterAccessories(serialized)
29
- }
30
- }
31
-
32
- // Create proxy that would choose the fake matter platform when preferred
33
- const Proxy = createPlatformProxy(function HAP() {}, FakeMatterPlatform)
34
- const platform = new (Proxy as any)(console, { preferMatter: true, enableMatter: true }, {})
35
-
36
- // Make a small device descriptor to load
37
- const device = { id: 'dev-m-1', type: 'light', name: 'MatterLight' }
38
- // Simulate platform method that loads devices (use same method name used in code)
39
- if (typeof platform.loadDevices === 'function') {
40
- // Add an internal method to return our single device for creation
41
- platform._discoverDevices = async () => [device]
42
- // Call loadDevices which should call the MatterPlatform.registerMatterAccessories
43
- await platform.loadDevices()
44
-
45
- // Verify FakeMatterPlatform received the accessory
46
- expect(platform).toBeInstanceOf(FakeMatterPlatform)
47
- expect((platform as any).registered.length).toBeGreaterThanOrEqual(1)
48
- const registered = (platform as any).registered[0]
49
- expect(registered).toHaveProperty('protocol', 'matter')
50
- expect(registered).toHaveProperty('id', device.id)
51
- } else {
52
- throw new Error('platform.loadDevices not implemented')
53
- }
54
- })
55
- })
@@ -1,97 +0,0 @@
1
- import { describe, it, expect, vi, beforeEach } from 'vitest'
2
- import { FanDevice, LightDevice, CurtainDevice, LockDevice } from '../src/devices/genericDevice'
3
-
4
- describe('Matter descriptors and translations (per-device)', () => {
5
- beforeEach(() => {
6
- vi.restoreAllMocks()
7
- })
8
-
9
- it('Fan: maps oscillation and swing to setDeviceState commands', async () => {
10
- const calls: any[] = []
11
- const fakeClient: any = { setDeviceState: async (id: string, body: any) => { calls.push({ id, body }); return { ok: true } } }
12
- const d = new FanDevice({ id: 'f1', type: 'fan' } as any, { _client: fakeClient } as any)
13
-
14
- // Oscillate
15
- await d.setState({ oscillate: true })
16
- expect(calls.pop()?.body.command).toBe('setOscillation')
17
-
18
- // Swing boolean
19
- await d.setState({ swing: true })
20
- expect(calls.pop()?.body.command).toBe('setSwing')
21
-
22
- // Swing angle
23
- await d.setState({ swingAngle: 45 })
24
- const last = calls.pop()
25
- expect(last.body.command).toBe('setSwing')
26
- expect(last.body.parameter).toBe('45')
27
- })
28
-
29
- it('Light: color/CT mapping and setState commands', async () => {
30
- const calls: any[] = []
31
- const fakeClient: any = { setDeviceState: async (id: string, body: any) => { calls.push(body); return { ok: true } } }
32
- const d = new LightDevice({ id: 'l1', type: 'light' } as any, { _client: fakeClient } as any)
33
-
34
- // color temperature (mired)
35
- await d.setState({ colorTemperature: 300 })
36
- expect(calls.pop()?.command).toBe('setColorTemperature')
37
-
38
- // hue + saturation
39
- await d.setState({ hue: 120, saturation: 50 })
40
- const c = calls.pop()
41
- expect(c.command).toBe('setColor')
42
- expect(c.parameter).toBe('120,50')
43
-
44
- // hex color string
45
- await d.setState({ color: '#ff0000' })
46
- expect(calls.pop()?.command).toBe('setColor')
47
- })
48
-
49
- it('Curtain: position mapping and Matter attribute writes', async () => {
50
- const calls: any[] = []
51
- const fakeClient: any = { setDeviceState: async (id: string, body: any) => { calls.push(body); return { ok: true } } }
52
- const d = new CurtainDevice({ id: 'c1', type: 'curtain' } as any, { _client: fakeClient } as any)
53
-
54
- // Setting position via HAP-style setState
55
- await d.setState({ position: 25 })
56
- expect(calls.pop()?.command).toBe('setPosition')
57
-
58
- // Matter descriptor write: call targetPosition write handler
59
- const matter = d.createMatterAccessory({})
60
- const cluster = matter.clusters.find((c: any) => c.type === 'Shade')
61
- // targetPosition attribute exists and has a write function
62
- expect(cluster.attributes.targetPosition.write).toBeTypeOf('function')
63
- await cluster.attributes.targetPosition.write(80)
64
- expect(calls.pop()?.command).toBe('setPosition')
65
- })
66
-
67
- it('Lock: user management actions map to correct commands', async () => {
68
- const calls: any[] = []
69
- const fakeClient: any = { setDeviceState: async (id: string, body: any) => { calls.push(body); return { ok: true } } }
70
- const d = new LockDevice({ id: 'lock1', type: 'lock' } as any, { _client: fakeClient } as any)
71
-
72
- // addUser via setState
73
- await d.setState({ action: 'addUser', user: 'u1', pin: '1234' })
74
- let last = calls.pop()
75
- expect(last.command).toBe('addUserCode')
76
- expect(last.parameter).toBe('u1:1234')
77
-
78
- // removeUser
79
- await d.setState({ action: 'removeUser', user: 'u1' })
80
- last = calls.pop()
81
- expect(last.command).toBe('removeUserCode')
82
-
83
- // unlockWithPin
84
- await d.setState({ action: 'unlockWithPin', pin: '0000' })
85
- last = calls.pop()
86
- expect(last.command).toBe('unlockWithPin')
87
-
88
- // Matter descriptor write for addUser
89
- const matter = d.createMatterAccessory({})
90
- const mgmt = matter.clusters.find((c: any) => c.type === 'DoorLockUserManagement')
91
- expect(mgmt.attributes.addUser.write).toBeTypeOf('function')
92
- await mgmt.attributes.addUser.write({ user: 'u2', pin: '4321' })
93
- last = calls.pop()
94
- expect(last.command).toBe('addUserCode')
95
- expect(last.parameter).toBe('u2:4321')
96
- })
97
- })
@@ -1,101 +0,0 @@
1
- import { describe, it, expect } from 'vitest'
2
- import { LightDevice, FanDevice, CurtainDevice, LockDevice, HumidifierMatterDevice } from '../src/devices/genericDevice'
3
- import { makeFakeApiWithMatter } from './helpers/matter-harness'
4
-
5
- const api = makeFakeApiWithMatter()
6
-
7
- describe('Matter device state translations', () => {
8
- it('Light: reads and writes map to setState/getState', async () => {
9
- const device = new LightDevice({ id: 'l-1', type: 'light', name: 'L1' }, {})
10
- let lastSet: any = null
11
- device.getState = async () => ({ on: true, brightness: 75, hue: 120, saturation: 50, colorTemperature: 350 })
12
- device.setState = async (c: any) => { lastSet = c; return { success: true } }
13
-
14
- const desc = device.createMatterAccessory(api)
15
- const levelCluster = desc.clusters.find((c: any) => c.clusterId === 0x0008 || c.type === 'LevelControl')
16
- expect(levelCluster).toBeDefined()
17
- const levelAttr = levelCluster.attributes.currentLevel || levelCluster.attributes["0x0000"] || Object.values(levelCluster.attributes)[0]
18
- const read = await levelAttr.read()
19
- expect(read).toBe(75)
20
-
21
- // write should call setState with brightness
22
- await levelAttr.write(30)
23
- expect(lastSet).toMatchObject({ brightness: 30 })
24
- })
25
-
26
- it('Fan: reads speed and writes speed/oscillation', async () => {
27
- const device = new FanDevice({ id: 'f-1', type: 'fan', name: 'F1' }, {})
28
- let last: any = null
29
- device.getState = async () => ({ on: false, speed: 45, oscillating: true, swingMode: 'auto' })
30
- device.setState = async (c: any) => { last = c; return { success: true } }
31
-
32
- const desc = device.createMatterAccessory(api)
33
- const fanCluster = desc.clusters.find((c: any) => c.clusterId === 0x0202 || c.type === 'FanControl')
34
- expect(fanCluster).toBeDefined()
35
- const speedAttr = fanCluster.attributes.rotationSpeed || Object.values(fanCluster.attributes)[0]
36
- const speed = await speedAttr.read()
37
- expect(speed).toBe(45)
38
-
39
- await speedAttr.write(80)
40
- expect(last).toMatchObject({ speed: 80 })
41
-
42
- // oscillation write
43
- const osc = fanCluster.attributes.oscillation
44
- await osc.write(true)
45
- expect(last).toMatchObject({ oscillate: true })
46
- })
47
-
48
- it('Curtain: reads and writes position', async () => {
49
- const device = new CurtainDevice({ id: 'c-1', type: 'curtain', name: 'C1' }, {})
50
- let last: any = null
51
- device.getState = async () => ({ position: 42 })
52
- device.setState = async (c: any) => { last = c; return { success: true } }
53
-
54
- const desc = device.createMatterAccessory(api)
55
- const shade = desc.clusters.find((c: any) => c.clusterId === 0x0102 || c.type === 'Shade')
56
- expect(shade).toBeDefined()
57
- const current = shade.attributes.currentPosition || shade.attributes["0x0000"]
58
- const v = await current.read()
59
- expect(v).toBe(42)
60
-
61
- const target = shade.attributes.targetPosition || shade.attributes["0x0001"]
62
- await target.write(10)
63
- expect(last).toMatchObject({ position: 10 })
64
- })
65
-
66
- it('Lock: reads and writes locked state', async () => {
67
- const device = new LockDevice({ id: 'k-1', type: 'lock', name: 'K1' }, {})
68
- let last: any = null
69
- device.getState = async () => ({ locked: true })
70
- device.setState = async (c: any) => { last = c; return { success: true } }
71
-
72
- const desc = device.createMatterAccessory(api)
73
- const lock = desc.clusters.find((c: any) => c.clusterId === 0x0101 || c.type === 'DoorLock')
74
- expect(lock).toBeDefined()
75
- const lockAttr = lock.attributes.lockState || lock.attributes["0x0000"]
76
- const v = await lockAttr.read()
77
- expect(v).toBe(true)
78
-
79
- await lockAttr.write(false)
80
- expect(last).toMatchObject({ locked: false })
81
- })
82
-
83
- it('Humidifier: reads humidity and toggles on/off', async () => {
84
- const device = new HumidifierMatterDevice({ id: 'h-1', type: 'humidifier', name: 'H1' }, {})
85
- let last: any = null
86
- device.getState = async () => ({ humidity: 55, on: false })
87
- device.setState = async (c: any) => { last = c; return { success: true } }
88
-
89
- const desc = device.createMatterAccessory(api)
90
- const rh = desc.clusters.find((c: any) => c.clusterId === 0x0405 || c.type === 'RelativeHumiditySensor')
91
- expect(rh).toBeDefined()
92
- const humAttr = rh.attributes.currentRelativeHumidity || rh.attributes["0x0000"]
93
- const hv = await humAttr.read()
94
- expect(hv).toBe(55)
95
-
96
- const onCluster = desc.clusters.find((c: any) => c.clusterId === 0x0006 || c.type === 'OnOff')
97
- const onAttr = onCluster.attributes.onOff || onCluster.attributes["0x0000"]
98
- await onAttr.write(true)
99
- expect(last).toMatchObject({ on: true })
100
- })
101
- })
@@ -1,70 +0,0 @@
1
- import { describe, it, expect } from 'vitest'
2
- import { createPlatformProxy } from '../src/utils'
3
- import { makeFakeApiWithMatter, makeFakeApiWithoutMatter, makeFakeMatterApi } from './helpers/matter-harness'
4
- import SwitchBotHAPPlatform, { SwitchBotMatterPlatform } from '../src/platform'
5
-
6
- // Expanded Matter integration harness tests
7
-
8
- describe('Matter integration harness', () => {
9
- it('registers Matter accessories when Matter API available', async () => {
10
- // Instantiate the real Matter platform directly to test its registration path
11
- const matterApi = makeFakeMatterApi()
12
- const api = makeFakeApiWithMatter(matterApi)
13
-
14
- const platform = new SwitchBotMatterPlatform(console as any, { devices: [{ id: 'm-1', type: 'light', name: 'L1' }], enableMatter: true, preferMatter: true } as any, api as any)
15
-
16
- // Call register path directly
17
- if (typeof (platform as any).registerMatterAccessories === 'function') {
18
- await (platform as any).registerMatterAccessories()
19
- } else if (typeof (platform as any).loadDevices === 'function') {
20
- await (platform as any).loadDevices()
21
- }
22
-
23
- const regs = matterApi.getRegistered()
24
- expect(regs.length).toBeGreaterThanOrEqual(1)
25
- // The platform registers serialized accessories; ensure deviceId is present in context
26
- expect(regs[0]).toHaveProperty('context')
27
- expect(regs[0].context).toHaveProperty('deviceId', 'm-1')
28
- })
29
-
30
- it('falls back to HAP when Matter API unavailable', async () => {
31
- // Directly instantiate the HAP platform to exercise HAP fallback
32
- const api = makeFakeApiWithoutMatter()
33
- const platform = new SwitchBotHAPPlatform(console as any, { devices: [{ id: 'h-1', type: 'light', name: 'H1' }], enableMatter: true, preferMatter: true } as any, api as any)
34
-
35
- // call loadDevices for HAP flow
36
- if (typeof (platform as any).loadDevices === 'function') await (platform as any).loadDevices()
37
-
38
- // Platform should have an accessories map populated
39
- const accMap = (platform as any).accessories
40
- expect(accMap).toBeDefined()
41
- const keys = Array.from(accMap.keys())
42
- expect(keys.length).toBeGreaterThanOrEqual(1)
43
- })
44
-
45
- it('reuses restored Matter accessories instead of registering duplicates', async () => {
46
- const matterApi = makeFakeMatterApi()
47
- const api = makeFakeApiWithMatter(matterApi)
48
-
49
- const platform = new SwitchBotMatterPlatform(console as any, { devices: [{ id: 'r-1', type: 'light', name: 'R1' }], enableMatter: true, preferMatter: true } as any, api as any)
50
-
51
- // Simulate a restored accessory present before registration
52
- const restored = { uuid: matterApi.uuid.generate('r-1'), displayName: 'Restored R1', context: { deviceId: 'r-1' } }
53
- if (typeof (platform as any).configureMatterAccessory === 'function') {
54
- await (platform as any).configureMatterAccessory(restored)
55
- } else if (typeof (platform as any).configureAccessory === 'function') {
56
- await (platform as any).configureAccessory(restored)
57
- }
58
-
59
- // Now call register path
60
- if (typeof (platform as any).registerMatterAccessories === 'function') await (platform as any).registerMatterAccessories()
61
- else if (typeof (platform as any).loadDevices === 'function') {
62
- await (platform as any).loadDevices()
63
- if (typeof (platform as any).registerMatterAccessories === 'function') await (platform as any).registerMatterAccessories()
64
- }
65
-
66
- const accs = (platform as any).accessories
67
- const found = Array.from(accs.values()).some((a: any) => a && (a.uuid === restored.uuid || a.UUID === restored.uuid))
68
- expect(found).toBeTruthy()
69
- })
70
- })
@@ -1,55 +0,0 @@
1
- import { describe, it, expect } from 'vitest'
2
- import { SwitchBotHAPPlatform } from '../src/platform'
3
-
4
- // Minimal mock logger
5
- const log = {
6
- info: () => {},
7
- warn: () => {},
8
- error: () => {},
9
- debug: () => {},
10
- }
11
-
12
- describe('platform integration (HAP)', () => {
13
- it('registers HAP accessories using Homebridge API', async () => {
14
- const registered: any[] = []
15
-
16
- // Minimal hap stub
17
- const hap = {
18
- uuid: { generate: (s: string) => `uuid-${s}` },
19
- Service: { Switch: 'Switch', Lightbulb: 'Lightbulb' },
20
- Characteristic: { On: 'On', Brightness: 'Brightness' },
21
- }
22
-
23
- // Simple platformAccessory constructor
24
- function PlatformAccessory(name: string, uuid: string) {
25
- this.displayName = name
26
- this.UUID = uuid
27
- this.services = []
28
- this.getService = (type: any) => this.services.find((s: any) => s.type === type)
29
- this.addService = (type: any) => {
30
- const s: any = { type, characteristics: {} }
31
- this.services.push(s)
32
- return s
33
- }
34
- }
35
-
36
- const api: any = {
37
- hap,
38
- platformAccessory: PlatformAccessory as any,
39
- registerPlatformAccessories: (_p: string, _n: string, accs: any[]) => registered.push(...accs),
40
- on: (_ev: string, cb: Function) => { /* do not auto-run */ },
41
- }
42
-
43
- const config: any = { devices: [{ id: 'dev1', type: 'light', name: 'L1' }] }
44
-
45
- const platform = new SwitchBotHAPPlatform(log as any, config, api)
46
-
47
- // call loadDevices directly to control flow
48
- await (platform as any).loadDevices()
49
-
50
- // Expect accessory created and registered
51
- expect(registered.length).toBeGreaterThanOrEqual(1)
52
- const keys = Array.from((platform as any).accessories.keys())
53
- expect(keys.some((k: string) => k.includes('dev1'))).toBeTruthy()
54
- })
55
- })