@avantics/node-red-home-automation 6.0.0 → 6.2.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 (75) hide show
  1. package/README.md +276 -48
  2. package/dist/lib/cover-publishing.js +20 -6
  3. package/dist/lib/home-assistant-setting.d.ts +23 -0
  4. package/dist/lib/home-assistant-setting.js +39 -0
  5. package/dist/lib/motion-policy.d.ts +1 -0
  6. package/dist/lib/motion-policy.js +21 -1
  7. package/dist/lib/room-controller.d.ts +4 -0
  8. package/dist/lib/room-controller.js +122 -14
  9. package/dist/lib/room-holds.d.ts +34 -0
  10. package/dist/lib/room-holds.js +66 -0
  11. package/dist/lib/room-publishing.d.ts +1 -0
  12. package/dist/lib/room-publishing.js +11 -0
  13. package/dist/lib/scene-publishing.d.ts +30 -0
  14. package/dist/lib/scene-publishing.js +63 -0
  15. package/dist/lib/scene.d.ts +61 -0
  16. package/dist/lib/scene.js +170 -0
  17. package/dist/lib/shelly-cover.js +5 -0
  18. package/dist/lib/shelly-relay-light.d.ts +24 -0
  19. package/dist/lib/shelly-relay-light.js +93 -0
  20. package/dist/lib/shelly-switch.d.ts +8 -0
  21. package/dist/lib/shelly-switch.js +37 -8
  22. package/dist/lib/theatre-activity-controller.js +4 -0
  23. package/dist/lib/theatre-metrics.js +6 -4
  24. package/dist/lib/theatre-slo.js +3 -0
  25. package/dist/lib/theatre-state-contract.d.ts +4 -2
  26. package/dist/lib/theatre-state-contract.js +51 -8
  27. package/dist/lib/theatre-state.js +3 -0
  28. package/dist/nodes/activity-lighting-controller.html +1 -1
  29. package/dist/nodes/adapter.d.ts +5 -1
  30. package/dist/nodes/adapter.js +35 -2
  31. package/dist/nodes/avr-controller.html +86 -20
  32. package/dist/nodes/avr-controller.js +23 -11
  33. package/dist/nodes/circadian-policy.html +88 -19
  34. package/dist/nodes/circadian-policy.js +30 -11
  35. package/dist/nodes/cover-controller.html +91 -24
  36. package/dist/nodes/cover-controller.js +25 -12
  37. package/dist/nodes/device-side.d.ts +20 -0
  38. package/dist/nodes/device-side.js +72 -0
  39. package/dist/nodes/home-assistant-discovery.d.ts +26 -0
  40. package/dist/nodes/home-assistant-discovery.js +64 -0
  41. package/dist/nodes/house-schedule-mqtt.html +74 -4
  42. package/dist/nodes/house-schedule-mqtt.js +22 -8
  43. package/dist/nodes/motion-policy.html +91 -21
  44. package/dist/nodes/motion-policy.js +44 -11
  45. package/dist/nodes/mqtt-broker-link.d.ts +41 -0
  46. package/dist/nodes/mqtt-broker-link.js +91 -0
  47. package/dist/nodes/occupancy-fusion.html +87 -19
  48. package/dist/nodes/occupancy-fusion.js +23 -11
  49. package/dist/nodes/publishing-side.d.ts +20 -0
  50. package/dist/nodes/publishing-side.js +66 -0
  51. package/dist/nodes/room-controller.html +128 -43
  52. package/dist/nodes/room-controller.js +63 -16
  53. package/dist/nodes/scene.d.ts +3 -0
  54. package/dist/nodes/scene.html +226 -0
  55. package/dist/nodes/scene.js +121 -0
  56. package/dist/nodes/shelly-cover.html +46 -2
  57. package/dist/nodes/shelly-cover.js +19 -4
  58. package/dist/nodes/shelly-switch.html +80 -6
  59. package/dist/nodes/shelly-switch.js +32 -7
  60. package/dist/nodes/theatre-state-contract.html +76 -8
  61. package/dist/nodes/theatre-state-contract.js +33 -14
  62. package/dist/nodes/z2m-cover.html +46 -2
  63. package/dist/nodes/z2m-cover.js +21 -4
  64. package/dist/nodes/z2m-light.html +50 -2
  65. package/dist/nodes/z2m-light.js +26 -6
  66. package/examples/Cover control.json +38 -102
  67. package/examples/Cover pair.json +36 -3
  68. package/examples/Home theatre activity.json +46 -102
  69. package/examples/Home theatre equipment.json +34 -2
  70. package/examples/House schedule.json +58 -80
  71. package/examples/Room lighting.json +45 -55
  72. package/examples/Scene.json +215 -0
  73. package/examples/Shelly cover.json +36 -116
  74. package/examples/Shelly switch.json +52 -148
  75. package/package.json +2 -1
@@ -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,11 +27,36 @@
13
27
  return /^\d+$/.test(String(v).trim());
14
28
  }},
15
29
  // auto: as the device is configured.
