@switchbot/homebridge-switchbot 5.0.0-beta.4 → 5.0.0-beta.40

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 (162) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/README.md +23 -3
  3. package/config.schema.json +722 -13684
  4. package/dist/devices-hap/device.d.ts +18 -8
  5. package/dist/devices-hap/device.d.ts.map +1 -1
  6. package/dist/devices-hap/device.js +121 -68
  7. package/dist/devices-hap/device.js.map +1 -1
  8. package/dist/devices-matter/BaseMatterAccessory.d.ts +27 -0
  9. package/dist/devices-matter/BaseMatterAccessory.d.ts.map +1 -1
  10. package/dist/devices-matter/BaseMatterAccessory.js +169 -5
  11. package/dist/devices-matter/BaseMatterAccessory.js.map +1 -1
  12. package/dist/devices-matter/ColorLightAccessory.d.ts.map +1 -1
  13. package/dist/devices-matter/ColorLightAccessory.js +12 -12
  14. package/dist/devices-matter/ColorLightAccessory.js.map +1 -1
  15. package/dist/devices-matter/ColorTemperatureLightAccessory.d.ts.map +1 -1
  16. package/dist/devices-matter/ColorTemperatureLightAccessory.js +5 -7
  17. package/dist/devices-matter/ColorTemperatureLightAccessory.js.map +1 -1
  18. package/dist/devices-matter/DimmableLightAccessory.js +9 -9
  19. package/dist/devices-matter/DimmableLightAccessory.js.map +1 -1
  20. package/dist/devices-matter/ExtendedColorLightAccessory.d.ts.map +1 -1
  21. package/dist/devices-matter/ExtendedColorLightAccessory.js +14 -15
  22. package/dist/devices-matter/ExtendedColorLightAccessory.js.map +1 -1
  23. package/dist/devices-matter/OnOffLightAccessory.d.ts.map +1 -1
  24. package/dist/devices-matter/OnOffLightAccessory.js +8 -16
  25. package/dist/devices-matter/OnOffLightAccessory.js.map +1 -1
  26. package/dist/devices-matter/OnOffOutletAccessory.d.ts +2 -0
  27. package/dist/devices-matter/OnOffOutletAccessory.d.ts.map +1 -1
  28. package/dist/devices-matter/OnOffOutletAccessory.js +10 -7
  29. package/dist/devices-matter/OnOffOutletAccessory.js.map +1 -1
  30. package/dist/devices-matter/OnOffSwitchAccessory.js +2 -2
  31. package/dist/devices-matter/OnOffSwitchAccessory.js.map +1 -1
  32. package/dist/homebridge-ui/public/index.html +48 -1
  33. package/dist/homebridge-ui/server.js +53 -8
  34. package/dist/homebridge-ui/server.js.map +1 -1
  35. package/dist/index.d.ts.map +1 -1
  36. package/dist/index.js +4 -7
  37. package/dist/index.js.map +1 -1
  38. package/dist/irdevice/irdevice.d.ts +11 -10
  39. package/dist/irdevice/irdevice.d.ts.map +1 -1
  40. package/dist/irdevice/irdevice.js +76 -35
  41. package/dist/irdevice/irdevice.js.map +1 -1
  42. package/dist/platform-hap.d.ts +21 -15
  43. package/dist/platform-hap.d.ts.map +1 -1
  44. package/dist/platform-hap.js +246 -147
  45. package/dist/platform-hap.js.map +1 -1
  46. package/dist/platform-matter.d.ts +88 -6
  47. package/dist/platform-matter.d.ts.map +1 -1
  48. package/dist/platform-matter.js +1726 -243
  49. package/dist/platform-matter.js.map +1 -1
  50. package/dist/settings.d.ts +41 -6
  51. package/dist/settings.d.ts.map +1 -1
  52. package/dist/settings.js.map +1 -1
  53. package/dist/test/hap/platform-hap.logging.test.d.ts +2 -0
  54. package/dist/test/hap/platform-hap.logging.test.d.ts.map +1 -0
  55. package/dist/test/hap/platform-hap.logging.test.js +33 -0
  56. package/dist/test/hap/platform-hap.logging.test.js.map +1 -0
  57. package/dist/test/hap/platform-hap.test.d.ts +2 -0
  58. package/dist/test/hap/platform-hap.test.d.ts.map +1 -0
  59. package/dist/test/hap/platform-hap.test.js +62 -0
  60. package/dist/test/hap/platform-hap.test.js.map +1 -0
  61. package/dist/test/helpers/platform-fixtures.d.ts +9 -0
  62. package/dist/test/helpers/platform-fixtures.d.ts.map +1 -0
  63. package/dist/test/helpers/platform-fixtures.js +30 -0
  64. package/dist/test/helpers/platform-fixtures.js.map +1 -0
  65. package/dist/{index.test.d.ts.map → test/index.test.d.ts.map} +1 -1
  66. package/dist/test/index.test.js +19 -0
  67. package/dist/test/index.test.js.map +1 -0
  68. package/dist/test/matter/devices-matter/baseMatterAccessory.test.d.ts +2 -0
  69. package/dist/test/matter/devices-matter/baseMatterAccessory.test.d.ts.map +1 -0
  70. package/dist/test/matter/devices-matter/baseMatterAccessory.test.js +71 -0
  71. package/dist/test/matter/devices-matter/baseMatterAccessory.test.js.map +1 -0
  72. package/dist/test/matter/platform-matter.additional.test.d.ts +2 -0
  73. package/dist/test/matter/platform-matter.additional.test.d.ts.map +1 -0
  74. package/dist/test/matter/platform-matter.additional.test.js +35 -0
  75. package/dist/test/matter/platform-matter.additional.test.js.map +1 -0
  76. package/dist/test/matter/platform-matter.bleparse.test.d.ts +2 -0
  77. package/dist/test/matter/platform-matter.bleparse.test.d.ts.map +1 -0
  78. package/dist/test/matter/platform-matter.bleparse.test.js +43 -0
  79. package/dist/test/matter/platform-matter.bleparse.test.js.map +1 -0
  80. package/dist/test/matter/platform-matter.cleanup.test.d.ts +2 -0
  81. package/dist/test/matter/platform-matter.cleanup.test.d.ts.map +1 -0
  82. package/dist/test/matter/platform-matter.cleanup.test.js +70 -0
  83. package/dist/test/matter/platform-matter.cleanup.test.js.map +1 -0
  84. package/dist/test/matter/platform-matter.keepstale.test.d.ts +2 -0
  85. package/dist/test/matter/platform-matter.keepstale.test.d.ts.map +1 -0
  86. package/dist/test/matter/platform-matter.keepstale.test.js +27 -0
  87. package/dist/test/matter/platform-matter.keepstale.test.js.map +1 -0
  88. package/dist/test/matter/platform-matter.logging.test.d.ts +2 -0
  89. package/dist/test/matter/platform-matter.logging.test.d.ts.map +1 -0
  90. package/dist/test/matter/platform-matter.logging.test.js +29 -0
  91. package/dist/test/matter/platform-matter.logging.test.js.map +1 -0
  92. package/dist/test/matter/platform-matter.mapping.test.d.ts +2 -0
  93. package/dist/test/matter/platform-matter.mapping.test.d.ts.map +1 -0
  94. package/dist/test/matter/platform-matter.mapping.test.js +43 -0
  95. package/dist/test/matter/platform-matter.mapping.test.js.map +1 -0
  96. package/dist/test/matter/platform-matter.openapi-mapping.test.d.ts +2 -0
  97. package/dist/test/matter/platform-matter.openapi-mapping.test.d.ts.map +1 -0
  98. package/dist/test/matter/platform-matter.openapi-mapping.test.js +84 -0
  99. package/dist/test/matter/platform-matter.openapi-mapping.test.js.map +1 -0
  100. package/dist/test/matter/platform-matter.test.d.ts +2 -0
  101. package/dist/test/matter/platform-matter.test.d.ts.map +1 -0
  102. package/dist/test/matter/platform-matter.test.js +117 -0
  103. package/dist/test/matter/platform-matter.test.js.map +1 -0
  104. package/dist/test/matter/platform-matter.unregister.test.d.ts +2 -0
  105. package/dist/test/matter/platform-matter.unregister.test.d.ts.map +1 -0
  106. package/dist/test/matter/platform-matter.unregister.test.js +30 -0
  107. package/dist/test/matter/platform-matter.unregister.test.js.map +1 -0
  108. package/dist/test/utils.test.d.ts +2 -0
  109. package/dist/test/utils.test.d.ts.map +1 -0
  110. package/dist/test/utils.test.js +95 -0
  111. package/dist/test/utils.test.js.map +1 -0
  112. package/dist/test/verifyconfig.test.d.ts.map +1 -0
  113. package/dist/{verifyconfig.test.js → test/verifyconfig.test.js} +2 -2
  114. package/dist/test/verifyconfig.test.js.map +1 -0
  115. package/dist/utils.d.ts +196 -3
  116. package/dist/utils.d.ts.map +1 -1
  117. package/dist/utils.js +656 -30
  118. package/dist/utils.js.map +1 -1
  119. package/docs/assets/main.js +2 -2
  120. package/docs/index.html +20 -2
  121. package/docs/variables/default.html +1 -1
  122. package/package.json +14 -14
  123. package/src/devices-hap/device.ts +129 -69
  124. package/src/devices-matter/BaseMatterAccessory.ts +176 -5
  125. package/src/devices-matter/ColorLightAccessory.ts +12 -12
  126. package/src/devices-matter/ColorTemperatureLightAccessory.ts +5 -7
  127. package/src/devices-matter/DimmableLightAccessory.ts +9 -9
  128. package/src/devices-matter/ExtendedColorLightAccessory.ts +14 -15
  129. package/src/devices-matter/OnOffLightAccessory.ts +8 -16
  130. package/src/devices-matter/OnOffOutletAccessory.ts +12 -7
  131. package/src/devices-matter/OnOffSwitchAccessory.ts +2 -2
  132. package/src/homebridge-ui/public/index.html +48 -1
  133. package/src/homebridge-ui/server.ts +55 -8
  134. package/src/index.ts +4 -7
  135. package/src/irdevice/irdevice.ts +74 -35
  136. package/src/platform-hap.ts +270 -160
  137. package/src/platform-matter.ts +1768 -240
  138. package/src/settings.ts +45 -2
  139. package/src/test/hap/platform-hap.logging.test.ts +36 -0
  140. package/src/test/hap/platform-hap.test.ts +70 -0
  141. package/src/test/helpers/platform-fixtures.ts +33 -0
  142. package/src/test/index.test.ts +24 -0
  143. package/src/test/matter/devices-matter/baseMatterAccessory.test.ts +88 -0
  144. package/src/test/matter/platform-matter.additional.test.ts +44 -0
  145. package/src/test/matter/platform-matter.bleparse.test.ts +47 -0
  146. package/src/test/matter/platform-matter.cleanup.test.ts +86 -0
  147. package/src/test/matter/platform-matter.keepstale.test.ts +37 -0
  148. package/src/test/matter/platform-matter.logging.test.ts +33 -0
  149. package/src/test/matter/platform-matter.mapping.test.ts +57 -0
  150. package/src/test/matter/platform-matter.openapi-mapping.test.ts +109 -0
  151. package/src/test/matter/platform-matter.test.ts +144 -0
  152. package/src/test/matter/platform-matter.unregister.test.ts +39 -0
  153. package/src/test/utils.test.ts +96 -0
  154. package/src/{verifyconfig.test.ts → test/verifyconfig.test.ts} +12 -11
  155. package/src/utils.ts +714 -32
  156. package/dist/index.test.js +0 -14
  157. package/dist/index.test.js.map +0 -1
  158. package/dist/verifyconfig.test.d.ts.map +0 -1
  159. package/dist/verifyconfig.test.js.map +0 -1
  160. package/src/index.test.ts +0 -19
  161. /package/dist/{index.test.d.ts → test/index.test.d.ts} +0 -0
  162. /package/dist/{verifyconfig.test.d.ts → test/verifyconfig.test.d.ts} +0 -0
