@avantics/node-red-home-automation 6.1.0 → 6.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (115) hide show
  1. package/README.md +260 -46
  2. package/dist/lib/activity-lighting-controller.d.ts +1 -0
  3. package/dist/lib/activity-lighting-controller.js +37 -10
  4. package/dist/lib/avr-controller.d.ts +3 -0
  5. package/dist/lib/avr-controller.js +24 -2
  6. package/dist/lib/backlighting-controller.d.ts +5 -2
  7. package/dist/lib/backlighting-controller.js +27 -2
  8. package/dist/lib/cover-publishing.js +20 -6
  9. package/dist/lib/display-mode-controller.d.ts +3 -0
  10. package/dist/lib/display-mode-controller.js +43 -16
  11. package/dist/lib/esphome-sensor.d.ts +73 -0
  12. package/dist/lib/esphome-sensor.js +261 -0
  13. package/dist/lib/home-assistant-setting.d.ts +23 -0
  14. package/dist/lib/home-assistant-setting.js +39 -0
  15. package/dist/lib/motion-policy.d.ts +1 -0
  16. package/dist/lib/motion-policy.js +21 -1
  17. package/dist/lib/occupancy-fusion.d.ts +1 -0
  18. package/dist/lib/occupancy-fusion.js +15 -2
  19. package/dist/lib/room-controller.d.ts +1 -0
  20. package/dist/lib/room-controller.js +5 -0
  21. package/dist/lib/room-publishing.d.ts +1 -0
  22. package/dist/lib/room-publishing.js +11 -0
  23. package/dist/lib/scene-publishing.d.ts +30 -0
  24. package/dist/lib/scene-publishing.js +63 -0
  25. package/dist/lib/scene.d.ts +69 -0
  26. package/dist/lib/scene.js +223 -0
  27. package/dist/lib/shelly-cover.js +5 -0
  28. package/dist/lib/shelly-relay-light.d.ts +24 -0
  29. package/dist/lib/shelly-relay-light.js +93 -0
  30. package/dist/lib/shelly-switch.d.ts +8 -0
  31. package/dist/lib/shelly-switch.js +37 -8
  32. package/dist/lib/source-capability-controller.d.ts +4 -1
  33. package/dist/lib/source-capability-controller.js +49 -2
  34. package/dist/lib/target-observation.d.ts +14 -0
  35. package/dist/lib/target-observation.js +22 -0
  36. package/dist/lib/theatre-activity-controller.d.ts +5 -0
  37. package/dist/lib/theatre-activity-controller.js +91 -12
  38. package/dist/lib/theatre-metrics.js +6 -4
  39. package/dist/lib/theatre-slo.js +3 -0
  40. package/dist/lib/theatre-state-contract.d.ts +4 -2
  41. package/dist/lib/theatre-state-contract.js +51 -8
  42. package/dist/lib/theatre-state.d.ts +2 -0
  43. package/dist/lib/theatre-state.js +13 -1
  44. package/dist/lib/wled.d.ts +48 -0
  45. package/dist/lib/wled.js +105 -0
  46. package/dist/lib/z2m-power.d.ts +38 -0
  47. package/dist/lib/z2m-power.js +83 -0
  48. package/dist/lib/z2m-sensor.d.ts +37 -0
  49. package/dist/lib/z2m-sensor.js +88 -0
  50. package/dist/nodes/apollo-r-pro-1.d.ts +2 -0
  51. package/dist/nodes/apollo-r-pro-1.html +80 -0
  52. package/dist/nodes/apollo-r-pro-1.js +4 -0
  53. package/dist/nodes/avr-controller.html +82 -21
  54. package/dist/nodes/avr-controller.js +24 -12
  55. package/dist/nodes/backlighting-controller.js +3 -2
  56. package/dist/nodes/circadian-policy.html +84 -20
  57. package/dist/nodes/circadian-policy.js +30 -11
  58. package/dist/nodes/cover-controller.html +87 -25
  59. package/dist/nodes/cover-controller.js +25 -12
  60. package/dist/nodes/device-side.d.ts +21 -0
  61. package/dist/nodes/device-side.js +76 -0
  62. package/dist/nodes/display-mode-controller.js +11 -2
  63. package/dist/nodes/esphome-sensor.d.ts +2 -0
  64. package/dist/nodes/esphome-sensor.js +77 -0
  65. package/dist/nodes/everything-presence-one.d.ts +2 -0
  66. package/dist/nodes/everything-presence-one.html +78 -0
  67. package/dist/nodes/everything-presence-one.js +4 -0
  68. package/dist/nodes/home-assistant-discovery.d.ts +26 -0
  69. package/dist/nodes/home-assistant-discovery.js +64 -0
  70. package/dist/nodes/house-schedule-mqtt.html +70 -5
  71. package/dist/nodes/house-schedule-mqtt.js +22 -8
  72. package/dist/nodes/motion-policy.html +87 -22
  73. package/dist/nodes/motion-policy.js +44 -11
  74. package/dist/nodes/mqtt-broker-link.d.ts +41 -0
  75. package/dist/nodes/mqtt-broker-link.js +91 -0
  76. package/dist/nodes/occupancy-fusion.html +86 -20
  77. package/dist/nodes/occupancy-fusion.js +25 -12
  78. package/dist/nodes/publishing-side.d.ts +20 -0
  79. package/dist/nodes/publishing-side.js +66 -0
  80. package/dist/nodes/room-controller.html +106 -35
  81. package/dist/nodes/room-controller.js +63 -16
  82. package/dist/nodes/scene.d.ts +3 -0
  83. package/dist/nodes/scene.html +247 -0
  84. package/dist/nodes/scene.js +128 -0
  85. package/dist/nodes/shelly-cover.html +47 -2
  86. package/dist/nodes/shelly-cover.js +19 -4
  87. package/dist/nodes/shelly-switch.html +76 -6
  88. package/dist/nodes/shelly-switch.js +32 -7
  89. package/dist/nodes/source-capability-controller.js +3 -2
  90. package/dist/nodes/theatre-activity-controller.html +4 -2
  91. package/dist/nodes/theatre-state-contract.html +72 -9
  92. package/dist/nodes/theatre-state-contract.js +33 -14
  93. package/dist/nodes/wled.d.ts +3 -0
  94. package/dist/nodes/wled.html +56 -0
  95. package/dist/nodes/wled.js +53 -0
  96. package/dist/nodes/z2m-cover.html +47 -2
  97. package/dist/nodes/z2m-cover.js +21 -4
  98. package/dist/nodes/z2m-light.html +49 -3
  99. package/dist/nodes/z2m-light.js +40 -4
  100. package/dist/nodes/z2m-power.d.ts +3 -0
  101. package/dist/nodes/z2m-power.html +58 -0
  102. package/dist/nodes/z2m-power.js +53 -0
  103. package/dist/nodes/z2m-sensor.d.ts +3 -0
  104. package/dist/nodes/z2m-sensor.html +69 -0
  105. package/dist/nodes/z2m-sensor.js +62 -0
  106. package/examples/Cover control.json +38 -91
  107. package/examples/Cover pair.json +36 -3
  108. package/examples/Home theatre activity.json +46 -91
  109. package/examples/Home theatre equipment.json +34 -2
  110. package/examples/House schedule.json +58 -80
  111. package/examples/Room lighting.json +45 -44
  112. package/examples/Scene.json +167 -0
  113. package/examples/Shelly cover.json +36 -105
  114. package/examples/Shelly switch.json +52 -148
  115. package/package.json +8 -2
