@switchbot/homebridge-switchbot 5.0.0-beta.6 → 5.0.0-beta.60

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 (267) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/README.md +45 -3
  3. package/config.schema.json +866 -13754
  4. package/dist/devices-hap/airpurifier.d.ts.map +1 -1
  5. package/dist/devices-hap/airpurifier.js +12 -6
  6. package/dist/devices-hap/airpurifier.js.map +1 -1
  7. package/dist/devices-hap/blindtilt.js +3 -3
  8. package/dist/devices-hap/bot.d.ts.map +1 -1
  9. package/dist/devices-hap/bot.js +16 -5
  10. package/dist/devices-hap/bot.js.map +1 -1
  11. package/dist/devices-hap/ceilinglight.d.ts.map +1 -1
  12. package/dist/devices-hap/ceilinglight.js +13 -7
  13. package/dist/devices-hap/ceilinglight.js.map +1 -1
  14. package/dist/devices-hap/colorbulb.d.ts.map +1 -1
  15. package/dist/devices-hap/colorbulb.js +49 -9
  16. package/dist/devices-hap/colorbulb.js.map +1 -1
  17. package/dist/devices-hap/contact.js +3 -3
  18. package/dist/devices-hap/curtain.js +2 -2
  19. package/dist/devices-hap/curtain.js.map +1 -1
  20. package/dist/devices-hap/device.d.ts +18 -8
  21. package/dist/devices-hap/device.d.ts.map +1 -1
  22. package/dist/devices-hap/device.js +141 -69
  23. package/dist/devices-hap/device.js.map +1 -1
  24. package/dist/devices-hap/fan.d.ts.map +1 -1
  25. package/dist/devices-hap/fan.js +12 -6
  26. package/dist/devices-hap/fan.js.map +1 -1
  27. package/dist/devices-hap/hub.d.ts.map +1 -1
  28. package/dist/devices-hap/hub.js +6 -5
  29. package/dist/devices-hap/hub.js.map +1 -1
  30. package/dist/devices-hap/humidifier.d.ts +5 -0
  31. package/dist/devices-hap/humidifier.d.ts.map +1 -1
  32. package/dist/devices-hap/humidifier.js +92 -4
  33. package/dist/devices-hap/humidifier.js.map +1 -1
  34. package/dist/devices-hap/iosensor.d.ts.map +1 -1
  35. package/dist/devices-hap/iosensor.js +36 -21
  36. package/dist/devices-hap/iosensor.js.map +1 -1
  37. package/dist/devices-hap/lightstrip.d.ts.map +1 -1
  38. package/dist/devices-hap/lightstrip.js +38 -8
  39. package/dist/devices-hap/lightstrip.js.map +1 -1
  40. package/dist/devices-hap/lock.d.ts.map +1 -1
  41. package/dist/devices-hap/lock.js +14 -6
  42. package/dist/devices-hap/lock.js.map +1 -1
  43. package/dist/devices-hap/meter.d.ts.map +1 -1
  44. package/dist/devices-hap/meter.js +6 -5
  45. package/dist/devices-hap/meter.js.map +1 -1
  46. package/dist/devices-hap/meterplus.d.ts.map +1 -1
  47. package/dist/devices-hap/meterplus.js +6 -5
  48. package/dist/devices-hap/meterplus.js.map +1 -1
  49. package/dist/devices-hap/meterpro.d.ts.map +1 -1
  50. package/dist/devices-hap/meterpro.js +7 -6
  51. package/dist/devices-hap/meterpro.js.map +1 -1
  52. package/dist/devices-hap/motion.js +3 -3
  53. package/dist/devices-hap/plug.d.ts.map +1 -1
  54. package/dist/devices-hap/plug.js +11 -6
  55. package/dist/devices-hap/plug.js.map +1 -1
  56. package/dist/devices-hap/relayswitch.js +3 -3
  57. package/dist/devices-hap/robotvacuumcleaner.d.ts.map +1 -1
  58. package/dist/devices-hap/robotvacuumcleaner.js +13 -6
  59. package/dist/devices-hap/robotvacuumcleaner.js.map +1 -1
  60. package/dist/devices-hap/waterdetector.js +3 -3
  61. package/dist/devices-matter/BaseMatterAccessory.d.ts +27 -0
  62. package/dist/devices-matter/BaseMatterAccessory.d.ts.map +1 -1
  63. package/dist/devices-matter/BaseMatterAccessory.js +169 -5
  64. package/dist/devices-matter/BaseMatterAccessory.js.map +1 -1
  65. package/dist/devices-matter/ColorLightAccessory.d.ts.map +1 -1
  66. package/dist/devices-matter/ColorLightAccessory.js +12 -12
  67. package/dist/devices-matter/ColorLightAccessory.js.map +1 -1
  68. package/dist/devices-matter/ColorTemperatureLightAccessory.d.ts.map +1 -1
  69. package/dist/devices-matter/ColorTemperatureLightAccessory.js +5 -7
  70. package/dist/devices-matter/ColorTemperatureLightAccessory.js.map +1 -1
  71. package/dist/devices-matter/DimmableLightAccessory.js +9 -9
  72. package/dist/devices-matter/DimmableLightAccessory.js.map +1 -1
  73. package/dist/devices-matter/ExtendedColorLightAccessory.d.ts.map +1 -1
  74. package/dist/devices-matter/ExtendedColorLightAccessory.js +14 -15
  75. package/dist/devices-matter/ExtendedColorLightAccessory.js.map +1 -1
  76. package/dist/devices-matter/OnOffLightAccessory.d.ts.map +1 -1
  77. package/dist/devices-matter/OnOffLightAccessory.js +8 -16
  78. package/dist/devices-matter/OnOffLightAccessory.js.map +1 -1
  79. package/dist/devices-matter/OnOffOutletAccessory.d.ts +2 -0
  80. package/dist/devices-matter/OnOffOutletAccessory.d.ts.map +1 -1
  81. package/dist/devices-matter/OnOffOutletAccessory.js +10 -7
  82. package/dist/devices-matter/OnOffOutletAccessory.js.map +1 -1
  83. package/dist/devices-matter/OnOffSwitchAccessory.js +2 -2
  84. package/dist/devices-matter/OnOffSwitchAccessory.js.map +1 -1
  85. package/dist/devices-matter/RoboticVacuumAccessory.d.ts +29 -43
  86. package/dist/devices-matter/RoboticVacuumAccessory.d.ts.map +1 -1
  87. package/dist/devices-matter/RoboticVacuumAccessory.js +287 -262
  88. package/dist/devices-matter/RoboticVacuumAccessory.js.map +1 -1
  89. package/dist/homebridge-ui/public/index.html +200 -18
  90. package/dist/homebridge-ui/server.js +82 -9
  91. package/dist/homebridge-ui/server.js.map +1 -1
  92. package/dist/index.d.ts.map +1 -1
  93. package/dist/index.js +4 -7
  94. package/dist/index.js.map +1 -1
  95. package/dist/irdevice/irdevice.d.ts +11 -10
  96. package/dist/irdevice/irdevice.d.ts.map +1 -1
  97. package/dist/irdevice/irdevice.js +76 -35
  98. package/dist/irdevice/irdevice.js.map +1 -1
  99. package/dist/platform-hap.d.ts +26 -15
  100. package/dist/platform-hap.d.ts.map +1 -1
  101. package/dist/platform-hap.js +333 -153
  102. package/dist/platform-hap.js.map +1 -1
  103. package/dist/platform-matter.d.ts +93 -6
  104. package/dist/platform-matter.d.ts.map +1 -1
  105. package/dist/platform-matter.js +1822 -224
  106. package/dist/platform-matter.js.map +1 -1
  107. package/dist/settings.d.ts +58 -7
  108. package/dist/settings.d.ts.map +1 -1
  109. package/dist/settings.js.map +1 -1
  110. package/dist/test/apiRequestTracker.test.d.ts +2 -0
  111. package/dist/test/apiRequestTracker.test.d.ts.map +1 -0
  112. package/dist/test/apiRequestTracker.test.js +392 -0
  113. package/dist/test/apiRequestTracker.test.js.map +1 -0
  114. package/dist/test/hap/device-webhook-context.test.d.ts +2 -0
  115. package/dist/test/hap/device-webhook-context.test.d.ts.map +1 -0
  116. package/dist/test/hap/device-webhook-context.test.js +128 -0
  117. package/dist/test/hap/device-webhook-context.test.js.map +1 -0
  118. package/dist/test/hap/platform-hap.logging.test.d.ts +2 -0
  119. package/dist/test/hap/platform-hap.logging.test.d.ts.map +1 -0
  120. package/dist/test/hap/platform-hap.logging.test.js +33 -0
  121. package/dist/test/hap/platform-hap.logging.test.js.map +1 -0
  122. package/dist/test/hap/platform-hap.test.d.ts +2 -0
  123. package/dist/test/hap/platform-hap.test.d.ts.map +1 -0
  124. package/dist/test/hap/platform-hap.test.js +62 -0
  125. package/dist/test/hap/platform-hap.test.js.map +1 -0
  126. package/dist/test/helpers/platform-fixtures.d.ts +9 -0
  127. package/dist/test/helpers/platform-fixtures.d.ts.map +1 -0
  128. package/dist/test/helpers/platform-fixtures.js +30 -0
  129. package/dist/test/helpers/platform-fixtures.js.map +1 -0
  130. package/dist/test/homebridge-ui/server.test.d.ts +2 -0
  131. package/dist/test/homebridge-ui/server.test.d.ts.map +1 -0
  132. package/dist/test/homebridge-ui/server.test.js +445 -0
  133. package/dist/test/homebridge-ui/server.test.js.map +1 -0
  134. package/dist/{index.test.d.ts.map → test/index.test.d.ts.map} +1 -1
  135. package/dist/test/index.test.js +19 -0
  136. package/dist/test/index.test.js.map +1 -0
  137. package/dist/test/matter/devices-matter/baseMatterAccessory.test.d.ts +2 -0
  138. package/dist/test/matter/devices-matter/baseMatterAccessory.test.d.ts.map +1 -0
  139. package/dist/test/matter/devices-matter/baseMatterAccessory.test.js +71 -0
  140. package/dist/test/matter/devices-matter/baseMatterAccessory.test.js.map +1 -0
  141. package/dist/test/matter/devices-matter/roboticVacuumAccessory.test.d.ts +2 -0
  142. package/dist/test/matter/devices-matter/roboticVacuumAccessory.test.d.ts.map +1 -0
  143. package/dist/test/matter/devices-matter/roboticVacuumAccessory.test.js +366 -0
  144. package/dist/test/matter/devices-matter/roboticVacuumAccessory.test.js.map +1 -0
  145. package/dist/test/matter/platform-matter.additional.test.d.ts +2 -0
  146. package/dist/test/matter/platform-matter.additional.test.d.ts.map +1 -0
  147. package/dist/test/matter/platform-matter.additional.test.js +35 -0
  148. package/dist/test/matter/platform-matter.additional.test.js.map +1 -0
  149. package/dist/test/matter/platform-matter.bleparse.test.d.ts +2 -0
  150. package/dist/test/matter/platform-matter.bleparse.test.d.ts.map +1 -0
  151. package/dist/test/matter/platform-matter.bleparse.test.js +43 -0
  152. package/dist/test/matter/platform-matter.bleparse.test.js.map +1 -0
  153. package/dist/test/matter/platform-matter.cleanup.test.d.ts +2 -0
  154. package/dist/test/matter/platform-matter.cleanup.test.d.ts.map +1 -0
  155. package/dist/test/matter/platform-matter.cleanup.test.js +70 -0
  156. package/dist/test/matter/platform-matter.cleanup.test.js.map +1 -0
  157. package/dist/test/matter/platform-matter.keepstale.test.d.ts +2 -0
  158. package/dist/test/matter/platform-matter.keepstale.test.d.ts.map +1 -0
  159. package/dist/test/matter/platform-matter.keepstale.test.js +27 -0
  160. package/dist/test/matter/platform-matter.keepstale.test.js.map +1 -0
  161. package/dist/test/matter/platform-matter.logging.test.d.ts +2 -0
  162. package/dist/test/matter/platform-matter.logging.test.d.ts.map +1 -0
  163. package/dist/test/matter/platform-matter.logging.test.js +29 -0
  164. package/dist/test/matter/platform-matter.logging.test.js.map +1 -0
  165. package/dist/test/matter/platform-matter.mapping.test.d.ts +2 -0
  166. package/dist/test/matter/platform-matter.mapping.test.d.ts.map +1 -0
  167. package/dist/test/matter/platform-matter.mapping.test.js +43 -0
  168. package/dist/test/matter/platform-matter.mapping.test.js.map +1 -0
  169. package/dist/test/matter/platform-matter.openapi-mapping.test.d.ts +2 -0
  170. package/dist/test/matter/platform-matter.openapi-mapping.test.d.ts.map +1 -0
  171. package/dist/test/matter/platform-matter.openapi-mapping.test.js +84 -0
  172. package/dist/test/matter/platform-matter.openapi-mapping.test.js.map +1 -0
  173. package/dist/test/matter/platform-matter.test.d.ts +2 -0
  174. package/dist/test/matter/platform-matter.test.d.ts.map +1 -0
  175. package/dist/test/matter/platform-matter.test.js +117 -0
  176. package/dist/test/matter/platform-matter.test.js.map +1 -0
  177. package/dist/test/matter/platform-matter.unregister.test.d.ts +2 -0
  178. package/dist/test/matter/platform-matter.unregister.test.d.ts.map +1 -0
  179. package/dist/test/matter/platform-matter.unregister.test.js +30 -0
  180. package/dist/test/matter/platform-matter.unregister.test.js.map +1 -0
  181. package/dist/test/matter/platform-matter.webhook.test.d.ts +2 -0
  182. package/dist/test/matter/platform-matter.webhook.test.d.ts.map +1 -0
  183. package/dist/test/matter/platform-matter.webhook.test.js +46 -0
  184. package/dist/test/matter/platform-matter.webhook.test.js.map +1 -0
  185. package/dist/test/utils.test.d.ts +2 -0
  186. package/dist/test/utils.test.d.ts.map +1 -0
  187. package/dist/test/utils.test.js +95 -0
  188. package/dist/test/utils.test.js.map +1 -0
  189. package/dist/test/verifyconfig.test.d.ts.map +1 -0
  190. package/dist/{verifyconfig.test.js → test/verifyconfig.test.js} +2 -2
  191. package/dist/test/verifyconfig.test.js.map +1 -0
  192. package/dist/utils.d.ts +204 -3
  193. package/dist/utils.d.ts.map +1 -1
  194. package/dist/utils.js +713 -33
  195. package/dist/utils.js.map +1 -1
  196. package/docs/assets/highlight.css +14 -0
  197. package/docs/assets/main.js +2 -2
  198. package/docs/index.html +31 -2
  199. package/docs/variables/default.html +1 -1
  200. package/package.json +15 -15
  201. package/src/devices-hap/airpurifier.ts +11 -6
  202. package/src/devices-hap/blindtilt.ts +3 -3
  203. package/src/devices-hap/bot.ts +15 -5
  204. package/src/devices-hap/ceilinglight.ts +12 -7
  205. package/src/devices-hap/colorbulb.ts +46 -10
  206. package/src/devices-hap/contact.ts +3 -3
  207. package/src/devices-hap/curtain.ts +2 -2
  208. package/src/devices-hap/device.ts +149 -70
  209. package/src/devices-hap/fan.ts +11 -6
  210. package/src/devices-hap/hub.ts +6 -5
  211. package/src/devices-hap/humidifier.ts +97 -4
  212. package/src/devices-hap/iosensor.ts +36 -21
  213. package/src/devices-hap/lightstrip.ts +35 -8
  214. package/src/devices-hap/lock.ts +13 -6
  215. package/src/devices-hap/meter.ts +6 -5
  216. package/src/devices-hap/meterplus.ts +6 -5
  217. package/src/devices-hap/meterpro.ts +7 -6
  218. package/src/devices-hap/motion.ts +3 -3
  219. package/src/devices-hap/plug.ts +10 -6
  220. package/src/devices-hap/relayswitch.ts +3 -3
  221. package/src/devices-hap/robotvacuumcleaner.ts +12 -6
  222. package/src/devices-hap/waterdetector.ts +3 -3
  223. package/src/devices-matter/BaseMatterAccessory.ts +176 -5
  224. package/src/devices-matter/ColorLightAccessory.ts +12 -12
  225. package/src/devices-matter/ColorTemperatureLightAccessory.ts +5 -7
  226. package/src/devices-matter/DimmableLightAccessory.ts +9 -9
  227. package/src/devices-matter/ExtendedColorLightAccessory.ts +14 -15
  228. package/src/devices-matter/OnOffLightAccessory.ts +8 -16
  229. package/src/devices-matter/OnOffOutletAccessory.ts +12 -7
  230. package/src/devices-matter/OnOffSwitchAccessory.ts +2 -2
  231. package/src/devices-matter/RoboticVacuumAccessory.ts +340 -313
  232. package/src/homebridge-ui/public/index.html +200 -18
  233. package/src/homebridge-ui/server.ts +85 -9
  234. package/src/index.ts +4 -7
  235. package/src/irdevice/irdevice.ts +74 -35
  236. package/src/platform-hap.ts +365 -169
  237. package/src/platform-matter.ts +1872 -229
  238. package/src/settings.ts +62 -3
  239. package/src/test/apiRequestTracker.test.ts +417 -0
  240. package/src/test/hap/device-webhook-context.test.ts +136 -0
  241. package/src/test/hap/platform-hap.logging.test.ts +36 -0
  242. package/src/test/hap/platform-hap.test.ts +70 -0
  243. package/src/test/helpers/platform-fixtures.ts +33 -0
  244. package/src/test/homebridge-ui/server.test.ts +486 -0
  245. package/src/test/index.test.ts +24 -0
  246. package/src/test/matter/devices-matter/baseMatterAccessory.test.ts +88 -0
  247. package/src/test/matter/devices-matter/roboticVacuumAccessory.test.ts +453 -0
  248. package/src/test/matter/platform-matter.additional.test.ts +44 -0
  249. package/src/test/matter/platform-matter.bleparse.test.ts +47 -0
  250. package/src/test/matter/platform-matter.cleanup.test.ts +86 -0
  251. package/src/test/matter/platform-matter.keepstale.test.ts +37 -0
  252. package/src/test/matter/platform-matter.logging.test.ts +33 -0
  253. package/src/test/matter/platform-matter.mapping.test.ts +57 -0
  254. package/src/test/matter/platform-matter.openapi-mapping.test.ts +109 -0
  255. package/src/test/matter/platform-matter.test.ts +144 -0
  256. package/src/test/matter/platform-matter.unregister.test.ts +39 -0
  257. package/src/test/matter/platform-matter.webhook.test.ts +54 -0
  258. package/src/test/utils.test.ts +96 -0
  259. package/src/{verifyconfig.test.ts → test/verifyconfig.test.ts} +12 -11
  260. package/src/utils.ts +777 -36
  261. package/dist/index.test.js +0 -14
  262. package/dist/index.test.js.map +0 -1
  263. package/dist/verifyconfig.test.d.ts.map +0 -1
  264. package/dist/verifyconfig.test.js.map +0 -1
  265. package/src/index.test.ts +0 -19
  266. /package/dist/{index.test.d.ts → test/index.test.d.ts} +0 -0
  267. /package/dist/{verifyconfig.test.d.ts → test/verifyconfig.test.d.ts} +0 -0
