@avantics/node-red-home-automation 6.1.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 (70) hide show
  1. package/README.md +204 -42
  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 +1 -0
  8. package/dist/lib/room-controller.js +5 -0
  9. package/dist/lib/room-publishing.d.ts +1 -0
  10. package/dist/lib/room-publishing.js +11 -0
  11. package/dist/lib/scene-publishing.d.ts +30 -0
  12. package/dist/lib/scene-publishing.js +63 -0
  13. package/dist/lib/scene.d.ts +61 -0
  14. package/dist/lib/scene.js +170 -0
  15. package/dist/lib/shelly-cover.js +5 -0
  16. package/dist/lib/shelly-relay-light.d.ts +24 -0
  17. package/dist/lib/shelly-relay-light.js +93 -0
  18. package/dist/lib/shelly-switch.d.ts +8 -0
  19. package/dist/lib/shelly-switch.js +37 -8
  20. package/dist/lib/theatre-activity-controller.js +4 -0
  21. package/dist/lib/theatre-metrics.js +6 -4
  22. package/dist/lib/theatre-slo.js +3 -0
  23. package/dist/lib/theatre-state-contract.d.ts +4 -2
  24. package/dist/lib/theatre-state-contract.js +51 -8
  25. package/dist/lib/theatre-state.js +3 -0
  26. package/dist/nodes/avr-controller.html +82 -21
  27. package/dist/nodes/avr-controller.js +23 -11
  28. package/dist/nodes/circadian-policy.html +84 -20
  29. package/dist/nodes/circadian-policy.js +30 -11
  30. package/dist/nodes/cover-controller.html +87 -25
  31. package/dist/nodes/cover-controller.js +25 -12
  32. package/dist/nodes/device-side.d.ts +20 -0
  33. package/dist/nodes/device-side.js +72 -0
  34. package/dist/nodes/home-assistant-discovery.d.ts +26 -0
  35. package/dist/nodes/home-assistant-discovery.js +64 -0
  36. package/dist/nodes/house-schedule-mqtt.html +70 -5
  37. package/dist/nodes/house-schedule-mqtt.js +22 -8
  38. package/dist/nodes/motion-policy.html +87 -22
  39. package/dist/nodes/motion-policy.js +44 -11
  40. package/dist/nodes/mqtt-broker-link.d.ts +41 -0
  41. package/dist/nodes/mqtt-broker-link.js +91 -0
  42. package/dist/nodes/occupancy-fusion.html +83 -20
  43. package/dist/nodes/occupancy-fusion.js +23 -11
  44. package/dist/nodes/publishing-side.d.ts +20 -0
  45. package/dist/nodes/publishing-side.js +66 -0
  46. package/dist/nodes/room-controller.html +106 -35
  47. package/dist/nodes/room-controller.js +63 -16
  48. package/dist/nodes/scene.d.ts +3 -0
  49. package/dist/nodes/scene.html +226 -0
  50. package/dist/nodes/scene.js +121 -0
  51. package/dist/nodes/shelly-cover.html +46 -2
  52. package/dist/nodes/shelly-cover.js +19 -4
  53. package/dist/nodes/shelly-switch.html +75 -6
  54. package/dist/nodes/shelly-switch.js +32 -7
  55. package/dist/nodes/theatre-state-contract.html +72 -9
  56. package/dist/nodes/theatre-state-contract.js +33 -14
  57. package/dist/nodes/z2m-cover.html +46 -2
  58. package/dist/nodes/z2m-cover.js +21 -4
  59. package/dist/nodes/z2m-light.html +46 -2
  60. package/dist/nodes/z2m-light.js +23 -4
  61. package/examples/Cover control.json +38 -91
  62. package/examples/Cover pair.json +36 -3
  63. package/examples/Home theatre activity.json +46 -91
  64. package/examples/Home theatre equipment.json +34 -2
  65. package/examples/House schedule.json +58 -80
  66. package/examples/Room lighting.json +45 -44
  67. package/examples/Scene.json +215 -0
  68. package/examples/Shelly cover.json +36 -105
  69. package/examples/Shelly switch.json +52 -148
  70. package/package.json +2 -1