16
- mode: {value: "auto"}
30
+ mode: {value: "auto"},
31
+ // The room's id for the light its relay is; empty: not a room light,
32
+ // as before this setting.
33
+ target: {value: ""}
34
+ },
35
+ onadd: function () {
36
+ if (this.name !== "_DEFAULT_") return;
37
+ this.name = "";
38
+ this.publishesThroughOutput = false;
39
+ },
40
+ oneditprepare: function () {
41
+ var node = this;
42
+ // While it reaches its device through MQTT in and out nodes, that it does
43
+ // until given a broker.
44
+ function rows() {
45
+ var broker = $("#node-input-broker").val();
46
+ $(".device-through-output").toggle(node.publishesThroughOutput !== false && (!broker || broker === "_ADD_"));
47
+ }
48
+ $("#node-input-broker").on("change", rows);
49
+ rows();
50
+ },
51
+ oneditsave: function () {
52
+ // A broker chosen: it publishes itself from now on.
53
+ var broker = $("#node-input-broker").val();
54
+ if (broker && broker !== "_ADD_") this.publishesThroughOutput = false;
55
+ else if (this.publishesThroughOutput !== false) this.publishesThroughOutput = true;
17
56
  },
18
57
  inputs: 1,
19
- outputs: 2,
20
- outputLabels: ["to the device (MQTT out)", "to the switch policy (detached), or the relay's state"],
58
+ outputs: 3,
59
+ outputLabels: ["to the device (MQTT out, without a broker)", "to the switch policy (detached), or the relay's state", "to the room lighting controller (with a Target)"],
21
60
  icon: "font-awesome/fa-toggle-on",
22
61
  label: function () { return this.name || "shelly switch"; },
23
62
  paletteLabel: "shelly switch"
@@ -26,6 +65,11 @@
26
65
 
27
66
  <script type="text/html" data-template-name="home-shelly-switch">
28
67
  <div class="form-row"><label for="node-input-name">Name</label><input type="text" id="node-input-name"></div>
68
+ <div class="form-row"><label for="node-input-broker">Broker</label><input type="text" id="node-input-broker"></div>
69
+ <div class="form-tips">With a broker, it subscribes to its device's topics and publishes on them itself: the MQTT in and
70
+ MQTT out nodes around it are no longer needed and can be deleted.</div>
71
+ <div class="form-tips device-through-output">This node reaches its device through MQTT in and out nodes, as before 6.2,
72
+ and keeps working that way. Choose a Broker to let it do so itself.</div>
29
73
  <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. shellyplus1-a8032ab12345"></div>
30
74
  <div class="form-row"><label for="node-input-channel">Switch ID</label><input type="number" min="0" step="1" id="node-input-channel"></div>
31
75
  <div class="form-row">
@@ -36,17 +80,23 @@
36
80
  <option value="relay">Relay: the relay is the light</option>
37
81
  </select>
38
82
  </div>
83
+ <div class="form-row"><label for="node-input-target">Target</label><input type="text" id="node-input-target" placeholder="the room's id for the light the relay is (optional)"></div>
39
84
  </script>
40
85
 
41
86
  <script type="text/html" data-help-name="home-shelly-switch">
42
87
  <p>Connects one switch channel of a Shelly Gen2 or later relay, over the device's own MQTT API, used the way the
43
88
  device is set up: no function nodes, no Home Assistant and no HTTP.</p>
44
89
  <h3>Wiring</h3>
90
+ <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
91
+ in and MQTT out nodes below (still wired, they change nothing). Its first output then sends nothing. A node saved
92
+ before 6.2 keeps using them until you choose a broker.</p>
45
93
  <ol>
46
94
  <li>An <b>MQTT in</b> node subscribed to <code>&lt;prefix&gt;/#</code> (output: auto-detect) → this node.</li>
47
95
  <li>This node's first output → an <b>MQTT out</b> node on the same broker, with no topic set.</li>
48
96
  <li>Detached: this node's second output → a <b>home switch lighting policy</b>, whose intents go to the room
49
97
  controller.</li>
98
+ <li>When the relay is a room light (a <b>Target</b> set): the room controller's second output (its light
99
+ commands) → this node, and this node's third output → the room controller.</li>
50
100
  </ol>
51
101
  <p>On the device, MQTT must be on with <b>Enable MQTT Control</b> and <b>RPC status notifications</b> (both the
52
102
  default).</p>
@@ -58,7 +108,26 @@
58
108
  node per switch.</dd>
59
109
  <dt>Mode</dt><dd><i>As the device is set up</i> (the default) reads the device's own configuration; the other two
60
110
  override it, and are needed when it can't be read.</dd>
111
+ <dt>Target</dt><dd>optional: the room's id for the light the relay is, in the room controller's Targets JSON,
112
+ marked <code>"onOffOnly": true</code>. Set, the node is that light's adapter, in either mode. Empty, the relay
113
+ isn't a room light, as before.</dd>
61
114
  </dl>