@@ -28,7 +28,8 @@
28
28
  <div id="menuWrapper" class="btn-group w-100 mb-0" role="group" aria-label="UI Menu" style="display: none">
29
29
  <button type="button" class="btn btn-primary" id="menuSettings">Settings</button>
30
30
  <button type="button" class="btn btn-primary" id="menuDevices">Devices</button>
31
- <button type="button" class="btn btn-primary mr-0" id="menuHome">Support</button>
31
+ <button type="button" class="btn btn-primary" id="menuHome">Support</button>
32
+ <button type="button" class="btn btn-primary mr-0" id="dynamic-config-tab">Dynamic Config</button>
32
33
  </div>
33
34
  <div id="disabledBanner" class="alert alert-secondary mb-0 mt-3" role="alert" style="display: none">
34
35
  Plugin is currently disabled
@@ -43,7 +44,7 @@
43
44
  </form>
44
45
  <table class="table w-100" id="deviceTable" style="display: none">
45
46
  <thead>
46
- <tr class="table-active">
47
+ <tr>
47
48
  <th scope="col" style="width: 40%">Device Name</th>
48
49
  <th scope="col" style="width: 60%" id="displayName"></th>
49
50
  </tr>
@@ -69,6 +70,18 @@
69
70
  <th scope="row">Connection Type</th>
