@ecopoesis/homebridge-dmx 0.5.4 → 0.6.0
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.
- package/CLAUDE.md +1 -0
- package/README.md +67 -0
- package/config.schema.json +104 -0
- package/dist/color/types.d.ts +9 -0
- package/dist/color/types.d.ts.map +1 -1
- package/dist/color/types.js +34 -0
- package/dist/color/types.js.map +1 -1
- package/dist/config.d.ts +5 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +18 -1
- package/dist/config.js.map +1 -1
- package/dist/controller.d.ts +3 -2
- package/dist/controller.d.ts.map +1 -1
- package/dist/controller.js +7 -4
- package/dist/controller.js.map +1 -1
- package/dist/dmxController.d.ts +9 -1
- package/dist/dmxController.d.ts.map +1 -1
- package/dist/platform.d.ts +1 -0
- package/dist/platform.d.ts.map +1 -1
- package/dist/platform.js +37 -2
- package/dist/platform.js.map +1 -1
- package/dist/platformAccessory.d.ts +8 -0
- package/dist/platformAccessory.d.ts.map +1 -1
- package/dist/platformAccessory.js +45 -17
- package/dist/platformAccessory.js.map +1 -1
- package/dist/sacn.d.ts +3 -2
- package/dist/sacn.d.ts.map +1 -1
- package/dist/sacn.js +7 -4
- package/dist/sacn.js.map +1 -1
- package/dist/settings.d.ts +13 -0
- package/dist/settings.d.ts.map +1 -1
- package/dist/settings.js +14 -0
- package/dist/settings.js.map +1 -1
- package/dist/show/colors.d.ts +25 -0
- package/dist/show/colors.d.ts.map +1 -0
- package/dist/show/colors.js +216 -0
- package/dist/show/colors.js.map +1 -0
- package/dist/show/dsl.d.ts +56 -0
- package/dist/show/dsl.d.ts.map +1 -0
- package/dist/show/dsl.js +326 -0
- package/dist/show/dsl.js.map +1 -0
- package/dist/show/engine.d.ts +50 -0
- package/dist/show/engine.d.ts.map +1 -0
- package/dist/show/engine.js +264 -0
- package/dist/show/engine.js.map +1 -0
- package/dist/showAccessory.d.ts +9 -0
- package/dist/showAccessory.d.ts.map +1 -0
- package/dist/showAccessory.js +33 -0
- package/dist/showAccessory.js.map +1 -0
- package/dist/stateRegistry.d.ts +8 -3
- package/dist/stateRegistry.d.ts.map +1 -1
- package/dist/stateRegistry.js +4 -4
- package/dist/stateRegistry.js.map +1 -1
- package/dist/zoneAccessory.d.ts +3 -0
- package/dist/zoneAccessory.d.ts.map +1 -1
- package/dist/zoneAccessory.js +36 -13
- package/dist/zoneAccessory.js.map +1 -1
- package/examples/dmx.yaml +76 -0
- package/package.json +1 -1
- package/src/color/types.ts +30 -0
- package/src/config.ts +22 -1
- package/src/controller.ts +8 -5
- package/src/dmxController.ts +10 -1
- package/src/platform.ts +35 -2
- package/src/platformAccessory.ts +43 -16
- package/src/sacn.ts +8 -4
- package/src/settings.ts +19 -0
- package/src/show/colors.ts +224 -0
- package/src/show/dsl.ts +370 -0
- package/src/show/engine.ts +280 -0
- package/src/showAccessory.ts +46 -0
- package/src/stateRegistry.ts +11 -5
- package/src/zoneAccessory.ts +36 -14
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
// ShowEngine — runs at most one show at a time.
|
|
2
|
+
//
|
|
3
|
+
// Every frame (the involved controllers' fastest frame interval, 25 Hz on
|
|
4
|
+
// sACN) the engine works out where each fixture is between the previous
|
|
5
|
+
// step's look and the current one, renders it through the controller
|
|
6
|
+
// (quietly — no per-frame logging) and moves on. The registry only hears
|
|
7
|
+
// about it at step boundaries (throttled) and when the show stops, so
|
|
8
|
+
// HomeKit tiles track the show without a 25 Hz event storm.
|
|
9
|
+
//
|
|
10
|
+
// Rules:
|
|
11
|
+
// - starting a show stops whichever one was running (its switch flips off)
|
|
12
|
+
// - the first fade starts from the fixtures' live registry state, so a
|
|
13
|
+
// show fades IN from whatever the lights were doing
|
|
14
|
+
// - stopping freezes the lights on the current frame and writes that
|
|
15
|
+
// frame to the registry (so tiles are accurate)
|
|
16
|
+
// - a HomeKit change to any fixture in the running show stops the show;
|
|
17
|
+
// the fixture that was touched keeps the user's new value
|
|
18
|
+
|
|
19
|
+
import type { Logger } from 'homebridge';
|
|
20
|
+
|
|
21
|
+
import { Fixture } from '../config.js';
|
|
22
|
+
import { HomeKitLightState } from '../color/types.js';
|
|
23
|
+
import { DmxController } from '../dmxController.js';
|
|
24
|
+
import { StateRegistry } from '../stateRegistry.js';
|
|
25
|
+
import { SHOW_FRAME_MS_DEFAULT, SHOW_KEYFRAME_MIN_MS } from '../settings.js';
|
|
26
|
+
import { LookColor, mixLook } from './colors.js';
|
|
27
|
+
import { Show } from './dsl.js';
|
|
28
|
+
|
|
29
|
+
export type ShowListener = (runningId: string | null) => void;
|
|
30
|
+
|
|
31
|
+
interface Run {
|
|
32
|
+
show: Show;
|
|
33
|
+
startedAt: number;
|
|
34
|
+
timer: NodeJS.Timeout;
|
|
35
|
+
/** Registry state of each fixture when the show started. */
|
|
36
|
+
snapshot: Map<string, LookColor>;
|
|
37
|
+
fixtures: Map<string, Fixture>;
|
|
38
|
+
frameMs: number;
|
|
39
|
+
/** The frame most recently put on the wire — what stop() freezes on. */
|
|
40
|
+
lastFrame: Map<string, LookColor> | null;
|
|
41
|
+
lastKeyframeStep: number;
|
|
42
|
+
lastKeyframeAt: number;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export class ShowEngine {
|
|
46
|
+
private run: Run | null = null;
|
|
47
|
+
private listeners = new Set<ShowListener>();
|
|
48
|
+
private lastEmitted: string | null = null;
|
|
49
|
+
private shows = new Map<string, Show>();
|
|
50
|
+
|
|
51
|
+
constructor(
|
|
52
|
+
shows: Show[],
|
|
53
|
+
private readonly controllers: Map<string, DmxController>,
|
|
54
|
+
private readonly registry: StateRegistry,
|
|
55
|
+
private readonly log: Logger,
|
|
56
|
+
) {
|
|
57
|
+
for (const s of shows) this.shows.set(s.id, s);
|
|
58
|
+
// Any HomeKit change to a fixture in the running show ends the show.
|
|
59
|
+
registry.onChange((id, source) => {
|
|
60
|
+
if (source !== 'homekit' || !this.run) return;
|
|
61
|
+
if (this.run.fixtures.has(id)) {
|
|
62
|
+
this.stop(`"${id}" changed from HomeKit`, id);
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
running(): string | null {
|
|
68
|
+
return this.run?.show.id ?? null;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
onChange(cb: ShowListener): () => void {
|
|
72
|
+
this.listeners.add(cb);
|
|
73
|
+
return () => this.listeners.delete(cb);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
start(id: string): void {
|
|
77
|
+
const show = this.shows.get(id);
|
|
78
|
+
if (!show) { this.log.error(`show "${id}": unknown`); return; }
|
|
79
|
+
if (!show.runnable) {
|
|
80
|
+
this.log.error(`show "${id}": unbound params (${show.unboundParams.join(', ')})`);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
if (this.run?.show.id === id) return;
|
|
84
|
+
if (this.run) this.stopRun(`"${id}" starting`, undefined, false);
|
|
85
|
+
|
|
86
|
+
const snapshot = new Map<string, LookColor>();
|
|
87
|
+
for (const fx of show.fixtures) snapshot.set(fx.id, fromState(this.registry.get(fx.id)));
|
|
88
|
+
|
|
89
|
+
// One timer drives every involved controller, so tick at the SLOWEST
|
|
90
|
+
// frame interval among them: flooding the Stick with 25 Hz sets would
|
|
91
|
+
// keep resetting its debounce and it would never transact at all.
|
|
92
|
+
const frameMs = show.fixtures.reduce((max, fx) => {
|
|
93
|
+
const c = this.controllers.get(fx.controller.id);
|
|
94
|
+
return Math.max(max, c?.frameIntervalMs ?? SHOW_FRAME_MS_DEFAULT);
|
|
95
|
+
}, 0) || SHOW_FRAME_MS_DEFAULT;
|
|
96
|
+
const run: Run = {
|
|
97
|
+
show,
|
|
98
|
+
startedAt: Date.now(),
|
|
99
|
+
timer: setInterval(() => this.tick(), frameMs),
|
|
100
|
+
snapshot,
|
|
101
|
+
fixtures: new Map(show.fixtures.map((f) => [f.id, f])),
|
|
102
|
+
frameMs,
|
|
103
|
+
lastFrame: null,
|
|
104
|
+
lastKeyframeStep: -1,
|
|
105
|
+
lastKeyframeAt: 0,
|
|
106
|
+
};
|
|
107
|
+
run.timer.unref?.();
|
|
108
|
+
this.run = run;
|
|
109
|
+
this.log.info(
|
|
110
|
+
`show "${show.id}" started: ${show.steps.length} steps, ${show.fixtures.length} fixtures, ` +
|
|
111
|
+
`cycle ${(show.cycleMs / 1000).toFixed(1)} s, ${show.loop ? 'looping' : 'one-shot'}, ` +
|
|
112
|
+
`${frameMs} ms frames`,
|
|
113
|
+
);
|
|
114
|
+
this.tick();
|
|
115
|
+
this.emit();
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** Stop the running show, freezing the lights where they are: the
|
|
119
|
+
* registry gets the last frame that actually went on the wire. `except`
|
|
120
|
+
* names a fixture whose registry state must NOT be overwritten (it was
|
|
121
|
+
* just set from HomeKit and is the reason we're stopping). */
|
|
122
|
+
stop(reason = 'switched off', except?: string): void {
|
|
123
|
+
this.stopRun(reason, except, true);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** `notify: false` on a handoff inside start(): the switches hear one
|
|
127
|
+
* transition (A → B) instead of A → nothing → B, which would bounce
|
|
128
|
+
* the tapped tile off and back on. */
|
|
129
|
+
private stopRun(reason: string, except: string | undefined, notify: boolean): void {
|
|
130
|
+
const run = this.run;
|
|
131
|
+
if (!run) return;
|
|
132
|
+
clearInterval(run.timer);
|
|
133
|
+
this.run = null;
|
|
134
|
+
// Before the first tick nothing has been sent; after a one-shot's end
|
|
135
|
+
// the final look is on the wire (see applyFinal).
|
|
136
|
+
const frozen = run.lastFrame ?? run.show.steps[run.show.steps.length - 1].targets;
|
|
137
|
+
for (const [id, look] of frozen) {
|
|
138
|
+
if (id === except) continue;
|
|
139
|
+
this.registry.set(id, toState(look, this.fixture(run, id)), 'show');
|
|
140
|
+
}
|
|
141
|
+
this.log.info(`show "${run.show.id}" stopped (${reason})`);
|
|
142
|
+
if (notify) this.emit();
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
shutdown(): void {
|
|
146
|
+
if (this.run) { clearInterval(this.run.timer); this.run = null; }
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** Notify switches of the running show — only on an actual transition,
|
|
150
|
+
* so the handoff inside start() (stop A, start B) doesn't bounce B's
|
|
151
|
+
* tile off and back on. */
|
|
152
|
+
private emit(): void {
|
|
153
|
+
const id = this.running();
|
|
154
|
+
if (id === this.lastEmitted) return;
|
|
155
|
+
this.lastEmitted = id;
|
|
156
|
+
for (const l of this.listeners) l(id);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
private fixture(run: Run, id: string): Fixture {
|
|
160
|
+
return run.fixtures.get(id)!;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
private tick(): void {
|
|
164
|
+
const run = this.run;
|
|
165
|
+
if (!run) return;
|
|
166
|
+
const now = Date.now();
|
|
167
|
+
const frame = this.frameAt(run, now);
|
|
168
|
+
if (!frame) {
|
|
169
|
+
// One-shot show ran off the end: land on the final look and stop.
|
|
170
|
+
this.applyFinal(run);
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
for (const [id, look] of frame.looks) {
|
|
174
|
+
const fx = this.fixture(run, id);
|
|
175
|
+
const c = this.controllers.get(fx.controller.id);
|
|
176
|
+
c?.setFixture(fx, toState(look, fx, true), { quiet: true });
|
|
177
|
+
}
|
|
178
|
+
run.lastFrame = frame.looks;
|
|
179
|
+
// Keyframe: on entering a new step, tell the registry where the lights
|
|
180
|
+
// are heading — the step's destination, not the in-between value, so
|
|
181
|
+
// a long fade shows its target for the whole fade. Throttled so fast
|
|
182
|
+
// chases don't flood HomeKit; on a sub-second chase the tiles lag by
|
|
183
|
+
// an unbounded amount, which is fine for a chase.
|
|
184
|
+
if (now - run.lastKeyframeAt >= SHOW_KEYFRAME_MIN_MS) {
|
|
185
|
+
run.lastKeyframeAt = now;
|
|
186
|
+
if (frame.step !== run.lastKeyframeStep) {
|
|
187
|
+
run.lastKeyframeStep = frame.step;
|
|
188
|
+
const targets = run.show.steps[frame.step].targets;
|
|
189
|
+
for (const [id, look] of targets) {
|
|
190
|
+
this.registry.set(id, toState(look, this.fixture(run, id)), 'show');
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
private applyFinal(run: Run): void {
|
|
197
|
+
clearInterval(run.timer);
|
|
198
|
+
this.run = null; // before any registry write, same as stop()
|
|
199
|
+
const last = run.show.steps[run.show.steps.length - 1];
|
|
200
|
+
for (const [id, look] of last.targets) {
|
|
201
|
+
const fx = this.fixture(run, id);
|
|
202
|
+
this.controllers.get(fx.controller.id)?.setFixture(fx, toState(look, fx, true), { quiet: true });
|
|
203
|
+
this.registry.set(id, toState(look, fx), 'show');
|
|
204
|
+
}
|
|
205
|
+
this.log.info(`show "${run.show.id}" finished`);
|
|
206
|
+
this.emit();
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** Where every fixture is at wall-clock `now`. Null once a one-shot show
|
|
210
|
+
* has run past its end. */
|
|
211
|
+
private frameAt(run: Run, now: number): { step: number; looks: Map<string, LookColor> } | null {
|
|
212
|
+
const { show, snapshot } = run;
|
|
213
|
+
let e = now - run.startedAt;
|
|
214
|
+
let firstCycle = true;
|
|
215
|
+
if (show.loop) {
|
|
216
|
+
if (e >= show.cycleMs) { firstCycle = false; e = e % show.cycleMs; }
|
|
217
|
+
} else if (e >= show.cycleMs) {
|
|
218
|
+
return null;
|
|
219
|
+
}
|
|
220
|
+
// Locate the step and the position within its fade/hold.
|
|
221
|
+
let i = 0;
|
|
222
|
+
let t = 1;
|
|
223
|
+
let acc = 0;
|
|
224
|
+
for (; i < show.steps.length; i++) {
|
|
225
|
+
const st = show.steps[i];
|
|
226
|
+
const len = st.fadeMs + st.holdMs;
|
|
227
|
+
if (e < acc + len || i === show.steps.length - 1) {
|
|
228
|
+
const local = e - acc;
|
|
229
|
+
t = st.fadeMs > 0 ? Math.min(1, local / st.fadeMs) : 1;
|
|
230
|
+
break;
|
|
231
|
+
}
|
|
232
|
+
acc += len;
|
|
233
|
+
}
|
|
234
|
+
const st = show.steps[i];
|
|
235
|
+
const from = i > 0 ? show.steps[i - 1].targets
|
|
236
|
+
: firstCycle ? null : show.steps[show.steps.length - 1].targets;
|
|
237
|
+
const eased = st.ease === 'sine' ? (1 - Math.cos(Math.PI * t)) / 2 : t;
|
|
238
|
+
const looks = new Map<string, LookColor>();
|
|
239
|
+
for (const fx of show.fixtures) {
|
|
240
|
+
const start = snapshot.get(fx.id)!;
|
|
241
|
+
const a = from?.get(fx.id) ?? start;
|
|
242
|
+
const b = st.targets.get(fx.id) ?? start;
|
|
243
|
+
looks.set(fx.id, mixLook(a, b, eased));
|
|
244
|
+
}
|
|
245
|
+
return { step: i, looks };
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// ── LookColor ↔ HomeKitLightState ──────────────────────────────────────────
|
|
250
|
+
|
|
251
|
+
export function fromState(s: HomeKitLightState): LookColor {
|
|
252
|
+
const out: LookColor = { brightness: s.on ? s.brightness : 0 };
|
|
253
|
+
if (s.hue != null) out.hue = s.hue;
|
|
254
|
+
if (s.saturation != null) out.saturation = s.saturation;
|
|
255
|
+
if (s.colorTemperatureMireds != null) out.mireds = s.colorTemperatureMireds;
|
|
256
|
+
return out;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/** Shape a look for a specific fixture: only the characteristics its color
|
|
260
|
+
* model exposes, HomeKit-style on/brightness. A dark look parks the
|
|
261
|
+
* brightness at 100 so the next HomeKit "on" restores full (mirrors
|
|
262
|
+
* hsvcct.parse). `precise` keeps fractional values — the wire path
|
|
263
|
+
* renders 16-bit intensity, so fades stay smooth below 1 % steps; the
|
|
264
|
+
* registry (HomeKit) gets integers. */
|
|
265
|
+
export function toState(look: LookColor, fx: Fixture, precise = false): HomeKitLightState {
|
|
266
|
+
const chars = fx.profile.model.characteristics;
|
|
267
|
+
const r = precise ? (n: number): number => n : Math.round;
|
|
268
|
+
const on = look.brightness > 0.5;
|
|
269
|
+
const s: HomeKitLightState = {
|
|
270
|
+
on,
|
|
271
|
+
brightness: on ? Math.max(1, r(look.brightness)) : 100,
|
|
272
|
+
};
|
|
273
|
+
const colorMode = look.hue != null && (look.saturation ?? 0) > 0;
|
|
274
|
+
if (chars.includes('Hue') && look.hue != null) s.hue = r(look.hue);
|
|
275
|
+
if (chars.includes('Saturation')) s.saturation = r(look.saturation ?? 0);
|
|
276
|
+
if (chars.includes('ColorTemperature') && look.mireds != null && !colorMode) {
|
|
277
|
+
s.colorTemperatureMireds = r(look.mireds);
|
|
278
|
+
}
|
|
279
|
+
return s;
|
|
280
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// ShowSwitch — one HomeKit Switch per runnable show.
|
|
2
|
+
//
|
|
3
|
+
// On → engine.start(id): whatever show was running stops (its own switch
|
|
4
|
+
// flips off through the engine's change listener).
|
|
5
|
+
// Off → engine.stop(): lights freeze on the current frame.
|
|
6
|
+
// The switch also flips off by itself when a one-shot show finishes or a
|
|
7
|
+
// user touches one of the show's lights.
|
|
8
|
+
|
|
9
|
+
import type { PlatformAccessory, Service } from 'homebridge';
|
|
10
|
+
|
|
11
|
+
import type { DmxPlatform } from './platform.js';
|
|
12
|
+
import { Show } from './show/dsl.js';
|
|
13
|
+
import { ShowEngine } from './show/engine.js';
|
|
14
|
+
|
|
15
|
+
export class ShowSwitch {
|
|
16
|
+
private sw: Service;
|
|
17
|
+
|
|
18
|
+
constructor(
|
|
19
|
+
platform: DmxPlatform,
|
|
20
|
+
accessory: PlatformAccessory,
|
|
21
|
+
show: Show,
|
|
22
|
+
engine: ShowEngine,
|
|
23
|
+
) {
|
|
24
|
+
const C = platform.Characteristic;
|
|
25
|
+
const info = accessory.getService(platform.Service.AccessoryInformation);
|
|
26
|
+
info?.setCharacteristic(C.Manufacturer, 'DMX')
|
|
27
|
+
?.setCharacteristic(C.Model, `Show (${show.steps.length} steps, ${show.fixtures.length} fixtures)`)
|
|
28
|
+
?.setCharacteristic(C.SerialNumber, `show-${show.id}`);
|
|
29
|
+
|
|
30
|
+
this.sw =
|
|
31
|
+
accessory.getService(platform.Service.Switch)
|
|
32
|
+
?? accessory.addService(platform.Service.Switch, show.name);
|
|
33
|
+
this.sw.setCharacteristic(C.Name, show.name);
|
|
34
|
+
|
|
35
|
+
this.sw.getCharacteristic(C.On)
|
|
36
|
+
.onGet(() => engine.running() === show.id)
|
|
37
|
+
.onSet((v) => {
|
|
38
|
+
if (v) engine.start(show.id);
|
|
39
|
+
else if (engine.running() === show.id) engine.stop();
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
engine.onChange((runningId) => {
|
|
43
|
+
this.sw.updateCharacteristic(C.On, runningId === show.id);
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
}
|
package/src/stateRegistry.ts
CHANGED
|
@@ -13,7 +13,13 @@
|
|
|
13
13
|
|
|
14
14
|
import { HomeKitLightState } from './color/types.js';
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
/** Who made a change. Accessories write as 'homekit' (the default);
|
|
17
|
+
* the gateway read-back writes 'sync'; the show engine writes 'show'.
|
|
18
|
+
* The engine uses this to end a show when a user touches one of its
|
|
19
|
+
* lights without reacting to its own keyframes or to read-back. */
|
|
20
|
+
export type StateSource = 'homekit' | 'sync' | 'show';
|
|
21
|
+
|
|
22
|
+
export type StateChangeListener = (fixtureId: string, source: StateSource) => void;
|
|
17
23
|
|
|
18
24
|
const DEFAULT_STATE: HomeKitLightState = { on: false, brightness: 100 };
|
|
19
25
|
|
|
@@ -26,16 +32,16 @@ export class StateRegistry {
|
|
|
26
32
|
}
|
|
27
33
|
|
|
28
34
|
/** Replace a fixture's state and fire change listeners. */
|
|
29
|
-
set(id: string, state: HomeKitLightState): void {
|
|
35
|
+
set(id: string, state: HomeKitLightState, source: StateSource = 'homekit'): void {
|
|
30
36
|
this.states.set(id, { ...state });
|
|
31
|
-
for (const l of this.listeners) l(id);
|
|
37
|
+
for (const l of this.listeners) l(id, source);
|
|
32
38
|
}
|
|
33
39
|
|
|
34
40
|
/** Merge a partial update into a fixture's state, returning the new full
|
|
35
41
|
* state. Fires listeners. */
|
|
36
|
-
update(id: string, patch: Partial<HomeKitLightState
|
|
42
|
+
update(id: string, patch: Partial<HomeKitLightState>, source: StateSource = 'homekit'): HomeKitLightState {
|
|
37
43
|
const next: HomeKitLightState = { ...this.get(id), ...patch };
|
|
38
|
-
this.set(id, next);
|
|
44
|
+
this.set(id, next, source);
|
|
39
45
|
return next;
|
|
40
46
|
}
|
|
41
47
|
|
package/src/zoneAccessory.ts
CHANGED
|
@@ -22,7 +22,7 @@ import type {
|
|
|
22
22
|
} from 'homebridge';
|
|
23
23
|
|
|
24
24
|
import { Fixture, Zone } from './config.js';
|
|
25
|
-
import { HKCharacteristic, HomeKitLightState } from './color/types.js';
|
|
25
|
+
import { HKCharacteristic, HomeKitLightState, miredsToHueSat } from './color/types.js';
|
|
26
26
|
import { DmxController } from './dmxController.js';
|
|
27
27
|
import type { DmxPlatform } from './platform.js';
|
|
28
28
|
import { CHARACTERISTIC_UPDATE_DELAY_MS } from './settings.js';
|
|
@@ -65,17 +65,17 @@ export class ZoneFixture {
|
|
|
65
65
|
}
|
|
66
66
|
if (has('Hue')) {
|
|
67
67
|
this.bulb.getCharacteristic(C.Hue)
|
|
68
|
-
.onGet(() => this.
|
|
68
|
+
.onGet(() => this.display().hue)
|
|
69
69
|
.onSet((v) => this.applyToMembers({ hue: Number(v) }));
|
|
70
70
|
}
|
|
71
71
|
if (has('Saturation')) {
|
|
72
72
|
this.bulb.getCharacteristic(C.Saturation)
|
|
73
|
-
.onGet(() => this.
|
|
73
|
+
.onGet(() => this.display().saturation)
|
|
74
74
|
.onSet((v) => this.applyToMembers({ saturation: Number(v) }));
|
|
75
75
|
}
|
|
76
76
|
if (has('ColorTemperature')) {
|
|
77
77
|
this.bulb.getCharacteristic(C.ColorTemperature)
|
|
78
|
-
.onGet(() => this.
|
|
78
|
+
.onGet(() => this.display().ct)
|
|
79
79
|
.onSet((v) => this.applyToMembers({
|
|
80
80
|
colorTemperatureMireds: Number(v),
|
|
81
81
|
saturation: 0,
|
|
@@ -101,6 +101,25 @@ export class ZoneFixture {
|
|
|
101
101
|
};
|
|
102
102
|
}
|
|
103
103
|
|
|
104
|
+
/** HomeKit-facing view of the majority state — same dual-mode display
|
|
105
|
+
* convention as StickFixture.display() (HAP-NodeJS#618). */
|
|
106
|
+
private display(): { hue: number; saturation: number; ct: number; whiteMode: boolean } {
|
|
107
|
+
const s = this.majorityState();
|
|
108
|
+
if (!this.zone.characteristics.includes('ColorTemperature')
|
|
109
|
+
|| !this.zone.characteristics.includes('Hue')) {
|
|
110
|
+
return { hue: s.hue ?? 0, saturation: s.saturation ?? 0,
|
|
111
|
+
ct: s.colorTemperatureMireds ?? 200, whiteMode: (s.saturation ?? 0) === 0 };
|
|
112
|
+
}
|
|
113
|
+
if ((s.saturation ?? 0) === 0) {
|
|
114
|
+
const ct = s.colorTemperatureMireds ?? 200;
|
|
115
|
+
const rep = miredsToHueSat(ct);
|
|
116
|
+
return { hue: rep.hue, saturation: rep.saturation, ct, whiteMode: true };
|
|
117
|
+
}
|
|
118
|
+
const ctChar = this.bulb.getCharacteristic(this.platform.Characteristic.ColorTemperature);
|
|
119
|
+
const minCt = (ctChar.props.minValue as number | undefined) ?? 140;
|
|
120
|
+
return { hue: s.hue ?? 0, saturation: s.saturation!, ct: minCt, whiteMode: false };
|
|
121
|
+
}
|
|
122
|
+
|
|
104
123
|
/** A HomeKit set on the zone: build the new state from the current
|
|
105
124
|
* majority + the patch the user is applying, write it into every
|
|
106
125
|
* member, and trigger the controller after a small debounce so
|
|
@@ -154,23 +173,26 @@ export class ZoneFixture {
|
|
|
154
173
|
this.refreshPending = setTimeout(() => {
|
|
155
174
|
this.refreshPending = null;
|
|
156
175
|
const s = this.majorityState();
|
|
176
|
+
const d = this.display();
|
|
157
177
|
const C = this.platform.Characteristic;
|
|
158
178
|
this.bulb.updateCharacteristic(C.On, s.on);
|
|
159
179
|
if (this.zone.characteristics.includes('Brightness')) {
|
|
160
180
|
this.bulb.updateCharacteristic(C.Brightness, s.brightness);
|
|
161
181
|
}
|
|
162
|
-
if (this.zone.characteristics.includes('Hue')
|
|
163
|
-
this.bulb.updateCharacteristic(C.Hue,
|
|
182
|
+
if (this.zone.characteristics.includes('Hue')) {
|
|
183
|
+
this.bulb.updateCharacteristic(C.Hue, d.hue);
|
|
164
184
|
}
|
|
165
|
-
if (this.zone.characteristics.includes('Saturation')
|
|
166
|
-
this.bulb.updateCharacteristic(C.Saturation,
|
|
185
|
+
if (this.zone.characteristics.includes('Saturation')) {
|
|
186
|
+
this.bulb.updateCharacteristic(C.Saturation, d.saturation);
|
|
167
187
|
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
188
|
+
if (this.zone.characteristics.includes('ColorTemperature')) {
|
|
189
|
+
if (d.whiteMode) {
|
|
190
|
+
// Real CCT, pushed last so temperature wins the display-mode race.
|
|
191
|
+
this.bulb.updateCharacteristic(C.ColorTemperature, d.ct);
|
|
192
|
+
} else {
|
|
193
|
+
// Park CT at min silently (see StickFixture.pushFromRegistry).
|
|
194
|
+
this.bulb.getCharacteristic(C.ColorTemperature).value = d.ct;
|
|
195
|
+
}
|
|
174
196
|
}
|
|
175
197
|
}, CHARACTERISTIC_UPDATE_DELAY_MS);
|
|
176
198
|
}
|