@avantics/node-red-home-automation 5.0.0 → 6.1.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/README.md +413 -77
  2. package/contract-examples/room-lighting.mjs +39 -27
  3. package/dist/lib/activity-lighting-controller.d.ts +7 -2
  4. package/dist/lib/activity-lighting-controller.js +75 -16
  5. package/dist/lib/avr-controller.d.ts +2 -2
  6. package/dist/lib/avr-controller.js +21 -26
  7. package/dist/lib/circadian-policy.d.ts +47 -0
  8. package/dist/lib/circadian-policy.js +111 -0
  9. package/dist/lib/circadian-publishing.d.ts +33 -0
  10. package/dist/lib/circadian-publishing.js +154 -0
  11. package/dist/lib/circadian-schedule.d.ts +4 -0
  12. package/dist/lib/circadian-schedule.js +12 -0
  13. package/dist/lib/command-echo.d.ts +8 -1
  14. package/dist/lib/command-echo.js +37 -6
  15. package/dist/lib/cover-controller.js +4 -0
  16. package/dist/lib/denon-protocol.js +3 -2
  17. package/dist/lib/enable-input.d.ts +3 -0
  18. package/dist/lib/enable-input.js +18 -0
  19. package/dist/lib/house-schedule.js +2 -2
  20. package/dist/lib/light-contract.d.ts +53 -0
  21. package/dist/lib/light-contract.js +11 -0
  22. package/dist/lib/motion-policy.d.ts +0 -1
  23. package/dist/lib/motion-policy.js +9 -8
  24. package/dist/lib/motion-publishing.js +2 -1
  25. package/dist/lib/mqtt-message.d.ts +6 -0
  26. package/dist/lib/mqtt-message.js +21 -0
  27. package/dist/lib/room-controller.d.ts +41 -27
  28. package/dist/lib/room-controller.js +422 -213
  29. package/dist/lib/room-holds.d.ts +34 -0
  30. package/dist/lib/room-holds.js +66 -0
  31. package/dist/lib/room-lighting-discovery.d.ts +0 -14
  32. package/dist/lib/room-lighting-discovery.js +19 -125
  33. package/dist/lib/room-publishing.d.ts +7 -12
  34. package/dist/lib/room-publishing.js +65 -125
  35. package/dist/lib/shelly-cover.d.ts +2 -4
  36. package/dist/lib/shelly-cover.js +30 -70
  37. package/dist/lib/shelly-gen2.d.ts +25 -0
  38. package/dist/lib/shelly-gen2.js +58 -0
  39. package/dist/lib/shelly-switch.d.ts +95 -0
  40. package/dist/lib/shelly-switch.js +237 -0
  41. package/dist/lib/theatre-state-contract.js +12 -4
  42. package/dist/lib/z2m-cover.d.ts +49 -0
  43. package/dist/lib/z2m-cover.js +120 -0
  44. package/dist/lib/z2m-light-adapter.d.ts +62 -0
  45. package/dist/lib/z2m-light-adapter.js +195 -0
  46. package/dist/lib/z2m-light.d.ts +2 -2
  47. package/dist/lib/z2m-light.js +11 -14
  48. package/dist/nodes/activity-lighting-controller.html +15 -6
  49. package/dist/nodes/activity-lighting-controller.js +21 -0
  50. package/dist/nodes/adapter.d.ts +5 -1
  51. package/dist/nodes/adapter.js +35 -2
  52. package/dist/nodes/avr-controller.html +20 -18
  53. package/dist/nodes/avr-controller.js +5 -5
  54. package/dist/nodes/circadian-policy.d.ts +3 -0
  55. package/dist/nodes/circadian-policy.html +145 -0
  56. package/dist/nodes/circadian-policy.js +162 -0
  57. package/dist/nodes/cover-controller.html +9 -3
  58. package/dist/nodes/cover-controller.js +1 -1
  59. package/dist/nodes/display-mode-controller.html +14 -17
  60. package/dist/nodes/display-mode-controller.js +4 -1
  61. package/dist/nodes/house-schedule-mqtt.html +5 -0
  62. package/dist/nodes/house-schedule-mqtt.js +1 -1
  63. package/dist/nodes/house-schedule.html +3 -3
  64. package/dist/nodes/motion-policy.html +10 -2
  65. package/dist/nodes/motion-policy.js +4 -1
  66. package/dist/nodes/occupancy-fusion.html +5 -0
  67. package/dist/nodes/occupancy-fusion.js +1 -1
  68. package/dist/nodes/refused-enable.d.ts +7 -0
  69. package/dist/nodes/refused-enable.js +24 -0
  70. package/dist/nodes/room-controller.html +232 -85
  71. package/dist/nodes/room-controller.js +71 -97
  72. package/dist/nodes/shelly-switch.d.ts +3 -0
  73. package/dist/nodes/shelly-switch.html +87 -0
  74. package/dist/nodes/shelly-switch.js +48 -0
  75. package/dist/nodes/theatre-state-contract.html +9 -3
  76. package/dist/nodes/theatre-state-contract.js +1 -1
  77. package/dist/nodes/z2m-cover.d.ts +3 -0
  78. package/dist/nodes/z2m-cover.html +68 -0
  79. package/dist/nodes/z2m-cover.js +41 -0
  80. package/dist/nodes/z2m-light.d.ts +3 -0
  81. package/dist/nodes/z2m-light.html +76 -0
  82. package/dist/nodes/z2m-light.js +51 -0
  83. package/examples/Cover control.json +28 -71
  84. package/examples/Home theatre activity.json +53 -19
  85. package/examples/House schedule.json +106 -24
  86. package/examples/Room lighting.json +57 -25
  87. package/examples/Shelly cover.json +5 -16
  88. package/examples/Shelly switch.json +197 -0
  89. package/package.json +12 -4
  90. package/dist/lib/room-schedule.d.ts +0 -8
  91. package/dist/lib/room-schedule.js +0 -41
