@switchbot/homebridge-switchbot 5.0.0-beta.98 → 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 -450
  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 -526
  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 -850
  267. package/dist/platform.js.map +0 -1
  268. package/src/platform.ts +0 -867
  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
@@ -0,0 +1,141 @@
1
+ import type { SwitchBotPluginConfig } from './settings.js';
2
+ import type { API, Logger, PlatformConfig } from 'homebridge';
3
+ /**
4
+ * Homebridge platform class for SwitchBot Matter integration.
5
+ * Handles device discovery, registration, polling, and accessory lifecycle for Matter-enabled SwitchBot devices.
6
+ *
7
+ * @class SwitchBotMatterPlatform
8
+ * @param {Logger} log - Homebridge logger instance
9
+ * @param {PlatformConfig} config - Platform configuration object
10
+ * @param {API} [api] - Optional Homebridge API instance
11
+ * @property {API | undefined} api - Homebridge API instance
12
+ * @property {Logger} log - Homebridge logger instance
13
+ * @property {SwitchBotPluginConfig} config - Parsed plugin config
14
+ * @property {any[]} devices - All created device instances
15
+ * @property {Map<string, any>} accessories - Map of accessory UUID to accessory object
16
+ * @property {string} lastConfigHash - Hash of last loaded config for change detection
17
+ * @property {NodeJS.Timeout | null} configReloadInterval - Interval for periodic config reload
18
+ * @property {Map<string, NodeJS.Timeout>} openApiPollTimers - Timers for per-device OpenAPI polling
19
+ * @property {NodeJS.Timeout | null} openApiBatchTimer - Timer for batched OpenAPI polling
20
+ * @property {number} openApiRequestsToday - Count of OpenAPI requests made today
21
+ * @property {number} openApiLastReset - Timestamp (ms) of last OpenAPI daily counter reset
22
+ */
23
+ export declare class SwitchBotMatterPlatform {
24
+ /** Homebridge API instance */
25
+ api: API | undefined;
26
+ /** Homebridge logger instance */
27
+ log: Logger;
28
+ /** Parsed plugin config */
29
+ config: SwitchBotPluginConfig;
30
+ /** All created device instances */
31
+ devices: any[];
32
+ /** Map of accessory UUID to accessory object */
33
+ accessories: Map<string, any>;
34
+ /** Hash of last loaded config for change detection */
35
+ private lastConfigHash;
36
+ /** Interval for periodic config reload */
37
+ private configReloadInterval;
38
+ /** Timers for per-device OpenAPI polling */
39
+ private openApiPollTimers;
40
+ /** Timer for batched OpenAPI polling */
41
+ private openApiBatchTimer;
42
+ /** Count of OpenAPI requests made today */
43
+ private openApiRequestsToday;
44
+ /** Timestamp (ms) of last OpenAPI daily counter reset */
45
+ private openApiLastReset;
46
+ /**
47
+ * Construct the SwitchBot Matter platform.
48
+ * @param log Homebridge logger
49
+ * @param config Platform config
50
+ * @param api Homebridge API instance
51
+ */
52
+ constructor(log: Logger, config: PlatformConfig, api?: API);
53
+ /**
54
+ * Discover and create all device instances from config.
55
+ * Populates this.devices and logs registration status for each device.
56
+ *
57
+ * @returns {Promise<void>} Resolves when all devices are loaded and registered
58
+ */
59
+ loadDevices(): Promise<void>;
60
+ /**
61
+ * Registers all Matter accessories with the Homebridge Matter API.
62
+ *
63
+ * This method is called after all device instances have been created. It handles both new and restored
64
+ * accessories, updating their context, clusters, and other Matter-specific metadata as needed. Accessories
65
+ * are registered with Homebridge using the Matter API, and the internal accessory map is updated accordingly.
66
+ *
67
+ * @param {Array<{created: any, d: any, type: string, useMatter: boolean, matterAvailable: boolean}>} createdDevices - Array of device descriptors:
68
+ * - created: The created device instance
69
+ * - d: The normalized device config object
70
+ * - type: The normalized device type string
71
+ * - useMatter: Whether Matter is enabled for this device
72
+ * - matterAvailable: Whether Matter is available on this bridge
73
+ * @returns {Promise<void>} Resolves when registration is complete
74
+ *
75
+ * Differences from HAP registration:
76
+ * - Uses the Matter API (not HAP API) for accessory registration.
77
+ * - Adds Matter clusters and handlers to each accessory based on the device descriptor.
78
+ * - Only registers accessories where Matter is enabled and supported.
79
+ * - Accessory context and cluster wiring are Matter-specific.
80
+ *
81
+ * If the Homebridge Matter API is not available, registration is skipped and a log message is emitted.
82
+ * Accessories that are not enabled for Matter are ignored.
83
+ */
84
+ registerMatterAccessories(createdDevices: {
85
+ created: any;
86
+ d: any;
87
+ type: string;
88
+ useMatter: boolean;
89
+ matterAvailable: boolean;
90
+ }[]): Promise<void>;
91
+ /**
92
+ * Returns the timestamp (ms) of the last OpenAPI daily counter reset.
93
+ * @returns {number} Timestamp in ms
94
+ */
95
+ getOpenApiLastReset(): number;
96
+ /**
97
+ * Logs the last OpenAPI reset time in a human-readable format.
98
+ * @returns {void}
99
+ */
100
+ logOpenApiLastReset(): void;
101
+ /**
102
+ * Compute a hash of the current device config for change detection.
103
+ * @returns {string} JSON string hash of device config
104
+ */
105
+ private getConfigHash;
106
+ /**
107
+ * Reload devices if config has changed since last load.
108
+ * Unregisters accessories and removes devices no longer in config.
109
+ * Calls loadDevices to repopulate devices and accessories.
110
+ *
111
+ * @returns {Promise<void>} Resolves when reload is complete
112
+ */
113
+ private checkAndReloadDevices;
114
+ /**
115
+ * Cleanup method to clear config reload interval on shutdown.
116
+ * Called by Homebridge on shutdown event.
117
+ * @returns {void}
118
+ */
119
+ shutdown(): void;
120
+ /**
121
+ * Setup OpenAPI polling for all devices according to config (global, per-device, batch, rate limit).
122
+ * Handles daily request limits, per-device and batch polling, and resets.
123
+ *
124
+ * @returns {void}
125
+ */
126
+ private _setupOpenApiPolling;
127
+ /**
128
+ * Called by Homebridge to restore cached Matter accessories on startup.
129
+ * @param {any} accessory - The cached accessory object
130
+ * @returns {Promise<void>} Resolves when accessory is restored
131
+ */
132
+ configureAccessory(accessory: any): Promise<void>;
133
+ /**
134
+ * Called by Homebridge to restore cached Matter accessories (alternate signature).
135
+ * @param {any} accessory - The cached accessory object
136
+ * @returns {void}
137
+ */
138
+ configureMatterAccessory(accessory: any): void;
139
+ }
140
+ export default SwitchBotMatterPlatform;
141
+ //# sourceMappingURL=SwitchBotMatterPlatform.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SwitchBotMatterPlatform.d.ts","sourceRoot":"","sources":["../src/SwitchBotMatterPlatform.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,eAAe,CAAA;AAC1D,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AAO7D;;;;;;;;;;;;;;;;;;;GAmBG;AACH,qBAAa,uBAAuB;IAClC,8BAA8B;IAC9B,GAAG,EAAE,GAAG,GAAG,SAAS,CAAA;IACpB,iCAAiC;IACjC,GAAG,EAAE,MAAM,CAAA;IACX,2BAA2B;IAC3B,MAAM,EAAE,qBAAqB,CAAA;IAC7B,mCAAmC;IACnC,OAAO,EAAE,GAAG,EAAE,CAAK;IACnB,gDAAgD;IAChD,WAAW,EAAE,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;IAC7B,sDAAsD;IACtD,OAAO,CAAC,cAAc,CAAa;IACnC,0CAA0C;IAC1C,OAAO,CAAC,oBAAoB,CAA8B;IAC1D,4CAA4C;IAC5C,OAAO,CAAC,iBAAiB,CAAyC;IAClE,wCAAwC;IACxC,OAAO,CAAC,iBAAiB,CAA8B;IACvD,2CAA2C;IAC3C,OAAO,CAAC,oBAAoB,CAAI;IAChC,yDAAyD;IACzD,OAAO,CAAC,gBAAgB,CAAI;IAE5B;;;;;OAKG;gBACS,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,cAAc,EAAE,GAAG,CAAC,EAAE,GAAG;IA2C1D;;;;;OAKG;IACG,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC;IAkDlC;;;;;;;;;;;;;;;;;;;;;;;OAuBG;IACG,yBAAyB,CAAC,cAAc,EAAE;QAAE,OAAO,EAAE,GAAG,CAAC;QAAC,CAAC,EAAE,GAAG,CAAC;QAAC,IAAI,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,OAAO,CAAC;QAAC,eAAe,EAAE,OAAO,CAAA;KAAE,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAqGtJ;;;OAGG;IACH,mBAAmB,IAAI,MAAM;IAI7B;;;OAGG;IACH,mBAAmB,IAAI,IAAI;IAS3B;;;OAGG;IACH,OAAO,CAAC,aAAa;IAUrB;;;;;;OAMG;YACW,qBAAqB;IAiCnC;;;;OAIG;IACH,QAAQ,IAAI,IAAI;IAQhB;;;;;OAKG;IACH,OAAO,CAAC,oBAAoB;IAkI5B;;;;OAIG;IACG,kBAAkB,CAAC,SAAS,EAAE,GAAG,GAAG,OAAO,CAAC,IAAI,CAAC;IAUvD;;;;OAIG;IACH,wBAAwB,CAAC,SAAS,EAAE,GAAG,GAAG,IAAI;CAS/C;AAED,eAAe,uBAAuB,CAAA"}
@@ -0,0 +1,536 @@
1
+ import { createDevice } from './deviceFactory.js';
2
+ import { PLATFORM_NAME, PLUGIN_NAME } from './settings.js';
3
+ import { SwitchBotClient } from './switchbotClient.js';
4
+ import { createMatterHandlers, DEVICE_MATTER_CLUSTERS, DEVICE_MATTER_SUPPORTED, normalizeTypeForMatter, resolveMatterDeviceType } from './utils.js';
5
+ /**
6
+ * Homebridge platform class for SwitchBot Matter integration.
7
+ * Handles device discovery, registration, polling, and accessory lifecycle for Matter-enabled SwitchBot devices.
8
+ *
9
+ * @class SwitchBotMatterPlatform
10
+ * @param {Logger} log - Homebridge logger instance
11
+ * @param {PlatformConfig} config - Platform configuration object
12
+ * @param {API} [api] - Optional Homebridge API instance
13
+ * @property {API | undefined} api - Homebridge API instance
14
+ * @property {Logger} log - Homebridge logger instance
15
+ * @property {SwitchBotPluginConfig} config - Parsed plugin config
16
+ * @property {any[]} devices - All created device instances
17
+ * @property {Map<string, any>} accessories - Map of accessory UUID to accessory object
18
+ * @property {string} lastConfigHash - Hash of last loaded config for change detection
19
+ * @property {NodeJS.Timeout | null} configReloadInterval - Interval for periodic config reload
20
+ * @property {Map<string, NodeJS.Timeout>} openApiPollTimers - Timers for per-device OpenAPI polling
21
+ * @property {NodeJS.Timeout | null} openApiBatchTimer - Timer for batched OpenAPI polling
22
+ * @property {number} openApiRequestsToday - Count of OpenAPI requests made today
23
+ * @property {number} openApiLastReset - Timestamp (ms) of last OpenAPI daily counter reset
24
+ */
25
+ export class SwitchBotMatterPlatform {
26
+ /** Homebridge API instance */
27
+ api;
28
+ /** Homebridge logger instance */
29
+ log;
30
+ /** Parsed plugin config */
31
+ config;
32
+ /** All created device instances */
33
+ devices = [];
34
+ /** Map of accessory UUID to accessory object */
35
+ accessories;
36
+ /** Hash of last loaded config for change detection */
37
+ lastConfigHash = '';
38
+ /** Interval for periodic config reload */
39
+ configReloadInterval = null;
40
+ /** Timers for per-device OpenAPI polling */
41
+ openApiPollTimers = new Map();
42
+ /** Timer for batched OpenAPI polling */
43
+ openApiBatchTimer = null;
44
+ /** Count of OpenAPI requests made today */
45
+ openApiRequestsToday = 0;
46
+ /** Timestamp (ms) of last OpenAPI daily counter reset */
47
+ openApiLastReset = 0;
48
+ /**
49
+ * Construct the SwitchBot Matter platform.
50
+ * @param log Homebridge logger
51
+ * @param config Platform config
52
+ * @param api Homebridge API instance
53
+ */
54
+ constructor(log, config, api) {
55
+ this.log = log;
56
+ // Ensure both log and logger are set for downstream device constructors
57
+ this.config = { ...config, log, logger: log };
58
+ this.api = api;
59
+ this.accessories = new Map();
60
+ this.log.info('SwitchBot Matter platform initialized');
61
+ // Create/shared SwitchBot client and attach to config so child devices reuse it.
62
+ try {
63
+ const client = new SwitchBotClient(this.config);
64
+ void client.init();
65
+ this.config._client = client;
66
+ }
67
+ catch (e) {
68
+ this.log.debug('Failed to create shared SwitchBot client', e);
69
+ }
70
+ // Wait for Homebridge to finish launching to create/register accessories
71
+ if (this.api && typeof this.api.on === 'function') {
72
+ this.api.on('didFinishLaunching', async () => {
73
+ await this.loadDevices();
74
+ this._setupOpenApiPolling();
75
+ // Start periodic config reload to pick up UI changes
76
+ this.configReloadInterval = setInterval(() => {
77
+ void this.checkAndReloadDevices();
78
+ }, 10000); // Check every 10 seconds
79
+ // Listen for Homebridge shutdown to clear interval
80
+ if (typeof this.api.on === 'function') {
81
+ this.api.on('shutdown', () => {
82
+ this.shutdown();
83
+ });
84
+ }
85
+ });
86
+ }
87
+ else {
88
+ void this.loadDevices();
89
+ this._setupOpenApiPolling();
90
+ // Start periodic config reload to pick up UI changes
91
+ this.configReloadInterval = setInterval(() => {
92
+ void this.checkAndReloadDevices();
93
+ }, 10000); // Check every 10 seconds
94
+ }
95
+ }
96
+ /**
97
+ * Discover and create all device instances from config.
98
+ * Populates this.devices and logs registration status for each device.
99
+ *
100
+ * @returns {Promise<void>} Resolves when all devices are loaded and registered
101
+ */
102
+ async loadDevices() {
103
+ const newHash = this.getConfigHash();
104
+ if (newHash === this.lastConfigHash) {
105
+ this.log.debug('Config unchanged, skipping device reload');
106
+ return;
107
+ }
108
+ const devices = this.config?.devices ?? [];
109
+ const createdDevices = [];
110
+ for (const raw of devices) {
111
+ // Normalize config keys from UI schema to internal shape (for cross-platform consistency)
112
+ const d = {
113
+ id: raw.deviceId ?? raw.id,
114
+ name: raw.configDeviceName ?? raw.name,
115
+ type: raw.configDeviceType ?? raw.type ?? raw.deviceType ?? 'unknown',
116
+ encryptionKey: raw.encryptionKey,
117
+ keyId: raw.keyId,
118
+ _raw: raw,
119
+ };
120
+ const type = normalizeTypeForMatter(d.type);
121
+ const deviceOpts = { id: d.id, type, name: d.name, encryptionKey: d.encryptionKey, keyId: d.keyId, log: this.log };
122
+ this.log.debug(`[Matter/Debug] Device options for ${d.name ?? d.id}:`, JSON.stringify(deviceOpts, null, 2));
123
+ const matterSupported = !!DEVICE_MATTER_SUPPORTED[(type || '').toLowerCase()];
124
+ const matterAvailable = !!(this.api?.isMatterAvailable?.() && this.api?.isMatterEnabled?.());
125
+ const matterEnabled = matterAvailable || !!this.config.enableMatter;
126
+ const useMatter = matterEnabled && matterSupported;
127
+ try {
128
+ const created = await createDevice(deviceOpts, this.config, useMatter);
129
+ this.devices.push(created);
130
+ createdDevices.push({ created, d, type, useMatter, matterAvailable });
131
+ if (useMatter) {
132
+ this.log.info(`Prepared Matter accessory for ${d.id} (${type})${matterAvailable ? ' (auto-detected)' : ' (manually enabled)'}`);
133
+ }
134
+ else {
135
+ if (!matterEnabled) {
136
+ this.log.info(`Skipping Matter for ${d.id} (${type}) - Matter not available on this bridge`);
137
+ }
138
+ else if (!matterSupported) {
139
+ this.log.info(`Skipping Matter for ${d.id} (${type}) - device type not supported`);
140
+ }
141
+ else {
142
+ this.log.info(`Skipping Matter for ${d.id} (${type}) - not supported`);
143
+ }
144
+ }
145
+ }
146
+ catch (e) {
147
+ this.log.error(`Failed to create Matter device ${d.id}:`, e instanceof Error ? e.stack || e.message : e);
148
+ }
149
+ }
150
+ // Register Matter accessories after device creation for symmetry with HAP platform
151
+ await this.registerMatterAccessories(createdDevices);
152
+ this.lastConfigHash = newHash;
153
+ }
154
+ /**
155
+ * Registers all Matter accessories with the Homebridge Matter API.
156
+ *
157
+ * This method is called after all device instances have been created. It handles both new and restored
158
+ * accessories, updating their context, clusters, and other Matter-specific metadata as needed. Accessories
159
+ * are registered with Homebridge using the Matter API, and the internal accessory map is updated accordingly.
160
+ *
161
+ * @param {Array<{created: any, d: any, type: string, useMatter: boolean, matterAvailable: boolean}>} createdDevices - Array of device descriptors:
162
+ * - created: The created device instance
163
+ * - d: The normalized device config object
164
+ * - type: The normalized device type string
165
+ * - useMatter: Whether Matter is enabled for this device
166
+ * - matterAvailable: Whether Matter is available on this bridge
167
+ * @returns {Promise<void>} Resolves when registration is complete
168
+ *
169
+ * Differences from HAP registration:
170
+ * - Uses the Matter API (not HAP API) for accessory registration.
171
+ * - Adds Matter clusters and handlers to each accessory based on the device descriptor.
172
+ * - Only registers accessories where Matter is enabled and supported.
173
+ * - Accessory context and cluster wiring are Matter-specific.
174
+ *
175
+ * If the Homebridge Matter API is not available, registration is skipped and a log message is emitted.
176
+ * Accessories that are not enabled for Matter are ignored.
177
+ */
178
+ async registerMatterAccessories(createdDevices) {
179
+ if (!this.api || !this.api.matter || typeof this.api.matter.registerPlatformAccessories !== 'function') {
180
+ this.log.info('Homebridge Matter API not available; skipping Matter accessory registration');
181
+ return;
182
+ }
183
+ const matterApi = this.api.matter;
184
+ const accessoriesToRegister = [];
185
+ for (const { created, d, type, useMatter, matterAvailable } of createdDevices) {
186
+ // Only register accessories where Matter is enabled and supported
187
+ if (!useMatter) {
188
+ // Log reason for skipping registration
189
+ if (!matterAvailable) {
190
+ this.log.info(`Skipping Matter registration for ${d.id} (${type}) - Matter API not available on this bridge`);
191
+ }
192
+ else {
193
+ this.log.info(`Skipping Matter registration for ${d.id} (${type}) - device type not supported for Matter`);
194
+ }
195
+ continue;
196
+ }
197
+ try {
198
+ // Prepare accessory descriptor from device
199
+ const createdDesc = await created.createAccessory(this.api);
200
+ const uuid = matterApi.uuid.generate(`${d.id}`);
201
+ // Reuse cached accessory if available by uuid
202
+ let accessory = this.accessories.get(uuid);
203
+ // Reuse cached accessory if available by uuid or deviceId
204
+ if (!accessory) {
205
+ for (const [, a] of this.accessories.entries()) {
206
+ try {
207
+ if (a && a.context && a.context.deviceId === d.id) {
208
+ accessory = a;
209
+ break;
210
+ }
211
+ }
212
+ catch (e) {
213
+ // ignore
214
+ }
215
+ }
216
+ }
217
+ if (!accessory) {
218
+ // Create new accessory object for Matter
219
+ let clusters = DEVICE_MATTER_CLUSTERS[type.toLowerCase()];
220
+ if (!clusters) {
221
+ clusters = createdDesc.clusters || { onOff: { onOff: false } };
222
+ }
223
+ const deviceType = resolveMatterDeviceType(matterApi, type, createdDesc.deviceType, clusters);
224
+ accessory = {
225
+ UUID: uuid,
226
+ displayName: createdDesc.name || d.name || type,
227
+ deviceType,
228
+ manufacturer: createdDesc.manufacturer || 'SwitchBot',
229
+ model: createdDesc.model || type,
230
+ serialNumber: createdDesc.serialNumber || d.id,
231
+ reachable: createdDesc.reachable !== false,
232
+ firmwareRevision: createdDesc.firmwareRevision || '1.0.0',
233
+ hardwareRevision: createdDesc.hardwareRevision || '',
234
+ clusters,
235
+ handlers: createdDesc.handlers || createMatterHandlers(this.log, d.id, type, this.config?._client) || undefined,
236
+ context: { deviceId: d.id, type, created: true },
237
+ };
238
+ accessoriesToRegister.push(accessory);
239
+ this.accessories.set(uuid, accessory);
240
+ }
241
+ else {
242
+ // Ensure context and update properties for restored accessory
243
+ let clusters = DEVICE_MATTER_CLUSTERS[type.toLowerCase()];
244
+ if (!clusters) {
245
+ clusters = accessory.clusters || createdDesc.clusters || { onOff: { onOff: false } };
246
+ }
247
+ const deviceType = resolveMatterDeviceType(matterApi, type, accessory.deviceType || createdDesc.deviceType, clusters);
248
+ accessory.context = accessory.context || {};
249
+ accessory.context.deviceId = accessory.context.deviceId || d.id;
250
+ accessory.context.type = accessory.context.type || type;
251
+ accessory.deviceType = deviceType;
252
+ accessory.manufacturer = accessory.manufacturer || createdDesc.manufacturer || 'SwitchBot';
253
+ accessory.model = accessory.model || createdDesc.model || type;
254
+ accessory.serialNumber = accessory.serialNumber || createdDesc.serialNumber || d.id;
255
+ accessory.reachable = accessory.reachable !== false;
256
+ accessory.firmwareRevision = accessory.firmwareRevision || createdDesc.firmwareRevision || '1.0.0';
257
+ accessory.hardwareRevision = accessory.hardwareRevision || createdDesc.hardwareRevision || '';
258
+ accessory.clusters = clusters;
259
+ accessory.handlers = createdDesc.handlers || createMatterHandlers(this.log, d.id, type, this.config?._client) || undefined;
260
+ accessory.displayName = createdDesc.name || d.name || type;
261
+ accessory.UUID = accessory.UUID || accessory.uuid || uuid;
262
+ accessoriesToRegister.push(accessory);
263
+ this.accessories.set(accessory.UUID || uuid, accessory);
264
+ }
265
+ this.log.info(`Created/updated Matter accessory ${d.id} (${type})`);
266
+ }
267
+ catch (e) {
268
+ this.log.warn(`Matter accessory creation failed for ${d.id} (${type})`, e);
269
+ }
270
+ }
271
+ if (accessoriesToRegister.length > 0) {
272
+ try {
273
+ await matterApi.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, accessoriesToRegister);
274
+ this.log.info(`Registered ${accessoriesToRegister.length} Matter accessory(ies) with Homebridge`);
275
+ }
276
+ catch (e) {
277
+ this.log.warn('Failed to register Matter accessories', e);
278
+ }
279
+ }
280
+ else {
281
+ this.log.info('No Matter accessories to register');
282
+ }
283
+ }
284
+ /**
285
+ * Returns the timestamp (ms) of the last OpenAPI daily counter reset.
286
+ * @returns {number} Timestamp in ms
287
+ */
288
+ getOpenApiLastReset() {
289
+ return this.openApiLastReset;
290
+ }
291
+ /**
292
+ * Logs the last OpenAPI reset time in a human-readable format.
293
+ * @returns {void}
294
+ */
295
+ logOpenApiLastReset() {
296
+ if (this.openApiLastReset) {
297
+ const date = new Date(this.openApiLastReset);
298
+ this.log.info(`[OpenAPI] Last daily counter reset: ${date.toLocaleString()}`);
299
+ }
300
+ else {
301
+ this.log.info('[OpenAPI] Daily counter has not been reset yet.');
302
+ }
303
+ }
304
+ /**
305
+ * Compute a hash of the current device config for change detection.
306
+ * @returns {string} JSON string hash of device config
307
+ */
308
+ getConfigHash() {
309
+ // Create a simple hash of current device config to detect changes
310
+ const devices = this.config?.devices ?? [];
311
+ return JSON.stringify(devices.map((d) => ({
312
+ id: d.deviceId ?? d.id,
313
+ type: d.configDeviceType ?? d.type,
314
+ name: d.configDeviceName ?? d.name,
315
+ })));
316
+ }
317
+ /**
318
+ * Reload devices if config has changed since last load.
319
+ * Unregisters accessories and removes devices no longer in config.
320
+ * Calls loadDevices to repopulate devices and accessories.
321
+ *
322
+ * @returns {Promise<void>} Resolves when reload is complete
323
+ */
324
+ async checkAndReloadDevices() {
325
+ const currentHash = this.getConfigHash();
326
+ if (currentHash !== this.lastConfigHash) {
327
+ this.log.info('[SwitchBot] Detected config changes, reloading devices...');
328
+ // Identify device IDs in new config
329
+ const devicesInConfig = new Set((this.config?.devices ?? []).map((d) => d.deviceId ?? d.id));
330
+ // Find accessories to remove (not in config)
331
+ const accessoriesToRemove = [];
332
+ for (const [uuid, accessory] of this.accessories.entries()) {
333
+ const deviceId = accessory?.context?.deviceId;
334
+ if (deviceId && !devicesInConfig.has(deviceId)) {
335
+ accessoriesToRemove.push({ accessory, uuid });
336
+ }
337
+ }
338
+ // Unregister removed accessories from Homebridge (Matter API)
339
+ if (accessoriesToRemove.length > 0 && this.api && this.api.matter && this.api.matter.unregisterPlatformAccessories) {
340
+ try {
341
+ this.api.matter.unregisterPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, accessoriesToRemove.map(a => a.accessory));
342
+ this.log.info(`Unregistered ${accessoriesToRemove.length} Matter accessory(ies) removed from config`);
343
+ for (const { uuid } of accessoriesToRemove) {
344
+ this.accessories.delete(uuid);
345
+ }
346
+ }
347
+ catch (e) {
348
+ this.log.warn('Failed to unregister removed Matter accessories', e);
349
+ }
350
+ }
351
+ // Remove devices from this.devices that are no longer in config
352
+ this.devices = this.devices.filter((dev) => devicesInConfig.has(dev?.id));
353
+ await this.loadDevices();
354
+ this.lastConfigHash = currentHash;
355
+ }
356
+ }
357
+ /**
358
+ * Cleanup method to clear config reload interval on shutdown.
359
+ * Called by Homebridge on shutdown event.
360
+ * @returns {void}
361
+ */
362
+ shutdown() {
363
+ if (this.configReloadInterval) {
364
+ clearInterval(this.configReloadInterval);
365
+ this.configReloadInterval = null;
366
+ this.log.info('Cleared config reload interval on shutdown');
367
+ }
368
+ }
369
+ /**
370
+ * Setup OpenAPI polling for all devices according to config (global, per-device, batch, rate limit).
371
+ * Handles daily request limits, per-device and batch polling, and resets.
372
+ *
373
+ * @returns {void}
374
+ */
375
+ _setupOpenApiPolling() {
376
+ // Clear any existing timers
377
+ for (const t of this.openApiPollTimers.values())
378
+ clearInterval(t);
379
+ this.openApiPollTimers.clear();
380
+ if (this.openApiBatchTimer) {
381
+ clearInterval(this.openApiBatchTimer);
382
+ }
383
+ this.openApiBatchTimer = null;
384
+ const cfg = this.config;
385
+ const devices = cfg.devices ?? [];
386
+ const globalRate = Math.max(Number(cfg.openApiRefreshRate) || 300, 30);
387
+ const batchEnabled = cfg.matterBatchEnabled !== false;
388
+ const batchRate = Math.max(Number(cfg.matterBatchRefreshRate) || globalRate, 30);
389
+ const batchConcurrency = Math.max(Number(cfg.matterBatchConcurrency) || 5, 1);
390
+ const batchJitter = Math.max(Number(cfg.matterBatchJitter) || 0, 0);
391
+ const dailyLimit = Math.max(Number(cfg.dailyApiLimit) || 10000, 1000);
392
+ const dailyReserve = Math.max(Number(cfg.dailyApiReserveForCommands) || 1000, 0);
393
+ const resetAtLocalMidnight = !!cfg.dailyApiResetLocalMidnight;
394
+ const webhookOnlyOnReserve = !!cfg.webhookOnlyOnReserve;
395
+ // Helper to reset daily counter
396
+ const resetCounter = () => {
397
+ this.openApiRequestsToday = 0;
398
+ this.openApiLastReset = Date.now();
399
+ this.log.info('[OpenAPI] Daily request counter reset');
400
+ };
401
+ // Schedule reset at midnight
402
+ const scheduleMidnightReset = () => {
403
+ const now = new Date();
404
+ let nextReset;
405
+ if (resetAtLocalMidnight) {
406
+ nextReset = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1, 0, 0, 1);
407
+ }
408
+ else {
409
+ nextReset = new Date(now);
410
+ nextReset.setUTCHours(24, 0, 1, 0);
411
+ }
412
+ const ms = nextReset.getTime() - now.getTime();
413
+ setTimeout(() => {
414
+ resetCounter();
415
+ scheduleMidnightReset();
416
+ }, ms);
417
+ };
418
+ scheduleMidnightReset();
419
+ // Helper to check if polling is allowed
420
+ const canPoll = () => {
421
+ if (this.openApiRequestsToday + dailyReserve >= dailyLimit) {
422
+ if (!webhookOnlyOnReserve) {
423
+ this.log.warn('[OpenAPI] Daily request limit reached, pausing background polling');
424
+ }
425
+ return false;
426
+ }
427
+ return true;
428
+ };
429
+ // Per-device polling (devices with per-device refreshRate)
430
+ for (const dev of devices) {
431
+ const id = dev.deviceId ?? dev.id;
432
+ const enabled = dev.enabled !== false;
433
+ if (!id || !enabled) {
434
+ continue;
435
+ }
436
+ const perDeviceRate = dev.refreshRate ? Math.max(Number(dev.refreshRate), 30) : null;
437
+ if (perDeviceRate) {
438
+ // Individual polling interval for this device
439
+ const timer = setInterval(async () => {
440
+ if (!canPoll()) {
441
+ return;
442
+ }
443
+ try {
444
+ const client = this.config._client;
445
+ if (client && typeof client.getDevice === 'function') {
446
+ await client.getDevice(id);
447
+ this.openApiRequestsToday++;
448
+ this.log.debug(`[OpenAPI] Polled device ${id} (per-device interval ${perDeviceRate}s) [${this.openApiRequestsToday}/${dailyLimit}]`);
449
+ }
450
+ }
451
+ catch (e) {
452
+ this.log.debug(`[OpenAPI] Polling failed for device ${id}:`, e?.message);
453
+ }
454
+ }, perDeviceRate * 1000);
455
+ this.openApiPollTimers.set(id, timer);
456
+ }
457
+ }
458
+ // Batched polling for all other devices
459
+ if (batchEnabled) {
460
+ // Devices not already polled individually
461
+ const batchDevices = devices.filter((dev) => {
462
+ const id = dev.deviceId ?? dev.id;
463
+ const enabled = dev.enabled !== false;
464
+ const perDeviceRate = dev.refreshRate ? Math.max(Number(dev.refreshRate), 30) : null;
465
+ return id && enabled && !perDeviceRate;
466
+ });
467
+ // Optional jitter before first batch
468
+ const startBatch = () => {
469
+ this.openApiBatchTimer = setInterval(async () => {
470
+ if (!canPoll()) {
471
+ return;
472
+ }
473
+ const client = this.config._client;
474
+ if (!client || typeof client.getDevice !== 'function') {
475
+ return;
476
+ }
477
+ // Limit concurrency
478
+ const chunks = [];
479
+ for (let i = 0; i < batchDevices.length; i += batchConcurrency) {
480
+ chunks.push(batchDevices.slice(i, i + batchConcurrency));
481
+ }
482
+ for (const chunk of chunks) {
483
+ await Promise.all(chunk.map(async (dev) => {
484
+ try {
485
+ await client.getDevice(dev.deviceId ?? dev.id);
486
+ this.openApiRequestsToday++;
487
+ this.log.debug(`[OpenAPI] Batched poll device ${dev.deviceId ?? dev.id} [${this.openApiRequestsToday}/${dailyLimit}]`);
488
+ }
489
+ catch (e) {
490
+ this.log.debug(`[OpenAPI] Batched polling failed for device ${dev.deviceId ?? dev.id}:`, e?.message);
491
+ }
492
+ }));
493
+ }
494
+ }, batchRate * 1000);
495
+ };
496
+ if (batchJitter > 0) {
497
+ setTimeout(startBatch, Math.floor(Math.random() * batchJitter * 1000));
498
+ }
499
+ else {
500
+ startBatch();
501
+ }
502
+ }
503
+ }
504
+ /**
505
+ * Called by Homebridge to restore cached Matter accessories on startup.
506
+ * @param {any} accessory - The cached accessory object
507
+ * @returns {Promise<void>} Resolves when accessory is restored
508
+ */
509
+ async configureAccessory(accessory) {
510
+ try {
511
+ const uuid = accessory.UUID || accessory.UUID;
512
+ this.accessories.set(uuid, accessory);
513
+ this.log.info(`Restored cached Matter accessory ${accessory.displayName || uuid}`);
514
+ }
515
+ catch (e) {
516
+ this.log.warn('configureAccessory failed to restore Matter accessory', e);
517
+ }
518
+ }
519
+ /**
520
+ * Called by Homebridge to restore cached Matter accessories (alternate signature).
521
+ * @param {any} accessory - The cached accessory object
522
+ * @returns {void}
523
+ */
524
+ configureMatterAccessory(accessory) {
525
+ try {
526
+ const uuid = accessory.uuid || accessory.UUID || accessory.uuid;
527
+ this.accessories.set(uuid, accessory);
528
+ this.log.info(`Restored cached Matter accessory ${accessory.displayName || uuid}`);
529
+ }
530
+ catch (e) {
531
+ this.log.warn('configureMatterAccessory failed to restore Matter accessory', e);
532
+ }
533
+ }
534
+ }
535
+ export default SwitchBotMatterPlatform;
536
+ //# sourceMappingURL=SwitchBotMatterPlatform.js.map