@avantics/node-red-home-automation 3.0.0 → 4.0.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 +285 -13
- package/dist/lib/activity-lighting-controller.d.ts +7 -1
- package/dist/lib/activity-lighting-controller.js +37 -3
- package/dist/lib/adopted-request.d.ts +26 -0
- package/dist/lib/adopted-request.js +44 -0
- package/dist/lib/avr-controller.d.ts +8 -1
- package/dist/lib/avr-controller.js +27 -3
- package/dist/lib/avr-publishing.d.ts +24 -0
- package/dist/lib/avr-publishing.js +80 -0
- package/dist/lib/backlighting-controller.d.ts +7 -1
- package/dist/lib/backlighting-controller.js +26 -3
- package/dist/lib/circadian.d.ts +1 -0
- package/dist/lib/circadian.js +3 -0
- package/dist/lib/command-echo.d.ts +2 -0
- package/dist/lib/command-echo.js +39 -15
- package/dist/lib/cover-availability.d.ts +7 -0
- package/dist/lib/cover-availability.js +24 -0
- package/dist/lib/cover-controller.d.ts +13 -1
- package/dist/lib/cover-controller.js +113 -30
- package/dist/lib/cover-group.d.ts +62 -0
- package/dist/lib/cover-group.js +125 -0
- package/dist/lib/cover-models.d.ts +37 -0
- package/dist/lib/cover-models.js +83 -0
- package/dist/lib/cover-publishing.d.ts +40 -0
- package/dist/lib/cover-publishing.js +102 -0
- package/dist/lib/cover-requests.d.ts +21 -0
- package/dist/lib/cover-requests.js +49 -0
- package/dist/lib/display-mode-controller.d.ts +4 -0
- package/dist/lib/display-mode-controller.js +26 -2
- package/dist/lib/ha-discovery.d.ts +27 -0
- package/dist/lib/ha-discovery.js +91 -0
- package/dist/lib/house-schedule-publishing.d.ts +23 -0
- package/dist/lib/house-schedule-publishing.js +71 -0
- package/dist/lib/house-schedule.d.ts +23 -0
- package/dist/lib/house-schedule.js +29 -0
- package/dist/lib/motion-policy.d.ts +9 -0
- package/dist/lib/motion-policy.js +45 -11
- package/dist/lib/motion-publishing.d.ts +28 -0
- package/dist/lib/motion-publishing.js +92 -0
- package/dist/lib/occupancy-publishing.d.ts +10 -0
- package/dist/lib/occupancy-publishing.js +45 -0
- package/dist/lib/open-close-cover.d.ts +92 -0
- package/dist/lib/open-close-cover.js +287 -0
- package/dist/lib/publishing-node.d.ts +39 -0
- package/dist/lib/publishing-node.js +72 -0
- package/dist/lib/retained-topics.d.ts +10 -0
- package/dist/lib/retained-topics.js +19 -0
- package/dist/lib/room-controller.d.ts +10 -0
- package/dist/lib/room-controller.js +68 -59
- package/dist/lib/room-lighting-discovery.d.ts +20 -1
- package/dist/lib/room-lighting-discovery.js +135 -13
- package/dist/lib/room-publishing.d.ts +42 -0
- package/dist/lib/room-publishing.js +182 -0
- package/dist/lib/room-schedule.d.ts +8 -0
- package/dist/lib/room-schedule.js +41 -0
- package/dist/lib/settings.d.ts +2 -0
- package/dist/lib/settings.js +20 -0
- package/dist/lib/source-capability-controller.d.ts +8 -1
- package/dist/lib/source-capability-controller.js +29 -3
- package/dist/lib/theatre-activity-controller.d.ts +0 -1
- package/dist/lib/theatre-activity-controller.js +27 -24
- package/dist/lib/theatre-ha-discovery.d.ts +3 -4
- package/dist/lib/theatre-ha-discovery.js +46 -36
- package/dist/lib/theatre-state-contract.d.ts +6 -0
- package/dist/lib/theatre-state-contract.js +83 -47
- package/dist/lib/theatre-state.d.ts +1 -1
- package/dist/lib/theatre-state.js +1 -1
- package/dist/lib/topic-names.d.ts +4 -0
- package/dist/lib/topic-names.js +31 -0
- package/dist/nodes/activity-lighting-controller.js +5 -3
- package/dist/nodes/adapter.d.ts +6 -1
- package/dist/nodes/adapter.js +65 -1
- package/dist/nodes/avr-controller.html +59 -5
- package/dist/nodes/avr-controller.js +74 -4
- package/dist/nodes/backlighting-controller.js +5 -3
- package/dist/nodes/cover-controller.d.ts +3 -0
- package/dist/nodes/cover-controller.html +177 -0
- package/dist/nodes/cover-controller.js +178 -0
- package/dist/nodes/cover-group.d.ts +3 -0
- package/dist/nodes/cover-group.html +89 -0
- package/dist/nodes/cover-group.js +84 -0
- package/dist/nodes/flows-started.d.ts +4 -0
- package/dist/nodes/flows-started.js +39 -0
- package/dist/nodes/house-schedule-mqtt.d.ts +3 -0
- package/dist/nodes/house-schedule-mqtt.html +61 -0
- package/dist/nodes/house-schedule-mqtt.js +80 -0
- package/dist/nodes/house-schedule.d.ts +3 -0
- package/dist/nodes/house-schedule.html +31 -0
- package/dist/nodes/house-schedule.js +51 -0
- package/dist/nodes/motion-policy.html +81 -5
- package/dist/nodes/motion-policy.js +69 -8
- package/dist/nodes/occupancy-fusion.html +69 -14
- package/dist/nodes/occupancy-fusion.js +46 -4
- package/dist/nodes/publishing-lifecycle.d.ts +25 -0
- package/dist/nodes/publishing-lifecycle.js +38 -0
- package/dist/nodes/room-controller.html +54 -5
- package/dist/nodes/room-controller.js +67 -8
- package/dist/nodes/shared-schedule.d.ts +6 -0
- package/dist/nodes/shared-schedule.js +15 -0
- package/dist/nodes/source-capability-controller.js +5 -3
- package/dist/nodes/theatre-activity-controller.html +1 -3
- package/dist/nodes/theatre-activity-controller.js +1 -2
- package/dist/nodes/theatre-state-contract.html +28 -5
- package/dist/nodes/theatre-state-contract.js +22 -4
- package/examples/Cover control.json +380 -0
- package/examples/Cover pair.json +230 -0
- package/examples/Home theatre activity.json +4 -4
- package/examples/House schedule.json +164 -0
- package/examples/Room lighting.json +4 -3
- package/package.json +6 -2
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
<script type="text/javascript">
|
|
2
|
+
RED.nodes.registerType("home-cover-group", {
|
|
3
|
+
category: "home automation",
|
|
4
|
+
color: "#e2d96e",
|
|
5
|
+
defaults: {
|
|
6
|
+
name: {value: ""},
|
|
7
|
+
area: {value: ""},
|
|
8
|
+
members: {value: "[\"left\",\"right\"]", required: true, validate: function (v) {
|
|
9
|
+
try {
|
|
10
|
+
var parsed = JSON.parse(v);
|
|
11
|
+
return Array.isArray(parsed) && parsed.length >= 2 && parsed.every(function (name) {
|
|
12
|
+
return typeof name === "string" && /^[a-z][a-z0-9_-]*$/.test(name);
|
|
13
|
+
}) && parsed.filter(function (name, index) { return parsed.indexOf(name) === index; }).length === parsed.length;
|
|
14
|
+
} catch (e) { return false; }
|
|
15
|
+
}},
|
|
16
|
+
deadlineSeconds: {value: 120, required: true, validate: function (v) { return RED.validators.number()(v) && Number(v) >= 1; }},
|
|
17
|
+
// The group's result, then one output per member (as the core switch
|
|
18
|
+
// node sets its outputs from its rules).
|
|
19
|
+
outputs: {value: 3}
|
|
20
|
+
},
|
|
21
|
+
inputs: 1,
|
|
22
|
+
outputs: 3,
|
|
23
|
+
outputLabels: function (index) {
|
|
24
|
+
if (index === 0) return "group result";
|
|
25
|
+
try { return JSON.parse(this.members)[index - 1] || ""; } catch (e) { return ""; }
|
|
26
|
+
},
|
|
27
|
+
oneditprepare: function () {
|
|
28
|
+
// As the core switch node does, the hidden outputs field holds a port
|
|
29
|
+
// map, {old port: new port, or "-1" when removed}, plus a key per new
|
|
30
|
+
// port, so the editor moves each member's wire with its name. Every
|
|
31
|
+
// kept port is listed, as the editor counts the outputs from the map.
|
|
32
|
+
var opened = [];
|
|
33
|
+
try { opened = JSON.parse(this.members); } catch (e) { opened = []; }
|
|
34
|
+
if (!Array.isArray(opened)) opened = [];
|
|
35
|
+
function portMap() {
|
|
36
|
+
var members;
|
|
37
|
+
try { members = JSON.parse($("#node-input-members").val()); } catch (e) { return; }
|
|
38
|
+
if (!Array.isArray(members)) return;
|
|
39
|
+
var map = {"0": "0"};
|
|
40
|
+
opened.forEach(function (name, index) {
|
|
41
|
+
var now = members.indexOf(name);
|
|
42
|
+
map[String(index + 1)] = now < 0 ? "-1" : String(now + 1);
|
|
43
|
+
});
|
|
44
|
+
members.forEach(function (name, index) {
|
|
45
|
+
if (opened.indexOf(name) < 0) map["new-" + name] = String(index + 1);
|
|
46
|
+
});
|
|
47
|
+
$("#node-input-outputs").val(JSON.stringify(map));
|
|
48
|
+
}
|
|
49
|
+
$("#node-input-members").on("change keyup", portMap);
|
|
50
|
+
portMap();
|
|
51
|
+
},
|
|
52
|
+
icon: "font-awesome/fa-columns",
|
|
53
|
+
label: function () { return this.name || (this.area ? this.area + " " : "") + "cover group"; }
|
|
54
|
+
});
|
|
55
|
+
</script>
|
|
56
|
+
|
|
57
|
+
<script type="text/html" data-template-name="home-cover-group">
|
|
58
|
+
<div class="form-row"><label for="node-input-name">Name</label><input type="text" id="node-input-name"></div>
|
|
59
|
+
<div class="form-row"><label for="node-input-area">Area</label><input type="text" id="node-input-area"></div>
|
|
60
|
+
<div class="form-row"><label for="node-input-members">Members</label><input type="text" id="node-input-members"></div>
|
|
61
|
+
<input type="hidden" id="node-input-outputs">
|
|
62
|
+
<div class="form-row"><label for="node-input-deadlineSeconds">Answer deadline (s)</label><input type="number" id="node-input-deadlineSeconds"></div>
|
|
63
|
+
</script>
|
|
64
|
+
|
|
65
|
+
<script type="text/html" data-help-name="home-cover-group">
|
|
66
|
+
<p>Moves several covers as one, such as the left and right of a curtain: one request goes to every member, and
|
|
67
|
+
the group says honestly how far it got.</p>
|
|
68
|
+
<h3>Inputs</h3>
|
|
69
|
+
<dl class="message-properties">
|
|
70
|
+
<dt>payload</dt><dd>a cover request (<code>"open"</code>, <code>"close"</code>, <code>"stop"</code>, a position or a
|
|
71
|
+
preset name), with <code>stopIfMoving</code> or <code>respectManual</code> if wanted. Each member gets the same
|
|
72
|
+
request.</dd>
|
|
73
|
+
<dt>theatre</dt><dd>a member's result: wire each member cover node's second output back into the group. Other
|
|
74
|
+
state from a cover node, and its <code>moving</code> signal, are ignored.</dd>
|
|
75
|
+
</dl>
|
|
76
|
+
<h3>Outputs</h3>
|
|
77
|
+
<ol class="node-ports">
|
|
78
|
+
<li>The group's result, <code>msg.theatre = {kind: "target_state", target: "cover", status, details}</code>:
|
|
79
|
+
<code>ready</code> only when every member confirms; once every member has settled with any not ready,
|
|
80
|
+
<code>failed</code> with reason <code>partial</code> (some confirmed) or <code>none-confirmed</code>, and
|
|
81
|
+
<code>details.members</code> saying what each did. A member silent at the <b>Answer deadline</b> counts as
|
|
82
|
+
<code>no-answer</code>, and a new request first answers the open one <code>failed</code> with reason
|
|
83
|
+
<code>superseded</code>.</li>
|
|
84
|
+
<li>Then one output per member, in the order of <b>Members</b> and labelled with its name: that member's
|
|
85
|
+
request, with its own <code>coverRequestId</code> (and <code>msg.topic</code> naming it). Wire each to its
|
|
86
|
+
cover node; no switch node is needed. A cover node answers the request even when it doesn't carry it out,
|
|
87
|
+
with the reason. Changing <b>Members</b> changes the number of outputs.</li>
|
|
88
|
+
</ol>
|
|
89
|
+
</script>
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const cover_group_1 = require("../lib/cover-group");
|
|
3
|
+
const settings_1 = require("../lib/settings");
|
|
4
|
+
const adapter_1 = require("./adapter");
|
|
5
|
+
const registerCoverGroup = (RED) => {
|
|
6
|
+
function HomeCoverGroup(config) {
|
|
7
|
+
RED.nodes.createNode(this, config);
|
|
8
|
+
const node = this;
|
|
9
|
+
let members;
|
|
10
|
+
let area;
|
|
11
|
+
let deadlineSeconds;
|
|
12
|
+
try {
|
|
13
|
+
area = (0, adapter_1.optionalArea)(config);
|
|
14
|
+
members = (0, cover_group_1.coverGroupMembers)(config.members);
|
|
15
|
+
deadlineSeconds = Math.max(1, (0, settings_1.numberSetting)(config.deadlineSeconds, "answer deadline", cover_group_1.DEFAULT_GROUP_DEADLINE_SECONDS));
|
|
16
|
+
}
|
|
17
|
+
catch (error) {
|
|
18
|
+
(0, adapter_1.invalidConfig)(node, error);
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
const trace = { area, component: "cover-group" };
|
|
22
|
+
let timer;
|
|
23
|
+
function process(event, send) {
|
|
24
|
+
const result = (0, adapter_1.runReducer)(node, trace, (saved, now) => (0, cover_group_1.transitionCoverGroup)(saved, event, members, now, deadlineSeconds));
|
|
25
|
+
// The group's result on output 1, then one output per member, in the
|
|
26
|
+
// order of Members, each with that member's request (msg.topic still
|
|
27
|
+
// names it).
|
|
28
|
+
const outputs = [
|
|
29
|
+
result.events.length ? result.events.map((entry) => ({ payload: undefined, ...entry })) : null,
|
|
30
|
+
...members.map((member) => {
|
|
31
|
+
const request = result.commands.filter((command) => command.topic === member);
|
|
32
|
+
return request.length ? request.map((command) => ({ ...command })) : null;
|
|
33
|
+
})
|
|
34
|
+
];
|
|
35
|
+
if (outputs.some((output) => output !== null))
|
|
36
|
+
send(outputs);
|
|
37
|
+
node.status({
|
|
38
|
+
fill: result.status.total && result.status.ready === result.status.total ? "green" : "blue",
|
|
39
|
+
shape: "dot",
|
|
40
|
+
text: result.status.text
|
|
41
|
+
});
|
|
42
|
+
// At the deadline, members that never answered count as not answering.
|
|
43
|
+
clearTimeout(timer);
|
|
44
|
+
timer = undefined;
|
|
45
|
+
const request = result.state.request;
|
|
46
|
+
if (request && !request.settled) {
|
|
47
|
+
timer = setTimeout((0, adapter_1.guarded)(node, () => process({ kind: "tick" }, node.send.bind(node))), Math.max(0, request.deadlineAt - Date.now()));
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
// Context survives a redeploy but timers do not: re-arm the deadline of
|
|
51
|
+
// a request still open (an overdue one settles at the first tick).
|
|
52
|
+
const open = node.context().get("state")?.request;
|
|
53
|
+
if (open && !open.settled && Number.isFinite(open.deadlineAt)) {
|
|
54
|
+
timer = setTimeout((0, adapter_1.guarded)(node, () => process({ kind: "tick" }, node.send.bind(node))), Math.max(0, open.deadlineAt - Date.now()));
|
|
55
|
+
}
|
|
56
|
+
(0, adapter_1.onInput)(node, (msg, send) => {
|
|
57
|
+
const theatre = (0, adapter_1.objectField)(msg, "theatre");
|
|
58
|
+
// A member's answer, from its cover node's second output. Anything else
|
|
59
|
+
// a cover node sends (other state, its moving signal) is not a request.
|
|
60
|
+
if (theatre?.kind === "target_state" && typeof theatre.requestId === "string") {
|
|
61
|
+
process({ kind: "member", requestId: theatre.requestId, status: String(theatre.status), classification: theatre.classification, reason: theatre.failure?.reason ?? null }, send);
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
if (theatre || msg.topic === "moving") {
|
|
65
|
+
(0, adapter_1.runReducer)(node, trace, (state) => ({ state, traces: [{ event: "input", outcome: "ignored", reason: theatre ? "not-a-member-answer" : "moving-signal", details: {} }] }));
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
process({
|
|
69
|
+
kind: "request",
|
|
70
|
+
payload: msg.payload,
|
|
71
|
+
stopIfMoving: msg.stopIfMoving === true,
|
|
72
|
+
respectManual: msg.respectManual === true,
|
|
73
|
+
source: typeof msg.source === "string" ? msg.source : undefined
|
|
74
|
+
}, send);
|
|
75
|
+
});
|
|
76
|
+
node.on("close", (_removed, done) => {
|
|
77
|
+
clearTimeout(timer);
|
|
78
|
+
done();
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
RED.nodes.registerType("home-cover-group", HomeCoverGroup);
|
|
82
|
+
};
|
|
83
|
+
module.exports = registerCoverGroup;
|
|
84
|
+
//# sourceMappingURL=cover-group.js.map
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.onFlowsStarted = onFlowsStarted;
|
|
4
|
+
const callbacks = new WeakMap();
|
|
5
|
+
// Calls `callback` every time the flows start, until the returned function
|
|
6
|
+
// is called. Callbacks run in the order they were added; one added or
|
|
7
|
+
// removed while they run takes effect from the next start. Each runs on its
|
|
8
|
+
// own: an error goes to `report` (the registering node's error), so it can
|
|
9
|
+
// neither stop the others nor escape Node-RED's emit of "flows:started".
|
|
10
|
+
function onFlowsStarted(events, callback, report) {
|
|
11
|
+
let waiting = callbacks.get(events);
|
|
12
|
+
if (!waiting) {
|
|
13
|
+
const created = new Set();
|
|
14
|
+
waiting = created;
|
|
15
|
+
callbacks.set(events, created);
|
|
16
|
+
events.on("flows:started", () => {
|
|
17
|
+
for (const run of [...created])
|
|
18
|
+
run();
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
const own = () => {
|
|
22
|
+
try {
|
|
23
|
+
callback();
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
try {
|
|
27
|
+
report(error);
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
// Reporting failed too: nothing more can be done here.
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
waiting.add(own);
|
|
35
|
+
return () => {
|
|
36
|
+
waiting.delete(own);
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=flows-started.js.map
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
<script type="text/javascript">
|
|
2
|
+
(function () {
|
|
3
|
+
function on(value) { return value === true || value === "true"; }
|
|
4
|
+
RED.nodes.registerType("home-house-schedule-mqtt", {
|
|
5
|
+
category: "home automation",
|
|
6
|
+
color: "#ffd27f",
|
|
7
|
+
defaults: {
|
|
8
|
+
// A marker only a node new from the palette has, as core nodes do: import
|
|
9
|
+
// and paste keep the saved name, so onadd leaves a saved node's settings.
|
|
10
|
+
name: {value: "_DEFAULT_"},
|
|
11
|
+
schedule: {value: "", type: "home-house-schedule", required: true},
|
|
12
|
+
discovery: {value: true},
|
|
13
|
+
discoveryPrefix: {value: "homeassistant", required: true, validate: function (v) { var prefix = String(v || "").trim().replace(/^\/+|\/+$/g, ""); return Boolean(prefix) && !/[+#]/.test(prefix); }},
|
|
14
|
+
// How Home Assistant IDs are named; this node has no older entities,
|
|
15
|
+
// so not set also means "node".
|
|
16
|
+
idScheme: {value: null}
|
|
17
|
+
},
|
|
18
|
+
onadd: function () {
|
|
19
|
+
if (this.name !== "_DEFAULT_") return;
|
|
20
|
+
this.name = "";
|
|
21
|
+
this.idScheme = "node";
|
|
22
|
+
},
|
|
23
|
+
oneditprepare: function () {
|
|
24
|
+
$("#node-input-discovery").prop("checked", on(this.discovery));
|
|
25
|
+
},
|
|
26
|
+
inputs: 1,
|
|
27
|
+
outputs: 1,
|
|
28
|
+
outputLabels: ["MQTT publications"],
|
|
29
|
+
icon: "font-awesome/fa-clock-o",
|
|
30
|
+
label: function () { return this.name || "house schedule MQTT"; }
|
|
31
|
+
});
|
|
32
|
+
})();
|
|
33
|
+
</script>
|
|
34
|
+
|
|
35
|
+
<script type="text/html" data-template-name="home-house-schedule-mqtt">
|
|
36
|
+
<div class="form-row"><label for="node-input-name">Name</label><input type="text" id="node-input-name"></div>
|
|
37
|
+
<div class="form-row"><label for="node-input-schedule">Schedule</label><input type="text" id="node-input-schedule"></div>
|
|
38
|
+
<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;"></div>
|
|
39
|
+
<div class="form-row"><label for="node-input-discoveryPrefix">Discovery prefix</label><input type="text" id="node-input-discoveryPrefix"></div>
|
|
40
|
+
</script>
|
|
41
|
+
|
|
42
|
+
<script type="text/html" data-help-name="home-house-schedule-mqtt">
|
|
43
|
+
<p>A house schedule's MQTT and Home Assistant side. Optional: rooms follow the schedule without it.</p>
|
|
44
|
+
<p>It publishes the schedule's times, retained, on <code>home/<name>/schedule/wake-time/state</code> and
|
|
45
|
+
<code>bed-time/state</code>, with availability on <code>home/<name>/schedule/availability</code>; with
|
|
46
|
+
<b>Home Assistant discovery</b>, wake and bed time entities, named after the schedule so a replacement node keeps
|
|
47
|
+
them. Use one per schedule: two would share the same topics and entities, and deleting one clears them for both
|
|
48
|
+
until the other next starts. Wire its output to an MQTT out node, and an MQTT in node on the set topics to its input.</p>
|
|
49
|
+
<h3>Inputs</h3>
|
|
50
|
+
<dl class="message-properties">
|
|
51
|
+
<dt>topic = home/<name>/schedule/wake-time/set or bed-time/set</dt><dd>a time from Home Assistant.
|
|
52
|
+
Retained messages and anything but a time are ignored.</dd>
|
|
53
|
+
<dt>lighting</dt><dd><code>{kind: "schedule", wakeTime, bedTime}</code>, each <code>HH:MM</code> and either one
|
|
54
|
+
alone: new times for the schedule.</dd>
|
|
55
|
+
<dt>source = "startup"</dt><dd>announce again, as after the broker reconnects.</dd>
|
|
56
|
+
</dl>
|
|
57
|
+
<h3>Outputs</h3>
|
|
58
|
+
<ol class="node-ports">
|
|
59
|
+
<li>MQTT publications.</li>
|
|
60
|
+
</ol>
|
|
61
|
+
</script>
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const ha_discovery_1 = require("../lib/ha-discovery");
|
|
3
|
+
const house_schedule_publishing_1 = require("../lib/house-schedule-publishing");
|
|
4
|
+
const settings_1 = require("../lib/settings");
|
|
5
|
+
const topic_names_1 = require("../lib/topic-names");
|
|
6
|
+
const adapter_1 = require("./adapter");
|
|
7
|
+
const publishing_lifecycle_1 = require("./publishing-lifecycle");
|
|
8
|
+
const shared_schedule_1 = require("./shared-schedule");
|
|
9
|
+
const registerHouseScheduleMqtt = (RED) => {
|
|
10
|
+
function HomeHouseScheduleMqtt(config) {
|
|
11
|
+
RED.nodes.createNode(this, config);
|
|
12
|
+
const node = this;
|
|
13
|
+
let house;
|
|
14
|
+
let publishing;
|
|
15
|
+
try {
|
|
16
|
+
const referenced = (0, shared_schedule_1.referencedSchedule)(RED, config.schedule);
|
|
17
|
+
if (!referenced.schedule)
|
|
18
|
+
throw new Error(referenced.missing ? "the house schedule is missing" : "no house schedule");
|
|
19
|
+
house = referenced.schedule;
|
|
20
|
+
publishing = {
|
|
21
|
+
area: (0, topic_names_1.publishingArea)(house.name, true),
|
|
22
|
+
// Publishing is what this node is for; discovery is off unless
|
|
23
|
+
// switched on.
|
|
24
|
+
publishState: true,
|
|
25
|
+
discovery: (0, settings_1.optionalBooleanSetting)(config.discovery, "Home Assistant discovery") ?? false,
|
|
26
|
+
discoveryPrefix: (0, ha_discovery_1.discoveryPrefixSetting)(config.discoveryPrefix),
|
|
27
|
+
// Named after the schedule, not this node, so a replacement node
|
|
28
|
+
// keeps the same entities.
|
|
29
|
+
nodeId: config.schedule,
|
|
30
|
+
idScheme: (0, settings_1.optionalChoiceSetting)(config.idScheme, "ID scheme", ha_discovery_1.ID_SCHEMES) ?? "node"
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
catch (error) {
|
|
34
|
+
(0, adapter_1.invalidConfig)(node, error);
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
const trace = { area: publishing.area, component: "house-schedule-mqtt" };
|
|
38
|
+
function showTimes() {
|
|
39
|
+
const { wakeTime, bedTime } = house.schedule();
|
|
40
|
+
node.status({ fill: "green", shape: "dot", text: `wake ${wakeTime} · bed ${bedTime}` });
|
|
41
|
+
}
|
|
42
|
+
const lifecycle = (0, publishing_lifecycle_1.publishingLifecycle)(RED, node, {
|
|
43
|
+
retainedTopics: () => (0, house_schedule_publishing_1.houseRetainedTopics)(publishing),
|
|
44
|
+
// Its own entities, named after the schedule, cleared when discovery is
|
|
45
|
+
// switched off, with or without a record (see ownDiscoveryTopics).
|
|
46
|
+
possible: { owner: publishing, topics: () => (0, house_schedule_publishing_1.houseRetainedTopics)({ ...publishing, discovery: true }) },
|
|
47
|
+
starting: () => (0, house_schedule_publishing_1.houseStartingOutputs)(publishing, house.schedule()),
|
|
48
|
+
stopping: () => (0, house_schedule_publishing_1.houseStoppingOutputs)(publishing),
|
|
49
|
+
output: 0,
|
|
50
|
+
outputs: 1
|
|
51
|
+
});
|
|
52
|
+
const unsubscribe = house.subscribe((0, adapter_1.guarded)(node, () => {
|
|
53
|
+
lifecycle.publish(node.send.bind(node), (0, house_schedule_publishing_1.houseStateOutputs)(publishing, house.schedule()));
|
|
54
|
+
showTimes();
|
|
55
|
+
}));
|
|
56
|
+
node.on("close", unsubscribe);
|
|
57
|
+
showTimes();
|
|
58
|
+
(0, adapter_1.onInput)(node, (msg, send) => {
|
|
59
|
+
const lighting = (0, adapter_1.objectField)(msg, "lighting");
|
|
60
|
+
if (lighting?.kind === "startup" || msg.source === "startup") {
|
|
61
|
+
lifecycle.startup(send, (0, house_schedule_publishing_1.houseStartingOutputs)(publishing, house.schedule()));
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
if (lighting?.kind === "schedule") {
|
|
65
|
+
house.change({ kind: "schedule", wakeTime: lighting.wakeTime, bedTime: lighting.bedTime });
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
const inbound = (0, house_schedule_publishing_1.houseInbound)(msg, publishing);
|
|
69
|
+
if (inbound?.event) {
|
|
70
|
+
house.change(inbound.event, inbound.traces);
|
|
71
|
+
}
|
|
72
|
+
else if (inbound) {
|
|
73
|
+
(0, adapter_1.runReducer)(node, trace, (state) => ({ state, traces: inbound.traces }));
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
RED.nodes.registerType("home-house-schedule-mqtt", HomeHouseScheduleMqtt);
|
|
78
|
+
};
|
|
79
|
+
module.exports = registerHouseScheduleMqtt;
|
|
80
|
+
//# sourceMappingURL=house-schedule-mqtt.js.map
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
<script type="text/javascript">
|
|
2
|
+
(function () {
|
|
3
|
+
function clock(v) { return /^([01]\d|2[0-3]):[0-5]\d$/.test(String(v || "").trim()); }
|
|
4
|
+
// The schedule rooms share: a config node, with no wires.
|
|
5
|
+
RED.nodes.registerType("home-house-schedule", {
|
|
6
|
+
category: "config",
|
|
7
|
+
defaults: {
|
|
8
|
+
// Also its name in topics, home/<name>/schedule/...
|
|
9
|
+
name: {value: "house", required: true, validate: function (v) { return /[a-z0-9]/i.test(String(v || "")); }},
|
|
10
|
+
wakeTime: {value: "07:00", required: true, validate: clock},
|
|
11
|
+
bedTime: {value: "23:00", required: true, validate: function (v) { return clock(v) && String(v).trim() !== String($("#node-config-input-wakeTime").val() || this.wakeTime || "").trim(); }}
|
|
12
|
+
},
|
|
13
|
+
label: function () { return this.name || "house schedule"; }
|
|
14
|
+
});
|
|
15
|
+
})();
|
|
16
|
+
</script>
|
|
17
|
+
|
|
18
|
+
<script type="text/html" data-template-name="home-house-schedule">
|
|
19
|
+
<div class="form-row"><label for="node-config-input-name">Name</label><input type="text" id="node-config-input-name" placeholder="house"></div>
|
|
20
|
+
<div class="form-row"><label for="node-config-input-wakeTime">Wake time</label><input type="text" id="node-config-input-wakeTime" placeholder="07:00"></div>
|
|
21
|
+
<div class="form-row"><label for="node-config-input-bedTime">Bed time</label><input type="text" id="node-config-input-bedTime" placeholder="23:00"></div>
|
|
22
|
+
</script>
|
|
23
|
+
|
|
24
|
+
<script type="text/html" data-help-name="home-house-schedule">
|
|
25
|
+
<p>One wake and bed time shared by every room lighting controller whose <b>Schedule</b> is this one. Rooms that keep
|
|
26
|
+
their own times (a child's room, say) choose none.</p>
|
|
27
|
+
<p>The rooms hear of every change as it happens, without wires. Times change from a message or from Home Assistant
|
|
28
|
+
through a <i>house schedule MQTT</i> node; a time changed that way lasts until this schedule is deployed with a
|
|
29
|
+
different <b>Wake time</b> or <b>Bed time</b>, which then applies.</p>
|
|
30
|
+
<p>The <b>Name</b> labels it and names its topics, <code>home/<name>/schedule/...</code>.</p>
|
|
31
|
+
</script>
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const circadian_schedule_1 = require("../lib/circadian-schedule");
|
|
3
|
+
const house_schedule_1 = require("../lib/house-schedule");
|
|
4
|
+
const settings_1 = require("../lib/settings");
|
|
5
|
+
const adapter_1 = require("./adapter");
|
|
6
|
+
const registerHouseSchedule = (RED) => {
|
|
7
|
+
function HomeHouseSchedule(config) {
|
|
8
|
+
RED.nodes.createNode(this, config);
|
|
9
|
+
const node = this;
|
|
10
|
+
let setting;
|
|
11
|
+
let name;
|
|
12
|
+
try {
|
|
13
|
+
setting = (0, circadian_schedule_1.resolveSchedule)((0, settings_1.textSetting)(config.wakeTime, "wake time", "07:00"), (0, settings_1.textSetting)(config.bedTime, "bed time", "23:00"));
|
|
14
|
+
// Also its name in topics, home/<name>/schedule/...
|
|
15
|
+
name = (0, settings_1.textSetting)(config.name, "name", "house");
|
|
16
|
+
}
|
|
17
|
+
catch (error) {
|
|
18
|
+
(0, adapter_1.invalidConfig)(node, error);
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
const trace = { area: name, component: "house-schedule" };
|
|
22
|
+
const listeners = new Set();
|
|
23
|
+
const shared = {
|
|
24
|
+
name,
|
|
25
|
+
schedule: () => (0, house_schedule_1.houseScheduleInUse)(node.context().get("state"), setting),
|
|
26
|
+
change(event, traces = []) {
|
|
27
|
+
const result = (0, adapter_1.runReducer)(node, trace, (saved) => {
|
|
28
|
+
const decided = (0, house_schedule_1.transitionHouseSchedule)(saved, event, setting);
|
|
29
|
+
return { ...decided, traces: [...traces, ...decided.traces] };
|
|
30
|
+
});
|
|
31
|
+
if (!result.changed)
|
|
32
|
+
return;
|
|
33
|
+
for (const listener of [...listeners])
|
|
34
|
+
listener({ ...result.state.schedule });
|
|
35
|
+
},
|
|
36
|
+
subscribe(listener) {
|
|
37
|
+
listeners.add(listener);
|
|
38
|
+
return () => {
|
|
39
|
+
listeners.delete(listener);
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
Object.assign(node, shared);
|
|
44
|
+
node.on("close", () => {
|
|
45
|
+
listeners.clear();
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
RED.nodes.registerType("home-house-schedule", HomeHouseSchedule);
|
|
49
|
+
};
|
|
50
|
+
module.exports = registerHouseSchedule;
|
|
51
|
+
//# sourceMappingURL=house-schedule.js.map
|
|
@@ -1,20 +1,59 @@
|
|
|
1
1
|
<script type="text/javascript">
|
|
2
|
+
(function () {
|
|
3
|
+
function on(value) { return value === true || value === "true"; }
|
|
2
4
|
RED.nodes.registerType("home-motion-lighting-policy", {
|
|
3
5
|
category: "home automation",
|
|
4
6
|
color: "#d8bfd8",
|
|
5
7
|
defaults: {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
+
// A marker only a node new from the palette has, as core nodes do: import
|
|
9
|
+
// and paste keep the saved name, so onadd leaves a saved node's settings.
|
|
10
|
+
name: {value: "_DEFAULT_"},
|
|
11
|
+
// Required only when the node publishes, and then with a letter or
|
|
12
|
+
// 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
|
+
}},
|
|
8
17
|
onTargets: {value: "[\"main\"]", required: true, validate: function (v) { try { JSON.parse(v); return true; } catch (e) { return false; } }},
|
|
9
18
|
offTargets: {value: "[\"main\"]", required: true, validate: function (v) { try { JSON.parse(v); return true; } catch (e) { return false; } }},
|
|
10
19
|
defaultDelaySeconds: {value: 120, required: true, validate: RED.validators.number()},
|
|
11
|
-
defaultIlluminanceCutoff: {value: 10, required: true, validate: RED.validators.number()}
|
|
20
|
+
defaultIlluminanceCutoff: {value: 10, required: true, validate: RED.validators.number()},
|
|
21
|
+
// How long a pause lasts unless renewed, so a lost release can't stop
|
|
22
|
+
// motion lighting for good.
|
|
23
|
+
maxPauseSeconds: {value: 600, required: true, validate: function (v) { return RED.validators.number()(v) && Number(v) >= 1; }},
|
|
24
|
+
// Motion published nothing before these settings, so both start off.
|
|
25
|
+
publishState: {value: false},
|
|
26
|
+
discovery: {value: false},
|
|
27
|
+
discoveryPrefix: {value: "homeassistant", required: true, validate: function (v) { var prefix = String(v || "").trim().replace(/^\/+|\/+$/g, ""); return Boolean(prefix) && !/[+#]/.test(prefix); }},
|
|
28
|
+
// How Home Assistant IDs are named; motion has no older entities, so not
|
|
29
|
+
// set also means "node".
|
|
30
|
+
idScheme: {value: null}
|
|
31
|
+
},
|
|
32
|
+
onadd: function () {
|
|
33
|
+
if (this.name !== "_DEFAULT_") return;
|
|
34
|
+
this.name = "";
|
|
35
|
+
this.idScheme = "node";
|
|
36
|
+
},
|
|
37
|
+
oneditprepare: function () {
|
|
38
|
+
$("#node-input-publishState").prop("checked", on(this.publishState));
|
|
39
|
+
$("#node-input-discovery").prop("checked", on(this.discovery));
|
|
40
|
+
// Discovery describes the published state, so it needs it.
|
|
41
|
+
function follow() {
|
|
42
|
+
var publishing = $("#node-input-publishState").prop("checked");
|
|
43
|
+
if (!publishing) $("#node-input-discovery").prop("checked", false);
|
|
44
|
+
$("#node-input-discovery").prop("disabled", !publishing);
|
|
45
|
+
$("#node-input-area").trigger("change");
|
|
46
|
+
}
|
|
47
|
+
$("#node-input-publishState, #node-input-discovery").on("change", follow);
|
|
48
|
+
follow();
|
|
12
49
|
},
|
|
13
50
|
inputs: 1,
|
|
14
|
-
outputs:
|
|
51
|
+
outputs: 2,
|
|
52
|
+
outputLabels: ["lighting intents", "MQTT publications"],
|
|
15
53
|
icon: "font-awesome/fa-lightbulb-o",
|
|
16
54
|
label: function () { return this.name || (this.area ? this.area + " " : "") + "motion policy"; }
|
|
17
55
|
});
|
|
56
|
+
})();
|
|
18
57
|
</script>
|
|
19
58
|
|
|
20
59
|
<script type="text/html" data-template-name="home-motion-lighting-policy">
|
|
@@ -24,6 +63,10 @@
|
|
|
24
63
|
<div class="form-row"><label for="node-input-offTargets">OFF targets</label><input type="text" id="node-input-offTargets"></div>
|
|
25
64
|
<div class="form-row"><label for="node-input-defaultDelaySeconds">Vacancy seconds</label><input type="number" id="node-input-defaultDelaySeconds"></div>
|
|
26
65
|
<div class="form-row"><label for="node-input-defaultIlluminanceCutoff">Lux cutoff</label><input type="number" id="node-input-defaultIlluminanceCutoff"></div>
|
|
66
|
+
<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"><label for="node-input-publishState">Publish state over MQTT</label><input type="checkbox" id="node-input-publishState" style="display:inline-block; width:auto;"></div>
|
|
68
|
+
<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>
|
|
69
|
+
<div class="form-row"><label for="node-input-discoveryPrefix">Discovery prefix</label><input type="text" id="node-input-discoveryPrefix"></div>
|
|
27
70
|
</script>
|
|
28
71
|
|
|
29
72
|
<script type="text/html" data-help-name="home-motion-lighting-policy">
|
|
@@ -41,8 +84,41 @@
|
|
|
41
84
|
<dt>area-enabled</dt><dd>ON or OFF for this node; <code>global-enabled</code> for every motion node.</dd>
|
|
42
85
|
<dt>delay</dt><dd>a new vacancy time, in <b>seconds</b>, the same unit as <b>Vacancy seconds</b>.</dd>
|
|
43
86
|
<dt>illuminance-cut-off</dt><dd>a new lux cutoff.</dd>
|
|
87
|
+
<dt>pause</dt><dd><code>true</code> pauses motion lighting, <code>false</code> releases it, for the owner in
|
|
88
|
+
<code>msg.owner</code> (for example a cover's name), which is required. A pause lasts at most the
|
|
89
|
+
<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,
|
|
90
|
+
and a pause cancels a pending vacancy timer, as switching it off does. A pause never changes the enables,
|
|
91
|
+
which are the user's own: wire a cover's <i>moving</i> output here to pause motion while it moves.</dd>
|
|
44
92
|
</dl>
|
|
45
93
|
<p>A <code>delay</code> or <code>illuminance-cut-off</code> message overrides the setting, and lasts across
|
|
46
94
|
restarts until the node is deployed with a different <b>Vacancy seconds</b> or <b>Lux cutoff</b>, which then
|
|
47
|
-
applies.</p>
|
|
95
|
+
applies. Lasting across a redeploy or restart needs a context store (see the README's recommended setup).</p>
|
|
96
|
+
<p>When the node starts with nothing saved (its first deploy, or a redeploy or restart without a context store),
|
|
97
|
+
it doesn't know whether the room is occupied, so it counts the room as vacant from then: unless occupancy is
|
|
98
|
+
reported within <b>Vacancy seconds</b>, it sends its off intent. Lights left on when Node-RED stopped are not
|
|
99
|
+
left on for good, but someone sitting still in a lit room has the lights switched off after the delay unless
|
|
100
|
+
motion is seen. After a restart the room controller has also forgotten which lights a person set, so it doesn't
|
|
101
|
+
keep those either. With a context store, a running off-timer carries on instead.</p>
|
|
102
|
+
<h3>Publishing</h3>
|
|
103
|
+
<p>Off unless switched on, and then named after the <b>Area</b>, which is needed.</p>
|
|
104
|
+
<dl class="message-properties">
|
|
105
|
+
<dt>Publish state over MQTT</dt><dd>whether this node's motion lighting is enabled, retained <code>ON</code> or
|
|
106
|
+
<code>OFF</code> on <code>home/<area>/motion/enabled/state</code>, and its vacancy delay in seconds on
|
|
107
|
+
<code>home/<area>/motion/delay/state</code>, with <code>online</code> or <code>offline</code> on
|
|
108
|
+
<code>home/<area>/motion/availability</code>. It acts on <code>ON</code>/<code>OFF</code> sent to
|
|
109
|
+
<code>…/enabled/set</code> and a delay of 0 to 86400 seconds sent to <code>…/delay/set</code>, as the
|
|
110
|
+
<code>area-enabled</code> and <code>delay</code> inputs do; wire an MQTT in node on
|
|
111
|
+
<code>home/<area>/motion/+/set</code> into this node. The shared <code>global-enabled</code> is not
|
|
112
|
+
published, and the switch shows this node's enable only, so motion can still be off everywhere while it
|
|
113
|
+
shows on.</dd>
|
|
114
|
+
<dt>Home Assistant discovery</dt><dd>a motion lighting switch and a vacancy delay number (in the device's
|
|
115
|
+
configuration). It needs <b>Publish state</b>.</dd>
|
|
116
|
+
</dl>
|
|
117
|
+
<p>These go on the second output; wire it to an MQTT out node. The node announces itself once the flows have
|
|
118
|
+
started, and again when it receives <code>msg.source = "startup"</code>.</p>
|
|
119
|
+
<h3>Outputs</h3>
|
|
120
|
+
<ol class="node-ports">
|
|
121
|
+
<li>Lighting intents, for the room lighting controller.</li>
|
|
122
|
+
<li>MQTT publications, when publishing is on.</li>
|
|
123
|
+
</ol>
|
|
48
124
|
</script>
|