@avantics/node-red-home-automation 6.0.0 → 6.2.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.
Files changed (75) hide show
  1. package/README.md +276 -48
  2. package/dist/lib/cover-publishing.js +20 -6
  3. package/dist/lib/home-assistant-setting.d.ts +23 -0
  4. package/dist/lib/home-assistant-setting.js +39 -0
  5. package/dist/lib/motion-policy.d.ts +1 -0
  6. package/dist/lib/motion-policy.js +21 -1
  7. package/dist/lib/room-controller.d.ts +4 -0
  8. package/dist/lib/room-controller.js +122 -14
  9. package/dist/lib/room-holds.d.ts +34 -0
  10. package/dist/lib/room-holds.js +66 -0
  11. package/dist/lib/room-publishing.d.ts +1 -0
  12. package/dist/lib/room-publishing.js +11 -0
  13. package/dist/lib/scene-publishing.d.ts +30 -0
  14. package/dist/lib/scene-publishing.js +63 -0
  15. package/dist/lib/scene.d.ts +61 -0
  16. package/dist/lib/scene.js +170 -0
  17. package/dist/lib/shelly-cover.js +5 -0
  18. package/dist/lib/shelly-relay-light.d.ts +24 -0
  19. package/dist/lib/shelly-relay-light.js +93 -0
  20. package/dist/lib/shelly-switch.d.ts +8 -0
  21. package/dist/lib/shelly-switch.js +37 -8
  22. package/dist/lib/theatre-activity-controller.js +4 -0
  23. package/dist/lib/theatre-metrics.js +6 -4
  24. package/dist/lib/theatre-slo.js +3 -0
  25. package/dist/lib/theatre-state-contract.d.ts +4 -2
  26. package/dist/lib/theatre-state-contract.js +51 -8
  27. package/dist/lib/theatre-state.js +3 -0
  28. package/dist/nodes/activity-lighting-controller.html +1 -1
  29. package/dist/nodes/adapter.d.ts +5 -1
  30. package/dist/nodes/adapter.js +35 -2
  31. package/dist/nodes/avr-controller.html +86 -20
  32. package/dist/nodes/avr-controller.js +23 -11
  33. package/dist/nodes/circadian-policy.html +88 -19
  34. package/dist/nodes/circadian-policy.js +30 -11
  35. package/dist/nodes/cover-controller.html +91 -24
  36. package/dist/nodes/cover-controller.js +25 -12
  37. package/dist/nodes/device-side.d.ts +20 -0
  38. package/dist/nodes/device-side.js +72 -0
  39. package/dist/nodes/home-assistant-discovery.d.ts +26 -0
  40. package/dist/nodes/home-assistant-discovery.js +64 -0
  41. package/dist/nodes/house-schedule-mqtt.html +74 -4
  42. package/dist/nodes/house-schedule-mqtt.js +22 -8
  43. package/dist/nodes/motion-policy.html +91 -21
  44. package/dist/nodes/motion-policy.js +44 -11
  45. package/dist/nodes/mqtt-broker-link.d.ts +41 -0
  46. package/dist/nodes/mqtt-broker-link.js +91 -0
  47. package/dist/nodes/occupancy-fusion.html +87 -19
  48. package/dist/nodes/occupancy-fusion.js +23 -11
  49. package/dist/nodes/publishing-side.d.ts +20 -0
  50. package/dist/nodes/publishing-side.js +66 -0
  51. package/dist/nodes/room-controller.html +128 -43
  52. package/dist/nodes/room-controller.js +63 -16
  53. package/dist/nodes/scene.d.ts +3 -0
  54. package/dist/nodes/scene.html +226 -0
  55. package/dist/nodes/scene.js +121 -0
  56. package/dist/nodes/shelly-cover.html +46 -2
  57. package/dist/nodes/shelly-cover.js +19 -4
  58. package/dist/nodes/shelly-switch.html +80 -6
  59. package/dist/nodes/shelly-switch.js +32 -7
  60. package/dist/nodes/theatre-state-contract.html +76 -8
  61. package/dist/nodes/theatre-state-contract.js +33 -14
  62. package/dist/nodes/z2m-cover.html +46 -2
  63. package/dist/nodes/z2m-cover.js +21 -4
  64. package/dist/nodes/z2m-light.html +50 -2
  65. package/dist/nodes/z2m-light.js +26 -6
  66. package/examples/Cover control.json +38 -102
  67. package/examples/Cover pair.json +36 -3
  68. package/examples/Home theatre activity.json +46 -102
  69. package/examples/Home theatre equipment.json +34 -2
  70. package/examples/House schedule.json +58 -80
  71. package/examples/Room lighting.json +45 -55
  72. package/examples/Scene.json +215 -0
  73. package/examples/Shelly cover.json +36 -116
  74. package/examples/Shelly switch.json +52 -148
  75. package/package.json +2 -1
