@avantics/node-red-home-automation 0.0.0-stage → 2.3.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 (91) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +292 -2
  3. package/contract-examples/README.md +28 -0
  4. package/contract-examples/_shared.mjs +11 -0
  5. package/contract-examples/motion-lighting.mjs +42 -0
  6. package/contract-examples/occupancy-fusion.mjs +35 -0
  7. package/contract-examples/room-lighting.mjs +58 -0
  8. package/contract-examples/run.mjs +16 -0
  9. package/contract-examples/source-readiness.mjs +64 -0
  10. package/contract-examples/switch-gestures.mjs +34 -0
  11. package/contract-examples/theatre-activity.mjs +62 -0
  12. package/contract-examples/volume-ownership.mjs +59 -0
  13. package/dist/lib/activity-lighting-controller.d.ts +129 -0
  14. package/dist/lib/activity-lighting-controller.js +372 -0
  15. package/dist/lib/avr-controller.d.ts +169 -0
  16. package/dist/lib/avr-controller.js +948 -0
  17. package/dist/lib/backlighting-controller.d.ts +108 -0
  18. package/dist/lib/backlighting-controller.js +269 -0
  19. package/dist/lib/bounded-trace.d.ts +30 -0
  20. package/dist/lib/bounded-trace.js +36 -0
  21. package/dist/lib/circadian-refresh.d.ts +7 -0
  22. package/dist/lib/circadian-refresh.js +11 -0
  23. package/dist/lib/circadian-schedule.d.ts +11 -0
  24. package/dist/lib/circadian-schedule.js +63 -0
  25. package/dist/lib/circadian.d.ts +6 -0
  26. package/dist/lib/circadian.js +15 -0
  27. package/dist/lib/cover-controller.d.ts +130 -0
  28. package/dist/lib/cover-controller.js +336 -0
  29. package/dist/lib/display-mode-controller.d.ts +148 -0
  30. package/dist/lib/display-mode-controller.js +587 -0
  31. package/dist/lib/display-transport.d.ts +41 -0
  32. package/dist/lib/display-transport.js +177 -0
  33. package/dist/lib/light-styling.d.ts +11 -0
  34. package/dist/lib/light-styling.js +15 -0
  35. package/dist/lib/motion-policy.d.ts +72 -0
  36. package/dist/lib/motion-policy.js +251 -0
  37. package/dist/lib/observation-time.d.ts +10 -0
  38. package/dist/lib/observation-time.js +40 -0
  39. package/dist/lib/occupancy-fusion.d.ts +53 -0
  40. package/dist/lib/occupancy-fusion.js +131 -0
  41. package/dist/lib/room-controller.d.ts +113 -0
  42. package/dist/lib/room-controller.js +431 -0
  43. package/dist/lib/source-capability-controller.d.ts +144 -0
  44. package/dist/lib/source-capability-controller.js +494 -0
  45. package/dist/lib/switch-policy.d.ts +53 -0
  46. package/dist/lib/switch-policy.js +110 -0
  47. package/dist/lib/theatre-activity-controller.d.ts +165 -0
  48. package/dist/lib/theatre-activity-controller.js +846 -0
  49. package/dist/lib/theatre-slo.d.ts +49 -0
  50. package/dist/lib/theatre-slo.js +156 -0
  51. package/dist/lib/theatre-state-contract.d.ts +77 -0
  52. package/dist/lib/theatre-state-contract.js +391 -0
  53. package/dist/lib/wall-input.d.ts +62 -0
  54. package/dist/lib/wall-input.js +198 -0
  55. package/dist/nodes/activity-lighting-controller.d.ts +3 -0
  56. package/dist/nodes/activity-lighting-controller.html +38 -0
  57. package/dist/nodes/activity-lighting-controller.js +116 -0
  58. package/dist/nodes/avr-controller.d.ts +3 -0
  59. package/dist/nodes/avr-controller.html +47 -0
  60. package/dist/nodes/avr-controller.js +130 -0
  61. package/dist/nodes/backlighting-controller.d.ts +3 -0
  62. package/dist/nodes/backlighting-controller.html +31 -0
  63. package/dist/nodes/backlighting-controller.js +103 -0
  64. package/dist/nodes/display-mode-controller.d.ts +3 -0
  65. package/dist/nodes/display-mode-controller.html +66 -0
  66. package/dist/nodes/display-mode-controller.js +132 -0
  67. package/dist/nodes/motion-policy.d.ts +3 -0
  68. package/dist/nodes/motion-policy.html +35 -0
  69. package/dist/nodes/motion-policy.js +194 -0
  70. package/dist/nodes/occupancy-fusion.d.ts +3 -0
  71. package/dist/nodes/occupancy-fusion.html +32 -0
  72. package/dist/nodes/occupancy-fusion.js +53 -0
  73. package/dist/nodes/room-controller.d.ts +3 -0
  74. package/dist/nodes/room-controller.html +82 -0
  75. package/dist/nodes/room-controller.js +123 -0
  76. package/dist/nodes/source-capability-controller.d.ts +3 -0
  77. package/dist/nodes/source-capability-controller.html +46 -0
  78. package/dist/nodes/source-capability-controller.js +114 -0
  79. package/dist/nodes/switch-policy.d.ts +3 -0
  80. package/dist/nodes/switch-policy.html +29 -0
  81. package/dist/nodes/switch-policy.js +129 -0
  82. package/dist/nodes/theatre-activity-controller.d.ts +3 -0
  83. package/dist/nodes/theatre-activity-controller.html +32 -0
  84. package/dist/nodes/theatre-activity-controller.js +105 -0
  85. package/dist/nodes/theatre-state-contract.d.ts +3 -0
  86. package/dist/nodes/theatre-state-contract.html +41 -0
  87. package/dist/nodes/theatre-state-contract.js +83 -0
  88. package/examples/Home theatre activity.json +477 -0
  89. package/examples/Home theatre equipment.json +329 -0
  90. package/examples/Room lighting.json +212 -0
  91. package/package.json +62 -4
