@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
@@ -1,6 +1,31 @@
1
1
  <script type="text/javascript">
2
2
  (function () {
3
3
  function clockTime(v) { return /^([01]\d|2[0-3]):[0-5]\d$/.test(String(v || "")); }
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-circadian-lighting-policy", {
5
30
  category: "home automation",
6
31
  color: "#ffd27f",
@@ -8,12 +33,7 @@
8
33
  name: {value: ""},
9
34
  // Required only when the node publishes, and then with a letter or
10
35
  // digit, from which its topics are made.
11
- area: {value: "", validate: function (v) {
12
- var publishing = $("#node-input-publishState").length
13
- ? $("#node-input-publishState").prop("checked") || $("#node-input-discovery").prop("checked")
14
- : this.publishState === true || this.discovery === true;
15
- return !publishing || /[a-z0-9]/i.test(String(v || ""));
16
- }},
36
+ area: {value: "", validate: function (v) { return !needsArea(this) || /[a-z0-9]/i.test(String(v || "")); }},
17
37
  // The house schedule it follows; none: its own wake and bed times.
18
38
  houseSchedule: {value: "", type: "home-house-schedule", required: false},
19
39
  wakeTime: {value: "07:00", validate: function (v) { return Boolean(this.houseSchedule) || clockTime(v); }},
@@ -25,22 +45,46 @@
25
45
  refreshTransitionSeconds: {value: 60, required: true, validate: RED.validators.number()},
26
46
  refreshSeconds: {value: 60, required: true, validate: function (v) { return RED.validators.number()(v) && Number(v) >= 10; }},
27
47
  initialSleep: {value: false},
48
+ // Publish: off, state or discovery. null means "not set": a node saved
49
+ // before this setting, read from the two below, kept with no rows.
50
+ homeAssistant: {value: null},
51
+ // Node-RED's MQTT broker node, through which it publishes and reads its set
52
+ // topics itself. Needed unless Publish is "Don't publish". The editor offers
53
+ // the first broker when the dialog is opened and saved, not when the node
54
+ // is dropped.
55
+ broker: {value: "", type: "mqtt-broker", required: false, validate: function (v) {
56
+ return publishChoice(this) === "off" || (Boolean(v) && v !== "_ADD_") || throughOutput(this) ? true : "choose a broker, or set Publish to Don't publish";
57
+ }},
28
58
  publishState: {value: false},
29
59
  discovery: {value: false},
60
+ // true for a node that publishes through its output, as before 6.2, and
61
+ // was opened and saved without a broker: it stays valid until a broker is
62
+ // chosen. No form row.
63
+ publishesThroughOutput: {value: false},
30
64
  discoveryPrefix: {value: "homeassistant", required: true, validate: function (v) { var prefix = String(v || "").trim().replace(/^\/+|\/+$/g, ""); return Boolean(prefix) && !/[+#]/.test(prefix); }},
31
65
  // A new node, so its Home Assistant entities are named after it.
32
66
  idScheme: {value: "node"}
33
67
  },
68
+ oneditsave: function () {
69
+ // Through its output until a broker is chosen (read before the
70
+ // dialog's values are applied).
71
+ var broker = $("#node-input-broker").val();
72
+ this.publishesThroughOutput = throughOutput(this) && (!broker || broker === "_ADD_");
73
+ },
34
74
  oneditprepare: function () {
35
- // Discovery describes the published state, so it needs it.
36
- function follow() {
37
- var publishing = $("#node-input-publishState").prop("checked");
38
- if (!publishing) $("#node-input-discovery").prop("checked", false);
39
- $("#node-input-discovery").prop("disabled", !publishing);
40
- $("#node-input-area").trigger("change");
75
+ $("#node-input-homeAssistant").val(publishChoice(this));
76
+ // The broker only while it publishes, the prefix only with discovery.
77
+ var outputBefore = throughOutput(this);
78
+ function publishRows() {
79
+ var broker = $("#node-input-broker").val();
80
+ $(".publish-through-output").toggle($("#node-input-homeAssistant").val() !== "off" && outputBefore && (!broker || broker === "_ADD_"));
81
+ var choice = $("#node-input-homeAssistant").val();
82
+ $(".publish-broker").toggle(choice !== "off");
83
+ $(".publish-discovery").toggle(choice === "discovery");
84
+ $("#node-input-area, #node-input-broker").trigger("change");
41
85
  }
42
- $("#node-input-publishState, #node-input-discovery").on("change", follow);
43
- follow();
86
+ $("#node-input-homeAssistant, #node-input-broker").on("change", publishRows);
87
+ publishRows();
44
88
  // Following a house, the times are the house's.
45
89
  function times() {
46
90
  var own = !$("#node-input-houseSchedule").val() || $("#node-input-houseSchedule").val() === "_ADD_";
@@ -71,9 +115,20 @@
71
115
  <div class="form-row"><label for="node-input-refreshSeconds">Restyle interval (s)</label><input type="number" min="10" id="node-input-refreshSeconds"></div>
72
116
  <div class="form-row"><label for="node-input-initialSleep">Start in sleep mode</label><input type="checkbox" id="node-input-initialSleep" style="display:inline-block; width:auto;"></div>
73
117
  <div class="form-row"><label for="node-input-area">Area</label><input type="text" id="node-input-area" placeholder="needed when publishing"></div>
74
- <div class="form-row"><label for="node-input-publishState">Publish state over MQTT</label><input type="checkbox" id="node-input-publishState" style="display:inline-block; width:auto;"></div>
75
- <div class="form-row"><label for="node-input-discovery">Home Assistant discovery</label><input type="checkbox" id="node-input-discovery" style="display:inline-block; width:auto;"> <span>(needs Publish state)</span></div>
76
- <div class="form-row"><label for="node-input-discoveryPrefix">Discovery prefix</label><input type="text" id="node-input-discoveryPrefix"></div>
118
+ <div class="form-row">
119
+ <label for="node-input-homeAssistant">Publish</label>
120
+ <select id="node-input-homeAssistant">
121
+ <option value="off">Don't publish</option>
122
+ <option value="state">MQTT state</option>
123
+ <option value="discovery">MQTT state and Home Assistant discovery</option>
124
+ </select>
125
+ </div>
126
+ <div class="form-row publish-broker"><label for="node-input-broker">Broker</label><input type="text" id="node-input-broker"></div>
127
+ <div class="form-tips publish-broker">With a broker, it publishes and reads its set topics itself: an MQTT out on its
128
+ publication output, an MQTT in on its set topics, and a Status node are no longer needed and can be deleted.</div>
129
+ <div class="form-tips publish-through-output">This node publishes through its output, as before 6.2, and keeps
130
+ working that way. Choose a Broker to let it publish itself.</div>
131
+ <div class="form-row publish-discovery"><label for="node-input-discoveryPrefix">Discovery prefix</label><input type="text" id="node-input-discoveryPrefix"></div>
77
132
  </script>
78
133
 
79
134
  <script type="text/html" data-help-name="home-circadian-lighting-policy">
@@ -107,7 +162,7 @@
107
162
  the flows start, as the examples do.</dd>
108
163
  <dt>topic = home/&lt;area&gt;/circadian/set, home/&lt;area&gt;/sleep/set</dt>
109
164
  <dd><code>ON</code> or <code>OFF</code> from Home Assistant, as <code>lighting.kind = "circadian"</code> or
110
- <code>"sleep"</code> does. Read only while <b>Publish state</b> is on. Retained messages and other payloads are
165
+ <code>"sleep"</code> does. Read only while it publishes. Retained messages and other payloads are
111
166
  ignored.</dd>
112
167
  <dt>topic = home/&lt;area&gt;/wake-time/set or bed-time/set</dt>
113
168
  <dd>A time from Home Assistant, with its own times only.</dd>
@@ -130,15 +185,24 @@
130
185
  <dt>Restyle transition</dt><dd>How long a room takes to move a light that is on to a new style. A light turning
131
186
  on uses the room's own transition.</dd>
132
187
  <dt>Restyle interval</dt><dd>How often it sends its style, at least 10 seconds.</dd>
133
- <dt>Publish state over MQTT, Home Assistant discovery</dt><dd>Off unless switched on. With both on, Home Assistant
188
+ <dt>Publish</dt><dd><i>Don't publish</i> unless chosen. <i>MQTT state</i> publishes its state and reads its set
189
+ topics, plain MQTT; with <i>MQTT state and Home Assistant discovery</i>, Home Assistant
134
190
  shows Circadian lighting and Sleep switches and, with its own times, Wake time and Bed time, named after this
135
191
  node. The topics keep the names the room controller used before 6.0, so a flow already writing
136
192
  <code>home/&lt;area&gt;/circadian/set</code> keeps working when this node has that area. Before announcing, it
137
193
  clears the circadian entities older room controllers in its area published, migrated or not, in the same batch,
138
194
  so Home Assistant can give the new ones the same entity IDs.</dd>
139
195
  </dl>
196
+ <h3>Broker</h3>
197
+ <p>Choose Node-RED's <b>MQTT broker</b> node, and it publishes and reads its set topics itself: it announces everything each time the
198
+ broker connects, reconnects included, and each time Home Assistant comes back (<code>online</code> on
199
+ <code>&lt;prefix&gt;/status</code>), so its entities come back by themselves.
200
+ Each connect also runs its startup message, as a Status node's "connected" did without a broker. Its publication output then sends
201
+ nothing, and an MQTT out on it, an MQTT in on its set topics and a Status node are no longer needed: still wired, they change nothing.
202
+ Opening and saving the node offers the first broker in your flows. A node saved before 6.2 that publishes keeps
203
+ publishing on its output, as below, and needs no broker until you choose one.</p>
140
204
  <h3>When MQTT out connects</h3>
141
- <p>Wire a <b>Status node</b> whose scope is your MQTT out node into this node. Each time MQTT out shows
205
+ <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
142
206
  "connected", this node takes it as its startup message (<code>msg.lighting = {kind: "startup"}</code>) and announces itself again:
143
207
  Node-RED's MQTT broker node drops what it is given before it connects, so what the node sends as the flows
144
208
  start is lost on a first start, a full deploy or a restart. Any other status message is ignored.</p>
@@ -8,7 +8,8 @@ const publishing_node_1 = require("../lib/publishing-node");
8
8
  const settings_1 = require("../lib/settings");
9
9
  const topic_names_1 = require("../lib/topic-names");
10
10
  const adapter_1 = require("./adapter");
11
- const publishing_lifecycle_1 = require("./publishing-lifecycle");
11
+ const publishing_side_1 = require("./publishing-side");
12
+ const home_assistant_setting_1 = require("../lib/home-assistant-setting");
12
13
  const refused_enable_1 = require("./refused-enable");
13
14
  const shared_schedule_1 = require("./shared-schedule");
14
15
  const registerCircadianPolicy = (RED) => {
@@ -22,6 +23,7 @@ const registerCircadianPolicy = (RED) => {
22
23
  const { schedule: house, missing: houseMissing } = (0, shared_schedule_1.referencedSchedule)(RED, config.houseSchedule);
23
24
  let policyConfig;
24
25
  let publishing;
26
+ let broker;
25
27
  try {
26
28
  const area = (0, adapter_1.optionalArea)(config);
27
29
  const levels = {
@@ -43,14 +45,14 @@ const registerCircadianPolicy = (RED) => {
43
45
  };
44
46
  // Off unless switched on; a new node, so its entities are named after
45
47
  // it.
46
- const publishState = (0, settings_1.optionalBooleanSetting)(config.publishState, "publish state") ?? false;
47
- const discovery = (0, settings_1.optionalBooleanSetting)(config.discovery, "Home Assistant discovery") ?? false;
48
- if (publishState || discovery)
48
+ const publish = (0, home_assistant_setting_1.publishingChoice)(config, { publishState: false, discovery: false });
49
+ if (publish.needsArea)
49
50
  (0, topic_names_1.publishingArea)(area, true);
51
+ broker = (0, publishing_side_1.brokerSetting)(RED, config.broker, publish.publishState);
50
52
  publishing = {
51
53
  area,
52
- publishState,
53
- discovery,
54
+ publishState: publish.publishState,
55
+ discovery: publish.discovery,
54
56
  discoveryPrefix: (0, ha_discovery_1.discoveryPrefixSetting)(config.discoveryPrefix),
55
57
  nodeId: node.id,
56
58
  // Always named after the node, so its entries never share a topic
@@ -81,14 +83,25 @@ const registerCircadianPolicy = (RED) => {
81
83
  }
82
84
  // Styles go to rooms on the first output; MQTT publications on the
83
85
  // second.
84
- const lifecycle = (0, publishing_lifecycle_1.publishingLifecycle)(RED, node, {
86
+ const trace = { area: publishing.area, component: "circadian-policy" };
87
+ const lifecycle = (0, publishing_side_1.publishingSide)(RED, node, broker, {
85
88
  retainedTopics: () => (0, circadian_publishing_1.circadianRetainedTopics)(publishing),
86
89
  possible: { owner: publishing, topics: () => (0, publishing_node_1.publishesState)(publishing) ? (0, circadian_publishing_1.circadianRetainedTopics)({ ...publishing, publishState: true, discovery: true }) : [] },
87
90
  starting: () => (0, circadian_publishing_1.circadianStartingOutputs)(publishing, savedState(), roomsInArea()),
88
91
  stopping: () => (0, circadian_publishing_1.circadianStoppingOutputs)(publishing),
89
92
  output: 1,
90
- outputs: 2
91
- });
93
+ outputs: 2,
94
+ startupMessage: { lighting: { kind: "startup" } },
95
+ statusTopic: publishing.discovery ? publishing.discoveryPrefix + "/status" : null,
96
+ // Following a house, the wake and bed times are the house's to set.
97
+ setTopics: (0, publishing_node_1.publishesState)(publishing) ? [
98
+ (0, circadian_publishing_1.circadianSwitchTopic)(publishing.area, "circadian", "set"),
99
+ (0, circadian_publishing_1.circadianSwitchTopic)(publishing.area, "sleep", "set"),
100
+ ...(publishing.timesFrom !== "house"
101
+ ? [(0, circadian_publishing_1.circadianTimeTopic)(publishing.area, "wake-time", "set"), (0, circadian_publishing_1.circadianTimeTopic)(publishing.area, "bed-time", "set")]
102
+ : [])
103
+ ] : []
104
+ }, trace, (msg, send) => handle(msg, send));
92
105
  function updateStatus(status) {
93
106
  const values = status.values;
94
107
  node.status(values
@@ -127,7 +140,7 @@ const registerCircadianPolicy = (RED) => {
127
140
  process({ kind: "schedule", ...house.schedule() }, node.send.bind(node));
128
141
  })));
129
142
  }
130
- (0, adapter_1.onInput)(node, (msg, send) => {
143
+ function handle(msg, send) {
131
144
  const lighting = (0, adapter_1.objectField)(msg, "lighting");
132
145
  if (lighting?.kind === "startup") {
133
146
  lifecycle.startup(send, (0, circadian_publishing_1.circadianStartingOutputs)(publishing, savedState(), roomsInArea()));
@@ -143,7 +156,13 @@ const registerCircadianPolicy = (RED) => {
143
156
  else {
144
157
  process(lighting ?? { kind: undefined }, send);
145
158
  }
146
- }, { message: { lighting: { kind: "startup" } }, trace: { area: publishing.area, component: "circadian-policy" } });
159
+ }
160
+ (0, adapter_1.onInput)(node, (msg, send) => {
161
+ // With a broker, its set topics come from the broker: an MQTT in or a
162
+ // Status node still wired in changes nothing.
163
+ if (!lifecycle.fromBroker(msg))
164
+ handle(msg, send);
165
+ }, lifecycle.startupOption({ lighting: { kind: "startup" } }));
147
166
  tickTimer = setInterval((0, adapter_1.guarded)(node, () => process({ kind: "tick" }, node.send.bind(node))), policyConfig.intervalSeconds * 1000);
148
167
  // Deleted: rooms stop styling at once rather than when the last style
149
168
  // runs out.
@@ -1,11 +1,29 @@
1
1
  <script type="text/javascript">
2
2
  (function () {
3
- // Whether the node publishes, from the open dialog or its saved settings.
4
- function publishes(node) {
3
+ // Its Publish choice (off, state or discovery), from the open dialog or
4
+ // its saved settings: a node saved before the choice reads its two old
5
+ // settings (lib/home-assistant-setting.ts).
6
+ function publishChoice(node) {
5
7
  function on(value) { return value === true || value === "true"; }
6
- var state = $("#node-input-publishState");
7
- var discovery = $("#node-input-discovery");
8
- return state.length ? state.prop("checked") || discovery.prop("checked") : on(node.publishState) || on(node.discovery);
8
+ var select = $("#node-input-homeAssistant");
9
+ if (select.length) return select.val();
10
+ if (node.homeAssistant === "off" || node.homeAssistant === "state" || node.homeAssistant === "discovery") return node.homeAssistant;
11
+ return !on(node.publishState) ? "off" : on(node.discovery) ? "discovery" : "state";
12
+ }
13
+ // Whether it publishes through its output, as before 6.2: a node saved
14
+ // before the Publish choice that published, or one opened and saved since
15
+ // without a broker (publishesThroughOutput). It needs no broker until one
16
+ // is chosen; from saved values only, not the open dialog.
17
+ function throughOutput(node) {
18
+ if (node.publishesThroughOutput === true) return true;
19
+ return (node.homeAssistant === null || node.homeAssistant === undefined) && (node.publishState === true || node.publishState === "true");
20
+ }
21
+ // Its area is needed while it publishes, and for a node saved before the
22
+ // choice whenever either old setting was on, as the runtime checks.
23
+ function needsArea(node) {
24
+ function on(value) { return value === true || value === "true"; }
25
+ var saved = !$("#node-input-homeAssistant").length && (node.homeAssistant === null || node.homeAssistant === undefined);
26
+ return publishChoice(node) !== "off" || (saved && (on(node.publishState) || on(node.discovery)));
9
27
  }
10
28
  RED.nodes.registerType("home-cover-controller", {
11
29
  category: "home automation",
@@ -14,7 +32,7 @@
14
32
  // A marker only a node new from the palette has, as core nodes do: import
15
33
  // and paste keep the saved name, so onadd leaves a saved node's settings.
16
34
  name: {value: "_DEFAULT_"},
17
- area: {value: "", validate: function (v) { return !publishes(this) || /[a-z0-9]/i.test(String(v || "")); }},
35
+ area: {value: "", validate: function (v) { return !needsArea(this) || /[a-z0-9]/i.test(String(v || "")); }},
18
36
  // Not set: a position cover, as every cover node was before this setting.
19
37
  coverType: {value: "position"},
20
38
  presets: {value: "{\"partial\":60}", required: true, validate: function (v) {
@@ -38,10 +56,24 @@
38
56
  // Its name in its topics, home/<area>/cover/<name>/..., as a room
39
57
  // usually has more than one cover: no default, so two covers can't
40
58
  // share topics by leaving it alone. Needed only while it publishes.
41
- topicName: {value: "", validate: function (v) { return !publishes(this) || /[a-z0-9]/i.test(String(v || "")); }},
59
+ topicName: {value: "", validate: function (v) { return !needsArea(this) || /[a-z0-9]/i.test(String(v || "")); }},
42
60
  // Off until switched on.
61
+ // Publish: off, state or discovery. null means "not set": a node saved
62
+ // before this setting, read from the two below, kept with no rows.
63
+ homeAssistant: {value: null},
64
+ // Node-RED's MQTT broker node, through which it publishes and reads its set
65
+ // topics itself. Needed unless Publish is "Don't publish". The editor offers
66
+ // the first broker when the dialog is opened and saved, not when the node
67
+ // is dropped.
68
+ broker: {value: "", type: "mqtt-broker", required: false, validate: function (v) {
69
+ return publishChoice(this) === "off" || (Boolean(v) && v !== "_ADD_") || throughOutput(this) ? true : "choose a broker, or set Publish to Don't publish";
70
+ }},
43
71
  publishState: {value: false},
44
72
  discovery: {value: false},
73
+ // true for a node that publishes through its output, as before 6.2, and
74
+ // was opened and saved without a broker: it stays valid until a broker is
75
+ // chosen. No form row.
76
+ publishesThroughOutput: {value: false},
45
77
  discoveryPrefix: {value: "homeassistant", required: true, validate: function (v) { var prefix = String(v || "").trim().replace(/^\/+|\/+$/g, ""); return Boolean(prefix) && !/[+#]/.test(prefix); }},
46
78
  // How Home Assistant IDs are named; not set also means "node".
47
79
  idScheme: {value: null}
@@ -51,6 +83,12 @@
51
83
  this.name = "";
52
84
  this.idScheme = "node";
53
85
  },
86
+ oneditsave: function () {
87
+ // Through its output until a broker is chosen (read before the
88
+ // dialog's values are applied).
89
+ var broker = $("#node-input-broker").val();
90
+ this.publishesThroughOutput = throughOutput(this) && (!broker || broker === "_ADD_");
91
+ },
54
92
  oneditprepare: function () {
55
93
  function on(value) { return value === true || value === "true"; }
56
94
  function follow() {
@@ -60,16 +98,19 @@
60
98
  }
61
99
  $("#node-input-coverType").on("change", follow);
62
100
  follow();
63
- $("#node-input-publishState").prop("checked", on(this.publishState));
64
- $("#node-input-discovery").prop("checked", on(this.discovery));
65
- // Discovery describes the published state, so it needs it.
66
- function publishing() {
67
- var state = $("#node-input-publishState").prop("checked");
68
- if (!state) $("#node-input-discovery").prop("checked", false);
69
- $("#node-input-discovery").prop("disabled", !state);
101
+ $("#node-input-homeAssistant").val(publishChoice(this));
102
+ // The broker only while it publishes, the prefix only with discovery.
103
+ var outputBefore = throughOutput(this);
104
+ function publishRows() {
105
+ var broker = $("#node-input-broker").val();
106
+ $(".publish-through-output").toggle($("#node-input-homeAssistant").val() !== "off" && outputBefore && (!broker || broker === "_ADD_"));
107
+ var choice = $("#node-input-homeAssistant").val();
108
+ $(".publish-broker").toggle(choice !== "off");
109
+ $(".publish-discovery").toggle(choice === "discovery");
110
+ $("#node-input-area, #node-input-broker").trigger("change");
70
111
  }
71
- $("#node-input-publishState, #node-input-discovery").on("change", publishing);
72
- publishing();
112
+ $("#node-input-homeAssistant, #node-input-broker").on("change", publishRows);
113
+ publishRows();
73
114
  },
74
115
  inputs: 1,
75
116
  outputs: 4,
@@ -100,9 +141,20 @@
100
141
  <div class="form-row cover-open-close"><label for="node-input-marginMs">Travel margin (ms)</label><input type="number" id="node-input-marginMs"></div>
101
142
  <div class="form-row"><label for="node-input-maxAttempts">Maximum attempts</label><input type="number" id="node-input-maxAttempts"></div>
102
143
  <div class="form-row"><label for="node-input-topicName">Name in topics</label><input type="text" id="node-input-topicName" placeholder="left, for example"></div>
103
- <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>
104
- <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>
105
- <div class="form-row"><label for="node-input-discoveryPrefix">Discovery prefix</label><input type="text" id="node-input-discoveryPrefix"></div>
144
+ <div class="form-row">
145
+ <label for="node-input-homeAssistant">Publish</label>
146
+ <select id="node-input-homeAssistant">
147
+ <option value="off">Don't publish</option>
148
+ <option value="state">MQTT state</option>
149
+ <option value="discovery">MQTT state and Home Assistant discovery</option>
150
+ </select>
151
+ </div>
152
+ <div class="form-row publish-broker"><label for="node-input-broker">Broker</label><input type="text" id="node-input-broker"></div>
153
+ <div class="form-tips publish-broker">With a broker, it publishes and reads its set topics itself: an MQTT out on its
154
+ publication output, an MQTT in on its set topics, and a Status node are no longer needed and can be deleted.</div>
155
+ <div class="form-tips publish-through-output">This node publishes through its output, as before 6.2, and keeps
156
+ working that way. Choose a Broker to let it publish itself.</div>
157
+ <div class="form-row publish-discovery"><label for="node-input-discoveryPrefix">Discovery prefix</label><input type="text" id="node-input-discoveryPrefix"></div>
106
158
  </script>
107
159
 
108
160
  <script type="text/html" data-help-name="home-cover-controller">
@@ -147,19 +199,21 @@
147
199
  motion lighting while a blind moves, send it to the motion policy's <code>pause</code> input (with
148
200
  <code>msg.owner</code>), as the example does, not its enable (<code>area-enabled</code>): that is the
149
201
  user's own switch, and a cover move must not change it.</li>
150
- <li>MQTT publications, when <b>Publish state</b> is on: wire it to an MQTT out node.</li>
202
+ <li>MQTT publications, while it publishes without a <b>Broker</b>: wire it to an MQTT out node.</li>
151
203
  </ol>
152
204
  <h3>Publishing</h3>
153
205
  <p>Off unless switched on, and then named after the <b>Area</b> and the <b>Name in topics</b>, both needed then.
154
- The name tells this cover from others in the room and has no default, so give each cover its own. With <b>Publish state</b> it publishes, retained, the cover's
206
+ The name tells this cover from others in the room and has no default, so give each cover its own. While it publishes, it sends, retained, the cover's
155
207
  state (<code>open</code>, <code>closed</code>, <code>opening</code>, <code>closing</code> or
156
208
  <code>stopped</code>) on <code>home/&lt;area&gt;/cover/&lt;name&gt;/state</code> and, for a position cover,
157
209
  its position on <code>…/position</code>, once it has reported, with <code>online</code> or <code>offline</code>
158
210
  on <code>…/availability</code>. It takes <code>OPEN</code>, <code>CLOSE</code> or <code>STOP</code> on
159
211
  <code>…/set</code>, and a position on <code>…/position/set</code>, as requests like any other, carried out and
160
- confirmed as above; wire an MQTT in node on <code>home/&lt;area&gt;/cover/&lt;name&gt;/+</code> (and
161
- <code>…/position/set</code>) into this node. Nothing commands the device through Home Assistant. With
162
- <b>Home Assistant discovery</b>, a cover entity named by <b>Name in topics</b>, with open, close and stop, and
212
+ confirmed as above; wire an MQTT in node on <code>home/&lt;area&gt;/cover/&lt;name&gt;/set</code> (and, for a position cover,
213
+ one on <code>…/position/set</code>) into this node. Anything else on its own topics, and any other cover's topics
214
+ (<code>home/&lt;area&gt;/cover/&lt;name&gt;/…</code>), such as their state coming back through a wildcard
215
+ subscription, is ignored, with a trace. Nothing commands the device through Home Assistant. With
216
+ <i>MQTT state and Home Assistant discovery</i>, a cover entity named by <b>Name in topics</b>, with open, close and stop, and
163
217
  for a position cover also its position and set position.</p>
164
218
  <h3>Open, close and stop only</h3>
165
219
  <p>For a motor with no reliable position, set <b>Cover</b> to <i>Open, close and stop only</i>. It takes
@@ -179,8 +233,16 @@
179
233
  </ul>
180
234
  <p>Battery and charging status, when the cover reports them, are kept as facts and shown in the status; the node
181
235
  never acts on them.</p>
236
+ <h3>Broker</h3>
237
+ <p>Choose Node-RED's <b>MQTT broker</b> node, and it publishes and reads its set topics itself: it announces everything each time the
238
+ broker connects, reconnects included, and each time Home Assistant comes back (<code>online</code> on
239
+ <code>&lt;prefix&gt;/status</code>), so its entities come back by themselves.
240
+ Each connect also runs its startup message, as a Status node's "connected" did without a broker. Its publication output then sends
241
+ nothing, and an MQTT out on it, an MQTT in on its set topics and a Status node are no longer needed: still wired, they change nothing.
242
+ Opening and saving the node offers the first broker in your flows. A node saved before 6.2 that publishes keeps
243
+ publishing on its output, as below, and needs no broker until you choose one.</p>
182
244
  <h3>When MQTT out connects</h3>
183
- <p>Wire a <b>Status node</b> whose scope is your MQTT out node into this node. Each time MQTT out shows
245
+ <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
184
246
  "connected", this node takes it as its startup message (<code>msg.cover = {kind: "startup"}</code>) and announces itself again:
185
247
  Node-RED's MQTT broker node drops what it is given before it connects, so what the node sends as the flows
186
248
  start is lost on a first start, a full deploy or a restart. Any other status message is ignored.</p>
@@ -8,7 +8,8 @@ const settings_1 = require("../lib/settings");
8
8
  const target_state_1 = require("../lib/target-state");
9
9
  const topic_names_1 = require("../lib/topic-names");
10
10
  const adapter_1 = require("./adapter");
11
- const publishing_lifecycle_1 = require("./publishing-lifecycle");
11
+ const publishing_side_1 = require("./publishing-side");
12
+ const home_assistant_setting_1 = require("../lib/home-assistant-setting");
12
13
  // The cover's name in its topics, which it needs while it publishes.
13
14
  function coverTopicName(value) {
14
15
  const name = (0, settings_1.textSetting)(value, "name in topics");
@@ -27,6 +28,7 @@ const registerCoverController = (RED) => {
27
28
  let presets;
28
29
  let area;
29
30
  let publishing;
31
+ let broker;
30
32
  try {
31
33
  area = (0, adapter_1.optionalArea)(config);
32
34
  // Not set: a position cover, as every cover node was before the type.
@@ -35,17 +37,17 @@ const registerCoverController = (RED) => {
35
37
  presets = (0, cover_requests_1.coverPresets)(config.presets);
36
38
  // Off unless switched on, as the cover published nothing before; not
37
39
  // set names its entity after the node.
38
- const publishState = (0, settings_1.optionalBooleanSetting)(config.publishState, "publish state") ?? false;
39
- const discovery = (0, settings_1.optionalBooleanSetting)(config.discovery, "Home Assistant discovery") ?? false;
40
- if (publishState || discovery)
40
+ const publish = (0, home_assistant_setting_1.publishingChoice)(config, { publishState: false, discovery: false });
41
+ if (publish.needsArea)
41
42
  (0, topic_names_1.publishingArea)(area, true);
43
+ broker = (0, publishing_side_1.brokerSetting)(RED, config.broker, publish.publishState);
42
44
  publishing = {
43
45
  area,
44
46
  // No default: two covers in a room must not share topics.
45
- name: publishState || discovery ? coverTopicName(config.topicName) : "",
47
+ name: publish.needsArea ? coverTopicName(config.topicName) : "",
46
48
  positions: model.positions,
47
- publishState,
48
- discovery,
49
+ publishState: publish.publishState,
50
+ discovery: publish.discovery,
49
51
  discoveryPrefix: (0, ha_discovery_1.discoveryPrefixSetting)(config.discoveryPrefix),
50
52
  nodeId: node.id,
51
53
  idScheme: (0, settings_1.optionalChoiceSetting)(config.idScheme, "ID scheme", ha_discovery_1.ID_SCHEMES) ?? "node"
@@ -72,14 +74,19 @@ const registerCoverController = (RED) => {
72
74
  // MQTT publications on output 4, apart from the cover's own outputs:
73
75
  // available and clear of what it no longer publishes as soon as it runs,
74
76
  // offline as it stops, nothing left behind when deleted.
75
- const lifecycle = (0, publishing_lifecycle_1.publishingLifecycle)(RED, node, {
77
+ const lifecycle = (0, publishing_side_1.publishingSide)(RED, node, broker, {
76
78
  retainedTopics: () => (0, cover_publishing_1.coverRetainedTopics)(publishing),
77
79
  possible: { owner: publishing, topics: () => (0, publishing_node_1.publishesState)(publishing) ? (0, cover_publishing_1.coverRetainedTopics)({ ...publishing, discovery: true }) : [] },
78
80
  starting: () => (0, cover_publishing_1.coverStartingOutputs)(publishing, model.view(saved())),
79
81
  stopping: () => (0, cover_publishing_1.coverStoppingOutputs)(publishing),
80
82
  output: 3,
81
- outputs: 4
82
- });
83
+ outputs: 4,
84
+ startupMessage: { cover: { kind: "startup" } },
85
+ statusTopic: publishing.discovery ? publishing.discoveryPrefix + "/status" : null,
86
+ setTopics: (0, publishing_node_1.publishesState)(publishing)
87
+ ? [(0, cover_publishing_1.coverCommandTopic)(publishing), ...(publishing.positions ? [(0, cover_publishing_1.coverSetPositionTopic)(publishing)] : [])]
88
+ : []
89
+ }, trace, (msg, send) => handle(msg, send));
83
90
  function process(event, send) {
84
91
  const wasMoving = model.moving(saved());
85
92
  const viewBefore = JSON.stringify(model.view(saved()));
@@ -116,7 +123,7 @@ const registerCoverController = (RED) => {
116
123
  process({ kind: "startup" }, node.send.bind(node));
117
124
  }), 0);
118
125
  }
119
- (0, adapter_1.onInput)(node, (msg, send) => {
126
+ function handle(msg, send) {
120
127
  const cover = (0, adapter_1.objectField)(msg, "cover");
121
128
  if (cover) {
122
129
  if (cover.kind === "startup")
@@ -132,7 +139,13 @@ const registerCoverController = (RED) => {
132
139
  return;
133
140
  }
134
141
  handleRequest(inbound ? { payload: inbound.request, source: "home-assistant" } : msg, send);
135
- }, { message: { cover: { kind: "startup" } }, trace: trace });
142
+ }
143
+ (0, adapter_1.onInput)(node, (msg, send) => {
144
+ // With a broker, its set topics come from the broker: an MQTT in or a
145
+ // Status node still wired in changes nothing.
146
+ if (!lifecycle.fromBroker(msg))
147
+ handle(msg, send);
148
+ }, lifecycle.startupOption({ cover: { kind: "startup" } }));
136
149
  function handleRequest(msg, send) {
137
150
  requests += 1;
138
151
  // A cover group's own ID for its request, so it can match the answer.
@@ -0,0 +1,21 @@
1
+ import type { Node } 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
+ export interface DevicePublication {
6
+ topic: string;
7
+ payload: unknown;
8
+ retain?: boolean;
9
+ }
10
+ export interface DeviceSide {
11
+ broker: boolean;
12
+ publish(publications: readonly DevicePublication[]): Array<Record<string, unknown>> | null;
13
+ fromBroker(msg: InputMessage): boolean;
14
+ subscribe(topic: string): void;
15
+ }
16
+ export declare function deviceSide(node: Node, broker: BrokerNode | null, prefix: string, subscriptions: string[], trace: TraceMetadata, handle: (msg: InputMessage, send: Send) => void, onConnected?: () => void): DeviceSide;
17
+ export declare function deviceBroker(RED: {
18
+ nodes: {
19
+ getNode(id: string): unknown;
20
+ };
21
+ }, value: unknown): BrokerNode | null;
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.deviceSide = deviceSide;
4
+ exports.deviceBroker = deviceBroker;
5
+ const bounded_trace_1 = require("../lib/bounded-trace");
6
+ const adapter_1 = require("./adapter");
7
+ const mqtt_broker_link_1 = require("./mqtt-broker-link");
8
+ // MQTT payloads as text: an object as JSON, as the broker node would.
9
+ function text(payload) {
10
+ if (payload === null || payload === undefined)
11
+ return "";
12
+ if (typeof payload === "string")
13
+ return payload;
14
+ if (typeof payload === "object")
15
+ return JSON.stringify(payload);
16
+ return String(payload);
17
+ }
18
+ function deviceSide(node, broker,
19
+ // Every topic of its device lies under this prefix (or is it): a message
20
+ // on its input there is one it now gets from the broker.
21
+ prefix,
22
+ // What it subscribes to: its device's topics.
23
+ subscriptions, trace,
24
+ // The node's own input handling, for a device message from the broker.
25
+ handle,
26
+ // On each connect, reconnects included: whatever it does at startup.
27
+ onConnected) {
28
+ if (!broker) {
29
+ return {
30
+ broker: false,
31
+ publish: (publications) => publications.length ? publications.map((publication) => ({ ...publication })) : null,
32
+ fromBroker: () => false,
33
+ subscribe: () => { }
34
+ };
35
+ }
36
+ const link = (0, mqtt_broker_link_1.brokerLink)(broker, node.id, (error) => node.error(error));
37
+ const subscribe = (topic) => {
38
+ link.subscribe(topic, (message) => handle({ topic: message.topic, payload: message.payload, retain: message.retain }, node.send.bind(node)));
39
+ };
40
+ for (const topic of subscriptions)
41
+ subscribe(topic);
42
+ if (onConnected)
43
+ link.onConnected(onConnected);
44
+ node.on("close", (removed, done) => link.close(removed, done));
45
+ return {
46
+ broker: true,
47
+ subscribe,
48
+ publish(publications) {
49
+ for (const { topic, payload, retain } of publications)
50
+ link.publish(topic, text(payload), { retain: retain === true });
51
+ return null;
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 device = typeof msg.topic === "string" && (msg.topic === prefix || msg.topic.startsWith(prefix + "/"));
57
+ if (!statusNode && !device)
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
+ };
64
+ }
65
+ // The Broker setting of an adapter: the mqtt-broker node, or null without
66
+ // one (as before 6.2, through MQTT in and out nodes).
67
+ function deviceBroker(RED, value) {
68
+ const id = typeof value === "string" ? value.trim() : "";
69
+ if (!id)
70
+ return null;
71
+ const broker = RED.nodes.getNode(id);
72
+ if (!broker)
73
+ throw new Error("its MQTT broker node isn't in the flows: choose one, or none to use MQTT in and out nodes");
74
+ return broker;
75
+ }
76
+ //# sourceMappingURL=device-side.js.map