@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.
- package/README.md +204 -42
- package/dist/lib/cover-publishing.js +20 -6
- package/dist/lib/home-assistant-setting.d.ts +23 -0
- package/dist/lib/home-assistant-setting.js +39 -0
- package/dist/lib/motion-policy.d.ts +1 -0
- package/dist/lib/motion-policy.js +21 -1
- package/dist/lib/room-controller.d.ts +1 -0
- package/dist/lib/room-controller.js +5 -0
- package/dist/lib/room-publishing.d.ts +1 -0
- package/dist/lib/room-publishing.js +11 -0
- package/dist/lib/scene-publishing.d.ts +30 -0
- package/dist/lib/scene-publishing.js +63 -0
- package/dist/lib/scene.d.ts +61 -0
- package/dist/lib/scene.js +170 -0
- package/dist/lib/shelly-cover.js +5 -0
- package/dist/lib/shelly-relay-light.d.ts +24 -0
- package/dist/lib/shelly-relay-light.js +93 -0
- package/dist/lib/shelly-switch.d.ts +8 -0
- package/dist/lib/shelly-switch.js +37 -8
- package/dist/lib/theatre-activity-controller.js +4 -0
- package/dist/lib/theatre-metrics.js +6 -4
- package/dist/lib/theatre-slo.js +3 -0
- package/dist/lib/theatre-state-contract.d.ts +4 -2
- package/dist/lib/theatre-state-contract.js +51 -8
- package/dist/lib/theatre-state.js +3 -0
- package/dist/nodes/avr-controller.html +82 -21
- package/dist/nodes/avr-controller.js +23 -11
- package/dist/nodes/circadian-policy.html +84 -20
- package/dist/nodes/circadian-policy.js +30 -11
- package/dist/nodes/cover-controller.html +87 -25
- package/dist/nodes/cover-controller.js +25 -12
- package/dist/nodes/device-side.d.ts +20 -0
- package/dist/nodes/device-side.js +72 -0
- package/dist/nodes/home-assistant-discovery.d.ts +26 -0
- package/dist/nodes/home-assistant-discovery.js +64 -0
- package/dist/nodes/house-schedule-mqtt.html +70 -5
- package/dist/nodes/house-schedule-mqtt.js +22 -8
- package/dist/nodes/motion-policy.html +87 -22
- package/dist/nodes/motion-policy.js +44 -11
- package/dist/nodes/mqtt-broker-link.d.ts +41 -0
- package/dist/nodes/mqtt-broker-link.js +91 -0
- package/dist/nodes/occupancy-fusion.html +83 -20
- package/dist/nodes/occupancy-fusion.js +23 -11
- package/dist/nodes/publishing-side.d.ts +20 -0
- package/dist/nodes/publishing-side.js +66 -0
- package/dist/nodes/room-controller.html +106 -35
- package/dist/nodes/room-controller.js +63 -16
- package/dist/nodes/scene.d.ts +3 -0
- package/dist/nodes/scene.html +226 -0
- package/dist/nodes/scene.js +121 -0
- package/dist/nodes/shelly-cover.html +46 -2
- package/dist/nodes/shelly-cover.js +19 -4
- package/dist/nodes/shelly-switch.html +75 -6
- package/dist/nodes/shelly-switch.js +32 -7
- package/dist/nodes/theatre-state-contract.html +72 -9
- package/dist/nodes/theatre-state-contract.js +33 -14
- package/dist/nodes/z2m-cover.html +46 -2
- package/dist/nodes/z2m-cover.js +21 -4
- package/dist/nodes/z2m-light.html +46 -2
- package/dist/nodes/z2m-light.js +23 -4
- package/examples/Cover control.json +38 -91
- package/examples/Cover pair.json +36 -3
- package/examples/Home theatre activity.json +46 -91
- package/examples/Home theatre equipment.json +34 -2
- package/examples/House schedule.json +58 -80
- package/examples/Room lighting.json +45 -44
- package/examples/Scene.json +215 -0
- package/examples/Shelly cover.json +36 -105
- package/examples/Shelly switch.json +52 -148
- package/package.json +2 -1
|
@@ -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
|
|
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
|
|
47
|
-
|
|
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
|
|
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
|
-
|
|
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
|
-
}
|
|
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
|
-
//
|
|
4
|
-
|
|
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
|
|
7
|
-
|
|
8
|
-
|
|
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 !
|
|
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 !
|
|
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-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
function
|
|
67
|
-
var
|
|
68
|
-
|
|
69
|
-
$("#node-input-
|
|
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-
|
|
72
|
-
|
|
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"
|
|
104
|
-
|
|
105
|
-
|
|
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,
|
|
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.
|
|
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/<area>/cover/<name>/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/<area>/cover/<name>
|
|
161
|
-
<code>…/position/set</code>) into this node.
|
|
162
|
-
<
|
|
212
|
+
confirmed as above; wire an MQTT in node on <code>home/<area>/cover/<name>/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/<area>/cover/<name>/…</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><prefix>/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>
|
|
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
|
|
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
|
|
39
|
-
|
|
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:
|
|
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,
|
|
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
|
-
|
|
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
|
-
}
|
|
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,20 @@
|
|
|
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
|
+
}
|
|
15
|
+
export declare function deviceSide(node: Node, broker: BrokerNode | null, prefix: string, subscriptions: string[], trace: TraceMetadata, handle: (msg: InputMessage, send: Send) => void, onConnected?: () => void): DeviceSide;
|
|
16
|
+
export declare function deviceBroker(RED: {
|
|
17
|
+
nodes: {
|
|
18
|
+
getNode(id: string): unknown;
|
|
19
|
+
};
|
|
20
|
+
}, value: unknown): BrokerNode | null;
|
|
@@ -0,0 +1,72 @@
|
|
|
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
|
+
};
|
|
34
|
+
}
|
|
35
|
+
const link = (0, mqtt_broker_link_1.brokerLink)(broker, node.id, (error) => node.error(error));
|
|
36
|
+
for (const topic of subscriptions) {
|
|
37
|
+
link.subscribe(topic, (message) => handle({ topic: message.topic, payload: message.payload, retain: message.retain }, node.send.bind(node)));
|
|
38
|
+
}
|
|
39
|
+
if (onConnected)
|
|
40
|
+
link.onConnected(onConnected);
|
|
41
|
+
node.on("close", (removed, done) => link.close(removed, done));
|
|
42
|
+
return {
|
|
43
|
+
broker: true,
|
|
44
|
+
publish(publications) {
|
|
45
|
+
for (const { topic, payload, retain } of publications)
|
|
46
|
+
link.publish(topic, text(payload), { retain: retain === true });
|
|
47
|
+
return null;
|
|
48
|
+
},
|
|
49
|
+
fromBroker(msg) {
|
|
50
|
+
const status = (0, adapter_1.objectField)(msg, "status");
|
|
51
|
+
const statusNode = Boolean(status && status.source && typeof status.source === "object");
|
|
52
|
+
const device = typeof msg.topic === "string" && (msg.topic === prefix || msg.topic.startsWith(prefix + "/"));
|
|
53
|
+
if (!statusNode && !device)
|
|
54
|
+
return false;
|
|
55
|
+
(0, bounded_trace_1.appendTrace)(node.context(), trace, [{ event: "input", outcome: "ignored", reason: "has-broker",
|
|
56
|
+
details: statusNode ? { status: String(status.text ?? "") } : { topic: msg.topic } }]);
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
// The Broker setting of an adapter: the mqtt-broker node, or null without
|
|
62
|
+
// one (as before 6.2, through MQTT in and out nodes).
|
|
63
|
+
function deviceBroker(RED, value) {
|
|
64
|
+
const id = typeof value === "string" ? value.trim() : "";
|
|
65
|
+
if (!id)
|
|
66
|
+
return null;
|
|
67
|
+
const broker = RED.nodes.getNode(id);
|
|
68
|
+
if (!broker)
|
|
69
|
+
throw new Error("its MQTT broker node isn't in the flows: choose one, or none to use MQTT in and out nodes");
|
|
70
|
+
return broker;
|
|
71
|
+
}
|
|
72
|
+
//# sourceMappingURL=device-side.js.map
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { Node, NodeAPI } from "node-red";
|
|
2
|
+
import { type TraceMetadata } from "../lib/bounded-trace";
|
|
3
|
+
import { type DiscoveryOwner } from "../lib/ha-discovery";
|
|
4
|
+
import type { BrokerLink, BrokerMessage } from "./mqtt-broker-link";
|
|
5
|
+
export interface RetainedPublication {
|
|
6
|
+
topic: string;
|
|
7
|
+
payload: unknown;
|
|
8
|
+
retain: boolean;
|
|
9
|
+
}
|
|
10
|
+
export interface HomeAssistantParts {
|
|
11
|
+
retainedTopics(): string[];
|
|
12
|
+
possible?: {
|
|
13
|
+
owner: DiscoveryOwner;
|
|
14
|
+
topics(): string[];
|
|
15
|
+
};
|
|
16
|
+
announcement(): RetainedPublication[];
|
|
17
|
+
stopping(): RetainedPublication[];
|
|
18
|
+
statusTopic: string | null;
|
|
19
|
+
setTopics?: string[];
|
|
20
|
+
onSet?(message: BrokerMessage): void;
|
|
21
|
+
onConnected?(): void;
|
|
22
|
+
}
|
|
23
|
+
export interface HomeAssistantDiscovery {
|
|
24
|
+
publish(publications: RetainedPublication[]): void;
|
|
25
|
+
}
|
|
26
|
+
export declare function homeAssistantDiscovery(RED: NodeAPI, node: Node, link: BrokerLink, parts: HomeAssistantParts, trace: TraceMetadata): HomeAssistantDiscovery;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.homeAssistantDiscovery = homeAssistantDiscovery;
|
|
4
|
+
const bounded_trace_1 = require("../lib/bounded-trace");
|
|
5
|
+
const ha_discovery_1 = require("../lib/ha-discovery");
|
|
6
|
+
const retained_topics_1 = require("../lib/retained-topics");
|
|
7
|
+
const adapter_1 = require("./adapter");
|
|
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 homeAssistantDiscovery(RED, node, link, parts, trace) {
|
|
19
|
+
const record = (event, outcome, reason, details = {}) => (0, bounded_trace_1.appendTrace)(node.context(), trace, [{ event, outcome, reason, details }]);
|
|
20
|
+
function publish(publications) {
|
|
21
|
+
for (const { topic, payload, retain } of publications)
|
|
22
|
+
link.publish(topic, text(payload), { retain });
|
|
23
|
+
}
|
|
24
|
+
const possible = () => parts.possible ? (0, ha_discovery_1.ownDiscoveryTopics)(parts.possible.owner, parts.possible.topics()) : [];
|
|
25
|
+
function announce(reason) {
|
|
26
|
+
publish([...(0, adapter_1.clearStaleRetained)(node, parts.retainedTopics(), "startup", possible()), ...parts.announcement()]);
|
|
27
|
+
record("home-assistant", "announced", reason);
|
|
28
|
+
}
|
|
29
|
+
link.onConnected(() => {
|
|
30
|
+
if (!parts.onConnected) {
|
|
31
|
+
announce("connected");
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
publish((0, adapter_1.clearStaleRetained)(node, parts.retainedTopics(), "startup", possible()));
|
|
35
|
+
record("home-assistant", "startup", "connected");
|
|
36
|
+
parts.onConnected();
|
|
37
|
+
});
|
|
38
|
+
if (parts.statusTopic) {
|
|
39
|
+
link.subscribe(parts.statusTopic, (message) => {
|
|
40
|
+
if (message.payload.trim().toLowerCase() === "online")
|
|
41
|
+
announce("home-assistant-online");
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
for (const topic of parts.setTopics ?? []) {
|
|
45
|
+
link.subscribe(topic, (message) => {
|
|
46
|
+
// A retained set message would replay on every reconnect.
|
|
47
|
+
if (message.retain)
|
|
48
|
+
record("set", "ignored", "retained-message", { topic: message.topic });
|
|
49
|
+
else
|
|
50
|
+
parts.onSet?.(message);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
node.on("close", (removed, done) => {
|
|
54
|
+
// Reaches the broker only while it is still connected, as in a deploy of
|
|
55
|
+
// modified nodes or flows; the broker's own Close and Will messages cover
|
|
56
|
+
// a full deploy, a restart and a crash.
|
|
57
|
+
publish((0, adapter_1.wasDeleted)(RED, node, removed)
|
|
58
|
+
? (0, retained_topics_1.retainedClears)((0, adapter_1.allRetainedTopics)(node, [...parts.retainedTopics(), ...possible()]))
|
|
59
|
+
: parts.stopping());
|
|
60
|
+
link.close(removed, done);
|
|
61
|
+
});
|
|
62
|
+
return { publish };
|
|
63
|
+
}
|
|
64
|
+
//# sourceMappingURL=home-assistant-discovery.js.map
|