@switchbot/homebridge-switchbot 5.0.0-beta.12 → 5.0.0-beta.13

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 (43) hide show
  1. package/dist/baseMatterAccessory.test.d.ts +2 -0
  2. package/dist/baseMatterAccessory.test.d.ts.map +1 -0
  3. package/dist/baseMatterAccessory.test.js +71 -0
  4. package/dist/baseMatterAccessory.test.js.map +1 -0
  5. package/dist/devices-matter/BaseMatterAccessory.d.ts +15 -0
  6. package/dist/devices-matter/BaseMatterAccessory.d.ts.map +1 -1
  7. package/dist/devices-matter/BaseMatterAccessory.js +120 -0
  8. package/dist/devices-matter/BaseMatterAccessory.js.map +1 -1
  9. package/dist/devices-matter/ColorLightAccessory.d.ts.map +1 -1
  10. package/dist/devices-matter/ColorLightAccessory.js +12 -12
  11. package/dist/devices-matter/ColorLightAccessory.js.map +1 -1
  12. package/dist/devices-matter/ColorTemperatureLightAccessory.d.ts.map +1 -1
  13. package/dist/devices-matter/ColorTemperatureLightAccessory.js +5 -7
  14. package/dist/devices-matter/ColorTemperatureLightAccessory.js.map +1 -1
  15. package/dist/devices-matter/DimmableLightAccessory.js +9 -9
  16. package/dist/devices-matter/DimmableLightAccessory.js.map +1 -1
  17. package/dist/devices-matter/ExtendedColorLightAccessory.d.ts.map +1 -1
  18. package/dist/devices-matter/ExtendedColorLightAccessory.js +14 -15
  19. package/dist/devices-matter/ExtendedColorLightAccessory.js.map +1 -1
  20. package/dist/devices-matter/OnOffLightAccessory.d.ts.map +1 -1
  21. package/dist/devices-matter/OnOffLightAccessory.js +8 -16
  22. package/dist/devices-matter/OnOffLightAccessory.js.map +1 -1
  23. package/dist/devices-matter/OnOffOutletAccessory.d.ts +2 -0
  24. package/dist/devices-matter/OnOffOutletAccessory.d.ts.map +1 -1
  25. package/dist/devices-matter/OnOffOutletAccessory.js +10 -7
  26. package/dist/devices-matter/OnOffOutletAccessory.js.map +1 -1
  27. package/dist/devices-matter/OnOffSwitchAccessory.js +2 -2
  28. package/dist/devices-matter/OnOffSwitchAccessory.js.map +1 -1
  29. package/dist/platform-matter.d.ts.map +1 -1
  30. package/dist/platform-matter.js +37 -22
  31. package/dist/platform-matter.js.map +1 -1
  32. package/docs/variables/default.html +1 -1
  33. package/package.json +1 -1
  34. package/src/baseMatterAccessory.test.ts +88 -0
  35. package/src/devices-matter/BaseMatterAccessory.ts +118 -0
  36. package/src/devices-matter/ColorLightAccessory.ts +12 -12
  37. package/src/devices-matter/ColorTemperatureLightAccessory.ts +5 -7
  38. package/src/devices-matter/DimmableLightAccessory.ts +9 -9
  39. package/src/devices-matter/ExtendedColorLightAccessory.ts +14 -15
  40. package/src/devices-matter/OnOffLightAccessory.ts +8 -16
  41. package/src/devices-matter/OnOffOutletAccessory.ts +12 -7
  42. package/src/devices-matter/OnOffSwitchAccessory.ts +2 -2
  43. package/src/platform-matter.ts +36 -22
