@avantics/node-red-home-automation 2.3.0 → 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 (79) hide show
  1. package/README.md +32 -8
  2. package/contract-examples/room-lighting.mjs +12 -7
  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 +13 -47
  6. package/dist/lib/avr-controller.js +39 -90
  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/command-echo.d.ts +20 -0
  11. package/dist/lib/command-echo.js +82 -0
  12. package/dist/lib/cover-controller.d.ts +15 -28
  13. package/dist/lib/cover-controller.js +6 -8
  14. package/dist/lib/denon-protocol.d.ts +29 -0
  15. package/dist/lib/denon-protocol.js +76 -0
  16. package/dist/lib/display-mode-controller.d.ts +7 -18
  17. package/dist/lib/display-mode-controller.js +39 -47
  18. package/dist/lib/motion-policy.d.ts +12 -22
  19. package/dist/lib/motion-policy.js +53 -56
  20. package/dist/lib/occupancy-fusion.d.ts +8 -12
  21. package/dist/lib/room-controller.d.ts +14 -34
  22. package/dist/lib/room-controller.js +80 -132
  23. package/dist/lib/room-lighting-discovery.d.ts +9 -0
  24. package/dist/lib/room-lighting-discovery.js +45 -0
  25. package/dist/lib/settings.d.ts +5 -0
  26. package/dist/lib/settings.js +68 -0
  27. package/dist/lib/source-capability-controller.d.ts +30 -35
  28. package/dist/lib/source-capability-controller.js +35 -36
  29. package/dist/lib/switch-policy.d.ts +2 -7
  30. package/dist/lib/target-state.d.ts +24 -0
  31. package/dist/lib/target-state.js +38 -0
  32. package/dist/lib/theatre-activity-controller.d.ts +6 -108
  33. package/dist/lib/theatre-activity-controller.js +67 -318
  34. package/dist/lib/theatre-ha-discovery.d.ts +14 -0
  35. package/dist/lib/theatre-ha-discovery.js +163 -0
  36. package/dist/lib/theatre-metrics.d.ts +4 -0
  37. package/dist/lib/theatre-metrics.js +66 -0
  38. package/dist/lib/theatre-profiles.d.ts +23 -0
  39. package/dist/lib/theatre-profiles.js +73 -0
  40. package/dist/lib/theatre-refresh.d.ts +20 -0
  41. package/dist/lib/theatre-refresh.js +46 -0
  42. package/dist/lib/theatre-slo.js +7 -9
  43. package/dist/lib/theatre-state-contract.d.ts +7 -19
  44. package/dist/lib/theatre-state-contract.js +33 -169
  45. package/dist/lib/theatre-state.d.ts +110 -0
  46. package/dist/lib/theatre-state.js +139 -0
  47. package/dist/lib/wall-input.d.ts +2 -7
  48. package/dist/lib/z2m-light.d.ts +16 -0
  49. package/dist/lib/z2m-light.js +36 -0
  50. package/dist/nodes/activity-lighting-controller.html +5 -3
  51. package/dist/nodes/activity-lighting-controller.js +21 -52
  52. package/dist/nodes/adapter.d.ts +15 -0
  53. package/dist/nodes/adapter.js +60 -0
  54. package/dist/nodes/avr-controller.html +3 -3
  55. package/dist/nodes/avr-controller.js +17 -56
  56. package/dist/nodes/backlighting-controller.html +1 -1
  57. package/dist/nodes/backlighting-controller.js +25 -47
  58. package/dist/nodes/display-mode-controller.html +2 -2
  59. package/dist/nodes/display-mode-controller.js +19 -57
  60. package/dist/nodes/motion-policy.html +23 -10
  61. package/dist/nodes/motion-policy.js +14 -135
  62. package/dist/nodes/occupancy-fusion.html +2 -2
  63. package/dist/nodes/occupancy-fusion.js +23 -29
  64. package/dist/nodes/room-controller.html +80 -26
  65. package/dist/nodes/room-controller.js +37 -54
  66. package/dist/nodes/source-capability-controller.html +4 -13
  67. package/dist/nodes/source-capability-controller.js +17 -52
  68. package/dist/nodes/switch-policy.html +3 -3
  69. package/dist/nodes/switch-policy.js +23 -42
  70. package/dist/nodes/theatre-activity-controller.html +4 -2
  71. package/dist/nodes/theatre-activity-controller.js +18 -45
  72. package/dist/nodes/theatre-state-contract.html +55 -22
  73. package/dist/nodes/theatre-state-contract.js +30 -41
  74. package/examples/Home theatre activity.json +13 -12
  75. package/examples/Home theatre equipment.json +4 -4
  76. package/examples/Room lighting.json +9 -9
  77. package/package.json +1 -1
  78. package/dist/lib/circadian-refresh.d.ts +0 -7
  79. package/dist/lib/circadian-refresh.js +0 -11
