@avantics/node-red-home-automation 3.0.1 → 4.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 (108) hide show
  1. package/README.md +262 -13
  2. package/dist/lib/activity-lighting-controller.d.ts +7 -1
  3. package/dist/lib/activity-lighting-controller.js +37 -3
  4. package/dist/lib/adopted-request.d.ts +26 -0
  5. package/dist/lib/adopted-request.js +44 -0
  6. package/dist/lib/avr-controller.d.ts +8 -1
  7. package/dist/lib/avr-controller.js +27 -3
  8. package/dist/lib/avr-publishing.d.ts +24 -0
  9. package/dist/lib/avr-publishing.js +80 -0
  10. package/dist/lib/backlighting-controller.d.ts +7 -1
  11. package/dist/lib/backlighting-controller.js +26 -3
  12. package/dist/lib/circadian.d.ts +1 -0
  13. package/dist/lib/circadian.js +3 -0
  14. package/dist/lib/cover-availability.d.ts +7 -0
  15. package/dist/lib/cover-availability.js +24 -0
  16. package/dist/lib/cover-controller.d.ts +13 -1
  17. package/dist/lib/cover-controller.js +113 -30
  18. package/dist/lib/cover-group.d.ts +62 -0
  19. package/dist/lib/cover-group.js +125 -0
  20. package/dist/lib/cover-models.d.ts +37 -0
  21. package/dist/lib/cover-models.js +83 -0
  22. package/dist/lib/cover-publishing.d.ts +40 -0
  23. package/dist/lib/cover-publishing.js +102 -0
  24. package/dist/lib/cover-requests.d.ts +21 -0
  25. package/dist/lib/cover-requests.js +49 -0
  26. package/dist/lib/display-mode-controller.d.ts +4 -0
  27. package/dist/lib/display-mode-controller.js +26 -2
  28. package/dist/lib/ha-discovery.d.ts +27 -0
  29. package/dist/lib/ha-discovery.js +91 -0
  30. package/dist/lib/house-schedule-publishing.d.ts +23 -0
  31. package/dist/lib/house-schedule-publishing.js +71 -0
  32. package/dist/lib/house-schedule.d.ts +23 -0
  33. package/dist/lib/house-schedule.js +29 -0
  34. package/dist/lib/motion-policy.d.ts +9 -0
  35. package/dist/lib/motion-policy.js +45 -11
  36. package/dist/lib/motion-publishing.d.ts +28 -0
  37. package/dist/lib/motion-publishing.js +92 -0
  38. package/dist/lib/occupancy-publishing.d.ts +10 -0
  39. package/dist/lib/occupancy-publishing.js +45 -0
  40. package/dist/lib/open-close-cover.d.ts +92 -0
  41. package/dist/lib/open-close-cover.js +287 -0
  42. package/dist/lib/publishing-node.d.ts +39 -0
  43. package/dist/lib/publishing-node.js +72 -0
  44. package/dist/lib/retained-topics.d.ts +10 -0
  45. package/dist/lib/retained-topics.js +19 -0
  46. package/dist/lib/room-controller.d.ts +9 -0
  47. package/dist/lib/room-controller.js +47 -56
  48. package/dist/lib/room-lighting-discovery.d.ts +20 -1
  49. package/dist/lib/room-lighting-discovery.js +135 -13
  50. package/dist/lib/room-publishing.d.ts +42 -0
  51. package/dist/lib/room-publishing.js +182 -0
  52. package/dist/lib/room-schedule.d.ts +8 -0
  53. package/dist/lib/room-schedule.js +41 -0
  54. package/dist/lib/settings.d.ts +2 -0
  55. package/dist/lib/settings.js +20 -0
  56. package/dist/lib/source-capability-controller.d.ts +8 -1
  57. package/dist/lib/source-capability-controller.js +29 -3
  58. package/dist/lib/theatre-activity-controller.d.ts +0 -1
  59. package/dist/lib/theatre-activity-controller.js +27 -24
  60. package/dist/lib/theatre-ha-discovery.d.ts +3 -4
  61. package/dist/lib/theatre-ha-discovery.js +46 -36
  62. package/dist/lib/theatre-state-contract.d.ts +6 -0
  63. package/dist/lib/theatre-state-contract.js +83 -47
  64. package/dist/lib/theatre-state.d.ts +1 -1
  65. package/dist/lib/theatre-state.js +1 -1
  66. package/dist/lib/topic-names.d.ts +4 -0
  67. package/dist/lib/topic-names.js +31 -0
  68. package/dist/nodes/activity-lighting-controller.js +5 -3
  69. package/dist/nodes/adapter.d.ts +6 -1
  70. package/dist/nodes/adapter.js +65 -1
  71. package/dist/nodes/avr-controller.html +59 -5
  72. package/dist/nodes/avr-controller.js +74 -4
  73. package/dist/nodes/backlighting-controller.js +5 -3
  74. package/dist/nodes/cover-controller.d.ts +3 -0
  75. package/dist/nodes/cover-controller.html +177 -0
  76. package/dist/nodes/cover-controller.js +178 -0
  77. package/dist/nodes/cover-group.d.ts +3 -0
  78. package/dist/nodes/cover-group.html +89 -0
  79. package/dist/nodes/cover-group.js +84 -0
  80. package/dist/nodes/flows-started.d.ts +4 -0
  81. package/dist/nodes/flows-started.js +39 -0
  82. package/dist/nodes/house-schedule-mqtt.d.ts +3 -0
  83. package/dist/nodes/house-schedule-mqtt.html +61 -0
  84. package/dist/nodes/house-schedule-mqtt.js +80 -0
  85. package/dist/nodes/house-schedule.d.ts +3 -0
  86. package/dist/nodes/house-schedule.html +31 -0
  87. package/dist/nodes/house-schedule.js +51 -0
  88. package/dist/nodes/motion-policy.html +81 -5
  89. package/dist/nodes/motion-policy.js +69 -8
  90. package/dist/nodes/occupancy-fusion.html +69 -14
  91. package/dist/nodes/occupancy-fusion.js +46 -4
  92. package/dist/nodes/publishing-lifecycle.d.ts +25 -0
  93. package/dist/nodes/publishing-lifecycle.js +38 -0
  94. package/dist/nodes/room-controller.html +49 -5
  95. package/dist/nodes/room-controller.js +67 -8
  96. package/dist/nodes/shared-schedule.d.ts +6 -0
  97. package/dist/nodes/shared-schedule.js +15 -0
  98. package/dist/nodes/source-capability-controller.js +5 -3
  99. package/dist/nodes/theatre-activity-controller.html +1 -3
  100. package/dist/nodes/theatre-activity-controller.js +1 -2
  101. package/dist/nodes/theatre-state-contract.html +28 -5
  102. package/dist/nodes/theatre-state-contract.js +22 -4
  103. package/examples/Cover control.json +380 -0
  104. package/examples/Cover pair.json +230 -0
  105. package/examples/Home theatre activity.json +4 -4
  106. package/examples/House schedule.json +164 -0
  107. package/examples/Room lighting.json +4 -3
  108. package/package.json +6 -2