@@ -0,0 +1,88 @@
1
+ import { describe, expect, it, vi } from 'vitest'
2
+
3
+ import { BaseMatterAccessory } from './devices-matter/BaseMatterAccessory.js'
4
+
5
+ // Minimal concrete subclass for testing
6
+ class TestAccessory extends BaseMatterAccessory {
7
+ constructor(api: any, log: any, opts?: any) {
8
+ super(api, log, {
9
+ uuid: opts?.uuid ?? api.matter.uuid.generate('test'),
10
+ displayName: opts?.displayName ?? 'Test',
11
+ deviceType: opts?.deviceType ?? ('OnOffLight' as any),
12
+ serialNumber: opts?.serialNumber ?? 'TEST-1',
13
+ manufacturer: opts?.manufacturer ?? 'TestCo',
14
+ model: opts?.model ?? 'T-1',
15
+ firmwareRevision: opts?.firmwareRevision ?? '1.0',
16
+ hardwareRevision: opts?.hardwareRevision ?? '1.0',
17
+ context: opts?.context ?? {},
18
+ clusters: opts?.clusters,
19
+ handlers: opts?.handlers,
20
+ })
21
+ }
22
+ }
23
+
24
+ describe('baseMatterAccessory helpers', () => {
25
+ it('sendOnCommand uses OpenAPI helper and updates state', async () => {
26
+ const update = vi.fn()
27
+ const sendOpenAPI = vi.fn(async () => ({ response: {}, statusCode: 200 }))
28
+ const api: any = { matter: { clusterNames: { OnOff: 'onOff', LevelControl: 'level', ColorControl: 'color' }, uuid: { generate: () => 'uuid-1' }, updateAccessoryState: update } }
29
+ const log: any = { info: vi.fn(), debug: vi.fn(), error: vi.fn(), warn: vi.fn() }
30
+
31
+ const ctx = { deviceId: 'DEV-1', sendOpenAPI, connectionType: 'OpenAPI' }
32
+ const acc = new TestAccessory(api, log, { context: ctx })
33
+
34
+ await acc.sendOnCommand()
35
+
36
+ expect(sendOpenAPI).toHaveBeenCalledWith('turnOn', 'default')
37
+ expect(update).toHaveBeenCalledWith(acc.uuid, 'onOff', { onOff: true })
38
+ })
39
+
40
+ it('sendOnCommand uses BLE helper when connectionType is BLE', async () => {
41
+ const update = vi.fn()
42
+ const sendBLE = vi.fn(async () => true)
43
+ const api: any = { matter: { clusterNames: { OnOff: 'onOff', LevelControl: 'level', ColorControl: 'color' }, uuid: { generate: () => 'uuid-2' }, updateAccessoryState: update } }
44
+ const log: any = { info: vi.fn(), debug: vi.fn(), error: vi.fn(), warn: vi.fn() }
45
+
46
+ const ctx = { deviceId: 'DEV-2', sendBLE, connectionType: 'BLE' }
47
+ const acc = new TestAccessory(api, log, { context: ctx })
48
+
49
+ await acc.sendOnCommand()
50
+
51
+ expect(sendBLE).toHaveBeenCalledWith('turnOn')
52
+ expect(update).toHaveBeenCalledWith(acc.uuid, 'onOff', { onOff: true })
53
+ })
54
+
55
+ it('sendSetBrightness sends percent and updates LevelControl', async () => {
56
+ const update = vi.fn()
57
+ const sendOpenAPI = vi.fn(async () => ({ response: {}, statusCode: 200 }))
58
+ const api: any = { matter: { clusterNames: { OnOff: 'onOff', LevelControl: 'level', ColorControl: 'color' }, uuid: { generate: () => 'uuid-3' }, updateAccessoryState: update } }
59
+ const log: any = { info: vi.fn(), debug: vi.fn(), error: vi.fn(), warn: vi.fn() }
60
+
61
+ const ctx = { deviceId: 'DEV-3', sendOpenAPI, connectionType: 'OpenAPI' }
62
+ const acc = new TestAccessory(api, log, { context: ctx })
63
+
64
+ await acc.sendSetBrightness(50)
65
+
66
+ expect(sendOpenAPI).toHaveBeenCalledWith('setBrightness', '50')
67
+ const expectedLevel = Math.round((50 / 100) * 254)
68
+ expect(update).toHaveBeenCalledWith(acc.uuid, 'level', { currentLevel: expectedLevel })
69
+ })
70
+
71
+ it('sendSetColor sends RGB and updates ColorControl', async () => {
72
+ const update = vi.fn()
73
+ const sendOpenAPI = vi.fn(async () => ({ response: {}, statusCode: 200 }))
74
+ const api: any = { matter: { clusterNames: { OnOff: 'onOff', LevelControl: 'level', ColorControl: 'color' }, uuid: { generate: () => 'uuid-4' }, updateAccessoryState: update } }
75
+ const log: any = { info: vi.fn(), debug: vi.fn(), error: vi.fn(), warn: vi.fn() }
76
+
77
+ const ctx = { deviceId: 'DEV-4', sendOpenAPI, connectionType: 'OpenAPI' }
78
+ const acc = new TestAccessory(api, log, { context: ctx })
79
+
80
+ await acc.sendSetColor(255, 128, 0)
81
+
82
+ expect(sendOpenAPI).toHaveBeenCalledWith('setColor', '255:128:0')
83
+ expect(update).toHaveBeenCalled()
84
+ // Ensure we updated ColorControl cluster with numeric attributes
85
+ const calledWith = (update.mock.calls[0] || update.mock.calls[1])
86
+ expect(calledWith[1] === 'color' || calledWith[1] === api.matter.clusterNames.ColorControl).toBeTruthy()
87
+ })
88
+ })
@@ -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
@@ -108,6 +110,122 @@ export abstract class BaseMatterAccessory implements MatterAccessory {
108
110
  this.log.warn(`[${this.displayName}] ${message}`, ...args)
109
111
  }
110
112
 