@@ -2,10 +2,9 @@
2
2
  (function () {
3
3
  function on(value) { return value === true || value === "true"; }
4
4
  function given(value) { return value !== undefined && value !== null && String(value).trim() !== ""; }
5
- // A dB bound, or a bound saved before 5.0 in its place.
6
- function volumeBound(value, legacy) {
7
- return given(value) ? Number.isFinite(Number(value)) : given(legacy) && Number.isFinite(Number(legacy));
8
- }
5
+ // A dB bound. A node saved before 5.0 has none (its 0-98 bounds were
6
+ // removed in 6.0), so it shows as invalid until they are set.
7
+ function volumeBound(value) { return given(value) && Number.isFinite(Number(value)); }
9
8
  RED.nodes.registerType("home-avr-controller", {
10
9
  category: "home automation",
11
10
  color: "#c7d9f1",
@@ -22,15 +21,16 @@
22
21
  sources: {value: "{\"shield\":\"SAT/CBL\",\"playstation\":\"GAME\"}", required: true, validate: function (v) { try { JSON.parse(v); return true; } catch (e) { return false; } }},
23
22
  retryMs: {value: 2000, required: true, validate: RED.validators.number()},
24
23
  maxAttempts: {value: 3, required: true, validate: RED.validators.number()},
25
- // In dB (the receiver's 80 is 0 dB). A flow saved before 5.0 has
26
- // minVolume and maxVolume in the receiver's own 0-98 instead, which
27
- // still work: the node isn't shown invalid for them, and the dialog
28
- // fills these from them.
29
- minVolumeDb: {value: -80, validate: function (v) { return volumeBound(v, this.minVolume); }},
30
- maxVolumeDb: {value: -20, validate: function (v) { return volumeBound(v, this.maxVolume); }},
31
- // The bounds before 5.0, with no form rows. Listed so the editor keeps
32
- // them: it holds only the properties named here. Cleared once the
33
- // dialog saves the dB ones.
24
+ // In dB (the receiver's 80 is 0 dB).
25
+ minVolumeDb: {value: -80, validate: volumeBound},
26
+ maxVolumeDb: {value: -20, validate: volumeBound},
27
+ // The 0-98 bounds of a node saved before 5.0, removed in 6.0 but kept
28
+ // here, with no form row, so the editor carries them: the editor keeps
29
+ // only the properties named here, and without them a deploy from the
30
+ // editor would leave the node with no bounds at all, which the runtime
31
+ // would give the default bounds, possibly louder than the old cap.
32
+ // With them, the node stays an invalid configuration until its dB
33
+ // bounds are set.
34
34
  minVolume: {value: ""},
35
35
  maxVolume: {value: ""},
36
36
  // The receiver published nothing before these settings, so both start off.
@@ -47,10 +47,6 @@
47
47
  this.idScheme = "node";
48
48
  },
49
49
  oneditprepare: function () {
50
- // A flow saved before 5.0: show its volume bounds in dB, so saving
51
- // the dialog keeps them.
52
- if (!given(this.minVolumeDb) && given(this.minVolume)) $("#node-input-minVolumeDb").val(Number(this.minVolume) - 80);
53
- if (!given(this.maxVolumeDb) && given(this.maxVolume)) $("#node-input-maxVolumeDb").val(Number(this.maxVolume) - 80);
54
50
  $("#node-input-publishState").prop("checked", on(this.publishState));
55
51
  $("#node-input-discovery").prop("checked", on(this.discovery));
56
52
  // Discovery describes the published state, so it needs it.
@@ -63,6 +59,7 @@
63
59
  $("#node-input-publishState, #node-input-discovery").on("change", follow);
64
60
  follow();
65
61
  },
62
+ // Once the dB bounds are set, the removed 0-98 ones go.
66
63
  oneditsave: function () {
67
64
  if (given($("#node-input-minVolumeDb").val())) this.minVolume = "";
68
65
  if (given($("#node-input-maxVolumeDb").val())) this.maxVolume = "";
@@ -93,7 +90,7 @@
93
90
  <p>Owns explicit power, main-zone, source and bounded volume commands for a Denon AVR.</p>
94
91
  <p>Send protocol observations as <code>msg.avr = {kind: "observed", line: msg.payload}</code>.</p>
95
92
  <p>Send activity requests as <code>msg.avr = {kind: "intent", requestId, generation, power: "on", source: "shield"}</code>. Omit <code>source</code> to preserve the current input, or use <code>power: "standby"</code> to stop the AVR.</p>
96
- <p>Send volume requests as <code>msg.avr = {kind: "volume", requestId, mode: "absolute", valueDb: -50}</code> or use <code>mode: "relative"</code> with a positive or negative change. Volume is in dB, as are the bounds, results and published state; the receiver's 80 is 0 dB. A <code>value</code> in the receiver's own 0-98 still works during 5.x, with a warning in the log. The controller applies configured bounds, accumulates rapid relative requests, confirms fresh <code>MV</code> evidence and releases ownership so external adjustments are preserved.</p>
93
+ <p>Send volume requests as <code>msg.avr = {kind: "volume", requestId, mode: "absolute", valueDb: -50}</code> or use <code>mode: "relative"</code> with a positive or negative change. Volume is in dB, as are the bounds, results and published state; the receiver's 80 is 0 dB. A <code>value</code> in the receiver's own 0-98 was removed in 6.0: such a request is answered <code>volume_rejected</code> with reason <code>removed-volume-value</code>, with a warning in the log. A node saved before 5.0 with only its 0-98 bounds is an invalid configuration until <b>Minimum volume (dB)</b> and <b>Maximum volume (dB)</b> are set. The controller applies configured bounds, accumulates rapid relative requests, confirms fresh <code>MV</code> evidence and releases ownership so external adjustments are preserved.</p>
97
94
  <p>Send <code>kind: "startup"</code> after deployment to discard stale observations and query the receiver; a volume request still pending then is reported as failed with reason <code>restart</code>. Send <code>kind: "refresh"</code> to re-read the receiver without disturbing work in flight. Output 1 connects to a TCP output node; output 2 emits state plus ready, failed, rejected, superseded and volume results.</p>
98
95
  <p>While it has a request, output 2 also carries <code>msg.theatre = {kind: "target_state", target: "avr", requestId, generation, status, observedAt, details}</code>, so it can be wired straight back to a theatre activity controller. A ready state is stamped with the oldest receiver reading the request relies on, and a <code>refresh</code> is answered once the receiver reports again.</p>
99
96
  <h3>Volume results</h3>
@@ -120,4 +117,9 @@
120
117
  </dl>
121
118
  <p>These go on the third output; wire it to an MQTT out node. The node announces itself once the flows have
122
119
  started, and again at <code>kind: "startup"</code>.</p>
120
+ <h3>When MQTT out connects</h3>
121
+ <p>Wire a <b>Status node</b> whose scope is your MQTT out node into this node. Each time MQTT out shows
122
+ "connected", this node takes it as its startup message (<code>msg.avr = {kind: "startup"}</code>) and announces itself again:
123
+ Node-RED's MQTT broker node drops what it is given before it connects, so what the node sends as the flows
124
+ start is lost on a first start, a full deploy or a restart. Any other status message is ignored.</p>
123
125
  </script>
@@ -87,7 +87,7 @@ const registerAvrController = (RED) => {
87
87
  output: 2,
88
88
  outputs: 3
89
89
  });
90
- let warnedLegacyVolume = false;
90
+ let warnedRemovedVolume = false;
91
91
  function process(msg, send) {
92
92
  const before = observed();
93
93
  const result = (0, adapter_1.runReducer)(node, { area: controllerConfig.area, component: "avr-controller" }, (saved, now) => (0, avr_controller_1.transitionAvr)(saved, msg, controllerConfig, now));
@@ -97,9 +97,9 @@ const registerAvrController = (RED) => {
97
97
  }));
98
98
  // Once per deploy, so a flow still sending the receiver's own units is
99
99
  // told without filling the log.
100
- if (!warnedLegacyVolume && result.traces.some(({ reason }) => reason === avr_controller_1.LEGACY_VOLUME_UNITS)) {
101
- warnedLegacyVolume = true;
102
- node.warn("volume requests with value in the receiver's own 0-98 are deprecated and will stop working in 6.0: " +
100
+ if (!warnedRemovedVolume && result.events.some(({ avr }) => avr.kind === "volume_rejected" && avr.reason === avr_controller_1.REMOVED_VOLUME_VALUE)) {
101
+ warnedRemovedVolume = true;
102
+ node.warn("volume requests with value in the receiver's own 0-98 were removed in 6.0 and are rejected: " +
103
103
  "send valueDb (dB, where the receiver's 80 is 0 dB) instead");
104
104
  }
105
105
  const outputs = [...result.events, ...result.targetStates];
@@ -158,7 +158,7 @@ const registerAvrController = (RED) => {
158
158
  avr: (0, adapter_1.objectField)(msg, "avr"),
159
159
  payload: msg.payload
160
160
  }, send);
161
- });
161
+ }, { message: { avr: { kind: "startup" } }, trace: { area: controllerConfig.area, component: "avr-controller" } });
162
162
  node.on("close", (_removed, done) => {
163
163
  clearTimeout(retryTimer);
164
164
  clearTimeout(volumeSetTimer);
@@ -0,0 +1,3 @@
1
+ import type { NodeInitializer } from "node-red";
2
+ declare const registerCircadianPolicy: NodeInitializer;
3
+ export = registerCircadianPolicy;
@@ -0,0 +1,145 @@
1
+ <script type="text/javascript">
2
+ (function () {
3
+ function clockTime(v) { return /^([01]\d|2[0-3]):[0-5]\d$/.test(String(v || "")); }
4
+ RED.nodes.registerType("home-circadian-lighting-policy", {
5
+ category: "home automation",
6
+ color: "#ffd27f",
7
+ defaults: {
8
+ name: {value: ""},
9
+ // Required only when the node publishes, and then with a letter or
10
+ // digit, from which its topics are made.
11
+ area: {value: "", validate: function (v) {
12
+ var publishing = $("#node-input-publishState").length
13
+ ? $("#node-input-publishState").prop("checked") || $("#node-input-discovery").prop("checked")
14
+ : this.publishState === true || this.discovery === true;
15
+ return !publishing || /[a-z0-9]/i.test(String(v || ""));
16
+ }},
17
+ // The house schedule it follows; none: its own wake and bed times.
18
+ houseSchedule: {value: "", type: "home-house-schedule", required: false},
19
+ wakeTime: {value: "07:00", validate: function (v) { return Boolean(this.houseSchedule) || clockTime(v); }},
20
+ bedTime: {value: "23:00", validate: function (v) { return Boolean(this.houseSchedule) || clockTime(v); }},
21
+ maxBrightnessPct: {value: 100, required: true, validate: RED.validators.number()},
22
+ maxColorTempKelvin: {value: 6500, required: true, validate: RED.validators.number()},
23
+ sleepBrightnessPct: {value: 1, required: true, validate: RED.validators.number()},
24
+ sleepColorTempKelvin: {value: 2200, required: true, validate: RED.validators.number()},
25
+ refreshTransitionSeconds: {value: 60, required: true, validate: RED.validators.number()},
26
+ refreshSeconds: {value: 60, required: true, validate: function (v) { return RED.validators.number()(v) && Number(v) >= 10; }},
27
+ initialSleep: {value: false},
28
+ publishState: {value: false},
29
+ discovery: {value: false},
30
+ discoveryPrefix: {value: "homeassistant", required: true, validate: function (v) { var prefix = String(v || "").trim().replace(/^\/+|\/+$/g, ""); return Boolean(prefix) && !/[+#]/.test(prefix); }},
31
+ // A new node, so its Home Assistant entities are named after it.
32
+ idScheme: {value: "node"}
33
+ },
34
+ oneditprepare: function () {
35
+ // Discovery describes the published state, so it needs it.
36
+ function follow() {
37
+ var publishing = $("#node-input-publishState").prop("checked");
38
+ if (!publishing) $("#node-input-discovery").prop("checked", false);
39
+ $("#node-input-discovery").prop("disabled", !publishing);
40
+ $("#node-input-area").trigger("change");
41
+ }
42
+ $("#node-input-publishState, #node-input-discovery").on("change", follow);
43
+ follow();
44
+ // Following a house, the times are the house's.
45
+ function times() {
46
+ var own = !$("#node-input-houseSchedule").val() || $("#node-input-houseSchedule").val() === "_ADD_";
47
+ $(".circadian-own-times").toggle(own);
48
+ }
49
+ $("#node-input-houseSchedule").on("change", times);
50
+ times();
51
+ },
52
+ inputs: 1,
53
+ outputs: 2,
54
+ outputLabels: ["style, to room lighting controllers", "publications (MQTT out)"],
55
+ icon: "font-awesome/fa-sun-o",
56
+ label: function () { return this.name || (this.area ? this.area + " " : "") + "circadian lighting"; }
57
+ });
58
+ })();
59
+ </script>
60
+
61
+ <script type="text/html" data-template-name="home-circadian-lighting-policy">
62
+ <div class="form-row"><label for="node-input-name">Name</label><input type="text" id="node-input-name"></div>
63
+ <div class="form-row"><label for="node-input-houseSchedule">Schedule</label><input type="text" id="node-input-houseSchedule"></div>
64
+ <div class="form-row circadian-own-times"><label for="node-input-wakeTime">Wake time</label><input type="text" id="node-input-wakeTime" placeholder="07:00"></div>
65
+ <div class="form-row circadian-own-times"><label for="node-input-bedTime">Bed time</label><input type="text" id="node-input-bedTime" placeholder="23:00"></div>
66
+ <div class="form-row"><label for="node-input-maxBrightnessPct">Daytime brightness %</label><input type="number" id="node-input-maxBrightnessPct"></div>
67
+ <div class="form-row"><label for="node-input-maxColorTempKelvin">Daytime kelvin</label><input type="number" id="node-input-maxColorTempKelvin"></div>
68
+ <div class="form-row"><label for="node-input-sleepBrightnessPct">Sleep brightness %</label><input type="number" id="node-input-sleepBrightnessPct"></div>
69
+ <div class="form-row"><label for="node-input-sleepColorTempKelvin">Sleep kelvin</label><input type="number" id="node-input-sleepColorTempKelvin"></div>
70
+ <div class="form-row"><label for="node-input-refreshTransitionSeconds">Restyle transition (s)</label><input type="number" id="node-input-refreshTransitionSeconds"></div>
71
+ <div class="form-row"><label for="node-input-refreshSeconds">Restyle interval (s)</label><input type="number" min="10" id="node-input-refreshSeconds"></div>
72
+ <div class="form-row"><label for="node-input-initialSleep">Start in sleep mode</label><input type="checkbox" id="node-input-initialSleep" style="display:inline-block; width:auto;"></div>
73
+ <div class="form-row"><label for="node-input-area">Area</label><input type="text" id="node-input-area" placeholder="needed when publishing"></div>
74
+ <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>
75
+ <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>
76
+ <div class="form-row"><label for="node-input-discoveryPrefix">Discovery prefix</label><input type="text" id="node-input-discoveryPrefix"></div>
77
+ </script>
78
+
79
+ <script type="text/html" data-help-name="home-circadian-lighting-policy">
80
+ <p>Works out how lights should look over the day and sends it to room lighting controllers as a <i>style</i>: a
81
+ brightness and colour temperature for lights that nobody has set otherwise. Wire its first output to one or more
82
+ room lighting controllers. A room without it styles nothing: its lights just go on and off.</p>
83
+ <p>The lights rise to the daytime level over the hour after waking, stay there through the day, and wind down to
84
+ the sleep levels over the three hours before bed. Sleep mode holds the sleep levels whatever the time. Times are
85
+ the Node-RED host's local time.</p>
86
+ <h3>What a room does with a style</h3>
87
+ <p>A style is not a request to change a light: it never switches one on or off, and never reaches a light that an
88
+ owner (such as an activity) holds or a person set. A room uses it when a light it controls comes on with no values
89
+ given (motion, a wall-switch press), on each new style for lights that are on, after someone else's bare ON, and
90
+ when an owner releases a light. Switching circadian lighting on is the one exception: it takes back the lights a
91
+ person set, even when it is already on, so a flow can send it to bring lights back into step.</p>
92
+ <p>It sends a style once the flows start, every <b>Restyle interval</b>, at its startup message, and at once when
93
+ circadian lighting, sleep or the times change. Each style lasts three intervals: if the policy stops (deleted,
94
+ disabled, or its flow stopped), rooms stop styling soon after. Deleted with a modified deploy, it tells rooms at
95
+ once. While circadian lighting is off it sends a style with no values, and rooms style nothing.</p>
96
+ <h3>Inputs</h3>
97
+ <dl class="message-properties">
98
+ <dt>lighting.kind = "circadian" or "sleep"</dt>
99
+ <dd><code>lighting.enabled</code> <code>true</code> or <code>false</code> switches circadian lighting or sleep. Any
100
+ other value, such as <code>"on"</code>, is refused: nothing changes, and the node logs a warning once per deploy
101
+ and shows it on its status.</dd>
102
+ <dt>lighting.kind = "schedule"</dt>
103
+ <dd><code>lighting.wakeTime</code> and <code>lighting.bedTime</code>, <code>"HH:MM"</code>, either alone. A time set
104
+ this way lasts until the node is deployed with a different setting for it.</dd>
105
+ <dt>lighting.kind = "startup"</dt>
106
+ <dd>Sends the style, and with publishing on, its availability, state and discovery. Send it a few seconds after
107
+ the flows start, as the examples do.</dd>
108
+ <dt>topic = home/&lt;area&gt;/circadian/set, home/&lt;area&gt;/sleep/set</dt>
109
+ <dd><code>ON</code> or <code>OFF</code> from Home Assistant, as <code>lighting.kind = "circadian"</code> or
110
+ <code>"sleep"</code> does. Read only while <b>Publish state</b> is on. Retained messages and other payloads are
111
+ ignored.</dd>
112
+ <dt>topic = home/&lt;area&gt;/wake-time/set or bed-time/set</dt>
113
+ <dd>A time from Home Assistant, with its own times only.</dd>
114
+ </dl>
115
+ <h3>Outputs</h3>
116
+ <ol class="node-ports">
117
+ <li>Style, to room lighting controllers: <code>msg.lighting = {kind: "style", source: "circadian", values:
118
+ {brightnessPct, colorTempKelvin} | null, transitionSeconds, until, takeBack?}</code>.</li>
119
+ <li>Publications, to an MQTT out node with no topic set: availability on
120
+ <code>home/&lt;area&gt;/circadian/availability</code>, the circadian and sleep state on
121
+ <code>home/&lt;area&gt;/circadian/state</code> and <code>home/&lt;area&gt;/sleep/state</code>, its own wake and
122
+ bed times on <code>home/&lt;area&gt;/wake-time/state</code> and <code>…/bed-time/state</code>, and discovery.</li>
123
+ </ol>
124
+ <h3>Settings</h3>
125
+ <dl class="message-properties">
126
+ <dt>Schedule</dt><dd>A house schedule, so several rooms share one wake and bed time; none uses its own
127
+ <b>Wake time</b> and <b>Bed time</b>. Following a house, it has no time entities of its own: the house schedule
128
+ MQTT node offers them.</dd>
129
+ <dt>Daytime and sleep brightness and kelvin</dt><dd>The levels at full day and in sleep.</dd>
130
+ <dt>Restyle transition</dt><dd>How long a room takes to move a light that is on to a new style. A light turning
131
+ on uses the room's own transition.</dd>
132
+ <dt>Restyle interval</dt><dd>How often it sends its style, at least 10 seconds.</dd>
133
+ <dt>Publish state over MQTT, Home Assistant discovery</dt><dd>Off unless switched on. With both on, Home Assistant
134
+ shows Circadian lighting and Sleep switches and, with its own times, Wake time and Bed time, named after this
135
+ node. The topics keep the names the room controller used before 6.0, so a flow already writing
136
+ <code>home/&lt;area&gt;/circadian/set</code> keeps working when this node has that area. Before announcing, it
137
+ clears the circadian entities older room controllers in its area published, migrated or not, in the same batch,
138
+ so Home Assistant can give the new ones the same entity IDs.</dd>
139
+ </dl>
140
+ <h3>When MQTT out connects</h3>
141
+ <p>Wire a <b>Status node</b> whose scope is your MQTT out node into this node. Each time MQTT out shows
142
+ "connected", this node takes it as its startup message (<code>msg.lighting = {kind: "startup"}</code>) and announces itself again:
143
+ Node-RED's MQTT broker node drops what it is given before it connects, so what the node sends as the flows
144
+ start is lost on a first start, a full deploy or a restart. Any other status message is ignored.</p>
145
+ </script>
@@ -0,0 +1,162 @@
1
+ "use strict";
2
+ const circadian_1 = require("../lib/circadian");
3
+ const circadian_policy_1 = require("../lib/circadian-policy");
4
+ const circadian_publishing_1 = require("../lib/circadian-publishing");
5
+ const circadian_schedule_1 = require("../lib/circadian-schedule");
6
+ const ha_discovery_1 = require("../lib/ha-discovery");
7
+ const publishing_node_1 = require("../lib/publishing-node");
8
+ const settings_1 = require("../lib/settings");
9
+ const topic_names_1 = require("../lib/topic-names");
10
+ const adapter_1 = require("./adapter");
11
+ const publishing_lifecycle_1 = require("./publishing-lifecycle");
12
+ const refused_enable_1 = require("./refused-enable");
13
+ const shared_schedule_1 = require("./shared-schedule");
14
+ const registerCircadianPolicy = (RED) => {
15
+ function HomeCircadianLightingPolicy(config) {
16
+ RED.nodes.createNode(this, config);
17
+ const node = this;
18
+ const context = node.context();
19
+ let tickTimer;
20
+ // A schedule that is set but not running, as when it was deleted: the
21
+ // policy keeps to its own times and says so.
22
+ const { schedule: house, missing: houseMissing } = (0, shared_schedule_1.referencedSchedule)(RED, config.houseSchedule);
23
+ let policyConfig;
24
+ let publishing;
25
+ try {
26
+ const area = (0, adapter_1.optionalArea)(config);
27
+ const levels = {
28
+ maxBrightnessPct: (0, settings_1.numberSetting)(config.maxBrightnessPct, "daytime brightness", 100),
29
+ maxColorTempKelvin: (0, settings_1.numberSetting)(config.maxColorTempKelvin, "daytime colour temperature", 6500),
30
+ sleepBrightnessPct: (0, settings_1.numberSetting)(config.sleepBrightnessPct, "sleep brightness", 1),
31
+ sleepColorTempKelvin: (0, settings_1.numberSetting)(config.sleepColorTempKelvin, "sleep colour temperature", 2200)
32
+ };
33
+ const schedule = house
34
+ ? house.schedule()
35
+ : (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"));
36
+ policyConfig = {
37
+ lighting: (0, circadian_1.scheduledLighting)(schedule, levels),
38
+ schedule,
39
+ timesFrom: house ? "house" : "own",
40
+ initialSleep: (0, settings_1.booleanSetting)(config.initialSleep, "initial sleep", false),
41
+ transitionSeconds: (0, settings_1.numberSetting)(config.refreshTransitionSeconds, "restyle transition", 60),
42
+ intervalSeconds: Math.max(10, (0, settings_1.numberSetting)(config.refreshSeconds, "restyle interval", 60))
43
+ };
44
+ // Off unless switched on; a new node, so its entities are named after
45
+ // it.
46
+ const publishState = (0, settings_1.optionalBooleanSetting)(config.publishState, "publish state") ?? false;
47
+ const discovery = (0, settings_1.optionalBooleanSetting)(config.discovery, "Home Assistant discovery") ?? false;
48
+ if (publishState || discovery)
49
+ (0, topic_names_1.publishingArea)(area, true);
50
+ publishing = {
51
+ area,
52
+ publishState,
53
+ discovery,
54
+ discoveryPrefix: (0, ha_discovery_1.discoveryPrefixSetting)(config.discoveryPrefix),
55
+ nodeId: node.id,
56
+ // Always named after the node, so its entries never share a topic
57
+ // with the area-named ones older rooms published, which it clears.
58
+ idScheme: "node",
59
+ timesFrom: policyConfig.timesFrom
60
+ };
61
+ }
62
+ catch (error) {
63
+ (0, adapter_1.invalidConfig)(node, error);
64
+ return;
65
+ }
66
+ function savedState() {
67
+ return (0, circadian_policy_1.createCircadianState)(policyConfig, context.get("state"));
68
+ }
69
+ // The room controllers in the flows with this area, migrated or not:
70
+ // their old circadian entries named after them are cleared with this
71
+ // node's own (see circadianStartingOutputs).
72
+ function roomsInArea() {
73
+ const ids = [];
74
+ RED.nodes.eachNode((entry) => {
75
+ const room = entry;
76
+ if (room.type === "home-room-lighting-controller" && typeof room.area === "string" && publishing.area !== "" &&
77
+ (0, topic_names_1.areaId)(room.area) === (0, topic_names_1.areaId)(publishing.area))
78
+ ids.push(room.id);
79
+ });
80
+ return ids;
81
+ }
82
+ // Styles go to rooms on the first output; MQTT publications on the
83
+ // second.
84
+ const lifecycle = (0, publishing_lifecycle_1.publishingLifecycle)(RED, node, {
85
+ retainedTopics: () => (0, circadian_publishing_1.circadianRetainedTopics)(publishing),
86
+ possible: { owner: publishing, topics: () => (0, publishing_node_1.publishesState)(publishing) ? (0, circadian_publishing_1.circadianRetainedTopics)({ ...publishing, publishState: true, discovery: true }) : [] },
87
+ starting: () => (0, circadian_publishing_1.circadianStartingOutputs)(publishing, savedState(), roomsInArea()),
88
+ stopping: () => (0, circadian_publishing_1.circadianStoppingOutputs)(publishing),
89
+ output: 1,
90
+ outputs: 2
91
+ });
92
+ function updateStatus(status) {
93
+ const values = status.values;
94
+ node.status(values
95
+ ? { fill: "green", shape: "dot", text: (status.sleeping ? "sleep" : "day") + " · " +
96
+ Math.round(values.brightnessPct) + "% " + Math.round(values.colorTempKelvin) + "K" +
97
+ (houseMissing ? " · schedule missing: own times" : "") }
98
+ : { fill: "grey", shape: "dot", text: "off" + (houseMissing ? " · schedule missing: own times" : "") });
99
+ }
100
+ const showRefused = (0, refused_enable_1.refusedEnables)(node, (source) => `${source}'s enabled`);
101
+ function process(event, send, inboundTraces = [], fromSetTopic = false) {
102
+ const result = (0, adapter_1.runReducer)(node, { area: publishing.area, component: "circadian-policy" }, (saved, now) => {
103
+ const decided = (0, circadian_policy_1.transitionCircadian)(saved, event, policyConfig, now);
104
+ return { ...decided, traces: [...inboundTraces, ...decided.traces] };
105
+ });
106
+ if (result.style)
107
+ send([{ payload: undefined, lighting: result.style }, null]);
108
+ // Published whenever it changes, and in answer to a set, so Home
109
+ // Assistant shows what is in use.
110
+ if (result.stateChanged || fromSetTopic)
111
+ lifecycle.publish(send, (0, circadian_publishing_1.circadianStateOutputs)(publishing, result.state));
112
+ updateStatus(result.status);
113
+ showRefused(result.traces);
114
+ return result;
115
+ }
116
+ // Once the flows have started: the house's times (this node may have
117
+ // restarted alone) and the style. Its publications go out then too, from
118
+ // the lifecycle.
119
+ (0, adapter_1.whenFlowsStarted)(RED, node, (0, adapter_1.guarded)(node, () => {
120
+ const send = node.send.bind(node);
121
+ if (house)
122
+ process({ kind: "schedule", ...house.schedule() }, send);
123
+ process({ kind: "tick" }, send);
124
+ }));
125
+ if (house) {
126
+ node.on("close", house.subscribe((0, adapter_1.guarded)(node, () => {
127
+ process({ kind: "schedule", ...house.schedule() }, node.send.bind(node));
128
+ })));
129
+ }
130
+ (0, adapter_1.onInput)(node, (msg, send) => {
131
+ const lighting = (0, adapter_1.objectField)(msg, "lighting");
132
+ if (lighting?.kind === "startup") {
133
+ lifecycle.startup(send, (0, circadian_publishing_1.circadianStartingOutputs)(publishing, savedState(), roomsInArea()));
134
+ process({ kind: "tick" }, send);
135
+ return;
136
+ }
137
+ // Home Assistant's set topics become the policy's own events; a flow
138
+ // sends the same shapes the room controller took before 6.0.
139
+ const inbound = (0, circadian_publishing_1.circadianInbound)(msg, publishing);
140
+ if (inbound) {
141
+ process(inbound.event, send, inbound.traces, true);
142
+ }
143
+ else {
144
+ process(lighting ?? { kind: undefined }, send);
145
+ }
146
+ }, { message: { lighting: { kind: "startup" } }, trace: { area: publishing.area, component: "circadian-policy" } });
147
+ tickTimer = setInterval((0, adapter_1.guarded)(node, () => process({ kind: "tick" }, node.send.bind(node))), policyConfig.intervalSeconds * 1000);
148
+ // Deleted: rooms stop styling at once rather than when the last style
149
+ // runs out.
150
+ node.on("close", (removed, done) => {
151
+ clearInterval(tickTimer);
152
+ if ((0, adapter_1.wasDeleted)(RED, node, removed)) {
153
+ node.send([{ payload: undefined, lighting: { ...(0, circadian_policy_1.styleMessage)(policyConfig, savedState(), new Date()), values: null } }, null]);
154
+ }
155
+ done();
156
+ });
157
+ updateStatus((0, circadian_policy_1.transitionCircadian)(context.get("state"), { kind: "tick" }, policyConfig).status);
158
+ }
159
+ RED.nodes.registerType("home-circadian-lighting-policy", HomeCircadianLightingPolicy);
160
+ };
161
+ module.exports = registerCircadianPolicy;
162
+ //# sourceMappingURL=circadian-policy.js.map
@@ -137,8 +137,8 @@
137
137
  <ol class="node-ports">
138
138
  <li>Device commands, in a neutral shape: <code>msg.cover_command = {operation, position, requestId, generation,
139
139
  attempt}</code>, where <code>operation</code> is <code>set_position</code>, <code>stop</code> or
140
- <code>observe</code> (read the cover now). Map them to your device, as the example flow does for a
141
- Zigbee2MQTT cover.</li>
140
+ <code>observe</code> (read the cover now). Wire them to a device adapter, such as the zigbee2mqtt cover or
141
+ shelly cover node, as the example flows do.</li>
142
142
  <li>State and results: <code>msg.theatre = {kind: "target_state", target: "cover", status, details}</code>, with
143
143
  the confirmed and observed positions and any failure or manual override.</li>
144
144
  <li>Whether the cover is moving, <code>msg.payload</code> <code>true</code> or <code>false</code> with
@@ -159,7 +159,8 @@
159
159
  <code>…/set</code>, and a position on <code>…/position/set</code>, as requests like any other, carried out and
160
160
  confirmed as above; wire an MQTT in node on <code>home/&lt;area&gt;/cover/&lt;name&gt;/+</code> (and
161
161
  <code>…/position/set</code>) into this node. Nothing commands the device through Home Assistant. With
162
- <b>Home Assistant discovery</b>, a cover entity, named after this node.</p>
162
+ <b>Home Assistant discovery</b>, a cover entity named by <b>Name in topics</b>, with open, close and stop, and
163
+ for a position cover also its position and set position.</p>
163
164
  <h3>Open, close and stop only</h3>
164
165
  <p>For a motor with no reliable position, set <b>Cover</b> to <i>Open, close and stop only</i>. It takes
165
166
  <code>"open"</code>, <code>"close"</code> and <code>"stop"</code> (positions and presets are ignored), and sends
@@ -178,4 +179,9 @@
178
179
  </ul>
179
180
  <p>Battery and charging status, when the cover reports them, are kept as facts and shown in the status; the node
180
181
  never acts on them.</p>
182
+ <h3>When MQTT out connects</h3>
183
+ <p>Wire a <b>Status node</b> whose scope is your MQTT out node into this node. Each time MQTT out shows
184
+ "connected", this node takes it as its startup message (<code>msg.cover = {kind: "startup"}</code>) and announces itself again:
185
+ Node-RED's MQTT broker node drops what it is given before it connects, so what the node sends as the flows
186
+ start is lost on a first start, a full deploy or a restart. Any other status message is ignored.</p>
181
187
  </script>
@@ -132,7 +132,7 @@ const registerCoverController = (RED) => {
132
132
  return;
133
133
  }
134
134
  handleRequest(inbound ? { payload: inbound.request, source: "home-assistant" } : msg, send);
135
- });
135
+ }, { message: { cover: { kind: "startup" } }, trace: trace });
136
136
  function handleRequest(msg, send) {
137
137
  requests += 1;
138
138
  // A cover group's own ID for its request, so it can match the answer.
@@ -7,12 +7,15 @@
7
7
  area: {value: ""},
8
8
  tvHost: {value: "", required: true},
9
9
  screenHost: {value: "", required: true},
10
- // projectorMeterHost before 5.0; the dialog fills this from it.
11
- // Optional: without one, the flow reports the projector's power.
12
- projectorPowerMeterHost: {value: ""},
13
- // The name before 5.0, with no form row. Listed so the editor keeps it:
14
- // it holds only the properties named here. Cleared once the dialog
15
- // saves the new one.
10
+ // Optional: without one, the flow reports the projector's power. A node
11
+ // still naming only the old key is invalid until this is set.
12
+ projectorPowerMeterHost: {value: "", validate: function (v) {
13
+ return String(v || "").trim() !== "" || String(this.projectorMeterHost || "").trim() === "";
14
+ }},
15
+ // The name before 5.0, removed in 6.0 but kept here, with no form row,
16
+ // so the editor carries it: dropped, a deploy from the editor would
17
+ // quietly switch the node to having no built-in meter. With it, the
18
+ // node stays an invalid configuration until the new host is set.
16
19
  projectorMeterHost: {value: ""},
17
20
  // Empty: the screen's own open command. No shared default position: a
18
21
  // node saved with one keeps it.
@@ -37,16 +40,9 @@
37
40
  transportTimeoutMs: {value: 5000, required: true, validate: RED.validators.number()}
38
41
  },
39
42
  credentials: {tvPsk: {type: "password"}},
40
- oneditprepare: function () {
41
- // A flow saved before 5.0: show its meter host, so saving the dialog keeps it.
42
- if (!this.projectorPowerMeterHost && this.projectorMeterHost) {
43
- $("#node-input-projectorPowerMeterHost").val(this.projectorMeterHost);
44
- }
45
- },
43
+ // Once the new host is set, the removed name goes.
46
44
  oneditsave: function () {
47
- // The dialog showed the old host in the field, so what's left there is
48
- // the user's choice, including none.
49
- this.projectorMeterHost = "";
45
+ if (String($("#node-input-projectorPowerMeterHost").val() || "").trim() !== "") this.projectorMeterHost = "";
50
46
  },
51
47
  inputs: 1,
52
48
  outputs: 3,
@@ -97,8 +93,9 @@
97
93
  that knows where it is: one that doesn't is closed first, and if it still has no position (not calibrated), the
98
94
  request fails with reason <code>screen-not-calibrated</code>; calibrate it, or clear the viewing position. The
99
95
  <b>Projector power meter host</b>, if set, is a Shelly Gen2 switch with power metering on the projector's supply
100
- (switch 1), read against <b>Projector on watts</b> and <b>Projector standby watts</b>. A flow saved before 5.0 names
101
- that host <code>projectorMeterHost</code>, which is still read.</p>
96
+ (switch 1), read against <b>Projector on watts</b> and <b>Projector standby watts</b>. A node still naming
97
+ that host <code>projectorMeterHost</code>, as before 5.0, is an invalid configuration, in the editor and when
98
+ deployed: that name was removed in 6.0, so set <b>Projector power meter host</b>.</p>
102
99
  <p>Output 1 carries the projector power toggle (<code>msg.payload = "power"</code> with <code>msg.display_command</code>) for your infrared transport, commanded directly rather than through Home Assistant. Enable “Projector power is fallback” to include its use in activity metrics. Output 2 emits observed state plus ready, manual-override, failed, rejected and superseded results, and <code>msg.theatre</code> target state for the theatre activity controller: wire it straight back to that controller. Output 3 asks for the projector's power when there is no meter host (below).</p>
103
100
  <h3>Projector power from your own flow</h3>
104
101
  <p>Leave <b>Projector power meter host</b> empty to report the projector's power from any meter or adapter: send
@@ -17,7 +17,10 @@ const registerDisplayModeController = (RED) => {
17
17
  controllerConfig = (0, display_mode_controller_1.normaliseConfig)(config);
18
18
  // Optional: without a meter of its own, the node asks the flow for the
19
19
  // projector's power on its third output, and reads what comes back.
20
- projectorPowerMeterHost = String(config.projectorPowerMeterHost ?? "").trim() || String(config.projectorMeterHost ?? "").trim();
20
+ projectorPowerMeterHost = String(config.projectorPowerMeterHost ?? "").trim();
21
+ if (!projectorPowerMeterHost && String(config.projectorMeterHost ?? "").trim()) {
22
+ throw new Error("projectorMeterHost was removed in 6.0: set Projector power meter host");
23
+ }
21
24
  transport = new display_transport_1.DisplayTransport({
22
25
  // No defaults: these name the user's own devices.
23
26
  tvHost: (0, settings_1.textSetting)(config.tvHost, "TV host"),
@@ -58,4 +58,9 @@
58
58
  <ol class="node-ports">
59
59
  <li>MQTT publications.</li>
60
60
  </ol>
61
+ <h3>When MQTT out connects</h3>
62
+ <p>Wire a <b>Status node</b> whose scope is your MQTT out node into this node. Each time MQTT out shows
63
+ "connected", this node takes it as its startup message (<code>msg.source = "startup"</code>) and announces itself again:
64
+ Node-RED's MQTT broker node drops what it is given before it connects, so what the node sends as the flows
65
+ start is lost on a first start, a full deploy or a restart. Any other status message is ignored.</p>
61
66
  </script>
@@ -72,7 +72,7 @@ const registerHouseScheduleMqtt = (RED) => {
72
72
  else if (inbound) {
73
73
  (0, adapter_1.runReducer)(node, trace, (state) => ({ state, traces: inbound.traces }));
74
74
  }
75
- });
75
+ }, { message: { lighting: { kind: "startup" } }, trace: trace });
76
76
  }
77
77
  RED.nodes.registerType("home-house-schedule-mqtt", HomeHouseScheduleMqtt);
78
78
  };
