@switchbot/homebridge-switchbot 5.0.0-beta.26 → 5.0.0-beta.27

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.
@@ -122,6 +122,41 @@
122
122
  (async () => {
123
123
  try {
124
124
  const currentConfig = await homebridge.getPluginConfig();
125
+
126
+ // Defensive wrapper: ensure token/secret aren't accidentally cleared by
127
+ // the UI/schema form. Some versions of config UI can omit sensitive
128
+ // fields from the submitted payload; when that happens we copy the
129
+ // existing values from `currentConfig` into the outgoing payload so
130
+ // saved config does not inadvertently remove credentials.
131
+ try {
132
+ if (typeof homebridge.updatePluginConfig === 'function') {
133
+ const _origUpdatePluginConfig = homebridge.updatePluginConfig.bind(homebridge)
134
+ homebridge.updatePluginConfig = async (cfg) => {
135
+ try {
136
+ if (Array.isArray(cfg) && cfg.length > 0 && Array.isArray(currentConfig) && currentConfig.length > 0) {
137
+ const incoming = cfg[0] || {}
138
+ const existing = currentConfig[0] || {}
139
+ incoming.credentials = incoming.credentials || {}
140
+ // Preserve token/secret when incoming payload leaves them blank/undefined
141
+ if ((incoming.credentials.token === undefined || String(incoming.credentials.token).trim() === '') && existing.credentials && existing.credentials.token) {
142
+ incoming.credentials.token = existing.credentials.token
143
+ }
144
+ if ((incoming.credentials.secret === undefined || String(incoming.credentials.secret).trim() === '') && existing.credentials && existing.credentials.secret) {
145
+ incoming.credentials.secret = existing.credentials.secret
146
+ }
147
+ cfg[0] = incoming
148
+ }
149
+ } catch (e) {
150
+ // Swallow any wrapper errors but log to console for debugging
151
+ // (do not expose secrets).
152
+ console.error('updatePluginConfig wrapper error', e)
153
+ }
154
+ return await _origUpdatePluginConfig(cfg)
155
+ }
156
+ }
157
+ } catch (e) {
158
+ console.error('Failed to attach updatePluginConfig wrapper', e)
159
+ }
125
160
  showIntro = () => {
126
161
  const introLink = document.getElementById('introLink');
127
162
  introLink.addEventListener('click', () => {
@@ -144,10 +179,22 @@
144
179
  document.getElementById('menuSettings').classList.add('btn-primary');
145
180
  document.getElementById('pageSupport').style.display = 'none';
146
181
  document.getElementById('pageDevices').style.display = 'block';
147
- const cachedAccessories =
182
+ let cachedAccessories =
148
183
  typeof homebridge.getCachedAccessories === 'function'
149
184
  ? await homebridge.getCachedAccessories()
150
185
  : await homebridge.request('/getCachedAccessories');
186
+
187
+ // If no HAP cached accessories were returned, try the Matter cached list
188
+ if ((!cachedAccessories || cachedAccessories.length === 0) && typeof homebridge.request === 'function') {
189
+ try {
190
+ const matter = await homebridge.request('/getCachedMatterAccessories')
191
+ if (Array.isArray(matter) && matter.length > 0) {
192
+ cachedAccessories = matter
193
+ }
194
+ } catch (e) {
195
+ // ignore
196
+ }
197
+ }
151
198
  if (cachedAccessories.length > 0) {
152
199
  cachedAccessories.sort((a, b) => {
153
200
  return a.displayName.toLowerCase() > b.displayName.toLowerCase() ? 1 : b.displayName.toLowerCase() > a.displayName.toLowerCase() ? -1 : 0;
@@ -33,6 +33,41 @@ class PluginUiServer extends HomebridgePluginUiServer {
33
33
  return [];
34
34
  }
35
35
  });
36
+ // Provide Matter cached accessories if Homebridge stores them separately.
37
+ this.onRequest('getCachedMatterAccessories', () => {
38
+ try {
39
+ const plugin = 'homebridge-switchbot';
40
+ const devicesToReturn = [];
41
+ const accFile = `${this.homebridgeStoragePath}/accessories/cachedAccessories`;
42
+ const matterFile = `${this.homebridgeStoragePath}/accessories/cachedMatterAccessories`;
43
+ const readAndCollect = (filePath) => {
44
+ if (!fs.existsSync(filePath)) {
45
+ return;
46
+ }
47
+ try {
48
+ const parsed = JSON.parse(fs.readFileSync(filePath, 'utf8'));
49
+ parsed.forEach((entry) => {
50
+ // Entry shape varies between Homebridge versions; try common locations
51
+ const pluginName = entry.plugin || entry?.accessory?.plugin || entry?.accessory?.pluginName;
52
+ const acc = entry.accessory ?? entry;
53
+ if (pluginName === plugin) {
54
+ devicesToReturn.push(acc);
55
+ }
56
+ });
57
+ }
58
+ catch {
59
+ // ignore parse errors for a single file
60
+ }
61
+ };
62
+ // Read both canonical files (some Homebridge versions use one or the other)
63
+ readAndCollect(accFile);
64
+ readAndCollect(matterFile);
65
+ return devicesToReturn;
66
+ }
67
+ catch {
68
+ return [];
69
+ }
70
+ });
36
71
  this.ready();
37
72
  }
38
73
  }
@@ -1 +1 @@
1
- {"version":3,"file":"server.js","sourceRoot":"","sources":["../../src/homebridge-ui/server.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,SAAS,CAAA;AAExB,OAAO,EAAE,wBAAwB,EAAE,MAAM,6BAA6B,CAAA;AAEtE,MAAM,cAAe,SAAQ,wBAAwB;IACnD;QACE,KAAK,EAAE,CAAA;QACP;;;UAGE;QACF,IAAI,CAAC,SAAS,CAAC,sBAAsB,EAAE,GAAG,EAAE;YAC1C,IAAI,CAAC;gBACH,MAAM,MAAM,GAAG,sBAAsB,CAAA;gBACrC,MAAM,eAAe,GAAG,EAAE,CAAA;gBAE1B,8CAA8C;gBAC9C,MAAM,OAAO,GAAG,GAAG,IAAI,CAAC,qBAAqB,gCAAgC,CAAA;gBAE7E,wBAAwB;gBACxB,IAAI,EAAE,CAAC,UAAU,CAAC,OAAO,CAAC,EAAE,CAAC;oBAC3B,mCAAmC;oBACnC,MAAM,iBAAiB,GAAU,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,YAAY,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC,CAAA;oBAE7E,iBAAiB,CAAC,OAAO,CAAC,CAAC,SAAc,EAAE,EAAE;wBAC3C,0CAA0C;wBAC1C,IAAI,SAAS,CAAC,MAAM,KAAK,MAAM,EAAE,CAAC;4BAChC,wCAAwC;4BACxC,eAAe,CAAC,IAAI,CAAC,SAAS,CAAC,SAAkB,CAAC,CAAA;wBACpD,CAAC;oBACH,CAAC,CAAC,CAAA;gBACJ,CAAC;gBACD,mBAAmB;gBACnB,OAAO,eAAe,CAAA;YACxB,CAAC;YAAC,MAAM,CAAC;gBACP,4DAA4D;gBAC5D,OAAO,EAAE,CAA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OAAO,EAAE,CAAA;YACX,CAAC;QACH,CAAC,CAAC,CAAA;QACF,IAAI,CAAC,KAAK,EAAE,CAAA;IACd,CAAC;CACF;AAED,SAAS,mBAAmB;IAC1B,OAAO,IAAI,cAAc,EAAE,CAAA;AAC7B,CAAC;AAED,mBAAmB,EAAE,CAAA"}
@@ -1 +1 @@
1
- <!DOCTYPE html><html class="default" lang="en" data-base="../"><head><meta charset="utf-8"/><meta http-equiv="x-ua-compatible" content="IE=edge"/><title>default | @switchbot/homebridge-switchbot</title><meta name="description" content="Documentation for @switchbot/homebridge-switchbot"/><meta name="viewport" content="width=device-width, initial-scale=1"/><link rel="stylesheet" href="../assets/style.css"/><link rel="stylesheet" href="../assets/highlight.css"/><script defer src="../assets/main.js"></script><script async src="../assets/icons.js" id="tsd-icons-script"></script><script async src="../assets/search.js" id="tsd-search-script"></script><script async src="../assets/navigation.js" id="tsd-nav-script"></script></head><body><script>document.documentElement.dataset.theme = localStorage.getItem("tsd-theme") || "os";document.body.style.display="none";setTimeout(() => window.app?app.showPage():document.body.style.removeProperty("display"),500)</script><header class="tsd-page-toolbar"><div class="tsd-toolbar-contents container"><a href="../index.html" class="title">@switchbot/homebridge-switchbot</a><div id="tsd-toolbar-links"></div><button id="tsd-search-trigger" class="tsd-widget" aria-label="Search"><svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-search"></use></svg></button><dialog id="tsd-search" aria-label="Search"><input role="combobox" id="tsd-search-input" aria-controls="tsd-search-results" aria-autocomplete="list" aria-expanded="true" autocapitalize="off" autocomplete="off" placeholder="Search the docs" maxLength="100"/><ul role="listbox" id="tsd-search-results"></ul><div id="tsd-search-status" aria-live="polite" aria-atomic="true"><div>Preparing search index...</div></div></dialog><a href="#" class="tsd-widget menu" id="tsd-toolbar-menu-trigger" data-toggle="menu" aria-label="Menu"><svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-menu"></use></svg></a></div></header><div class="container container-main"><div class="col-content"><div class="tsd-page-title"><ul class="tsd-breadcrumb" aria-label="Breadcrumb"><li><a href="" aria-current="page">default</a></li></ul><h1>Variable default</h1></div><div class="tsd-signature"><span class="tsd-kind-variable">default</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-symbol">(</span><span class="tsd-kind-parameter">api</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">API</span><span class="tsd-signature-symbol">)</span> <span class="tsd-signature-symbol">=&gt;</span> <span class="tsd-signature-type">void</span></div><div class="tsd-type-declaration"><h4>Type Declaration</h4><ul class="tsd-parameters"><li class="tsd-parameter-signature"><ul class="tsd-signatures"><li class="tsd-signature" id="__type"><span class="tsd-signature-symbol">(</span><span class="tsd-kind-parameter">api</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">API</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">void</span></li><li class="tsd-description"><div class="tsd-parameters"><h4 class="tsd-parameters-title">Parameters</h4><ul class="tsd-parameter-list"><li><span><span class="tsd-kind-parameter">api</span>: <span class="tsd-signature-type">API</span></span></li></ul></div><h4 class="tsd-returns-title">Returns <span class="tsd-signature-type">void</span></h4></li></ul></li></ul></div><aside class="tsd-sources"><ul><li>Defined in <a href="https://github.com/OpenWonderLabs/homebridge-switchbot/blob/3bcf4440f13e715dc17d2efbf3a412326be4e1ff/src/index.ts#L13">index.ts:13</a></li></ul></aside></div><div class="col-sidebar"><div class="page-menu"><div class="tsd-navigation settings"><details class="tsd-accordion"><summary class="tsd-accordion-summary"><svg width="20" height="20" viewBox="0 0 24 24" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-chevronDown"></use></svg><h3>Settings</h3></summary><div class="tsd-accordion-details"><div class="tsd-filter-visibility"><span class="settings-label">Member Visibility</span><ul id="tsd-filter-options"><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-inherited" name="inherited" checked/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Inherited</span></label></li></ul></div><div class="tsd-theme-toggle"><label class="settings-label" for="tsd-theme">Theme</label><select id="tsd-theme"><option value="os">OS</option><option value="light">Light</option><option value="dark">Dark</option></select></div></div></details></div></div><div class="site-menu"><nav class="tsd-navigation"><a href="../modules.html">@switchbot/homebridge-switchbot</a><ul class="tsd-small-nested-navigation" id="tsd-nav-container"><li>Loading...</li></ul></nav></div></div></div><footer></footer><div class="overlay"></div></body></html>
1
+ <!DOCTYPE html><html class="default" lang="en" data-base="../"><head><meta charset="utf-8"/><meta http-equiv="x-ua-compatible" content="IE=edge"/><title>default | @switchbot/homebridge-switchbot</title><meta name="description" content="Documentation for @switchbot/homebridge-switchbot"/><meta name="viewport" content="width=device-width, initial-scale=1"/><link rel="stylesheet" href="../assets/style.css"/><link rel="stylesheet" href="../assets/highlight.css"/><script defer src="../assets/main.js"></script><script async src="../assets/icons.js" id="tsd-icons-script"></script><script async src="../assets/search.js" id="tsd-search-script"></script><script async src="../assets/navigation.js" id="tsd-nav-script"></script></head><body><script>document.documentElement.dataset.theme = localStorage.getItem("tsd-theme") || "os";document.body.style.display="none";setTimeout(() => window.app?app.showPage():document.body.style.removeProperty("display"),500)</script><header class="tsd-page-toolbar"><div class="tsd-toolbar-contents container"><a href="../index.html" class="title">@switchbot/homebridge-switchbot</a><div id="tsd-toolbar-links"></div><button id="tsd-search-trigger" class="tsd-widget" aria-label="Search"><svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-search"></use></svg></button><dialog id="tsd-search" aria-label="Search"><input role="combobox" id="tsd-search-input" aria-controls="tsd-search-results" aria-autocomplete="list" aria-expanded="true" autocapitalize="off" autocomplete="off" placeholder="Search the docs" maxLength="100"/><ul role="listbox" id="tsd-search-results"></ul><div id="tsd-search-status" aria-live="polite" aria-atomic="true"><div>Preparing search index...</div></div></dialog><a href="#" class="tsd-widget menu" id="tsd-toolbar-menu-trigger" data-toggle="menu" aria-label="Menu"><svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-menu"></use></svg></a></div></header><div class="container container-main"><div class="col-content"><div class="tsd-page-title"><ul class="tsd-breadcrumb" aria-label="Breadcrumb"><li><a href="" aria-current="page">default</a></li></ul><h1>Variable default</h1></div><div class="tsd-signature"><span class="tsd-kind-variable">default</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-symbol">(</span><span class="tsd-kind-parameter">api</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">API</span><span class="tsd-signature-symbol">)</span> <span class="tsd-signature-symbol">=&gt;</span> <span class="tsd-signature-type">void</span></div><div class="tsd-type-declaration"><h4>Type Declaration</h4><ul class="tsd-parameters"><li class="tsd-parameter-signature"><ul class="tsd-signatures"><li class="tsd-signature" id="__type"><span class="tsd-signature-symbol">(</span><span class="tsd-kind-parameter">api</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">API</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">void</span></li><li class="tsd-description"><div class="tsd-parameters"><h4 class="tsd-parameters-title">Parameters</h4><ul class="tsd-parameter-list"><li><span><span class="tsd-kind-parameter">api</span>: <span class="tsd-signature-type">API</span></span></li></ul></div><h4 class="tsd-returns-title">Returns <span class="tsd-signature-type">void</span></h4></li></ul></li></ul></div><aside class="tsd-sources"><ul><li>Defined in <a href="https://github.com/OpenWonderLabs/homebridge-switchbot/blob/e157f6ee6c491bf1bc8c8b0848d5a78d13ab298e/src/index.ts#L13">index.ts:13</a></li></ul></aside></div><div class="col-sidebar"><div class="page-menu"><div class="tsd-navigation settings"><details class="tsd-accordion"><summary class="tsd-accordion-summary"><svg width="20" height="20" viewBox="0 0 24 24" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-chevronDown"></use></svg><h3>Settings</h3></summary><div class="tsd-accordion-details"><div class="tsd-filter-visibility"><span class="settings-label">Member Visibility</span><ul id="tsd-filter-options"><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-inherited" name="inherited" checked/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Inherited</span></label></li></ul></div><div class="tsd-theme-toggle"><label class="settings-label" for="tsd-theme">Theme</label><select id="tsd-theme"><option value="os">OS</option><option value="light">Light</option><option value="dark">Dark</option></select></div></div></details></div></div><div class="site-menu"><nav class="tsd-navigation"><a href="../modules.html">@switchbot/homebridge-switchbot</a><ul class="tsd-small-nested-navigation" id="tsd-nav-container"><li>Loading...</li></ul></nav></div></div></div><footer></footer><div class="overlay"></div></body></html>
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@switchbot/homebridge-switchbot",
3
3
  "displayName": "SwitchBot",
4
4
  "type": "module",
5
- "version": "5.0.0-beta.26",
5
+ "version": "5.0.0-beta.27",
6
6
  "description": "The SwitchBot plugin allows you to access your SwitchBot device(s) from HomeKit.",
7
7
  "author": "SwitchBot <support@wondertechlabs.com> (https://github.com/SwitchBot)",
8
8
  "contributors": [
@@ -122,6 +122,41 @@
122
122
  (async () => {
123
123
  try {
124
124
  const currentConfig = await homebridge.getPluginConfig();
125
+
126
+ // Defensive wrapper: ensure token/secret aren't accidentally cleared by
127
+ // the UI/schema form. Some versions of config UI can omit sensitive
128
+ // fields from the submitted payload; when that happens we copy the
129
+ // existing values from `currentConfig` into the outgoing payload so
130
+ // saved config does not inadvertently remove credentials.
131
+ try {
132
+ if (typeof homebridge.updatePluginConfig === 'function') {
133
+ const _origUpdatePluginConfig = homebridge.updatePluginConfig.bind(homebridge)
134
+ homebridge.updatePluginConfig = async (cfg) => {
135
+ try {
136
+ if (Array.isArray(cfg) && cfg.length > 0 && Array.isArray(currentConfig) && currentConfig.length > 0) {
137
+ const incoming = cfg[0] || {}
138
+ const existing = currentConfig[0] || {}
139
+ incoming.credentials = incoming.credentials || {}
140
+ // Preserve token/secret when incoming payload leaves them blank/undefined
141
+ if ((incoming.credentials.token === undefined || String(incoming.credentials.token).trim() === '') && existing.credentials && existing.credentials.token) {
142
+ incoming.credentials.token = existing.credentials.token
143
+ }
144
+ if ((incoming.credentials.secret === undefined || String(incoming.credentials.secret).trim() === '') && existing.credentials && existing.credentials.secret) {
145
+ incoming.credentials.secret = existing.credentials.secret
146
+ }
147
+ cfg[0] = incoming
148
+ }
149
+ } catch (e) {
150
+ // Swallow any wrapper errors but log to console for debugging
151
+ // (do not expose secrets).
152
+ console.error('updatePluginConfig wrapper error', e)
153
+ }
154
+ return await _origUpdatePluginConfig(cfg)
155
+ }
156
+ }
157
+ } catch (e) {
158
+ console.error('Failed to attach updatePluginConfig wrapper', e)
159
+ }
125
160
  showIntro = () => {
126
161
  const introLink = document.getElementById('introLink');
127
162
  introLink.addEventListener('click', () => {
@@ -144,10 +179,22 @@
144
179
  document.getElementById('menuSettings').classList.add('btn-primary');
145
180
  document.getElementById('pageSupport').style.display = 'none';
146
181
  document.getElementById('pageDevices').style.display = 'block';
147
- const cachedAccessories =
182
+ let cachedAccessories =
148
183
  typeof homebridge.getCachedAccessories === 'function'
149
184
  ? await homebridge.getCachedAccessories()
150
185
  : await homebridge.request('/getCachedAccessories');
186
+
187
+ // If no HAP cached accessories were returned, try the Matter cached list
188
+ if ((!cachedAccessories || cachedAccessories.length === 0) && typeof homebridge.request === 'function') {
189
+ try {
190
+ const matter = await homebridge.request('/getCachedMatterAccessories')
191
+ if (Array.isArray(matter) && matter.length > 0) {
192
+ cachedAccessories = matter
193
+ }
194
+ } catch (e) {
195
+ // ignore
196
+ }
197
+ }
151
198
  if (cachedAccessories.length > 0) {
152
199
  cachedAccessories.sort((a, b) => {
153
200
  return a.displayName.toLowerCase() > b.displayName.toLowerCase() ? 1 : b.displayName.toLowerCase() > a.displayName.toLowerCase() ? -1 : 0;
@@ -37,6 +37,43 @@ class PluginUiServer extends HomebridgePluginUiServer {
37
37
  return []
38
38
  }
39
39
  })
40
+ // Provide Matter cached accessories if Homebridge stores them separately.
41
+ this.onRequest('getCachedMatterAccessories', () => {
42
+ try {
43
+ const plugin = 'homebridge-switchbot'
44
+ const devicesToReturn: any[] = []
45
+
46
+ const accFile = `${this.homebridgeStoragePath}/accessories/cachedAccessories`
47
+ const matterFile = `${this.homebridgeStoragePath}/accessories/cachedMatterAccessories`
48
+
49
+ const readAndCollect = (filePath: string) => {
50
+ if (!fs.existsSync(filePath)) {
51
+ return
52
+ }
53
+ try {
54
+ const parsed: any[] = JSON.parse(fs.readFileSync(filePath, 'utf8'))
55
+ parsed.forEach((entry: any) => {
56
+ // Entry shape varies between Homebridge versions; try common locations
57
+ const pluginName = entry.plugin || entry?.accessory?.plugin || entry?.accessory?.pluginName
58
+ const acc = entry.accessory ?? entry
59
+ if (pluginName === plugin) {
60
+ devicesToReturn.push(acc as never)
61
+ }
62
+ })
63
+ } catch {
64
+ // ignore parse errors for a single file
65
+ }
66
+ }
67
+
68
+ // Read both canonical files (some Homebridge versions use one or the other)
69
+ readAndCollect(accFile)
70
+ readAndCollect(matterFile)
71
+
72
+ return devicesToReturn
73
+ } catch {
74
+ return []
75
+ }
76
+ })
40
77
  this.ready()
41
78
  }
42
79
  }