113
+ /**
114
+ * Convenience helpers that delegate OpenAPI/BLE commands to platform-provided
115
+ * functions that are injected into the accessory `context` by
116
+ * `platform-matter` when the accessory is created from a discovered device.
117
+ *
118
+ * These methods are intentionally thin wrappers — the platform controls
119
+ * retries, discovery and client lifecycle via the helper functions.
120
+ */
121
+ protected async sendOpenAPICommand(command: string, parameter = 'default'): Promise<any> {
122
+ const ctx: any = this.context as any
123
+ const fn = ctx?.sendOpenAPI
124
+ if (typeof fn === 'function') {
125
+ return fn(command, parameter)
126
+ }
127
+ throw new Error('OpenAPI helper not available')
128
+ }
129
+
130
+ protected async sendBLECommand(methodName: string, ...args: any[]): Promise<any> {
131
+ const ctx: any = this.context as any
132
+ const fn = ctx?.sendBLE
133
+ if (typeof fn === 'function') {
134
+ return fn(methodName, ...args)
135
+ }
136
+ throw new Error('BLE helper not available')
137
+ }
138
+
139
+ public async sendOnCommand(deviceId?: string): Promise<void> {
140
+ const ctx: any = this.context as any
141
+ const id = deviceId ?? ctx?.deviceId
142
+ try {
143
+ if (ctx?.connectionType === 'BLE') {
144
+ await this.sendBLECommand('turnOn')
145
+ } else {
146
+ await this.sendOpenAPICommand('turnOn')
147
+ }
148
+ // update our matter state
149
+ await this.updateState(this.api.matter.clusterNames.OnOff, { onOff: true })
150
+ this.logInfo(`sendOnCommand successful for ${id}`)
151
+ } catch (e: any) {
152
+ this.logError(`sendOnCommand failed for ${id}: ${String(e?.message ?? e)}`)
153
+ throw e
154
+ }
155
+ }
156
+
157
+ public async sendOffCommand(deviceId?: string): Promise<void> {
158
+ const ctx: any = this.context as any
159
+ const id = deviceId ?? ctx?.deviceId
160
+ try {
161
+ if (ctx?.connectionType === 'BLE') {
162
+ await this.sendBLECommand('turnOff')
163
+ } else {
164
+ await this.sendOpenAPICommand('turnOff')
165
+ }
166
+ await this.updateState(this.api.matter.clusterNames.OnOff, { onOff: false })
167
+ this.logInfo(`sendOffCommand successful for ${id}`)
168
+ } catch (e: any) {
169
+ this.logError(`sendOffCommand failed for ${id}: ${String(e?.message ?? e)}`)
170
+ throw e
171
+ }
172
+ }
173
+
174
+ public async sendSetBrightness(percent: number, deviceId?: string): Promise<void> {
175
+ const ctx: any = this.context as any
176
+ const id = deviceId ?? ctx?.deviceId
177
+ try {
178
+ if (ctx?.connectionType === 'BLE') {
179
+ await this.sendBLECommand('setBrightness', percent)
180
+ } else {
181
+ await this.sendOpenAPICommand('setBrightness', String(percent))
182
+ }
183
+ const level = Math.round((percent / 100) * 254)
184
+ await this.updateState(this.api.matter.clusterNames.LevelControl, { currentLevel: level })
185
+ this.logInfo(`sendSetBrightness successful for ${id}: ${percent}%`)
186
+ } catch (e: any) {
187
+ this.logError(`sendSetBrightness failed for ${id}: ${String(e?.message ?? e)}`)
188
+ throw e
189
+ }
190
+ }
191
+
192
+ public async sendSetColor(r: number, g: number, b: number, deviceId?: string): Promise<void> {
193
+ const ctx: any = this.context as any
194
+ const id = deviceId ?? ctx?.deviceId
195
+ try {
196
+ if (ctx?.connectionType === 'BLE') {
197
+ await this.sendBLECommand('setRGB', r, g, b)
198
+ } else {
199
+ await this.sendOpenAPICommand('setColor', `${r}:${g}:${b}`)
200
+ }
201
+ // Convert to hue/sat for matter state update
202
+ const [h, s] = rgb2hs(r, g, b)
203
+ await this.updateState(this.api.matter.clusterNames.ColorControl, { currentHue: Math.round((h / 360) * 254), currentSaturation: Math.round((s / 100) * 254) })
204
+ this.logInfo(`sendSetColor successful for ${id}: ${r},${g},${b}`)
205
+ } catch (e: any) {
206
+ this.logError(`sendSetColor failed for ${id}: ${String(e?.message ?? e)}`)
207
+ throw e
208
+ }
209
+ }
210
+
211
+ public async sendSetColorTemperature(kelvin: number, deviceId?: string): Promise<void> {
212
+ const ctx: any = this.context as any
213
+ const id = deviceId ?? ctx?.deviceId
214
+ try {
215
+ if (ctx?.connectionType === 'BLE') {
216
+ await this.sendBLECommand('setColorTemperature', kelvin)
217
+ } else {
218
+ await this.sendOpenAPICommand('setColorTemperature', `${kelvin}`)
219
+ }
220
+ const mireds = Math.round(1000000 / kelvin)
221
+ await this.updateState(this.api.matter.clusterNames.ColorControl, { colorTemperatureMireds: mireds })
222
+ this.logInfo(`sendSetColorTemperature successful for ${id}: ${kelvin}K`)
223
+ } catch (e: any) {
224
+ this.logError(`sendSetColorTemperature failed for ${id}: ${String(e?.message ?? e)}`)
225
+ throw e
226
+ }
227
+ }
228
+
111
229
  /**
112
230
  * Convert this class instance to a plain MatterAccessory object
113
231
  * This is what gets registered with Homebridge
@@ -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 {