@@ -50,7 +50,8 @@ function createMotionState(config, saved = {}) {
50
50
  delaySecondsSetting: config.defaultDelaySeconds,
51
51
  offTimerGeneration: Number(saved.offTimerGeneration) || 0,
52
52
  offTimerDueAt: Number(saved.offTimerDueAt) || null,
53
- offTimerArmedAt: Number(saved.offTimerArmedAt) || null
53
+ offTimerArmedAt: Number(saved.offTimerArmedAt) || null,
54
+ vacantSince: Number(saved.vacantSince) || null
54
55
  };
55
56
  }
56
57
  function lightingIntent(targets, state, source, extra = {}) {
@@ -69,6 +70,9 @@ function transitionMotion(saved, input, config, globalEnabled, now = Date.now())
69
70
  const effects = [];
70
71
  const traces = [];
71
72
  let nextGlobalEnabled = globalEnabled !== false;
73
+ // Whether motion could act before this input, counting a pause that has
74
+ // just run out, so its running out resumes motion like a release.
75
+ const wasEnabled = state.areaEnabled && nextGlobalEnabled && state.pausedBy.length === 0;
72
76
  // A pause past its time counts as released, even if its owner never said.
73
77
  for (const pause of state.pausedBy.filter(({ until }) => until <= now)) {
74
78
  traces.push({ event: "pause", outcome: "expired", reason: null, details: { owner: pause.owner } });
@@ -182,6 +186,7 @@ function transitionMotion(saved, input, config, globalEnabled, now = Date.now())
182
186
  : {};
183
187
  const occupied = payloadRecord.occupied === true;
184
188
  if (occupied) {
189
+ state.vacantSince = null;
185
190
  cancelOffTimer("occupancy-detected");
186
191
  const darkEnough = state.illuminance === null || state.illuminance <= state.illuminanceCutoff;
187
192
  const shouldEmit = enabled() && darkEnough;
@@ -199,6 +204,7 @@ function transitionMotion(saved, input, config, globalEnabled, now = Date.now())
199
204
  });
200
205
  }
201
206
  else {
207
+ state.vacantSince = now;
202
208
  if (enabled())
203
209
  scheduleOffTimer();
204
210
  else
@@ -251,9 +257,14 @@ function transitionMotion(saved, input, config, globalEnabled, now = Date.now())
251
257
  const vacantSince = new Date(state.offTimerArmedAt ?? now).toISOString();
252
258
  state.offTimerDueAt = null;
253
259
  state.offTimerArmedAt = null;
260
+ state.vacantSince = null;
254
261
  emitIntent(lightingIntent(config.offTargets, "OFF", "motion", { vacantSince }), "off-timer");
255
262
  }
256
263
  }
