@avantics/node-red-home-automation 2.2.1 → 3.0.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 (85) hide show
  1. package/README.md +76 -89
  2. package/contract-examples/room-lighting.mjs +12 -9
  3. package/dist/lib/activity-lighting-controller.d.ts +13 -25
  4. package/dist/lib/activity-lighting-controller.js +15 -11
  5. package/dist/lib/avr-controller.d.ts +17 -28
  6. package/dist/lib/avr-controller.js +165 -116
  7. package/dist/lib/backlighting-controller.d.ts +12 -25
  8. package/dist/lib/backlighting-controller.js +11 -18
  9. package/dist/lib/bounded-trace.d.ts +6 -0
  10. package/dist/lib/circadian-schedule.d.ts +11 -0
  11. package/dist/lib/circadian-schedule.js +63 -0
  12. package/dist/lib/circadian.d.ts +6 -0
  13. package/dist/lib/circadian.js +15 -0
  14. package/dist/lib/command-echo.d.ts +20 -0
  15. package/dist/lib/command-echo.js +82 -0
  16. package/dist/lib/cover-controller.d.ts +15 -28
  17. package/dist/lib/cover-controller.js +6 -8
  18. package/dist/lib/denon-protocol.d.ts +29 -0
  19. package/dist/lib/denon-protocol.js +76 -0
  20. package/dist/lib/display-mode-controller.d.ts +11 -6
  21. package/dist/lib/display-mode-controller.js +90 -39
  22. package/dist/lib/light-styling.d.ts +11 -0
  23. package/dist/lib/light-styling.js +15 -0
  24. package/dist/lib/motion-policy.d.ts +12 -22
  25. package/dist/lib/motion-policy.js +53 -56
  26. package/dist/lib/occupancy-fusion.d.ts +8 -12
  27. package/dist/lib/occupancy-fusion.js +3 -2
  28. package/dist/lib/room-controller.d.ts +13 -38
  29. package/dist/lib/room-controller.js +85 -180
  30. package/dist/lib/room-lighting-discovery.d.ts +9 -0
  31. package/dist/lib/room-lighting-discovery.js +45 -0
  32. package/dist/lib/settings.d.ts +5 -0
  33. package/dist/lib/settings.js +68 -0
  34. package/dist/lib/source-capability-controller.d.ts +32 -35
  35. package/dist/lib/source-capability-controller.js +51 -35
  36. package/dist/lib/switch-policy.d.ts +2 -7
  37. package/dist/lib/target-state.d.ts +24 -0
  38. package/dist/lib/target-state.js +38 -0
  39. package/dist/lib/theatre-activity-controller.d.ts +6 -108
  40. package/dist/lib/theatre-activity-controller.js +67 -318
  41. package/dist/lib/theatre-ha-discovery.d.ts +14 -0
  42. package/dist/lib/theatre-ha-discovery.js +163 -0
  43. package/dist/lib/theatre-metrics.d.ts +4 -0
  44. package/dist/lib/theatre-metrics.js +66 -0
  45. package/dist/lib/theatre-profiles.d.ts +23 -0
  46. package/dist/lib/theatre-profiles.js +73 -0
  47. package/dist/lib/theatre-refresh.d.ts +20 -0
  48. package/dist/lib/theatre-refresh.js +46 -0
  49. package/dist/lib/theatre-slo.js +7 -9
  50. package/dist/lib/theatre-state-contract.d.ts +7 -19
  51. package/dist/lib/theatre-state-contract.js +33 -169
  52. package/dist/lib/theatre-state.d.ts +110 -0
  53. package/dist/lib/theatre-state.js +139 -0
  54. package/dist/lib/wall-input.d.ts +2 -7
  55. package/dist/lib/z2m-light.d.ts +16 -0
  56. package/dist/lib/z2m-light.js +36 -0
  57. package/dist/nodes/activity-lighting-controller.html +5 -3
  58. package/dist/nodes/activity-lighting-controller.js +21 -52
  59. package/dist/nodes/adapter.d.ts +15 -0
  60. package/dist/nodes/adapter.js +60 -0
  61. package/dist/nodes/avr-controller.html +13 -4
  62. package/dist/nodes/avr-controller.js +23 -62
  63. package/dist/nodes/backlighting-controller.html +1 -1
  64. package/dist/nodes/backlighting-controller.js +25 -47
  65. package/dist/nodes/display-mode-controller.html +4 -4
  66. package/dist/nodes/display-mode-controller.js +22 -59
  67. package/dist/nodes/motion-policy.html +23 -10
  68. package/dist/nodes/motion-policy.js +14 -135
  69. package/dist/nodes/occupancy-fusion.html +2 -2
  70. package/dist/nodes/occupancy-fusion.js +23 -29
  71. package/dist/nodes/room-controller.html +92 -34
  72. package/dist/nodes/room-controller.js +46 -64
  73. package/dist/nodes/source-capability-controller.html +4 -13
  74. package/dist/nodes/source-capability-controller.js +17 -52
  75. package/dist/nodes/switch-policy.html +3 -3
  76. package/dist/nodes/switch-policy.js +23 -42
  77. package/dist/nodes/theatre-activity-controller.html +5 -3
  78. package/dist/nodes/theatre-activity-controller.js +23 -43
  79. package/dist/nodes/theatre-state-contract.html +55 -22
  80. package/dist/nodes/theatre-state-contract.js +30 -41
  81. package/examples/Home theatre activity.json +307 -43
  82. package/examples/Home theatre equipment.json +4 -4
  83. package/examples/Room lighting.json +15 -17
  84. package/package.json +3 -7
  85. package/THIRD-PARTY-NOTICES.md +0 -218
