@switchbot/homebridge-switchbot 5.0.0-beta.3 → 5.0.0-beta.30

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 (137) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/config.schema.json +42 -4
  3. package/dist/devices-hap/device.d.ts +1 -0
  4. package/dist/devices-hap/device.d.ts.map +1 -1
  5. package/dist/devices-hap/device.js +70 -30
  6. package/dist/devices-hap/device.js.map +1 -1
  7. package/dist/devices-matter/BaseMatterAccessory.d.ts +23 -0
  8. package/dist/devices-matter/BaseMatterAccessory.d.ts.map +1 -1
  9. package/dist/devices-matter/BaseMatterAccessory.js +167 -5
  10. package/dist/devices-matter/BaseMatterAccessory.js.map +1 -1
  11. package/dist/devices-matter/ColorLightAccessory.d.ts.map +1 -1
  12. package/dist/devices-matter/ColorLightAccessory.js +12 -12
  13. package/dist/devices-matter/ColorLightAccessory.js.map +1 -1
  14. package/dist/devices-matter/ColorTemperatureLightAccessory.d.ts.map +1 -1
  15. package/dist/devices-matter/ColorTemperatureLightAccessory.js +5 -7
  16. package/dist/devices-matter/ColorTemperatureLightAccessory.js.map +1 -1
  17. package/dist/devices-matter/DimmableLightAccessory.js +9 -9
  18. package/dist/devices-matter/DimmableLightAccessory.js.map +1 -1
  19. package/dist/devices-matter/ExtendedColorLightAccessory.d.ts.map +1 -1
  20. package/dist/devices-matter/ExtendedColorLightAccessory.js +14 -15
  21. package/dist/devices-matter/ExtendedColorLightAccessory.js.map +1 -1
  22. package/dist/devices-matter/OnOffLightAccessory.d.ts.map +1 -1
  23. package/dist/devices-matter/OnOffLightAccessory.js +8 -16
  24. package/dist/devices-matter/OnOffLightAccessory.js.map +1 -1
  25. package/dist/devices-matter/OnOffOutletAccessory.d.ts +2 -0
  26. package/dist/devices-matter/OnOffOutletAccessory.d.ts.map +1 -1
  27. package/dist/devices-matter/OnOffOutletAccessory.js +10 -7
  28. package/dist/devices-matter/OnOffOutletAccessory.js.map +1 -1
  29. package/dist/devices-matter/OnOffSwitchAccessory.js +2 -2
  30. package/dist/devices-matter/OnOffSwitchAccessory.js.map +1 -1
  31. package/dist/homebridge-ui/public/index.html +48 -1
  32. package/dist/homebridge-ui/server.js +35 -0
  33. package/dist/homebridge-ui/server.js.map +1 -1
  34. package/dist/index.d.ts.map +1 -1
  35. package/dist/index.js +4 -5
  36. package/dist/index.js.map +1 -1
  37. package/dist/index.test.js +7 -2
  38. package/dist/index.test.js.map +1 -1
  39. package/dist/irdevice/irdevice.d.ts +11 -10
  40. package/dist/irdevice/irdevice.d.ts.map +1 -1
  41. package/dist/irdevice/irdevice.js +76 -35
  42. package/dist/irdevice/irdevice.js.map +1 -1
  43. package/dist/platform-hap.d.ts +11 -14
  44. package/dist/platform-hap.d.ts.map +1 -1
  45. package/dist/platform-hap.js +64 -64
  46. package/dist/platform-hap.js.map +1 -1
  47. package/dist/platform-matter.d.ts +85 -6
  48. package/dist/platform-matter.d.ts.map +1 -1
  49. package/dist/platform-matter.js +1736 -84
  50. package/dist/platform-matter.js.map +1 -1
  51. package/dist/settings.d.ts +9 -0
  52. package/dist/settings.d.ts.map +1 -1
  53. package/dist/settings.js.map +1 -1
  54. package/dist/test/devices-matter/baseMatterAccessory.test.d.ts +2 -0
  55. package/dist/test/devices-matter/baseMatterAccessory.test.d.ts.map +1 -0
  56. package/dist/test/devices-matter/baseMatterAccessory.test.js +71 -0
  57. package/dist/test/devices-matter/baseMatterAccessory.test.js.map +1 -0
  58. package/dist/test/helpers/platform-fixtures.d.ts +9 -0
  59. package/dist/test/helpers/platform-fixtures.d.ts.map +1 -0
  60. package/dist/test/helpers/platform-fixtures.js +30 -0
  61. package/dist/test/helpers/platform-fixtures.js.map +1 -0
  62. package/dist/test/platform-matter.additional.test.d.ts +2 -0
  63. package/dist/test/platform-matter.additional.test.d.ts.map +1 -0
  64. package/dist/test/platform-matter.additional.test.js +35 -0
  65. package/dist/test/platform-matter.additional.test.js.map +1 -0
  66. package/dist/test/platform-matter.bleparse.test.d.ts +2 -0
  67. package/dist/test/platform-matter.bleparse.test.d.ts.map +1 -0
  68. package/dist/test/platform-matter.bleparse.test.js +43 -0
  69. package/dist/test/platform-matter.bleparse.test.js.map +1 -0
  70. package/dist/test/platform-matter.cleanup.test.d.ts +2 -0
  71. package/dist/test/platform-matter.cleanup.test.d.ts.map +1 -0
  72. package/dist/test/platform-matter.cleanup.test.js +70 -0
  73. package/dist/test/platform-matter.cleanup.test.js.map +1 -0
  74. package/dist/test/platform-matter.keepstale.test.d.ts +2 -0
  75. package/dist/test/platform-matter.keepstale.test.d.ts.map +1 -0
  76. package/dist/test/platform-matter.keepstale.test.js +27 -0
  77. package/dist/test/platform-matter.keepstale.test.js.map +1 -0
  78. package/dist/test/platform-matter.mapping.test.d.ts +2 -0
  79. package/dist/test/platform-matter.mapping.test.d.ts.map +1 -0
  80. package/dist/test/platform-matter.mapping.test.js +43 -0
  81. package/dist/test/platform-matter.mapping.test.js.map +1 -0
  82. package/dist/test/platform-matter.openapi-mapping.test.d.ts +2 -0
  83. package/dist/test/platform-matter.openapi-mapping.test.d.ts.map +1 -0
  84. package/dist/test/platform-matter.openapi-mapping.test.js +84 -0
  85. package/dist/test/platform-matter.openapi-mapping.test.js.map +1 -0
  86. package/dist/test/platform-matter.test.d.ts +2 -0
  87. package/dist/test/platform-matter.test.d.ts.map +1 -0
  88. package/dist/test/platform-matter.test.js +117 -0
  89. package/dist/test/platform-matter.test.js.map +1 -0
  90. package/dist/test/platform-matter.unregister.test.d.ts +2 -0
  91. package/dist/test/platform-matter.unregister.test.d.ts.map +1 -0
  92. package/dist/test/platform-matter.unregister.test.js +30 -0
  93. package/dist/test/platform-matter.unregister.test.js.map +1 -0
  94. package/dist/utils.d.ts +127 -0
  95. package/dist/utils.d.ts.map +1 -1
  96. package/dist/utils.js +380 -0
  97. package/dist/utils.js.map +1 -1
  98. package/dist/utils.test.d.ts +2 -0
  99. package/dist/utils.test.d.ts.map +1 -0
  100. package/dist/utils.test.js +95 -0
  101. package/dist/utils.test.js.map +1 -0
  102. package/dist/verifyconfig.test.js +2 -2
  103. package/dist/verifyconfig.test.js.map +1 -1
  104. package/docs/assets/main.js +2 -2
  105. package/docs/index.html +2 -2
  106. package/docs/variables/default.html +1 -1
  107. package/package.json +14 -14
  108. package/src/devices-hap/device.ts +68 -30
  109. package/src/devices-matter/BaseMatterAccessory.ts +168 -5
  110. package/src/devices-matter/ColorLightAccessory.ts +12 -12
  111. package/src/devices-matter/ColorTemperatureLightAccessory.ts +5 -7
  112. package/src/devices-matter/DimmableLightAccessory.ts +9 -9
  113. package/src/devices-matter/ExtendedColorLightAccessory.ts +14 -15
  114. package/src/devices-matter/OnOffLightAccessory.ts +8 -16
  115. package/src/devices-matter/OnOffOutletAccessory.ts +12 -7
  116. package/src/devices-matter/OnOffSwitchAccessory.ts +2 -2
  117. package/src/homebridge-ui/public/index.html +48 -1
  118. package/src/homebridge-ui/server.ts +37 -0
  119. package/src/index.test.ts +7 -2
  120. package/src/index.ts +4 -5
  121. package/src/irdevice/irdevice.ts +74 -35
  122. package/src/platform-hap.ts +68 -73
  123. package/src/platform-matter.ts +1772 -87
  124. package/src/settings.ts +13 -0
  125. package/src/test/devices-matter/baseMatterAccessory.test.ts +88 -0
  126. package/src/test/helpers/platform-fixtures.ts +33 -0
  127. package/src/test/platform-matter.additional.test.ts +44 -0
  128. package/src/test/platform-matter.bleparse.test.ts +47 -0
  129. package/src/test/platform-matter.cleanup.test.ts +86 -0
  130. package/src/test/platform-matter.keepstale.test.ts +37 -0
  131. package/src/test/platform-matter.mapping.test.ts +57 -0
  132. package/src/test/platform-matter.openapi-mapping.test.ts +109 -0
  133. package/src/test/platform-matter.test.ts +144 -0
  134. package/src/test/platform-matter.unregister.test.ts +39 -0
  135. package/src/utils.test.ts +96 -0
  136. package/src/utils.ts +391 -3
  137. package/src/verifyconfig.test.ts +11 -10