@@ -0,0 +1,91 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MQTT_CONNECTED = void 0;
4
+ exports.brokerLink = brokerLink;
5
+ // The one place that touches Node-RED's MQTT broker configuration node
6
+ // (mqtt-broker), so a node can publish and subscribe itself rather than
7
+ // through MQTT in and out nodes. None of this is the broker node's
8
+ // documented API, though Node-RED's own MQTT in and out nodes use exactly
9
+ // these calls (Node-RED 5.0.10, @node-red/nodes/core/network/10-mqtt.js):
10
+ // - register(user) adds the user, and connects with the first one; on every
11
+ // connect, connecting and disconnect the broker calls user.status() with
12
+ // "node-red:common.status.connected" and the like (:381-420, :739). So the
13
+ // link registers a small proxy rather than its node, whose own status the
14
+ // broker would otherwise overwrite, and turns "connected" into its
15
+ // onConnected callbacks. A user registered while the broker is already
16
+ // connected gets no call, so the link checks `connected` itself.
17
+ // - deregister(user, done, autoDisconnect) (:752), as MQTT in and out pass
18
+ // it: `removed`.
19
+ // - subscribe(topic, options, callback, ref) (:987) and unsubscribe(topic,
20
+ // ref, remove) (:1101): the callback gets (topic, payload Buffer, packet),
21
+ // and the broker subscribes again by itself after a reconnect.
22
+ // - publish(msg, done) (:1149) drops anything sent while disconnected, with
23
+ // no error. So the link never queues: callers announce again in
24
+ // onConnected, and what is current is what is sent.
25
+ exports.MQTT_CONNECTED = "node-red:common.status.connected";
26
+ function brokerLink(broker, ownerId, onError = () => { }) {
27
+ const callbacks = [];
28
+ const topics = new Set();
29
+ const ref = ownerId + "-home-automation";
30
+ let closed = false;
31
+ const run = () => {
32
+ for (const callback of callbacks) {
33
+ try {
34
+ callback();
35
+ }
36
+ catch (error) {
37
+ onError(error);
38
+ }
39
+ }
40
+ };
41
+ // Stands in for the node: only status() is ever called on it.
42
+ const proxy = {
43
+ id: ref,
44
+ status(status) {
45
+ if (!closed && status && status.text === exports.MQTT_CONNECTED)
46
+ run();
47
+ }
48
+ };
49
+ broker.register(proxy);
50
+ return {
51
+ connected: () => broker.connected === true,
52
+ onConnected(callback) {
53
+ callbacks.push(callback);
54
+ if (broker.connected === true) {
55
+ setImmediate(() => {
56
+ if (closed)
57
+ return;
58
+ try {
59
+ callback();
60
+ }
61
+ catch (error) {
62
+ onError(error);
63
+ }
64
+ });
65
+ }
66
+ },
67
+ publish(topic, payload, { retain = false, qos = 0 } = {}) {
68
+ if (closed)
69
+ return;
70
+ broker.publish({ topic, payload, qos, retain }, (error) => {
71
+ if (error)
72
+ onError(error);
73
+ });
74
+ },
75
+ subscribe(topic, callback) {
76
+ topics.add(topic);
77
+ broker.subscribe(topic, { qos: 0 }, (received, payload, packet) => {
78
+ if (closed)
79
+ return;
80
+ callback({ topic: received, payload: Buffer.isBuffer(payload) ? payload.toString("utf8") : String(payload ?? ""), retain: packet?.retain === true });
81
+ }, ref);
82
+ },
83
+ close(removed, done) {
84
+ closed = true;
85
+ for (const topic of topics)
86
+ broker.unsubscribe(topic, ref, removed);
87
+ broker.deregister(proxy, done, removed);
88
+ }
89
+ };
90
+ }
91
+ //# sourceMappingURL=mqtt-broker-link.js.map
@@ -1,6 +1,31 @@
1
1
  <script type="text/javascript">
