@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
@@ -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,22 +60,28 @@ 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;
73
80
  }
74
81
  const input = {
75
82
  source: String(msg.source ?? ""),
76
- payload: msg.payload
83
+ payload: msg.payload,
84
+ baseline: msg.baseline === true
77
85
  };
78
86
  const result = (0, adapter_1.runReducer)(node, { area: fusionConfig.area, component: "occupancy-fusion" }, (saved) => (0, occupancy_fusion_1.transitionOccupancy)(saved, input, fusionConfig));
79
87
  for (const output of result.outputs) {
@@ -81,7 +89,12 @@ const registerOccupancyFusion = (RED) => {
81
89
  lifecycle.publish(send, (0, occupancy_publishing_1.occupancyStateOutputs)(publishing, output.payload.occupied));
82
90
  }
83
91
  updateStatus(result.status);
84
- }, { message: { source: "startup" }, trace: { area: fusionConfig.area, component: "occupancy-fusion" } });
92
+ }
93
+ (0, adapter_1.onInput)(node, (msg, send) => {
94
+ // With a broker, a Status node still wired in changes nothing.
95
+ if (!lifecycle.fromBroker(msg))
96
+ handle(msg, send);
97
+ }, lifecycle.startupOption({ source: "startup" }));
85
98
  }
86
99
  RED.nodes.registerType("home-occupancy-fusion", HomeOccupancyFusion);
87
100
  };
@@ -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>
@@ -5,6 +5,10 @@ const publishing_lifecycle_1 = require("./publishing-lifecycle");
5
5
  const room_publishing_1 = require("../lib/room-publishing");
6
6
  const adapter_1 = require("./adapter");
7
7
  const settings_1 = require("../lib/settings");
8
+ const home_assistant_setting_1 = require("../lib/home-assistant-setting");
9
+ const bounded_trace_1 = require("../lib/bounded-trace");
10
+ const mqtt_broker_link_1 = require("./mqtt-broker-link");
11
+ const home_assistant_discovery_1 = require("./home-assistant-discovery");
8
12
  const ha_discovery_1 = require("../lib/ha-discovery");
9
13
  const topic_names_1 = require("../lib/topic-names");
10
14
  // The room's lights: an id each, and onOffOnly for one that only switches.