@@ -0,0 +1,247 @@
1
+ <script type="text/javascript">
2
+ (function () {
3
+ // The lights as lib/scene.ts reads them: its room's area and its id there,
4
+ // ON or OFF, and for an ON light optional brightnessPct (0-100) and
5
+ // colorTempKelvin (above 0); each light once.
6
+ function validLights(v) {
7
+ try {
8
+ var lights = JSON.parse(v);
9
+ if (!Array.isArray(lights)) return false;
10
+ var seen = {};
11
+ return lights.every(function (light) {
12
+ if (!light || typeof light !== "object" || Array.isArray(light)) return false;
13
+ var target = typeof light.target === "string" ? light.target.trim() : "";
14
+ var area = topicName(light.area);
15
+ var state = String(light.state || "").toUpperCase();
16
+ if (!target || !area || seen[area + "/" + target] || (state !== "ON" && state !== "OFF")) return false;
17
+ seen[area + "/" + target] = true;
18
+ return Object.keys(light).every(function (key) {
19
+ var value = light[key];
20
+ if (key === "area" || key === "target" || key === "state") return true;
21
+ if (state === "OFF" || typeof value !== "number" || !isFinite(value)) return false;
22
+ if (key === "brightnessPct") return value >= 0 && value <= 100;
23
+ if (key === "colorTempKelvin") return value > 0;
24
+ return false;
25
+ });
26
+ });
27
+ } catch (e) { return false; }
28
+ }
29
+ // A name or area as topics and Home Assistant IDs read it (lib/topic-names.ts areaId).
30
+ function topicName(v) { return String(v || "").trim().toLowerCase().replace(/[^a-z0-9_-]+/g, "_").replace(/^[_-]+|[_-]+$/g, ""); }
31
+ // Its Publish choice (off, state or discovery), from the open dialog or
32
+ // its saved settings: a node saved before the choice reads its two old
33
+ // settings (lib/home-assistant-setting.ts).
34
+ function publishChoice(node) {
35
+ function on(value) { return value === true || value === "true"; }
36
+ var select = $("#node-input-homeAssistant");
37
+ if (select.length) return select.val();
38
+ if (node.homeAssistant === "off" || node.homeAssistant === "state" || node.homeAssistant === "discovery") return node.homeAssistant;
39
+ return !on(node.publishState) ? "off" : on(node.discovery) ? "discovery" : "state";
40
+ }
41
+ // Whether it publishes through its output, as before 6.2: a node saved
42
+ // before the Publish choice that published, or one opened and saved since
43
+ // without a broker (publishesThroughOutput). It needs no broker until one
44
+ // is chosen; from saved values only, not the open dialog.
45
+ function throughOutput(node) {
46
+ if (node.publishesThroughOutput === true) return true;
47
+ return (node.homeAssistant === null || node.homeAssistant === undefined) && (node.publishState === true || node.publishState === "true");
48
+ }
49
+ // Its area is needed while it publishes, and for a node saved before the
50
+ // choice whenever either old setting was on, as the runtime checks.
51
+ function needsArea(node) {
52
+ function on(value) { return value === true || value === "true"; }
53
+ var saved = !$("#node-input-homeAssistant").length && (node.homeAssistant === null || node.homeAssistant === undefined);
54
+ return publishChoice(node) !== "off" || (saved && (on(node.publishState) || on(node.discovery)));
55
+ }
56
+ RED.nodes.registerType("home-scene", {
57
+ category: "home automation",
58
+ color: "#e2d96e",
59
+ defaults: {
60
+ // No default: it names the scene to its rooms, motion policies and
61
+ // Home Assistant, so two scenes can't share one: they would share an
62
+ // owner, and one's end would release the other's lights and pause.
63
+ name: {value: "", required: true, validate: function (v) {
64
+ var own = topicName(v);
65
+ if (!own) return false;
66
+ var self = this;
67
+ var clash = false;
68
+ RED.nodes.eachNode(function (other) {
69
+ if (other.type === "home-scene" && other.id !== self.id && topicName(other.name) === own) clash = true;
70
+ });
71
+ return clash ? "another scene has this name" : true;
72
+ }},
73
+ lights: {value: "[]", required: true, validate: validLights},
74
+ safetyHours: {value: 4, required: true, validate: function (v) { var n = Number(v); return String(v).trim() !== "" && isFinite(n) && n >= 0; }},
75
+ // Optional: a power meter's reading (msg.power) below this many watts,
76
+ // for endBelowSeconds, ends the scene. Empty: none. A scene saved
77
+ // before these settings loads without them, which is valid.
78
+ endBelowWatts: {value: "", validate: function (v) {
79
+ if (v === undefined || v === null || String(v).trim() === "") return true;
80
+ var n = Number(v); return isFinite(n) && n > 0;
81
+ }},
82
+ endBelowSeconds: {value: 60, validate: function (v) {
83
+ if (v === undefined || v === null || String(v).trim() === "") return true;
84
+ var n = Number(v); return isFinite(n) && n >= 0;
85
+ }},
86
+ area: {value: "", validate: function (v) { return !needsArea(this) || /[a-z0-9]/i.test(String(v || "")); }},
87
+ // Off until switched on.
88
+ // Publish: off, state or discovery. null means "not set": a node saved
89
+ // before this setting, read from the two below, kept with no rows.
90
+ homeAssistant: {value: null},
91
+ // Node-RED's MQTT broker node, through which it publishes and reads its set
92
+ // topics itself. Needed unless Publish is "Don't publish". The editor offers
93
+ // the first broker when the dialog is opened and saved, not when the node
94
+ // is dropped.
95
+ broker: {value: "", type: "mqtt-broker", required: false, validate: function (v) {
96
+ return publishChoice(this) === "off" || (Boolean(v) && v !== "_ADD_") || throughOutput(this) ? true : "choose a broker, or set Publish to Don't publish";
97
+ }},
98
+ publishState: {value: false},
99
+ discovery: {value: false},
100
+ // true for a node that publishes through its output, as before 6.2, and
101
+ // was opened and saved without a broker: it stays valid until a broker is
102
+ // chosen. No form row.
103
+ publishesThroughOutput: {value: false},
104
+ discoveryPrefix: {value: "homeassistant", required: true, validate: function (v) { var prefix = String(v || "").trim().replace(/^\/+|\/+$/g, ""); return Boolean(prefix) && !/[+#]/.test(prefix); }},
105
+ idScheme: {value: "node"}
106
+ },
107
+ oneditsave: function () {
108
+ // Through its output until a broker is chosen (read before the
109
+ // dialog's values are applied).
110
+ var broker = $("#node-input-broker").val();
111
+ this.publishesThroughOutput = throughOutput(this) && (!broker || broker === "_ADD_");
112
+ },
113
+ oneditprepare: function () {
114
+ function on(value) { return value === true || value === "true"; }
115
+ $("#node-input-homeAssistant").val(publishChoice(this));
116
+ // The broker only while it publishes, the prefix only with discovery.
117
+ var outputBefore = throughOutput(this);
118
+ function publishRows() {
119
+ var broker = $("#node-input-broker").val();
120
+ $(".publish-through-output").toggle($("#node-input-homeAssistant").val() !== "off" && outputBefore && (!broker || broker === "_ADD_"));
121
+ var choice = $("#node-input-homeAssistant").val();
122
+ $(".publish-broker").toggle(choice !== "off");
123
+ $(".publish-discovery").toggle(choice === "discovery");
124
+ $("#node-input-area, #node-input-broker").trigger("change");
125
+ }
126
+ $("#node-input-homeAssistant, #node-input-broker").on("change", publishRows);
127
+ publishRows();
128
+ },
129
+ inputs: 1,
130
+ outputs: 3,
131
+ outputLabels: ["to room lighting controllers", "to motion lighting policies", "MQTT publications"],
132
+ icon: "font-awesome/fa-magic",
133
+ label: function () { return this.name || "scene"; },
134
+ paletteLabel: "scene"
135
+ });
136
+ })();
137
+ </script>
138
+
139
+ <script type="text/html" data-template-name="home-scene">
140
+ <div class="form-row"><label for="node-input-name">Name</label><input type="text" id="node-input-name" placeholder="Bath, for example"></div>
141
+ <div class="form-row"><label for="node-input-lights">Lights</label><input type="text" id="node-input-lights" placeholder='[{"area":"gym","target":"main","state":"OFF"}]'></div>
142
+ <div class="form-row"><label for="node-input-safetyHours">Safety timer (hours)</label><input type="number" id="node-input-safetyHours" min="0" step="0.25"> <span>(0: off)</span></div>
143
+ <div class="form-row"><label for="node-input-endBelowWatts">End below (W)</label><input type="number" id="node-input-endBelowWatts" min="0" step="1" placeholder="none"> <span>for</span>
144
+ <input type="number" id="node-input-endBelowSeconds" min="0" step="1" style="width:70px"> <span>s</span></div>
145
+ <div class="form-tips">Wire a power meter node (such as a zigbee2mqtt power node on a device's plug) to the scene: power
146
+ below this, held that long, ends the scene "by power".</div>
147
+ <div class="form-row"><label for="node-input-area">Area</label><input type="text" id="node-input-area"></div>
148
+ <div class="form-row">
149
+ <label for="node-input-homeAssistant">Publish</label>
150
+ <select id="node-input-homeAssistant">
151
+ <option value="off">Don't publish</option>
152
+ <option value="state">MQTT state</option>
153
+ <option value="discovery">MQTT state and Home Assistant discovery</option>
154
+ </select>
155
+ </div>
156
+ <div class="form-row publish-broker"><label for="node-input-broker">Broker</label><input type="text" id="node-input-broker"></div>
157
+ <div class="form-tips publish-broker">With a broker, it publishes and reads its set topics itself: an MQTT out on its
158
+ publication output, an MQTT in on its set topics, and a Status node are no longer needed and can be deleted.</div>
159
+ <div class="form-tips publish-through-output">This node publishes through its output, as before 6.2, and keeps
160
+ working that way. Choose a Broker to let it publish itself.</div>
161
+ <div class="form-row publish-discovery"><label for="node-input-discoveryPrefix">Discovery prefix</label><input type="text" id="node-input-discoveryPrefix"></div>
162
+ </script>
163
+
164
+ <script type="text/html" data-help-name="home-scene">
165
+ <p>One scene, such as <i>Bath</i> or <i>Exercise mode</i>. While it is on, it holds some of a room's lights on or
166
+ off with the values it gives them, and pauses motion lighting. When it ends, it lets them go, and each room gives
167
+ its lights back what they were before the scene (or to a scene that is still on beneath it). It reaches lights
168
+ only through room lighting controllers, and never through Home Assistant.</p>
169
+ <h3>Wiring</h3>
170
+ <ol>
171
+ <li>Output 1 → each <b>room lighting controller</b> whose lights it sets.</li>
172
+ <li>Output 2 → each <b>motion lighting policy</b> it should pause while on.</li>
173
+ <li>Output 3 → an <b>MQTT out</b> node, when it publishes its switch for Home Assistant, with an <b>MQTT in</b>
174
+ node on <code>home/&lt;area&gt;/scene/&lt;name&gt;/set</code> into this node.</li>
175
+ </ol>
176
+ <h3>Inputs</h3>
177
+ <dl class="message-properties">
178
+ <dt>scene <span class="property-type">object</span></dt>
179
+ <dd><code>{kind: "start"}</code> turns it on (on again: a fresh safety timer, its lights left as they are);
180
+ <code>{kind: "end", by}</code> ends it, with <code>by</code> (optional) shown as what ended it, for any
181
+ end trigger a flow decides. <code>{kind: "startup"}</code> announces its switch again.</dd>
182
+ <dt>power <span class="property-type">object</span></dt>
183
+ <dd><code>{kind: "observed", watts}</code> from a power meter node: with <b>End below</b> set, power below it,
184
+ held for its seconds, ends the scene <i>by power</i>.</dd>
185
+ <dt>payload <span class="property-type">string</span></dt>
186
+ <dd><code>ON</code> or <code>OFF</code> on its set topic, from Home Assistant: a start, or an end by
187
+ <code>home-assistant</code>.</dd>
188
+ </dl>
189
+ <h3>Broker</h3>
190
+ <p>Choose Node-RED's <b>MQTT broker</b> node, and it publishes and reads its set topic itself: it announces everything each time the
191
+ broker connects, reconnects included, and each time Home Assistant comes back (<code>online</code> on
192
+ <code>&lt;prefix&gt;/status</code>), so its entities come back by themselves.
193
+ Each connect also runs its startup message, as a Status node's "connected" did without a broker. Its publication output then sends
194
+ nothing, and an MQTT out on it, an MQTT in on its set topic and a Status node are no longer needed: still wired, they change nothing.
195
+ Opening and saving the node offers the first broker in your flows. A node saved before 6.2 that publishes keeps
196
+ publishing on its output, as below, and needs no broker until you choose one.</p>
197
+ <h3>When MQTT out connects</h3>
198
+ <p>Without a broker, wire a <b>Status node</b> whose scope is your MQTT out node into this node. Each time MQTT out shows
199
+ "connected", this node takes it as its startup message (<code>msg.scene = {kind: "startup"}</code>) and announces
200
+ itself again: Node-RED's MQTT broker node drops what it is given before it connects, so what the node sends as
201
+ the flows start is lost on a first start, a full deploy or a restart. Any other status message is ignored.</p>
202
+ <h3>Outputs</h3>
203
+ <ol class="node-ports">
204
+ <li>Room lighting controllers: an owner intent per light as it starts,
205
+ <code>{lighting: {kind: "intent", area, targets: [id], state, values?, owner: "scene:&lt;name&gt;", source: "scene"}}</code>,
206
+ and a release per room as it ends, <code>{lighting: {kind: "release", area, owner, targets, source: "scene"}}</code>.</li>
207
+ <li>Motion lighting policies: <code>{source: "pause", payload: true, owner}</code> as it starts and every minute
208
+ while on, and <code>payload: false</code> as it ends. Keep each motion policy's <b>Maximum pause</b> above a
209
+ minute (10 minutes by default), or motion runs between renewals. If the room went vacant meanwhile, motion's
210
+ vacancy delay starts when the pause lifts.</li>
211
+ <li>MQTT publications: its switch's state, availability and discovery.</li>
212
+ </ol>
213
+ <h3>Settings</h3>
214
+ <dl class="message-properties">
215
+ <dt>Name</dt><dd>Required, and each scene's own: it names the scene to its rooms and motion policies (owner
216
+ <code>scene:&lt;name&gt;</code>), and in its topics and Home Assistant. The editor marks a name another scene
217
+ has (compared as in topics, so <code>Bath</code> and <code>bath</code> are the same).</dd>
218
+ <dt>Lights</dt><dd>A JSON array, one entry per light: <code>area</code> (its room controller's <b>Area</b>),
219
+ <code>target</code> (the light's id in that room's Targets JSON), <code>state</code> <code>ON</code> or
220
+ <code>OFF</code>, and for an ON light <code>brightnessPct</code> (0-100) and <code>colorTempKelvin</code>
221
+ (optional). A socket is a room target with on/off only: give it just its state. For example
222
+ <code>[{"area": "gym", "target": "main", "state": "OFF"}, {"area": "gym", "target": "tv", "state": "ON"}]</code>.</dd>
223
+ <dt>Safety timer</dt><dd>Hours until it ends itself, so a forgotten scene doesn't hold the room: 4 by
224
+ default; 0 turns it off.</dd>
225
+ <dt>End below</dt><dd>Optional: watts, and for how many seconds (60 by default). Once the device has been
226
+ used since the scene started (a power reading, <code>msg.power</code>, at or above it), a reading below it,
227
+ held that long, ends the scene <i>by power</i>; one at or above it starts the wait again. Before the device is
228
+ used, its standby draw is ignored. Empty: no power end.</dd>
229
+ <dt>Area, Publish</dt><dd><i>Don't publish</i> unless chosen. <i>MQTT state</i>: its state and set topic, plain
230
+ MQTT; <i>MQTT state and Home Assistant discovery</i>: also a switch entity for the scene
231
+ that shows whether it is on and turns it on and off, on <code>home/&lt;area&gt;/scene/&lt;name&gt;/…</code>.</dd>
232
+ </dl>
233
+ <h3>Rules</h3>
234
+ <ul>
235
+ <li>Each light names its room by the room controller's <b>Area</b>, so set the Area on each room a scene uses.
236
+ Its intents and releases carry that area, and a room acts only on those for its own Area: two rooms may each
237
+ have a <code>main</code>.</li>
238
+ <li>Scenes stack: a newer scene holding a light another scene holds takes it, and hands it back to the older
239
+ one when it ends. Ending the older one first leaves the light with the newer.</li>
240
+ <li>A person changing a light the scene holds makes it theirs: the scene's end leaves it alone.</li>
241
+ <li>On while the flows restart (with a context store), it stays on, with its timer; if its time ran out
242
+ meanwhile, it ends as they start. Deleted while on, it lets its lights and motion go.</li>
243
+ </ul>
244
+ <h3>Status</h3>
245
+ <p><b>on · 3h 59m left</b> (green) while on; <b>off · ended by timer</b>, <b>request</b>,
246
+ <b>home-assistant</b> or what the end gave; <b>off</b> before it has run.</p>
247
+ </script>
@@ -0,0 +1,128 @@
1
+ "use strict";
2
+ const ha_discovery_1 = require("../lib/ha-discovery");
3
+ const publishing_node_1 = require("../lib/publishing-node");
4
+ const scene_1 = require("../lib/scene");
5
+ const scene_publishing_1 = require("../lib/scene-publishing");
6
+ const settings_1 = require("../lib/settings");
7
+ const topic_names_1 = require("../lib/topic-names");
8
+ const adapter_1 = require("./adapter");
9
+ const publishing_side_1 = require("./publishing-side");
10
+ const home_assistant_setting_1 = require("../lib/home-assistant-setting");
11
+ const registerScene = (RED) => {
12
+ function HomeScene(config) {
13
+ RED.nodes.createNode(this, config);
14
+ const node = this;
15
+ const context = node.context();
16
+ let timer;
17
+ let scene;
18
+ let publishing;
19
+ let area;
20
+ let broker;
21
+ try {
22
+ // No default: it names the scene to its rooms, motion policies and
23
+ // Home Assistant.
24
+ const name = (0, settings_1.textSetting)(config.name, "name");
25
+ if (!(0, topic_names_1.areaId)(name))
26
+ throw new Error("name must contain a letter or digit");
27
+ scene = (0, scene_1.normaliseSceneConfig)({ owner: "scene:" + name, lights: config.lights, safetyHours: config.safetyHours,
28
+ endBelowWatts: config.endBelowWatts, endBelowSeconds: config.endBelowSeconds });
29
+ area = (0, adapter_1.optionalArea)(config);
30
+ const publish = (0, home_assistant_setting_1.publishingChoice)(config, { publishState: false, discovery: false });
31
+ if (publish.needsArea)
32
+ (0, topic_names_1.publishingArea)(area, true);
33
+ broker = (0, publishing_side_1.brokerSetting)(RED, config.broker, publish.publishState);
34
+ publishing = {
35
+ area,
36
+ name,
37
+ publishState: publish.publishState,
38
+ discovery: publish.discovery,
39
+ discoveryPrefix: (0, ha_discovery_1.discoveryPrefixSetting)(config.discoveryPrefix),
40
+ nodeId: node.id,
41
+ idScheme: (0, settings_1.optionalChoiceSetting)(config.idScheme, "ID scheme", ha_discovery_1.ID_SCHEMES) ?? "node"
42
+ };
43
+ }
44
+ catch (error) {
45
+ (0, adapter_1.invalidConfig)(node, error);
46
+ return;
47
+ }
48
+ const trace = { area, component: "scene" };
49
+ const saved = () => context.get("state");
50
+ const active = () => saved()?.active === true;
51
+ // Its switch, available and clear of what it no longer publishes once
52
+ // it can be (with a broker, on each connect; without one, on output 3
53
+ // once the flows have started), offline as it stops, nothing left
54
+ // behind when deleted.
55
+ const lifecycle = (0, publishing_side_1.publishingSide)(RED, node, broker, {
56
+ retainedTopics: () => (0, scene_publishing_1.sceneRetainedTopics)(publishing),
57
+ possible: { owner: publishing, topics: () => (0, publishing_node_1.publishesState)(publishing) ? (0, scene_publishing_1.sceneRetainedTopics)({ ...publishing, discovery: true }) : [] },
58
+ starting: () => (0, scene_publishing_1.sceneStartingOutputs)(publishing, active()),
59
+ stopping: () => (0, scene_publishing_1.sceneStoppingOutputs)(publishing),
60
+ output: 2,
61
+ outputs: 3,
62
+ startupMessage: { scene: { kind: "startup" } },
63
+ statusTopic: publishing.discovery ? publishing.discoveryPrefix + "/status" : null,
64
+ setTopics: (0, publishing_node_1.publishesState)(publishing) ? [(0, scene_publishing_1.sceneCommandTopic)(publishing)] : []
65
+ }, trace, (msg, send) => handle(msg, send));
66
+ function process(event, send) {
67
+ const wasActive = active();
68
+ const result = (0, adapter_1.runReducer)(node, trace, (state, now) => (0, scene_1.transitionScene)(state, { scene: event }, scene, now));
69
+ // Owner intents and releases for room controllers on output 1, owner
70
+ // pauses for motion policies on output 2.
71
+ if (result.lighting.length || result.motion.length) {
72
+ send([result.lighting.length ? result.lighting : null, result.motion.length ? result.motion : null, null]);
73
+ }
74
+ if (result.state.active !== wasActive)
75
+ lifecycle.publish(send, (0, scene_publishing_1.sceneStateOutputs)(publishing, result.state.active));
76
+ node.status(result.status);
77
+ clearTimeout(timer);
78
+ const at = (0, scene_1.sceneTickAt)(result.state, Date.now(), scene);
79
+ timer = at === null ? undefined : setTimeout((0, adapter_1.guarded)(node, () => process({ kind: "tick" }, node.send.bind(node))), Math.max(0, at - Date.now()));
80
+ }
81
+ // A scene on when the flows stopped is still on: renew its pause and its
82
+ // timer, or end it if its time ran out meanwhile.
83
+ (0, adapter_1.whenFlowsStarted)(RED, node, () => process({ kind: "startup" }, node.send.bind(node)));
84
+ function handle(msg, send) {
85
+ // A power meter's reading, for its End below.
86
+ const power = (0, adapter_1.objectField)(msg, "power");
87
+ if (power?.kind === "observed" && !(0, adapter_1.objectField)(msg, "scene")) {
88
+ process({ kind: "power", watts: power.watts }, send);
89
+ return;
90
+ }
91
+ const event = (0, adapter_1.objectField)(msg, "scene");
92
+ if (event) {
93
+ if (event.kind === "startup")
94
+ lifecycle.startup(send, (0, scene_publishing_1.sceneStartingOutputs)(publishing, active()));
95
+ process(event, send);
96
+ return;
97
+ }
98
+ // Home Assistant's switch, on the scene's set topic.
99
+ const inbound = (0, scene_publishing_1.sceneInbound)(msg, publishing);
100
+ if (inbound?.kind) {
101
+ process(inbound.kind === "end" ? { kind: "end", by: "home-assistant" } : { kind: "start" }, send);
102
+ }
103
+ else if (inbound) {
104
+ (0, adapter_1.runReducer)(node, trace, (state) => ({ state, traces: inbound.traces }));
105
+ }
106
+ else {
107
+ (0, adapter_1.runReducer)(node, trace, (state) => ({ state, traces: [{ event: "input", outcome: "ignored", reason: "not-a-scene-message", details: {} }] }));
108
+ }
109
+ }
110
+ (0, adapter_1.onInput)(node, (msg, send) => {
111
+ // With a broker, its set topic comes from the broker: an MQTT in or a
112
+ // Status node still wired in changes nothing.
113
+ if (!lifecycle.fromBroker(msg))
114
+ handle(msg, send);
115
+ }, lifecycle.startupOption({ scene: { kind: "startup" } }));
116
+ node.on("close", (removed, done) => {
117
+ clearTimeout(timer);
118
+ // Deleted while on: let its lights and motion go. Best effort: it
119
+ // reaches nodes still running, as in a deploy of modified nodes.
120
+ if ((0, adapter_1.wasDeleted)(RED, node, removed) && active())
121
+ process({ kind: "end", by: "deleted" }, node.send.bind(node));
122
+ done();
123
+ });
124
+ }
125
+ RED.nodes.registerType("home-scene", HomeScene);
126
+ };
127
+ module.exports = registerScene;
128
+ //# sourceMappingURL=scene.js.map
@@ -3,7 +3,21 @@
3
3
  category: "home automation",
4
4
  color: "#e2d96e",
5
5
  defaults: {
6
- name: {value: ""},
6
+ // A marker only a node new from the palette has, as core nodes do: import
7
+ // and paste keep the saved name, so onadd leaves a saved node's settings.
8
+ name: {value: "_DEFAULT_"},
9
+ // Node-RED's MQTT broker node, through which it subscribes to its device's
10
+ // topics and publishes on them itself. The editor offers the first broker
11
+ // when the dialog is opened and saved, not when the node is dropped.
12
+ broker: {value: "", type: "mqtt-broker", required: false, validate: function (v) {
13
+ return (Boolean(v) && v !== "_ADD_") || this.publishesThroughOutput !== false ? true : "choose a broker";
14
+ }},
15
+ // Whether it reaches its device through MQTT in and out nodes, as before
16
+ // 6.2, and so needs no broker until one is chosen. A node saved before
17
+ // this setting loads without it (the editor applies no defaults to the
18
+ // deployed flows), which counts as true; a new node sets false (onadd).
19
+ // No row.
20
+ publishesThroughOutput: {value: false},
7
21
  // No default: it names the user's own device.
8
22
  topicPrefix: {value: "", required: true, validate: function (v) {
9
23
  var prefix = String(v || "").trim().replace(/^\/+|\/+$/g, "");
@@ -13,9 +27,31 @@
13
27
  return /^\d+$/.test(String(v).trim());
14
28
  }}
15
29
  },
30
+ onadd: function () {
31
+ if (this.name !== "_DEFAULT_") return;
32
+ this.name = "";
33
+ this.publishesThroughOutput = false;
34
+ },
35
+ oneditprepare: function () {
36
+ var node = this;
37
+ // While it reaches its device through MQTT in and out nodes, that it does
38
+ // until given a broker.
39
+ function rows() {
40
+ var broker = $("#node-input-broker").val();
41
+ $(".device-through-output").toggle(node.publishesThroughOutput !== false && (!broker || broker === "_ADD_"));
42
+ }
43
+ $("#node-input-broker").on("change", rows);
44
+ rows();
45
+ },
46
+ oneditsave: function () {
47
+ // A broker chosen: it publishes itself from now on.
48
+ var broker = $("#node-input-broker").val();
49
+ if (broker && broker !== "_ADD_") this.publishesThroughOutput = false;
50
+ else if (this.publishesThroughOutput !== false) this.publishesThroughOutput = true;
51
+ },
16
52
  inputs: 1,
17
53
  outputs: 2,
18
- outputLabels: ["to the device (MQTT out)", "to the cover controller"],
54
+ outputLabels: ["to the device (MQTT out, without a broker)", "to the cover controller"],
19
55
  icon: "font-awesome/fa-exchange",
20
56
  label: function () { return this.name || "shelly cover"; },
21
57
  paletteLabel: "shelly cover"
@@ -24,6 +60,11 @@
24
60
 
25
61
  <script type="text/html" data-template-name="home-shelly-cover">
26
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-broker">Broker</label><input type="text" id="node-input-broker"></div>
64
+ <div class="form-tips">With a broker, it subscribes to its device's topics and publishes on them itself: the MQTT in and
65
+ MQTT out nodes around it are no longer needed and can be deleted.</div>
66
+ <div class="form-tips device-through-output">This node reaches its device through MQTT in and out nodes, as before 6.2,
67
+ and keeps working that way. Choose a Broker to let it do so itself.</div>
27
68
  <div class="form-row"><label for="node-input-topicPrefix">MQTT topic prefix</label><input type="text" id="node-input-topicPrefix" placeholder="the device's ID, e.g. shellyplus2pm-a8032ab12345"></div>
28
69
  <div class="form-row"><label for="node-input-channel">Cover ID</label><input type="number" min="0" step="1" id="node-input-channel"></div>
29
70
  </script>
@@ -31,7 +72,11 @@
31
72
  <script type="text/html" data-help-name="home-shelly-cover">
32
73
  <p>Connects a Shelly Gen2 or later device in cover mode to a <b>home cover controller</b>, over the device's own
33
74
  MQTT API: no function nodes, no Home Assistant and no HTTP.</p>
75
+ <p>The devices: <a href="https://shelly-api-docs.shelly.cloud/gen2/" target="_blank">Shelly's Gen2+ device documentation</a>.</p>
34
76
  <h3>Wiring</h3>
77
+ <p>With a <b>Broker</b>, it subscribes to <code>&lt;prefix&gt;/#</code> and publishes on its device's topics itself: leave out the MQTT
78
+ in and MQTT out nodes below (still wired, they change nothing). Its first output then sends nothing. It reads its cover on each connect, as a Status node's startup did through the cover controller. A node saved
79
+ before 6.2 keeps using them until you choose a broker.</p>
35
80
  <ol>
36
81
  <li>An <b>MQTT in</b> node subscribed to <code>&lt;prefix&gt;/#</code> (output: auto-detect) → this node.</li>
37
82
  <li>The cover controller's first output (its device commands) → this node.</li>
@@ -1,13 +1,16 @@
1
1
  "use strict";
2
2
  const shelly_cover_1 = require("../lib/shelly-cover");
3
3
  const adapter_1 = require("./adapter");
4
+ const device_side_1 = require("./device-side");
4
5
  const registerShellyCover = (RED) => {
5
6
  function HomeShellyCover(config) {
6
7
  RED.nodes.createNode(this, config);
7
8
  const node = this;
8
9
  let device;
10
+ let broker;
9
11
  try {
10
12
  device = (0, shelly_cover_1.normaliseShellyCoverConfig)(config);
13
+ broker = (0, device_side_1.deviceBroker)(RED, config.broker);
11
14
  }
12
15
  catch (error) {
13
16
  (0, adapter_1.invalidConfig)(node, error);
@@ -15,17 +18,29 @@ const registerShellyCover = (RED) => {
15
18
  }
16
19
  const trace = { area: "", component: "shelly-cover" };
17
20
  node.status({ fill: "grey", shape: "ring", text: "waiting for the device" });
18
- (0, adapter_1.onInput)(node, (msg, send) => {
21
+ // With a broker, it reads its device on each connect, reconnects
22
+ // included, through its own observe: after a restart nothing reported it
23
+ // while it was away, and the controller acts on the reading as on one it
24
+ // asked for.
25
+ // Its prefix is one device: everything under it.
26
+ const side = (0, device_side_1.deviceSide)(node, broker, device.topicPrefix, [device.topicPrefix + "/#"], trace, (msg, send) => handle(msg, send), () => handle({ cover_command: { operation: "observe" } }, node.send.bind(node)));
27
+ function handle(msg, send) {
19
28
  const result = (0, adapter_1.runReducer)(node, trace, (saved, now) => (0, shelly_cover_1.transitionShellyCover)(saved, msg, device, now));
20
- // MQTT publications for the device on output 1, for an MQTT out node;
21
- // readings for the cover controller on output 2.
29
+ // MQTT publications for the device: to the broker, or on output 1 for
30
+ // an MQTT out node; readings for the cover controller on output 2.
22
31
  const outputs = [
23
- result.publications.length ? result.publications.map((publication) => ({ ...publication })) : null,
32
+ side.publish(result.publications),
24
33
  result.readings.length ? result.readings.map((reading) => ({ payload: undefined, ...reading })) : null
25
34
  ];
26
35
  if (outputs.some((output) => output !== null))
27
36
  send(outputs);
28
37
  node.status({ fill: result.status.calibrated ? "green" : "yellow", shape: "dot", text: result.status.text });
38
+ }
39
+ (0, adapter_1.onInput)(node, (msg, send) => {
40
+ // With a broker, its device's messages come from the broker: an MQTT
41
+ // in or a Status node still wired in changes nothing.
42
+ if (!side.fromBroker(msg))
43
+ handle(msg, send);
29
44
  });
30
45
  }
31
46
  RED.nodes.registerType("home-shelly-cover", HomeShellyCover);