@avantics/node-red-home-automation 3.0.0 → 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 (110) hide show
  1. package/README.md +285 -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/command-echo.d.ts +2 -0
  15. package/dist/lib/command-echo.js +39 -15
  16. package/dist/lib/cover-availability.d.ts +7 -0
  17. package/dist/lib/cover-availability.js +24 -0
  18. package/dist/lib/cover-controller.d.ts +13 -1
  19. package/dist/lib/cover-controller.js +113 -30
  20. package/dist/lib/cover-group.d.ts +62 -0
  21. package/dist/lib/cover-group.js +125 -0
  22. package/dist/lib/cover-models.d.ts +37 -0
  23. package/dist/lib/cover-models.js +83 -0
  24. package/dist/lib/cover-publishing.d.ts +40 -0
  25. package/dist/lib/cover-publishing.js +102 -0
  26. package/dist/lib/cover-requests.d.ts +21 -0
  27. package/dist/lib/cover-requests.js +49 -0
  28. package/dist/lib/display-mode-controller.d.ts +4 -0
  29. package/dist/lib/display-mode-controller.js +26 -2
  30. package/dist/lib/ha-discovery.d.ts +27 -0
  31. package/dist/lib/ha-discovery.js +91 -0
  32. package/dist/lib/house-schedule-publishing.d.ts +23 -0
  33. package/dist/lib/house-schedule-publishing.js +71 -0
  34. package/dist/lib/house-schedule.d.ts +23 -0
  35. package/dist/lib/house-schedule.js +29 -0
  36. package/dist/lib/motion-policy.d.ts +9 -0
  37. package/dist/lib/motion-policy.js +45 -11
  38. package/dist/lib/motion-publishing.d.ts +28 -0
  39. package/dist/lib/motion-publishing.js +92 -0
  40. package/dist/lib/occupancy-publishing.d.ts +10 -0
  41. package/dist/lib/occupancy-publishing.js +45 -0
  42. package/dist/lib/open-close-cover.d.ts +92 -0
  43. package/dist/lib/open-close-cover.js +287 -0
  44. package/dist/lib/publishing-node.d.ts +39 -0
  45. package/dist/lib/publishing-node.js +72 -0
  46. package/dist/lib/retained-topics.d.ts +10 -0
  47. package/dist/lib/retained-topics.js +19 -0
  48. package/dist/lib/room-controller.d.ts +10 -0
  49. package/dist/lib/room-controller.js +68 -59
  50. package/dist/lib/room-lighting-discovery.d.ts +20 -1
  51. package/dist/lib/room-lighting-discovery.js +135 -13
  52. package/dist/lib/room-publishing.d.ts +42 -0
  53. package/dist/lib/room-publishing.js +182 -0
  54. package/dist/lib/room-schedule.d.ts +8 -0
  55. package/dist/lib/room-schedule.js +41 -0
  56. package/dist/lib/settings.d.ts +2 -0
  57. package/dist/lib/settings.js +20 -0
  58. package/dist/lib/source-capability-controller.d.ts +8 -1
  59. package/dist/lib/source-capability-controller.js +29 -3
  60. package/dist/lib/theatre-activity-controller.d.ts +0 -1
  61. package/dist/lib/theatre-activity-controller.js +27 -24
  62. package/dist/lib/theatre-ha-discovery.d.ts +3 -4
  63. package/dist/lib/theatre-ha-discovery.js +46 -36
  64. package/dist/lib/theatre-state-contract.d.ts +6 -0
  65. package/dist/lib/theatre-state-contract.js +83 -47
  66. package/dist/lib/theatre-state.d.ts +1 -1
  67. package/dist/lib/theatre-state.js +1 -1
  68. package/dist/lib/topic-names.d.ts +4 -0
  69. package/dist/lib/topic-names.js +31 -0
  70. package/dist/nodes/activity-lighting-controller.js +5 -3
  71. package/dist/nodes/adapter.d.ts +6 -1
  72. package/dist/nodes/adapter.js +65 -1
  73. package/dist/nodes/avr-controller.html +59 -5
  74. package/dist/nodes/avr-controller.js +74 -4
  75. package/dist/nodes/backlighting-controller.js +5 -3
  76. package/dist/nodes/cover-controller.d.ts +3 -0
  77. package/dist/nodes/cover-controller.html +177 -0
  78. package/dist/nodes/cover-controller.js +178 -0
  79. package/dist/nodes/cover-group.d.ts +3 -0
  80. package/dist/nodes/cover-group.html +89 -0
  81. package/dist/nodes/cover-group.js +84 -0
  82. package/dist/nodes/flows-started.d.ts +4 -0
  83. package/dist/nodes/flows-started.js +39 -0
  84. package/dist/nodes/house-schedule-mqtt.d.ts +3 -0
  85. package/dist/nodes/house-schedule-mqtt.html +61 -0
  86. package/dist/nodes/house-schedule-mqtt.js +80 -0
  87. package/dist/nodes/house-schedule.d.ts +3 -0
  88. package/dist/nodes/house-schedule.html +31 -0
  89. package/dist/nodes/house-schedule.js +51 -0
  90. package/dist/nodes/motion-policy.html +81 -5
  91. package/dist/nodes/motion-policy.js +69 -8
  92. package/dist/nodes/occupancy-fusion.html +69 -14
  93. package/dist/nodes/occupancy-fusion.js +46 -4
  94. package/dist/nodes/publishing-lifecycle.d.ts +25 -0
  95. package/dist/nodes/publishing-lifecycle.js +38 -0
  96. package/dist/nodes/room-controller.html +54 -5
  97. package/dist/nodes/room-controller.js +67 -8
  98. package/dist/nodes/shared-schedule.d.ts +6 -0
  99. package/dist/nodes/shared-schedule.js +15 -0
  100. package/dist/nodes/source-capability-controller.js +5 -3
  101. package/dist/nodes/theatre-activity-controller.html +1 -3
  102. package/dist/nodes/theatre-activity-controller.js +1 -2
  103. package/dist/nodes/theatre-state-contract.html +28 -5
  104. package/dist/nodes/theatre-state-contract.js +22 -4
  105. package/examples/Cover control.json +380 -0
  106. package/examples/Cover pair.json +230 -0
  107. package/examples/Home theatre activity.json +4 -4
  108. package/examples/House schedule.json +164 -0
  109. package/examples/Room lighting.json +4 -3
  110. package/package.json +6 -2
