@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
@@ -2,8 +2,11 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.normaliseCapabilities = normaliseCapabilities;
4
4
  exports.normaliseConfig = normaliseConfig;
5
+ exports.readinessEvidence = readinessEvidence;
5
6
  exports.transitionSource = transitionSource;
7
+ const settings_1 = require("./settings");
6
8
  const observation_time_1 = require("./observation-time");
9
+ const target_state_1 = require("./target-state");
7
10
  const ACTIONS = { activate: true, deactivate: true };
8
11
  const CONTROL_MODES = {
9
12
  command: true,
@@ -70,12 +73,14 @@ function normaliseCapabilities(value) {
70
73
  throw new Error("sources must not be empty");
71
74
  return sources;
72
75
  }
76
+ // Settings may be editor strings; missing, zero or unusable numbers take
77
+ // defaults.
73
78
  function normaliseConfig(config = {}) {
74
79
  return {
75
80
  sources: normaliseCapabilities(config.sources),
76
- pollMs: Math.max(250, Number(config.pollMs) || 2000),
77
- confirmationTimeoutMs: Math.max(1000, Number(config.confirmationTimeoutMs) || 15000),
78
- maxAttempts: Math.max(1, Math.floor(Number(config.maxAttempts) || 2))
81
+ pollMs: Math.max(250, (0, settings_1.numberSetting)(config.pollMs, "poll interval", 2000)),
82
+ confirmationTimeoutMs: Math.max(1000, (0, settings_1.numberSetting)(config.confirmationTimeoutMs, "confirmation timeout", 15000)),
83
+ maxAttempts: Math.max(1, Math.floor((0, settings_1.numberSetting)(config.maxAttempts, "max attempts", 2)))
79
84
  };
80
85
  }
81
86
  function initialState(saved = {}, sources = {}) {
@@ -100,12 +105,12 @@ function initialState(saved = {}, sources = {}) {
100
105
  : null,
101
106
  pending: saved.pending && typeof saved.pending === "object" ? { ...saved.pending } : null,
102
107
  failure: saved.failure && typeof saved.failure === "object" ? { ...saved.failure } : null,
103
- blockedGeneration: Number.isInteger(saved.blockedGeneration) ? saved.blockedGeneration : null,
104
108
  classification: saved.classification === "manual_override" ? "manual_override" : null,
105
109
  lastAttempt: Number.isInteger(saved.lastAttempt) && (saved.lastAttempt ?? -1) >= 0
106
110
  ? saved.lastAttempt
107
111
  : 0,
108
- fallbackUsed: saved.fallbackUsed === true
112
+ fallbackUsed: saved.fallbackUsed === true,
113
+ refreshAsked: saved.refreshAsked === true
109
114
  };
110
115
  }
111
116
  function commandFor(state, operation, attempt = 1) {
@@ -137,35 +142,34 @@ function descriptorFor(state, sources) {
137
142
  return null;
138
143
  return sources[state.desired.sourceId] ?? null;
139
144
  }
145
+ // What each kind of confirmation needs from an observation before the source
146
+ // counts as ready.
147
+ const READINESS = {
148
+ available: ({ available }) => available,
149
+ active: ({ active }) => active,
150
+ application: ({ active, application }) => active && application !== null,
151
+ playing: ({ active, playback }) => active && playback === "playing",
152
+ unavailable: () => false
153
+ };
140
154
  function readinessEvidence(observation, required) {
141
- const available = observation?.status === "active" || observation?.status === "available";
142
- const active = observation?.status === "active";
143
- const application = observation?.application ?? null;
144
- const playback = observation?.playback ?? "unknown";
145
- const satisfied = required === "available"
146
- ? available
147
- : required === "active"
148
- ? active
149
- : required === "application"
150
- ? active && application !== null
151
- : required === "playing"
152
- ? active && playback === "playing"
153
- : false;
154
- return { required, available, active, application, playback, satisfied };
155
+ const facts = {
156
+ available: observation?.status === "active" || observation?.status === "available",
157
+ active: observation?.status === "active",
158
+ application: observation?.application ?? null,
159
+ playback: observation?.playback ?? "unknown"
160
+ };
161
+ return { required, ...facts, satisfied: READINESS[required](facts) };
155
162
  }
156
163
  function targetEvent(state, now, sources) {
157
164
  if (!state.desired)
158
165
  return null;
159
166
  const readiness = readinessEvidence(state.observed, descriptorFor(state, sources)?.confirmation ?? "unavailable");
160
- const theatre = {
161
- kind: "target_state",
167
+ const envelope = (0, target_state_1.targetStateEvent)({
162
168
  target: "source",
163
169
  requestId: state.desired.requestId,
164
170
  generation: state.generation,
165
171
  status: targetStatus(state.phase),
166
- observedAt: state.observed?.observedAt ?? iso(now),
167
- sourceId: state.desired.sourceId,
168
- action: state.desired.action,
172
+ observedAt: state.observed?.confirmedAt ?? state.observed?.observedAt ?? iso(now),
169
173
  details: {
170
174
  observedStatus: state.observed?.status ?? null,
171
175
  readiness,
@@ -179,13 +183,11 @@ function targetEvent(state, now, sources) {
179
183
  retries: Math.max(0, state.lastAttempt - 1),
180
184
  fallbackUsed: state.fallbackUsed
181
185
  }
182
- }
183
- };
184
- if (state.failure)
185
- theatre.failure = { ...state.failure };
186
- if (state.classification)
187
- theatre.classification = state.classification;
188
- return { theatre };
186
+ },
187
+ classification: state.classification,
188
+ failure: state.failure
189
+ });
190
+ return { theatre: { ...envelope, sourceId: state.desired.sourceId, action: state.desired.action } };
189
191
  }
190
192
  function statusFor(state) {
191
193
  const sourceId = state.desired?.sourceId || "none";
@@ -224,7 +226,6 @@ function transitionSource(saved, msg, suppliedConfig = {}, nowValue = new Date()
224
226
  function fail(operation, reason) {
225
227
  state.pending = null;
226
228
  state.phase = "failed";
227
- state.blockedGeneration = state.generation;
228
229
  state.failure = { operation, reason, occurredAt: iso(now) };
229
230
  trace("source", "failed", reason, { sourceId: state.desired?.sourceId ?? null, operation });
230
231
  }
@@ -260,7 +261,6 @@ function transitionSource(saved, msg, suppliedConfig = {}, nowValue = new Date()
260
261
  if (observationSatisfies(state.observed)) {
261
262
  state.pending = null;
262
263
  state.failure = null;
263
- state.blockedGeneration = null;
264
264
  state.classification = null;
265
265
  state.phase = "ready";
266
266
  const readiness = readinessEvidence(state.observed, descriptor()?.confirmation ?? "unavailable");
@@ -319,9 +319,9 @@ function transitionSource(saved, msg, suppliedConfig = {}, nowValue = new Date()
319
319
  };
320
320
  state.generation = event.generation;
321
321
  state.observed = null;
322
+ state.refreshAsked = false;
322
323
  state.pending = null;
323
324
  state.failure = null;
324
- state.blockedGeneration = null;
325
325
  state.classification = null;
326
326
  state.lastAttempt = 0;
327
327
  state.fallbackUsed = false;
@@ -383,6 +383,11 @@ function transitionSource(saved, msg, suppliedConfig = {}, nowValue = new Date()
383
383
  application,
384
384
  playback
385
385
  };
386
+ // Theatre gets the time the report arrived, as from every other target:
387
+ // a quiet source repeats an unchanged time, and a device clock running
388
+ // behind would otherwise date it before the request it answers.
389
+ state.observed.confirmedAt = new Date(Math.max(bounded.ms, nowMs)).toISOString();
390
+ state.refreshAsked = false;
386
391
  reconcileObservation();
387
392
  }
388
393
  function acceptCommandResult(event) {
@@ -442,7 +447,17 @@ function transitionSource(saved, msg, suppliedConfig = {}, nowValue = new Date()
442
447
  else if (event.kind === "tick") {
443
448
  tick(event);
444
449
  }
450
+ else if (event.kind === "refresh" && state.desired && state.phase === "ready" && !state.pending &&
451
+ descriptor()?.confirmation !== "unavailable") {
452
+ // Re-read a settled source without putting it back into confirmation: it
453
+ // stays ready, and its target state follows the reading. A refresh that
454
+ // is never answered leaves it to the activity controller to report.
455
+ commands.push(commandFor(state, "observe"));
456
+ state.refreshAsked = true;
457
+ trace("refresh", "querying", null, { sourceId: state.desired.sourceId });
458
+ }
445
459
  else if (event.kind === "startup" || event.kind === "refresh") {
460
+ state.refreshAsked = false;
446
461
  if (state.desired) {
447
462
  if (state.desired.sourceId === null || descriptor()?.confirmation === "unavailable") {
448
463
  markUnavailable(state.desired.sourceId === null ? "unknown-active-source" : "unobservable-source");
@@ -469,7 +484,8 @@ function transitionSource(saved, msg, suppliedConfig = {}, nowValue = new Date()
469
484
  else {
470
485
  throw new Error("unknown source capability event kind");
471
486
  }
472
- const outputEvent = targetEvent(state, now, config.sources);
487
+ // A refresh of a settled source reports nothing until its reading arrives.
488
+ const outputEvent = event.kind === "refresh" && state.refreshAsked ? null : targetEvent(state, now, config.sources);
473
489
  if (outputEvent)
474
490
  events.push(outputEvent);
475
491
  return { state, commands, events, traces, status: statusFor(state) };
@@ -1,3 +1,4 @@
1
+ import type { TraceEntry } from "./bounded-trace";
1
2
  import { type WallInputConfig, type WallInputState } from "./wall-input";
2
3
  export interface SwitchPolicyConfig extends WallInputConfig {
3
4
  targets: string[];
@@ -38,16 +39,10 @@ export type SwitchPolicyEffect = {
38
39
  kind: "send";
39
40
  message: SwitchLightingIntent;
40
41
  };
41
- export interface SwitchPolicyTrace {
42
- event: string;
43
- outcome: string;
44
- reason: string | null;
45
- details: Record<string, unknown>;
46
- }
47
42
  export declare function createSwitchPolicyState(saved?: Partial<SwitchPolicyState>): SwitchPolicyState;
48
43
  export declare function transitionSwitchPolicy(saved: Partial<SwitchPolicyState> | undefined, input: SwitchPolicyInput, config: SwitchPolicyConfig, now?: number): {
49
44
  state: SwitchPolicyState;
50
45
  effects: SwitchPolicyEffect[];
51
- traces: SwitchPolicyTrace[];
46
+ traces: TraceEntry<Record<string, unknown>>[];
52
47
  status: import("./wall-input").WallInputStatus;
53
48
  };
@@ -0,0 +1,24 @@
1
+ export type TargetStatus = "discovering" | "transitioning" | "confirming" | "ready" | "waiting_for_source" | "failed" | "unavailable";
2
+ export declare function isTargetStatus(value: unknown): value is TargetStatus;
3
+ export declare function asTargetStatus(phase: string, otherwise: TargetStatus): TargetStatus;
4
+ export interface TargetStateEvent<Target extends string = string, Details = Record<string, unknown>, Failure = Record<string, unknown>> {
5
+ kind: "target_state";
6
+ target: Target;
7
+ requestId: string;
8
+ generation: number;
9
+ status: TargetStatus;
10
+ observedAt: string;
11
+ details: Details;
12
+ classification?: "manual_override";
13
+ failure?: Failure;
14
+ }
15
+ export declare function targetStateEvent<Target extends string, Details, Failure>(fields: {
16
+ target: Target;
17
+ requestId: string;
18
+ generation: number;
19
+ status: TargetStatus;
20
+ observedAt: string;
21
+ details: Details;
22
+ classification?: "manual_override" | null;
23
+ failure?: Failure | null;
24
+ }): TargetStateEvent<Target, Details, Failure>;
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isTargetStatus = isTargetStatus;
4
+ exports.asTargetStatus = asTargetStatus;
5
+ exports.targetStateEvent = targetStateEvent;
6
+ const TARGET_STATUSES = {
7
+ discovering: true,
8
+ transitioning: true,
9
+ confirming: true,
10
+ ready: true,
11
+ waiting_for_source: true,
12
+ failed: true,
13
+ unavailable: true
14
+ };
15
+ function isTargetStatus(value) {
16
+ return typeof value === "string" && Object.hasOwn(TARGET_STATUSES, value);
17
+ }
18
+ // An owner's phase when it is already a target status, or the given status.
19
+ function asTargetStatus(phase, otherwise) {
20
+ return isTargetStatus(phase) ? phase : otherwise;
21
+ }
22
+ function targetStateEvent(fields) {
23
+ const event = {
24
+ kind: "target_state",
25
+ target: fields.target,
26
+ requestId: fields.requestId,
27
+ generation: fields.generation,
28
+ status: fields.status,
29
+ observedAt: fields.observedAt,
30
+ details: fields.details
31
+ };
32
+ if (fields.classification)
33
+ event.classification = fields.classification;
34
+ if (fields.failure)
35
+ event.failure = { ...fields.failure };
36
+ return event;
37
+ }
38
+ //# sourceMappingURL=target-state.js.map
@@ -1,103 +1,8 @@
1
- export declare const TARGETS: readonly ["display", "avr", "source", "lighting", "backlighting"];
2
- export type TheatreTarget = typeof TARGETS[number];
3
- export type TargetStatus = "discovering" | "transitioning" | "confirming" | "ready" | "waiting_for_source" | "failed" | "unavailable";
4
- export type TargetClassification = "drift" | "manual_override";
5
- export type TheatreAvailability = "unknown" | "available" | "degraded" | "unavailable";
6
- export interface ActivitySourceProfile extends Record<string, unknown> {
7
- action: "activate" | "deactivate";
8
- readiness: "required" | "optional";
9
- sourceId?: string | null;
10
- }
11
- export interface ActivityLightingProfile extends Record<string, unknown> {
12
- profile: string;
13
- }
14
- export interface ActivityBacklightingProfile extends Record<string, unknown> {
15
- power: "on" | "off";
16
- }
17
- export type ActivityProfile = Record<TheatreTarget, Record<string, unknown>> & {
18
- source: ActivitySourceProfile;
19
- lighting: ActivityLightingProfile;
20
- backlighting: ActivityBacklightingProfile;
21
- };
22
- export type ActivityProfiles = Record<string, ActivityProfile>;
23
- export interface TheatreDesired {
24
- requestId: string;
25
- generation: number;
26
- activity: string;
27
- requestedAt: string;
28
- }
29
- export interface TargetFailure {
30
- operation?: string | null;
31
- reason?: string;
32
- occurredAt?: string;
33
- }
34
- export interface TargetState {
35
- status: TargetStatus;
36
- observedAt: string;
37
- classification: TargetClassification | null;
38
- failure: TargetFailure | null;
39
- details: Record<string, unknown> | null;
40
- }
41
- export interface TheatreFailure {
42
- target: TheatreTarget;
43
- operation: string | null;
44
- reason: string;
45
- occurredAt: string;
46
- }
47
- export interface TheatreTargetMetrics {
48
- attempts: number;
49
- retries: number;
50
- failures: number;
51
- manualOverrides: number;
52
- fallbackUses: number;
53
- fallbackUsed: boolean;
54
- lastConfirmationMs: number | null;
55
- observationAgeMs: number | null;
56
- availability: TheatreAvailability;
57
- }
58
- export interface TheatreMetrics {
59
- requests: number;
60
- ready: number;
61
- failed: number;
62
- superseded: number;
63
- manualOverrides: number;
64
- driftEvents: number;
65
- recoveriesAdopted: number;
66
- recoveriesSafeOff: number;
67
- lastTransitionMs: number | null;
68
- maxTransitionMs: number | null;
69
- lastOutcome: string | null;
70
- targets: Record<TheatreTarget, TheatreTargetMetrics>;
71
- }
72
- export interface RecoveryObservation {
73
- observedAt: string;
74
- values: Record<string, unknown>;
75
- }
76
- export type RecoveryObservations = Partial<Record<TheatreTarget, RecoveryObservation>>;
77
- export interface RefreshPending {
78
- sentAt: number;
79
- attempts: number;
80
- exhausted: boolean;
81
- }
82
- export interface TheatreState {
83
- desired: TheatreDesired | null;
84
- currentActivity: string;
85
- generation: number;
86
- lastProposedGeneration: number;
87
- phase: string;
88
- targets: Record<TheatreTarget, TargetState | null>;
89
- refreshPending: Record<TheatreTarget, RefreshPending | null>;
90
- failure: TheatreFailure | null;
91
- blockedGeneration: number | null;
92
- degraded: Array<{
93
- target: TheatreTarget;
94
- reason: string;
95
- }>;
96
- availability: TheatreAvailability;
97
- transitionStartedAt: string | null;
98
- transitionCompletedAt: string | null;
99
- metrics: TheatreMetrics;
100
- }
1
+ import type { TraceEntry } from "./bounded-trace";
2
+ import { type ActivityProfiles, type TheatreTarget } from "./theatre-profiles";
3
+ import { type RecoveryObservations, type TargetClassification, type TargetFailure, type TargetStatus, type TheatreState } from "./theatre-state";
4
+ export { TARGETS, normaliseActivityProfiles, type ActivityBacklightingProfile, type ActivityLightingProfile, type ActivityProfile, type ActivityProfiles, type ActivitySourceProfile, type TheatreTarget } from "./theatre-profiles";
5
+ export { createTheatreState, type RecoveryObservation, type RecoveryObservations, type RefreshPending, type TargetClassification, type TargetFailure, type TargetState, type TargetStatus, type TheatreAvailability, type TheatreDesired, type TheatreFailure, type TheatreMetrics, type TheatreSnapshot, type TheatreState, type TheatreTargetMetrics } from "./theatre-state";
101
6
  export interface TheatreConfig {
102
7
  area: string;
103
8
  freshnessMs: number;
@@ -141,8 +46,6 @@ export interface TargetCommand extends Record<string, unknown> {
141
46
  requestId: string | null;
142
47
  generation: number;
143
48
  }
144
- export declare function normaliseActivityProfiles(value: unknown): ActivityProfiles;
145
- export declare function createTheatreState(saved?: Partial<TheatreState>, activityProfiles?: ActivityProfiles): TheatreState;
146
49
  export declare function normaliseConfig(config?: Partial<TheatreConfig> & {
147
50
  activityProfiles?: unknown;
148
51
  }): TheatreConfig;
@@ -152,12 +55,7 @@ export declare function transitionTheatre(saved: Partial<TheatreState> | undefin
152
55
  state: TheatreState;
153
56
  commands: TargetCommand[];
154
57
  events: TheatreOutput[];
155
- traces: {
156
- event: string;
157
- outcome: string;
158
- reason: string | null;
159
- details: Record<string, unknown>;
160
- }[];
58
+ traces: TraceEntry<Record<string, unknown>>[];
161
59
  status: {
162
60
  phase: string;
163
61
  text: string;