@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
@@ -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,24 @@
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>
89
+ <p>The devices: <a href="https://shelly-api-docs.shelly.cloud/gen2/" target="_blank">Shelly's Gen2+ device documentation</a>.</p>
44
90
  <h3>Wiring</h3>
91
+ <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
92
+ in and MQTT out nodes below (still wired, they change nothing). Its first output then sends nothing. A node saved
93
+ before 6.2 keeps using them until you choose a broker.</p>
45
94
  <ol>
46
95
  <li>An <b>MQTT in</b> node subscribed to <code>&lt;prefix&gt;/#</code> (output: auto-detect) → this node.</li>
47
96
  <li>This node's first output → an <b>MQTT out</b> node on the same broker, with no topic set.</li>
48
97
  <li>Detached: this node's second output → a <b>home switch lighting policy</b>, whose intents go to the room
49
98
  controller.</li>
99
+ <li>When the relay is a room light (a <b>Target</b> set): the room controller's second output (its light
100
+ commands) → this node, and this node's third output → the room controller.</li>
50
101
  </ol>
51
102
  <p>On the device, MQTT must be on with <b>Enable MQTT Control</b> and <b>RPC status notifications</b> (both the
52
103
  default).</p>
@@ -58,7 +109,26 @@
58
109
  node per switch.</dd>
59
110
  <dt>Mode</dt><dd><i>As the device is set up</i> (the default) reads the device's own configuration; the other two
60
111
  override it, and are needed when it can't be read.</dd>
112
+ <dt>Target</dt><dd>optional: the room's id for the light the relay is, in the room controller's Targets JSON,
113
+ marked <code>"onOffOnly": true</code>. Set, the node is that light's adapter, in either mode. Empty, the relay
114
+ isn't a room light, as before.</dd>
61
115
  </dl>
116
+ <h3>As a room light</h3>
117
+ <p>With a <b>Target</b>, the relay is that light, whether the device is detached (the wall switch's presses still
118
+ go to the switch policy, and the room switches the relay) or not (the relay follows the switch, and the room
119
+ switches it too). It speaks the room's neutral light contract, like the zigbee2mqtt light:</p>
120
+ <ul>
121
+ <li>The room's <code>msg.light</code> commands for its Target switch the relay: <code>on</code> or
122
+ <code>off</code> on <code>&lt;prefix&gt;/command/switch:&lt;n&gt;</code>. A relay has no brightness or colour,
123
+ so those are left out; mark the target on/off only. Commands for other lights are ignored, so one wire from the
124
+ room can feed every light's node. An <code>observe</code> asks the relay for its status.</li>
125
+ <li>Its readings go to the room on output 3: <code>observed</code> with the relay's output; <code>external</code>
126
+ when the relay changes with no command of this node's pending (a person at a switch the relay follows, the
127
+ Shelly app), or when someone else's <code>on</code>, <code>off</code> or <code>toggle</code> appears on its
128
+ command topic (its own commands' echoes are recognised as its own); and <code>availability</code>, shown,
129
+ never acted on.</li>
130
+ <li>The status shows the light's state, such as <i>detached, switch input · hall on</i>.</li>
131
+ </ul>
62
132
  <h3>What it does</h3>
63
133
  <ul>
64
134
  <li>At startup, and whenever the device comes online, it asks the device for its configuration
@@ -66,13 +136,13 @@
66
136
  receives it) and for its whole status. The node status shows what it found, such as <i>detached, switch
67
137
  input</i>. If nothing answers within 10 seconds, the status says <i>configuration unreadable: set Mode</i>, and
68
138
  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
139
+ <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
140
  on/off becomes <code>msg.source = "switch"</code> with <code>msg.payload = {state}</code> (the first, from the
71
141
  device's status, marked <code>baseline</code>); a button-type input's pushes become
72
142
  <code>msg.source = "switch-action"</code> with <code>single</code>, <code>double</code> or <code>long</code>
73
143
  (the switch policy acts on single and double; long is passed on for later use). The device going offline or online becomes
74
144
  <code>msg.source = "switch-availability"</code>. If the relay reports off, the status warns: the bulbs have lost
75
- power.</li>
145
+ power. With a Target, the relay is the light and the room switches it (see above).</li>
76
146
  <li><b>Relay</b> (follow, flip, momentary and the other modes): the relay is the light. Its state becomes
77
147
  <code>msg.switchState = {kind: "observed", on, person, observedAt}</code>, with <code>person</code> true when
78
148
  the change came from the device's own input; <code>msg.switchCommand = {power: "on" | "off"}</code> switches it.
@@ -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), { message: { shellySwitch: { kind: "startup" } }, trace: trace });
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();
@@ -48,8 +48,9 @@ const registerSourceCapabilityController = (RED) => {
48
48
  null
49
49
  ]);