@@ -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>
@@ -1,18 +1,39 @@
1
1
  "use strict";
2
2
  const occupancy_fusion_1 = require("../lib/occupancy-fusion");
3
- const adapter_1 = require("./adapter");
3
+ const occupancy_publishing_1 = require("../lib/occupancy-publishing");
4
+ const ha_discovery_1 = require("../lib/ha-discovery");
4
5
  const settings_1 = require("../lib/settings");
6
+ const topic_names_1 = require("../lib/topic-names");
7
+ const adapter_1 = require("./adapter");
8
+ const publishing_node_1 = require("../lib/publishing-node");
9
+ const publishing_lifecycle_1 = require("./publishing-lifecycle");
5
10
  const registerOccupancyFusion = (RED) => {
6
11
  function HomeOccupancyFusion(config) {
7
12
  RED.nodes.createNode(this, config);
8
13
  const node = this;
9
14
  let fusionConfig;
15
+ let publishing;
10
16
  try {
11
17
  // The editor offers "none" (PIR only) and "hold" (PIR plus presence).
12
18
  const presence = (0, settings_1.textSetting)(config.presenceMode, "presence sensor", "none");
13
19
  if (presence !== "none" && presence !== "hold")
14
20
  throw new Error("presence sensor must be none or hold");
15
- fusionConfig = { area: (0, adapter_1.optionalArea)(config), presenceMode: presence === "hold" ? "hold" : "ignore" };
21
+ const area = (0, adapter_1.optionalArea)(config);
22
+ fusionConfig = { area, presenceMode: presence === "hold" ? "hold" : "ignore" };
23
+ // Off unless switched on, as occupancy published nothing before; there
24
+ // are no older entities to keep, so not set names them after the node.
25
+ const publishState = (0, settings_1.optionalBooleanSetting)(config.publishState, "publish state") ?? false;
26
+ const discovery = (0, settings_1.optionalBooleanSetting)(config.discovery, "Home Assistant discovery") ?? false;
27
+ if (publishState || discovery)
28
+ (0, topic_names_1.publishingArea)(area, true);
29
+ publishing = {
30
+ area,
31
+ publishState,
32
+ discovery,
33
+ discoveryPrefix: (0, ha_discovery_1.discoveryPrefixSetting)(config.discoveryPrefix),
34
+ nodeId: node.id,
35
+ idScheme: (0, settings_1.optionalChoiceSetting)(config.idScheme, "ID scheme", ha_discovery_1.ID_SCHEMES) ?? "node"
36
+ };
16
37
  }
17
38
  catch (error) {
18
39
  (0, adapter_1.invalidConfig)(node, error);
@@ -30,14 +51,35 @@ const registerOccupancyFusion = (RED) => {
30
51
  text: details.join(" · ")
31
52
  });
32
53
  }
54
+ function occupied() {
55
+ return node.context().get("state")?.occupied === true;
56
+ }
57
+ // MQTT publications go on the second output, away from the occupancy
58
+ // events the motion policy reads on the first. Available, and clear of
59
+ // what it no longer publishes, as soon as it runs; offline as it stops;
60
+ // nothing left behind when deleted.
61
+ const lifecycle = (0, publishing_lifecycle_1.publishingLifecycle)(RED, node, {
62
+ retainedTopics: () => (0, occupancy_publishing_1.occupancyRetainedTopics)(publishing),
63
+ possible: { owner: publishing, topics: () => (0, publishing_node_1.publishesState)(publishing) ? (0, occupancy_publishing_1.occupancyRetainedTopics)({ ...publishing, publishState: true, discovery: true }) : [] },
64
+ starting: () => (0, occupancy_publishing_1.occupancyStartingOutputs)(publishing, occupied()),
65
+ stopping: () => (0, occupancy_publishing_1.occupancyStoppingOutputs)(publishing),
66
+ output: 1,
67
+ outputs: 2
68
+ });
33
69
  (0, adapter_1.onInput)(node, (msg, send) => {
70
+ if (msg.source === "startup") {
71
+ lifecycle.startup(send, (0, occupancy_publishing_1.occupancyStartingOutputs)(publishing, occupied()));
72
+ return;
73
+ }
34
74
  const input = {
35
75
  source: String(msg.source ?? ""),
36
76
  payload: msg.payload
37
77
  };
38
78
  const result = (0, adapter_1.runReducer)(node, { area: fusionConfig.area, component: "occupancy-fusion" }, (saved) => (0, occupancy_fusion_1.transitionOccupancy)(saved, input, fusionConfig));
39
- for (const output of result.outputs)
40
- send({ ...output, payload: output.payload });
79
+ for (const output of result.outputs) {
80
+ send([{ ...output, payload: output.payload }, null]);
81
+ lifecycle.publish(send, (0, occupancy_publishing_1.occupancyStateOutputs)(publishing, output.payload.occupied));
82
+ }
41
83
  updateStatus(result.status);
42
84
  });
43
85
  }