@@ -32,6 +36,7 @@ const registerRoomController = (RED) => {
32
36
  const node = this;
33
37
  const area = (0, adapter_1.optionalArea)(config);
34
38
  let controllerConfig;
39
+ let broker = null;
35
40
  try {
36
41
  // A room saved before 6.0 still has its circadian settings, which moved
37
42
  // to the circadian lighting policy: it runs only once migrated, so it
@@ -50,11 +55,25 @@ const registerRoomController = (RED) => {
50
55
  }
51
56
  // Not set (null, or absent from a flow saved before these settings)
52
57
  // means on, so an older flow keeps publishing as before.
53
- const publishState = (0, settings_1.optionalBooleanSetting)(config.publishState, "publish state") ?? true;
54
- const discovery = (0, settings_1.optionalBooleanSetting)(config.discovery, "Home Assistant discovery") ?? true;
58
+ const homeAssistant = (0, home_assistant_setting_1.homeAssistantSetting)(config, { publishState: true, discovery: true });
59
+ const publishState = homeAssistant !== "off";
60
+ const discovery = homeAssistant === "discovery";
61
+ // A room saved before the choice needed an area whenever either old
62
+ // setting was on, even discovery alone, which published nothing: kept,
63
+ // so it validates as before.
64
+ const legacyArea = (config.homeAssistant === undefined || config.homeAssistant === null) &&
65
+ (((0, settings_1.optionalBooleanSetting)(config.publishState, "publish state") ?? true) || ((0, settings_1.optionalBooleanSetting)(config.discovery, "Home Assistant discovery") ?? true));
66
+ // With a broker, it publishes itself; without one, through output 1,
67
+ // as before 6.2.
68
+ const brokerId = typeof config.broker === "string" ? config.broker.trim() : "";
69
+ if (publishState && brokerId) {
70
+ broker = RED.nodes.getNode(brokerId);
71
+ if (!broker)
72
+ throw new Error("its MQTT broker node isn't in the flows: choose one, or set Publish to Don't publish");
73
+ }
55
74
  // Both publish under topics and IDs named after the area; otherwise the
56
75
  // area is only a label.
57
- if (publishState || discovery)
76
+ if (publishState || legacyArea)
58
77
  (0, topic_names_1.publishingArea)(area, true);
59
78
  controllerConfig = {
60
79
  nodeId: node.id,
@@ -81,15 +100,33 @@ const registerRoomController = (RED) => {
81
100
  const recorded = node.context().get("retainedTopics");
82
101
  if (Array.isArray(recorded))
83
102
  node.context().set("retainedTopics", (0, room_publishing_1.withoutCircadianState)(recorded));
84
- // Available, and clear of what it no longer publishes, as soon as it
85
- // runs; offline as it stops; nothing left behind when deleted.
86
- const lifecycle = (0, publishing_lifecycle_1.publishingLifecycle)(RED, node, {
87
- retainedTopics: () => (0, room_publishing_1.roomRetainedTopics)(controllerConfig),
88
- starting: () => (0, room_publishing_1.roomStartingOutputs)(controllerConfig),
89
- stopping: () => (0, room_publishing_1.roomStoppingOutputs)(controllerConfig),
90
- output: 0,
91
- outputs: 3
92
- });
103
+ const trace = { area: controllerConfig.area, component: "room-controller" };
104
+ // With a broker: everything announced on each connect and when Home
105
+ // Assistant comes back, offline as it stops, nothing left behind when
106
+ // deleted. Without one: the same through output 1, from the flows start
107
+ // and the startup message.
108
+ let homeAssistant = null;
109
+ let lifecycle = null;
110
+ if (broker) {
111
+ homeAssistant = (0, home_assistant_discovery_1.homeAssistantDiscovery)(RED, node, (0, mqtt_broker_link_1.brokerLink)(broker, node.id, (error) => node.error(error)), {
112
+ retainedTopics: () => (0, room_publishing_1.roomRetainedTopics)(controllerConfig),
113
+ announcement: () => (0, room_publishing_1.roomAnnouncement)(controllerConfig, node.context().get("state")),
114
+ stopping: () => (0, room_publishing_1.roomStoppingOutputs)(controllerConfig),
115
+ statusTopic: controllerConfig.discovery ? controllerConfig.discoveryPrefix + "/status" : null,
116
+ // Each connect runs its startup, as a Status node's "connected" did:
117
+ // its announcement, and each light asked for a reading.
118
+ onConnected: () => process({ lighting: { kind: "startup" } }, node.send.bind(node))
119
+ }, trace);
120
+ }
121
+ else {
122
+ lifecycle = (0, publishing_lifecycle_1.publishingLifecycle)(RED, node, {
123
+ retainedTopics: () => (0, room_publishing_1.roomRetainedTopics)(controllerConfig),
124
+ starting: () => (0, room_publishing_1.roomStartingOutputs)(controllerConfig),
125
+ stopping: () => (0, room_publishing_1.roomStoppingOutputs)(controllerConfig),
126
+ output: 0,
127
+ outputs: 3
128
+ });
129
+ }
93
130
  function updateStatus(status) {
94
131
  node.status({
95
132
  fill: status.manualCount ? "yellow" : status.styled ? "green" : "grey",
@@ -110,7 +147,10 @@ const registerRoomController = (RED) => {
110
147
  ];
111
148
  const warned = new Set();
112
149
  function process(msg, send) {
113
- const result = (0, adapter_1.runReducer)(node, { area: controllerConfig.area, component: "room-controller" }, (saved, now) => (0, room_publishing_1.publishRoom)(saved, msg, controllerConfig, now));
150
+ const result = (0, adapter_1.runReducer)(node, trace, (saved, now) => (0, room_publishing_1.publishRoom)(saved, msg, controllerConfig, now));
151
+ // With a broker, the publications go to it, and output 1 is quiet.
152
+ if (homeAssistant)
153
+ homeAssistant.publish(result.outputs);
114
154
  // Output 1: MQTT publications (control states, availability,
115
155
  // discovery). Output 2: light commands, for the lights' adapters.
116
156
  // Output 3: each light's reading passed on with its control state, for
@@ -119,7 +159,7 @@ const registerRoomController = (RED) => {
119
159
  controllerConfig.targets.some(({ id }) => id === msg.light.target) ? msg.light : null;
120
160
  const control = reading ? result.status.control[reading.target] : undefined;
121
161
  const outputs = [
122
- result.outputs.length ? result.outputs.map((output) => ({ ...output, payload: output.payload })) : null,
162
+ !homeAssistant && result.outputs.length ? result.outputs.map((output) => ({ ...output, payload: output.payload })) : null,
123
163
  result.commands.length ? result.commands.map((light) => ({ payload: undefined, light })) : null,
124
164
  reading ? { payload: undefined, light: { ...reading, control, manual: control === "manual" } } : null
125
165
  ];
@@ -135,9 +175,16 @@ const registerRoomController = (RED) => {
135
175
  return result;
136
176
  }
137
177
  (0, adapter_1.onInput)(node, (msg, send) => {
178
+ // With a broker it announces itself on every connect: a Status node
179
+ // still wired in, from before, changes nothing.
180
+ const status = (0, adapter_1.objectField)(msg, "status");
181
+ if (homeAssistant && status && status.source && typeof status.source === "object") {
182
+ (0, bounded_trace_1.appendTrace)(node.context(), trace, [{ event: "input", outcome: "ignored", reason: "has-broker", details: { status: String(status.text ?? "") } }]);
183
+ return;
184
+ }
138
185
  // The startup's own announcement comes from publishRoom.
139
186
  if ((0, adapter_1.objectField)(msg, "lighting")?.kind === "startup")
140
- lifecycle.startup(send);
187
+ lifecycle?.startup(send);
141
188
  process({
142
189
  lighting: (0, adapter_1.objectField)(msg, "lighting"),
143
190
  light: msg.light,
@@ -145,7 +192,7 @@ const registerRoomController = (RED) => {
145
192
  payload: msg.payload,
146
193
  retain: msg.retain === true
147
194
  }, send);
148
- }, { message: { lighting: { kind: "startup" } }, trace: { area: controllerConfig.area, component: "room-controller" } });
195
+ }, broker ? undefined : { message: { lighting: { kind: "startup" } }, trace });
149
196
  }
150
197
  RED.nodes.registerType("home-room-lighting-controller", HomeRoomLightingController);
151
198
  };
@@ -0,0 +1,3 @@
1
+ import type { NodeInitializer } from "node-red";
2
+ declare const registerScene: NodeInitializer;
3
+ export = registerScene;