70
71
  <td id="connectionType"></td>
71
72
  </tr>
73
+ <tr>
74
+ <th scope="row">Refresh Rate</th>
75
+ <td id="refreshRate"></td>
76
+ </tr>
77
+ <tr>
78
+ <th scope="row">Update Rate</th>
79
+ <td id="updateRate"></td>
80
+ </tr>
81
+ <tr>
82
+ <th scope="row">Push Rate</th>
83
+ <td id="pushRate"></td>
84
+ </tr>
72
85
  </tbody>
73
86
  </table>
74
87
  <p class="text-center">External accessories will not be displayed here.</p>
@@ -109,7 +122,7 @@
109
122
  <h5>Help/About</h5>
110
123
  <ul>
111
124
  <li>
112
- <a href="https://github.com/OpenWonderLabs/homebridge-switchbot/issues/new/choose" target="_blank">Support
125
+ <a href="https://
113
126
  Request</a>
114
127
  </li>
115
128
  <li>
@@ -118,10 +131,50 @@
118
131
  </li>
119
132
  </ul>
120
133
  </div>
134
+ <div id="dynamic-config-content" class="tab-content" style="display: none;">
135
+ <h2>Dynamic Config</h2>
136
+ <p>Discover and configure devices dynamically.</p>
137
+ <!-- Placeholder for dynamic content -->
138
+ </div>
121
139
  <script>