@@ -0,0 +1,105 @@
1
+ "use strict";
2
+ const bounded_trace_1 = require("../lib/bounded-trace");
3
+ const theatre_activity_controller_1 = require("../lib/theatre-activity-controller");
4
+ function number(value, fallback) {
5
+ const parsed = Number(value);
6
+ return Number.isFinite(parsed) ? parsed : fallback;
7
+ }
8
+ const registerTheatreActivityController = (RED) => {
9
+ function HomeTheatreActivityController(config) {
10
+ RED.nodes.createNode(this, config);
11
+ const node = this;
12
+ const context = node.context();
13
+ let timer;
14
+ let activityProfiles;
15
+ try {
16
+ activityProfiles = (0, theatre_activity_controller_1.normaliseActivityProfiles)(JSON.parse(config.activityProfiles || "{}"));
17
+ }
18
+ catch (error) {
19
+ node.status({ fill: "red", shape: "ring", text: "invalid activity profiles" });
20
+ node.error(error);
21
+ return;
22
+ }
23
+ const controllerConfig = {
24
+ area: config.area || "lounge",
25
+ freshnessMs: Math.max(1000, number(config.freshnessMs, 30000)),
26
+ activityProfiles,
27
+ recoveryOffActivity: config.recoveryOffActivity || "off"
28
+ };
29
+ function updateStatus(status) {
30
+ const appearances = {
31
+ idle: { fill: "grey", shape: "dot" },
32
+ transitioning: { fill: "blue", shape: "dot" },
33
+ confirming: { fill: "blue", shape: "ring" },
34
+ waiting_for_source: { fill: "yellow", shape: "ring" },
35
+ recovering: { fill: "yellow", shape: "ring" },
36
+ manual_override: { fill: "yellow", shape: "ring" },
37
+ ready: { fill: "green", shape: "dot" },
38
+ failed: { fill: "red", shape: "ring" }
39
+ };
40
+ const appearance = appearances[status.phase] ?? { fill: "grey", shape: "ring" };
41
+ node.status({ ...appearance, text: status.text });
42
+ }
43
+ function process(msg, send) {
44
+ const now = new Date();
45
+ const saved = context.get("state");
46
+ const result = (0, theatre_activity_controller_1.transitionTheatre)(saved, msg, controllerConfig, now);
47
+ context.set("state", result.state);
48
+ (0, bounded_trace_1.appendTrace)(context, {
49
+ area: controllerConfig.area,
50
+ component: "theatre-activity-controller"
51
+ }, result.traces, now);
52
+ if (result.commands.length) {
53
+ send([
54
+ result.commands.map((theatreCommand) => ({ payload: undefined, theatreCommand })),
55
+ null
56
+ ]);
57
+ }
58
+ if (result.events.length) {
59
+ send([null, result.events.map((event) => ({ payload: undefined, ...event }))]);
60
+ }
61
+ updateStatus(result.status);
62
+ return result;
63
+ }
64
+ node.on("input", (msg, send, done) => {
65
+ // Target controllers' output 2 also carries their own events (such as
66
+ // msg.avr and msg.display) beside target state; only msg.theatre is
67
+ // for this controller.
68
+ if (!msg.theatre || typeof msg.theatre !== "object") {
69
+ done();
70
+ return;
71
+ }
72
+ try {
73
+ process({
74
+ theatre: msg.theatre && typeof msg.theatre === "object"
75
+ ? msg.theatre
76
+ : undefined
77
+ }, send);
78
+ done();
79
+ }
80
+ catch (error) {
81
+ node.error(error, msg);
82
+ done(error instanceof Error ? error : new Error(String(error)));
83
+ }
84
+ });
85
+ timer = setInterval(() => {
86
+ const saved = context.get("state");
87
+ // Recovering has no desired activity yet but still needs ticks to time out.
88
+ if (!saved?.desired && saved?.phase !== "recovering")
89
+ return;
90
+ try {
91
+ process({ theatre: { kind: "tick", generation: saved.generation } }, node.send.bind(node));
92
+ }
93
+ catch (error) {
94
+ node.error(error);
95
+ }
96
+ }, Math.max(1000, Math.floor(controllerConfig.freshnessMs / 2)));
97
+ node.on("close", (_removed, done) => {
98
+ clearInterval(timer);
99
+ done();
100
+ });
101
+ }
102
+ RED.nodes.registerType("home-theatre-activity-controller", HomeTheatreActivityController);
103
+ };
104
+ module.exports = registerTheatreActivityController;
105
+ //# sourceMappingURL=theatre-activity-controller.js.map
@@ -0,0 +1,3 @@
1
+ import type { NodeInitializer } from "node-red";
2
+ declare const registerTheatreStateContract: NodeInitializer;
3
+ export = registerTheatreStateContract;
@@ -0,0 +1,41 @@
1
+ <script type="text/javascript">
2
+ RED.nodes.registerType("home-theatre-state-contract", {
3
+ category: "home automation",
4
+ color: "#d9c7f1",
5
+ defaults: {
6
+ name: {value: ""},
7
+ area: {value: "lounge", required: true},
8
+ displayName: {value: "Lounge", required: true},
9
+ baseTopic: {value: "home/lounge/theatre", required: true},
10
+ discoveryPrefix: {value: "homeassistant", required: true},
11
+ activities: {value: "[]", required: true},
12
+ sloWindowSize: {value: 20, required: true, validate: RED.validators.number()},
13
+ sloMinimumSamples: {value: 5, required: true, validate: RED.validators.number()},
14
+ sloLatencyBudgetMs: {value: 60000, required: true, validate: RED.validators.number()},
15
+ sloSuccessObjectivePct: {value: 95, required: true, validate: RED.validators.number()}
16
+ },
17
+ inputs: 1,
18
+ outputs: 2,
19
+ outputLabels: ["activity requests", "retained MQTT state and HA discovery"],
20
+ icon: "font-awesome/fa-exchange",
21
+ label: function() { return this.name || "theatre state contract"; }
22
+ });
23
+ </script>
24
+
25
+ <script type="text/html" data-template-name="home-theatre-state-contract">
26
+ <div class="form-row"><label for="node-input-name">Name</label><input type="text" id="node-input-name"></div>
27
+ <div class="form-row"><label for="node-input-area">Area</label><input type="text" id="node-input-area"></div>
28
+ <div class="form-row"><label for="node-input-displayName">Display name</label><input type="text" id="node-input-displayName"></div>
29
+ <div class="form-row"><label for="node-input-baseTopic">Base topic</label><input type="text" id="node-input-baseTopic"></div>
30
+ <div class="form-row"><label for="node-input-discoveryPrefix">Discovery prefix</label><input type="text" id="node-input-discoveryPrefix"></div>
31
+ <div class="form-row"><label for="node-input-activities">Activities JSON</label><input type="text" id="node-input-activities"></div>
32
+ <div class="form-row"><label for="node-input-sloWindowSize">SLO window</label><input type="number" id="node-input-sloWindowSize"></div>
33
+ <div class="form-row"><label for="node-input-sloMinimumSamples">Minimum samples</label><input type="number" id="node-input-sloMinimumSamples"></div>
34
+ <div class="form-row"><label for="node-input-sloLatencyBudgetMs">Latency budget (ms)</label><input type="number" id="node-input-sloLatencyBudgetMs"></div>
35
+ <div class="form-row"><label for="node-input-sloSuccessObjectivePct">Success objective (%)</label><input type="number" id="node-input-sloSuccessObjectivePct"></div>
36
+ </script>
37
+
38
+ <script type="text/html" data-help-name="home-theatre-state-contract">
39
+ <p>Bridges non-retained MQTT activity requests to the theatre controller and publishes retained controller truth.</p>
40
+ <p>Requests arrive on <code>&lt;base&gt;/request</code> with a request ID and activity. Retained requests are inert. Complete <code>msg.theatre</code> state snapshots publish desired, current, phase, failure, availability, operational SLO and per-target health topics. A startup event emits Home Assistant discovery for the activity selector and status sensors.</p>
41
+ </script>
@@ -0,0 +1,83 @@
1
+ "use strict";
2
+ const bounded_trace_1 = require("../lib/bounded-trace");
3
+ const theatre_state_contract_1 = require("../lib/theatre-state-contract");
4
+ const registerTheatreStateContract = (RED) => {
5
+ function HomeTheatreStateContract(config) {
6
+ RED.nodes.createNode(this, config);
7
+ const node = this;
8
+ const context = node.context();
9
+ let controllerConfig;
10
+ try {
11
+ const activities = JSON.parse(config.activities || "[]");
12
+ controllerConfig = (0, theatre_state_contract_1.normaliseStateContractConfig)({
13
+ area: config.area || "lounge",
14
+ displayName: config.displayName || config.area || "Lounge",
15
+ baseTopic: config.baseTopic || `home/${config.area || "lounge"}/theatre`,
16
+ discoveryPrefix: config.discoveryPrefix || "homeassistant",
17
+ activities: Array.isArray(activities) ? activities.map(String) : [],
18
+ slo: {
19
+ windowSize: Number(config.sloWindowSize ?? 20),
20
+ minimumSamples: Number(config.sloMinimumSamples ?? 5),
21
+ latencyBudgetMs: Number(config.sloLatencyBudgetMs ?? 60000),
22
+ successObjectivePct: Number(config.sloSuccessObjectivePct ?? 95)
23
+ }
24
+ });
25
+ }
26
+ catch (error) {
27
+ node.status({ fill: "red", shape: "ring", text: "invalid state contract" });
28
+ node.error(error);
29
+ return;
30
+ }
31
+ function process(msg, send) {
32
+ const now = new Date();
33
+ const saved = context.get("state");
34
+ const result = (0, theatre_state_contract_1.transitionTheatreStateContract)(saved, msg, controllerConfig, now);
35
+ context.set("state", result.state);
36
+ (0, bounded_trace_1.appendTrace)(context, {
37
+ area: controllerConfig.area,
38
+ component: "theatre-state-contract"
39
+ }, result.traces, now);
40
+ if (result.requests.length) {
41
+ send([
42
+ result.requests.map((request) => ({ payload: undefined, ...request })),
43
+ null
44
+ ]);
45
+ }
46
+ if (result.publications.length) {
47
+ send([null, result.publications.map((entry) => ({ ...entry }))]);
48
+ }
49
+ node.status({
50
+ fill: result.status.phase === "published" ? "green" : result.status.phase === "requested" ? "blue" : "grey",
51
+ shape: result.status.phase === "idle" ? "ring" : "dot",
52
+ text: result.status.text
53
+ });
54
+ return result;
55
+ }
56
+ node.on("input", (msg, send, done) => {
57
+ try {
58
+ process({
59
+ topic: typeof msg.topic === "string" ? msg.topic : undefined,
60
+ payload: msg.payload,
61
+ retain: msg.retain === true,
62
+ theatre: msg.theatre && typeof msg.theatre === "object"
63
+ ? msg.theatre
64
+ : undefined,
65
+ theatreStateContract: msg.theatreStateContract && typeof msg.theatreStateContract === "object"
66
+ ? msg.theatreStateContract
67
+ : undefined
68
+ }, send);
69
+ done();
70
+ }
71
+ catch (error) {
72
+ node.error(error, msg);
73
+ done(error instanceof Error ? error : new Error(String(error)));
74
+ }
75
+ });
76
+ node.on("close", (_removed, done) => {
77
+ done();
78
+ });
79
+ }
80
+ RED.nodes.registerType("home-theatre-state-contract", HomeTheatreStateContract);
81
+ };
82
+ module.exports = registerTheatreStateContract;
83
+ //# sourceMappingURL=theatre-state-contract.js.map