@@ -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
 
@@ -1,12 +1,9 @@
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
  }
@@ -22,20 +19,10 @@ const registerAvrController = (RED) => {
22
19
  try {
23
20
  // The same validation the reducer applies, so a bad configuration
24
21
  // fails here at deploy rather than on every message.
25
- controllerConfig = (0, avr_controller_1.normaliseAvrConfig)({
26
- area: config.area || "room",
27
- sources: parseSources(config.sources),
28
- retryMs: Math.max(250, number(config.retryMs, 2000)),
29
- maxAttempts: Math.max(1, Math.floor(number(config.maxAttempts, 3))),
30
- minVolume: number(config.minVolume, 0),
31
- maxVolume: number(config.maxVolume, 60)
32
- });
22
+ controllerConfig = (0, avr_controller_1.normaliseAvrConfig)({ ...config, area: (0, adapter_1.optionalArea)(config), sources: parseSources(config.sources) });
33
23
  }
34
24
  catch (error) {
35
- // A short badge; the full reason goes to the log and debug sidebar.
36
- const reason = error instanceof Error ? error.message : String(error);
37
- node.status({ fill: "red", shape: "ring", text: "invalid configuration" });
38
- node.error("invalid configuration: " + reason);
25
+ (0, adapter_1.invalidConfig)(node, error);
39
26
  return;
40
27
  }
41
28
  function updateStatus(status) {
@@ -56,24 +43,12 @@ const registerAvrController = (RED) => {
56
43
  if (!state.pending?.deadlineAt)
57
44
  return;
58
45
  const delay = Math.max(0, state.pending.deadlineAt - Date.now());
59
- retryTimer = setTimeout(() => {
60
- try {
61
- process({ avr: { kind: "tick" } }, node.send.bind(node));
62
- }
63
- catch (error) {
64
- node.error(error);
65
- }
66
- }, delay);
46
+ retryTimer = setTimeout((0, adapter_1.guarded)(node, () => {
47
+ process({ avr: { kind: "tick" } }, node.send.bind(node));
48
+ }), delay);
67
49
  }
68
50
  function process(msg, send) {
69
- const now = new Date();
70
- const saved = context.get("state");
71
- const result = (0, avr_controller_1.transitionAvr)(saved, msg, controllerConfig, now);
72
- context.set("state", result.state);
73
- (0, bounded_trace_1.appendTrace)(context, {
74
- area: controllerConfig.area,
75
- component: "avr-controller"
76
- }, 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));
77
52
  const commands = result.commands.map((command) => ({
78
53
  payload: Buffer.from(command.payload, "utf8"),
79
54
  avr_command: command.avrCommand
@@ -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
 
@@ -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,14 +63,7 @@ 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);
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));
96
67
  const outputs = [...result.events, ...result.targetStates];
97
68
  if (outputs.length) {
98
69
  send([null, outputs.map((event) => ({ payload: undefined, ...event }))]);
@@ -104,19 +75,10 @@ const registerDisplayModeController = (RED) => {
104
75
  }
105
76
  return result;
106
77
  }
107
- node.on("input", (msg, send, done) => {
108
- try {
109
- process({
110
- display: msg.display && typeof msg.display === "object"
111
- ? msg.display
112
- : undefined
113
- }, send);
114
- done();
115
- }
116
- catch (error) {
117
- node.error(error, msg);
118
- done(error instanceof Error ? error : new Error(String(error)));
119
- }
78
+ (0, adapter_1.onInput)(node, (msg, send) => {
79
+ process({
80
+ display: (0, adapter_1.objectField)(msg, "display")
81
+ }, send);
120
82
  });
121
83
  node.on("close", (_removed, done) => {
122
84
  closed = true;
@@ -4,18 +4,16 @@
4
4
  color: "#d8bfd8",
5
5
  defaults: {
6
6
  name: {value: ""},
7
- area: {value: "room", required: true},
8
- onTargets: {value: "[\"main\"]", required: true},
9
- offTargets: {value: "[\"main\"]", required: true},
7
+ area: {value: ""},
8
+ onTargets: {value: "[\"main\"]", required: true, validate: function (v) { try { JSON.parse(v); return true; } catch (e) { return false; } }},
9
+ offTargets: {value: "[\"main\"]", required: true, validate: function (v) { try { JSON.parse(v); return true; } catch (e) { return false; } }},
10
10
  defaultDelaySeconds: {value: 120, required: true, validate: RED.validators.number()},
11
- defaultIlluminanceCutoff: {value: 10, required: true, validate: RED.validators.number()},
12
- switchWindowMs: {value: 500, required: true, validate: RED.validators.number()},
13
- switchFreshnessSeconds: {value: 3600, required: true, validate: RED.validators.number()}
11
+ defaultIlluminanceCutoff: {value: 10, required: true, validate: RED.validators.number()}
14
12
  },
15
13
  inputs: 1,
16
14
  outputs: 1,
17
15
  icon: "font-awesome/fa-lightbulb-o",
18
- label: function () { return this.name || this.area + " motion policy"; }
16
+ label: function () { return this.name || (this.area ? this.area + " " : "") + "motion policy"; }
19
17
  });
20
18
  </script>
21
19
 
@@ -26,10 +24,25 @@
26
24
  <div class="form-row"><label for="node-input-offTargets">OFF targets</label><input type="text" id="node-input-offTargets"></div>
27
25
  <div class="form-row"><label for="node-input-defaultDelaySeconds">Vacancy seconds</label><input type="number" id="node-input-defaultDelaySeconds"></div>
28
26
  <div class="form-row"><label for="node-input-defaultIlluminanceCutoff">Lux cutoff</label><input type="number" id="node-input-defaultIlluminanceCutoff"></div>
29
- <div class="form-row"><label for="node-input-switchWindowMs">Switch window ms</label><input type="number" id="node-input-switchWindowMs"></div>
30
- <div class="form-row"><label for="node-input-switchFreshnessSeconds">Switch freshness seconds</label><input type="number" id="node-input-switchFreshnessSeconds"></div>
31
27
  </script>
32
28
 
33
29
  <script type="text/html" data-help-name="home-motion-lighting-policy">
34
- <p>Turns normalised occupancy, illuminance, enable-state and wall-switch events into <code>msg.lighting</code> intents. Raw wall state preserves a persisted baseline, treats retained observations as inert re-baselines and exposes fresh, stale, unavailable or awaiting-baseline status. Send availability as <code>msg.source = "switch-availability"</code>; mark a polled snapshot with <code>msg.baseline = true</code>.</p>
30
+ <p>Turns normalised occupancy, illuminance and enable-state events into <code>msg.lighting</code> intents: ON when
31
+ the room is occupied and dark enough, and OFF once it has been vacant for the vacancy time. The OFF intent carries
32
+ <code>vacantSince</code>, so the room lighting controller keeps any light a person set after the room went
33
+ vacant.</p>
34
+ <p>Wall switches go to the <b>switch lighting policy</b> node, not here; switch messages sent to this node are
35
+ ignored.</p>
36
+ <h3>Inputs</h3>
37
+ <p>Each message names its kind in <code>msg.source</code>:</p>
38
+ <dl class="message-properties">
39
+ <dt>occupancy</dt><dd><code>msg.payload.occupied</code>, from the occupancy fusion node.</dd>
40
+ <dt>illuminance</dt><dd>the light level in lux.</dd>
41
+ <dt>area-enabled</dt><dd>ON or OFF for this node; <code>global-enabled</code> for every motion node.</dd>
42
+ <dt>delay</dt><dd>a new vacancy time, in <b>seconds</b>, the same unit as <b>Vacancy seconds</b>.</dd>
43
+ <dt>illuminance-cut-off</dt><dd>a new lux cutoff.</dd>
44
+ </dl>
45
+ <p>A <code>delay</code> or <code>illuminance-cut-off</code> message overrides the setting, and lasts across
46
+ restarts until the node is deployed with a different <b>Vacancy seconds</b> or <b>Lux cutoff</b>, which then
47
+ applies.</p>
35
48
  </script>