@@ -0,0 +1,61 @@
1
+ <script type="text/javascript">
2
+ (function () {
3
+ function on(value) { return value === true || value === "true"; }
4
+ RED.nodes.registerType("home-house-schedule-mqtt", {
5
+ category: "home automation",
6
+ color: "#ffd27f",
7
+ defaults: {
8
+ // A marker only a node new from the palette has, as core nodes do: import
9
+ // and paste keep the saved name, so onadd leaves a saved node's settings.
10
+ name: {value: "_DEFAULT_"},
11
+ schedule: {value: "", type: "home-house-schedule", required: true},
12
+ discovery: {value: true},
13
+ discoveryPrefix: {value: "homeassistant", required: true, validate: function (v) { var prefix = String(v || "").trim().replace(/^\/+|\/+$/g, ""); return Boolean(prefix) && !/[+#]/.test(prefix); }},
14
+ // How Home Assistant IDs are named; this node has no older entities,
15
+ // so not set also means "node".
16
+ idScheme: {value: null}
17
+ },
18
+ onadd: function () {
19
+ if (this.name !== "_DEFAULT_") return;
20
+ this.name = "";
21
+ this.idScheme = "node";
22
+ },
23
+ oneditprepare: function () {
24
+ $("#node-input-discovery").prop("checked", on(this.discovery));
25
+ },
26
+ inputs: 1,
27
+ outputs: 1,
28
+ outputLabels: ["MQTT publications"],
29
+ icon: "font-awesome/fa-clock-o",
30
+ label: function () { return this.name || "house schedule MQTT"; }
31
+ });
32
+ })();
33
+ </script>
34
+
35
+ <script type="text/html" data-template-name="home-house-schedule-mqtt">
36
+ <div class="form-row"><label for="node-input-name">Name</label><input type="text" id="node-input-name"></div>
37
+ <div class="form-row"><label for="node-input-schedule">Schedule</label><input type="text" id="node-input-schedule"></div>
38
+ <div class="form-row"><label for="node-input-discovery">Home Assistant discovery</label><input type="checkbox" id="node-input-discovery" style="display:inline-block; width:auto;"></div>
39
+ <div class="form-row"><label for="node-input-discoveryPrefix">Discovery prefix</label><input type="text" id="node-input-discoveryPrefix"></div>
40
+ </script>
41
+
42
+ <script type="text/html" data-help-name="home-house-schedule-mqtt">
43
+ <p>A house schedule's MQTT and Home Assistant side. Optional: rooms follow the schedule without it.</p>
44
+ <p>It publishes the schedule's times, retained, on <code>home/&lt;name&gt;/schedule/wake-time/state</code> and
45
+ <code>bed-time/state</code>, with availability on <code>home/&lt;name&gt;/schedule/availability</code>; with
46
+ <b>Home Assistant discovery</b>, wake and bed time entities, named after the schedule so a replacement node keeps
47
+ them. Use one per schedule: two would share the same topics and entities, and deleting one clears them for both
48
+ until the other next starts. Wire its output to an MQTT out node, and an MQTT in node on the set topics to its input.</p>
49
+ <h3>Inputs</h3>
50
+ <dl class="message-properties">
51
+ <dt>topic = home/&lt;name&gt;/schedule/wake-time/set or bed-time/set</dt><dd>a time from Home Assistant.
52
+ Retained messages and anything but a time are ignored.</dd>
53
+ <dt>lighting</dt><dd><code>{kind: "schedule", wakeTime, bedTime}</code>, each <code>HH:MM</code> and either one
54
+ alone: new times for the schedule.</dd>
55
+ <dt>source = "startup"</dt><dd>announce again, as after the broker reconnects.</dd>
56
+ </dl>
57
+ <h3>Outputs</h3>
58
+ <ol class="node-ports">
59
+ <li>MQTT publications.</li>
60
+ </ol>
61
+ </script>
@@ -0,0 +1,80 @@
1
+ "use strict";
2
+ const ha_discovery_1 = require("../lib/ha-discovery");
3
+ const house_schedule_publishing_1 = require("../lib/house-schedule-publishing");
4
+ const settings_1 = require("../lib/settings");
5
+ const topic_names_1 = require("../lib/topic-names");
6
+ const adapter_1 = require("./adapter");
7
+ const publishing_lifecycle_1 = require("./publishing-lifecycle");
8
+ const shared_schedule_1 = require("./shared-schedule");
9
+ const registerHouseScheduleMqtt = (RED) => {
10
+ function HomeHouseScheduleMqtt(config) {
11
+ RED.nodes.createNode(this, config);
12
+ const node = this;
13
+ let house;
14
+ let publishing;
15
+ try {
16
+ const referenced = (0, shared_schedule_1.referencedSchedule)(RED, config.schedule);
17
+ if (!referenced.schedule)
18
+ throw new Error(referenced.missing ? "the house schedule is missing" : "no house schedule");
19
+ house = referenced.schedule;
20
+ publishing = {
21
+ area: (0, topic_names_1.publishingArea)(house.name, true),
22
+ // Publishing is what this node is for; discovery is off unless
23
+ // switched on.
24
+ publishState: true,
25
+ discovery: (0, settings_1.optionalBooleanSetting)(config.discovery, "Home Assistant discovery") ?? false,
26
+ discoveryPrefix: (0, ha_discovery_1.discoveryPrefixSetting)(config.discoveryPrefix),
27
+ // Named after the schedule, not this node, so a replacement node
28
+ // keeps the same entities.
29
+ nodeId: config.schedule,
30
+ idScheme: (0, settings_1.optionalChoiceSetting)(config.idScheme, "ID scheme", ha_discovery_1.ID_SCHEMES) ?? "node"
31
+ };
32
+ }
33
+ catch (error) {
34
+ (0, adapter_1.invalidConfig)(node, error);
35
+ return;
36
+ }
37
+ const trace = { area: publishing.area, component: "house-schedule-mqtt" };
38
+ function showTimes() {
39
+ const { wakeTime, bedTime } = house.schedule();
40
+ node.status({ fill: "green", shape: "dot", text: `wake ${wakeTime} · bed ${bedTime}` });
41
+ }
42
+ const lifecycle = (0, publishing_lifecycle_1.publishingLifecycle)(RED, node, {
43
+ retainedTopics: () => (0, house_schedule_publishing_1.houseRetainedTopics)(publishing),
44
+ // Its own entities, named after the schedule, cleared when discovery is
45
+ // switched off, with or without a record (see ownDiscoveryTopics).
46
+ possible: { owner: publishing, topics: () => (0, house_schedule_publishing_1.houseRetainedTopics)({ ...publishing, discovery: true }) },
47
+ starting: () => (0, house_schedule_publishing_1.houseStartingOutputs)(publishing, house.schedule()),
48
+ stopping: () => (0, house_schedule_publishing_1.houseStoppingOutputs)(publishing),
49
+ output: 0,
50
+ outputs: 1
51
+ });
52
+ const unsubscribe = house.subscribe((0, adapter_1.guarded)(node, () => {
53
+ lifecycle.publish(node.send.bind(node), (0, house_schedule_publishing_1.houseStateOutputs)(publishing, house.schedule()));
54
+ showTimes();
55
+ }));
56
+ node.on("close", unsubscribe);
57
+ showTimes();
58
+ (0, adapter_1.onInput)(node, (msg, send) => {
59
+ const lighting = (0, adapter_1.objectField)(msg, "lighting");
60
+ if (lighting?.kind === "startup" || msg.source === "startup") {
61
+ lifecycle.startup(send, (0, house_schedule_publishing_1.houseStartingOutputs)(publishing, house.schedule()));
62
+ return;
63
+ }
64
+ if (lighting?.kind === "schedule") {
65
+ house.change({ kind: "schedule", wakeTime: lighting.wakeTime, bedTime: lighting.bedTime });
66
+ return;
67
+ }
68
+ const inbound = (0, house_schedule_publishing_1.houseInbound)(msg, publishing);
69
+ if (inbound?.event) {
70
+ house.change(inbound.event, inbound.traces);
71
+ }
72
+ else if (inbound) {
73
+ (0, adapter_1.runReducer)(node, trace, (state) => ({ state, traces: inbound.traces }));
74
+ }
75
+ });
76
+ }
77
+ RED.nodes.registerType("home-house-schedule-mqtt", HomeHouseScheduleMqtt);
78
+ };
79
+ module.exports = registerHouseScheduleMqtt;
80
+ //# sourceMappingURL=house-schedule-mqtt.js.map
@@ -0,0 +1,3 @@
1
+ import type { NodeInitializer } from "node-red";
2
+ declare const registerHouseSchedule: NodeInitializer;
3
+ export = registerHouseSchedule;
@@ -0,0 +1,31 @@
1
+ <script type="text/javascript">
2
+ (function () {
3
+ function clock(v) { return /^([01]\d|2[0-3]):[0-5]\d$/.test(String(v || "").trim()); }
4
+ // The schedule rooms share: a config node, with no wires.
5
+ RED.nodes.registerType("home-house-schedule", {
6
+ category: "config",
7
+ defaults: {
8
+ // Also its name in topics, home/<name>/schedule/...
9
+ name: {value: "house", required: true, validate: function (v) { return /[a-z0-9]/i.test(String(v || "")); }},
10
+ wakeTime: {value: "07:00", required: true, validate: clock},
11
+ bedTime: {value: "23:00", required: true, validate: function (v) { return clock(v) && String(v).trim() !== String($("#node-config-input-wakeTime").val() || this.wakeTime || "").trim(); }}
12
+ },
13
+ label: function () { return this.name || "house schedule"; }
14
+ });
15
+ })();
16
+ </script>
17
+
18
+ <script type="text/html" data-template-name="home-house-schedule">
19
+ <div class="form-row"><label for="node-config-input-name">Name</label><input type="text" id="node-config-input-name" placeholder="house"></div>
20
+ <div class="form-row"><label for="node-config-input-wakeTime">Wake time</label><input type="text" id="node-config-input-wakeTime" placeholder="07:00"></div>
21
+ <div class="form-row"><label for="node-config-input-bedTime">Bed time</label><input type="text" id="node-config-input-bedTime" placeholder="23:00"></div>
22
+ </script>
23
+
24
+ <script type="text/html" data-help-name="home-house-schedule">
25
+ <p>One wake and bed time shared by every room lighting controller whose <b>Schedule</b> is this one. Rooms that keep
26
+ their own times (a child's room, say) choose none.</p>
27
+ <p>The rooms hear of every change as it happens, without wires. Times change from a message or from Home Assistant
28
+ through a <i>house schedule MQTT</i> node; a time changed that way lasts until this schedule is deployed with a
29
+ different <b>Wake time</b> or <b>Bed time</b>, which then applies.</p>
30
+ <p>The <b>Name</b> labels it and names its topics, <code>home/&lt;name&gt;/schedule/...</code>.</p>
31
+ </script>
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ const circadian_schedule_1 = require("../lib/circadian-schedule");
3
+ const house_schedule_1 = require("../lib/house-schedule");
4
+ const settings_1 = require("../lib/settings");
5
+ const adapter_1 = require("./adapter");
6
+ const registerHouseSchedule = (RED) => {
7
+ function HomeHouseSchedule(config) {
8
+ RED.nodes.createNode(this, config);
9
+ const node = this;
10
+ let setting;
11
+ let name;
12
+ try {
13
+ setting = (0, circadian_schedule_1.resolveSchedule)((0, settings_1.textSetting)(config.wakeTime, "wake time", "07:00"), (0, settings_1.textSetting)(config.bedTime, "bed time", "23:00"));
14
+ // Also its name in topics, home/<name>/schedule/...
15
+ name = (0, settings_1.textSetting)(config.name, "name", "house");
16
+ }
17
+ catch (error) {
18
+ (0, adapter_1.invalidConfig)(node, error);
19
+ return;
20
+ }
21
+ const trace = { area: name, component: "house-schedule" };
22
+ const listeners = new Set();
23
+ const shared = {
24
+ name,
25
+ schedule: () => (0, house_schedule_1.houseScheduleInUse)(node.context().get("state"), setting),
26
+ change(event, traces = []) {
27
+ const result = (0, adapter_1.runReducer)(node, trace, (saved) => {
28
+ const decided = (0, house_schedule_1.transitionHouseSchedule)(saved, event, setting);
29
+ return { ...decided, traces: [...traces, ...decided.traces] };
30
+ });
31
+ if (!result.changed)
32
+ return;
33
+ for (const listener of [...listeners])
34
+ listener({ ...result.state.schedule });
35
+ },
36
+ subscribe(listener) {
37
+ listeners.add(listener);
38
+ return () => {
39
+ listeners.delete(listener);
40
+ };
41
+ }
42
+ };
43
+ Object.assign(node, shared);
44
+ node.on("close", () => {
45
+ listeners.clear();
46
+ });
47
+ }
48
+ RED.nodes.registerType("home-house-schedule", HomeHouseSchedule);
49
+ };
50
+ module.exports = registerHouseSchedule;
51
+ //# sourceMappingURL=house-schedule.js.map
@@ -1,20 +1,59 @@
1
1
  <script type="text/javascript">
2
+ (function () {
3
+ function on(value) { return value === true || value === "true"; }
2
4
  RED.nodes.registerType("home-motion-lighting-policy", {
3
5
  category: "home automation",
4
6
  color: "#d8bfd8",
5
7
  defaults: {
6
- name: {value: ""},
7
- area: {value: ""},
8
+ // A marker only a node new from the palette has, as core nodes do: import
9
+ // and paste keep the saved name, so onadd leaves a saved node's settings.
10
+ name: {value: "_DEFAULT_"},
11
+ // Required only when the node publishes, and then with a letter or
12
+ // digit, from which its topics and IDs are made.
13
+ area: {value: "", validate: function (v) {
14
+ var publishing = $("#node-input-publishState").length ? $("#node-input-publishState").prop("checked") : on(this.publishState);
15
+ return !publishing || /[a-z0-9]/i.test(String(v || ""));
16
+ }},
8
17
  onTargets: {value: "[\"main\"]", required: true, validate: function (v) { try { JSON.parse(v); return true; } catch (e) { return false; } }},
9
18
  offTargets: {value: "[\"main\"]", required: true, validate: function (v) { try { JSON.parse(v); return true; } catch (e) { return false; } }},
10
19
  defaultDelaySeconds: {value: 120, required: true, validate: RED.validators.number()},
11
- defaultIlluminanceCutoff: {value: 10, required: true, validate: RED.validators.number()}
20
+ defaultIlluminanceCutoff: {value: 10, required: true, validate: RED.validators.number()},
21
+ // How long a pause lasts unless renewed, so a lost release can't stop
22
+ // motion lighting for good.
23
+ maxPauseSeconds: {value: 600, required: true, validate: function (v) { return RED.validators.number()(v) && Number(v) >= 1; }},
24
+ // Motion published nothing before these settings, so both start off.
25
+ publishState: {value: false},
26
+ discovery: {value: false},
27
+ discoveryPrefix: {value: "homeassistant", required: true, validate: function (v) { var prefix = String(v || "").trim().replace(/^\/+|\/+$/g, ""); return Boolean(prefix) && !/[+#]/.test(prefix); }},
28
+ // How Home Assistant IDs are named; motion has no older entities, so not
29
+ // set also means "node".
30
+ idScheme: {value: null}
31
+ },
32
+ onadd: function () {
33
+ if (this.name !== "_DEFAULT_") return;
34
+ this.name = "";
35
+ this.idScheme = "node";
36
+ },
37
+ oneditprepare: function () {
38
+ $("#node-input-publishState").prop("checked", on(this.publishState));
39
+ $("#node-input-discovery").prop("checked", on(this.discovery));
40
+ // Discovery describes the published state, so it needs it.
41
+ function follow() {
42
+ var publishing = $("#node-input-publishState").prop("checked");
43
+ if (!publishing) $("#node-input-discovery").prop("checked", false);
44
+ $("#node-input-discovery").prop("disabled", !publishing);
45
+ $("#node-input-area").trigger("change");
46
+ }
47
+ $("#node-input-publishState, #node-input-discovery").on("change", follow);
48
+ follow();
12
49
  },
13
50
  inputs: 1,
14
- outputs: 1,
51
+ outputs: 2,
52
+ outputLabels: ["lighting intents", "MQTT publications"],
15
53
  icon: "font-awesome/fa-lightbulb-o",
16
54
  label: function () { return this.name || (this.area ? this.area + " " : "") + "motion policy"; }
17
55
  });
56
+ })();
18
57
  </script>
19
58
 
20
59
  <script type="text/html" data-template-name="home-motion-lighting-policy">
@@ -24,6 +63,10 @@
24
63
  <div class="form-row"><label for="node-input-offTargets">OFF targets</label><input type="text" id="node-input-offTargets"></div>
25
64
  <div class="form-row"><label for="node-input-defaultDelaySeconds">Vacancy seconds</label><input type="number" id="node-input-defaultDelaySeconds"></div>
26
65
  <div class="form-row"><label for="node-input-defaultIlluminanceCutoff">Lux cutoff</label><input type="number" id="node-input-defaultIlluminanceCutoff"></div>
66
+ <div class="form-row"><label for="node-input-maxPauseSeconds">Maximum pause (s)</label><input type="number" id="node-input-maxPauseSeconds"></div>
67
+ <div class="form-row"><label for="node-input-publishState">Publish state over MQTT</label><input type="checkbox" id="node-input-publishState" style="display:inline-block; width:auto;"></div>
68
+ <div class="form-row"><label for="node-input-discovery">Home Assistant discovery</label><input type="checkbox" id="node-input-discovery" style="display:inline-block; width:auto;"> <span>(needs Publish state)</span></div>
69
+ <div class="form-row"><label for="node-input-discoveryPrefix">Discovery prefix</label><input type="text" id="node-input-discoveryPrefix"></div>
27
70
  </script>
28
71
 
29
72
  <script type="text/html" data-help-name="home-motion-lighting-policy">
@@ -41,8 +84,41 @@
41
84
  <dt>area-enabled</dt><dd>ON or OFF for this node; <code>global-enabled</code> for every motion node.</dd>
42
85
  <dt>delay</dt><dd>a new vacancy time, in <b>seconds</b>, the same unit as <b>Vacancy seconds</b>.</dd>
43
86
  <dt>illuminance-cut-off</dt><dd>a new lux cutoff.</dd>
87
+ <dt>pause</dt><dd><code>true</code> pauses motion lighting, <code>false</code> releases it, for the owner in
88
+ <code>msg.owner</code> (for example a cover's name), which is required. A pause lasts at most the
89
+ <b>Maximum pause</b> unless sent again, so a lost release can't stop motion lighting for good. Motion lighting acts only while no owner holds a pause,
90
+ and a pause cancels a pending vacancy timer, as switching it off does. A pause never changes the enables,
91
+ which are the user's own: wire a cover's <i>moving</i> output here to pause motion while it moves.</dd>
44
92
  </dl>
45
93
  <p>A <code>delay</code> or <code>illuminance-cut-off</code> message overrides the setting, and lasts across
46
94
  restarts until the node is deployed with a different <b>Vacancy seconds</b> or <b>Lux cutoff</b>, which then
47
- applies.</p>
95
+ applies. Lasting across a redeploy or restart needs a context store (see the README's recommended setup).</p>
96
+ <p>When the node starts with nothing saved (its first deploy, or a redeploy or restart without a context store),
97
+ it doesn't know whether the room is occupied, so it counts the room as vacant from then: unless occupancy is
98
+ reported within <b>Vacancy seconds</b>, it sends its off intent. Lights left on when Node-RED stopped are not
99
+ left on for good, but someone sitting still in a lit room has the lights switched off after the delay unless
100
+ motion is seen. After a restart the room controller has also forgotten which lights a person set, so it doesn't
101
+ keep those either. With a context store, a running off-timer carries on instead.</p>
102
+ <h3>Publishing</h3>
103
+ <p>Off unless switched on, and then named after the <b>Area</b>, which is needed.</p>
104
+ <dl class="message-properties">
105
+ <dt>Publish state over MQTT</dt><dd>whether this node's motion lighting is enabled, retained <code>ON</code> or
106
+ <code>OFF</code> on <code>home/&lt;area&gt;/motion/enabled/state</code>, and its vacancy delay in seconds on
107
+ <code>home/&lt;area&gt;/motion/delay/state</code>, with <code>online</code> or <code>offline</code> on
108
+ <code>home/&lt;area&gt;/motion/availability</code>. It acts on <code>ON</code>/<code>OFF</code> sent to
109
+ <code>…/enabled/set</code> and a delay of 0 to 86400 seconds sent to <code>…/delay/set</code>, as the
110
+ <code>area-enabled</code> and <code>delay</code> inputs do; wire an MQTT in node on
111
+ <code>home/&lt;area&gt;/motion/+/set</code> into this node. The shared <code>global-enabled</code> is not
112
+ published, and the switch shows this node's enable only, so motion can still be off everywhere while it
113
+ shows on.</dd>
114
+ <dt>Home Assistant discovery</dt><dd>a motion lighting switch and a vacancy delay number (in the device's
115
+ configuration). It needs <b>Publish state</b>.</dd>
116
+ </dl>
117
+ <p>These go on the second output; wire it to an MQTT out node. The node announces itself once the flows have
118
+ started, and again when it receives <code>msg.source = "startup"</code>.</p>
119
+ <h3>Outputs</h3>
120
+ <ol class="node-ports">
121
+ <li>Lighting intents, for the room lighting controller.</li>
122
+ <li>MQTT publications, when publishing is on.</li>
123
+ </ol>
48
124
  </script>
@@ -1,6 +1,12 @@
1
1
  "use strict";
2
2
  const motion_policy_1 = require("../lib/motion-policy");
3
+ const motion_publishing_1 = require("../lib/motion-publishing");
4
+ const ha_discovery_1 = require("../lib/ha-discovery");
5
+ const settings_1 = require("../lib/settings");
6
+ const topic_names_1 = require("../lib/topic-names");
3
7
  const adapter_1 = require("./adapter");
8
+ const publishing_node_1 = require("../lib/publishing-node");
9
+ const publishing_lifecycle_1 = require("./publishing-lifecycle");
4
10
  const registerMotionPolicy = (RED) => {
5
11
  function HomeMotionLightingPolicy(config) {
6
12
  RED.nodes.createNode(this, config);
@@ -9,8 +15,24 @@ const registerMotionPolicy = (RED) => {
9
15
  const globalContext = context.global;
10
16
  let offTimer;
11
17
  let policyConfig;
18
+ let publishing;
12
19
  try {
13
- policyConfig = { area: (0, adapter_1.optionalArea)(config), ...(0, motion_policy_1.normaliseMotionConfig)(config) };
20
+ const area = (0, adapter_1.optionalArea)(config);
21
+ policyConfig = { area, ...(0, motion_policy_1.normaliseMotionConfig)(config) };
22
+ // Off unless switched on, as motion published nothing before; there are
23
+ // no older entities to keep, so not set names them after the node.
24
+ const publishState = (0, settings_1.optionalBooleanSetting)(config.publishState, "publish state") ?? false;
25
+ const discovery = (0, settings_1.optionalBooleanSetting)(config.discovery, "Home Assistant discovery") ?? false;
26
+ if (publishState || discovery)
27
+ (0, topic_names_1.publishingArea)(area, true);
28
+ publishing = {
29
+ area,
30
+ publishState,
31
+ discovery,
32
+ discoveryPrefix: (0, ha_discovery_1.discoveryPrefixSetting)(config.discoveryPrefix),
33
+ nodeId: node.id,
34
+ idScheme: (0, settings_1.optionalChoiceSetting)(config.idScheme, "ID scheme", ha_discovery_1.ID_SCHEMES) ?? "node"
35
+ };
14
36
  }
15
37
  catch (error) {
16
38
  (0, adapter_1.invalidConfig)(node, error);
@@ -20,7 +42,7 @@ const registerMotionPolicy = (RED) => {
20
42
  node.status({
21
43
  fill: status.enabled ? "green" : "grey",
22
44
  shape: "dot",
23
- text: (status.enabled ? "enabled" : "disabled") + " · " +
45
+ text: (status.enabled ? "enabled" : status.paused ? "paused" : "disabled") + " · " +
24
46
  (status.illuminance === null ? "lux ?" : status.illuminance + " lux") + " · " +
25
47
  status.delaySeconds + "s"
26
48
  });
@@ -32,9 +54,30 @@ const registerMotionPolicy = (RED) => {
32
54
  process({ source: "off-timer", generation }, node.send.bind(node));
33
55
  }), delayMs);
34
56
  }
35
- function process(input, send) {
57
+ // The enable state and delay in use, by the reducer's own rule: a saved
58
+ // delay stands only while the setting it was based on is unchanged.
59
+ function savedPolicyState() {
60
+ return (0, motion_policy_1.createMotionState)(policyConfig, context.get("state"));
61
+ }
62
+ // MQTT publications go on the second output, away from the lighting
63
+ // intents the room controller reads on the first. Available, and clear
64
+ // of what it no longer publishes, as soon as it runs; offline as it
65
+ // stops; nothing left behind when deleted.
66
+ const lifecycle = (0, publishing_lifecycle_1.publishingLifecycle)(RED, node, {
67
+ retainedTopics: () => (0, motion_publishing_1.motionRetainedTopics)(publishing),
68
+ possible: { owner: publishing, topics: () => (0, publishing_node_1.publishesState)(publishing) ? (0, motion_publishing_1.motionRetainedTopics)({ ...publishing, publishState: true, discovery: true }) : [] },
69
+ starting: () => (0, motion_publishing_1.motionStartingOutputs)(publishing, savedPolicyState()),
70
+ stopping: () => (0, motion_publishing_1.motionStoppingOutputs)(publishing),
71
+ output: 1,
72
+ outputs: 2
73
+ });
74
+ function process(input, send, inboundTraces = null) {
36
75
  const globalEnabled = globalContext.get("MOTION_ENABLED_STATE") !== false;
37
- const result = (0, adapter_1.runReducer)(node, { area: policyConfig.area, component: "motion-policy" }, (saved, now) => (0, motion_policy_1.transitionMotion)(saved, input, policyConfig, globalEnabled, now.getTime()));
76
+ const before = savedPolicyState();
77
+ const result = (0, adapter_1.runReducer)(node, { area: policyConfig.area, component: "motion-policy" }, (saved, now) => {
78
+ const decided = (0, motion_policy_1.transitionMotion)(saved, input, policyConfig, globalEnabled, now.getTime());
79
+ return { ...decided, traces: [...(inboundTraces?.traces ?? []), ...decided.traces] };
80
+ });
38
81
  if (input.source === "global-enabled") {
39
82
  globalContext.set("MOTION_ENABLED_STATE", result.globalEnabled);
40
83
  }
@@ -47,20 +90,38 @@ const registerMotionPolicy = (RED) => {
47
90
  armOffTimer(effect.delayMs, effect.generation);
48
91
  }
49
92
  else if (effect.kind === "send") {
50
- send({ payload: undefined, ...effect.message });
93
+ send([{ payload: undefined, ...effect.message }, null]);
51
94
  }
52
95
  }
96
+ // Published whenever either changes, and in answer to a set, so Home
97
+ // Assistant shows what is in use.
98
+ if (before.areaEnabled !== result.state.areaEnabled || before.delaySeconds !== result.state.delaySeconds ||
99
+ input.source === "area-enabled" || input.source === "delay") {
100
+ lifecycle.publish(send, (0, motion_publishing_1.motionStateOutputs)(publishing, result.state));
101
+ }
53
102
  updateStatus(result.status);
54
103
  return result;
55
104
  }
56
- // Context survives a redeploy but timers do not: a pending off-timer is
57
- // re-armed.
105
+ // With a configured context store (see the README) the node's state
106
+ // survives a redeploy or restart, but timers never do: a pending
107
+ // off-timer is re-armed. Without one, it starts with nothing saved, and
108
+ // vacancy starts now.
58
109
  const savedState = context.get("state");
59
110
  if (typeof savedState?.offTimerDueAt === "number") {
60
111
  armOffTimer(Math.max(0, savedState.offTimerDueAt - Date.now()), Number(savedState.offTimerGeneration) || 0);
61
112
  }
113
+ else if (savedState === undefined) {
114
+ process({ source: "cold-start" }, node.send.bind(node));
115
+ }
62
116
  (0, adapter_1.onInput)(node, (msg, send) => {
63
- process({ source: String(msg.source ?? ""), payload: msg.payload }, send);
117
+ if (msg.source === "startup") {
118
+ lifecycle.startup(send, (0, motion_publishing_1.motionStartingOutputs)(publishing, savedPolicyState()));
119
+ return;
120
+ }
121
+ // Home Assistant's set topics become the node's own inputs; anything
122
+ // else is an input as sent, with a pause's owner.
123
+ const inbound = (0, motion_publishing_1.motionInbound)(msg, publishing);
124
+ process(inbound?.input ?? { source: String(msg.source ?? ""), payload: msg.payload, owner: typeof msg.owner === "string" ? msg.owner : undefined }, send, inbound);
64
125
  });
65
126
  node.on("close", (_removed, done) => {
66
127
  clearTimeout(offTimer);
@@ -1,22 +1,58 @@
1
1
  <script type="text/javascript">
2
- RED.nodes.registerType("home-occupancy-fusion", {
3
- category: "home automation",
4
- color: "#c7e9c0",
5
- defaults: {
6
- name: {value: ""},
7
- area: {value: ""},
8
- presenceMode: {value: "none", required: true}
9
- },
10
- inputs: 1,
11
- outputs: 1,
12
- icon: "font-awesome/fa-users",
13
- label: function () { return this.name || (this.area ? this.area + " " : "") + "occupancy"; }
14
- });
2
+ (function () {
3
+ function on(value) { return value === true || value === "true"; }
4
+ RED.nodes.registerType("home-occupancy-fusion", {
5
+ category: "home automation",
6
+ color: "#c7e9c0",
7
+ defaults: {
8
+ // A marker only a node new from the palette has, as core nodes do: import
9
+ // and paste keep the saved name, so onadd leaves a saved node's settings.
10
+ name: {value: "_DEFAULT_"},
11
+ // Required only when the node publishes, and then with a letter or
12
+ // digit, from which its topics and IDs are made.
13
+ area: {value: "", validate: function (v) {
14
+ var publishing = $("#node-input-publishState").length ? $("#node-input-publishState").prop("checked") : on(this.publishState);
15
+ return !publishing || /[a-z0-9]/i.test(String(v || ""));
16
+ }},
17
+ presenceMode: {value: "none", required: true},
18
+ // Occupancy published nothing before these settings, so both start off.
19
+ publishState: {value: false},
20
+ discovery: {value: false},
21
+ discoveryPrefix: {value: "homeassistant", required: true, validate: function (v) { var prefix = String(v || "").trim().replace(/^\/+|\/+$/g, ""); return Boolean(prefix) && !/[+#]/.test(prefix); }},
22
+ // How Home Assistant IDs are named; occupancy has no older entities,
23
+ // so not set also means "node".
24
+ idScheme: {value: null}
25
+ },
26
+ onadd: function () {
27
+ if (this.name !== "_DEFAULT_") return;
28
+ this.name = "";
29
+ this.idScheme = "node";
30
+ },
31
+ oneditprepare: function () {
32
+ $("#node-input-publishState").prop("checked", on(this.publishState));
33
+ $("#node-input-discovery").prop("checked", on(this.discovery));
34
+ // Discovery describes the published state, so it needs it.
35
+ function follow() {
36
+ var publishing = $("#node-input-publishState").prop("checked");
37
+ if (!publishing) $("#node-input-discovery").prop("checked", false);
38
+ $("#node-input-discovery").prop("disabled", !publishing);
39
+ $("#node-input-area").trigger("change");
40
+ }
41
+ $("#node-input-publishState, #node-input-discovery").on("change", follow);
42
+ follow();
43
+ },
44
+ inputs: 1,
45
+ outputs: 2,
46
+ outputLabels: ["occupancy events", "MQTT publications"],
47
+ icon: "font-awesome/fa-users",
48
+ label: function () { return this.name || (this.area ? this.area + " " : "") + "occupancy"; }
49
+ });
50
+ })();
15
51
  </script>
16
52
 
17
53
  <script type="text/html" data-template-name="home-occupancy-fusion">
18
54
  <div class="form-row"><label for="node-input-name">Name</label><input type="text" id="node-input-name"></div>
19
- <div class="form-row"><label for="node-input-area">Area</label><input type="text" id="node-input-area"></div>
55
+ <div class="form-row"><label for="node-input-area">Area</label><input type="text" id="node-input-area" placeholder="needed when publishing"></div>
20
56
  <div class="form-row">
21
57
  <label for="node-input-presenceMode">Presence sensor</label>
22
58
  <select id="node-input-presenceMode">
@@ -24,9 +60,28 @@
24
60
  <option value="hold">Hold occupancy (PIR + mmWave)</option>
25
61
  </select>
26
62
  </div>
63
+ <div class="form-row"><label for="node-input-publishState">Publish state over MQTT</label><input type="checkbox" id="node-input-publishState" style="display:inline-block; width:auto;"></div>
64
+ <div class="form-row"><label for="node-input-discovery">Home Assistant discovery</label><input type="checkbox" id="node-input-discovery" style="display:inline-block; width:auto;"> <span>(needs Publish state)</span></div>
65
+ <div class="form-row"><label for="node-input-discoveryPrefix">Discovery prefix</label><input type="text" id="node-input-discoveryPrefix"></div>
27
66
  </script>
28
67
 
29
68
  <script type="text/html" data-help-name="home-occupancy-fusion">
30
69
  <p>Combines normalised <code>motion</code> and optional <code>presence</code> events into <code>msg.source = "occupancy"</code>.</p>
31
70
  <p>PIR establishes occupancy. Presence can hold an established occupancy but cannot establish one by itself.</p>
71
+ <h3>Publishing</h3>
72
+ <p>Off unless switched on. Both are named after the <b>Area</b>, which is needed then; topics use it in lowercase,
73
+ with spaces and other characters as <code>_</code>.</p>
74
+ <dl class="message-properties">
75
+ <dt>Publish state over MQTT</dt><dd>the occupancy, retained <code>ON</code> or <code>OFF</code>, on
76
+ <code>home/&lt;area&gt;/occupancy/state</code> whenever it changes, and <code>online</code> or
77
+ <code>offline</code> on <code>home/&lt;area&gt;/occupancy/availability</code>.</dd>
78
+ <dt>Home Assistant discovery</dt><dd>an occupancy binary sensor reading that state. It needs <b>Publish state</b>.</dd>
79
+ </dl>
80
+ <p>These go on the second output; wire it to an MQTT out node. The node announces itself once the flows have
81
+ started, and again when it receives <code>msg.source = "startup"</code>.</p>
82
+ <h3>Outputs</h3>
83
+ <ol class="node-ports">
84
+ <li>Occupancy events, for the motion lighting policy.</li>
85
+ <li>MQTT publications, when publishing is on.</li>
86
+ </ol>
32
87
  </script>