115
+ <h3>As a room light</h3>
116
+ <p>With a <b>Target</b>, the relay is that light, whether the device is detached (the wall switch's presses still
117
+ go to the switch policy, and the room switches the relay) or not (the relay follows the switch, and the room
118
+ switches it too). It speaks the room's neutral light contract, like the zigbee2mqtt light:</p>
119
+ <ul>
120
+ <li>The room's <code>msg.light</code> commands for its Target switch the relay: <code>on</code> or
121
+ <code>off</code> on <code>&lt;prefix&gt;/command/switch:&lt;n&gt;</code>. A relay has no brightness or colour,
122
+ so those are left out; mark the target on/off only. Commands for other lights are ignored, so one wire from the
123
+ room can feed every light's node. An <code>observe</code> asks the relay for its status.</li>
124
+ <li>Its readings go to the room on output 3: <code>observed</code> with the relay's output; <code>external</code>
125
+ when the relay changes with no command of this node's pending (a person at a switch the relay follows, the
126
+ Shelly app), or when someone else's <code>on</code>, <code>off</code> or <code>toggle</code> appears on its
127
+ command topic (its own commands' echoes are recognised as its own); and <code>availability</code>, shown,
128
+ never acted on.</li>
129
+ <li>The status shows the light's state, such as <i>detached, switch input · hall on</i>.</li>
130
+ </ul>
62
131
  <h3>What it does</h3>
63
132
  <ul>
64
133
  <li>At startup, and whenever the device comes online, it asks the device for its configuration
@@ -66,17 +135,22 @@
66
135
  receives it) and for its whole status. The node status shows what it found, such as <i>detached, switch
67
136
  input</i>. If nothing answers within 10 seconds, the status says <i>configuration unreadable: set Mode</i>, and
68
137
  nothing is passed on until <b>Mode</b> says how.</li>