@@ -0,0 +1,25 @@
1
+ import type { Node, NodeAPI } from "node-red";
2
+ import { type DiscoveryOwner } from "../lib/ha-discovery";
3
+ import { type Send } from "./adapter";
4
+ interface RetainedPublication {
5
+ topic: string;
6
+ payload: unknown;
7
+ retain: boolean;
8
+ }
9
+ export interface PublishingNode {
10
+ retainedTopics(): string[];
11
+ possible?: {
12
+ owner: DiscoveryOwner;
13
+ topics(): string[];
14
+ };
15
+ starting(): RetainedPublication[];
16
+ stopping(): RetainedPublication[];
17
+ output: number;
18
+ outputs: number;
19
+ }
20
+ export interface PublishingLifecycle {
21
+ startup(send: Send, announce?: RetainedPublication[]): void;
22
+ publish(send: Send, publications: RetainedPublication[]): void;
23
+ }
24
+ export declare function publishingLifecycle(RED: NodeAPI, node: Node, publishing: PublishingNode): PublishingLifecycle;
25
+ export {};
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.publishingLifecycle = publishingLifecycle;
4
+ const ha_discovery_1 = require("../lib/ha-discovery");
5
+ const retained_topics_1 = require("../lib/retained-topics");
6
+ const adapter_1 = require("./adapter");
7
+ function publishingLifecycle(RED, node, publishing) {
8
+ const possible = () => publishing.possible ? (0, ha_discovery_1.ownDiscoveryTopics)(publishing.possible.owner, publishing.possible.topics()) : [];
9
+ function publish(send, publications) {
10
+ if (!publications.length)
11
+ return;
12
+ const envelope = Array.from({ length: publishing.outputs }, () => null);
13
+ envelope[publishing.output] = publications.map((entry) => ({ ...entry }));
14
+ send(envelope);
15
+ }
16
+ (0, adapter_1.whenFlowsStarted)(RED, node, () => {
17
+ publish(node.send.bind(node), [
18
+ ...(0, adapter_1.clearStaleRetained)(node, publishing.retainedTopics(), "flows-started", possible()),
19
+ ...publishing.starting()
20
+ ]);
21
+ });
22
+ // Reaches the broker only if the MQTT out node is still running, as in a
23
+ // deploy of modified nodes or flows; the broker's own Close and Will
24
+ // messages cover a full deploy, a restart and a crash (see the README).
25
+ node.on("close", (removed, done) => {
26
+ publish(node.send.bind(node), (0, adapter_1.wasDeleted)(RED, node, removed)
27
+ ? (0, retained_topics_1.retainedClears)((0, adapter_1.allRetainedTopics)(node, [...publishing.retainedTopics(), ...possible()]))
28
+ : publishing.stopping());
29
+ done();
30
+ });
31
+ return {
32
+ startup(send, announce = []) {
33
+ publish(send, [...(0, adapter_1.clearStaleRetained)(node, publishing.retainedTopics(), "startup", possible()), ...announce]);
34
+ },
35
+ publish
36
+ };
37
+ }
38
+ //# sourceMappingURL=publishing-lifecycle.js.map
@@ -7,20 +7,32 @@
7
7
  category: "home automation",