@@ -1,5 +1,5 @@
1
- import { type WallInputConfig, type WallInputState } from "./wall-input";
2
- export interface MotionConfig extends WallInputConfig {
1
+ import type { TraceEntry } from "./bounded-trace";
2
+ export interface MotionConfig {
3
3
  defaultIlluminanceCutoff: number;
4
4
  defaultDelaySeconds: number;
5
5
  onTargets: string[];
@@ -10,18 +10,16 @@ export interface MotionState {
10
10
  illuminance: number | null;
11
11
  illuminanceCutoff: number;
12
12
  delaySeconds: number;
13
- wallInput: WallInputState;
13
+ illuminanceCutoffSetting: number | null;
14
+ delaySecondsSetting: number | null;
14
15
  offTimerGeneration: number;
15
16
  offTimerDueAt: number | null;
17
+ offTimerArmedAt: number | null;
16
18
  }
17
19
  export interface MotionInput {
18
20
  source: string;
19
- observedAt?: unknown;
20
- baseline?: boolean;
21
21
  payload?: unknown;
22
22
  generation?: number;
23
- switchAt?: number;
24
- retain?: boolean;
25
23
  }
26
24
  export interface LightingIntent {
27
25
  lighting: {
@@ -38,35 +36,27 @@ export type MotionEffect = {
38
36
  kind: "schedule-off-timer";
39
37
  delayMs: number;
40
38
  generation: number;
41
- } | {
42
- kind: "cancel-switch-timer";
43
- } | {
44
- kind: "schedule-switch-timer";
45
- delayMs: number;
46
- generation: number;
47
- switchAt: number;
48
39
  } | {
49
40
  kind: "send";
50
41
  message: LightingIntent;
51
42
  };
52
- export interface MotionTrace {
53
- event: string;
54
- outcome: string;
55
- reason: string | null;
56
- details: Record<string, unknown>;
57
- }
58
43
  export declare function booleanOn(value: unknown): boolean;
44
+ export declare function normaliseMotionConfig(raw: {
45
+ onTargets?: unknown;
46
+ offTargets?: unknown;
47
+ defaultDelaySeconds?: unknown;
48
+ defaultIlluminanceCutoff?: unknown;
49
+ }): MotionConfig;
59
50
  export declare function createMotionState(config: MotionConfig, saved?: Partial<MotionState>): MotionState;
60
51
  export declare function lightingIntent(targets: string[], state: string, source: string, extra?: Record<string, unknown>): LightingIntent;
61
52
  export declare function transitionMotion(saved: Partial<MotionState> | undefined, input: MotionInput, config: MotionConfig, globalEnabled: boolean, now?: number): {
62
53
  state: MotionState;
63
54
  globalEnabled: boolean;
64
55
  effects: MotionEffect[];
65
- traces: MotionTrace[];
56
+ traces: TraceEntry<Record<string, unknown>>[];
66
57
  status: {
67
58
  enabled: boolean;
68
59
  illuminance: number | null;
69
60
  delaySeconds: number;
70
- switch: import("./wall-input").WallInputStatus;
71
61
  };
72
62
  };
@@ -1,10 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.booleanOn = booleanOn;
4
+ exports.normaliseMotionConfig = normaliseMotionConfig;
4
5
  exports.createMotionState = createMotionState;
5
6
  exports.lightingIntent = lightingIntent;
6
7
  exports.transitionMotion = transitionMotion;
7
- const wall_input_1 = require("./wall-input");
8
+ const settings_1 = require("./settings");
8
9
  function booleanOn(value) {
9
10
  return value === true || (typeof value === "string" && value.toLowerCase() === "on");
10
11
  }
@@ -12,22 +13,48 @@ function finiteNumber(value) {
12
13
  const number = Number(value);
13
14
  return Number.isFinite(number) ? number : null;
14
15
  }
16
+ // A JSON array of target names from the editor, or the fallback when the
17
+ // setting is absent from an older saved flow.
18
+ function targetList(value, label, fallback) {
19
+ const parsed = (0, settings_1.jsonSetting)(value, label, fallback);
20
+ if (!Array.isArray(parsed) || parsed.some((item) => typeof item !== "string")) {
21
+ throw new Error(`${label} must be a JSON array of strings`);
22
+ }
23
+ return parsed;
24
+ }
25
+ // The motion policy's settings from a node's editor values: on targets
26
+ // default to ["main"] and off targets to the on targets; the vacancy delay
27
+ // (120 s) and illuminance cutoff (10) take their defaults only when absent
28
+ // from an older saved flow. Throws on settings it cannot use.
29
+ function normaliseMotionConfig(raw) {
30
+ const onTargets = targetList(raw.onTargets, "on targets", ["main"]);
31
+ return {
32
+ onTargets,
33
+ offTargets: targetList(raw.offTargets, "off targets", onTargets),
34
+ defaultDelaySeconds: (0, settings_1.numberSetting)(raw.defaultDelaySeconds, "vacancy delay", 120),
35
+ defaultIlluminanceCutoff: (0, settings_1.numberSetting)(raw.defaultIlluminanceCutoff, "illuminance cutoff", 10)
36
+ };
37
+ }
38
+ // Saved wall-switch state from earlier versions (wallInput and the older
39
+ // switch* fields) is not read.
15
40
  function createMotionState(config, saved = {}) {
16
- const legacy = saved;
17
41
  return {
18
42
  areaEnabled: saved.areaEnabled !== false,
19
43
  illuminance: saved.illuminance ?? null,
20
- illuminanceCutoff: saved.illuminanceCutoff ?? config.defaultIlluminanceCutoff,
21
- delaySeconds: saved.delaySeconds ?? config.defaultDelaySeconds,
22
- wallInput: (0, wall_input_1.createWallInputState)(saved.wallInput ?? {
23
- baselineKnown: legacy.switchStateKnown,
24
- value: legacy.switchState === true,
25
- generation: legacy.switchGeneration,
26
- edgeAt: legacy.switchAt,
27
- timerDueAt: legacy.switchTimerDueAt
28
- }),
44
+ // A saved value stands only while the setting it was based on is
45
+ // unchanged; state saved before this rule recorded none, so the setting
46
+ // applies.
47
+ illuminanceCutoff: saved.illuminanceCutoffSetting === config.defaultIlluminanceCutoff && saved.illuminanceCutoff !== undefined
48
+ ? saved.illuminanceCutoff
49
+ : config.defaultIlluminanceCutoff,
50
+ delaySeconds: saved.delaySecondsSetting === config.defaultDelaySeconds && saved.delaySeconds !== undefined
51
+ ? saved.delaySeconds
52
+ : config.defaultDelaySeconds,
53
+ illuminanceCutoffSetting: config.defaultIlluminanceCutoff,
54
+ delaySecondsSetting: config.defaultDelaySeconds,
29
55
  offTimerGeneration: Number(saved.offTimerGeneration) || 0,
30
- offTimerDueAt: Number(saved.offTimerDueAt) || null
56
+ offTimerDueAt: Number(saved.offTimerDueAt) || null,
57
+ offTimerArmedAt: Number(saved.offTimerArmedAt) || null
31
58
  };
32
59
  }
33
60
  function lightingIntent(targets, state, source, extra = {}) {
@@ -56,6 +83,7 @@ function transitionMotion(saved, input, config, globalEnabled, now = Date.now())
56
83
  const wasActive = Number(state.offTimerDueAt) > now;
57
84
  state.offTimerGeneration += 1;
58
85
  state.offTimerDueAt = null;
86
+ state.offTimerArmedAt = null;
59
87
  effects.push({ kind: "cancel-off-timer" });
60
88
  if (wasActive && reason)
61
89
  trace("off-timer", "cancelled", reason);
@@ -63,6 +91,7 @@ function transitionMotion(saved, input, config, globalEnabled, now = Date.now())
63
91
  function scheduleOffTimer() {
64
92
  cancelOffTimer("rescheduled");
65
93
  const delayMs = Math.max(0, state.delaySeconds) * 1000;
94
+ state.offTimerArmedAt = now;
66
95
  state.offTimerDueAt = now + delayMs;
67
96
  effects.push({
68
97
  kind: "schedule-off-timer",
@@ -104,9 +133,10 @@ function transitionMotion(saved, input, config, globalEnabled, now = Date.now())
104
133
  cancelOffTimer("motion-disabled");
105
134
  }
106
135
  else if (source === "delay") {
107
- const minutes = finiteNumber(payload);
108
- if (minutes !== null)
109
- state.delaySeconds = minutes * 60;
136
+ // Seconds, the same unit as the vacancy setting.
137
+ const seconds = finiteNumber(payload);
138
+ if (seconds !== null)
139
+ state.delaySeconds = seconds;
110
140
  }
111
141
  else if (source === "illuminance-cut-off") {
112
142
  const cutoff = finiteNumber(payload);
@@ -192,48 +222,16 @@ function transitionMotion(saved, input, config, globalEnabled, now = Date.now())
192
222
  });
193
223
  }
194
224
  else {
225
+ // vacantSince lets the room controller keep a light a person set after
226
+ // the room went vacant.
227
+ const vacantSince = new Date(state.offTimerArmedAt ?? now).toISOString();
195
228
  state.offTimerDueAt = null;
196
- emitIntent(lightingIntent(config.offTargets, "OFF", "motion"), "off-timer");
229
+ state.offTimerArmedAt = null;
230
+ emitIntent(lightingIntent(config.offTargets, "OFF", "motion", { vacantSince }), "off-timer");
197
231
  }
198
232
  }
199
- else if (source === "switch-startup" ||
200
- source === "switch" ||
201
- source === "switch-availability" ||
202
- source === "switch-timer") {
203
- const wallInput = (0, wall_input_1.transitionWallInput)(state.wallInput, source === "switch-startup"
204
- ? { kind: "startup" }
205
- : source === "switch"
206
- ? {
207
- kind: "observation",
208
- payload,
209
- retain: input.retain,
210
- baseline: input.baseline,
211
- observedAt: input.observedAt
212
- }
213
- : source === "switch-availability"
214
- ? { kind: "availability", payload }
215
- : {
216
- kind: "timer",
217
- generation: input.generation,
218
- edgeAt: input.switchAt
219
- }, config, now);
220
- state.wallInput = wallInput.state;
221
- effects.push(...wallInput.effects);
222
- traces.push(...wallInput.traces);
223
- const startedGesture = source === "switch" && (wallInput.gesture === "double" ||
224
- wallInput.effects.some((effect) => effect.kind === "schedule-switch-timer"));
225
- if (startedGesture)
226
- cancelOffTimer("wall-switch");
227
- if (wallInput.gesture === "single") {
228
- emitIntent(lightingIntent(config.onTargets, "TOGGLE", "wall", { person: true }), "switch");
229
- }
230
- else if (wallInput.gesture === "double") {
231
- emitIntent(lightingIntent(config.onTargets, "ON", "wall", {
232
- person: true,
233
- manual: true,
234
- values: { brightness: 254 }
235
- }), "switch");
236
- }
233
+ else if (source.startsWith("switch")) {
234
+ trace("message", "ignored", "wall switches belong to the switch policy", { source });
237
235
  }
238
236
  return {
239
237
  state,
@@ -243,8 +241,7 @@ function transitionMotion(saved, input, config, globalEnabled, now = Date.now())
243
241
  status: {
244
242
  enabled: enabled(),
245
243
  illuminance: state.illuminance,
246
- delaySeconds: state.delaySeconds,
247
- switch: (0, wall_input_1.wallInputStatus)(state.wallInput, config.switchFreshnessMs, now)
244
+ delaySeconds: state.delaySeconds
248
245
  }
249
246
  };
250
247
  }
@@ -1,3 +1,4 @@
1
+ import type { TraceEntry } from "./bounded-trace";
1
2
  export type OccupancySource = "motion" | "presence";
2
3
  export interface OccupancyState {
3
4
  motionKnown: boolean;
@@ -26,19 +27,14 @@ export interface OccupancyOutput {
26
27
  };
27
28
  };
28
29
  }
29
- export interface OccupancyTrace {
30
- event: string;
31
- outcome: string;
32
- reason: string | null;
33
- details: {
34
- active: boolean | null;
35
- occupied: boolean;
36
- sources: {
37
- motion: boolean | null;
38
- presence: boolean | null;
39
- };
30
+ export type OccupancyTrace = TraceEntry<{
31
+ active: boolean | null;
32
+ occupied: boolean;
33
+ sources: {
34
+ motion: boolean | null;
35
+ presence: boolean | null;
40
36
  };
41
- }
37
+ }>;
42
38
  export declare function booleanState(value: unknown, source: OccupancySource): boolean | null;
43
39
  export declare function createOccupancyState(saved?: Partial<OccupancyState>): OccupancyState;
44
40
  export declare function transitionOccupancy(saved: Partial<OccupancyState> | undefined, input: OccupancyInput, config: OccupancyConfig): {
@@ -89,8 +89,9 @@ function transitionOccupancy(saved, input, config) {
89
89
  }
90
90
  }
91
91
  else if (usesPresence && state.presenceKnown && state.presenceActive) {
92
- state.occupied = true;
93
- trace("motion", "held", "presence-active");
92
+ // Presence only holds occupancy that motion established; it never
93
+ // establishes it, so the state (and what was last emitted) is unchanged.
94
+ trace("motion", state.occupied ? "held" : "unchanged", state.occupied ? "presence-active" : "motion-required");
94
95
  }
95
96
  else if (!wasKnown || wasActive || state.occupied) {
96
97
  emit(false, "all-sources-inactive");
@@ -1,3 +1,8 @@
1
+ import type { TraceEntry } from "./bounded-trace";
2
+ import type { CircadianLighting } from "./circadian";
3
+ import { type LightCommand, type LightReport } from "./z2m-light";
4
+ import { type ExpectedCommand } from "./command-echo";
5
+ export { fingerprint, parsePayload, type ExpectedCommand } from "./command-echo";
1
6
  export interface RoomTarget {
2
7
  id: string;
3
8
  stateTopic: string;
@@ -8,29 +13,15 @@ export interface RoomConfig {
8
13
  displayName: string;
9
14
  targets: RoomTarget[];
10
15
  initialSleep: boolean;
11
- latitude: number;
12
- longitude: number;
13
- sleepBrightnessPct: number;
14
- maxBrightnessPct: number;
15
- minBrightnessPct: number;
16
- sleepColorTempKelvin: number;
17
- minColorTempKelvin: number;
18
- maxColorTempKelvin: number;
16
+ publishState?: boolean;
17
+ discovery?: boolean;
19
18
  refreshTransitionSeconds: number;
20
19
  initialTransitionSeconds: number;
20
+ lighting: CircadianLighting;
21
21
  }
22
- export interface RoomObservation {
23
- on: boolean;
24
- brightness: unknown;
25
- color_temp: unknown;
22
+ export interface RoomObservation extends LightReport {
26
23
  observedAt: string;
27
24
  }
28
- export interface ExpectedCommand {
29
- fingerprint: string;
30
- expires: number;
31
- on?: boolean;
32
- echoed?: boolean;
33
- }
34
25
  export type TargetControlState = "in-control" | "overridden" | "manual";
35
26
  export type TargetControl = {
36
27
  state: "manual";
@@ -45,6 +36,7 @@ export interface RoomState {
45
36
  expected: Record<string, ExpectedCommand[]>;
46
37
  circadianEnabled: boolean;
47
38
  sleeping: boolean;
39
+ personAt: Record<string, number>;
48
40
  }
49
41
  export interface LightingEvent {
50
42
  kind?: string;
@@ -62,6 +54,7 @@ export interface LightingEvent {
62
54
  refresh?: boolean;
63
55
  retained?: boolean;
64
56
  enabled?: boolean;
57
+ vacantSince?: string;
65
58
  }
66
59
  export interface RoomMessage {
67
60
  lighting?: LightingEvent;
@@ -79,32 +72,14 @@ export interface RoomOutput {
79
72
  source: string;
80
73
  };
81
74
  }
82
- export declare function parsePayload(payload: unknown): unknown;
83
- export declare function fingerprint(payload: unknown): string;
84
75
  export declare function createRoomState(config: RoomConfig, saved?: Partial<RoomState> & {
85
76
  manual?: Record<string, boolean>;
86
77
  }): RoomState;
87
- export declare function sunPosition(now: Date, config: Pick<RoomConfig, "latitude" | "longitude">): number;
88
- export declare function adaptivePayload(lightState: string, isRefresh: boolean, sleeping: boolean, now: Date, config: RoomConfig): {
89
- state: string;
90
- brightness?: undefined;
91
- color_temp?: undefined;
92
- transition?: undefined;
93
- } | {
94
- state: string;
95
- brightness: number;
96
- color_temp: number;
97
- transition: number;
98
- };
78
+ export declare function adaptivePayload(lightState: string, isRefresh: boolean, sleeping: boolean, now: Date, config: RoomConfig): LightCommand;
99
79
  export declare function transitionRoom(saved: Partial<RoomState> | undefined, msg: RoomMessage, config: RoomConfig, nowValue?: Date | string | number): {
100
80
  state: RoomState;
101
81
  outputs: RoomOutput[];
102
- traces: {
103
- event: string;
104
- outcome: string;
105
- reason: string | null;
106
- details: Record<string, unknown>;
107
- }[];
82
+ traces: TraceEntry<Record<string, unknown>>[];
108
83
  status: {
109
84
  circadianEnabled: boolean;
110
85
  sleeping: boolean;