264
+ else if (source === "pause-check") {
265
+ // The node's own wake-up as a pause runs out: dropped above, and motion
266
+ // resumes below.
267
+ }
257
268
  else if (source === "cold-start") {
258
269
  // Started with nothing saved: a first deploy, or a redeploy or restart
259
270
  // without a context store, when a running off-timer was lost. Whether the
@@ -267,6 +278,15 @@ function transitionMotion(saved, input, config, globalEnabled, now = Date.now())
267
278
  else if (source.startsWith("switch")) {
268
279
  trace("message", "ignored", "wall switches belong to the switch policy", { source });
269
280
  }
281
+ // Motion resumed (a pause released or run out, or enabled again) in a room
282
+ // that went vacant meanwhile: the vacancy delay starts now, and the off
283
+ // intent names when the room went vacant.
284
+ if (!wasEnabled && enabled() && state.vacantSince !== null && state.offTimerDueAt === null) {
285
+ const vacantSince = state.vacantSince;
286
+ scheduleOffTimer();
287
+ state.offTimerArmedAt = vacantSince;
288
+ trace("off-timer", "scheduled", "resumed-vacant", { vacantSince: new Date(vacantSince).toISOString() });
289
+ }
270
290
  return {
271
291
  state,
272
292
  globalEnabled: nextGlobalEnabled,
@@ -1,6 +1,7 @@
1
1
  import type { TraceEntry } from "./bounded-trace";
2
2
  import type { LightStyle } from "./light-styling";
3
3
  import { type LightField, type LightRoomMessage } from "./light-contract";
4
+ import { type LightHold } from "./room-holds";
4
5
  import { type ExpectedCommand } from "./command-echo";
5
6
  export { fingerprint, parsePayload, type ExpectedCommand } from "./command-echo";
6
7
  export interface RoomTarget {
@@ -33,6 +34,7 @@ export type TargetControl = {
33
34
  state: "overridden";
34
35
  owner: string;
35
36
  };
37
+ export type { HeldSpec, LightHold } from "./room-holds";
36
38
  export declare const REMOVED_LIGHT_UNITS = "removed-light-units";
37
39
  export declare const INVALID_LIGHT_VALUES = "invalid-light-values";
38
40
  export declare const ON_OFF_ONLY = "on-off-only";
@@ -44,6 +46,7 @@ export interface RoomState {
44
46
  observed: Record<string, RoomObservation>;
45
47
  desired: Record<string, boolean>;
46
48
  control: Record<string, TargetControl>;
49
+ holds: Record<string, LightHold>;
47
50
  expected: Record<string, ExpectedCommand[]>;
48
51
  style: RoomStyle | null;
49
52
  personAt: Record<string, number>;
@@ -54,6 +57,7 @@ export interface RoomState {
54
57
  }
55
58
  export interface LightingEvent {
56
59
  kind?: string;
60
+ area?: string;
57
61
  targets?: string[];
58
62
  target?: string;
59
63
  state?: string;
@@ -8,6 +8,8 @@ exports.intentValues = intentValues;
8
8
  exports.transitionRoom = transitionRoom;
9
9
  const light_contract_1 = require("./light-contract");
10
10
  const z2m_light_1 = require("./z2m-light");
11
+ const topic_names_1 = require("./topic-names");
12
+ const room_holds_1 = require("./room-holds");
11
13
  const command_echo_1 = require("./command-echo");
12
14
  var command_echo_2 = require("./command-echo");
13
15
  Object.defineProperty(exports, "fingerprint", { enumerable: true, get: function () { return command_echo_2.fingerprint; } });
@@ -66,10 +68,12 @@ function savedControl(saved) {
66
68
  .map(([target]) => [target, { state: "manual" }]));
67
69
  }
68
70
  function createRoomState(config, saved = {}) {
71
+ const control = savedControl(saved);
69
72
  return {
70
73
  observed: { ...(saved.observed || {}) },
71
74
  desired: { ...(saved.desired || {}) },
72
- control: savedControl(saved),
75
+ control,
76
+ holds: (0, room_holds_1.savedHolds)(saved.holds, (target) => control[target]?.state === "overridden"),
73
77
  expected: Object.fromEntries(Object.entries(saved.expected || {}).map(([target, entries]) => [target, entries.map((entry) => ({ ...entry }))])),
74
78
  style: savedStyle(saved.style),
75
79
  personAt: Object.fromEntries(Object.entries(saved.personAt || {})
@@ -159,6 +163,9 @@ function transitionRoom(saved, msg, config, nowValue = new Date()) {
159
163
  return state.control[targetId]?.state ?? "in-control";
160
164
  }
161
165
  function setControl(targetId, next, reason) {
166
+ // A light no owner holds has no owners to remember.
167
+ if (next?.state !== "overridden")
168
+ delete state.holds[targetId];
162
169
  const previous = state.control[targetId] ?? null;
163
170
  if (JSON.stringify(previous) === JSON.stringify(next))
164
171
  return;
@@ -174,6 +181,72 @@ function transitionRoom(saved, msg, config, nowValue = new Date()) {
174
181
  previous: previous?.state ?? "in-control"
175
182
  });
176
183
  }
184
+ // An overridden light's owners. A light held under state saved before 6.1
185
+ // has its one owner, with nothing known of what it was before.
186
+ function holdOf(targetId) {
187
+ const entry = state.control[targetId];
188
+ state.holds[targetId] ??= entry?.state === "overridden" ? (0, room_holds_1.legacyHold)(entry.owner) : { owners: [], before: null };
189
+ return state.holds[targetId];
190
+ }
191
+ // Whether the owner on top holds the light off.
192
+ function heldOff(targetId) {
193
+ return controlOf(targetId) === "overridden" && (0, room_holds_1.heldOff)(state.holds[targetId]);
194
+ }
195
+ // An owner taking a light, or asking again. The first owner records what
196
+ // the light was; a newer one goes on top. True when the owner is beneath
197
+ // a newer one: its request is remembered for when the newer lets go, and
198
+ // the light is left as it is.
199
+ function takeHold(targetId, owner, wasOn) {
200
+ const entry = state.control[targetId];
201
+ if (entry?.state !== "overridden") {
202
+ const observed = state.observed[targetId];
203
+ state.holds[targetId] = (0, room_holds_1.newHold)(owner, {
204
+ control: entry?.state === "manual" ? "manual" : "in-control",
205
+ on: wasOn,
206
+ ...(typeof observed?.brightnessPct === "number" ? { brightnessPct: observed.brightnessPct } : {}),
207
+ ...(typeof observed?.colorTempKelvin === "number" ? { colorTempKelvin: observed.colorTempKelvin } : {})
208
+ });
209
+ setControl(targetId, { state: "overridden", owner }, "owner-intent");
210
+ return false;
211
+ }
212
+ const added = (0, room_holds_1.addOwner)(holdOf(targetId), owner);
213
+ if (added === "pushed")
214
+ setControl(targetId, { state: "overridden", owner }, "owner-intent");
215
+ return added === "beneath";
216
+ }
217
+ // The last owner let go: the light goes back to what it was. One the room
218
+ // was running is the room's again, on or off as it was, in the style in
219
+ // force; one that was a person's gets their values back and stays theirs.
220
+ function restoreHeld(targetId, before, source) {
221
+ setControl(targetId, before?.control === "manual" ? { state: "manual" } : null, "released");
222
+ told(targetId);
223
+ const on = intendedOn(targetId);
224
+ if (before && !before.on) {
225
+ if (!on)
226
+ return;
227
+ state.desired[targetId] = false;
228
+ command(targetId, { state: "OFF" }, source);
229
+ }
230
+ else if (before?.control === "manual") {
231
+ state.desired[targetId] = true;
232
+ command(targetId, onOffOnly(targetId) ? { state: "ON" } : {
233
+ state: "ON",
234
+ ...(before.brightnessPct !== undefined ? { brightnessPct: before.brightnessPct } : {}),
235
+ ...(before.colorTempKelvin !== undefined ? { colorTempKelvin: before.colorTempKelvin } : {}),
236
+ transitionSeconds: config.initialTransitionSeconds
237
+ }, source);
238
+ }
239
+ else if (on) {
240
+ // Back in the room's style, over the style's own transition.
241
+ const style = state.style ? styled(targetId, state.style.transitionSeconds) : null;
242
+ if (style)
243
+ command(targetId, style, source);
244
+ }
245
+ else if (before) {
246
+ state.desired[targetId] = true;
247
+ command(targetId, styled(targetId, config.initialTransitionSeconds) ?? { state: "ON" }, source);
248
+ }
249
+ }
177
250
  function told(targetId) {
178
251
  state.untold = state.untold.filter((id) => id !== targetId);
179
252
  }
@@ -272,18 +345,24 @@ function transitionRoom(saved, msg, config, nowValue = new Date()) {
272
345
  continue;
273
346
  if (event.preserveManual === true && controlOf(targetId) === "manual")
274
347
  continue;
275
- if (resolvedState === "OFF") {
348
+ // An owner holds a light on or off; a newer owner goes on top.
349
+ const beneath = owner ? takeHold(targetId, owner, wasOn) : false;
350
+ if (owner) {
351
+ // Held: set below.
352
+ }
353
+ else if (resolvedState === "OFF") {
276
354
  // An off light has no style to protect: it returns to the room controller.
277
355
  setControl(targetId, null, "off");
278
356
  }
279
- else if (owner) {
280
- setControl(targetId, { state: "overridden", owner }, "owner-intent");
281
- }
282
357
  else if (event.manual === true) {
283
358
  // Only a person's change makes a light manual: since 6.0, an intent's
284
359
  // values alone don't.
285
360
  setControl(targetId, { state: "manual" }, "manual-intent");
286
361
  }
362
+ else if (person && resolvedState === "ON" && heldOff(targetId)) {
363
+ // A person switching on a light an owner holds off: theirs now.
364
+ setControl(targetId, { state: "manual" }, "person-changed");
365
+ }
287
366
  let spec;
288
367
  if (resolvedState === "OFF") {
289
368
  spec = { state: "OFF" };
@@ -314,6 +393,13 @@ function transitionRoom(saved, msg, config, nowValue = new Date()) {
314
393
  // otherwise just on.
315
394
  spec = (controlOf(targetId) === "in-control" ? styled(targetId, config.initialTransitionSeconds) : null) ?? { state: "ON" };
316
395
  }
396
+ if (owner) {
397
+ (0, room_holds_1.setOwnerSpec)(holdOf(targetId), owner, spec);
398
+ if (beneath) {
399
+ trace("intent", "held", "beneath-owner", { target: targetId, owner, top: state.control[targetId].owner });
400
+ continue;
401
+ }
402
+ }
317
403
  state.desired[targetId] = resolvedState === "ON";
318
404
  told(targetId);
319
405
  sent.push(command(targetId, spec, event.source || "intent"));
@@ -358,6 +444,9 @@ function transitionRoom(saved, msg, config, nowValue = new Date()) {
358
444
  state.desired[target.id] = nextOn;
359
445
  if (!nextOn)
360
446
  setControl(target.id, null, "observed-off");
447
+ // Switched on while an owner holds it off: a person's now.
448
+ else if (heldOff(target.id))
449
+ setControl(target.id, { state: "manual" }, "person-changed");
361
450
  }
362
451
  }
363
452
  else if (reading?.kind === "echo-changed") {
@@ -394,6 +483,10 @@ function transitionRoom(saved, msg, config, nowValue = new Date()) {
394
483
  if (reading.setsValues === true && !onOffOnly(target.id)) {
395
484
  setControl(target.id, { state: "manual" }, "external-values");
396
485
  }
486
+ else if (heldOff(target.id)) {
487
+ // Switched on while an owner holds it off: a person's now.
488
+ setControl(target.id, { state: "manual" }, "person-changed");
489
+ }
397
490
  else if (currentStyle() && !onOffOnly(target.id) && controlOf(target.id) === "in-control") {
398
491
  // At most one restyle per light per echo window, whatever comes
399
492
  // back: a bare ON delivered twice (two subscriptions, or a QoS
@@ -454,20 +547,35 @@ function transitionRoom(saved, msg, config, nowValue = new Date()) {
454
547
  restyleLights(next.source);
455
548
  }
456
549
  }
550
+ else if ((event.kind === "intent" || event.kind === "release") && typeof event.area === "string" && (0, topic_names_1.areaId)(event.area) !== (0, topic_names_1.areaId)(config.area)) {
551
+ // For another room.
552
+ trace(event.kind, "ignored", "other-area", { area: event.area, source: event.source || event.kind });
553
+ }
457
554
  else if (event.kind === "release") {
458
- // Hand overridden lights back; an owner name releases only that owner's lights.
555
+ // An owner name lets go of that owner's lights; none lets go of every
556
+ // owner's. A light goes to the owner beneath, with what it asked for, or
557
+ // with none left back to what it was before the first.
459
558
  const targets = Array.isArray(event.targets) ? event.targets : config.targets.map((target) => target.id);
460
559
  const owner = typeof event.owner === "string" && event.owner ? event.owner : null;
560
+ const source = event.source || "release";
461
561
  for (const targetId of targets) {
462
- const entry = state.control[targetId];
463
- if (!targetById[targetId] || entry?.state !== "overridden" || (owner && entry.owner !== owner))
562
+ if (!targetById[targetId] || state.control[targetId]?.state !== "overridden")
464
563
  continue;
465
- setControl(targetId, null, "released");
466
- told(targetId);
467
- // Back in the room's style, over the style's own transition.
468
- const style = intendedOn(targetId) && state.style ? styled(targetId, state.style.transitionSeconds) : null;
469
- if (style)
470
- command(targetId, style, event.source || "release");
564
+ const released = (0, room_holds_1.releaseOwner)(holdOf(targetId), owner);
565
+ if (released.kind === "dropped") {
566
+ trace("release", "dropped", "beneath-owner", { target: targetId, owner });
567
+ }
568
+ else if (released.kind === "next") {
569
+ setControl(targetId, { state: "overridden", owner: released.owner }, "released-to-owner");
570
+ told(targetId);
571
+ if (released.spec) {
572
+ state.desired[targetId] = released.spec.state === "ON";
573
+ command(targetId, released.spec, source);
574
+ }
575
+ }
576
+ else if (released.kind === "restore") {
577
+ restoreHeld(targetId, released.before, source);
578
+ }
471
579
  }
472
580
  }
473
581
  else if (event.kind === "intent") {
@@ -0,0 +1,34 @@
1
+ import type { LightCommandMessage } from "./light-contract";
2
+ export type HeldSpec = Pick<LightCommandMessage, "state" | "brightnessPct" | "colorTempKelvin" | "transitionSeconds" | "extra">;
3
+ export interface HeldFrom {
4
+ control: "in-control" | "manual";
5
+ on: boolean;
6
+ brightnessPct?: number;
7
+ colorTempKelvin?: number;
8
+ }
9
+ export interface LightHold {
10
+ owners: Array<{
11
+ owner: string;
12
+ spec: HeldSpec | null;
13
+ }>;
14
+ before: HeldFrom | null;
15
+ }
16
+ export type Released = {
17
+ kind: "none";
18
+ } | {
19
+ kind: "dropped";
20
+ } | {
21
+ kind: "next";
22
+ owner: string;
23
+ spec: HeldSpec | null;
24
+ } | {
25
+ kind: "restore";
26
+ before: HeldFrom | null;
27
+ };
28
+ export declare function newHold(owner: string, before: HeldFrom): LightHold;
29
+ export declare function legacyHold(owner: string): LightHold;
30
+ export declare function savedHolds(saved: unknown, overridden: (target: string) => boolean): Record<string, LightHold>;
31
+ export declare function addOwner(hold: LightHold, owner: string): "top" | "beneath" | "pushed";
32
+ export declare function setOwnerSpec(hold: LightHold, owner: string, spec: HeldSpec): void;
33
+ export declare function heldOff(hold: LightHold | undefined): boolean;
34
+ export declare function releaseOwner(hold: LightHold, owner: string | null): Released;
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.newHold = newHold;
4
+ exports.legacyHold = legacyHold;
5
+ exports.savedHolds = savedHolds;
6
+ exports.addOwner = addOwner;
7
+ exports.setOwnerSpec = setOwnerSpec;
8
+ exports.heldOff = heldOff;
9
+ exports.releaseOwner = releaseOwner;
10
+ function newHold(owner, before) {
11
+ return { owners: [{ owner, spec: null }], before };
12
+ }
13
+ // The hold of a light overridden under state saved before 6.1: its one
14
+ // owner, with nothing known of what it was before.
15
+ function legacyHold(owner) {
16
+ return { owners: [{ owner, spec: null }], before: null };
17
+ }
18
+ function savedHolds(saved, overridden) {
19
+ if (!saved || typeof saved !== "object")
20
+ return {};
21
+ return Object.fromEntries(Object.entries(saved)
22
+ .filter(([target, hold]) => overridden(target) && Array.isArray(hold?.owners) && hold.owners.length > 0)
23
+ .map(([target, hold]) => [target, {
24
+ owners: hold.owners.map((entry) => ({ owner: String(entry.owner), spec: entry.spec ? { ...entry.spec } : null })),
25
+ before: hold.before ? { ...hold.before } : null
26
+ }]));
27
+ }
28
+ // An owner asking for a light already held: "top" when it already holds it,
29
+ // "beneath" when it is under a newer owner (its request is kept for when
30
+ // the newer lets go, and the light is left as it is), "pushed" when it is new
31
+ // and now on top.
32
+ function addOwner(hold, owner) {
33
+ const index = hold.owners.findIndex((entry) => entry.owner === owner);
34
+ if (index === hold.owners.length - 1)
35
+ return "top";
36
+ if (index >= 0)
37
+ return "beneath";
38
+ hold.owners.push({ owner, spec: null });
39
+ return "pushed";
40
+ }
41
+ function setOwnerSpec(hold, owner, spec) {
42
+ const entry = hold.owners.find((held) => held.owner === owner);
43
+ if (entry)
44
+ entry.spec = { ...spec, ...(spec.extra ? { extra: { ...spec.extra } } : {}) };
45
+ }
46
+ // Whether the owner on top holds the light off.
47
+ function heldOff(hold) {
48
+ return hold?.owners.at(-1)?.spec?.state === "OFF";
49
+ }
50
+ // Lets go of one owner, or of every owner (null).
51
+ function releaseOwner(hold, owner) {
52
+ const index = owner === null ? 0 : hold.owners.findIndex((entry) => entry.owner === owner);
53
+ if (index < 0)
54
+ return { kind: "none" };
55
+ if (owner !== null && index < hold.owners.length - 1) {
56
+ hold.owners.splice(index, 1);
57
+ return { kind: "dropped" };
58
+ }
59
+ if (owner !== null && hold.owners.length > 1) {
60
+ hold.owners.pop();
61
+ const next = hold.owners.at(-1);
62
+ return { kind: "next", owner: next.owner, spec: next.spec ? { ...next.spec } : null };
63
+ }
64
+ return { kind: "restore", before: hold.before };
65
+ }
66
+ //# sourceMappingURL=room-holds.js.map
@@ -18,6 +18,7 @@ export declare function withoutCircadianState(recorded: readonly string[]): stri
18
18
  export declare function roomPublishesState(config: PublishSettings): boolean;
19
19
  export declare function roomPublishesDiscovery(config: PublishSettings): boolean;
20
20
  export declare function roomStartingOutputs(config: PublishingRoomConfig): RoomOutput[];
21
+ export declare function roomAnnouncement(config: PublishingRoomConfig, saved: Partial<RoomState> | undefined): RoomOutput[];
21
22
  export declare function roomStoppingOutputs(config: PublishSettings): RoomOutput[];
22
23
  export declare function publishRoom(saved: Partial<RoomState> | undefined, msg: RoomMessage, config: PublishingRoomConfig, now?: Date | string | number): {
23
24
  state: RoomState;
@@ -5,6 +5,7 @@ exports.withoutCircadianState = withoutCircadianState;
5
5
  exports.roomPublishesState = roomPublishesState;
6
6
  exports.roomPublishesDiscovery = roomPublishesDiscovery;
7
7
  exports.roomStartingOutputs = roomStartingOutputs;
8
+ exports.roomAnnouncement = roomAnnouncement;
8
9
  exports.roomStoppingOutputs = roomStoppingOutputs;
9
10
  exports.publishRoom = publishRoom;
10
11
  // The room's MQTT and Home Assistant side, as the theatre state contract is
@@ -85,6 +86,16 @@ function roomStartingOutputs(config) {
85
86
  function roomStartupOutputs(config) {
86
87
  return [...oldCircadianClears(config), ...roomDiscoveryOutputs(config), ...(roomPublishesState(config) ? [(0, room_lighting_discovery_1.roomAvailabilityMessage)(config.area, true)] : [])];
87
88
  }
89
+ // Everything the room announces when its broker connects, or Home
90
+ // Assistant comes back: as at the startup message, then every light's
91
+ // control state, so Home Assistant has all of it again.
92
+ function roomAnnouncement(config, saved) {
93
+ const state = (0, room_controller_1.createRoomState)(config, saved);
94
+ return [
95
+ ...roomStartupOutputs(config),
96
+ ...(roomPublishesState(config) ? config.targets.map((target) => controlMessage(config, state, target.id)) : [])
97
+ ];
98
+ }
88
99
  function roomStoppingOutputs(config) {
89
100
  return roomPublishesState(config) ? [(0, room_lighting_discovery_1.roomAvailabilityMessage)(config.area, false)] : [];
90
101
  }
@@ -0,0 +1,30 @@
1
+ import type { TraceEntry } from "./bounded-trace";
2
+ import { type OptInPublishingConfig, type RetainedText } from "./publishing-node";
3
+ export interface ScenePublishingConfig extends OptInPublishingConfig {
4
+ name: string;
5
+ }
6
+ export declare const sceneStateTopic: (config: {
7
+ area: string;
8
+ name: string;
9
+ }) => string;
10
+ export declare const sceneCommandTopic: (config: {
11
+ area: string;
12
+ name: string;
13
+ }) => string;
14
+ export declare const sceneAvailabilityTopic: (config: {
15
+ area: string;
16
+ name: string;
17
+ }) => string;
18
+ export declare function sceneDiscovery(config: ScenePublishingConfig): RetainedText[];
19
+ export declare function sceneStateOutputs(config: ScenePublishingConfig, active: boolean): RetainedText[];
20
+ export declare const sceneStartingOutputs: (config: ScenePublishingConfig, state: boolean) => RetainedText[];
21
+ export declare const sceneStoppingOutputs: (config: ScenePublishingConfig) => RetainedText[];
22
+ export declare const sceneRetainedTopics: (config: ScenePublishingConfig) => string[];
23
+ export declare function sceneInbound(msg: {
24
+ topic?: unknown;
25
+ payload?: unknown;
26
+ retain?: unknown;
27
+ }, config: ScenePublishingConfig): {
28
+ kind: "start" | "end" | null;
29
+ traces: TraceEntry[];
30
+ } | null;
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.sceneRetainedTopics = exports.sceneStoppingOutputs = exports.sceneStartingOutputs = exports.sceneAvailabilityTopic = exports.sceneCommandTopic = exports.sceneStateTopic = void 0;
4
+ exports.sceneDiscovery = sceneDiscovery;
5
+ exports.sceneStateOutputs = sceneStateOutputs;
6
+ exports.sceneInbound = sceneInbound;
7
+ const publishing_node_1 = require("./publishing-node");
8
+ const topic_names_1 = require("./topic-names");
9
+ function sceneTopic(config, path) {
10
+ return (0, topic_names_1.homeTopic)(config.area, "scene", (0, topic_names_1.areaId)(config.name), path);
11
+ }
12
+ const sceneStateTopic = (config) => sceneTopic(config, "state");
13
+ exports.sceneStateTopic = sceneStateTopic;
14
+ const sceneCommandTopic = (config) => sceneTopic(config, "set");
15
+ exports.sceneCommandTopic = sceneCommandTopic;
16
+ const sceneAvailabilityTopic = (config) => sceneTopic(config, "availability");
17
+ exports.sceneAvailabilityTopic = sceneAvailabilityTopic;
18
+ function sceneDiscovery(config) {
19
+ return [
20
+ (0, publishing_node_1.discoveryEntry)(config, { kind: "scene_" + (0, topic_names_1.areaId)(config.name), label: "Scene " + config.name, model: "Scene" }, (0, exports.sceneAvailabilityTopic)(config), {
21
+ component: "switch",
22
+ id: "scene_" + (0, topic_names_1.areaId)(config.name),
23
+ name: config.name,
24
+ fields: {
25
+ icon: "mdi:palette",
26
+ command_topic: (0, exports.sceneCommandTopic)(config),
27
+ state_topic: (0, exports.sceneStateTopic)(config),
28
+ payload_on: "ON",
29
+ payload_off: "OFF",
30
+ optimistic: false
31
+ }
32
+ })
33
+ ];
34
+ }
35
+ function sceneStateOutputs(config, active) {
36
+ return (0, publishing_node_1.publishesState)(config) ? [(0, publishing_node_1.retainedText)((0, exports.sceneStateTopic)(config), active ? "ON" : "OFF")] : [];
37
+ }
38
+ const scene = (0, publishing_node_1.publishingNode)({
39
+ entries: sceneDiscovery,
40
+ availabilityTopic: (_area, config) => (0, exports.sceneAvailabilityTopic)(config),
41
+ stateTopics: (config) => [(0, exports.sceneStateTopic)(config)],
42
+ stateOutputs: sceneStateOutputs
43
+ });
44
+ exports.sceneStartingOutputs = scene.starting;
45
+ exports.sceneStoppingOutputs = scene.stopping;
46
+ exports.sceneRetainedTopics = scene.retainedTopics;
47
+ // Home Assistant's ON or OFF on the scene's set topic, as a start or an end.
48
+ // Read only while it publishes; retained messages and anything else are
49
+ // ignored with a trace. Anything on another topic passes through (null).
50
+ function sceneInbound(msg, config) {
51
+ if (!(0, publishing_node_1.publishesState)(config) || msg.topic !== (0, exports.sceneCommandTopic)(config))
52
+ return null;
53
+ const ignored = (reason, details = {}) => ({ kind: null, traces: [{ event: "command-set", outcome: "ignored", reason, details }] });
54
+ if (msg.retain === true)
55
+ return ignored("retained-message");
56
+ const text = typeof msg.payload === "string" ? msg.payload.trim().toUpperCase() : "";
57
+ if (text === "ON")
58
+ return { kind: "start", traces: [] };
59
+ if (text === "OFF")
60
+ return { kind: "end", traces: [] };
61
+ return ignored("not-on-or-off", { payload: typeof msg.payload === "string" ? msg.payload : typeof msg.payload });
62
+ }
63
+ //# sourceMappingURL=scene-publishing.js.map
@@ -0,0 +1,61 @@
1
+ import type { TraceEntry } from "./bounded-trace";
2
+ export interface SceneLight {
3
+ area: string;
4
+ target: string;
5
+ state: "ON" | "OFF";
6
+ brightnessPct?: number;
7
+ colorTempKelvin?: number;
8
+ }
9
+ export interface SceneConfig {
10
+ owner: string;
11
+ lights: SceneLight[];
12
+ safetyMs: number;
13
+ }
14
+ export declare const DEFAULT_SAFETY_HOURS = 4;
15
+ export declare const RENEW_MS = 60000;
16
+ export interface SceneState {
17
+ active: boolean;
18
+ owner: string | null;
19
+ lights: Array<{
20
+ area: string;
21
+ target: string;
22
+ }>;
23
+ startedAt: number | null;
24
+ endsAt: number | null;
25
+ ended: {
26
+ by: string;
27
+ at: number;
28
+ } | null;
29
+ }
30
+ export interface SceneEvent {
31
+ kind?: unknown;
32
+ by?: unknown;
33
+ }
34
+ export interface SceneLighting {
35
+ lighting: Record<string, unknown>;
36
+ }
37
+ export interface SceneMotion {
38
+ source: "pause";
39
+ payload: boolean;
40
+ owner: string;
41
+ }
42
+ export declare function normaliseSceneConfig(raw: {
43
+ owner: string;
44
+ lights?: unknown;
45
+ safetyHours?: unknown;
46
+ }): SceneConfig;
47
+ export declare function createSceneState(saved?: Partial<SceneState>): SceneState;
48
+ export declare function transitionScene(saved: Partial<SceneState> | undefined, msg: {
49
+ scene?: SceneEvent;
50
+ }, config: SceneConfig, nowValue?: Date | string | number): {
51
+ state: SceneState;
52
+ lighting: SceneLighting[];
53
+ motion: SceneMotion[];
54
+ traces: TraceEntry<Record<string, unknown>>[];
55
+ status: {
56
+ fill: string;
57
+ shape: string;
58
+ text: string;
59
+ };
60
+ };
61
+ export declare function sceneTickAt(state: SceneState, now: number): number | null;