@@ -10,6 +10,8 @@
10
10
 
11
11
  import type { API, EndpointType, Logger, MatterAccessory } from 'homebridge'
12
12
 
13
+ import { rgb2hs } from '../utils.js'
14
+
13
15
  export interface BaseMatterAccessoryConfig {
14
16
  uuid: string
15
17
  displayName: string
@@ -86,26 +88,187 @@ export abstract class BaseMatterAccessory implements MatterAccessory {
86
88
  */
87
89
  protected async updateState(cluster: string, attributes: Record<string, unknown>): Promise<void> {
88
90
  await this.api.matter.updateAccessoryState(this.uuid, cluster, attributes)
89
- this.log.debug(`[${this.displayName}] Updated ${cluster} state:`, attributes)
91
+ this.logDebug(`Updated ${cluster} state:`, attributes)
90
92
  }
91
93
 
92
94
  /**
93
95
  * Log helper methods
94
96
  */
97
+ // Generic logging delegation: prefer platform-provided log helpers in
98
+ // `this.context` (infoLog/debugLog/warnLog/errorLog) and fall back to the
99
+ // local `this.log` methods when not available.
100
+ protected logWith(level: 'info' | 'error' | 'debug' | 'warn', message: string, ...args: unknown[]): void {
101
+ const ctx: any = this.context as any
102
+ const map: Record<string, string> = {
103
+ info: 'infoLog',
104
+ error: 'errorLog',
105
+ debug: 'debugLog',
106
+ warn: 'warnLog',
107
+ }
108
+ const fn = ctx?.[map[level]]
109
+ if (typeof fn === 'function') {
110
+ fn(`[${this.displayName}] ${message}`, ...args)
111
+ return
112
+ }
113
+ const local = (this.log as any)[level]
114
+ if (typeof local === 'function') {
115
+ local.call(this.log, `[${this.displayName}] ${message}`, ...args)
116
+ }
117
+ }
118
+
95
119
  protected logInfo(message: string, ...args: unknown[]): void {
96
- this.log.info(`[${this.displayName}] ${message}`, ...args)
120
+ this.logWith('info', message, ...args)
97
121
  }
98
122
 
99
123
  protected logError(message: string, ...args: unknown[]): void {
100
- this.log.error(`[${this.displayName}] ${message}`, ...args)
124
+ this.logWith('error', message, ...args)
101
125
  }
102
126
 
103
127
  protected logDebug(message: string, ...args: unknown[]): void {
104
- this.log.debug(`[${this.displayName}] ${message}`, ...args)
128
+ this.logWith('debug', message, ...args)
105
129
  }
106
130
 
107
131
  protected logWarn(message: string, ...args: unknown[]): void {
108
- this.log.warn(`[${this.displayName}] ${message}`, ...args)
132
+ this.logWith('warn', message, ...args)
133
+ }
134
+
135
+ /**
136
+ * Logging helpers parity: allow Matter accessories to ask whether device-level
137
+ * logging is enabled or in debug mode. These mirror the helpers used by the
138
+ * HAP/IR device bases so behavior is consistent across platforms.
139
+ */
140
+ public async loggingIsDebug(): Promise<boolean> {
141
+ const ctx: any = this.context as any
142
+ const deviceLogging = ctx?.deviceLogging
143
+ // deviceLogging may be a string ('debug', 'debugMode', 'standard') or undefined
144
+ return deviceLogging === 'debugMode' || deviceLogging === 'debug'
145
+ }
146
+
147
+ public async enablingDeviceLogging(): Promise<boolean> {
148
+ const ctx: any = this.context as any
149
+ const deviceLogging = ctx?.deviceLogging
150
+ // If deviceLogging isn't provided, fall back to the platform-wide flag
151
+ // that indicates whether platform logging is enabled.
152
+ if (deviceLogging === undefined) {
153
+ return Boolean((ctx as any)?.platformLogging)
154
+ }
155
+ return deviceLogging === 'debugMode' || deviceLogging === 'debug' || deviceLogging === 'standard'
156
+ }
157
+
158
+ /**
159
+ * Convenience helpers that delegate OpenAPI/BLE commands to platform-provided
160
+ * functions that are injected into the accessory `context` by
161
+ * `platform-matter` when the accessory is created from a discovered device.
162
+ *
163
+ * These methods are intentionally thin wrappers — the platform controls
164
+ * retries, discovery and client lifecycle via the helper functions.
165
+ */
166
+ protected async sendOpenAPICommand(command: string, parameter = 'default'): Promise<any> {
167
+ const ctx: any = this.context as any
168
+ const fn = ctx?.sendOpenAPI
169
+ if (typeof fn === 'function') {
170
+ return fn(command, parameter)
171
+ }
172
+ throw new Error('OpenAPI helper not available')
173
+ }
174
+
175
+ protected async sendBLECommand(methodName: string, ...args: any[]): Promise<any> {
176
+ const ctx: any = this.context as any
177
+ const fn = ctx?.sendBLE
178
+ if (typeof fn === 'function') {
179
+ return fn(methodName, ...args)
180
+ }
181
+ throw new Error('BLE helper not available')
182
+ }
183
+
184
+ public async sendOnCommand(deviceId?: string): Promise<void> {
185
+ const ctx: any = this.context as any
186
+ const id = deviceId ?? ctx?.deviceId
187
+ try {
188
+ if (ctx?.connectionType === 'BLE') {
189
+ await this.sendBLECommand('turnOn')
190
+ } else {
191
+ await this.sendOpenAPICommand('turnOn')
192
+ }
193
+ // update our matter state
194
+ await this.updateState(this.api.matter.clusterNames.OnOff, { onOff: true })
195
+ this.logInfo(`sendOnCommand successful for ${id}`)
196
+ } catch (e: any) {
197
+ this.logError(`sendOnCommand failed for ${id}: ${String(e?.message ?? e)}`)
198
+ throw e
199
+ }
200
+ }
201
+
202
+ public async sendOffCommand(deviceId?: string): Promise<void> {
203
+ const ctx: any = this.context as any
204
+ const id = deviceId ?? ctx?.deviceId
205
+ try {
206
+ if (ctx?.connectionType === 'BLE') {
207
+ await this.sendBLECommand('turnOff')
208
+ } else {
209
+ await this.sendOpenAPICommand('turnOff')
210
+ }
211
+ await this.updateState(this.api.matter.clusterNames.OnOff, { onOff: false })
212
+ this.logInfo(`sendOffCommand successful for ${id}`)
213
+ } catch (e: any) {
214
+ this.logError(`sendOffCommand failed for ${id}: ${String(e?.message ?? e)}`)
215
+ throw e
216
+ }
217
+ }
218
+
219
+ public async sendSetBrightness(percent: number, deviceId?: string): Promise<void> {
220
+ const ctx: any = this.context as any
221
+ const id = deviceId ?? ctx?.deviceId
222
+ try {
223
+ if (ctx?.connectionType === 'BLE') {
224
+ await this.sendBLECommand('setBrightness', percent)
225
+ } else {
226
+ await this.sendOpenAPICommand('setBrightness', String(percent))
227
+ }
228
+ const level = Math.round((percent / 100) * 254)
229
+ await this.updateState(this.api.matter.clusterNames.LevelControl, { currentLevel: level })
230
+ this.logInfo(`sendSetBrightness successful for ${id}: ${percent}%`)
231
+ } catch (e: any) {
232
+ this.logError(`sendSetBrightness failed for ${id}: ${String(e?.message ?? e)}`)
233
+ throw e
234
+ }
235
+ }
236
+
237
+ public async sendSetColor(r: number, g: number, b: number, deviceId?: string): Promise<void> {
238
+ const ctx: any = this.context as any
239
+ const id = deviceId ?? ctx?.deviceId
240
+ try {
241
+ if (ctx?.connectionType === 'BLE') {
242
+ await this.sendBLECommand('setRGB', r, g, b)
243
+ } else {
244
+ await this.sendOpenAPICommand('setColor', `${r}:${g}:${b}`)
245
+ }
246
+ // Convert to hue/sat for matter state update
247
+ const [h, s] = rgb2hs(r, g, b)
248
+ await this.updateState(this.api.matter.clusterNames.ColorControl, { currentHue: Math.round((h / 360) * 254), currentSaturation: Math.round((s / 100) * 254) })
249
+ this.logInfo(`sendSetColor successful for ${id}: ${r},${g},${b}`)
250
+ } catch (e: any) {
251
+ this.logError(`sendSetColor failed for ${id}: ${String(e?.message ?? e)}`)
252
+ throw e
253
+ }
254
+ }
255
+
256
+ public async sendSetColorTemperature(kelvin: number, deviceId?: string): Promise<void> {
257
+ const ctx: any = this.context as any
258
+ const id = deviceId ?? ctx?.deviceId
259
+ try {
260
+ if (ctx?.connectionType === 'BLE') {
261
+ await this.sendBLECommand('setColorTemperature', kelvin)
262
+ } else {
263
+ await this.sendOpenAPICommand('setColorTemperature', `${kelvin}`)
264
+ }
265
+ const mireds = Math.round(1000000 / kelvin)
266
+ await this.updateState(this.api.matter.clusterNames.ColorControl, { colorTemperatureMireds: mireds })
267
+ this.logInfo(`sendSetColorTemperature successful for ${id}: ${kelvin}K`)
268
+ } catch (e: any) {
269
+ this.logError(`sendSetColorTemperature failed for ${id}: ${String(e?.message ?? e)}`)
270
+ throw e
271
+ }
109
272
  }
110
273
 
111
274
  /**
@@ -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 ColorLightAccessory extends BaseMatterAccessory {
@@ -56,38 +57,37 @@ export class ColorLightAccessory extends BaseMatterAccessory {
56
57
 
57
58
  private async handleOn(): Promise<void> {
58
59
  this.logInfo('turning on.')
59
- // TODO: await myLightAPI.turnOn()
60
+ await this.sendOnCommand()
60
61
  }
61
62
 
62
63
  private async handleOff(): Promise<void> {
63
64
  this.logInfo('turning off.')
64
- // TODO: await myLightAPI.turnOff()
65
+ await this.sendOffCommand()
65
66
  }
66
67
 
67
68
  private async handleSetLevel(request: MatterRequests.MoveToLevel): Promise<void> {
68
69
  this.logInfo(`MoveToLevel request: ${JSON.stringify(request)}`)
69
70
  const { level } = request
70
71
  const brightnessPercent = Math.round((level / 254) * 100)
71
- this.logInfo(`setting brightness to ${brightnessPercent}%.`)
72
- // TODO: await myLightAPI.setBrightness(brightnessPercent)
72
+ await this.sendSetBrightness(brightnessPercent)
73
73
  }
74
74
 
75
75
  private async handleSetColor(request: MatterRequests.MoveToColor): Promise<void> {
76
76
  this.logInfo(`MoveToColor request: ${JSON.stringify(request)}`)
77
- const { colorX, colorY, transitionTime } = request
78
- const xFloat = (colorX / 65535).toFixed(4)
79
- const yFloat = (colorY / 65535).toFixed(4)
80
- this.logInfo(`setting xy color to (${xFloat}, ${yFloat}) with transition ${transitionTime}.`)
81
- // TODO: await myLightAPI.setXY(xFloat, yFloat, transitionTime)
77
+ const { colorX, colorY } = request
78
+ const hueApprox = Math.round((colorX / 65535) * 360)
79
+ const satApprox = Math.round((colorY / 65535) * 100)
80
+ const [r, g, b] = hs2rgb(hueApprox, satApprox)
81
+ await this.sendSetColor(r, g, b)
82
82
  }
83
83
 
84
84
  private async handleSetHueSaturation(request: MatterRequests.MoveToHueAndSaturation): Promise<void> {
85
85
  this.logInfo(`MoveToHueAndSaturation request: ${JSON.stringify(request)}`)
86
- const { hue, saturation, transitionTime } = request
86
+ const { hue, saturation } = request
87
87
  const hueDegrees = Math.round((hue / 254) * 360)
88
88
  const saturationPercent = Math.round((saturation / 254) * 100)
89
- this.logInfo(`setting color to ${hueDegrees}°, ${saturationPercent}% with transition ${transitionTime}.`)
90
- // TODO: await myLightAPI.setColor(hueDegrees, saturationPercent, transitionTime)
89
+ const [r, g, b] = hs2rgb(hueDegrees, saturationPercent)
90
+ await this.sendSetColor(r, g, b)
91
91
  }
92
92
 
93
93
  public updateOnOffState(isOn: boolean): void {
@@ -52,28 +52,26 @@ export class ColorTemperatureLightAccessory extends BaseMatterAccessory {
52
52
 
53
53
  private async handleOn(): Promise<void> {
54
54
  this.logInfo('turning on.')
55
- // TODO: await myLightAPI.turnOn()
55
+ await this.sendOnCommand()
56
56
  }
57
57
 
58
58
  private async handleOff(): Promise<void> {
59
59
  this.logInfo('turning off.')
60
- // TODO: await myLightAPI.turnOff()
60
+ await this.sendOffCommand()
61
61
  }
62
62
 
63
63
  private async handleSetLevel(request: MatterRequests.MoveToLevel): Promise<void> {
64
64
  this.logInfo(`MoveToLevel request: ${JSON.stringify(request)}`)
65
65
  const { level } = request
66
66
  const brightnessPercent = Math.round((level / 254) * 100)
67
- this.logInfo(`setting brightness to ${brightnessPercent}% (level: ${level}).`)
68
- // TODO: await myLightAPI.setBrightness(brightnessPercent)
67
+ await this.sendSetBrightness(brightnessPercent)
69
68
  }
70
69
 
71
70
  private async handleSetColorTemperature(request: MatterRequests.MoveToColorTemperature): Promise<void> {
72
71
  this.logInfo(`MoveToColorTemperature request: ${JSON.stringify(request)}`)
73
- const { colorTemperatureMireds, transitionTime } = request
72
+ const { colorTemperatureMireds } = request
74
73
  const kelvin = Math.round(1000000 / colorTemperatureMireds)
75
- this.logInfo(`setting color temp to ${kelvin}k (${colorTemperatureMireds} mireds) with transitionTime=${transitionTime}.`)
76
- // TODO: await myLightAPI.setColorTemperature(kelvin, transitionTime)
74
+ await this.sendSetColorTemperature(kelvin)
77
75
  }
78
76
 
79
77
  public updateOnOffState(isOn: boolean): void {
@@ -69,10 +69,10 @@ export class DimmableLightAccessory extends BaseMatterAccessory {
69
69
  this.logInfo('turning on.')
70
70
 
71
71
  try {
72
- // TODO: Control your physical device
73
- // await myLightAPI.turnOn()
72
+ // Use platform helper (OpenAPI/BLE) when available
73
+ await this.sendOnCommand()
74
74
 
75
- this.logInfo('physical device turned on.')
75
+ this.logInfo('physical device turned on (via platform helper).')
76
76
  } catch (error) {
77
77
  this.logError('failed to turn on:', error)
78
78
  throw error
@@ -86,10 +86,10 @@ export class DimmableLightAccessory extends BaseMatterAccessory {
86
86
  this.logInfo('turning off.')
87
87
 
88
88
  try {
89
- // TODO: Control your physical device
90
- // await myLightAPI.turnOff()
89
+ // Use platform helper (OpenAPI/BLE) when available
90
+ await this.sendOffCommand()
91
91
 
92
- this.logInfo('physical device turned off.')
92
+ this.logInfo('physical device turned off (via platform helper).')
93
93
  } catch (error) {
94
94
  this.logError('failed to turn off:', error)
95
95
  throw error
@@ -110,10 +110,10 @@ export class DimmableLightAccessory extends BaseMatterAccessory {
110
110
  this.logInfo(`setting brightness to ${brightnessPercent}% (level: ${level}), transitionTime: ${transitionTime}.`)
111
111
 
112
112
  try {
113
- // TODO: Control your physical device
114
- // await myLightAPI.setBrightness(brightnessPercent, transitionTime)
113
+ // Use platform helper (OpenAPI/BLE) when available
114
+ await this.sendSetBrightness(brightnessPercent)
115
115
 
116
- this.logInfo(`physical device brightness set to ${brightnessPercent}%.`)
116
+ this.logInfo(`physical device brightness set to ${brightnessPercent}% (via platform helper).`)
117
117
  } catch (error) {
118
118
  this.logError('Failed to set brightness:', error)
119
119
  throw error
@@ -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;
@@ -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
  }
package/src/index.test.ts CHANGED
@@ -3,7 +3,6 @@ import type { API } from 'homebridge'
3
3
  import { describe, expect, it, vi } from 'vitest'
4
4
 
5
5
  import registerPlatform from './index.js'
6
- import { SwitchBotHAPPlatform } from './platform-hap.js'
7
6
  import { PLATFORM_NAME, PLUGIN_NAME } from './settings.js'
8
7
 
9
8
  describe('index.ts', () => {
@@ -14,6 +13,12 @@ describe('index.ts', () => {
14
13
 
15
14
  registerPlatform(api)
16
15
 
17
- expect(api.registerPlatform).toHaveBeenCalledWith(PLUGIN_NAME, PLATFORM_NAME, SwitchBotHAPPlatform)
16
+ // The platform registration now uses a runtime proxy/delegate constructor so
17
+ // assert the call happened and the third argument is a constructor function.
18
+ expect(api.registerPlatform).toHaveBeenCalled()
19
+ const callArgs = (api.registerPlatform as any).mock.calls[0]
20
+ expect(callArgs[0]).toBe(PLUGIN_NAME)
21
+ expect(callArgs[1]).toBe(PLATFORM_NAME)
22
+ expect(typeof callArgs[2]).toBe('function')
18
23
  })
19
24
  })
package/src/index.ts CHANGED
@@ -7,12 +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
- // If Matter is enabled register the Matter platform, otherwise use HAP platform.
17
- 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)
18
17
  }