8
8
  color: "#ffd27f",
9
9
  defaults: {
10
- name: {value: ""},
11
- // Required only when the room publishes something named after it.
10
+ // A marker only a node new from the palette has, as core nodes do: import
11
+ // and paste keep the saved name, so onadd leaves a saved node's settings.
12
+ name: {value: "_DEFAULT_"},
13
+ // Required only when the room publishes something named after it, and
14
+ // then with a letter or digit, from which its topics and IDs are made.
12
15
  area: {value: "", validate: function (v) {
13
16
  var publishing = $("#node-input-publishState").length
14
17
  ? $("#node-input-publishState").prop("checked") || $("#node-input-discovery").prop("checked")
15
18
  : on(this.publishState) || on(this.discovery);
16
- return !publishing || Boolean(v && v.trim());
19
+ return !publishing || /[a-z0-9]/i.test(String(v || ""));
17
20
  }},
18
21
  // null means "not set": Node-RED fills it into a saved flow from before
19
22
  // these settings, which keeps publishing. New nodes start with both
20
23
  // off (onadd).
21
24
  publishState: {value: null},
22
25
  discovery: {value: null},
26
+ // How Home Assistant IDs are named. null means "not set": a node saved
27
+ // before this setting, which keeps its area-named entities. New nodes
28
+ // name them after the node (onadd), so renaming the area keeps them.
29
+ idScheme: {value: null},
30
+ // Where wake and bed times come from: null (not set) is this room.
31
+ // The house schedule it follows; none: its own times. Node-RED's
32
+ // editor offers an existing schedule first for a new room.
33
+ houseSchedule: {value: "", type: "home-house-schedule", required: false},
23
34
  displayName: {value: ""},
35
+ discoveryPrefix: {value: "homeassistant", required: true, validate: function (v) { var prefix = String(v || "").trim().replace(/^\/+|\/+$/g, ""); return Boolean(prefix) && !/[+#]/.test(prefix); }},
24
36
  targets: {value: "[]", required: true, validate: function (v) { try { JSON.parse(v); return true; } catch (e) { return false; } }},
25
37
  maxBrightnessPct: {value: 100, required: true, validate: RED.validators.number()},
26
38
  maxColorTempKelvin: {value: 6500, required: true, validate: RED.validators.number()},
@@ -34,6 +46,9 @@
34
46
  bedTime: {value: "23:00", required: true, validate: function (v) { return /^([01]\d|2[0-3]):[0-5]\d$/.test(v); }}
35
47
  },
36
48
  onadd: function () {
49
+ if (this.name !== "_DEFAULT_") return;
50
+ this.name = "";
51
+ this.idScheme = "node";
37
52
  this.publishState = false;
38
53
  this.discovery = false;
39
54
  },
@@ -65,6 +80,7 @@
65
80
  <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>
66
81
  <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>
67
82
  <div class="form-row"><label for="node-input-displayName">Display name</label><input type="text" id="node-input-displayName"></div>
83
+ <div class="form-row"><label for="node-input-discoveryPrefix">Discovery prefix</label><input type="text" id="node-input-discoveryPrefix"></div>
68
84
  <div class="form-row"><label for="node-input-targets">Targets JSON</label><input type="text" id="node-input-targets"></div>
69
85
  <div class="form-row"><label for="node-input-maxBrightnessPct">Daytime brightness %</label><input type="number" id="node-input-maxBrightnessPct"></div>
70
86
  <div class="form-row"><label for="node-input-maxColorTempKelvin">Daytime kelvin</label><input type="number" id="node-input-maxColorTempKelvin"></div>
@@ -73,6 +89,7 @@
73
89
  <div class="form-row"><label for="node-input-initialTransitionSeconds">Initial transition seconds</label><input type="number" id="node-input-initialTransitionSeconds"></div>
74
90
  <div class="form-row"><label for="node-input-refreshTransitionSeconds">Refresh transition seconds</label><input type="number" id="node-input-refreshTransitionSeconds"></div>
75
91
  <div class="form-row"><label for="node-input-refreshSeconds">Refresh interval seconds</label><input type="number" id="node-input-refreshSeconds"></div>
92
+ <div class="form-row"><label for="node-input-houseSchedule">Schedule</label><input type="text" id="node-input-houseSchedule"></div>
76
93
  <div class="form-row"><label for="node-input-wakeTime">Wake time</label><input type="text" id="node-input-wakeTime" placeholder="07:00"></div>
77
94
  <div class="form-row"><label for="node-input-bedTime">Bed time</label><input type="text" id="node-input-bedTime" placeholder="23:00"></div>
78
95
  <div class="form-row"><label for="node-input-initialSleep">Initial sleep mode</label><input type="checkbox" id="node-input-initialSleep" style="display:inline-block; width:auto;"></div>
@@ -81,19 +98,37 @@
81
98
  <script type="text/html" data-help-name="home-room-lighting-controller">
82
99
  <p>Owns room MQTT commands, adaptive colour and brightness, manual overrides, discovery and bounded tracing.</p>
83
100
  <h3>Publishing</h3>
101
+ <p>Wire its commands to MQTT out unchanged. It watches each light's command topic to tell its own commands from
102
+ other people's, so a function that rewrites them on the way makes them look like someone else's. A bare ON or OFF
103
+ that arrives within 5 seconds of one of its own commands with that power, before that command has come back on the
104
+ topic, is taken as that command (traced as <code>mutated-own-command</code>), and it restyles a light after someone else's bare ON at most once every 5
105
+ seconds, so it never answers itself in a loop.</p>
84
106
  <p>The room always commands its lights on each target's own command topic. Two settings add what it publishes
85
- for other systems to see; both are named after the <b>Area</b>, which is needed only when one of them is on.</p>
107
+ for other systems to see; both are named after the <b>Area</b>, which is needed only when one of them is on. Topics
108
+ and IDs use it in lowercase, with spaces and other characters (anything but letters, digits, <code>_</code> and
109
+ <code>-</code>) as <code>_</code>: <i>Living Room</i> publishes under <code>home/living_room/…</code>.</p>
86
110
  <dl class="message-properties">
87
111
  <dt>Publish state over MQTT</dt><dd>each light's control state on
88
112
  <code>home/&lt;area&gt;/lighting/&lt;target&gt;/control</code> and the circadian state on
89
- <code>home/&lt;area&gt;/circadian/state</code>.</dd>
113
+ <code>home/&lt;area&gt;/circadian/state</code>. It also publishes a retained <code>online</code> on
114
+ <code>home/&lt;area&gt;/lighting/availability</code> once the flows start and <code>offline</code> as it stops, which the
115
+ circadian switch in Home Assistant follows. These get through only when this node is
116
+ deployed on its own, so set the MQTT broker node's Birth, Close and Will messages to the same (see the README).</dd>
90
117
  <dt>Home Assistant discovery</dt><dd>a circadian switch in Home Assistant, which reads that state and sends
91
118
  ON/OFF to <code>home/&lt;area&gt;/circadian/set</code>. To act on it, wire an MQTT in node subscribed to that
92
119
  topic into this node. It needs <b>Publish state</b>. Switching it off
93
120
  removes the switch from Home Assistant at the next start.</dd>
121
+ <dt>Discovery prefix</dt><dd>the topic prefix Home Assistant listens on for discovery: <code>homeassistant</code>
122
+ unless you changed it in Home Assistant's MQTT settings.</dd>
94
123
  </dl>
95
124
  <p>New nodes start with both off. A node saved before these settings existed keeps publishing both.</p>
96
125
  <h3>Circadian schedule</h3>
126
+ <p><b>Schedule</b>: a house schedule the room follows, shared with other rooms. Following it, the room takes its
127
+ wake and bed times from it whenever they change, has no wake and bed time entities of its own in Home Assistant
128
+ (they are cleared), and ignores its own wake-time and bed-time set topics. With none, the room keeps its own
129
+ <b>Wake time</b> and <b>Bed time</b>. If the schedule is missing when the room starts, as when it was deleted
130
+ from the flows file, the room keeps its own times and its status says <i>schedule missing: own times</i>; deleting
131
+ a schedule in the editor sets its rooms to none.</p>
97
132
  <p>Circadian levels follow <b>Wake time</b> and <b>Bed time</b> (24-hour <code>HH:MM</code>, default 07:00 and
98
133
  23:00): they rise to the daytime level over the hour after waking, stay at the daytime brightness and colour
99
134
  through the day, wind down to the sleep settings over the three hours before bed, and stay at the sleep
@@ -115,12 +150,26 @@
115
150
  seen at the light), or a release. Any other intent, including motion ON or OFF and owner-less values, leaves it
116
151
  alone and is traced as <code>ignored held-by</code>; other targets in the same intent are still handled.
117
152
  Switching a light off by its owner or a person returns it to in-control.</p>
153
+ <p>The control states are kept in the node's context. When the node starts with nothing saved (its first deploy,
154
+ or a redeploy or restart without a context store, see the README), it can't know which lights were held or set
155
+ by a person, so it leaves every light as it is: no periodic refresh restyles a light until an intent or release
156
+ names it, someone switches it on with a bare ON on its command topic, or circadian lighting is switched on. Sleep
157
+ and schedule changes restyle only lights it has been told about since. A light that is on at that start and that
158
+ nothing names can keep its start-time style all day; to bring it back into step, switch circadian lighting on
159
+ (for example with the Home Assistant circadian switch), or let motion or a wall switch name it.</p>
118
160
  <h3>Inputs</h3>
119
161
  <dl class="message-properties">
120
162
  <dt>topic = home/&lt;area&gt;/circadian/set</dt>
121
163
  <dd>payload <code>ON</code> or <code>OFF</code> (any case) switches circadian lighting on or off, as
122
164
  <code>lighting.kind = "circadian"</code> does. Read only while <b>Publish state</b> is on and the area is set.
123
165
  Retained messages and other payloads are ignored.</dd>
166
+ <dt>topic = home/&lt;area&gt;/sleep/set</dt>
167
+ <dd><code>ON</code> or <code>OFF</code> switches sleep, as <code>lighting.kind = "sleep"</code> does, under the
168
+ same conditions.</dd>
169
+ <dt>topic = home/&lt;area&gt;/wake-time/set or bed-time/set</dt>
170
+ <dd>a time, <code>HH:MM</code> or <code>HH:MM:SS</code>, changes that time, as
171
+ <code>lighting = {kind: "schedule", wakeTime, bedTime}</code> does. A time set this way lasts until the node is
172
+ deployed with a different <b>Wake time</b> or <b>Bed time</b>, which then applies.</dd>
124
173
  <dt>lighting.owner <span class="property-type">string</span></dt>
125
174
  <dd>on an <code>intent</code> that switches targets on, marks them overridden by this owner. With
126
175
  <code>preserveManual: true</code>, manual lights are skipped.</dd>