@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,31 +1,26 @@
1
1
  "use strict";
2
- const bounded_trace_1 = require("../lib/bounded-trace");
3
2
  const activity_lighting_controller_1 = require("../lib/activity-lighting-controller");
4
- function number(value, fallback) {
5
- const parsed = Number(value);
6
- return Number.isFinite(parsed) ? parsed : fallback;
7
- }
3
+ const adapter_1 = require("./adapter");
4
+ const settings_1 = require("../lib/settings");
8
5
  const registerActivityLightingController = (RED) => {
9
6
  function HomeActivityLightingController(config) {
10
7
  RED.nodes.createNode(this, config);
11
8
  const node = this;
12
9
  const context = node.context();
13
10
  let timer;
14
- let profiles;
11
+ let controllerConfig;
15
12
  try {
16
- profiles = (0, activity_lighting_controller_1.normaliseLightingProfiles)(JSON.parse(config.profiles || "{}"));
13
+ controllerConfig = {
14
+ profiles: (0, activity_lighting_controller_1.normaliseLightingProfiles)((0, settings_1.jsonSetting)(config.profiles, "profiles", {})),
15
+ owner: (0, settings_1.textSetting)(config.owner, "owner", "activity"),
16
+ confirmationTimeoutMs: (0, settings_1.numberSetting)(config.confirmationTimeoutMs, "confirmation timeout", 15000),
17
+ maxAttempts: (0, settings_1.numberSetting)(config.maxAttempts, "max attempts", 2)
18
+ };
17
19
  }
18
20
  catch (error) {
19
- node.status({ fill: "red", shape: "ring", text: "invalid profile JSON" });
20
- node.error(error);
21
+ (0, adapter_1.invalidConfig)(node, error);
21
22
  return;
22
23
  }
23
- const controllerConfig = {
24
- profiles,
25
- owner: config.owner || "activity",
26
- confirmationTimeoutMs: number(config.confirmationTimeoutMs, 15000),
27
- maxAttempts: number(config.maxAttempts, 2)
28
- };
29
24
  function updateStatus(status) {
30
25
  const appearances = {
31
26
  idle: { fill: "grey", shape: "dot" },
@@ -43,24 +38,12 @@ const registerActivityLightingController = (RED) => {
43
38
  if (!state.pending?.deadlineAt)
44
39
  return;
45
40
  const delay = Math.max(0, state.pending.deadlineAt - Date.now());
46
- timer = setTimeout(() => {
47
- try {
48
- process({ activityLighting: { kind: "tick", generation: state.generation } }, node.send.bind(node));
49
- }
50
- catch (error) {
51
- node.error(error);
52
- }
53
- }, delay);
41
+ timer = setTimeout((0, adapter_1.guarded)(node, () => {
42
+ process({ activityLighting: { kind: "tick", generation: state.generation } }, node.send.bind(node));
43
+ }), delay);
54
44
  }
55
45
  function process(msg, send) {
56
- const now = new Date();
57
- const saved = context.get("state");
58
- const result = (0, activity_lighting_controller_1.transitionActivityLighting)(saved, msg, controllerConfig, now);
59
- context.set("state", result.state);
60
- (0, bounded_trace_1.appendTrace)(context, {
61
- area: config.area || "lounge",
62
- component: "activity-lighting-controller"
63
- }, result.traces, now);
46
+ const result = (0, adapter_1.runReducer)(node, { area: (0, adapter_1.optionalArea)(config), component: "activity-lighting-controller" }, (saved, now) => (0, activity_lighting_controller_1.transitionActivityLighting)(saved, msg, controllerConfig, now));
64
47
  if (result.commands.length) {
65
48
  send([
66
49
  result.commands.map((activityLightingCommand) => ({
@@ -82,28 +65,14 @@ const registerActivityLightingController = (RED) => {
82
65
  // transition with the reducer's own startup (re-observe, never replay)
83
66
  // rather than ticking old state; a later startup from the flow repeats it.
84
67
  if (context.get("state")?.pending) {
85
- timer = setTimeout(() => {
86
- try {
87
- process({ activityLighting: { kind: "startup" } }, node.send.bind(node));
88
- }
89
- catch (error) {
90
- node.error(error);
91
- }
92
- }, 0);
68
+ timer = setTimeout((0, adapter_1.guarded)(node, () => {
69
+ process({ activityLighting: { kind: "startup" } }, node.send.bind(node));
70
+ }), 0);
93
71
  }
94
- node.on("input", (msg, send, done) => {
95
- try {
96
- process({
97
- activityLighting: msg.activityLighting && typeof msg.activityLighting === "object"
98
- ? msg.activityLighting
99
- : undefined
100
- }, send);
101
- done();
102
- }
103
- catch (error) {
104
- node.error(error, msg);
105
- done(error instanceof Error ? error : new Error(String(error)));
106
- }
72
+ (0, adapter_1.onInput)(node, (msg, send) => {
73
+ process({
74
+ activityLighting: (0, adapter_1.objectField)(msg, "activityLighting")
75
+ }, send);
107
76
  });
108
77
  node.on("close", (_removed, done) => {
109
78
  clearTimeout(timer);
@@ -0,0 +1,15 @@
1
+ import type { Node, NodeMessage } from "node-red";
2
+ import { type TraceInput, type TraceMetadata } from "../lib/bounded-trace";
3
+ export type Send = (msg: NodeMessage | Array<NodeMessage | NodeMessage[] | null>) => void;
4
+ export type InputMessage = NodeMessage & Record<string, unknown>;
5
+ export declare function objectField<T>(msg: InputMessage, key: string): T | undefined;
6
+ export declare function onInput(node: Node, handle: (msg: InputMessage, send: Send) => void): void;
7
+ export declare function guarded(node: Node, work: () => void): () => void;
8
+ export declare function runReducer<Saved, Result extends {
9
+ state: unknown;
10
+ traces: TraceInput[];
11
+ }>(node: Node, trace: TraceMetadata, reduce: (saved: Saved | undefined, now: Date) => Result): Result;
12
+ export declare function optionalArea(config: {
13
+ area?: unknown;
14
+ }): string;
15
+ export declare function invalidConfig(node: Node, reason: unknown): void;
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.objectField = objectField;
4
+ exports.onInput = onInput;
5
+ exports.guarded = guarded;
6
+ exports.runReducer = runReducer;
7
+ exports.optionalArea = optionalArea;
8
+ exports.invalidConfig = invalidConfig;
9
+ const bounded_trace_1 = require("../lib/bounded-trace");
10
+ // The object a message carries under a key, or undefined.
11
+ function objectField(msg, key) {
12
+ const value = msg[key];
13
+ return value && typeof value === "object" ? value : undefined;
14
+ }
15
+ // Handle each input; an error is reported against the message and ends it.
16
+ function onInput(node, handle) {
17
+ node.on("input", (msg, send, done) => {
18
+ try {
19
+ handle(msg, send);
20
+ done();
21
+ }
22
+ catch (error) {
23
+ node.error(error, msg);
24
+ done(error instanceof Error ? error : new Error(String(error)));
25
+ }
26
+ });
27
+ }
28
+ // Work run from a timer, with any error reported on the node.
29
+ function guarded(node, work) {
30
+ return () => {
31
+ try {
32
+ work();
33
+ }
34
+ catch (error) {
35
+ node.error(error);
36
+ }
37
+ };
38
+ }
39
+ // Run a reducer on the node's saved state, save the new state and append its
40
+ // traces.
41
+ function runReducer(node, trace, reduce) {
42
+ const context = node.context();
43
+ const now = new Date();
44
+ const result = reduce(context.get("state"), now);
45
+ context.set("state", result.state);
46
+ (0, bounded_trace_1.appendTrace)(context, trace, result.traces, now);
47
+ return result;
48
+ }
49
+ // A node's area. Most nodes only use it as a label, so it is optional and an
50
+ // empty one means no area; there is never a default.
51
+ function optionalArea(config) {
52
+ return typeof config.area === "string" ? config.area.trim() : "";
53
+ }
54
+ // The one badge for a configuration the node cannot use, with the reason in
55
+ // the log.
56
+ function invalidConfig(node, reason) {
57
+ node.status({ fill: "red", shape: "ring", text: "invalid configuration" });
58
+ node.error("invalid configuration: " + (reason instanceof Error ? reason.message : String(reason)));
59
+ }
60
+ //# sourceMappingURL=adapter.js.map
@@ -4,8 +4,8 @@
4
4
  color: "#c7d9f1",
5
5
  defaults: {
6
6
  name: {value: ""},
7
- area: {value: "lounge", required: true},
8
- sources: {value: "{\"shield\":\"SAT/CBL\",\"playstation\":\"GAME\"}", required: true},
7
+ area: {value: ""},
8
+ sources: {value: "{\"shield\":\"SAT/CBL\",\"playstation\":\"GAME\"}", required: true, validate: function (v) { try { JSON.parse(v); return true; } catch (e) { return false; } }},
9
9
  retryMs: {value: 2000, required: true, validate: RED.validators.number()},
10
10
  maxAttempts: {value: 3, required: true, validate: RED.validators.number()},
11
11
  minVolume: {value: 0, required: true, validate: RED.validators.number()},
@@ -15,7 +15,7 @@
15
15
  outputs: 2,
16
16
  outputLabels: ["Denon commands", "state and results"],
17
17
  icon: "font-awesome/fa-volume-up",
18
- label: function () { return this.name || this.area + " AVR controller"; }
18
+ label: function () { return this.name || (this.area ? this.area + " " : "") + "AVR controller"; }
19
19
  });
20
20
  </script>
21
21
 
@@ -34,5 +34,14 @@
34
34
  <p>Send protocol observations as <code>msg.avr = {kind: "observed", line: msg.payload}</code>.</p>
35
35
  <p>Send activity requests as <code>msg.avr = {kind: "intent", requestId, generation, power: "on", source: "shield"}</code>. Omit <code>source</code> to preserve the current input, or use <code>power: "standby"</code> to stop the AVR.</p>
36
36
  <p>Send volume requests as <code>msg.avr = {kind: "volume", requestId, mode: "absolute", value: 30}</code> or use <code>mode: "relative"</code> with a positive or negative delta. The controller applies configured bounds, accumulates rapid relative requests, confirms fresh <code>MV</code> evidence and releases ownership so external adjustments are preserved.</p>
37
- <p>Send <code>kind: "startup"</code> after deployment to discard stale observations and query the receiver. Output 1 connects to a TCP output node; output 2 emits state plus ready, failed, rejected, superseded and volume results.</p>
37
+ <p>Send <code>kind: "startup"</code> after deployment to discard stale observations and query the receiver; a volume request still pending then is reported as failed with reason <code>restart</code>. Send <code>kind: "refresh"</code> to re-read the receiver without disturbing work in flight. Output 1 connects to a TCP output node; output 2 emits state plus ready, failed, rejected, superseded and volume results.</p>
38
+ <p>While it has a request, output 2 also carries <code>msg.theatre = {kind: "target_state", target: "avr", requestId, generation, status, observedAt, details}</code>, so it can be wired straight back to a theatre activity controller. A ready state is stamped with the oldest receiver reading the request relies on, and a <code>refresh</code> is answered once the receiver reports again.</p>
39
+ <h3>Volume results</h3>
40
+ <dl class="message-properties">
41
+ <dt>volume_ready</dt><dd><code>{kind, area, requestId, volume, bounded}</code>: confirmed by a fresh reading.</dd>
42
+ <dt>volume_rejected</dt><dd><code>{kind, area, requestId, reason}</code>: not sent, for example <code>receiver-not-ready</code> or an invalid request.</dd>
43
+ <dt>volume_failed</dt><dd><code>{kind, area, requestId, reason, operation, attempts}</code>: <code>reason</code> is <code>attempts-exhausted</code> (with the operation and attempts) or <code>restart</code> (operation and attempts are <code>null</code>).</dd>
44
+ <dt>volume_manual_override</dt><dd><code>{kind, area, requestId, reason, observed, target}</code>: the receiver moved elsewhere while the request was in flight.</dd>
45
+ <dt>volume_external</dt><dd><code>{kind, area, previous, observed}</code>: a change made outside any request.</dd>
46
+ </dl>
38
47
  </script>
@@ -1,20 +1,12 @@
1
1
  "use strict";
2
- const bounded_trace_1 = require("../lib/bounded-trace");
3
2
  const avr_controller_1 = require("../lib/avr-controller");
4
- function number(value, fallback) {
5
- const parsed = Number(value);
6
- return Number.isFinite(parsed) ? parsed : fallback;
7
- }
3
+ const adapter_1 = require("./adapter");
4
+ const settings_1 = require("../lib/settings");
8
5
  function parseSources(value) {
9
- const sources = JSON.parse(value || "{}");
6
+ const sources = (0, settings_1.jsonSetting)(value, "sources", {});
10
7
  if (!sources || Array.isArray(sources) || typeof sources !== "object") {
11
8
  throw new Error("sources must be a JSON object");
12
9
  }
13
- for (const [name, code] of Object.entries(sources)) {
14
- if (typeof name !== "string" || typeof code !== "string") {
15
- throw new Error("sources must map names to Denon source codes");
16
- }
17
- }
18
10
  return sources;
19
11
  }
20
12
  const registerAvrController = (RED) => {
@@ -25,18 +17,12 @@ const registerAvrController = (RED) => {
25
17
  let retryTimer;
26
18
  let controllerConfig;
27
19
  try {
28
- controllerConfig = {
29
- area: config.area || "room",
30
- sources: parseSources(config.sources),
31
- retryMs: Math.max(250, number(config.retryMs, 2000)),
32
- maxAttempts: Math.max(1, Math.floor(number(config.maxAttempts, 3))),
33
- minVolume: number(config.minVolume, 0),
34
- maxVolume: number(config.maxVolume, 60)
35
- };
20
+ // The same validation the reducer applies, so a bad configuration
21
+ // fails here at deploy rather than on every message.
22
+ controllerConfig = (0, avr_controller_1.normaliseAvrConfig)({ ...config, area: (0, adapter_1.optionalArea)(config), sources: parseSources(config.sources) });
36
23
  }
37
24
  catch (error) {
38
- node.status({ fill: "red", shape: "ring", text: "invalid configuration" });
39
- node.error(error);
25
+ (0, adapter_1.invalidConfig)(node, error);
40
26
  return;
41
27
  }
42
28
  function updateStatus(status) {
@@ -57,33 +43,22 @@ const registerAvrController = (RED) => {
57
43
  if (!state.pending?.deadlineAt)
58
44
  return;
59
45
  const delay = Math.max(0, state.pending.deadlineAt - Date.now());
60
- retryTimer = setTimeout(() => {
61
- try {
62
- process({ avr: { kind: "tick" } }, node.send.bind(node));
63
- }
64
- catch (error) {
65
- node.error(error);
66
- }
67
- }, delay);
46
+ retryTimer = setTimeout((0, adapter_1.guarded)(node, () => {
47
+ process({ avr: { kind: "tick" } }, node.send.bind(node));
48
+ }), delay);
68
49
  }
69
50
  function process(msg, send) {
70
- const now = new Date();
71
- const saved = context.get("state");
72
- const result = (0, avr_controller_1.transitionAvr)(saved, msg, controllerConfig, now);
73
- context.set("state", result.state);
74
- (0, bounded_trace_1.appendTrace)(context, {
75
- area: controllerConfig.area,
76
- component: "avr-controller"
77
- }, result.traces, now);
51
+ const result = (0, adapter_1.runReducer)(node, { area: controllerConfig.area, component: "avr-controller" }, (saved, now) => (0, avr_controller_1.transitionAvr)(saved, msg, controllerConfig, now));
78
52
  const commands = result.commands.map((command) => ({
79
53
  payload: Buffer.from(command.payload, "utf8"),
80
54
  avr_command: command.avrCommand
81
55
  }));
82
- if (commands.length || result.events.length) {
56
+ const outputs = [...result.events, ...result.targetStates];
57
+ if (commands.length || outputs.length) {
83
58
  send([
84
59
  commands.length ? commands : null,
85
- result.events.length
86
- ? result.events.map((event) => ({ payload: undefined, ...event }))
60
+ outputs.length
61
+ ? outputs.map((event) => ({ payload: undefined, ...event }))
87
62
  : null
88
63
  ]);
89
64
  }
@@ -95,29 +70,15 @@ const registerAvrController = (RED) => {
95
70
  // transition with the reducer's own startup (re-observe, never replay)
96
71
  // rather than ticking old state; a later startup from the flow repeats it.
97
72
  if (context.get("state")?.pending) {
98
- retryTimer = setTimeout(() => {
99
- try {
100
- process({ avr: { kind: "startup" } }, node.send.bind(node));
101
- }
102
- catch (error) {
103
- node.error(error);
104
- }
105
- }, 0);
73
+ retryTimer = setTimeout((0, adapter_1.guarded)(node, () => {
74
+ process({ avr: { kind: "startup" } }, node.send.bind(node));
75
+ }), 0);
106
76
  }
107
- node.on("input", (msg, send, done) => {
108
- try {
109
- process({
110
- avr: msg.avr && typeof msg.avr === "object"
111
- ? msg.avr
112
- : undefined,
113
- payload: msg.payload
114
- }, send);
115
- done();
116
- }
117
- catch (error) {
118
- node.error(error, msg);
119
- done(error instanceof Error ? error : new Error(String(error)));
120
- }
77
+ (0, adapter_1.onInput)(node, (msg, send) => {
78
+ process({
79
+ avr: (0, adapter_1.objectField)(msg, "avr"),
80
+ payload: msg.payload
81
+ }, send);
121
82
  });
122
83
  node.on("close", (_removed, done) => {
123
84
  clearTimeout(retryTimer);
@@ -4,7 +4,7 @@
4
4
  color: "#c7d9f1",
5
5
  defaults: {
6
6
  name: {value: ""},
7
- area: {value: "lounge", required: true},
7
+ area: {value: ""},
8
8
  pollMs: {value: 2000, required: true, validate: RED.validators.number()},
9
9
  confirmationTimeoutMs: {value: 15000, required: true, validate: RED.validators.number()},
10
10
  maxAttempts: {value: 2, required: true, validate: RED.validators.number()}
@@ -1,21 +1,25 @@
1
1
  "use strict";
2
- const bounded_trace_1 = require("../lib/bounded-trace");
3
2
  const backlighting_controller_1 = require("../lib/backlighting-controller");
4
- function number(value, fallback) {
5
- const parsed = Number(value);
6
- return Number.isFinite(parsed) ? parsed : fallback;
7
- }
3
+ const adapter_1 = require("./adapter");
4
+ const settings_1 = require("../lib/settings");
8
5
  const registerBacklightingController = (RED) => {
9
6
  function HomeBacklightingController(config) {
10
7
  RED.nodes.createNode(this, config);
11
8
  const node = this;
12
9
  const context = node.context();
13
10
  let timer;
14
- const controllerConfig = {
15
- pollMs: number(config.pollMs, 2000),
16
- confirmationTimeoutMs: number(config.confirmationTimeoutMs, 15000),
17
- maxAttempts: number(config.maxAttempts, 2)
18
- };
11
+ let controllerConfig;
12
+ try {
13
+ controllerConfig = {
14
+ pollMs: (0, settings_1.numberSetting)(config.pollMs, "poll interval", 2000),
15
+ confirmationTimeoutMs: (0, settings_1.numberSetting)(config.confirmationTimeoutMs, "confirmation timeout", 15000),
16
+ maxAttempts: (0, settings_1.numberSetting)(config.maxAttempts, "max attempts", 2)
17
+ };
18
+ }
19
+ catch (error) {
20
+ (0, adapter_1.invalidConfig)(node, error);
21
+ return;
22
+ }
19
23
  function updateStatus(status) {
20
24
  const appearances = {
21
25
  idle: { fill: "grey", shape: "dot" },
@@ -34,24 +38,12 @@ const registerBacklightingController = (RED) => {
34
38
  return;
35
39
  const dueAt = Math.min(state.pending.nextAt, state.pending.deadlineAt);
36
40
  const delay = Math.max(0, dueAt - Date.now());
37
- timer = setTimeout(() => {
38
- try {
39
- process({ backlighting: { kind: "tick", generation: state.generation } }, node.send.bind(node));
40
- }
41
- catch (error) {
42
- node.error(error);
43
- }
44
- }, delay);
41
+ timer = setTimeout((0, adapter_1.guarded)(node, () => {
42
+ process({ backlighting: { kind: "tick", generation: state.generation } }, node.send.bind(node));
43
+ }), delay);
45
44
  }
46
45
  function process(msg, send) {
47
- const now = new Date();
48
- const saved = context.get("state");
49
- const result = (0, backlighting_controller_1.transitionBacklighting)(saved, msg, controllerConfig, now);
50
- context.set("state", result.state);
51
- (0, bounded_trace_1.appendTrace)(context, {
52
- area: config.area || "lounge",
53
- component: "backlighting-controller"
54
- }, result.traces, now);
46
+ const result = (0, adapter_1.runReducer)(node, { area: (0, adapter_1.optionalArea)(config), component: "backlighting-controller" }, (saved, now) => (0, backlighting_controller_1.transitionBacklighting)(saved, msg, controllerConfig, now));
55
47
  if (result.commands.length) {
56
48
  send([
57
49
  result.commands.map((backlightingCommand) => ({ payload: undefined, backlightingCommand })),
@@ -69,28 +61,14 @@ const registerBacklightingController = (RED) => {
69
61
  // transition with the reducer's own startup (re-observe, never replay)
70
62
  // rather than ticking old state; a later startup from the flow repeats it.
71
63
  if (context.get("state")?.pending) {
72
- timer = setTimeout(() => {
73
- try {
74
- process({ backlighting: { kind: "startup" } }, node.send.bind(node));
75
- }
76
- catch (error) {
77
- node.error(error);
78
- }
79
- }, 0);
64
+ timer = setTimeout((0, adapter_1.guarded)(node, () => {
65
+ process({ backlighting: { kind: "startup" } }, node.send.bind(node));
66
+ }), 0);
80
67
  }
81
- node.on("input", (msg, send, done) => {
82
- try {
83
- process({
84
- backlighting: msg.backlighting && typeof msg.backlighting === "object"
85
- ? msg.backlighting
86
- : undefined
87
- }, send);
88
- done();
89
- }
90
- catch (error) {
91
- node.error(error, msg);
92
- done(error instanceof Error ? error : new Error(String(error)));
93
- }
68
+ (0, adapter_1.onInput)(node, (msg, send) => {
69
+ process({
70
+ backlighting: (0, adapter_1.objectField)(msg, "backlighting")
71
+ }, send);
94
72
  });
95
73
  node.on("close", (_removed, done) => {
96
74
  clearTimeout(timer);
@@ -4,7 +4,7 @@
4
4
  color: "#c7d9f1",
5
5
  defaults: {
6
6
  name: {value: ""},
7
- area: {value: "lounge", required: true},
7
+ area: {value: ""},
8
8
  tvHost: {value: "", required: true},
9
9
  screenHost: {value: "", required: true},
10
10
  projectorMeterHost: {value: "", required: true},
@@ -29,7 +29,7 @@
29
29
  outputs: 2,
30
30
  outputLabels: ["projector IR fallback", "state and results"],
31
31
  icon: "font-awesome/fa-television",
32
- label: function () { return this.name || this.area + " display mode"; }
32
+ label: function () { return this.name || (this.area ? this.area + " " : "") + "display mode"; }
33
33
  });
34
34
  </script>
35
35
 
@@ -59,8 +59,8 @@
59
59
 
60
60
  <script type="text/html" data-help-name="home-display-mode-controller">
61
61
  <p>Owns Bravia power, projector lamp power and projector-screen position for <code>off</code>, <code>tv</code> and <code>projector</code> modes.</p>
62
- <p>Send <code>msg.display = {kind: "intent", requestId, generation, mode}</code>. Send <code>kind: "startup"</code> after deployment to query every owned device and reconcile persisted intent.</p>
63
- <p>The Bravia and both Shelly devices use direct LAN APIs. Output 1 is the explicitly named HA/Broadlink projector-power fallback and must connect only to <code>remote.send_command</code> for the projector. Enable “Projector power is fallback” to include its use in activity metrics. Output 2 emits observed state plus ready, manual-override, failed, rejected and superseded results.</p>
62
+ <p>Send <code>msg.display = {kind: "intent", requestId, generation, mode}</code>. Send <code>kind: "startup"</code> after deployment to query every owned device and reconcile persisted intent. Send <code>kind: "refresh"</code> to re-confirm a settled display from fresh readings.</p>
63
+ <p>The Bravia and both Shelly devices use direct LAN APIs. Output 1 carries the projector power toggle (<code>msg.payload = "power"</code> with <code>msg.display_command</code>) for your infrared transport, commanded directly rather than through Home Assistant. Enable “Projector power is fallback” to include its use in activity metrics. Output 2 emits observed state plus ready, manual-override, failed, rejected and superseded results, and <code>msg.theatre</code> target state for the theatre activity controller: wire it straight back to that controller.</p>
64
64
  <p>An unsolicited physical-state change after readiness latches manual ownership. The controller observes and reports it without issuing corrective commands; a newer activity intent resumes automation.</p>
65
65
  <p>The Bravia PSK remains in Node-RED's encrypted credential store and is never emitted in a message or status.</p>
66
66
  </script>
@@ -1,47 +1,30 @@
1
1
  "use strict";
2
- const bounded_trace_1 = require("../lib/bounded-trace");
3
2
  const display_mode_controller_1 = require("../lib/display-mode-controller");
4
3
  const display_transport_1 = require("../lib/display-transport");
5
- function number(value, fallback) {
6
- const parsed = Number(value);
7
- return Number.isFinite(parsed) ? parsed : fallback;
8
- }
4
+ const adapter_1 = require("./adapter");
5
+ const settings_1 = require("../lib/settings");
9
6
  const registerDisplayModeController = (RED) => {
10
7
  function HomeDisplayModeController(config) {
11
8
  RED.nodes.createNode(this, config);
12
9
  const node = this;
13
- const context = node.context();
14
10
  let timer;
15
11
  let closed = false;
16
- const controllerConfig = {
17
- screenOpenPosition: number(config.screenOpenPosition, 82),
18
- screenTolerance: number(config.screenTolerance, 2),
19
- pollMs: number(config.pollMs, 2000),
20
- queryTimeoutMs: number(config.queryTimeoutMs, 5000),
21
- tvTimeoutMs: number(config.tvTimeoutMs, 30000),
22
- screenTimeoutMs: number(config.screenTimeoutMs, 65000),
23
- projectorWarmupMs: number(config.projectorWarmupMs, 60000),
24
- projectorMinimumWarmupMs: number(config.projectorMinimumWarmupMs, 15000),
25
- projectorShutdownConfirmMs: number(config.projectorShutdownConfirmMs, 5000),
26
- projectorCooldownMs: number(config.projectorCooldownMs, 120000),
27
- maxAttempts: number(config.maxAttempts, 2),
28
- projectorOnWatts: number(config.projectorOnWatts, 20),
29
- projectorOffWatts: number(config.projectorOffWatts, 10),
30
- projectorPowerFallback: config.projectorPowerFallback === true
31
- };
12
+ // Defaults and limits come from the reducer's own normalisation.
13
+ let controllerConfig;
32
14
  let transport;
33
15
  try {
16
+ controllerConfig = (0, display_mode_controller_1.normaliseConfig)(config);
34
17
  transport = new display_transport_1.DisplayTransport({
35
- tvHost: String(config.tvHost ?? ""),
18
+ // No defaults: these name the user's own devices.
19
+ tvHost: (0, settings_1.textSetting)(config.tvHost, "TV host"),
36
20
  tvPsk: String(node.credentials?.tvPsk ?? ""),
37
- screenHost: String(config.screenHost ?? ""),
38
- projectorMeterHost: String(config.projectorMeterHost ?? ""),
39
- timeoutMs: number(config.transportTimeoutMs, 5000)
21
+ screenHost: (0, settings_1.textSetting)(config.screenHost, "screen host"),
22
+ projectorMeterHost: (0, settings_1.textSetting)(config.projectorMeterHost, "projector meter host"),
23
+ timeoutMs: (0, settings_1.numberSetting)(config.transportTimeoutMs, "transport timeout", 5000)
40
24
  });
41
25
  }
42
26
  catch (error) {
43
- node.status({ fill: "red", shape: "ring", text: "invalid configuration" });
44
- node.error(error);
27
+ (0, adapter_1.invalidConfig)(node, error);
45
28
  return;
46
29
  }
47
30
  function updateStatus(status) {
@@ -63,14 +46,9 @@ const registerDisplayModeController = (RED) => {
63
46
  return;
64
47
  const delay = Math.max(0, state.pending.nextAt - Date.now());
65
48
  const generation = state.desired?.generation || state.generation;
66
- timer = setTimeout(() => {
67
- try {
68
- process({ display: { kind: "tick", generation } }, node.send.bind(node));
69
- }
70
- catch (error) {
71
- node.error(error);
72
- }
73
- }, delay);
49
+ timer = setTimeout((0, adapter_1.guarded)(node, () => {
50
+ process({ display: { kind: "tick", generation } }, node.send.bind(node));
51
+ }), delay);
74
52
  }
75
53
  async function dispatch(command) {
76
54
  if (command.device === "projector" && command.operation === "power_toggle") {
@@ -85,16 +63,10 @@ const registerDisplayModeController = (RED) => {
85
63
  process(result, node.send.bind(node));
86
64
  }
87
65
  function process(msg, send) {
88
- const now = new Date();
89
- const saved = context.get("state");
90
- const result = (0, display_mode_controller_1.transitionDisplay)(saved, msg, controllerConfig, now);
91
- context.set("state", result.state);
92
- (0, bounded_trace_1.appendTrace)(context, {
93
- area: config.area || "lounge",
94
- component: "display-mode-controller"
95
- }, result.traces, now);
96
- if (result.events.length) {
97
- send([null, result.events.map((event) => ({ payload: undefined, ...event }))]);
66
+ const result = (0, adapter_1.runReducer)(node, { area: (0, adapter_1.optionalArea)(config), component: "display-mode-controller" }, (saved, now) => (0, display_mode_controller_1.transitionDisplay)(saved, msg, controllerConfig, now));
67
+ const outputs = [...result.events, ...result.targetStates];
68
+ if (outputs.length) {
69
+ send([null, outputs.map((event) => ({ payload: undefined, ...event }))]);
98
70
  }
99
71
  updateStatus(result.status);
100
72
  schedule(result.state);
@@ -103,19 +75,10 @@ const registerDisplayModeController = (RED) => {
103
75
  }
104
76
  return result;
105
77
  }
106
- node.on("input", (msg, send, done) => {
107
- try {
108
- process({
109
- display: msg.display && typeof msg.display === "object"
110
- ? msg.display
111
- : undefined
112
- }, send);
113
- done();
114
- }
115
- catch (error) {
116
- node.error(error, msg);
117
- done(error instanceof Error ? error : new Error(String(error)));
118
- }
78
+ (0, adapter_1.onInput)(node, (msg, send) => {
79
+ process({
80
+ display: (0, adapter_1.objectField)(msg, "display")
81
+ }, send);
119
82
  });
120
83
  node.on("close", (_removed, done) => {
121
84
  closed = true;