@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.
- package/README.md +260 -46
- package/dist/lib/activity-lighting-controller.d.ts +1 -0
- package/dist/lib/activity-lighting-controller.js +37 -10
- package/dist/lib/avr-controller.d.ts +3 -0
- package/dist/lib/avr-controller.js +24 -2
- package/dist/lib/backlighting-controller.d.ts +5 -2
- package/dist/lib/backlighting-controller.js +27 -2
- package/dist/lib/cover-publishing.js +20 -6
- package/dist/lib/display-mode-controller.d.ts +3 -0
- package/dist/lib/display-mode-controller.js +43 -16
- package/dist/lib/esphome-sensor.d.ts +73 -0
- package/dist/lib/esphome-sensor.js +261 -0
- 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/occupancy-fusion.d.ts +1 -0
- package/dist/lib/occupancy-fusion.js +15 -2
- 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 +69 -0
- package/dist/lib/scene.js +223 -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/source-capability-controller.d.ts +4 -1
- package/dist/lib/source-capability-controller.js +49 -2
- package/dist/lib/target-observation.d.ts +14 -0
- package/dist/lib/target-observation.js +22 -0
- package/dist/lib/theatre-activity-controller.d.ts +5 -0
- package/dist/lib/theatre-activity-controller.js +91 -12
- 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.d.ts +2 -0
- package/dist/lib/theatre-state.js +13 -1
- package/dist/lib/wled.d.ts +48 -0
- package/dist/lib/wled.js +105 -0
- package/dist/lib/z2m-power.d.ts +38 -0
- package/dist/lib/z2m-power.js +83 -0
- package/dist/lib/z2m-sensor.d.ts +37 -0
- package/dist/lib/z2m-sensor.js +88 -0
- package/dist/nodes/apollo-r-pro-1.d.ts +2 -0
- package/dist/nodes/apollo-r-pro-1.html +80 -0
- package/dist/nodes/apollo-r-pro-1.js +4 -0
- package/dist/nodes/avr-controller.html +82 -21
- package/dist/nodes/avr-controller.js +24 -12
- package/dist/nodes/backlighting-controller.js +3 -2
- 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 +21 -0
- package/dist/nodes/device-side.js +76 -0
- package/dist/nodes/display-mode-controller.js +11 -2
- package/dist/nodes/esphome-sensor.d.ts +2 -0
- package/dist/nodes/esphome-sensor.js +77 -0
- package/dist/nodes/everything-presence-one.d.ts +2 -0
- package/dist/nodes/everything-presence-one.html +78 -0
- package/dist/nodes/everything-presence-one.js +4 -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 +86 -20
- package/dist/nodes/occupancy-fusion.js +25 -12
- 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 +247 -0
- package/dist/nodes/scene.js +128 -0
- package/dist/nodes/shelly-cover.html +47 -2
- package/dist/nodes/shelly-cover.js +19 -4
- package/dist/nodes/shelly-switch.html +76 -6
- package/dist/nodes/shelly-switch.js +32 -7
- package/dist/nodes/source-capability-controller.js +3 -2
- package/dist/nodes/theatre-activity-controller.html +4 -2
- package/dist/nodes/theatre-state-contract.html +72 -9
- package/dist/nodes/theatre-state-contract.js +33 -14
- package/dist/nodes/wled.d.ts +3 -0
- package/dist/nodes/wled.html +56 -0
- package/dist/nodes/wled.js +53 -0
- package/dist/nodes/z2m-cover.html +47 -2
- package/dist/nodes/z2m-cover.js +21 -4
- package/dist/nodes/z2m-light.html +49 -3
- package/dist/nodes/z2m-light.js +40 -4
- package/dist/nodes/z2m-power.d.ts +3 -0
- package/dist/nodes/z2m-power.html +58 -0
- package/dist/nodes/z2m-power.js +53 -0
- package/dist/nodes/z2m-sensor.d.ts +3 -0
- package/dist/nodes/z2m-sensor.html +69 -0
- package/dist/nodes/z2m-sensor.js +62 -0
- 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 +167 -0
- package/examples/Shelly cover.json +36 -105
- package/examples/Shelly switch.json +52 -148
- package/package.json +8 -2
|
@@ -1,6 +1,31 @@
|
|
|
1
1
|
<script type="text/javascript">
|
|
2
2
|
(function () {
|
|
3
3
|
function on(value) { return value === true || value === "true"; }
|
|
4
|
+
// Its Publish choice (off, state or discovery), from the open dialog or
|
|
5
|
+
// its saved settings: a node saved before the choice reads its two old
|
|
6
|
+
// settings (lib/home-assistant-setting.ts).
|
|
7
|
+
function publishChoice(node) {
|
|
8
|
+
function on(value) { return value === true || value === "true"; }
|
|
9
|
+
var select = $("#node-input-homeAssistant");
|
|
10
|
+
if (select.length) return select.val();
|
|
11
|
+
if (node.homeAssistant === "off" || node.homeAssistant === "state" || node.homeAssistant === "discovery") return node.homeAssistant;
|
|
12
|
+
return !on(node.publishState) ? "off" : on(node.discovery) ? "discovery" : "state";
|
|
13
|
+
}
|
|
14
|
+
// Whether it publishes through its output, as before 6.2: a node saved
|
|
15
|
+
// before the Publish choice that published, or one opened and saved since
|
|
16
|
+
// without a broker (publishesThroughOutput). It needs no broker until one
|
|
17
|
+
// is chosen; from saved values only, not the open dialog.
|
|
18
|
+
function throughOutput(node) {
|
|
19
|
+
if (node.publishesThroughOutput === true) return true;
|
|
20
|
+
return (node.homeAssistant === null || node.homeAssistant === undefined) && (node.publishState === true || node.publishState === "true");
|
|
21
|
+
}
|
|
22
|
+
// Its area is needed while it publishes, and for a node saved before the
|
|
23
|
+
// choice whenever either old setting was on, as the runtime checks.
|
|
24
|
+
function needsArea(node) {
|
|
25
|
+
function on(value) { return value === true || value === "true"; }
|
|
26
|
+
var saved = !$("#node-input-homeAssistant").length && (node.homeAssistant === null || node.homeAssistant === undefined);
|
|
27
|
+
return publishChoice(node) !== "off" || (saved && (on(node.publishState) || on(node.discovery)));
|
|
28
|
+
}
|
|
4
29
|
RED.nodes.registerType("home-motion-lighting-policy", {
|
|
5
30
|
category: "home automation",
|
|
6
31
|
color: "#d8bfd8",
|
|
@@ -10,10 +35,7 @@
|
|
|
10
35
|
name: {value: "_DEFAULT_"},
|
|
11
36
|
// Required only when the node publishes, and then with a letter or
|
|
12
37
|
// digit, from which its topics and IDs are made.
|
|
13
|
-
area: {value: "", validate: function (v) {
|
|
14
|
-
var publishing = $("#node-input-publishState").length ? $("#node-input-publishState").prop("checked") : on(this.publishState);
|
|
15
|
-
return !publishing || /[a-z0-9]/i.test(String(v || ""));
|
|
16
|
-
}},
|
|
38
|
+
area: {value: "", validate: function (v) { return !needsArea(this) || /[a-z0-9]/i.test(String(v || "")); }},
|
|
17
39
|
onTargets: {value: "[\"main\"]", required: true, validate: function (v) { try { JSON.parse(v); return true; } catch (e) { return false; } }},
|
|
18
40
|
offTargets: {value: "[\"main\"]", required: true, validate: function (v) { try { JSON.parse(v); return true; } catch (e) { return false; } }},
|
|
19
41
|
defaultDelaySeconds: {value: 120, required: true, validate: RED.validators.number()},
|
|
@@ -22,8 +44,22 @@
|
|
|
22
44
|
// motion lighting for good.
|
|
23
45
|
maxPauseSeconds: {value: 600, required: true, validate: function (v) { return RED.validators.number()(v) && Number(v) >= 1; }},
|
|
24
46
|
// Motion published nothing before these settings, so both start off.
|
|
47
|
+
// Publish: off, state or discovery. null means "not set": a node saved
|
|
48
|
+
// before this setting, read from the two below, kept with no rows.
|
|
49
|
+
homeAssistant: {value: null},
|
|
50
|
+
// Node-RED's MQTT broker node, through which it publishes and reads its set
|
|
51
|
+
// topics itself. Needed unless Publish is "Don't publish". The editor offers
|
|
52
|
+
// the first broker when the dialog is opened and saved, not when the node
|
|
53
|
+
// is dropped.
|
|
54
|
+
broker: {value: "", type: "mqtt-broker", required: false, validate: function (v) {
|
|
55
|
+
return publishChoice(this) === "off" || (Boolean(v) && v !== "_ADD_") || throughOutput(this) ? true : "choose a broker, or set Publish to Don't publish";
|
|
56
|
+
}},
|
|
25
57
|
publishState: {value: false},
|
|
26
58
|
discovery: {value: false},
|
|
59
|
+
// true for a node that publishes through its output, as before 6.2, and
|
|
60
|
+
// was opened and saved without a broker: it stays valid until a broker is
|
|
61
|
+
// chosen. No form row.
|
|
62
|
+
publishesThroughOutput: {value: false},
|
|
27
63
|
discoveryPrefix: {value: "homeassistant", required: true, validate: function (v) { var prefix = String(v || "").trim().replace(/^\/+|\/+$/g, ""); return Boolean(prefix) && !/[+#]/.test(prefix); }},
|
|
28
64
|
// How Home Assistant IDs are named; motion has no older entities, so not
|
|
29
65
|
// set also means "node".
|
|
@@ -34,18 +70,26 @@
|
|
|
34
70
|
this.name = "";
|
|
35
71
|
this.idScheme = "node";
|
|
36
72
|
},
|
|
73
|
+
oneditsave: function () {
|
|
74
|
+
// Through its output until a broker is chosen (read before the
|
|
75
|
+
// dialog's values are applied).
|
|
76
|
+
var broker = $("#node-input-broker").val();
|
|
77
|
+
this.publishesThroughOutput = throughOutput(this) && (!broker || broker === "_ADD_");
|
|
78
|
+
},
|
|
37
79
|
oneditprepare: function () {
|
|
38
|
-
$("#node-input-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
function
|
|
42
|
-
var
|
|
43
|
-
|
|
44
|
-
$("#node-input-
|
|
45
|
-
$("
|
|
80
|
+
$("#node-input-homeAssistant").val(publishChoice(this));
|
|
81
|
+
// The broker only while it publishes, the prefix only with discovery.
|
|
82
|
+
var outputBefore = throughOutput(this);
|
|
83
|
+
function publishRows() {
|
|
84
|
+
var broker = $("#node-input-broker").val();
|
|
85
|
+
$(".publish-through-output").toggle($("#node-input-homeAssistant").val() !== "off" && outputBefore && (!broker || broker === "_ADD_"));
|
|
86
|
+
var choice = $("#node-input-homeAssistant").val();
|
|
87
|
+
$(".publish-broker").toggle(choice !== "off");
|
|
88
|
+
$(".publish-discovery").toggle(choice === "discovery");
|
|
89
|
+
$("#node-input-area, #node-input-broker").trigger("change");
|
|
46
90
|
}
|
|
47
|
-
$("#node-input-
|
|
48
|
-
|
|
91
|
+
$("#node-input-homeAssistant, #node-input-broker").on("change", publishRows);
|
|
92
|
+
publishRows();
|
|
49
93
|
},
|
|
50
94
|
inputs: 1,
|
|
51
95
|
outputs: 2,
|
|
@@ -64,9 +108,20 @@
|
|
|
64
108
|
<div class="form-row"><label for="node-input-defaultDelaySeconds">Vacancy seconds</label><input type="number" id="node-input-defaultDelaySeconds"></div>
|
|
65
109
|
<div class="form-row"><label for="node-input-defaultIlluminanceCutoff">Lux cutoff</label><input type="number" id="node-input-defaultIlluminanceCutoff"></div>
|
|
66
110
|
<div class="form-row"><label for="node-input-maxPauseSeconds">Maximum pause (s)</label><input type="number" id="node-input-maxPauseSeconds"></div>
|
|
67
|
-
<div class="form-row"
|
|
68
|
-
|
|
69
|
-
|
|
111
|
+
<div class="form-row">
|
|
112
|
+
<label for="node-input-homeAssistant">Publish</label>
|
|
113
|
+
<select id="node-input-homeAssistant">
|
|
114
|
+
<option value="off">Don't publish</option>
|
|
115
|
+
<option value="state">MQTT state</option>
|
|
116
|
+
<option value="discovery">MQTT state and Home Assistant discovery</option>
|
|
117
|
+
</select>
|
|
118
|
+
</div>
|
|
119
|
+
<div class="form-row publish-broker"><label for="node-input-broker">Broker</label><input type="text" id="node-input-broker"></div>
|
|
120
|
+
<div class="form-tips publish-broker">With a broker, it publishes and reads its set topics itself: an MQTT out on its
|
|
121
|
+
publication output, an MQTT in on its set topics, and a Status node are no longer needed and can be deleted.</div>
|
|
122
|
+
<div class="form-tips publish-through-output">This node publishes through its output, as before 6.2, and keeps
|
|
123
|
+
working that way. Choose a Broker to let it publish itself.</div>
|
|
124
|
+
<div class="form-row publish-discovery"><label for="node-input-discoveryPrefix">Discovery prefix</label><input type="text" id="node-input-discoveryPrefix"></div>
|
|
70
125
|
</script>
|
|
71
126
|
|
|
72
127
|
<script type="text/html" data-help-name="home-motion-lighting-policy">
|
|
@@ -89,7 +144,9 @@
|
|
|
89
144
|
<dt>pause</dt><dd><code>true</code> pauses motion lighting, <code>false</code> releases it, for the owner in
|
|
90
145
|
<code>msg.owner</code> (for example a cover's name), which is required. A pause lasts at most the
|
|
91
146
|
<b>Maximum pause</b> unless sent again, so a lost release can't stop motion lighting for good. Motion lighting acts only while no owner holds a pause,
|
|
92
|
-
and a pause cancels a pending vacancy timer, as switching it off does. A
|
|
147
|
+
and a pause cancels a pending vacancy timer, as switching it off does. A vacancy while paused or switched
|
|
148
|
+
off isn't forgotten: when motion resumes (every pause released or run out, and both enables on) in a room
|
|
149
|
+
still vacant, the vacancy timer starts then, and the off intent names when the room went vacant. A pause never changes the enables,
|
|
93
150
|
which are the user's own: wire a cover's <i>moving</i> output here to pause motion while it moves. Any value but
|
|
94
151
|
<code>true</code> or <code>false</code> is refused, as for the enables.</dd>
|
|
95
152
|
</dl>
|
|
@@ -105,7 +162,7 @@
|
|
|
105
162
|
<h3>Publishing</h3>
|
|
106
163
|
<p>Off unless switched on, and then named after the <b>Area</b>, which is needed.</p>
|
|
107
164
|
<dl class="message-properties">
|
|
108
|
-
<dt>Publish state
|
|
165
|
+
<dt>Publish: MQTT state</dt><dd>whether this node's motion lighting is enabled, retained <code>ON</code> or
|
|
109
166
|
<code>OFF</code> on <code>home/<area>/motion/enabled/state</code>, and its vacancy delay in seconds on
|
|
110
167
|
<code>home/<area>/motion/delay/state</code>, with <code>online</code> or <code>offline</code> on
|
|
111
168
|
<code>home/<area>/motion/availability</code>. It acts on <code>ON</code>/<code>OFF</code> sent to
|
|
@@ -114,8 +171,8 @@
|
|
|
114
171
|
<code>home/<area>/motion/+/set</code> into this node. The shared <code>global-enabled</code> is not
|
|
115
172
|
published, and the switch shows this node's enable only, so motion can still be off everywhere while it
|
|
116
173
|
shows on.</dd>
|
|
117
|
-
<dt>Home Assistant discovery</dt><dd>a motion lighting switch and a vacancy delay number (in the device's
|
|
118
|
-
configuration). It
|
|
174
|
+
<dt>Publish: MQTT state and Home Assistant discovery</dt><dd>a motion lighting switch and a vacancy delay number (in the device's
|
|
175
|
+
configuration). It includes the MQTT state, which they read.</dd>
|
|
119
176
|
</dl>
|
|
120
177
|
<p>These go on the second output; wire it to an MQTT out node. The node announces itself once the flows have
|
|
121
178
|
started, and again when it receives <code>msg.source = "startup"</code>.</p>
|
|
@@ -124,8 +181,16 @@
|
|
|
124
181
|
<li>Lighting intents, for the room lighting controller.</li>
|
|
125
182
|
<li>MQTT publications, when publishing is on.</li>
|
|
126
183
|
</ol>
|
|
184
|
+
<h3>Broker</h3>
|
|
185
|
+
<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
|
|
186
|
+
broker connects, reconnects included, and each time Home Assistant comes back (<code>online</code> on
|
|
187
|
+
<code><prefix>/status</code>), so its entities come back by themselves.
|
|
188
|
+
Each connect also runs its startup message, as a Status node's "connected" did without a broker. Its publication output then sends
|
|
189
|
+
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.
|
|
190
|
+
Opening and saving the node offers the first broker in your flows. A node saved before 6.2 that publishes keeps
|
|
191
|
+
publishing on its output, as below, and needs no broker until you choose one.</p>
|
|
127
192
|
<h3>When MQTT out connects</h3>
|
|
128
|
-
<p>
|
|
193
|
+
<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
|
|
129
194
|
"connected", this node takes it as its startup message (<code>msg.source = "startup"</code>) and announces itself again:
|
|
130
195
|
Node-RED's MQTT broker node drops what it is given before it connects, so what the node sends as the flows
|
|
131
196
|
start is lost on a first start, a full deploy or a restart. Any other status message is ignored.</p>
|
|
@@ -6,8 +6,11 @@ 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
|
|
9
|
+
const publishing_side_1 = require("./publishing-side");
|
|
10
|
+
const home_assistant_setting_1 = require("../lib/home-assistant-setting");
|
|
10
11
|
const refused_enable_1 = require("./refused-enable");
|
|
12
|
+
// The longest wait setTimeout takes: anything longer fires at once.
|
|
13
|
+
const MAX_TIMEOUT_MS = 2 ** 31 - 1;
|
|
11
14
|
const registerMotionPolicy = (RED) => {
|
|
12
15
|
function HomeMotionLightingPolicy(config) {
|
|
13
16
|
RED.nodes.createNode(this, config);
|
|
@@ -15,21 +18,23 @@ const registerMotionPolicy = (RED) => {
|
|
|
15
18
|
const context = node.context();
|
|
16
19
|
const globalContext = context.global;
|
|
17
20
|
let offTimer;
|
|
21
|
+
let pauseTimer;
|
|
18
22
|
let policyConfig;
|
|
19
23
|
let publishing;
|
|
24
|
+
let broker;
|
|
20
25
|
try {
|
|
21
26
|
const area = (0, adapter_1.optionalArea)(config);
|
|
22
27
|
policyConfig = { area, ...(0, motion_policy_1.normaliseMotionConfig)(config) };
|
|
23
28
|
// Off unless switched on, as motion published nothing before; there are
|
|
24
29
|
// no older entities to keep, so not set names them after the node.
|
|
25
|
-
const
|
|
26
|
-
|
|
27
|
-
if (publishState || discovery)
|
|
30
|
+
const publish = (0, home_assistant_setting_1.publishingChoice)(config, { publishState: false, discovery: false });
|
|
31
|
+
if (publish.needsArea)
|
|
28
32
|
(0, topic_names_1.publishingArea)(area, true);
|
|
33
|
+
broker = (0, publishing_side_1.brokerSetting)(RED, config.broker, publish.publishState);
|
|
29
34
|
publishing = {
|
|
30
35
|
area,
|
|
31
|
-
publishState,
|
|
32
|
-
discovery,
|
|
36
|
+
publishState: publish.publishState,
|
|
37
|
+
discovery: publish.discovery,
|
|
33
38
|
discoveryPrefix: (0, ha_discovery_1.discoveryPrefixSetting)(config.discoveryPrefix),
|
|
34
39
|
nodeId: node.id,
|
|
35
40
|
idScheme: (0, settings_1.optionalChoiceSetting)(config.idScheme, "ID scheme", ha_discovery_1.ID_SCHEMES) ?? "node"
|
|
@@ -55,6 +60,21 @@ const registerMotionPolicy = (RED) => {
|
|
|
55
60
|
process({ source: "off-timer", generation }, node.send.bind(node));
|
|
56
61
|
}), delayMs);
|
|
57
62
|
}
|
|
63
|
+
// Wakes the policy when the first pause runs out, so motion resumes then
|
|
64
|
+
// (and acts on a vacancy meanwhile) rather than at the next message. A
|
|
65
|
+
// wait longer than setTimeout takes (about 24.8 days) is split: it wakes
|
|
66
|
+
// at the longest and arms again.
|
|
67
|
+
function armPauseTimer(state) {
|
|
68
|
+
clearTimeout(pauseTimer);
|
|
69
|
+
pauseTimer = undefined;
|
|
70
|
+
const ends = (state?.pausedBy ?? []).map(({ until }) => until).filter(Number.isFinite);
|
|
71
|
+
if (!ends.length)
|
|
72
|
+
return;
|
|
73
|
+
pauseTimer = setTimeout((0, adapter_1.guarded)(node, () => {
|
|
74
|
+
pauseTimer = undefined;
|
|
75
|
+
process({ source: "pause-check" }, node.send.bind(node));
|
|
76
|
+
}), Math.min(MAX_TIMEOUT_MS, Math.max(0, Math.min(...ends) - Date.now()) + 1));
|
|
77
|
+
}
|
|
58
78
|
// The enable state and delay in use, by the reducer's own rule: a saved
|
|
59
79
|
// delay stands only while the setting it was based on is unchanged.
|
|
60
80
|
function savedPolicyState() {
|
|
@@ -64,14 +84,18 @@ const registerMotionPolicy = (RED) => {
|
|
|
64
84
|
// intents the room controller reads on the first. Available, and clear
|
|
65
85
|
// of what it no longer publishes, as soon as it runs; offline as it
|
|
66
86
|
// stops; nothing left behind when deleted.
|
|
67
|
-
const
|
|
87
|
+
const trace = { area: policyConfig.area, component: "motion-policy" };
|
|
88
|
+
const lifecycle = (0, publishing_side_1.publishingSide)(RED, node, broker, {
|
|
68
89
|
retainedTopics: () => (0, motion_publishing_1.motionRetainedTopics)(publishing),
|
|
69
90
|
possible: { owner: publishing, topics: () => (0, publishing_node_1.publishesState)(publishing) ? (0, motion_publishing_1.motionRetainedTopics)({ ...publishing, publishState: true, discovery: true }) : [] },
|
|
70
91
|
starting: () => (0, motion_publishing_1.motionStartingOutputs)(publishing, savedPolicyState()),
|
|
71
92
|
stopping: () => (0, motion_publishing_1.motionStoppingOutputs)(publishing),
|
|
72
93
|
output: 1,
|
|
73
|
-
outputs: 2
|
|
74
|
-
|
|
94
|
+
outputs: 2,
|
|
95
|
+
startupMessage: { source: "startup" },
|
|
96
|
+
statusTopic: publishing.discovery ? publishing.discoveryPrefix + "/status" : null,
|
|
97
|
+
setTopics: (0, publishing_node_1.publishesState)(publishing) ? [(0, motion_publishing_1.motionEnabledTopic)(publishing.area, "set"), (0, motion_publishing_1.motionDelayTopic)(publishing.area, "set")] : []
|
|
98
|
+
}, trace, (msg, send) => handle(msg, send));
|
|
75
99
|
function process(input, send, inboundTraces = null) {
|
|
76
100
|
const globalEnabled = globalContext.get("MOTION_ENABLED_STATE") !== false;
|
|
77
101
|
const before = savedPolicyState();
|
|
@@ -102,6 +126,7 @@ const registerMotionPolicy = (RED) => {
|
|
|
102
126
|
}
|
|
103
127
|
updateStatus(result.status);
|
|
104
128
|
showRefused(result.traces);
|
|
129
|
+
armPauseTimer(result.state);
|
|
105
130
|
return result;
|
|
106
131
|
}
|
|
107
132
|
const showRefused = (0, refused_enable_1.refusedEnables)(node);
|
|
@@ -110,13 +135,14 @@ const registerMotionPolicy = (RED) => {
|
|
|
110
135
|
// off-timer is re-armed. Without one, it starts with nothing saved, and
|
|
111
136
|
// vacancy starts now.
|
|
112
137
|
const savedState = context.get("state");
|
|
138
|
+
armPauseTimer(savedState);
|
|
113
139
|
if (typeof savedState?.offTimerDueAt === "number") {
|
|
114
140
|
armOffTimer(Math.max(0, savedState.offTimerDueAt - Date.now()), Number(savedState.offTimerGeneration) || 0);
|
|
115
141
|
}
|
|
116
142
|
else if (savedState === undefined) {
|
|
117
143
|
process({ source: "cold-start" }, node.send.bind(node));
|
|
118
144
|
}
|
|
119
|
-
|
|
145
|
+
function handle(msg, send) {
|
|
120
146
|
if (msg.source === "startup") {
|
|
121
147
|
lifecycle.startup(send, (0, motion_publishing_1.motionStartingOutputs)(publishing, savedPolicyState()));
|
|
122
148
|
return;
|
|
@@ -125,9 +151,16 @@ const registerMotionPolicy = (RED) => {
|
|
|
125
151
|
// else is an input as sent, with a pause's owner.
|
|
126
152
|
const inbound = (0, motion_publishing_1.motionInbound)(msg, publishing);
|
|
127
153
|
process(inbound?.input ?? { source: String(msg.source ?? ""), payload: msg.payload, owner: typeof msg.owner === "string" ? msg.owner : undefined }, send, inbound);
|
|
128
|
-
}
|
|
154
|
+
}
|
|
155
|
+
(0, adapter_1.onInput)(node, (msg, send) => {
|
|
156
|
+
// With a broker, its set topics come from the broker: an MQTT in or a
|
|
157
|
+
// Status node still wired in changes nothing.
|
|
158
|
+
if (!lifecycle.fromBroker(msg))
|
|
159
|
+
handle(msg, send);
|
|
160
|
+
}, lifecycle.startupOption({ source: "startup" }));
|
|
129
161
|
node.on("close", (_removed, done) => {
|
|
130
162
|
clearTimeout(offTimer);
|
|
163
|
+
clearTimeout(pauseTimer);
|
|
131
164
|
done();
|
|
132
165
|
});
|
|
133
166
|
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export declare const MQTT_CONNECTED = "node-red:common.status.connected";
|
|
2
|
+
export interface BrokerNode {
|
|
3
|
+
connected?: boolean;
|
|
4
|
+
register(user: BrokerUser): void;
|
|
5
|
+
deregister(user: BrokerUser, done: () => void, autoDisconnect?: boolean): void;
|
|
6
|
+
subscribe(topic: string, options: {
|
|
7
|
+
qos: number;
|
|
8
|
+
}, callback: (topic: string, payload: Buffer | string, packet?: {
|
|
9
|
+
retain?: boolean;
|
|
10
|
+
}) => void, ref: string): void;
|
|
11
|
+
unsubscribe(topic: string, ref: string, removeClientSubscription?: boolean): void;
|
|
12
|
+
publish(msg: {
|
|
13
|
+
topic: string;
|
|
14
|
+
payload: string;
|
|
15
|
+
qos: number;
|
|
16
|
+
retain: boolean;
|
|
17
|
+
}, done?: (error?: unknown) => void): void;
|
|
18
|
+
}
|
|
19
|
+
interface BrokerUser {
|
|
20
|
+
id: string;
|
|
21
|
+
status(status: {
|
|
22
|
+
text?: unknown;
|
|
23
|
+
}): void;
|
|
24
|
+
}
|
|
25
|
+
export interface BrokerMessage {
|
|
26
|
+
topic: string;
|
|
27
|
+
payload: string;
|
|
28
|
+
retain: boolean;
|
|
29
|
+
}
|
|
30
|
+
export interface BrokerLink {
|
|
31
|
+
connected(): boolean;
|
|
32
|
+
onConnected(callback: () => void): void;
|
|
33
|
+
publish(topic: string, payload: string, options?: {
|
|
34
|
+
retain?: boolean;
|
|
35
|
+
qos?: number;
|
|
36
|
+
}): void;
|
|
37
|
+
subscribe(topic: string, callback: (message: BrokerMessage) => void): void;
|
|
38
|
+
close(removed: boolean, done: () => void): void;
|
|
39
|
+
}
|
|
40
|
+
export declare function brokerLink(broker: BrokerNode, ownerId: string, onError?: (error: unknown) => void): BrokerLink;
|
|
41
|
+
export {};
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MQTT_CONNECTED = void 0;
|
|
4
|
+
exports.brokerLink = brokerLink;
|
|
5
|
+
// The one place that touches Node-RED's MQTT broker configuration node
|
|
6
|
+
// (mqtt-broker), so a node can publish and subscribe itself rather than
|
|
7
|
+
// through MQTT in and out nodes. None of this is the broker node's
|
|
8
|
+
// documented API, though Node-RED's own MQTT in and out nodes use exactly
|
|
9
|
+
// these calls (Node-RED 5.0.10, @node-red/nodes/core/network/10-mqtt.js):
|
|
10
|
+
// - register(user) adds the user, and connects with the first one; on every
|
|
11
|
+
// connect, connecting and disconnect the broker calls user.status() with
|
|
12
|
+
// "node-red:common.status.connected" and the like (:381-420, :739). So the
|
|
13
|
+
// link registers a small proxy rather than its node, whose own status the
|
|
14
|
+
// broker would otherwise overwrite, and turns "connected" into its
|
|
15
|
+
// onConnected callbacks. A user registered while the broker is already
|
|
16
|
+
// connected gets no call, so the link checks `connected` itself.
|
|
17
|
+
// - deregister(user, done, autoDisconnect) (:752), as MQTT in and out pass
|
|
18
|
+
// it: `removed`.
|
|
19
|
+
// - subscribe(topic, options, callback, ref) (:987) and unsubscribe(topic,
|
|
20
|
+
// ref, remove) (:1101): the callback gets (topic, payload Buffer, packet),
|
|
21
|
+
// and the broker subscribes again by itself after a reconnect.
|
|
22
|
+
// - publish(msg, done) (:1149) drops anything sent while disconnected, with
|
|
23
|
+
// no error. So the link never queues: callers announce again in
|
|
24
|
+
// onConnected, and what is current is what is sent.
|
|
25
|
+
exports.MQTT_CONNECTED = "node-red:common.status.connected";
|
|
26
|
+
function brokerLink(broker, ownerId, onError = () => { }) {
|
|
27
|
+
const callbacks = [];
|
|
28
|
+
const topics = new Set();
|
|
29
|
+
const ref = ownerId + "-home-automation";
|
|
30
|
+
let closed = false;
|
|
31
|
+
const run = () => {
|
|
32
|
+
for (const callback of callbacks) {
|
|
33
|
+
try {
|
|
34
|
+
callback();
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
onError(error);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
// Stands in for the node: only status() is ever called on it.
|
|
42
|
+
const proxy = {
|
|
43
|
+
id: ref,
|
|
44
|
+
status(status) {
|
|
45
|
+
if (!closed && status && status.text === exports.MQTT_CONNECTED)
|
|
46
|
+
run();
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
broker.register(proxy);
|
|
50
|
+
return {
|
|
51
|
+
connected: () => broker.connected === true,
|
|
52
|
+
onConnected(callback) {
|
|
53
|
+
callbacks.push(callback);
|
|
54
|
+
if (broker.connected === true) {
|
|
55
|
+
setImmediate(() => {
|
|
56
|
+
if (closed)
|
|
57
|
+
return;
|
|
58
|
+
try {
|
|
59
|
+
callback();
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
onError(error);
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
publish(topic, payload, { retain = false, qos = 0 } = {}) {
|
|
68
|
+
if (closed)
|
|
69
|
+
return;
|
|
70
|
+
broker.publish({ topic, payload, qos, retain }, (error) => {
|
|
71
|
+
if (error)
|
|
72
|
+
onError(error);
|
|
73
|
+
});
|
|
74
|
+
},
|
|
75
|
+
subscribe(topic, callback) {
|
|
76
|
+
topics.add(topic);
|
|
77
|
+
broker.subscribe(topic, { qos: 0 }, (received, payload, packet) => {
|
|
78
|
+
if (closed)
|
|
79
|
+
return;
|
|
80
|
+
callback({ topic: received, payload: Buffer.isBuffer(payload) ? payload.toString("utf8") : String(payload ?? ""), retain: packet?.retain === true });
|
|
81
|
+
}, ref);
|
|
82
|
+
},
|
|
83
|
+
close(removed, done) {
|
|
84
|
+
closed = true;
|
|
85
|
+
for (const topic of topics)
|
|
86
|
+
broker.unsubscribe(topic, ref, removed);
|
|
87
|
+
broker.deregister(proxy, done, removed);
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
//# sourceMappingURL=mqtt-broker-link.js.map
|
|
@@ -1,6 +1,31 @@
|
|
|
1
1
|
<script type="text/javascript">
|
|
2
2
|
(function () {
|
|
3
3
|
function on(value) { return value === true || value === "true"; }
|
|
4
|
+
// Its Publish choice (off, state or discovery), from the open dialog or
|
|
5
|
+
// its saved settings: a node saved before the choice reads its two old
|
|
6
|
+
// settings (lib/home-assistant-setting.ts).
|
|
7
|
+
function publishChoice(node) {
|
|
8
|
+
function on(value) { return value === true || value === "true"; }
|
|
9
|
+
var select = $("#node-input-homeAssistant");
|
|
10
|
+
if (select.length) return select.val();
|
|
11
|
+
if (node.homeAssistant === "off" || node.homeAssistant === "state" || node.homeAssistant === "discovery") return node.homeAssistant;
|
|
12
|
+
return !on(node.publishState) ? "off" : on(node.discovery) ? "discovery" : "state";
|
|
13
|
+
}
|
|
14
|
+
// Whether it publishes through its output, as before 6.2: a node saved
|
|
15
|
+
// before the Publish choice that published, or one opened and saved since
|
|
16
|
+
// without a broker (publishesThroughOutput). It needs no broker until one
|
|
17
|
+
// is chosen; from saved values only, not the open dialog.
|
|
18
|
+
function throughOutput(node) {
|
|
19
|
+
if (node.publishesThroughOutput === true) return true;
|
|
20
|
+
return (node.homeAssistant === null || node.homeAssistant === undefined) && (node.publishState === true || node.publishState === "true");
|
|
21
|
+
}
|
|
22
|
+
// Its area is needed while it publishes, and for a node saved before the
|
|
23
|
+
// choice whenever either old setting was on, as the runtime checks.
|
|
24
|
+
function needsArea(node) {
|
|
25
|
+
function on(value) { return value === true || value === "true"; }
|
|
26
|
+
var saved = !$("#node-input-homeAssistant").length && (node.homeAssistant === null || node.homeAssistant === undefined);
|
|
27
|
+
return publishChoice(node) !== "off" || (saved && (on(node.publishState) || on(node.discovery)));
|
|
28
|
+
}
|
|
4
29
|
RED.nodes.registerType("home-occupancy-fusion", {
|
|
5
30
|
category: "home automation",
|
|
6
31
|
color: "#c7e9c0",
|
|
@@ -10,14 +35,25 @@
|
|
|
10
35
|
name: {value: "_DEFAULT_"},
|
|
11
36
|
// Required only when the node publishes, and then with a letter or
|
|
12
37
|
// digit, from which its topics and IDs are made.
|
|
13
|
-
area: {value: "", validate: function (v) {
|
|
14
|
-
var publishing = $("#node-input-publishState").length ? $("#node-input-publishState").prop("checked") : on(this.publishState);
|
|
15
|
-
return !publishing || /[a-z0-9]/i.test(String(v || ""));
|
|
16
|
-
}},
|
|
38
|
+
area: {value: "", validate: function (v) { return !needsArea(this) || /[a-z0-9]/i.test(String(v || "")); }},
|
|
17
39
|
presenceMode: {value: "none", required: true},
|
|
18
40
|
// Occupancy published nothing before these settings, so both start off.
|
|
41
|
+
// Publish: off, state or discovery. null means "not set": a node saved
|
|
42
|
+
// before this setting, read from the two below, kept with no rows.
|
|
43
|
+
homeAssistant: {value: null},
|
|
44
|
+
// Node-RED's MQTT broker node, through which it publishes and reads its set
|
|
45
|
+
// topics itself. Needed unless Publish is "Don't publish". The editor offers
|
|
46
|
+
// the first broker when the dialog is opened and saved, not when the node
|
|
47
|
+
// is dropped.
|
|
48
|
+
broker: {value: "", type: "mqtt-broker", required: false, validate: function (v) {
|
|
49
|
+
return publishChoice(this) === "off" || (Boolean(v) && v !== "_ADD_") || throughOutput(this) ? true : "choose a broker, or set Publish to Don't publish";
|
|
50
|
+
}},
|
|
19
51
|
publishState: {value: false},
|
|
20
52
|
discovery: {value: false},
|
|
53
|
+
// true for a node that publishes through its output, as before 6.2, and
|
|
54
|
+
// was opened and saved without a broker: it stays valid until a broker is
|
|
55
|
+
// chosen. No form row.
|
|
56
|
+
publishesThroughOutput: {value: false},
|
|
21
57
|
discoveryPrefix: {value: "homeassistant", required: true, validate: function (v) { var prefix = String(v || "").trim().replace(/^\/+|\/+$/g, ""); return Boolean(prefix) && !/[+#]/.test(prefix); }},
|
|
22
58
|
// How Home Assistant IDs are named; occupancy has no older entities,
|
|
23
59
|
// so not set also means "node".
|
|
@@ -28,18 +64,26 @@
|
|
|
28
64
|
this.name = "";
|
|
29
65
|
this.idScheme = "node";
|
|
30
66
|
},
|
|
67
|
+
oneditsave: function () {
|
|
68
|
+
// Through its output until a broker is chosen (read before the
|
|
69
|
+
// dialog's values are applied).
|
|
70
|
+
var broker = $("#node-input-broker").val();
|
|
71
|
+
this.publishesThroughOutput = throughOutput(this) && (!broker || broker === "_ADD_");
|
|
72
|
+
},
|
|
31
73
|
oneditprepare: function () {
|
|
32
|
-
$("#node-input-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
function
|
|
36
|
-
var
|
|
37
|
-
|
|
38
|
-
$("#node-input-
|
|
39
|
-
$("
|
|
74
|
+
$("#node-input-homeAssistant").val(publishChoice(this));
|
|
75
|
+
// The broker only while it publishes, the prefix only with discovery.
|
|
76
|
+
var outputBefore = throughOutput(this);
|
|
77
|
+
function publishRows() {
|
|
78
|
+
var broker = $("#node-input-broker").val();
|
|
79
|
+
$(".publish-through-output").toggle($("#node-input-homeAssistant").val() !== "off" && outputBefore && (!broker || broker === "_ADD_"));
|
|
80
|
+
var choice = $("#node-input-homeAssistant").val();
|
|
81
|
+
$(".publish-broker").toggle(choice !== "off");
|
|
82
|
+
$(".publish-discovery").toggle(choice === "discovery");
|
|
83
|
+
$("#node-input-area, #node-input-broker").trigger("change");
|
|
40
84
|
}
|
|
41
|
-
$("#node-input-
|
|
42
|
-
|
|
85
|
+
$("#node-input-homeAssistant, #node-input-broker").on("change", publishRows);
|
|
86
|
+
publishRows();
|
|
43
87
|
},
|
|
44
88
|
inputs: 1,
|
|
45
89
|
outputs: 2,
|
|
@@ -60,22 +104,36 @@
|
|
|
60
104
|
<option value="hold">Hold occupancy (PIR + mmWave)</option>
|
|
61
105
|
</select>
|
|
62
106
|
</div>
|
|
63
|
-
<div class="form-row"
|
|
64
|
-
|
|
65
|
-
|
|
107
|
+
<div class="form-row">
|
|
108
|
+
<label for="node-input-homeAssistant">Publish</label>
|
|
109
|
+
<select id="node-input-homeAssistant">
|
|
110
|
+
<option value="off">Don't publish</option>
|
|
111
|
+
<option value="state">MQTT state</option>
|
|
112
|
+
<option value="discovery">MQTT state and Home Assistant discovery</option>
|
|
113
|
+
</select>
|
|
114
|
+
</div>
|
|
115
|
+
<div class="form-row publish-broker"><label for="node-input-broker">Broker</label><input type="text" id="node-input-broker"></div>
|
|
116
|
+
<div class="form-tips publish-broker">With a broker, it publishes and reads its set topics itself: an MQTT out on its
|
|
117
|
+
publication output, an MQTT in on its set topics, and a Status node are no longer needed and can be deleted.</div>
|
|
118
|
+
<div class="form-tips publish-through-output">This node publishes through its output, as before 6.2, and keeps
|
|
119
|
+
working that way. Choose a Broker to let it publish itself.</div>
|
|
120
|
+
<div class="form-row publish-discovery"><label for="node-input-discoveryPrefix">Discovery prefix</label><input type="text" id="node-input-discoveryPrefix"></div>
|
|
66
121
|
</script>
|
|
67
122
|
|
|
68
123
|
<script type="text/html" data-help-name="home-occupancy-fusion">
|
|
69
124
|
<p>Combines normalised <code>motion</code> and optional <code>presence</code> events into <code>msg.source = "occupancy"</code>.</p>
|
|
70
125
|
<p>PIR establishes occupancy. Presence can hold an established occupancy but cannot establish one by itself.</p>
|
|
126
|
+
<p>With <code>msg.baseline = true</code>, a motion or presence message is the sensor's state as Node-RED starts (such
|
|
127
|
+
as an ESPHome sensor's retained state): on makes the area occupied without sending anything, so no light is switched
|
|
128
|
+
on; off is read as usual, so an empty room starts the motion policy's vacancy countdown.</p>
|
|
71
129
|
<h3>Publishing</h3>
|
|
72
130
|
<p>Off unless switched on. Both are named after the <b>Area</b>, which is needed then; topics use it in lowercase,
|
|
73
131
|
with spaces and other characters as <code>_</code>.</p>
|
|
74
132
|
<dl class="message-properties">
|
|
75
|
-
<dt>Publish state
|
|
133
|
+
<dt>Publish: MQTT state</dt><dd>the occupancy, retained <code>ON</code> or <code>OFF</code>, on
|
|
76
134
|
<code>home/<area>/occupancy/state</code> whenever it changes, and <code>online</code> or
|
|
77
135
|
<code>offline</code> on <code>home/<area>/occupancy/availability</code>.</dd>
|
|
78
|
-
<dt>Home Assistant discovery</dt><dd>an occupancy binary sensor reading that state. It
|
|
136
|
+
<dt>Publish: MQTT state and Home Assistant discovery</dt><dd>an occupancy binary sensor reading that state. It includes the MQTT state, which it reads.</dd>
|
|
79
137
|
</dl>
|
|
80
138
|
<p>These go on the second output; wire it to an MQTT out node. The node announces itself once the flows have
|
|
81
139
|
started, and again when it receives <code>msg.source = "startup"</code>.</p>
|
|
@@ -84,8 +142,16 @@
|
|
|
84
142
|
<li>Occupancy events, for the motion lighting policy.</li>
|
|
85
143
|
<li>MQTT publications, when publishing is on.</li>
|
|
86
144
|
</ol>
|
|
145
|
+
<h3>Broker</h3>
|
|
146
|
+
<p>Choose Node-RED's <b>MQTT broker</b> node, and it publishes itself: it announces everything each time the
|
|
147
|
+
broker connects, reconnects included, and each time Home Assistant comes back (<code>online</code> on
|
|
148
|
+
<code><prefix>/status</code>), so its entities come back by themselves.
|
|
149
|
+
Each connect also runs its startup message, as a Status node's "connected" did without a broker. Its publication output then sends
|
|
150
|
+
nothing, and an MQTT out on it and a Status node are no longer needed: still wired, they change nothing.
|
|
151
|
+
Opening and saving the node offers the first broker in your flows. A node saved before 6.2 that publishes keeps
|
|
152
|
+
publishing on its output, as below, and needs no broker until you choose one.</p>
|
|
87
153
|
<h3>When MQTT out connects</h3>
|
|
88
|
-
<p>
|
|
154
|
+
<p>Without a broker, wire a <b>Status node</b> whose scope is your MQTT out node into this node. Each time MQTT out shows
|
|
89
155
|
"connected", this node takes it as its startup message (<code>msg.source = "startup"</code>) and announces itself again:
|
|
90
156
|
Node-RED's MQTT broker node drops what it is given before it connects, so what the node sends as the flows
|
|
91
157
|
start is lost on a first start, a full deploy or a restart. Any other status message is ignored.</p>
|