@@ -22,9 +22,9 @@
22
22
  </script>
23
23
 
24
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
25
+ <p>One wake and bed time shared by every circadian lighting policy whose <b>Schedule</b> is this one. A policy
26
+ that keeps its own times (for a child's room, say) chooses none.</p>
27
+ <p>The policies hear of every change as it happens, without wires. Times change from a message or from Home Assistant
28
28
  through a <i>house schedule MQTT</i> node; a time changed that way lasts until this schedule is deployed with a
29
29
  different <b>Wake time</b> or <b>Bed time</b>, which then applies.</p>
30
30
  <p>The <b>Name</b> labels it and names its topics, <code>home/&lt;name&gt;/schedule/...</code>.</p>
@@ -81,14 +81,17 @@
81
81
  <dl class="message-properties">
82
82
  <dt>occupancy</dt><dd><code>msg.payload.occupied</code>, from the occupancy fusion node.</dd>
83
83
  <dt>illuminance</dt><dd>the light level in lux.</dd>
84
- <dt>area-enabled</dt><dd>ON or OFF for this node; <code>global-enabled</code> for every motion node.</dd>
84
+ <dt>area-enabled</dt><dd><code>true</code> or <code>false</code> for this node; <code>global-enabled</code> for every
85
+ motion node. Any other value, such as <code>"ON"</code> (accepted before 6.0), is refused: nothing changes, the
86
+ node logs a warning once per deploy and shows it on its status.</dd>
85
87
  <dt>delay</dt><dd>a new vacancy time, in <b>seconds</b>, the same unit as <b>Vacancy seconds</b>.</dd>
86
88
  <dt>illuminance-cut-off</dt><dd>a new lux cutoff.</dd>
87
89
  <dt>pause</dt><dd><code>true</code> pauses motion lighting, <code>false</code> releases it, for the owner in
88
90
  <code>msg.owner</code> (for example a cover's name), which is required. A pause lasts at most the
89
91
  <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
92
  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>
93
+ which are the user's own: wire a cover's <i>moving</i> output here to pause motion while it moves. Any value but
94
+ <code>true</code> or <code>false</code> is refused, as for the enables.</dd>
92
95
  </dl>
93
96
  <p>A <code>delay</code> or <code>illuminance-cut-off</code> message overrides the setting, and lasts across
94
97
  restarts until the node is deployed with a different <b>Vacancy seconds</b> or <b>Lux cutoff</b>, which then
@@ -121,4 +124,9 @@
121
124
  <li>Lighting intents, for the room lighting controller.</li>
122
125
  <li>MQTT publications, when publishing is on.</li>
123
126
  </ol>
127
+ <h3>When MQTT out connects</h3>
128
+ <p>Wire a <b>Status node</b> whose scope is your MQTT out node into this node. Each time MQTT out shows
129
+ "connected", this node takes it as its startup message (<code>msg.source = "startup"</code>) and announces itself again:
130
+ Node-RED's MQTT broker node drops what it is given before it connects, so what the node sends as the flows
131
+ start is lost on a first start, a full deploy or a restart. Any other status message is ignored.</p>
124
132
  </script>