@@ -5,6 +5,7 @@
5
5
 
6
6
  import type { API, Logger, MatterRequests } from 'homebridge'
7
7
 
8
+ import { hs2rgb } from '../utils.js'
8
9
  import { BaseMatterAccessory } from './BaseMatterAccessory.js'
9
10
 
10
11
  export class ExtendedColorLightAccessory extends BaseMatterAccessory {
@@ -61,46 +62,44 @@ export class ExtendedColorLightAccessory extends BaseMatterAccessory {
61
62
 
62
63
  private async handleOn(): Promise<void> {
63
64
  this.logInfo('turning on.')
64
- // TODO: await myLightAPI.turnOn()
65
+ await this.sendOnCommand()
65
66
  }
66
67
 
67
68
  private async handleOff(): Promise<void> {
68
69
  this.logInfo('turning off.')
69
- // TODO: await myLightAPI.turnOff()
70
+ await this.sendOffCommand()
70
71
  }
71
72
 
72
73
  private async handleSetLevel(request: MatterRequests.MoveToLevel): Promise<void> {
73
74
  this.logInfo(`MoveToLevel request: ${JSON.stringify(request)}`)
74
75
  const { level } = request
75
76
  const brightnessPercent = Math.round((level / 254) * 100)
76
- this.logInfo(`setting brightness to ${brightnessPercent}%.`)
77
- // TODO: await myLightAPI.setBrightness(brightnessPercent)
77
+ await this.sendSetBrightness(brightnessPercent)
78
78
  }
79
79
 
80
80
  private async handleSetColor(request: MatterRequests.MoveToColor): Promise<void> {
81
81
  this.logInfo(`MoveToColor request: ${JSON.stringify(request)}`)
82
- const { colorX, colorY, transitionTime } = request
83
- const xFloat = (colorX / 65535).toFixed(4)
84
- const yFloat = (colorY / 65535).toFixed(4)
85
- this.logInfo(`setting xy color to (${xFloat}, ${yFloat}), ${transitionTime}ms.`)
86
- // TODO: await myLightAPI.setXY(xFloat, yFloat, transitionTime)
82
+ const { colorX, colorY } = request
83
+ const hueApprox = Math.round((colorX / 65535) * 360)
84
+ const satApprox = Math.round((colorY / 65535) * 100)
85
+ const [r, g, b] = hs2rgb(hueApprox, satApprox)
86
+ await this.sendSetColor(r, g, b)
87
87
  }
88
88
 
89
89
  private async handleSetHueSaturation(request: MatterRequests.MoveToHueAndSaturation): Promise<void> {
90
90
  this.logInfo(`MoveToHueAndSaturation request: ${JSON.stringify(request)}`)
91
- const { hue, saturation, transitionTime } = request
91
+ const { hue, saturation } = request
92
92
  const hueDegrees = Math.round((hue / 254) * 360)
93
93
  const saturationPercent = Math.round((saturation / 254) * 100)
94
- this.logInfo(`setting color to ${hueDegrees}°, ${saturationPercent}%, ${transitionTime}ms.`)
95
- // TODO: await myLightAPI.setColor(hueDegrees, saturationPercent, transitionTime)
94
+ const [r, g, b] = hs2rgb(hueDegrees, saturationPercent)
95
+ await this.sendSetColor(r, g, b)
96
96
  }
97
97
 
98
98
  private async handleSetColorTemperature(request: MatterRequests.MoveToColorTemperature): Promise<void> {
99
99
  this.logInfo(`MoveToColorTemperature request: ${JSON.stringify(request)}`)
100
- const { colorTemperatureMireds, transitionTime } = request
100
+ const { colorTemperatureMireds } = request
101
101
  const kelvin = Math.round(1000000 / colorTemperatureMireds)
102
- this.logInfo(`setting color temp to ${kelvin}k, ${transitionTime}ms.`)
103
- // TODO: await myLightAPI.setColorTemperature(kelvin, transitionTime)
102
+ await this.sendSetColorTemperature(kelvin)
104
103
  }
105
104
 
106
105
  public updateOnOffState(isOn: boolean): void {
@@ -27,12 +27,8 @@ export class OnOffLightAccessory extends BaseMatterAccessory {
27
27
  const clusters = opts?.clusters ?? { onOff: { onOff: true } }
28
28
  const handlers = opts?.handlers ?? {
29
29
  onOff: {
30
- on: async () => {
31
- log.debug(`${displayName} on handler invoked (default no-op).`)
32
- },
33
- off: async () => {
34
- log.debug(`${displayName} off handler invoked (default no-op).`)
35
- },
30
+ on: async () => this.handleOnCommand(),
31
+ off: async () => this.handleOffCommand(),
36
32
  },
37
33
  }
38
34
 
@@ -61,14 +57,10 @@ export class OnOffLightAccessory extends BaseMatterAccessory {
61
57
  this.logInfo('turning on.')
62
58
 
63
59
  try {
64
- // TODO: Control your physical device here
65
- // Examples:
66
- // await fetch('https://api.mydevice.com/light/on', { method: 'POST' })
67
- // await fetch('http://192.168.1.50/api/light/on')
68
- // mqttClient.publish('home/light/command', JSON.stringify({ state: 'ON' }))
69
- // await myLightAPI.turnOn(this.context.deviceId)
60
+ // Delegate to the platform-provided helper (OpenAPI/BLE) when available
61
+ await this.sendOnCommand()
70
62
 
71
- this.logInfo('physical device turned on.')
63
+ this.logInfo('physical device turned on (via platform helper).')
72
64
 
73
65
  // State automatically updated by Homebridge after handler completes
74
66
  } catch (error) {
@@ -84,10 +76,10 @@ export class OnOffLightAccessory extends BaseMatterAccessory {
84
76
  this.logInfo('turning off.')
85
77
 
86
78
  try {
87
- // TODO: Control your physical device here
88
- // await myLightAPI.turnOff(this.context.deviceId)
79
+ // Delegate to the platform-provided helper (OpenAPI/BLE) when available
80
+ await this.sendOffCommand()
89
81
 
90
- this.logInfo('physical device turned off.')
82
+ this.logInfo('physical device turned off (via platform helper).')
91
83
 
92
84
  // State automatically updated by Homebridge after handler completes
93
85
  } catch (error) {
@@ -13,13 +13,8 @@ export class OnOffOutletAccessory extends BaseMatterAccessory {
13
13
  const clusters = opts?.clusters ?? { onOff: { onOff: false } }
14
14
  const handlers = opts?.handlers ?? {
15
15
  onOff: {
16
- on: async () => {
17
- // default no-op; platform may inject handlers via opts.handlers
18
- log.debug(`${displayName} on handler invoked (default no-op).`)
19
- },
20
- off: async () => {
21
- log.debug(`${displayName} off handler invoked (default no-op).`)
22
- },
16
+ on: async () => this.handleOn(),
17
+ off: async () => this.handleOff(),
23
18
  },
24
19
  }
25
20
 
@@ -43,4 +38,14 @@ export class OnOffOutletAccessory extends BaseMatterAccessory {
43
38
  public updateOnOffState(isOn: boolean): void {
44
39
  this.updateState(this.api.matter.clusterNames.OnOff, { onOff: isOn })
45
40
  }
41
+
42
+ private async handleOn(): Promise<void> {
43
+ this.logInfo('turning on.')
44
+ await this.sendOnCommand()
45
+ }
46
+
47
+ private async handleOff(): Promise<void> {
48
+ this.logInfo('turning off.')
49
+ await this.sendOffCommand()
50
+ }
46
51
  }
@@ -37,12 +37,12 @@ export class OnOffSwitchAccessory extends BaseMatterAccessory {
37
37
 
38
38
  private async handleOn(): Promise<void> {
39
39
  this.logInfo('turning on.')
40
- // TODO: await mySwitchAPI.turnOn()
40
+ await this.sendOnCommand()
41
41
  }
42
42
 
43
43
  private async handleOff(): Promise<void> {
44
44
  this.logInfo('turning off.')
45
- // TODO: await mySwitchAPI.turnOff()
45
+ await this.sendOffCommand()
46
46
  }
47
47
 
48
48
  public updateOnOffState(isOn: boolean): void {
@@ -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;
@@ -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,11 +25,12 @@ 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
  }
@@ -36,7 +40,50 @@ class PluginUiServer extends HomebridgePluginUiServer {
36
40
  // Just return an empty accessory list in case of any errors
37
41
  return []
38
42
  }
39
- })
43
+ }
44
+ this.onRequest('getCachedAccessories', getCachedAccessoriesHandler)
45
+ // Also register with a leading slash for compatibility with some UIs
46
+ this.onRequest('/getCachedAccessories', getCachedAccessoriesHandler)
47
+ // Provide Matter cached accessories if Homebridge stores them separately.
48
+ const getCachedMatterAccessoriesHandler = () => {
49
+ try {
50
+ const pluginNames = ['@switchbot/homebridge-switchbot', 'homebridge-switchbot']
51
+ const devicesToReturn: any[] = []
52
+
53
+ const accFile = `${this.homebridgeStoragePath}/accessories/cachedAccessories`
54
+ const matterFile = `${this.homebridgeStoragePath}/accessories/cachedMatterAccessories`
55
+
56
+ const readAndCollect = (filePath: string) => {
57
+ if (!fs.existsSync(filePath)) {
58
+ return
59
+ }
60
+ try {
61
+ const parsed: any[] = JSON.parse(fs.readFileSync(filePath, 'utf8'))
62
+ parsed.forEach((entry: any) => {
63
+ // Entry shape varies between Homebridge versions; try common locations
64
+ const pluginName = entry.plugin || entry?.accessory?.plugin || entry?.accessory?.pluginName
65
+ const acc = entry.accessory ?? entry
66
+ if (pluginNames.includes(pluginName)) {
67
+ devicesToReturn.push(acc as never)
68
+ }
69
+ })
70
+ } catch {
71
+ // ignore parse errors for a single file
72
+ }
73
+ }
74
+
75
+ // Read both canonical files (some Homebridge versions use one or the other)
76
+ readAndCollect(accFile)
77
+ readAndCollect(matterFile)
78
+
79
+ return devicesToReturn
80
+ } catch {
81
+ return []
82
+ }
83
+ }
84
+ this.onRequest('getCachedMatterAccessories', getCachedMatterAccessoriesHandler)
85
+ // Also register with a leading slash for compatibility with some UIs
86
+ this.onRequest('/getCachedMatterAccessories', getCachedMatterAccessoriesHandler)
40
87
  this.ready()
41
88
  }
42
89
  }
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
  }