69
- <li><b>Detached</b>: the relay stays on, powering smart bulbs, and is never switched from here. A switch-type input's
138
+ <li><b>Detached</b>, without a Target: the relay stays on, powering smart bulbs, and is never switched from here. A switch-type input's
70
139
  on/off becomes <code>msg.source = "switch"</code> with <code>msg.payload = {state}</code> (the first, from the
71
140
  device's status, marked <code>baseline</code>); a button-type input's pushes become
72
141
  <code>msg.source = "switch-action"</code> with <code>single</code>, <code>double</code> or <code>long</code>
73
142
  (the switch policy acts on single and double; long is passed on for later use). The device going offline or online becomes
74
143
  <code>msg.source = "switch-availability"</code>. If the relay reports off, the status warns: the bulbs have lost
75
- power.</li>
144
+ power. With a Target, the relay is the light and the room switches it (see above).</li>
76
145
  <li><b>Relay</b> (follow, flip, momentary and the other modes): the relay is the light. Its state becomes
77
146
  <code>msg.switchState = {kind: "observed", on, person, observedAt}</code>, with <code>person</code> true when
78
147
  the change came from the device's own input; <code>msg.switchCommand = {power: "on" | "off"}</code> switches it.
79
148
  No node of this package reads that state yet.</li>
80
149
  <li>Readings are timed when they arrive.</li>
81
150
  </ul>
151
+ <h3>When MQTT out connects</h3>
152
+ <p>Wire a <b>Status node</b> whose scope is your MQTT out node into this node. Each time MQTT out shows
153
+ "connected", this node takes it as its startup message (<code>msg.shellySwitch = {kind: "startup"}</code>) and reads the device's configuration again:
154
+ Node-RED's MQTT broker node drops what it is given before it connects, so what the node sends as the flows
155
+ start is lost on a first start, a full deploy or a restart. Any other status message is ignored.</p>
82
156
  </script>
@@ -1,13 +1,16 @@
1
1
  "use strict";
2
2
  const shelly_switch_1 = require("../lib/shelly-switch");
3
3
  const adapter_1 = require("./adapter");
4
+ const device_side_1 = require("./device-side");
4
5
  const registerShellySwitch = (RED) => {
5
6
  function HomeShellySwitch(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_switch_1.normaliseShellySwitchConfig)(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,14 +18,30 @@ const registerShellySwitch = (RED) => {
15
18
  }
16
19
  const trace = { area: "", component: "shelly-switch" };
17
20
  let timer;
21
+ // With a broker, it reads the device's configuration each time it
22
+ // connects; without one, once the flows have started (and at a Status
23
+ // node's "connected").
24
+ // Its prefix is one device: everything under it.
25
+ const side = (0, device_side_1.deviceSide)(node, broker, device.topicPrefix, [device.topicPrefix + "/#"], trace, (msg, send) => process(msg, send), () => process({ shellySwitch: { kind: "startup" } }, node.send.bind(node)));
18
26
  function process(msg, send) {
19
- const result = (0, adapter_1.runReducer)(node, trace, (saved, now) => (0, shelly_switch_1.transitionShellySwitch)(saved, msg, device, now));
20
- // MQTT publications for the device on output 1, for an MQTT out node;
27
+ const result = (0, adapter_1.runReducer)(node, trace, (saved, now) => {
28
+ const decided = (0, shelly_switch_1.transitionShellySwitch)(saved, msg, device, now);
29
+ // Another light's command: nothing to keep or trace, and the result
30
+ // still says it was ignored.
31
+ return decided.ignored ? { ...decided, state: (saved ?? decided.state), traces: [] } : decided;
32
+ });
33
+ // Another light's command: nothing to keep, send or show.
34
+ if (result.ignored)
35
+ return;
36
+ // MQTT publications for the device: to the broker, or on output 1 for
37
+ // an MQTT out node;
21
38
  // readings on output 2: for the switch lighting policy when detached,
22
- // the relay's state otherwise.
39
+ // the relay's state otherwise; with a Target, the light's readings for
40
+ // the room on output 3.
23
41
  const outputs = [
24
- result.publications.length ? result.publications.map((publication) => ({ ...publication })) : null,
25
- result.readings.length ? result.readings.map((reading) => ({ payload: undefined, ...reading })) : null
42
+ side.publish(result.publications),
43
+ result.readings.length ? result.readings.map((reading) => ({ payload: undefined, ...reading })) : null,
44
+ result.lightReadings.length ? result.lightReadings.map((reading) => ({ payload: undefined, light: reading })) : null
26
45
  ];
27
46
  if (outputs.some((output) => output !== null))
28
47
  send(outputs);
@@ -35,8 +54,14 @@ const registerShellySwitch = (RED) => {
35
54
  }
36
55
  node.status({ fill: "grey", shape: "ring", text: "reading the device's configuration" });
37
56
  // Once the flows have started, so the MQTT out node is there to send through.
38
- (0, adapter_1.whenFlowsStarted)(RED, node, () => process({ shellySwitch: { kind: "startup" } }, node.send.bind(node)));
39
- (0, adapter_1.onInput)(node, (msg, send) => process(msg, send));
57
+ if (!side.broker)
58
+ (0, adapter_1.whenFlowsStarted)(RED, node, () => process({ shellySwitch: { kind: "startup" } }, node.send.bind(node)));
59
+ (0, adapter_1.onInput)(node, (msg, send) => {
60
+ // With a broker, its device's messages come from the broker: an MQTT
61
+ // in or a Status node still wired in changes nothing.
62
+ if (!side.fromBroker(msg))
63
+ process(msg, send);
64
+ }, side.broker ? undefined : { message: { shellySwitch: { kind: "startup" } }, trace: trace });
40
65
  node.on("close", (_removed, done) => {
41
66
  clearTimeout(timer);
42
67
  done();
@@ -3,6 +3,23 @@
3
3
  // A setting is on unless it was explicitly switched off; null ("not set")
4
4
  // shows as ticked.
5
5
  function on(value) { return value !== false && value !== "false"; }
6
+ // Its Publish choice: it always publishes its state, so state or
7
+ // discovery, from the open dialog or its saved settings; a contract saved
8
+ // before the choice reads its old discovery setting (not set: on).
9
+ function publishChoice(node) {
10
+ var select = $("#node-input-homeAssistant");
11
+ if (select.length) return select.val();
12
+ if (node.homeAssistant === "state" || node.homeAssistant === "discovery") return node.homeAssistant;
13
+ return on(node.discovery) ? "discovery" : "state";
14
+ }
15
+ // Whether it publishes through its output, as before 6.2: a node saved
16
+ // before the Publish choice that published, or one opened and saved since
17
+ // without a broker (publishesThroughOutput). It needs no broker until one
18
+ // is chosen; from saved values only, not the open dialog.
19
+ function throughOutput(node) {
20
+ if (node.publishesThroughOutput === true) return true;
21
+ return node.homeAssistant === null || node.homeAssistant === undefined;
22
+ }
6
23
  RED.nodes.registerType("home-theatre-state-contract", {
7
24
  category: "home automation",
8
25
  color: "#d9c7f1",
@@ -14,8 +31,8 @@
14
31
  // discovery of a contract saved before node IDs; when set, it needs a
15
32
  // letter or digit, from which topics and IDs are made.
16
33
  area: {value: "", validate: function (v) {
17
- var dialog = $("#node-input-discovery").length > 0;
18
- var discovery = dialog ? $("#node-input-discovery").prop("checked") : on(this.discovery);
34
+ var dialog = $("#node-input-homeAssistant").length > 0;
35
+ var discovery = publishChoice(this) === "discovery";
19
36
  var baseTopic = dialog ? $("#node-input-baseTopic").val() : this.baseTopic;
20
37
  var area = String(v || "").trim();
21
38
  // Needed for the default base topic, and for the entities of a
@@ -27,6 +44,20 @@
27
44
  // this setting, which keeps publishing discovery. New nodes start with
28
45
  // it off (onadd).
29
46
  discovery: {value: null},
47
+ // true for a node that publishes through its output, as before 6.2, and
48
+ // was opened and saved without a broker: it stays valid until a broker is
49
+ // chosen. No form row.
50
+ publishesThroughOutput: {value: false},
51
+ // Publish: state or discovery. null means "not set": a contract saved
52
+ // before this setting, read from discovery above, kept with no row.
53
+ homeAssistant: {value: null},
54
+ // Node-RED's MQTT broker node, through which it publishes and reads its
55
+ // requests itself. It always publishes, so it needs one. The editor offers
56
+ // the first broker when the dialog is opened and saved, not when the
57
+ // node is dropped.
58
+ broker: {value: "", type: "mqtt-broker", required: false, validate: function (v) {
59
+ return Boolean(v) && v !== "_ADD_" || throughOutput(this) ? true : "choose a broker";
60
+ }},
30
61
  // How Home Assistant IDs are named. null means "not set": a node saved
31
62
  // before this setting, which keeps its area-named entities. New nodes
32
63
  // name them after the node (onadd), so renaming the area keeps them.
@@ -47,12 +78,25 @@
47
78
  this.name = "";
48
79
  this.idScheme = "node";
49
80
  this.discovery = false;
81
+ this.homeAssistant = "state";
82
+ },
83
+ oneditsave: function () {
84
+ // Through its output until a broker is chosen (read before the
85
+ // dialog's values are applied).
86
+ var broker = $("#node-input-broker").val();
87
+ this.publishesThroughOutput = throughOutput(this) && (!broker || broker === "_ADD_");
50
88
  },
51
89
  oneditprepare: function () {
52
- $("#node-input-discovery").prop("checked", on(this.discovery));
53
- $("#node-input-discovery, #node-input-baseTopic").on("change keyup", function () {
90
+ $("#node-input-homeAssistant").val(publishChoice(this));
91
+ var outputBefore = throughOutput(this);
92
+ function publishRows() {
93
+ var broker = $("#node-input-broker").val();
94
+ $(".publish-through-output").toggle($("#node-input-homeAssistant").val() !== "off" && outputBefore && (!broker || broker === "_ADD_")); $(".publish-discovery").toggle($("#node-input-homeAssistant").val() === "discovery"); }
95
+ $("#node-input-homeAssistant, #node-input-broker, #node-input-baseTopic").on("change keyup", function () {
96
+ publishRows();
54
97
  $("#node-input-area").trigger("change");
55
98
  });
99
+ publishRows();
56
100
  },
57
101
  inputs: 1,
58
102
  outputs: 2,
@@ -68,8 +112,19 @@
68
112
  <div class="form-row"><label for="node-input-area">Area</label><input type="text" id="node-input-area"></div>
69
113
  <div class="form-row"><label for="node-input-displayName">Display name</label><input type="text" id="node-input-displayName"></div>
70
114
  <div class="form-row"><label for="node-input-baseTopic">Base topic</label><input type="text" id="node-input-baseTopic"></div>
71
- <div class="form-row"><label for="node-input-discovery">Home Assistant discovery</label><input type="checkbox" id="node-input-discovery" style="display:inline-block; width:auto;"></div>
72
- <div class="form-row"><label for="node-input-discoveryPrefix">Discovery prefix</label><input type="text" id="node-input-discoveryPrefix"></div>
115
+ <div class="form-row">
116
+ <label for="node-input-homeAssistant">Publish</label>
117
+ <select id="node-input-homeAssistant">
118
+ <option value="state">MQTT state</option>
119
+ <option value="discovery">MQTT state and Home Assistant discovery</option>
120
+ </select>
121
+ </div>
122
+ <div class="form-row"><label for="node-input-broker">Broker</label><input type="text" id="node-input-broker"></div>
123
+ <div class="form-tips">With a broker, it publishes and reads its requests itself: an MQTT out on its publication output, an
124
+ MQTT in on its request topic, and a Status node are no longer needed and can be deleted.</div>
125
+ <div class="form-tips publish-through-output">This node publishes through its output, as before 6.2, and keeps
126
+ working that way. Choose a Broker to let it publish itself.</div>
127
+ <div class="form-row publish-discovery"><label for="node-input-discoveryPrefix">Discovery prefix</label><input type="text" id="node-input-discoveryPrefix"></div>
73
128
  <div class="form-row"><label for="node-input-activities">Activities JSON</label><input type="text" id="node-input-activities"></div>
74
129
  <div class="form-row"><label for="node-input-sloWindowSize">SLO window</label><input type="number" id="node-input-sloWindowSize"></div>
75
130
  <div class="form-row"><label for="node-input-sloMinimumSamples">Minimum samples</label><input type="number" id="node-input-sloMinimumSamples"></div>
@@ -79,7 +134,7 @@
79
134
 
80
135
  <script type="text/html" data-help-name="home-theatre-state-contract">
81
136
  <p>Bridges non-retained MQTT activity requests to the theatre controller and publishes retained controller truth.</p>
82
- <p>Requests arrive on <code>&lt;base&gt;/request</code> with a request ID and activity. Retained requests are inert. Complete <code>msg.theatre</code> state snapshots publish desired, current, phase, transition (started and completed times and degraded targets, shown as the
137
+ <p>Requests arrive on <code>&lt;base&gt;/request</code> with a request ID and activity. Retained requests are inert. Complete <code>msg.theatre</code> state snapshots publish, each topic only when it changes beyond its times (a steady activity publishes nothing new), desired, current, phase, transition (started and completed times and degraded targets, shown as the
83
138
  Phase sensor's attributes in Home Assistant), failure, readiness, operational SLO and per-target health topics. </p>
84
139
  <p>With discovery, a contract added since node IDs suggests its <b>Area</b> to Home Assistant for its device, and
85
140
  Home Assistant creates an area of that name if none exists. With no area, its entity IDs come from the
@@ -88,11 +143,24 @@
88
143
  <code>offline</code> as it stops; its Home Assistant entities follow it. These get through only
89
144
  when this node is deployed on its own, so set the MQTT broker node's Birth, Close and Will messages to the same (see the
90
145
  README).</p>
91
- <p>With <b>Home Assistant discovery</b> on, it publishes discovery for the activity selector and status sensors once
146
+ <p>With <b>Publish</b> set to <i>MQTT state and Home Assistant discovery</i>, it publishes discovery for the activity selector and status sensors once
92
147
  the flows start, before <code>online</code>, and again at a startup event. Switched off, the next startup event
93
148
  clears them, so Home Assistant removes them. New nodes start with it off; a node saved before the setting existed
94
149
  keeps it on.</p>
95
150
  <p>The <b>Area</b> names the discovery entities and the default base topic <code>home/&lt;area&gt;/theatre</code>,
96
151
  so it is needed when discovery is on or the base topic is empty. With discovery off and a base topic set, it is
97
152
  optional.</p>
153
+ <h3>Broker</h3>
154
+ <p>Choose Node-RED's <b>MQTT broker</b> node, and it publishes and reads its request topic itself: it announces everything each time the
155
+ broker connects, reconnects included, and each time Home Assistant comes back (<code>online</code> on
156
+ <code>&lt;prefix&gt;/status</code>), so its entities come back by themselves.
157
+ Each connect also runs its startup message, as a Status node's "connected" did without a broker. Its publication output then sends
158
+ nothing, and an MQTT out on it, an MQTT in on its request topic and a Status node are no longer needed: still wired, they change nothing.
159
+ Opening and saving the node offers the first broker in your flows. A node saved before 6.2 that publishes keeps
160
+ publishing on its output, as below, and needs no broker until you choose one.</p>
161
+ <h3>When MQTT out connects</h3>
162
+ <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
163
+ "connected", this node takes it as its startup message (<code>msg.theatreStateContract = {kind: "startup"}</code>) and announces itself again:
164
+ Node-RED's MQTT broker node drops what it is given before it connects, so what the node sends as the flows
165
+ start is lost on a first start, a full deploy or a restart. Any other status message is ignored.</p>
98
166
  </script>
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
  const theatre_state_contract_1 = require("../lib/theatre-state-contract");
3
- const publishing_lifecycle_1 = require("./publishing-lifecycle");
3
+ const publishing_side_1 = require("./publishing-side");
4
+ const home_assistant_setting_1 = require("../lib/home-assistant-setting");
5
+ const theatre_ha_discovery_1 = require("../lib/theatre-ha-discovery");
4
6
  const adapter_1 = require("./adapter");
5
7
  const settings_1 = require("../lib/settings");
6
8
  const ha_discovery_1 = require("../lib/ha-discovery");
@@ -12,7 +14,9 @@ const registerTheatreStateContract = (RED) => {
12
14
  // says "area is required" when it is.
13
15
  const area = (0, adapter_1.optionalArea)(config);
14
16
  let controllerConfig;
17
+ let broker;
15
18
  try {
19
+ const choice = (0, settings_1.optionalChoiceSetting)(config.homeAssistant, "Publish", home_assistant_setting_1.HOME_ASSISTANT_CHOICES);
16
20
  const activities = (0, settings_1.jsonSetting)(config.activities, "activities", []);
17
21
  controllerConfig = (0, theatre_state_contract_1.normaliseStateContractConfig)({
18
22
  nodeId: node.id,
@@ -24,9 +28,11 @@ const registerTheatreStateContract = (RED) => {
24
28
  // area, and an empty base topic is home/<area>/theatre.
25
29
  displayName: config.displayName || area,
26
30
  baseTopic: config.baseTopic || "",
27
- // Not set (null, or absent from a flow saved before the setting)
28
- // means on, so an older flow keeps publishing discovery.
29
- discovery: (0, settings_1.optionalBooleanSetting)(config.discovery, "Home Assistant discovery") ?? true,
31
+ // Publish (MQTT state, or MQTT state and Home Assistant discovery)
32
+ // replaced the discovery setting. Neither set (null, or absent from
33
+ // a flow saved before the setting) means on, so an older flow keeps
34
+ // publishing discovery.
35
+ discovery: choice ? choice === "discovery" : (0, settings_1.optionalBooleanSetting)(config.discovery, "Home Assistant discovery") ?? true,
30
36
  discoveryPrefix: (0, ha_discovery_1.discoveryPrefixSetting)(config.discoveryPrefix),
31
37
  activities: (Array.isArray(activities) ? activities.map(String) : activities),
32
38
  slo: {
@@ -36,31 +42,38 @@ const registerTheatreStateContract = (RED) => {
36
42
  successObjectivePct: (0, settings_1.numberSetting)(config.sloSuccessObjectivePct, "success objective", 95)
37
43
  }
38
44
  });
45
+ broker = (0, publishing_side_1.brokerSetting)(RED, config.broker, true);
39
46
  }
40
47
  catch (error) {
41
48
  (0, adapter_1.invalidConfig)(node, error);
42
49
  return;
43
50
  }
51
+ const trace = { area: controllerConfig.area, component: "theatre-state-contract" };
44
52
  // Available, and clear of what it no longer publishes, as soon as it
45
- // runs; offline as it stops; nothing left behind when deleted.
46
- const lifecycle = (0, publishing_lifecycle_1.publishingLifecycle)(RED, node, {
53
+ // can be (with a broker, on each connect; without one, on output 2 once
54
+ // the flows start); offline as it stops; nothing left behind when
55
+ // deleted. With a broker it reads its requests itself.
56
+ const lifecycle = (0, publishing_side_1.publishingSide)(RED, node, broker, {
47
57
  retainedTopics: () => (0, theatre_state_contract_1.theatreRetainedTopics)(controllerConfig),
48
58
  starting: () => (0, theatre_state_contract_1.theatreStartingPublications)(controllerConfig),
49
59
  stopping: () => (0, theatre_state_contract_1.theatreStoppingPublications)(controllerConfig),
50
60
  output: 1,
51
- outputs: 2
52
- });
61
+ outputs: 2,
62
+ startupMessage: { theatreStateContract: { kind: "startup" } },
63
+ statusTopic: controllerConfig.discovery ? controllerConfig.discoveryPrefix + "/status" : null,
64
+ setTopics: [(0, theatre_ha_discovery_1.theatreTopic)(controllerConfig.baseTopic, "request")]
65
+ }, trace, (msg, send) => handle(msg, send));
53
66
  function process(msg, send) {
54
- const result = (0, adapter_1.runReducer)(node, { area: controllerConfig.area, component: "theatre-state-contract" }, (saved, now) => (0, theatre_state_contract_1.transitionTheatreStateContract)(saved, msg, controllerConfig, now));
67
+ const result = (0, adapter_1.runReducer)(node, trace, (saved, now) => (0, theatre_state_contract_1.transitionTheatreStateContract)(saved, msg, controllerConfig, now));
55
68
  if (result.requests.length) {
56
69
  send([
57
70
  result.requests.map((request) => ({ payload: undefined, ...request })),
58
71
  null
59
72
  ]);
60
73
  }
61
- if (result.publications.length) {
62
- send([null, result.publications.map((entry) => ({ ...entry }))]);
63
- }
74
+ // With a broker, its publications go to it, and output 2 is quiet.
75
+ if (result.publications.length)
76
+ lifecycle.publish(send, result.publications);
64
77
  node.status({
65
78
  fill: result.status.phase === "published" ? "green" : result.status.phase === "requested" ? "blue" : "grey",
66
79
  shape: result.status.phase === "idle" ? "ring" : "dot",
@@ -68,7 +81,7 @@ const registerTheatreStateContract = (RED) => {
68
81
  });
69
82
  return result;
70
83
  }
71
- (0, adapter_1.onInput)(node, (msg, send) => {
84
+ function handle(msg, send) {
72
85
  // The startup's own announcement comes from the contract's transition.
73
86
  if ((0, adapter_1.objectField)(msg, "theatreStateContract")?.kind === "startup") {
74
87
  lifecycle.startup(send);
@@ -80,7 +93,13 @@ const registerTheatreStateContract = (RED) => {
80
93
  theatre: (0, adapter_1.objectField)(msg, "theatre"),
81
94
  theatreStateContract: (0, adapter_1.objectField)(msg, "theatreStateContract")
82
95
  }, send);
83
- });
96
+ }
97
+ (0, adapter_1.onInput)(node, (msg, send) => {
98
+ // With a broker, its requests come from the broker: an MQTT in or a
99
+ // Status node still wired in changes nothing.
100
+ if (!lifecycle.fromBroker(msg))
101
+ handle(msg, send);
102
+ }, lifecycle.startupOption({ theatreStateContract: { kind: "startup" } }));
84
103
  // Available again as soon as this node runs, as after a redeploy of it
85
104
  // alone, rather than waiting for the flow's startup message.
86
105
  }
@@ -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
  baseTopic: {value: "zigbee2mqtt", required: true, validate: function (v) {
8
22
  var base = String(v || "").trim().replace(/^\/+|\/+$/g, "");
9
23
  return base !== "" && !/[+#]/.test(base);
@@ -14,9 +28,31 @@
14
28
  return name !== "" && !/[+#]/.test(name);
15
29
  }}
16
30
  },
31
+ onadd: function () {
32
+ if (this.name !== "_DEFAULT_") return;
33
+ this.name = "";
34
+ this.publishesThroughOutput = false;
35
+ },
36
+ oneditprepare: function () {
37
+ var node = this;
38
+ // While it reaches its device through MQTT in and out nodes, that it does
39
+ // until given a broker.
40
+ function rows() {
41
+ var broker = $("#node-input-broker").val();
42
+ $(".device-through-output").toggle(node.publishesThroughOutput !== false && (!broker || broker === "_ADD_"));
43
+ }
44
+ $("#node-input-broker").on("change", rows);
45
+ rows();
46
+ },
47
+ oneditsave: function () {
48
+ // A broker chosen: it publishes itself from now on.
49
+ var broker = $("#node-input-broker").val();
50
+ if (broker && broker !== "_ADD_") this.publishesThroughOutput = false;
51
+ else if (this.publishesThroughOutput !== false) this.publishesThroughOutput = true;
52
+ },
17
53
  inputs: 1,
18
54
  outputs: 2,
19
- outputLabels: ["to the device (MQTT out)", "to the cover controller"],
55
+ outputLabels: ["to the device (MQTT out, without a broker)", "to the cover controller"],
20
56
  icon: "font-awesome/fa-exchange",
21
57
  label: function () { return this.name || this.friendlyName || "zigbee2mqtt cover"; },
22
58
  paletteLabel: "zigbee2mqtt cover"
@@ -25,6 +61,11 @@
25
61
 
26
62
  <script type="text/html" data-template-name="home-z2m-cover">
27
63
  <div class="form-row"><label for="node-input-name">Name</label><input type="text" id="node-input-name"></div>
64
+ <div class="form-row"><label for="node-input-broker">Broker</label><input type="text" id="node-input-broker"></div>
65
+ <div class="form-tips">With a broker, it subscribes to its device's topics and publishes on them itself: the MQTT in and
66
+ MQTT out nodes around it are no longer needed and can be deleted.</div>
67
+ <div class="form-tips device-through-output">This node reaches its device through MQTT in and out nodes, as before 6.2,
68
+ and keeps working that way. Choose a Broker to let it do so itself.</div>
28
69
  <div class="form-row"><label for="node-input-baseTopic">Base topic</label><input type="text" id="node-input-baseTopic"></div>
29
70
  <div class="form-row"><label for="node-input-friendlyName">Friendly name</label><input type="text" id="node-input-friendlyName" placeholder="the device's name in Zigbee2MQTT"></div>
30
71
  </script>
@@ -33,6 +74,9 @@
33
74
  <p>Connects a Zigbee2MQTT cover (a blind, shutter or curtain motor) to a <b>home cover controller</b>, over
34
75
  Zigbee2MQTT's MQTT messages: no function nodes, and never Home Assistant.</p>
35
76
  <h3>Wiring</h3>
77
+ <p>With a <b>Broker</b>, it subscribes to <code>&lt;base&gt;/&lt;friendly name&gt;</code> and its <code>/availability</code> (exactly these: a friendly name can contain <code>/</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>
36
80
  <ol>
37
81
  <li>An <b>MQTT in</b> node subscribed to <code>&lt;base&gt;/&lt;friendly name&gt;</code> and another to
38
82
  <code>&lt;base&gt;/&lt;friendly name&gt;/availability</code> (output: auto-detect) → this node.</li>
@@ -1,13 +1,16 @@
1
1
  "use strict";
2
2
  const z2m_cover_1 = require("../lib/z2m-cover");
3
3
  const adapter_1 = require("./adapter");
4
+ const device_side_1 = require("./device-side");
4
5
  const registerZ2mCover = (RED) => {
5
6
  function HomeZ2mCover(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, z2m_cover_1.normaliseZ2mCoverConfig)(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,12 +18,20 @@ const registerZ2mCover = (RED) => {
15
18
  }
16
19
  const trace = { area: "", component: "z2m-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
+ // Exactly its own topics: a friendly name can contain "/", so <name>/#
26
+ // would take in other devices' too.
27
+ const topics = (0, z2m_cover_1.z2mCoverTopics)(device);
28
+ const side = (0, device_side_1.deviceSide)(node, broker, topics.device, [topics.device, topics.availability], trace, (msg, send) => handle(msg, send), () => handle({ cover_command: { operation: "observe" } }, node.send.bind(node)));
29
+ function handle(msg, send) {
19
30
  const result = (0, adapter_1.runReducer)(node, trace, (saved, now) => (0, z2m_cover_1.transitionZ2mCover)(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.
31
+ // MQTT publications for the device: to the broker, or on output 1 for
32
+ // an MQTT out node; readings for the cover controller on output 2.
22
33
  const outputs = [
23
- result.publications.length ? result.publications.map((publication) => ({ ...publication })) : null,
34
+ side.publish(result.publications),
24
35
  result.readings.length ? result.readings.map((reading) => ({ payload: undefined, ...reading })) : null
25
36
  ];
26
37
  if (outputs.some((output) => output !== null))
@@ -33,6 +44,12 @@ const registerZ2mCover = (RED) => {
33
44
  const where = last.position !== undefined ? `${last.position}%` : last.state ?? "position unknown";
34
45
  node.status({ fill: "green", shape: "dot", text: last.motion && last.motion !== "stopped" ? `${last.motion} · ${where}` : where });
35
46
  }
47
+ }
48
+ (0, adapter_1.onInput)(node, (msg, send) => {
49
+ // With a broker, its device's messages come from the broker: an MQTT
50
+ // in or a Status node still wired in changes nothing.
51
+ if (!side.fromBroker(msg))
52
+ handle(msg, send);
36
53
  });
37
54
  }
38
55
  RED.nodes.registerType("home-z2m-cover", HomeZ2mCover);