50
50
  }
51
- if (result.events.length) {
52
- send([null, result.events.map((event) => ({ payload: undefined, ...event }))]);
51
+ const theatre = [...result.events, ...result.observations];
52
+ if (theatre.length) {
53
+ send([null, theatre.map((event) => ({ payload: undefined, ...event }))]);
53
54
  }
54
55
  updateStatus(result.status);
55
56
  schedule(result.state);
@@ -27,6 +27,8 @@
27
27
 
28
28
  <script type="text/html" data-help-name="home-theatre-activity-controller">
29
29
  <p>Owns latest-request-wins whole-theatre activity state and aggregates generation-scoped target evidence.</p>
30
- <p>Input <code>msg.theatre</code> accepts activity intent, target-state, startup, refresh, recovery and tick events. Messages without <code>msg.theatre</code>, such as a target controller's own events on the same wire, are ignored. Output 1 emits semantic <code>msg.theatreCommand</code> messages for target owners. Output 2 emits activity events and a complete state snapshot. Device transports remain separate.</p>
31
- <p>Startup without persisted intent probes physical state. One fresh profile match is adopted without commands. With no evidence, or more than one match, the activity is unknown and nothing is commanded until the next request, so a redeploy or restart never switches a running activity off. Manual overrides are held without corrective commands and adopted only when the physical state uniquely matches a profile.</p>
30
+ <p>Input <code>msg.theatre</code> accepts activity intent, target-state, target-observation, startup, refresh, recovery and tick events. Messages without <code>msg.theatre</code>, such as a target controller's own events on the same wire, are ignored. Output 1 emits semantic <code>msg.theatreCommand</code> messages for target owners. Output 2 emits activity events and a complete state snapshot. Device transports remain separate.</p>
31
+ <p>Startup without persisted intent probes physical state: the display, AVR, source and backlighting controllers answer
32
+ with <code>msg.theatre = {kind: "target_observation", target, observedAt, values}</code>, what their devices show, on the
33
+ wires that carry their target states. One fresh profile match is adopted without commands. With no evidence, or more than one match, the activity is unknown and nothing is commanded until the next request, so a redeploy or restart never switches a running activity off. Manual overrides are held without corrective commands and adopted only when the physical state uniquely matches a profile.</p>
32
34
  </script>
@@ -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,15 +143,23 @@
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>
98
161
  <h3>When MQTT out connects</h3>
99
- <p>Wire a <b>Status node</b> whose scope is your MQTT out node into this node. Each time MQTT out shows
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
100
163
  "connected", this node takes it as its startup message (<code>msg.theatreStateContract = {kind: "startup"}</code>) and announces itself again:
101
164
  Node-RED's MQTT broker node drops what it is given before it connects, so what the node sends as the flows
102
165
  start is lost on a first start, a full deploy or a restart. Any other status message is ignored.</p>
@@ -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
- }, { message: { theatreStateContract: { kind: "startup" } }, trace: { area: controllerConfig.area, component: "theatre-state-contract" } });
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
  }