122
140
  (async () => {
123
141
  try {
124
142
  const currentConfig = await homebridge.getPluginConfig();
143
+
144
+ // Defensive wrapper: ensure token/secret aren't accidentally cleared by
145
+ // the UI/schema form. Some versions of config UI can omit sensitive
146
+ // fields from the submitted payload; when that happens we copy the
147
+ // existing values from `currentConfig` into the outgoing payload so
148
+ // saved config does not inadvertently remove credentials.
149
+ try {
150
+ if (typeof homebridge.updatePluginConfig === 'function') {
151
+ const _origUpdatePluginConfig = homebridge.updatePluginConfig.bind(homebridge)
152
+ homebridge.updatePluginConfig = async (cfg) => {
153
+ try {
154
+ if (Array.isArray(cfg) && cfg.length > 0 && Array.isArray(currentConfig) && currentConfig.length > 0) {
155
+ const incoming = cfg[0] || {}
156
+ const existing = currentConfig[0] || {}
157
+ incoming.credentials = incoming.credentials || {}
158
+ // Preserve token/secret when incoming payload leaves them blank/undefined
159
+ if ((incoming.credentials.token === undefined || String(incoming.credentials.token).trim() === '') && existing.credentials && existing.credentials.token) {
160
+ incoming.credentials.token = existing.credentials.token
161
+ }
162
+ if ((incoming.credentials.secret === undefined || String(incoming.credentials.secret).trim() === '') && existing.credentials && existing.credentials.secret) {
163
+ incoming.credentials.secret = existing.credentials.secret
164
+ }
165
+ cfg[0] = incoming
166
+ }
167
+ } catch (e) {
168
+ // Swallow any wrapper errors but log to console for debugging
169
+ // (do not expose secrets).
170
+ console.error('updatePluginConfig wrapper error', e)
171
+ }
172
+ return await _origUpdatePluginConfig(cfg)
173
+ }
174
+ }
175
+ } catch (e) {
176
+ console.error('Failed to attach updatePluginConfig wrapper', e)
177
+ }
125
178
  showIntro = () => {
126
179
  const introLink = document.getElementById('introLink');
127
180
  introLink.addEventListener('click', () => {
@@ -144,10 +197,25 @@
144
197
  document.getElementById('menuSettings').classList.add('btn-primary');
145
198
  document.getElementById('pageSupport').style.display = 'none';
146
199
  document.getElementById('pageDevices').style.display = 'block';
147
- const cachedAccessories =
200
+ let cachedAccessories =
148
201
  typeof homebridge.getCachedAccessories === 'function'
149
202
  ? await homebridge.getCachedAccessories()
150
203
  : await homebridge.request('/getCachedAccessories');
204
+
205
+ // If no HAP cached accessories were returned, try the Matter cached list using the new native method
206
+ if ((!cachedAccessories || cachedAccessories.length === 0)) {
207
+ try {
208
+ // Use the new native getCachedMatterAccessories() method from @homebridge/plugin-ui-utils v2.1.1-beta.0+
209
+ const matter = typeof homebridge.getCachedMatterAccessories === 'function'
210
+ ? await homebridge.getCachedMatterAccessories()
211
+ : await homebridge.request('/getCachedMatterAccessories'); // Fallback to custom handler for older UI versions
212
+ if (Array.isArray(matter) && matter.length > 0) {
213
+ cachedAccessories = matter
214
+ }
215
+ } catch (e) {
216
+ // ignore
217
+ }
218
+ }
151
219
  if (cachedAccessories.length > 0) {
152
220
  cachedAccessories.sort((a, b) => {
153
221
  return a.displayName.toLowerCase() > b.displayName.toLowerCase() ? 1 : b.displayName.toLowerCase() > a.displayName.toLowerCase() ? -1 : 0;
@@ -161,21 +229,127 @@
161
229
  option.value = a.UUID;
162
230
  deviceSelect.add(option);
163
231
  });
232
+ const validateConfig = (config) => {
233
+ const deviceIds = new Set();
234
+ const isValid = config[0].options.devices.every((device) => {
235
+ if (!device.deviceId || typeof device.deviceId !== 'string') {
236
+ homebridge.toast.error(`Invalid device ID: ${device.deviceId}`, 'Error');
237
+ return false;
238
+ }
239
+ if (deviceIds.has(device.deviceId)) {
240
+ homebridge.toast.error(`Duplicate device ID found: ${device.deviceId}`, 'Error');
241
+ return false;
242
+ }
243
+ deviceIds.add(device.deviceId);
244
+ return true;
245
+ });
246
+ return isValid;
247
+ };
248
+
249
+ const addDeviceToConfig = async (deviceId) => {
250
+ try {
251
+ homebridge.showSpinner();
252
+ const currentConfig = await homebridge.getPluginConfig();
253
+ if (!currentConfig[0].options.devices) {
254
+ currentConfig[0].options.devices = [];
255
+ }
256
+ currentConfig[0].options.devices.push({
257
+ deviceId,
258
+ hide_device: false,
259
+ disablePlatformBLE: false,
260
+ allowPush: false,
261
+ disableCaching: false,
262
+ });
263
+ if (!validateConfig(currentConfig)) {
264
+ homebridge.hideSpinner();
265
+ return;
266
+ }
267
+ await homebridge.updatePluginConfig(currentConfig);
268
+ await homebridge.savePluginConfig();
269
+ homebridge.toast.success('Device added to config successfully!', 'Success');
270
+ } catch (error) {
271
+ homebridge.toast.error('Failed to add device to config!', 'Error');
272
+ } finally {
273
+ homebridge.hideSpinner();
274
+ }
275
+ };
276
+
277
+ const addDeviceButton = document.createElement('button');
278
+ addDeviceButton.textContent = 'Add Device to Config';
279
+ addDeviceButton.className = 'btn btn-success';
280
+ addDeviceButton.style.marginTop = '10px';
281
+ addDeviceButton.addEventListener('click', () => {
282
+ const deviceId = document.getElementById('deviceId').textContent;
283
+ if (deviceId) {
284
+ addDeviceToConfig(deviceId);
285
+ } else {
286
+ homebridge.toast.error('No device ID found!', 'Error');
287
+ }
288
+ });
289
+ document.getElementById('deviceInfo').appendChild(addDeviceButton);
290
+
291
+ const protocolInfo = document.createElement('p');
292
+ protocolInfo.id = 'protocolInfo';
293
+ protocolInfo.style.marginTop = '10px';
294
+ document.getElementById('deviceInfo').appendChild(protocolInfo);
295
+
296
+ const updateTableWithDetails = (deviceContext) => {
297
+ const tableBody = document.querySelector('#deviceTable tbody');
298
+ tableBody.innerHTML = '';
299
+
300
+ Object.entries(deviceContext).forEach(([key, value]) => {
301
+ if (value !== undefined && value !== null && String(value).trim() !== '') {
302
+ const row = document.createElement('tr');
303
+ const keyCell = document.createElement('th');
304
+ keyCell.scope = 'row';
305
+ keyCell.textContent = key;
306
+ const valueCell = document.createElement('td');
307
+ valueCell.textContent = value;
308
+ row.appendChild(keyCell);
309
+ row.appendChild(valueCell);
310
+ tableBody.appendChild(row);
311
+ }
312
+ });
313
+ };
314
+
315
+ // Ensure the element exists before attempting to set its innerHTML
316
+ const safeSetInnerHTML = (id, value) => {
317
+ const element = document.getElementById(id);
318
+ if (element) {
319
+ element.innerHTML = value;
320
+ } else {
321
+ console.warn(`Element with id '${id}' not found.`);
322
+ }
323
+ };
324
+
164
325
  showDeviceInfo = async (UUID) => {
165
- homebridge.showSpinner();
166
- const thisAcc = cachedAccessories.find((x) => x.UUID === UUID);
167
- const context = thisAcc.context;
168
- document.getElementById('displayName').innerHTML = thisAcc.displayName;
169
- document.getElementById('deviceId').innerHTML = context.deviceId;
170
- document.getElementById('model').innerHTML = context.model;
171
- document.getElementById('version').innerHTML = context.version;
172
- document.getElementById('deviceType').innerHTML = context.deviceType;
173
- document.getElementById('connectionType').innerHTML = context.connectionType;
174
- document.getElementById('refreshRate').innerHTML = context.refreshRate;
175
- document.getElementById('updateRate').innerHTML = context.updateRate;
176
- document.getElementById('pushRate').innerHTML = context.pushRate;
177
- document.getElementById('deviceTable').style.display = 'inline-table';
178
- homebridge.hideSpinner();
326
+ try {
327
+ homebridge.showSpinner();
328
+ const thisAcc = cachedAccessories.find((x) => x.UUID === UUID);
329
+ if (!thisAcc) {
330
+ throw new Error('Device not found');
331
+ }
332
+ const context = thisAcc.context;
333
+ const protocol = thisAcc.isMatter ? 'Matter Protocol' : 'HAP Protocol';
334
+ safeSetInnerHTML('displayName', thisAcc.displayName);
335
+ safeSetInnerHTML('deviceId', context.deviceId);
336
+ safeSetInnerHTML('model', context.model);
337
+ safeSetInnerHTML('version', context.version);
338
+ safeSetInnerHTML('deviceType', context.deviceType);
339
+ safeSetInnerHTML('connectionType', context.connectionType);
340
+ safeSetInnerHTML('refreshRate', context.refreshRate);
341
+ safeSetInnerHTML('updateRate', context.updateRate);
342
+ safeSetInnerHTML('pushRate', context.pushRate);
343
+ safeSetInnerHTML('protocolInfo', protocol);
344
+ document.getElementById('deviceTable').style.display = 'inline-table';
345
+
346
+
347
+ updateTableWithDetails(context);
348
+ } catch (error) {
349
+ homebridge.toast.error(error.message, 'Error');
350
+ } finally {
351
+ homebridge.hideSpinner();
352
+ }
179
353
  };
180
354
  deviceSelect.addEventListener('change', (event) => showDeviceInfo(event.target.value));
181
355
  if (cachedAccessories.length > 0) {
@@ -246,4 +420,12 @@
246
420
  homebridge.hideSpinner();
247
421
  }
248
422
  })();
423
+
424
+ document.getElementById('dynamic-config-tab').addEventListener('click', function() {
425
+ // Hide other tabs
426
+ document.querySelectorAll('.tab-content').forEach(tab => tab.style.display = 'none');
427
+
428
+ // Show Dynamic Config tab
429
+ document.getElementById('dynamic-config-content').style.display = 'block';
430
+ });
249
431
  </script>
@@ -9,9 +9,12 @@ class PluginUiServer extends HomebridgePluginUiServer {
9
9
  A native method getCachedAccessories() was introduced in config-ui-x v4.37.0
10
10
  The following is for users who have a lower version of config-ui-x
11
11
  */
12
- this.onRequest('getCachedAccessories', () => {
12
+ const getCachedAccessoriesHandler = () => {
13
13
  try {
14
- const plugin = 'homebridge-switchbot'
14
+ // Some Homebridge versions store cached accessories with the scoped
15
+ // plugin name ("@switchbot/homebridge-switchbot"); others may use
16
+ // the unscoped id ("homebridge-switchbot"). Check both.
17
+ const pluginNames = ['@switchbot/homebridge-switchbot', 'homebridge-switchbot']
15
18
  const devicesToReturn = []
16
19
 
17
20
  // The path and file of the cached accessories
@@ -22,21 +25,94 @@ class PluginUiServer extends HomebridgePluginUiServer {
22
25
  // read the cached accessories file
23
26
  const cachedAccessories: any[] = JSON.parse(fs.readFileSync(accFile, 'utf8'))
24
27
 
25
- cachedAccessories.forEach((accessory: any) => {
26
- // Check the accessory is from this plugin
27
- if (accessory.plugin === plugin) {
28
- // Add the cached accessory to the array
29
- devicesToReturn.push(accessory.accessory as never)
28
+ cachedAccessories.forEach((entry: any) => {
29
+ // entry shape varies by UI version
30
+ const pluginName = entry.plugin || entry?.accessory?.plugin || entry?.accessory?.pluginName
31
+ const acc = entry.accessory ?? entry
32
+ if (pluginNames.includes(pluginName)) {
33
+ devicesToReturn.push(acc as never)
30
34
  }
31
35
  })
32
36
  }
33
37
  // Return the array
38
+ console.warn(`[Homebridge UI] getCachedAccessories returning ${devicesToReturn.length} device(s)`)
34
39
  return devicesToReturn
35
- } catch {
40
+ } catch (e: any) {
36
41
  // Just return an empty accessory list in case of any errors
42
+ console.error(`[Homebridge UI] getCachedAccessories error: ${e?.message ?? e}`)
37
43
  return []
38
44
  }
39
- })
45
+ }
46
+ this.onRequest('getCachedAccessories', getCachedAccessoriesHandler)
47
+ // Also register with a leading slash for compatibility with some UIs
48
+ this.onRequest('/getCachedAccessories', getCachedAccessoriesHandler)
49
+ // Provide Matter cached accessories if Homebridge stores them separately.
50
+ const getCachedMatterAccessoriesHandler = () => {
51
+ try {
52
+ const pluginNames = ['@switchbot/homebridge-switchbot', 'homebridge-switchbot']
53
+ const devicesToReturn: any[] = []
54
+
55
+ const accFile = `${this.homebridgeStoragePath}/accessories/cachedAccessories`
56
+ const matterFile = `${this.homebridgeStoragePath}/accessories/cachedMatterAccessories`
57
+
58
+ // Log all files in the accessories directory for debugging
59
+ try {
60
+ const accessoriesDir = `${this.homebridgeStoragePath}/accessories`
61
+ if (fs.existsSync(accessoriesDir)) {
62
+ const files = fs.readdirSync(accessoriesDir)
63
+ console.warn(`[Homebridge UI] Files in accessories directory: ${files.join(', ')}`)
64
+ }
65
+ } catch (e: any) {
66
+ console.error(`[Homebridge UI] Error listing accessories directory: ${e?.message ?? e}`)
67
+ }
68
+
69
+ console.warn(`[Homebridge UI] Checking for cached files:`)
70
+ console.warn(`[Homebridge UI] - cachedAccessories: ${fs.existsSync(accFile)}`)
71
+ console.warn(`[Homebridge UI] - cachedMatterAccessories: ${fs.existsSync(matterFile)}`)
72
+ console.warn(`[Homebridge UI] Matter cached accessories file exists: ${fs.existsSync(matterFile)}`)
73
+ if (fs.existsSync(matterFile)) {
74
+ const matterAccessories: any[] = JSON.parse(fs.readFileSync(matterFile, 'utf8'))
75
+ console.warn(`[Homebridge UI] Matter Cached Accessories:`, matterAccessories)
76
+ }
77
+
78
+ const readAndCollect = (filePath: string) => {
79
+ if (!fs.existsSync(filePath)) {
80
+ return
81
+ }
82
+ try {
83
+ const parsed: any[] = JSON.parse(fs.readFileSync(filePath, 'utf8'))
84
+ console.warn(`[Homebridge UI] - ${filePath}: found ${parsed.length} total entries`)
85
+ let matchCount = 0
86
+ parsed.forEach((entry: any) => {
87
+ // Entry shape varies between Homebridge versions; try common locations
88
+ const pluginName = entry.plugin || entry?.accessory?.plugin || entry?.accessory?.pluginName
89
+ const acc = entry.accessory ?? entry
90
+ if (pluginNames.includes(pluginName)) {
91
+ devicesToReturn.push(acc as never)
92
+ matchCount++
93
+ }
94
+ })
95
+ console.warn(`[Homebridge UI] - ${filePath}: matched ${matchCount} SwitchBot entries`)
96
+ } catch (e: any) {
97
+ // ignore parse errors for a single file
98
+ console.error(`[Homebridge UI] - ${filePath}: parse error - ${e?.message ?? e}`)
99
+ }
100
+ }
101
+
102
+ // Read both canonical files (some Homebridge versions use one or the other)
103
+ readAndCollect(accFile)
104
+ readAndCollect(matterFile)
105
+
106
+ console.warn(`[Homebridge UI] getCachedMatterAccessories returning ${devicesToReturn.length} device(s)`)
107
+ return devicesToReturn
108
+ } catch (e: any) {
109
+ console.error(`[Homebridge UI] getCachedMatterAccessories error: ${e?.message ?? e}`)
110
+ return []
111
+ }
112
+ }
113
+ this.onRequest('getCachedMatterAccessories', getCachedMatterAccessoriesHandler)
114
+ // Also register with a leading slash for compatibility with some UIs
115
+ this.onRequest('/getCachedMatterAccessories', getCachedMatterAccessoriesHandler)
40
116
  this.ready()
41
117
  }
42
118
  }
package/src/index.ts CHANGED
@@ -7,14 +7,11 @@ import type { API } from 'homebridge'
7
7
  import { SwitchBotHAPPlatform } from './platform-hap.js'
8
8
  import { SwitchBotMatterPlatform } from './platform-matter.js'
9
9
  import { PLATFORM_NAME, PLUGIN_NAME } from './settings.js'
10
+ import { createPlatformProxy } from './utils.js'
10
11
 
11
12
  // Register our platform with homebridge.
12
13
  export default (api: API): void => {
13
- // Call the instance method on the runtime `api` object (optional chaining in case
14
- // older homebridge doesn't provide it). Treat undefined as false.
15
- const isMatter = Boolean(api.isMatterEnabled?.())
16
- const log = (api as any).logger ?? console
17
- log.info?.(`Homebridge SwitchBot Plugin initializing in ${isMatter ? 'Matter' : 'HAP'} mode.`)
18
- // If Matter is enabled register the Matter platform, otherwise use HAP platform.
19
- api.registerPlatform(PLUGIN_NAME, PLATFORM_NAME, isMatter ? SwitchBotMatterPlatform : SwitchBotHAPPlatform)
14
+ // Create and register a small proxy that selects the correct platform (HAP or Matter) at runtime.
15
+ const ProxyCtor = createPlatformProxy(SwitchBotHAPPlatform, SwitchBotMatterPlatform)
16
+ api.registerPlatform(PLUGIN_NAME, PLATFORM_NAME, ProxyCtor as any)
20
17
  }
@@ -276,69 +276,108 @@ export abstract class irdeviceBase {
276
276
  /**
277
277
  * Logging for Device
278
278
  */
279
- async infoLog(...log: any[]): Promise<void> {
280
- if (await this.enablingDeviceLogging()) {
281
- this.log.info(`${this.device.remoteType}: ${this.accessory.displayName}`, String(...log))
279
+ infoLog(...log: any[]): void {
280
+ if (!this.enablingDeviceLogging()) {
281
+ return
282
282
  }
283
+ // Delegate to a single helper that prefers platform-provided loggers.
284
+ this.logWith('info', `${this.device.remoteType}: ${this.accessory.displayName}`, undefined, String(...log))
283
285
  }
284
286
 
285
- async successLog(...log: any[]): Promise<void> {
286
- if (await this.enablingDeviceLogging()) {
287
- this.log.success(`${this.device.remoteType}: ${this.accessory.displayName}`, String(...log))
287
+ successLog(...log: any[]): void {
288
+ if (!this.enablingDeviceLogging()) {
289
+ return
288
290
  }
291
+ this.logWith('success', `${this.device.remoteType}: ${this.accessory.displayName}`, undefined, String(...log))
289
292
  }
290
293
 
291
- async debugSuccessLog(...log: any[]): Promise<void> {
292
- if (await this.enablingDeviceLogging()) {
293
- if (await this.loggingIsDebug()) {
294
- this.log.success(`[DEBUG] ${this.device.remoteType}: ${this.accessory.displayName}`, String(...log))
295
- }
294
+ debugSuccessLog(...log: any[]): void {
295
+ if (!this.enablingDeviceLogging()) {
296
+ return
297
+ }
298
+ if (!this.loggingIsDebug()) {
299
+ return
296
300
  }
301
+ this.logWith('success', `[DEBUG] ${this.device.remoteType}: ${this.accessory.displayName}`, 'debugSuccessLog', String(...log))
297
302
  }
298
303
 
299
- async warnLog(...log: any[]): Promise<void> {
300
- if (await this.enablingDeviceLogging()) {
301
- this.log.warn(`${this.device.remoteType}: ${this.accessory.displayName}`, String(...log))
304
+ warnLog(...log: any[]): void {
305
+ if (!this.enablingDeviceLogging()) {
306
+ return
302
307
  }
308
+ this.logWith('warn', `${this.device.remoteType}: ${this.accessory.displayName}`, undefined, String(...log))
303
309
  }
304
310
 
305
- async debugWarnLog(...log: any[]): Promise<void> {
306
- if (await this.enablingDeviceLogging()) {
307
- if (await this.loggingIsDebug()) {
308
- this.log.warn(`[DEBUG] ${this.device.remoteType}: ${this.accessory.displayName}`, String(...log))
309
- }
311
+ debugWarnLog(...log: any[]): void {
312
+ if (!this.enablingDeviceLogging() || !this.loggingIsDebug()) {
313
+ return
310
314
  }
315
+ this.logWith('warn', `[DEBUG] ${this.device.remoteType}: ${this.accessory.displayName}`, 'debugWarnLog', String(...log))
311
316
  }
312
317
 
313
- async errorLog(...log: any[]): Promise<void> {
314
- if (await this.enablingDeviceLogging()) {
315
- this.log.error(`${this.device.remoteType}: ${this.accessory.displayName}`, String(...log))
318
+ errorLog(...log: any[]): void {
319
+ if (!this.enablingDeviceLogging()) {
320
+ return
316
321
  }
322
+ this.logWith('error', `${this.device.remoteType}: ${this.accessory.displayName}`, undefined, String(...log))
317
323
  }
318
324
 
319
- async debugErrorLog(...log: any[]): Promise<void> {
320
- if (await this.enablingDeviceLogging()) {
321
- if (await this.loggingIsDebug()) {
322
- this.log.error(`[DEBUG] ${this.device.remoteType}: ${this.accessory.displayName}`, String(...log))
323
- }
325
+ debugErrorLog(...log: any[]): void {
326
+ if (!this.enablingDeviceLogging() || !this.loggingIsDebug()) {
327
+ return
324
328
  }
329
+ this.logWith('error', `[DEBUG] ${this.device.remoteType}: ${this.accessory.displayName}`, 'debugErrorLog', String(...log))
325
330
  }
326
331
 
327
- async debugLog(...log: any[]): Promise<void> {
328
- if (await this.enablingDeviceLogging()) {
329
- if (this.deviceLogging === 'debug') {
330
- this.log.info(`[DEBUG] ${this.device.remoteType}: ${this.accessory.displayName}`, String(...log))
331
- } else if (this.deviceLogging === 'debugMode') {
332
- this.log.debug(`${this.device.remoteType}: ${this.accessory.displayName}`, String(...log))
332
+ debugLog(...log: any[]): void {
333
+ if (!this.enablingDeviceLogging()) {
334
+ return
335
+ }
336
+ if (this.deviceLogging === 'debug') {
337
+ this.logWith('debug', `[DEBUG] ${this.device.remoteType}: ${this.accessory.displayName}`, 'debugLog', String(...log))
338
+ } else if (this.deviceLogging === 'debugMode') {
339
+ this.logWith('debug', `${this.device.remoteType}: ${this.accessory.displayName}`, 'debugLog', String(...log))
340
+ }
341
+ }
342
+
343
+ // Generic platform/local logger delegate
344
+ protected logWith(level: string, message: string, platformMethodName?: string, payload?: string): void {
345
+ const method = platformMethodName ?? `${level}Log`
346
+ const pFn = (this.platform as any)?.[method]
347
+ if (typeof pFn === 'function') {
348
+ try {
349
+ if (payload !== undefined) {
350
+ pFn(message, payload)
351
+ } else {
352
+ pFn(message)
353
+ }
354
+ return
355
+ } catch (_err) {
356
+ // fallthrough to local logger
357
+ }
358
+ }
359
+ const map: Record<string, string> = {
360
+ info: 'info',
361
+ success: 'success',
362
+ debug: 'debug',
363
+ warn: 'warn',
364
+ error: 'error',
365
+ }
366
+ const local = (this.log as any)[map[level] ?? level]
367
+ if (typeof local === 'function') {
368
+ if (payload !== undefined) {
369
+ local.call(this.log, message, payload)
370
+ } else {
371
+ local.call(this.log, message)
333
372
  }
334
373
  }
335
374
  }
336
375
 
337
- async loggingIsDebug(): Promise<boolean> {
376
+ loggingIsDebug(): boolean {
338
377
  return this.deviceLogging === 'debugMode' || this.deviceLogging === 'debug'
339
378
  }
340
379
 
341
- async enablingDeviceLogging(): Promise<boolean> {
380
+ enablingDeviceLogging(): boolean {
342
381
  return this.deviceLogging === 'debugMode' || this.deviceLogging === 'debug' || this.deviceLogging === 'standard'
343
382
  }
344
383
  }