2
2
  (function () {
3
3
  function on(value) { return value === true || value === "true"; }
4
+ // Its Publish choice (off, state or discovery), from the open dialog or
5
+ // its saved settings: a node saved before the choice reads its two old
6
+ // settings (lib/home-assistant-setting.ts).
7
+ function publishChoice(node) {
8
+ function on(value) { return value === true || value === "true"; }
9
+ var select = $("#node-input-homeAssistant");
10
+ if (select.length) return select.val();
11
+ if (node.homeAssistant === "off" || node.homeAssistant === "state" || node.homeAssistant === "discovery") return node.homeAssistant;
12
+ return !on(node.publishState) ? "off" : on(node.discovery) ? "discovery" : "state";
13
+ }
14
+ // Whether it publishes through its output, as before 6.2: a node saved
15
+ // before the Publish choice that published, or one opened and saved since
16
+ // without a broker (publishesThroughOutput). It needs no broker until one
17
+ // is chosen; from saved values only, not the open dialog.
18
+ function throughOutput(node) {
19
+ if (node.publishesThroughOutput === true) return true;
20
+ return (node.homeAssistant === null || node.homeAssistant === undefined) && (node.publishState === true || node.publishState === "true");
21
+ }
22
+ // Its area is needed while it publishes, and for a node saved before the
23
+ // choice whenever either old setting was on, as the runtime checks.
24
+ function needsArea(node) {
25
+ function on(value) { return value === true || value === "true"; }
26
+ var saved = !$("#node-input-homeAssistant").length && (node.homeAssistant === null || node.homeAssistant === undefined);
27
+ return publishChoice(node) !== "off" || (saved && (on(node.publishState) || on(node.discovery)));
28
+ }
4
29
  RED.nodes.registerType("home-occupancy-fusion", {
5
30
  category: "home automation",
6
31
  color: "#c7e9c0",
@@ -10,14 +35,25 @@
10
35
  name: {value: "_DEFAULT_"},
11
36
  // Required only when the node publishes, and then with a letter or
12
37
  // digit, from which its topics and IDs are made.
13
- area: {value: "", validate: function (v) {
14
- var publishing = $("#node-input-publishState").length ? $("#node-input-publishState").prop("checked") : on(this.publishState);
15
- return !publishing || /[a-z0-9]/i.test(String(v || ""));
16
- }},
38
+ area: {value: "", validate: function (v) { return !needsArea(this) || /[a-z0-9]/i.test(String(v || "")); }},
17
39
  presenceMode: {value: "none", required: true},
18
40
  // Occupancy published nothing before these settings, so both start off.
41
+ // Publish: off, state or discovery. null means "not set": a node saved
42
+ // before this setting, read from the two below, kept with no rows.
43
+ homeAssistant: {value: null},
44
+ // Node-RED's MQTT broker node, through which it publishes and reads its set
45
+ // topics itself. Needed unless Publish is "Don't publish". The editor offers
46
+ // the first broker when the dialog is opened and saved, not when the node
47
+ // is dropped.
48
+ broker: {value: "", type: "mqtt-broker", required: false, validate: function (v) {
49
+ return publishChoice(this) === "off" || (Boolean(v) && v !== "_ADD_") || throughOutput(this) ? true : "choose a broker, or set Publish to Don't publish";
50
+ }},
19
51
  publishState: {value: false},
20
52
  discovery: {value: false},
53
+ // true for a node that publishes through its output, as before 6.2, and
54
+ // was opened and saved without a broker: it stays valid until a broker is
55
+ // chosen. No form row.
56
+ publishesThroughOutput: {value: false},
21
57
  discoveryPrefix: {value: "homeassistant", required: true, validate: function (v) { var prefix = String(v || "").trim().replace(/^\/+|\/+$/g, ""); return Boolean(prefix) && !/[+#]/.test(prefix); }},
22
58
  // How Home Assistant IDs are named; occupancy has no older entities,
23
59
  // so not set also means "node".
@@ -28,18 +64,26 @@
28
64
  this.name = "";
29
65
  this.idScheme = "node";
30
66
  },
67
+ oneditsave: function () {
68
+ // Through its output until a broker is chosen (read before the
69
+ // dialog's values are applied).
70
+ var broker = $("#node-input-broker").val();
71
+ this.publishesThroughOutput = throughOutput(this) && (!broker || broker === "_ADD_");
72
+ },
31
73
  oneditprepare: function () {
32
- $("#node-input-publishState").prop("checked", on(this.publishState));
33
- $("#node-input-discovery").prop("checked", on(this.discovery));
34
- // Discovery describes the published state, so it needs it.
35
- function follow() {
36
- var publishing = $("#node-input-publishState").prop("checked");
37
- if (!publishing) $("#node-input-discovery").prop("checked", false);
38
- $("#node-input-discovery").prop("disabled", !publishing);
39
- $("#node-input-area").trigger("change");
74
+ $("#node-input-homeAssistant").val(publishChoice(this));
75
+ // The broker only while it publishes, the prefix only with discovery.
76
+ var outputBefore = throughOutput(this);
77
+ function publishRows() {
78
+ var broker = $("#node-input-broker").val();
79
+ $(".publish-through-output").toggle($("#node-input-homeAssistant").val() !== "off" && outputBefore && (!broker || broker === "_ADD_"));
80
+ var choice = $("#node-input-homeAssistant").val();
81
+ $(".publish-broker").toggle(choice !== "off");
82
+ $(".publish-discovery").toggle(choice === "discovery");
83
+ $("#node-input-area, #node-input-broker").trigger("change");
40
84
  }
41
- $("#node-input-publishState, #node-input-discovery").on("change", follow);
42
- follow();
85
+ $("#node-input-homeAssistant, #node-input-broker").on("change", publishRows);
86
+ publishRows();
43
87
  },
44
88
  inputs: 1,
45
89
  outputs: 2,
@@ -60,9 +104,20 @@
60
104
  <option value="hold">Hold occupancy (PIR + mmWave)</option>
61
105
  </select>
62
106
  </div>
63
- <div class="form-row"><label for="node-input-publishState">Publish state over MQTT</label><input type="checkbox" id="node-input-publishState" style="display:inline-block; width:auto;"></div>
64
- <div class="form-row"><label for="node-input-discovery">Home Assistant discovery</label><input type="checkbox" id="node-input-discovery" style="display:inline-block; width:auto;"> <span>(needs Publish state)</span></div>
65
- <div class="form-row"><label for="node-input-discoveryPrefix">Discovery prefix</label><input type="text" id="node-input-discoveryPrefix"></div>
107
+ <div class="form-row">
108
+ <label for="node-input-homeAssistant">Publish</label>
109
+ <select id="node-input-homeAssistant">
110
+ <option value="off">Don't publish</option>
111
+ <option value="state">MQTT state</option>
112
+ <option value="discovery">MQTT state and Home Assistant discovery</option>
113
+ </select>
114
+ </div>
115
+ <div class="form-row publish-broker"><label for="node-input-broker">Broker</label><input type="text" id="node-input-broker"></div>
116
+ <div class="form-tips publish-broker">With a broker, it publishes and reads its set topics itself: an MQTT out on its
117
+ publication output, an MQTT in on its set topics, and a Status node are no longer needed and can be deleted.</div>
118
+ <div class="form-tips publish-through-output">This node publishes through its output, as before 6.2, and keeps
119
+ working that way. Choose a Broker to let it publish itself.</div>
120
+ <div class="form-row publish-discovery"><label for="node-input-discoveryPrefix">Discovery prefix</label><input type="text" id="node-input-discoveryPrefix"></div>
66
121
  </script>
67
122
 
68
123
  <script type="text/html" data-help-name="home-occupancy-fusion">
@@ -72,10 +127,10 @@
72
127
  <p>Off unless switched on. Both are named after the <b>Area</b>, which is needed then; topics use it in lowercase,
73
128
  with spaces and other characters as <code>_</code>.</p>
74
129
  <dl class="message-properties">
75
- <dt>Publish state over MQTT</dt><dd>the occupancy, retained <code>ON</code> or <code>OFF</code>, on
130
+ <dt>Publish: MQTT state</dt><dd>the occupancy, retained <code>ON</code> or <code>OFF</code>, on
76
131
  <code>home/&lt;area&gt;/occupancy/state</code> whenever it changes, and <code>online</code> or
77
132
  <code>offline</code> on <code>home/&lt;area&gt;/occupancy/availability</code>.</dd>
78
- <dt>Home Assistant discovery</dt><dd>an occupancy binary sensor reading that state. It needs <b>Publish state</b>.</dd>
133
+ <dt>Publish: MQTT state and Home Assistant discovery</dt><dd>an occupancy binary sensor reading that state. It includes the MQTT state, which it reads.</dd>
79
134
  </dl>
80
135
  <p>These go on the second output; wire it to an MQTT out node. The node announces itself once the flows have
81
136
  started, and again when it receives <code>msg.source = "startup"</code>.</p>
@@ -84,8 +139,16 @@
84
139
  <li>Occupancy events, for the motion lighting policy.</li>
85
140
  <li>MQTT publications, when publishing is on.</li>
86
141
  </ol>
142
+ <h3>Broker</h3>
143
+ <p>Choose Node-RED's <b>MQTT broker</b> node, and it publishes itself: it announces everything each time the
144
+ broker connects, reconnects included, and each time Home Assistant comes back (<code>online</code> on
145
+ <code>&lt;prefix&gt;/status</code>), so its entities come back by themselves.
146
+ Each connect also runs its startup message, as a Status node's "connected" did without a broker. Its publication output then sends
147
+ nothing, and an MQTT out on it and a Status node are no longer needed: still wired, they change nothing.
148
+ Opening and saving the node offers the first broker in your flows. A node saved before 6.2 that publishes keeps
149
+ publishing on its output, as below, and needs no broker until you choose one.</p>
87
150
  <h3>When MQTT out connects</h3>
88
- <p>Wire a <b>Status node</b> whose scope is your MQTT out node into this node. Each time MQTT out shows
151
+ <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
89
152
  "connected", this node takes it as its startup message (<code>msg.source = "startup"</code>) and announces itself again:
90
153
  Node-RED's MQTT broker node drops what it is given before it connects, so what the node sends as the flows
91
154
  start is lost on a first start, a full deploy or a restart. Any other status message is ignored.</p>
@@ -6,13 +6,15 @@ const settings_1 = require("../lib/settings");
6
6
  const topic_names_1 = require("../lib/topic-names");
7
7
  const adapter_1 = require("./adapter");
8
8
  const publishing_node_1 = require("../lib/publishing-node");
9
- const publishing_lifecycle_1 = require("./publishing-lifecycle");
9
+ const publishing_side_1 = require("./publishing-side");
10
+ const home_assistant_setting_1 = require("../lib/home-assistant-setting");
10
11
  const registerOccupancyFusion = (RED) => {
11
12
  function HomeOccupancyFusion(config) {
12
13
  RED.nodes.createNode(this, config);
13
14
  const node = this;
14
15
  let fusionConfig;
15
16
  let publishing;
17
+ let broker;
16
18
  try {
17
19
  // The editor offers "none" (PIR only) and "hold" (PIR plus presence).
18
20
  const presence = (0, settings_1.textSetting)(config.presenceMode, "presence sensor", "none");
@@ -22,14 +24,14 @@ const registerOccupancyFusion = (RED) => {
22
24
  fusionConfig = { area, presenceMode: presence === "hold" ? "hold" : "ignore" };
23
25
  // Off unless switched on, as occupancy published nothing before; there
24
26
  // are no older entities to keep, so not set names them after the node.
25
- const publishState = (0, settings_1.optionalBooleanSetting)(config.publishState, "publish state") ?? false;
26
- const discovery = (0, settings_1.optionalBooleanSetting)(config.discovery, "Home Assistant discovery") ?? false;
27
- if (publishState || discovery)
27
+ const publish = (0, home_assistant_setting_1.publishingChoice)(config, { publishState: false, discovery: false });
28
+ if (publish.needsArea)
28
29
  (0, topic_names_1.publishingArea)(area, true);
30
+ broker = (0, publishing_side_1.brokerSetting)(RED, config.broker, publish.publishState);
29
31
  publishing = {
30
32
  area,
31
- publishState,
32
- discovery,
33
+ publishState: publish.publishState,
34
+ discovery: publish.discovery,
33
35
  discoveryPrefix: (0, ha_discovery_1.discoveryPrefixSetting)(config.discoveryPrefix),
34
36
  nodeId: node.id,
35
37
  idScheme: (0, settings_1.optionalChoiceSetting)(config.idScheme, "ID scheme", ha_discovery_1.ID_SCHEMES) ?? "node"
@@ -58,15 +60,20 @@ const registerOccupancyFusion = (RED) => {
58
60
  // events the motion policy reads on the first. Available, and clear of
59
61
  // what it no longer publishes, as soon as it runs; offline as it stops;
60
62
  // nothing left behind when deleted.
61
- const lifecycle = (0, publishing_lifecycle_1.publishingLifecycle)(RED, node, {
63
+ const trace = { area: fusionConfig.area, component: "occupancy-fusion" };
64
+ // No set topics: it only publishes.
65
+ const lifecycle = (0, publishing_side_1.publishingSide)(RED, node, broker, {
62
66
  retainedTopics: () => (0, occupancy_publishing_1.occupancyRetainedTopics)(publishing),
63
67
  possible: { owner: publishing, topics: () => (0, publishing_node_1.publishesState)(publishing) ? (0, occupancy_publishing_1.occupancyRetainedTopics)({ ...publishing, publishState: true, discovery: true }) : [] },
64
68
  starting: () => (0, occupancy_publishing_1.occupancyStartingOutputs)(publishing, occupied()),
65
69
  stopping: () => (0, occupancy_publishing_1.occupancyStoppingOutputs)(publishing),
66
70
  output: 1,
67
- outputs: 2
68
- });
69
- (0, adapter_1.onInput)(node, (msg, send) => {
71
+ outputs: 2,
72
+ startupMessage: { source: "startup" },
73
+ statusTopic: publishing.discovery ? publishing.discoveryPrefix + "/status" : null,
74
+ setTopics: []
75
+ }, trace, (msg, send) => handle(msg, send));
76
+ function handle(msg, send) {
70
77
  if (msg.source === "startup") {
71
78
  lifecycle.startup(send, (0, occupancy_publishing_1.occupancyStartingOutputs)(publishing, occupied()));
72
79
  return;
@@ -81,7 +88,12 @@ const registerOccupancyFusion = (RED) => {
81
88
  lifecycle.publish(send, (0, occupancy_publishing_1.occupancyStateOutputs)(publishing, output.payload.occupied));
82
89
  }
83
90
  updateStatus(result.status);
84
- }, { message: { source: "startup" }, trace: { area: fusionConfig.area, component: "occupancy-fusion" } });
91
+ }
92
+ (0, adapter_1.onInput)(node, (msg, send) => {
93
+ // With a broker, a Status node still wired in changes nothing.
94
+ if (!lifecycle.fromBroker(msg))
95
+ handle(msg, send);
96
+ }, lifecycle.startupOption({ source: "startup" }));
85
97
  }
86
98
  RED.nodes.registerType("home-occupancy-fusion", HomeOccupancyFusion);
87
99
  };
@@ -0,0 +1,20 @@
1
+ import type { Node, NodeAPI } from "node-red";
2
+ import { type TraceMetadata } from "../lib/bounded-trace";
3
+ import { type InputMessage, type Send } from "./adapter";
4
+ import { type BrokerNode } from "./mqtt-broker-link";
5
+ import { type PublishingLifecycle, type PublishingNode } from "./publishing-lifecycle";
6
+ export interface PublishingSideParts extends PublishingNode {
7
+ statusTopic: string | null;
8
+ setTopics: string[];
9
+ startupMessage: Record<string, unknown>;
10
+ }
11
+ export interface PublishingSide extends PublishingLifecycle {
12
+ broker: boolean;
13
+ fromBroker(msg: InputMessage): boolean;
14
+ startupOption(message: Record<string, unknown>): {
15
+ message: Record<string, unknown>;
16
+ trace: TraceMetadata;
17
+ } | undefined;
18
+ }
19
+ export declare function brokerSetting(RED: NodeAPI, value: unknown, publishing: boolean): BrokerNode | null;
20
+ export declare function publishingSide(RED: NodeAPI, node: Node, broker: BrokerNode | null, parts: PublishingSideParts, trace: TraceMetadata, handle: (msg: InputMessage, send: Send) => void): PublishingSide;
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.brokerSetting = brokerSetting;
4
+ exports.publishingSide = publishingSide;
5
+ const bounded_trace_1 = require("../lib/bounded-trace");
6
+ const adapter_1 = require("./adapter");
7
+ const home_assistant_discovery_1 = require("./home-assistant-discovery");
8
+ const mqtt_broker_link_1 = require("./mqtt-broker-link");
9
+ const publishing_lifecycle_1 = require("./publishing-lifecycle");
10
+ // The Broker setting: the mqtt-broker node, or null without one (as before
11
+ // 6.2). Only read while the node publishes.
12
+ function brokerSetting(RED, value, publishing) {
13
+ const id = typeof value === "string" ? value.trim() : "";
14
+ if (!publishing || !id)
15
+ return null;
16
+ const broker = RED.nodes.getNode(id);
17
+ if (!broker)
18
+ throw new Error("its MQTT broker node isn't in the flows: choose one, or set Publish to Don't publish");
19
+ return broker;
20
+ }
21
+ function publishingSide(RED, node, broker, parts, trace,
22
+ // The node's own input handling: a message on one of its set topics from
23
+ // the broker, as if from an MQTT in node, and its startup message on each
24
+ // connect.
25
+ handle) {
26
+ if (!broker) {
27
+ const lifecycle = (0, publishing_lifecycle_1.publishingLifecycle)(RED, node, parts);
28
+ return {
29
+ ...lifecycle,
30
+ broker: false,
31
+ fromBroker: () => false,
32
+ startupOption: (message) => ({ message, trace })
33
+ };
34
+ }
35
+ const homeAssistant = (0, home_assistant_discovery_1.homeAssistantDiscovery)(RED, node, (0, mqtt_broker_link_1.brokerLink)(broker, node.id, (error) => node.error(error)), {
36
+ retainedTopics: parts.retainedTopics,
37
+ possible: parts.possible,
38
+ announcement: parts.starting,
39
+ stopping: parts.stopping,
40
+ statusTopic: parts.statusTopic,
41
+ setTopics: parts.setTopics,
42
+ onSet: (message) => handle({ topic: message.topic, payload: message.payload, retain: false }, node.send.bind(node)),
43
+ onConnected: () => handle(structuredClone(parts.startupMessage), node.send.bind(node))
44
+ }, trace);
45
+ return {
46
+ broker: true,
47
+ startup(_send, announce = []) {
48
+ homeAssistant.publish(announce);
49
+ },
50
+ publish(_send, publications) {
51
+ homeAssistant.publish(publications);
52
+ },
53
+ fromBroker(msg) {
54
+ const status = (0, adapter_1.objectField)(msg, "status");
55
+ const statusNode = Boolean(status && status.source && typeof status.source === "object");
56
+ const setTopic = typeof msg.topic === "string" && parts.setTopics.includes(msg.topic);
57
+ if (!statusNode && !setTopic)
58
+ return false;
59
+ (0, bounded_trace_1.appendTrace)(node.context(), trace, [{ event: "input", outcome: "ignored", reason: "has-broker",
60
+ details: statusNode ? { status: String(status.text ?? "") } : { topic: msg.topic } }]);
61
+ return true;
62
+ },
63
+ startupOption: () => undefined
64
+ };
65
+ }
66
+ //# sourceMappingURL=publishing-side.js.map
@@ -3,6 +3,24 @@
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 Home Assistant choice, from the open dialog or its saved settings: a
7
+ // room saved before the choice reads its two old settings, where not set
8
+ // means on (lib/home-assistant-setting.ts).
9
+ function choiceOf(node) {
10
+ var select = $("#node-input-homeAssistant");
11
+ if (select.length) return select.val();
12
+ if (node.homeAssistant === "off" || node.homeAssistant === "state" || node.homeAssistant === "discovery") return node.homeAssistant;
13
+ return !on(node.publishState) ? "off" : on(node.discovery) ? "discovery" : "state";
14
+ }
15
+ // Whether it publishes through its output, as before 6.2: a room saved
16
+ // before the Publish choice that published, or one opened and saved since
17
+ // without a broker (publishesThroughOutput). It needs no broker until
18
+ // one is chosen; from saved values only, not the open dialog.
19
+ function throughOutput(node) {
20
+ if (node.publishesThroughOutput === true) return true;
21
+ var saved = node.homeAssistant === null || node.homeAssistant === undefined;
22
+ return saved && on(node.publishState);
23
+ }
6
24
  // Each old setting, and the circadian lighting policy field it goes into.
7
25
  var MOVED = {
8
26
  houseSchedule: "Schedule", wakeTime: "Wake time", bedTime: "Bed time",
@@ -89,16 +107,31 @@
89
107
  // Required only when the room publishes something named after it, and
90
108
  // then with a letter or digit, from which its topics and IDs are made.
91
109
  area: {value: "", validate: function (v) {
92
- var publishing = $("#node-input-publishState").length
93
- ? $("#node-input-publishState").prop("checked") || $("#node-input-discovery").prop("checked")
94
- : on(this.publishState) || on(this.discovery);
95
- return !publishing || /[a-z0-9]/i.test(String(v || ""));
110
+ // A room saved before the choice needed one whenever either old
111
+ // setting was on, as the runtime still checks.
112
+ var legacy = !$("#node-input-homeAssistant").length && (this.homeAssistant === null || this.homeAssistant === undefined) &&
113
+ (on(this.publishState) || on(this.discovery));
114
+ return (choiceOf(this) === "off" && !legacy) || /[a-z0-9]/i.test(String(v || ""));
96
115
  }},
97
- // null means "not set": Node-RED fills it into a saved flow from before
98
- // these settings, which keeps publishing. New nodes start with both
99
- // off (onadd).
116
+ // off, state or discovery. null means "not set": a room saved before
117
+ // this setting, read from the two below. New nodes start off (onadd).
118
+ homeAssistant: {value: null},
119
+ // Node-RED's MQTT broker node, through which it publishes itself.
120
+ // Needed unless Publish is "Don't publish". The editor offers the first
121
+ // broker when the dialog is opened and saved, not when the node is
122
+ // dropped.
123
+ broker: {value: "", type: "mqtt-broker", required: false, validate: function (v) {
124
+ return choiceOf(this) === "off" || (Boolean(v) && v !== "_ADD_") || throughOutput(this) ? true : "choose a broker, or set Publish to Don't publish";
125
+ }},
126
+ // The two settings the choice replaced, kept with no form rows so the
127
+ // editor carries a saved room's: null means "not set", which kept
128
+ // publishing (on) before the choice.
100
129
  publishState: {value: null},
101
130
  discovery: {value: null},
131
+ // true for a room that publishes through its output, as before 6.2,
132
+ // and was opened and saved without a broker: it stays valid until a
133
+ // broker is chosen. No form row.
134
+ publishesThroughOutput: {value: false},
102
135
  // How Home Assistant IDs are named. null means "not set": a node saved
103
136
  // before this setting, which keeps its area-named entities. New nodes
104
137
  // name them after the node (onadd), so renaming the area keeps them.
@@ -129,21 +162,26 @@
129
162
  if (this.name !== "_DEFAULT_") return;
130
163
  this.name = "";
131
164
  this.idScheme = "node";
165
+ this.homeAssistant = "off";
132
166
  this.publishState = false;
133
167
  this.discovery = false;
134
168
  },
135
169
  oneditprepare: function () {
136
170
  var node = this;
137
- $("#node-input-publishState").prop("checked", on(node.publishState));
138
- $("#node-input-discovery").prop("checked", on(node.discovery));
139
- // Discovery describes the published state, so it needs it.
171
+ $("#node-input-homeAssistant").val(choiceOf(node));
172
+ // The broker only for publishing, the prefix only for discovery; for a
173
+ // room that publishes through its output, that it does until given a
174
+ // broker.
175
+ var outputBefore = throughOutput(node);
140
176
  function follow() {
141
- var publishing = $("#node-input-publishState").prop("checked");
142
- if (!publishing) $("#node-input-discovery").prop("checked", false);
143
- $("#node-input-discovery").prop("disabled", !publishing);
144
- $("#node-input-area").trigger("change");
177
+ var choice = $("#node-input-homeAssistant").val();
178
+ var broker = $("#node-input-broker").val();
179
+ $(".room-broker").toggle(choice !== "off");
180
+ $(".room-through-output").toggle(choice !== "off" && outputBefore && (!broker || broker === "_ADD_"));
181
+ $(".room-discovery").toggle(choice === "discovery");
182
+ $("#node-input-area, #node-input-broker").trigger("change");
145
183
  }
146
- $("#node-input-publishState, #node-input-discovery").on("change", follow);
184
+ $("#node-input-homeAssistant, #node-input-broker").on("change", follow);
147
185
  follow();
148
186
  // A room saved before 6.0: its circadian settings, to copy into a
149
187
  // circadian lighting policy.
@@ -175,6 +213,14 @@
175
213
  // Saving the dialog migrates the room: its old circadian settings go.
176
214
  oneditsave: function () {
177
215
  var node = this;
216
+ // The two settings it replaced, kept in step.
217
+ var choice = $("#node-input-homeAssistant").val();
218
+ // Through its output until a broker is chosen (read before the
219
+ // dialog's values are applied).
220
+ var broker = $("#node-input-broker").val();
221
+ node.publishesThroughOutput = throughOutput(node) && (!broker || broker === "_ADD_");
222
+ node.publishState = choice !== "off";
223
+ node.discovery = choice === "discovery";
178
224
  Object.keys(MOVED).forEach(function (key) { node[key] = ""; });
179
225
  // Its targets become ids: their topics are the lights' adapters' now.
180
226
  var targets = parsedTargets($("#node-input-targets").val());
@@ -211,10 +257,21 @@
211
257
  </div>
212
258
  <div class="form-row"><label for="node-input-name">Name</label><input type="text" id="node-input-name"></div>
213
259
  <div class="form-row"><label for="node-input-area">Area</label><input type="text" id="node-input-area" placeholder="needed when publishing"></div>
214
- <div class="form-row"><label for="node-input-publishState">Publish state over MQTT</label><input type="checkbox" id="node-input-publishState" style="display:inline-block; width:auto;"></div>
215
- <div class="form-row"><label for="node-input-discovery">Home Assistant discovery</label><input type="checkbox" id="node-input-discovery" style="display:inline-block; width:auto;"> <span>(needs Publish state)</span></div>
260
+ <div class="form-row">
261
+ <label for="node-input-homeAssistant">Publish</label>
262
+ <select id="node-input-homeAssistant">
263
+ <option value="off">Don't publish</option>
264
+ <option value="state">MQTT state</option>
265
+ <option value="discovery">MQTT state and Home Assistant discovery</option>
266
+ </select>
267
+ </div>
268
+ <div class="form-row room-broker"><label for="node-input-broker">Broker</label><input type="text" id="node-input-broker"></div>
269
+ <div class="form-tips room-broker">With a broker, the room publishes itself: an MQTT out on its first output, and a
270
+ Status node, are no longer needed and can be deleted.</div>
271
+ <div class="form-tips room-through-output">This room publishes through its first output, as before 6.2, and keeps
272
+ working that way. Choose a Broker to let it publish itself.</div>
216
273
  <div class="form-row"><label for="node-input-displayName">Display name</label><input type="text" id="node-input-displayName"></div>
217
- <div class="form-row"><label for="node-input-discoveryPrefix">Discovery prefix</label><input type="text" id="node-input-discoveryPrefix"></div>
274
+ <div class="form-row room-discovery"><label for="node-input-discoveryPrefix">Discovery prefix</label><input type="text" id="node-input-discoveryPrefix"></div>
218
275
  <div class="form-row"><label for="node-input-targets">Targets JSON</label><input type="text" id="node-input-targets"></div>
219
276
  <div class="form-row"><label for="node-input-initialTransitionSeconds">Turn-on transition seconds</label><input type="number" id="node-input-initialTransitionSeconds"></div>
220
277
  </script>
@@ -243,25 +300,34 @@
243
300
  nothing. For a light that isn't Zigbee2MQTT's, any flow that answers <code>msg.light</code> commands with
244
301
  <code>msg.light</code> readings works in the adapter's place (see the README's neutral light contract).</p>
245
302
  <h3>Publishing</h3>
246
- <p>Two settings add what it publishes
247
- for other systems to see; both are named after the <b>Area</b>, which is needed only when one of them is on. Topics
248
- and IDs use it in lowercase, with spaces and other characters (anything but letters, digits, <code>_</code> and
249
- <code>-</code>) as <code>_</code>: <i>Living Room</i> publishes under <code>home/living_room/…</code>.</p>
303
+ <p><b>Publish</b> adds what it publishes for other systems to see, named after the <b>Area</b>, which is
304
+ needed unless it is <i>Don't publish</i>. Topics and IDs use it in lowercase, with spaces and other characters (anything but
305
+ letters, digits, <code>_</code> and <code>-</code>) as <code>_</code>: <i>Living Room</i> publishes under
306
+ <code>home/living_room/…</code>.</p>
250
307
  <dl class="message-properties">
251
- <dt>Publish state over MQTT</dt><dd>each light's control state on
252
- <code>home/&lt;area&gt;/lighting/&lt;target&gt;/control</code>, and a retained <code>online</code> on
253
- <code>home/&lt;area&gt;/lighting/availability</code> once the flows start and <code>offline</code> as it stops.
254
- These get through only when this node is deployed on its own, so set the MQTT broker node's Birth, Close and
255
- Will messages to the same (see the README).</dd>
256
- <dt>Home Assistant discovery</dt><dd>a control sensor for each light. It needs <b>Publish state</b>. The entities
257
- are announced once the flows start, before <code>online</code>, and again at the startup message. Switching it
258
- off removes them from Home Assistant at the next startup message. At every start the room also clears the
259
- circadian, sleep, wake and bed entities it published before 6.0; the circadian lighting policy publishes its
260
- own.</dd>
308
+ <dt>Publish</dt><dd><i>Don't publish</i>; <i>MQTT state</i>, plain MQTT for any system to read: each light's control state on
309
+ <code>home/&lt;area&gt;/lighting/&lt;target&gt;/control</code>, and the room's availability on
310
+ <code>home/&lt;area&gt;/lighting/availability</code>; <i>MQTT state and Home Assistant discovery</i>: that, and
311
+ a control sensor for each light announced to Home Assistant, which reads that state. At every start the room also clears the circadian, sleep, wake and bed entities it published before 6.0.
312
+ A room saved before this setting reads its two old ones (Publish state over MQTT, Home Assistant discovery):
313
+ off/off is Don't publish, on/off MQTT state, on/on MQTT state and Home Assistant discovery, and not set at all
314
+ keeps publishing both.</dd>
315
+ <dt>Broker</dt><dd>Node-RED's MQTT broker node. With one, the room publishes itself: each time the broker connects,
316
+ reconnects included, and each time Home Assistant comes back (<code>online</code> on
317
+ <code>&lt;prefix&gt;/status</code>), it clears what it no longer publishes and announces its entities,
318
+ availability and every light's control state, so they come back by themselves. Each connect also runs its
319
+ startup, as a Status node's "connected" did without a broker: each light is asked for a reading. <code>offline</code> as it stops;
320
+ deleted, everything it published is cleared. An MQTT out on output 1 and a Status node are then no longer needed:
321
+ output 1 sends nothing, and a Status node still wired in is ignored. Needed for a new room unless Publish is <i>Don't publish</i>;
322
+ opening and saving the room offers the first broker in your flows. A room saved before 6.2 that publishes keeps
323
+ publishing through output 1 and needs no broker until you choose one. Without one, the room publishes through output 1 as
324
+ before: once the flows start and at its startup message, which a Status node watching your MQTT out sends each
325
+ time it connects, with <code>offline</code> getting through only when this node is deployed on its own, so set
326
+ the MQTT broker node's Birth, Close and Will messages to the same (see the README).</dd>
261
327
  <dt>Discovery prefix</dt><dd>the topic prefix Home Assistant listens on for discovery: <code>homeassistant</code>
262
- unless you changed it in Home Assistant's MQTT settings.</dd>
328
+ unless you changed it in Home Assistant's MQTT settings. Shown with Home Assistant discovery.</dd>
263
329
  </dl>
264
- <p>New nodes start with both off. A node saved before these settings existed keeps publishing both.</p>
330
+ <p>New nodes start with <i>Don't publish</i>.</p>
265
331
  <h3>Targets</h3>
266
332
  <p>A JSON list of lights, each <code>{"id"}</code>: the name its adapter's <b>Target</b> and the policies use. Add <code>"onOffOnly": true</code>
267
333
  for a light that only switches on and off: it is never sent brightness, colour or a transition, styles skip it,
@@ -308,13 +374,18 @@
308
374
  <dd>on an <code>intent</code>, holds its targets on or off for this owner (overridden), on top of any other
309
375
  owner's. With
310
376
  <code>preserveManual: true</code>, manual lights are skipped.</dd>
377
+ <dt>lighting.area <span class="property-type">string</span></dt>
378
+ <dd>on an <code>intent</code> or <code>release</code>, the room it is for: this room acts on it only when it
379
+ matches its <b>Area</b> (compared as in topics), so a scene wired to several rooms reaches only its own
380
+ lights. Without it, any room it reaches acts on it.</dd>
311
381
  <dt>lighting.kind = "release"</dt>
312
382
  <dd>lets go of overridden <code>targets</code> (default: all) for <code>owner</code>, or for every owner without
313
383
  one. A light goes to the owner beneath, or with none left back to what it was before the first.</dd>
314
384
  </dl>
315
385
  <h3>Outputs</h3>
316
386
  <ol class="node-ports">
317
- <li>MQTT publications, for an MQTT out node: with <b>Publish state</b> on, on startup and whenever a target's
387
+ <li>MQTT publications, for an MQTT out node, when it has no <b>Broker</b> (with one, it publishes itself and this
388
+ output sends nothing): unless <b>Publish</b> is <i>Don't publish</i>, on startup and whenever a target's
318
389
  control state or availability changes, a retained message on <code>home/&lt;area&gt;/lighting/&lt;target&gt;/control</code>
319
390
  with payload <code>{"state": "in-control" | "overridden" | "manual", "owner": string | null, "available"?: boolean}</code>
320
391
  (<code>available</code> once its adapter has reported it), and the room's availability and discovery.</li>