@@ -0,0 +1,3 @@
1
+ import type { NodeInitializer } from "node-red";
2
+ declare const registerWled: NodeInitializer;
3
+ export = registerWled;
@@ -0,0 +1,56 @@
1
+ <script type="text/javascript">
2
+ RED.nodes.registerType("home-wled", {
3
+ category: "home automation",
4
+ color: "#e2d96e",
5
+ defaults: {
6
+ name: {value: ""},
7
+ // Node-RED's MQTT broker node, through which it subscribes to WLED's
8
+ // topics and publishes its commands. The editor offers the first broker
9
+ // when the dialog is opened and saved.
10
+ broker: {value: "", type: "mqtt-broker", required: true},
11
+ // No default: it names the user's own device.
12
+ deviceTopic: {value: "", required: true, validate: function (v) {
13
+ var topic = String(v || "").trim().replace(/^\/+|\/+$/g, "");
14
+ return topic !== "" && !/[+#]/.test(topic);
15
+ }}
16
+ },
17
+ inputs: 1,
18
+ outputs: 1,
19
+ outputLabels: ["to the backlighting controller"],
20
+ icon: "font-awesome/fa-lightbulb-o",
21
+ label: function () { return this.name || this.deviceTopic || "wled"; },
22
+ paletteLabel: "wled"
23
+ });
24
+ </script>
25
+
26
+ <script type="text/html" data-template-name="home-wled">
27
+ <div class="form-row"><label for="node-input-name">Name</label><input type="text" id="node-input-name"></div>
28
+ <div class="form-row"><label for="node-input-broker">Broker</label><input type="text" id="node-input-broker"></div>
29
+ <div class="form-row"><label for="node-input-deviceTopic">Device topic</label><input type="text" id="node-input-deviceTopic" placeholder="WLED's Device Topic, as in its Sync settings"></div>
30
+ <div class="form-tips">It subscribes to the device topic's <code>/g</code> and <code>/status</code> and publishes WLED's
31
+ <code>ON</code> and <code>OFF</code> itself: no MQTT in or out nodes.</div>
32
+ </script>
33
+
34
+ <script type="text/html" data-help-name="home-wled">
35
+ <p>Connects a WLED light strip to a <b>home backlighting controller</b>, over WLED's own MQTT messages: no function
36
+ nodes, and never Home Assistant.</p>
37
+ <p>The device: <a href="https://kno.wled.ge/" target="_blank">WLED</a>.</p>
38
+ <h3>Wiring</h3>
39
+ <ol>
40
+ <li>The backlighting controller's first output (its device commands) → this node.</li>
41
+ <li>This node's output → the backlighting controller.</li>
42
+ </ol>
43
+ <p>Choose the <b>Broker</b> WLED uses, and set <b>Device topic</b> to WLED's own (Config → Sync Interfaces → MQTT; WLED's
44
+ default is <code>wled/</code> and its MAC address). It subscribes to <code>&lt;device topic&gt;/g</code> (brightness) and
45
+ <code>&lt;device topic&gt;/status</code> (online or offline), and publishes <code>ON</code> or <code>OFF</code> on the
46
+ device topic.</p>
47
+ <h3>Readings</h3>
48
+ <p>Each brightness WLED reports becomes <code>msg.backlighting = {kind: "observed", generation, power, observedAt}</code>
49
+ for the command in hand: brightness 0 is off, anything else on. WLED reports every change, from any source, and
50
+ doesn't say who made it: the backlighting controller judges a reading it didn't ask for as a manual change.
51
+ An <code>observe</code> is answered from the last report while WLED is online; with none since Node-RED started
52
+ there is nothing to answer until WLED next changes: WLED publishes only when it changes, and can't be asked over
53
+ MQTT (seen on the firmware we tested).</p>
54
+ <p>A <code>set_power</code> is answered with <code>command_result</code> ok once published, or not ok with reason
55
+ <code>unavailable</code> while WLED is offline.</p>
56
+ </script>
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ const wled_1 = require("../lib/wled");
3
+ const adapter_1 = require("./adapter");
4
+ const device_side_1 = require("./device-side");
5
+ const registerWled = (RED) => {
6
+ function HomeWled(config) {
7
+ RED.nodes.createNode(this, config);
8
+ const node = this;
9
+ let device;
10
+ let broker;
11
+ try {
12
+ device = (0, wled_1.normaliseWledConfig)(config);
13
+ // Newer than the MQTT in and out way of wiring adapters: it needs its
14
+ // broker.
15
+ broker = (0, device_side_1.deviceBroker)(RED, config.broker);
16
+ if (!broker)
17
+ throw new Error("choose its MQTT broker");
18
+ }
19
+ catch (error) {
20
+ (0, adapter_1.invalidConfig)(node, error);
21
+ return;
22
+ }
23
+ const trace = { area: "", component: "wled" };
24
+ node.status({ fill: "grey", shape: "ring", text: "waiting for the device" });
25
+ // Exactly its own topics: its brightness and its availability. Its
26
+ // device topic is where its commands go.
27
+ const topics = (0, wled_1.wledTopics)(device);
28
+ const side = (0, device_side_1.deviceSide)(node, broker, topics.device, [topics.brightness, topics.status], trace, (msg, send) => handle(msg, send));
29
+ function handle(msg, send) {
30
+ const result = (0, adapter_1.runReducer)(node, trace, (saved, now) => (0, wled_1.transitionWled)(saved, msg, device, now));
31
+ // Its commands to WLED through the broker; readings for the
32
+ // backlighting controller on its output.
33
+ side.publish(result.publications);
34
+ // One output: its messages together on it, as [[...]].
35
+ if (result.readings.length)
36
+ send([result.readings.map((reading) => ({ payload: undefined, ...reading }))]);
37
+ const state = result.state;
38
+ if (state.online === false)
39
+ node.status({ fill: "grey", shape: "ring", text: "offline" });
40
+ else if (state.power)
41
+ node.status({ fill: state.power === "on" ? "green" : "grey", shape: "dot", text: state.power });
42
+ }
43
+ (0, adapter_1.onInput)(node, (msg, send) => {
44
+ // WLED's messages come from the broker: an MQTT in node wired in
45
+ // changes nothing.
46
+ if (!side.fromBroker(msg))
47
+ handle(msg, send);
48
+ });
49
+ }
50
+ RED.nodes.registerType("home-wled", HomeWled);
51
+ };
52
+ module.exports = registerWled;
53
+ //# sourceMappingURL=wled.js.map