@avantics/node-red-home-automation 3.0.1 → 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 +262 -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/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 +9 -0
- package/dist/lib/room-controller.js +47 -56
- 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 +49 -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
|
@@ -1,18 +1,39 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
const occupancy_fusion_1 = require("../lib/occupancy-fusion");
|
|
3
|
-
const
|
|
3
|
+
const occupancy_publishing_1 = require("../lib/occupancy-publishing");
|
|
4
|
+
const ha_discovery_1 = require("../lib/ha-discovery");
|
|
4
5
|
const settings_1 = require("../lib/settings");
|
|
6
|
+
const topic_names_1 = require("../lib/topic-names");
|
|
7
|
+
const adapter_1 = require("./adapter");
|
|
8
|
+
const publishing_node_1 = require("../lib/publishing-node");
|
|
9
|
+
const publishing_lifecycle_1 = require("./publishing-lifecycle");
|
|
5
10
|
const registerOccupancyFusion = (RED) => {
|
|
6
11
|
function HomeOccupancyFusion(config) {
|
|
7
12
|
RED.nodes.createNode(this, config);
|
|
8
13
|
const node = this;
|
|
9
14
|
let fusionConfig;
|
|
15
|
+
let publishing;
|
|
10
16
|
try {
|
|
11
17
|
// The editor offers "none" (PIR only) and "hold" (PIR plus presence).
|
|
12
18
|
const presence = (0, settings_1.textSetting)(config.presenceMode, "presence sensor", "none");
|
|
13
19
|
if (presence !== "none" && presence !== "hold")
|
|
14
20
|
throw new Error("presence sensor must be none or hold");
|
|
15
|
-
|
|
21
|
+
const area = (0, adapter_1.optionalArea)(config);
|
|
22
|
+
fusionConfig = { area, presenceMode: presence === "hold" ? "hold" : "ignore" };
|
|
23
|
+
// Off unless switched on, as occupancy published nothing before; there
|
|
24
|
+
// are no older entities to keep, so not set names them after the node.
|
|
25
|
+
const publishState = (0, settings_1.optionalBooleanSetting)(config.publishState, "publish state") ?? false;
|
|
26
|
+
const discovery = (0, settings_1.optionalBooleanSetting)(config.discovery, "Home Assistant discovery") ?? false;
|
|
27
|
+
if (publishState || discovery)
|
|
28
|
+
(0, topic_names_1.publishingArea)(area, true);
|
|
29
|
+
publishing = {
|
|
30
|
+
area,
|
|
31
|
+
publishState,
|
|
32
|
+
discovery,
|
|
33
|
+
discoveryPrefix: (0, ha_discovery_1.discoveryPrefixSetting)(config.discoveryPrefix),
|
|
34
|
+
nodeId: node.id,
|
|
35
|
+
idScheme: (0, settings_1.optionalChoiceSetting)(config.idScheme, "ID scheme", ha_discovery_1.ID_SCHEMES) ?? "node"
|
|
36
|
+
};
|
|
16
37
|
}
|
|
17
38
|
catch (error) {
|
|
18
39
|
(0, adapter_1.invalidConfig)(node, error);
|
|
@@ -30,14 +51,35 @@ const registerOccupancyFusion = (RED) => {
|
|
|
30
51
|
text: details.join(" · ")
|
|
31
52
|
});
|
|
32
53
|
}
|
|
54
|
+
function occupied() {
|
|
55
|
+
return node.context().get("state")?.occupied === true;
|
|
56
|
+
}
|
|
57
|
+
// MQTT publications go on the second output, away from the occupancy
|
|
58
|
+
// events the motion policy reads on the first. Available, and clear of
|
|
59
|
+
// what it no longer publishes, as soon as it runs; offline as it stops;
|
|
60
|
+
// nothing left behind when deleted.
|
|
61
|
+
const lifecycle = (0, publishing_lifecycle_1.publishingLifecycle)(RED, node, {
|
|
62
|
+
retainedTopics: () => (0, occupancy_publishing_1.occupancyRetainedTopics)(publishing),
|
|
63
|
+
possible: { owner: publishing, topics: () => (0, publishing_node_1.publishesState)(publishing) ? (0, occupancy_publishing_1.occupancyRetainedTopics)({ ...publishing, publishState: true, discovery: true }) : [] },
|
|
64
|
+
starting: () => (0, occupancy_publishing_1.occupancyStartingOutputs)(publishing, occupied()),
|
|
65
|
+
stopping: () => (0, occupancy_publishing_1.occupancyStoppingOutputs)(publishing),
|
|
66
|
+
output: 1,
|
|
67
|
+
outputs: 2
|
|
68
|
+
});
|
|
33
69
|
(0, adapter_1.onInput)(node, (msg, send) => {
|
|
70
|
+
if (msg.source === "startup") {
|
|
71
|
+
lifecycle.startup(send, (0, occupancy_publishing_1.occupancyStartingOutputs)(publishing, occupied()));
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
34
74
|
const input = {
|
|
35
75
|
source: String(msg.source ?? ""),
|
|
36
76
|
payload: msg.payload
|
|
37
77
|
};
|
|
38
78
|
const result = (0, adapter_1.runReducer)(node, { area: fusionConfig.area, component: "occupancy-fusion" }, (saved) => (0, occupancy_fusion_1.transitionOccupancy)(saved, input, fusionConfig));
|
|
39
|
-
for (const output of result.outputs)
|
|
40
|
-
send({ ...output, payload: output.payload });
|
|
79
|
+
for (const output of result.outputs) {
|
|
80
|
+
send([{ ...output, payload: output.payload }, null]);
|
|
81
|
+
lifecycle.publish(send, (0, occupancy_publishing_1.occupancyStateOutputs)(publishing, output.payload.occupied));
|
|
82
|
+
}
|
|
41
83
|
updateStatus(result.status);
|
|
42
84
|
});
|
|
43
85
|
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Node, NodeAPI } from "node-red";
|
|
2
|
+
import { type DiscoveryOwner } from "../lib/ha-discovery";
|
|
3
|
+
import { type Send } from "./adapter";
|
|
4
|
+
interface RetainedPublication {
|
|
5
|
+
topic: string;
|
|
6
|
+
payload: unknown;
|
|
7
|
+
retain: boolean;
|
|
8
|
+
}
|
|
9
|
+
export interface PublishingNode {
|
|
10
|
+
retainedTopics(): string[];
|
|
11
|
+
possible?: {
|
|
12
|
+
owner: DiscoveryOwner;
|
|
13
|
+
topics(): string[];
|
|
14
|
+
};
|
|
15
|
+
starting(): RetainedPublication[];
|
|
16
|
+
stopping(): RetainedPublication[];
|
|
17
|
+
output: number;
|
|
18
|
+
outputs: number;
|
|
19
|
+
}
|
|
20
|
+
export interface PublishingLifecycle {
|
|
21
|
+
startup(send: Send, announce?: RetainedPublication[]): void;
|
|
22
|
+
publish(send: Send, publications: RetainedPublication[]): void;
|
|
23
|
+
}
|
|
24
|
+
export declare function publishingLifecycle(RED: NodeAPI, node: Node, publishing: PublishingNode): PublishingLifecycle;
|
|
25
|
+
export {};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.publishingLifecycle = publishingLifecycle;
|
|
4
|
+
const ha_discovery_1 = require("../lib/ha-discovery");
|
|
5
|
+
const retained_topics_1 = require("../lib/retained-topics");
|
|
6
|
+
const adapter_1 = require("./adapter");
|
|
7
|
+
function publishingLifecycle(RED, node, publishing) {
|
|
8
|
+
const possible = () => publishing.possible ? (0, ha_discovery_1.ownDiscoveryTopics)(publishing.possible.owner, publishing.possible.topics()) : [];
|
|
9
|
+
function publish(send, publications) {
|
|
10
|
+
if (!publications.length)
|
|
11
|
+
return;
|
|
12
|
+
const envelope = Array.from({ length: publishing.outputs }, () => null);
|
|
13
|
+
envelope[publishing.output] = publications.map((entry) => ({ ...entry }));
|
|
14
|
+
send(envelope);
|
|
15
|
+
}
|
|
16
|
+
(0, adapter_1.whenFlowsStarted)(RED, node, () => {
|
|
17
|
+
publish(node.send.bind(node), [
|
|
18
|
+
...(0, adapter_1.clearStaleRetained)(node, publishing.retainedTopics(), "flows-started", possible()),
|
|
19
|
+
...publishing.starting()
|
|
20
|
+
]);
|
|
21
|
+
});
|
|
22
|
+
// Reaches the broker only if the MQTT out node is still running, as in a
|
|
23
|
+
// deploy of modified nodes or flows; the broker's own Close and Will
|
|
24
|
+
// messages cover a full deploy, a restart and a crash (see the README).
|
|
25
|
+
node.on("close", (removed, done) => {
|
|
26
|
+
publish(node.send.bind(node), (0, adapter_1.wasDeleted)(RED, node, removed)
|
|
27
|
+
? (0, retained_topics_1.retainedClears)((0, adapter_1.allRetainedTopics)(node, [...publishing.retainedTopics(), ...possible()]))
|
|
28
|
+
: publishing.stopping());
|
|
29
|
+
done();
|
|
30
|
+
});
|
|
31
|
+
return {
|
|
32
|
+
startup(send, announce = []) {
|
|
33
|
+
publish(send, [...(0, adapter_1.clearStaleRetained)(node, publishing.retainedTopics(), "startup", possible()), ...announce]);
|
|
34
|
+
},
|
|
35
|
+
publish
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=publishing-lifecycle.js.map
|
|
@@ -7,20 +7,32 @@
|
|
|
7
7
|
category: "home automation",
|
|
8
8
|
color: "#ffd27f",
|
|
9
9
|
defaults: {
|
|
10
|
-
|
|
11
|
-
//
|
|
10
|
+
// A marker only a node new from the palette has, as core nodes do: import
|
|
11
|
+
// and paste keep the saved name, so onadd leaves a saved node's settings.
|
|
12
|
+
name: {value: "_DEFAULT_"},
|
|
13
|
+
// Required only when the room publishes something named after it, and
|
|
14
|
+
// then with a letter or digit, from which its topics and IDs are made.
|
|
12
15
|
area: {value: "", validate: function (v) {
|
|
13
16
|
var publishing = $("#node-input-publishState").length
|
|
14
17
|
? $("#node-input-publishState").prop("checked") || $("#node-input-discovery").prop("checked")
|
|
15
18
|
: on(this.publishState) || on(this.discovery);
|
|
16
|
-
return !publishing ||
|
|
19
|
+
return !publishing || /[a-z0-9]/i.test(String(v || ""));
|
|
17
20
|
}},
|
|
18
21
|
// null means "not set": Node-RED fills it into a saved flow from before
|
|
19
22
|
// these settings, which keeps publishing. New nodes start with both
|
|
20
23
|
// off (onadd).
|
|
21
24
|
publishState: {value: null},
|
|
22
25
|
discovery: {value: null},
|
|
26
|
+
// How Home Assistant IDs are named. null means "not set": a node saved
|
|
27
|
+
// before this setting, which keeps its area-named entities. New nodes
|
|
28
|
+
// name them after the node (onadd), so renaming the area keeps them.
|
|
29
|
+
idScheme: {value: null},
|
|
30
|
+
// Where wake and bed times come from: null (not set) is this room.
|
|
31
|
+
// The house schedule it follows; none: its own times. Node-RED's
|
|
32
|
+
// editor offers an existing schedule first for a new room.
|
|
33
|
+
houseSchedule: {value: "", type: "home-house-schedule", required: false},
|
|
23
34
|
displayName: {value: ""},
|
|
35
|
+
discoveryPrefix: {value: "homeassistant", required: true, validate: function (v) { var prefix = String(v || "").trim().replace(/^\/+|\/+$/g, ""); return Boolean(prefix) && !/[+#]/.test(prefix); }},
|
|
24
36
|
targets: {value: "[]", required: true, validate: function (v) { try { JSON.parse(v); return true; } catch (e) { return false; } }},
|
|
25
37
|
maxBrightnessPct: {value: 100, required: true, validate: RED.validators.number()},
|
|
26
38
|
maxColorTempKelvin: {value: 6500, required: true, validate: RED.validators.number()},
|
|
@@ -34,6 +46,9 @@
|
|
|
34
46
|
bedTime: {value: "23:00", required: true, validate: function (v) { return /^([01]\d|2[0-3]):[0-5]\d$/.test(v); }}
|
|
35
47
|
},
|
|
36
48
|
onadd: function () {
|
|
49
|
+
if (this.name !== "_DEFAULT_") return;
|
|
50
|
+
this.name = "";
|
|
51
|
+
this.idScheme = "node";
|
|
37
52
|
this.publishState = false;
|
|
38
53
|
this.discovery = false;
|
|
39
54
|
},
|
|
@@ -65,6 +80,7 @@
|
|
|
65
80
|
<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>
|
|
66
81
|
<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>
|
|
67
82
|
<div class="form-row"><label for="node-input-displayName">Display name</label><input type="text" id="node-input-displayName"></div>
|
|
83
|
+
<div class="form-row"><label for="node-input-discoveryPrefix">Discovery prefix</label><input type="text" id="node-input-discoveryPrefix"></div>
|
|
68
84
|
<div class="form-row"><label for="node-input-targets">Targets JSON</label><input type="text" id="node-input-targets"></div>
|
|
69
85
|
<div class="form-row"><label for="node-input-maxBrightnessPct">Daytime brightness %</label><input type="number" id="node-input-maxBrightnessPct"></div>
|
|
70
86
|
<div class="form-row"><label for="node-input-maxColorTempKelvin">Daytime kelvin</label><input type="number" id="node-input-maxColorTempKelvin"></div>
|
|
@@ -73,6 +89,7 @@
|
|
|
73
89
|
<div class="form-row"><label for="node-input-initialTransitionSeconds">Initial transition seconds</label><input type="number" id="node-input-initialTransitionSeconds"></div>
|
|
74
90
|
<div class="form-row"><label for="node-input-refreshTransitionSeconds">Refresh transition seconds</label><input type="number" id="node-input-refreshTransitionSeconds"></div>
|
|
75
91
|
<div class="form-row"><label for="node-input-refreshSeconds">Refresh interval seconds</label><input type="number" id="node-input-refreshSeconds"></div>
|
|
92
|
+
<div class="form-row"><label for="node-input-houseSchedule">Schedule</label><input type="text" id="node-input-houseSchedule"></div>
|
|
76
93
|
<div class="form-row"><label for="node-input-wakeTime">Wake time</label><input type="text" id="node-input-wakeTime" placeholder="07:00"></div>
|
|
77
94
|
<div class="form-row"><label for="node-input-bedTime">Bed time</label><input type="text" id="node-input-bedTime" placeholder="23:00"></div>
|
|
78
95
|
<div class="form-row"><label for="node-input-initialSleep">Initial sleep mode</label><input type="checkbox" id="node-input-initialSleep" style="display:inline-block; width:auto;"></div>
|
|
@@ -87,18 +104,31 @@
|
|
|
87
104
|
topic, is taken as that command (traced as <code>mutated-own-command</code>), and it restyles a light after someone else's bare ON at most once every 5
|
|
88
105
|
seconds, so it never answers itself in a loop.</p>
|
|
89
106
|
<p>The room always commands its lights on each target's own command topic. Two settings add what it publishes
|
|
90
|
-
for other systems to see; both are named after the <b>Area</b>, which is needed only when one of them is on
|
|
107
|
+
for other systems to see; both are named after the <b>Area</b>, which is needed only when one of them is on. Topics
|
|
108
|
+
and IDs use it in lowercase, with spaces and other characters (anything but letters, digits, <code>_</code> and
|
|
109
|
+
<code>-</code>) as <code>_</code>: <i>Living Room</i> publishes under <code>home/living_room/…</code>.</p>
|
|
91
110
|
<dl class="message-properties">
|
|
92
111
|
<dt>Publish state over MQTT</dt><dd>each light's control state on
|
|
93
112
|
<code>home/<area>/lighting/<target>/control</code> and the circadian state on
|
|
94
|
-
<code>home/<area>/circadian/state</code
|
|
113
|
+
<code>home/<area>/circadian/state</code>. It also publishes a retained <code>online</code> on
|
|
114
|
+
<code>home/<area>/lighting/availability</code> once the flows start and <code>offline</code> as it stops, which the
|
|
115
|
+
circadian switch in Home Assistant follows. These get through only when this node is
|
|
116
|
+
deployed on its own, so set the MQTT broker node's Birth, Close and Will messages to the same (see the README).</dd>
|
|
95
117
|
<dt>Home Assistant discovery</dt><dd>a circadian switch in Home Assistant, which reads that state and sends
|
|
96
118
|
ON/OFF to <code>home/<area>/circadian/set</code>. To act on it, wire an MQTT in node subscribed to that
|
|
97
119
|
topic into this node. It needs <b>Publish state</b>. Switching it off
|
|
98
120
|
removes the switch from Home Assistant at the next start.</dd>
|
|
121
|
+
<dt>Discovery prefix</dt><dd>the topic prefix Home Assistant listens on for discovery: <code>homeassistant</code>
|
|
122
|
+
unless you changed it in Home Assistant's MQTT settings.</dd>
|
|
99
123
|
</dl>
|
|
100
124
|
<p>New nodes start with both off. A node saved before these settings existed keeps publishing both.</p>
|
|
101
125
|
<h3>Circadian schedule</h3>
|
|
126
|
+
<p><b>Schedule</b>: a house schedule the room follows, shared with other rooms. Following it, the room takes its
|
|
127
|
+
wake and bed times from it whenever they change, has no wake and bed time entities of its own in Home Assistant
|
|
128
|
+
(they are cleared), and ignores its own wake-time and bed-time set topics. With none, the room keeps its own
|
|
129
|
+
<b>Wake time</b> and <b>Bed time</b>. If the schedule is missing when the room starts, as when it was deleted
|
|
130
|
+
from the flows file, the room keeps its own times and its status says <i>schedule missing: own times</i>; deleting
|
|
131
|
+
a schedule in the editor sets its rooms to none.</p>
|
|
102
132
|
<p>Circadian levels follow <b>Wake time</b> and <b>Bed time</b> (24-hour <code>HH:MM</code>, default 07:00 and
|
|
103
133
|
23:00): they rise to the daytime level over the hour after waking, stay at the daytime brightness and colour
|
|
104
134
|
through the day, wind down to the sleep settings over the three hours before bed, and stay at the sleep
|
|
@@ -120,12 +150,26 @@
|
|
|
120
150
|
seen at the light), or a release. Any other intent, including motion ON or OFF and owner-less values, leaves it
|
|
121
151
|
alone and is traced as <code>ignored held-by</code>; other targets in the same intent are still handled.
|
|
122
152
|
Switching a light off by its owner or a person returns it to in-control.</p>
|
|
153
|
+
<p>The control states are kept in the node's context. When the node starts with nothing saved (its first deploy,
|
|
154
|
+
or a redeploy or restart without a context store, see the README), it can't know which lights were held or set
|
|
155
|
+
by a person, so it leaves every light as it is: no periodic refresh restyles a light until an intent or release
|
|
156
|
+
names it, someone switches it on with a bare ON on its command topic, or circadian lighting is switched on. Sleep
|
|
157
|
+
and schedule changes restyle only lights it has been told about since. A light that is on at that start and that
|
|
158
|
+
nothing names can keep its start-time style all day; to bring it back into step, switch circadian lighting on
|
|
159
|
+
(for example with the Home Assistant circadian switch), or let motion or a wall switch name it.</p>
|
|
123
160
|
<h3>Inputs</h3>
|
|
124
161
|
<dl class="message-properties">
|
|
125
162
|
<dt>topic = home/<area>/circadian/set</dt>
|
|
126
163
|
<dd>payload <code>ON</code> or <code>OFF</code> (any case) switches circadian lighting on or off, as
|
|
127
164
|
<code>lighting.kind = "circadian"</code> does. Read only while <b>Publish state</b> is on and the area is set.
|
|
128
165
|
Retained messages and other payloads are ignored.</dd>
|
|
166
|
+
<dt>topic = home/<area>/sleep/set</dt>
|
|
167
|
+
<dd><code>ON</code> or <code>OFF</code> switches sleep, as <code>lighting.kind = "sleep"</code> does, under the
|
|
168
|
+
same conditions.</dd>
|
|
169
|
+
<dt>topic = home/<area>/wake-time/set or bed-time/set</dt>
|
|
170
|
+
<dd>a time, <code>HH:MM</code> or <code>HH:MM:SS</code>, changes that time, as
|
|
171
|
+
<code>lighting = {kind: "schedule", wakeTime, bedTime}</code> does. A time set this way lasts until the node is
|
|
172
|
+
deployed with a different <b>Wake time</b> or <b>Bed time</b>, which then applies.</dd>
|
|
129
173
|
<dt>lighting.owner <span class="property-type">string</span></dt>
|
|
130
174
|
<dd>on an <code>intent</code> that switches targets on, marks them overridden by this owner. With
|
|
131
175
|
<code>preserveManual: true</code>, manual lights are skipped.</dd>
|
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
const room_controller_1 = require("../lib/room-controller");
|
|
3
2
|
const circadian_1 = require("../lib/circadian");
|
|
4
3
|
const circadian_schedule_1 = require("../lib/circadian-schedule");
|
|
4
|
+
const publishing_lifecycle_1 = require("./publishing-lifecycle");
|
|
5
|
+
const room_publishing_1 = require("../lib/room-publishing");
|
|
5
6
|
const adapter_1 = require("./adapter");
|
|
7
|
+
const shared_schedule_1 = require("./shared-schedule");
|
|
6
8
|
const settings_1 = require("../lib/settings");
|
|
9
|
+
const ha_discovery_1 = require("../lib/ha-discovery");
|
|
10
|
+
const topic_names_1 = require("../lib/topic-names");
|
|
7
11
|
function parseTargets(value) {
|
|
8
12
|
const targets = (0, settings_1.jsonSetting)(value, "targets", []);
|
|
9
13
|
if (!Array.isArray(targets) || targets.some((target) => {
|
|
@@ -30,14 +34,18 @@ const registerRoomController = (RED) => {
|
|
|
30
34
|
let refreshTimer;
|
|
31
35
|
let controllerConfig;
|
|
32
36
|
let refreshSeconds;
|
|
37
|
+
// A schedule that is set but not running, as when it was deleted: the
|
|
38
|
+
// room keeps to its own times and says so.
|
|
39
|
+
const { schedule: house, missing: houseMissing } = (0, shared_schedule_1.referencedSchedule)(RED, config.houseSchedule);
|
|
33
40
|
try {
|
|
34
41
|
// Not set (null, or absent from a flow saved before these settings)
|
|
35
42
|
// means on, so an older flow keeps publishing as before.
|
|
36
43
|
const publishState = (0, settings_1.optionalBooleanSetting)(config.publishState, "publish state") ?? true;
|
|
37
44
|
const discovery = (0, settings_1.optionalBooleanSetting)(config.discovery, "Home Assistant discovery") ?? true;
|
|
38
|
-
// Both publish under topics and IDs named after the area
|
|
39
|
-
|
|
40
|
-
|
|
45
|
+
// Both publish under topics and IDs named after the area; otherwise the
|
|
46
|
+
// area is only a label.
|
|
47
|
+
if (publishState || discovery)
|
|
48
|
+
(0, topic_names_1.publishingArea)(area, true);
|
|
41
49
|
refreshSeconds = Math.max(10, (0, settings_1.numberSetting)(config.refreshSeconds, "refresh interval", 60));
|
|
42
50
|
// Saved latitude, longitude and minimum settings from earlier versions
|
|
43
51
|
// are ignored.
|
|
@@ -49,8 +57,14 @@ const registerRoomController = (RED) => {
|
|
|
49
57
|
};
|
|
50
58
|
const schedule = (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"));
|
|
51
59
|
controllerConfig = {
|
|
60
|
+
nodeId: node.id,
|
|
61
|
+
// Not set: a node saved before node IDs, which keeps its area-named
|
|
62
|
+
// entities. The editor gives new nodes "node".
|
|
63
|
+
idScheme: (0, settings_1.optionalChoiceSetting)(config.idScheme, "ID scheme", ha_discovery_1.ID_SCHEMES) ?? "area",
|
|
64
|
+
timesFrom: house ? "house" : "room",
|
|
52
65
|
area,
|
|
53
66
|
displayName: config.displayName || area,
|
|
67
|
+
discoveryPrefix: (0, ha_discovery_1.discoveryPrefixSetting)(config.discoveryPrefix),
|
|
54
68
|
publishState,
|
|
55
69
|
discovery,
|
|
56
70
|
targets: parseTargets(config.targets),
|
|
@@ -59,25 +73,35 @@ const registerRoomController = (RED) => {
|
|
|
59
73
|
initialSleep: (0, settings_1.booleanSetting)(config.initialSleep, "initial sleep", false),
|
|
60
74
|
// The adapter picks the circadian lighting; the controller only asks
|
|
61
75
|
// it how a light should look.
|
|
62
|
-
lighting: (0, circadian_1.scheduledLighting)(schedule, levels)
|
|
76
|
+
lighting: (0, circadian_1.scheduledLighting)(schedule, levels),
|
|
77
|
+
schedule
|
|
63
78
|
};
|
|
64
79
|
}
|
|
65
80
|
catch (error) {
|
|
66
81
|
(0, adapter_1.invalidConfig)(node, error);
|
|
67
82
|
return;
|
|
68
83
|
}
|
|
84
|
+
// Available, and clear of what it no longer publishes, as soon as it
|
|
85
|
+
// runs; offline as it stops; nothing left behind when deleted.
|
|
86
|
+
const lifecycle = (0, publishing_lifecycle_1.publishingLifecycle)(RED, node, {
|
|
87
|
+
retainedTopics: () => (0, room_publishing_1.roomRetainedTopics)(controllerConfig),
|
|
88
|
+
starting: () => (0, room_publishing_1.roomStartingOutputs)(controllerConfig),
|
|
89
|
+
stopping: () => (0, room_publishing_1.roomStoppingOutputs)(controllerConfig),
|
|
90
|
+
output: 0,
|
|
91
|
+
outputs: 1
|
|
92
|
+
});
|
|
69
93
|
function updateStatus(status) {
|
|
70
94
|
node.status({
|
|
71
|
-
fill: status.manualCount ? "yellow" : status.circadianEnabled ? "green" : "grey",
|
|
95
|
+
fill: status.manualCount || houseMissing ? "yellow" : status.circadianEnabled ? "green" : "grey",
|
|
72
96
|
shape: "dot",
|
|
73
|
-
text: (status.circadianEnabled
|
|
97
|
+
text: (houseMissing ? "schedule missing: own times · " : "") + (status.circadianEnabled
|
|
74
98
|
? (status.sleeping ? "sleep" : "circadian")
|
|
75
99
|
: "manual only") + " · " + status.onCount + " on · " + status.manualCount + " manual" +
|
|
76
100
|
(status.overriddenCount ? " · " + status.overriddenCount + " overridden" : "")
|
|
77
101
|
});
|
|
78
102
|
}
|
|
79
103
|
function process(msg, send) {
|
|
80
|
-
const result = (0, adapter_1.runReducer)(node, { area: controllerConfig.area, component: "room-controller" }, (saved, now) => (0,
|
|
104
|
+
const result = (0, adapter_1.runReducer)(node, { area: controllerConfig.area, component: "room-controller" }, (saved, now) => (0, room_publishing_1.publishRoom)(saved, msg, controllerConfig, now));
|
|
81
105
|
if (result.outputs.length) {
|
|
82
106
|
send([result.outputs.map((output) => ({ ...output, payload: output.payload }))]);
|
|
83
107
|
}
|
|
@@ -85,6 +109,9 @@ const registerRoomController = (RED) => {
|
|
|
85
109
|
return result;
|
|
86
110
|
}
|
|
87
111
|
(0, adapter_1.onInput)(node, (msg, send) => {
|
|
112
|
+
// The startup's own announcement comes from publishRoom.
|
|
113
|
+
if ((0, adapter_1.objectField)(msg, "lighting")?.kind === "startup")
|
|
114
|
+
lifecycle.startup(send);
|
|
88
115
|
process({
|
|
89
116
|
lighting: (0, adapter_1.objectField)(msg, "lighting"),
|
|
90
117
|
topic: typeof msg.topic === "string" ? msg.topic : undefined,
|
|
@@ -92,9 +119,41 @@ const registerRoomController = (RED) => {
|
|
|
92
119
|
retain: msg.retain === true
|
|
93
120
|
}, send);
|
|
94
121
|
});
|
|
122
|
+
// Times taken from a house are the house's: a room that no longer follows
|
|
123
|
+
// one, as when its schedule was set to none or deleted, goes back to its
|
|
124
|
+
// own times rather than keeping the house's last ones.
|
|
125
|
+
const roomTrace = { area: controllerConfig.area, component: "room-controller" };
|
|
126
|
+
if (house) {
|
|
127
|
+
node.context().set("followsHouse", true);
|
|
128
|
+
}
|
|
129
|
+
else if (node.context().get("followsHouse") === true) {
|
|
130
|
+
node.context().set("followsHouse", false);
|
|
131
|
+
(0, adapter_1.runReducer)(node, roomTrace, (saved) => ({
|
|
132
|
+
state: { ...saved, schedule: undefined, scheduleSetting: undefined },
|
|
133
|
+
traces: [{ event: "house-schedule", outcome: "changed", reason: "own-times", details: {} }]
|
|
134
|
+
}));
|
|
135
|
+
}
|
|
136
|
+
if (house) {
|
|
137
|
+
// The house's times: once the flows have started, as this room may
|
|
138
|
+
// have restarted alone, and at every change until it stops.
|
|
139
|
+
const follow = (0, adapter_1.guarded)(node, () => {
|
|
140
|
+
process({ lighting: { kind: "schedule", ...house.schedule() } }, node.send.bind(node));
|
|
141
|
+
});
|
|
142
|
+
(0, adapter_1.whenFlowsStarted)(RED, node, follow);
|
|
143
|
+
node.on("close", house.subscribe(follow));
|
|
144
|
+
}
|
|
145
|
+
else if (houseMissing) {
|
|
146
|
+
(0, adapter_1.runReducer)(node, roomTrace, (state) => ({
|
|
147
|
+
state,
|
|
148
|
+
traces: [{ event: "house-schedule", outcome: "ignored", reason: "schedule-missing", details: { schedule: config.houseSchedule } }]
|
|
149
|
+
}));
|
|
150
|
+
node.status({ fill: "yellow", shape: "ring", text: "schedule missing: own times" });
|
|
151
|
+
}
|
|
95
152
|
refreshTimer = setInterval((0, adapter_1.guarded)(node, () => {
|
|
96
153
|
process({ lighting: { kind: "refresh" } }, node.send.bind(node));
|
|
97
154
|
}), refreshSeconds * 1000);
|
|
155
|
+
// Available again as soon as this node runs, as after a redeploy of it
|
|
156
|
+
// alone, rather than waiting for the flow's startup message.
|
|
98
157
|
node.on("close", (_removed, done) => {
|
|
99
158
|
clearInterval(refreshTimer);
|
|
100
159
|
done();
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.referencedSchedule = referencedSchedule;
|
|
4
|
+
// The house schedule a node's setting references. Not set (or "", as the
|
|
5
|
+
// editor saves "none") means no schedule; one that is set but isn't running,
|
|
6
|
+
// as when it was deleted or its settings are invalid, is missing.
|
|
7
|
+
function referencedSchedule(RED, id) {
|
|
8
|
+
if (typeof id !== "string" || id === "")
|
|
9
|
+
return { schedule: null, missing: false };
|
|
10
|
+
const node = RED.nodes.getNode(id);
|
|
11
|
+
return typeof node?.subscribe === "function" && typeof node.schedule === "function"
|
|
12
|
+
? { schedule: node, missing: false }
|
|
13
|
+
: { schedule: null, missing: true };
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=shared-schedule.js.map
|
|
@@ -55,9 +55,11 @@ const registerSourceCapabilityController = (RED) => {
|
|
|
55
55
|
schedule(result.state);
|
|
56
56
|
return result;
|
|
57
57
|
}
|
|
58
|
-
//
|
|
59
|
-
//
|
|
60
|
-
//
|
|
58
|
+
// A pending transition is here only if the node's context survived its
|
|
59
|
+
// stop, which needs a configured context store (see the README); timers
|
|
60
|
+
// never survive. Recover it with the reducer's own startup (re-observe,
|
|
61
|
+
// never replay) rather than ticking old state; a later startup from the
|
|
62
|
+
// flow repeats it.
|
|
61
63
|
if (context.get("state")?.pending) {
|
|
62
64
|
timer = setTimeout((0, adapter_1.guarded)(node, () => {
|
|
63
65
|
process({ sourceCapability: { kind: "startup" } }, node.send.bind(node));
|
|
@@ -6,7 +6,6 @@
|
|
|
6
6
|
name: {value: ""},
|
|
7
7
|
area: {value: ""},
|
|
8
8
|
freshnessMs: {value: 30000, required: true, validate: RED.validators.number()},
|
|
9
|
-
recoveryOffActivity: {value: "off", required: true},
|
|
10
9
|
// User data with no sensible default: empty until the user fills it in,
|
|
11
10
|
// and marked invalid until then, as the runtime rejects it.
|
|
12
11
|
activityProfiles: {value: "{}", required: true, validate: function (v) { try { var parsed = JSON.parse(v); return Boolean(parsed) && typeof parsed === "object" && !Array.isArray(parsed) && Object.keys(parsed).length > 0; } catch (e) { return false; } }}
|
|
@@ -23,12 +22,11 @@
|
|
|
23
22
|
<div class="form-row"><label for="node-input-name">Name</label><input type="text" id="node-input-name"></div>
|
|
24
23
|
<div class="form-row"><label for="node-input-area">Area</label><input type="text" id="node-input-area"></div>
|
|
25
24
|
<div class="form-row"><label for="node-input-freshnessMs">Freshness ms</label><input type="number" id="node-input-freshnessMs"></div>
|
|
26
|
-
<div class="form-row"><label for="node-input-recoveryOffActivity">Recovery Off activity</label><input type="text" id="node-input-recoveryOffActivity"></div>
|
|
27
25
|
<div class="form-row"><label for="node-input-activityProfiles">Profiles JSON</label><input type="text" id="node-input-activityProfiles"></div>
|
|
28
26
|
</script>
|
|
29
27
|
|
|
30
28
|
<script type="text/html" data-help-name="home-theatre-activity-controller">
|
|
31
29
|
<p>Owns latest-request-wins whole-theatre activity state and aggregates generation-scoped target evidence.</p>
|
|
32
30
|
<p>Input <code>msg.theatre</code> accepts activity intent, target-state, startup, refresh, recovery and tick events. Messages without <code>msg.theatre</code>, such as a target controller's own events on the same wire, are ignored. Output 1 emits semantic <code>msg.theatreCommand</code> messages for target owners. Output 2 emits activity events and a complete state snapshot. Device transports remain separate.</p>
|
|
33
|
-
<p>Startup without persisted intent probes physical state. One fresh profile match is adopted without commands
|
|
31
|
+
<p>Startup without persisted intent probes physical state. One fresh profile match is adopted without commands. With no evidence, or more than one match, the activity is unknown and nothing is commanded until the next request, so a redeploy or restart never switches a running activity off. Manual overrides are held without corrective commands and adopted only when the physical state uniquely matches a profile.</p>
|
|
34
32
|
</script>
|
|
@@ -13,8 +13,7 @@ const registerTheatreActivityController = (RED) => {
|
|
|
13
13
|
controllerConfig = {
|
|
14
14
|
area: (0, adapter_1.optionalArea)(config),
|
|
15
15
|
freshnessMs: Math.max(1000, (0, settings_1.numberSetting)(config.freshnessMs, "freshness", 30000)),
|
|
16
|
-
activityProfiles: (0, theatre_activity_controller_1.normaliseActivityProfiles)((0, settings_1.jsonSetting)(config.activityProfiles, "activity profiles", {}))
|
|
17
|
-
recoveryOffActivity: (0, settings_1.textSetting)(config.recoveryOffActivity, "recovery off activity", "off")
|
|
16
|
+
activityProfiles: (0, theatre_activity_controller_1.normaliseActivityProfiles)((0, settings_1.jsonSetting)(config.activityProfiles, "activity profiles", {}))
|
|
18
17
|
};
|
|
19
18
|
}
|
|
20
19
|
catch (error) {
|
|
@@ -7,21 +7,33 @@
|
|
|
7
7
|
category: "home automation",
|
|
8
8
|
color: "#d9c7f1",
|
|
9
9
|
defaults: {
|
|
10
|
-
|
|
11
|
-
//
|
|
10
|
+
// A marker only a node new from the palette has, as core nodes do: import
|
|
11
|
+
// and paste keep the saved name, so onadd leaves a saved node's settings.
|
|
12
|
+
name: {value: "_DEFAULT_"},
|
|
13
|
+
// Required for the default base topic home/<area>/theatre, and for the
|
|
14
|
+
// discovery of a contract saved before node IDs; when set, it needs a
|
|
15
|
+
// letter or digit, from which topics and IDs are made.
|
|
12
16
|
area: {value: "", validate: function (v) {
|
|
13
17
|
var dialog = $("#node-input-discovery").length > 0;
|
|
14
18
|
var discovery = dialog ? $("#node-input-discovery").prop("checked") : on(this.discovery);
|
|
15
19
|
var baseTopic = dialog ? $("#node-input-baseTopic").val() : this.baseTopic;
|
|
16
|
-
|
|
20
|
+
var area = String(v || "").trim();
|
|
21
|
+
// Needed for the default base topic, and for the entities of a
|
|
22
|
+
// contract saved before node IDs.
|
|
23
|
+
var needed = !baseTopic || (discovery && this.idScheme !== "node");
|
|
24
|
+
return area ? /[a-z0-9]/i.test(area) : !needed;
|
|
17
25
|
}},
|
|
18
26
|
// null means "not set": Node-RED fills it into a saved flow from before
|
|
19
27
|
// this setting, which keeps publishing discovery. New nodes start with
|
|
20
28
|
// it off (onadd).
|
|
21
29
|
discovery: {value: null},
|
|
30
|
+
// How Home Assistant IDs are named. null means "not set": a node saved
|
|
31
|
+
// before this setting, which keeps its area-named entities. New nodes
|
|
32
|
+
// name them after the node (onadd), so renaming the area keeps them.
|
|
33
|
+
idScheme: {value: null},
|
|
22
34
|
displayName: {value: ""},
|
|
23
35
|
baseTopic: {value: ""},
|
|
24
|
-
discoveryPrefix: {value: "homeassistant", required: true},
|
|
36
|
+
discoveryPrefix: {value: "homeassistant", required: true, validate: function (v) { var prefix = String(v || "").trim().replace(/^\/+|\/+$/g, ""); return Boolean(prefix) && !/[+#]/.test(prefix); }},
|
|
25
37
|
// User data with no sensible default: empty until the user fills it in,
|
|
26
38
|
// and marked invalid until then, as the runtime rejects it.
|
|
27
39
|
activities: {value: "[]", required: true, validate: function (v) { try { var parsed = JSON.parse(v); return Array.isArray(parsed) && parsed.length > 0; } catch (e) { return false; } }},
|
|
@@ -31,6 +43,9 @@
|
|
|
31
43
|
sloSuccessObjectivePct: {value: 95, required: true, validate: RED.validators.number()}
|
|
32
44
|
},
|
|
33
45
|
onadd: function () {
|
|
46
|
+
if (this.name !== "_DEFAULT_") return;
|
|
47
|
+
this.name = "";
|
|
48
|
+
this.idScheme = "node";
|
|
34
49
|
this.discovery = false;
|
|
35
50
|
},
|
|
36
51
|
oneditprepare: function () {
|
|
@@ -64,7 +79,15 @@
|
|
|
64
79
|
|
|
65
80
|
<script type="text/html" data-help-name="home-theatre-state-contract">
|
|
66
81
|
<p>Bridges non-retained MQTT activity requests to the theatre controller and publishes retained controller truth.</p>
|
|
67
|
-
<p>Requests arrive on <code><base>/request</code> with a request ID and activity. Retained requests are inert. Complete <code>msg.theatre</code> state snapshots publish desired, current, phase,
|
|
82
|
+
<p>Requests arrive on <code><base>/request</code> with a request ID and activity. Retained requests are inert. Complete <code>msg.theatre</code> state snapshots publish desired, current, phase, transition (started and completed times and degraded targets, shown as the
|
|
83
|
+
Phase sensor's attributes in Home Assistant), failure, readiness, operational SLO and per-target health topics. </p>
|
|
84
|
+
<p>With discovery, a contract added since node IDs suggests its <b>Area</b> to Home Assistant for its device, and
|
|
85
|
+
Home Assistant creates an area of that name if none exists. With no area, its entity IDs come from the
|
|
86
|
+
<b>Display name</b> and no area is suggested.</p>
|
|
87
|
+
<p>Once the flows start it publishes a retained <code>online</code> on <code><base>/availability</code>, and
|
|
88
|
+
<code>offline</code> as it stops; its Home Assistant entities follow it. These get through only
|
|
89
|
+
when this node is deployed on its own, so set the MQTT broker node's Birth, Close and Will messages to the same (see the
|
|
90
|
+
README).</p>
|
|
68
91
|
<p>With <b>Home Assistant discovery</b> on, a startup event also publishes discovery for the activity selector and
|
|
69
92
|
status sensors; switched off, it clears them, so Home Assistant removes them. New nodes start with it off; a node
|
|
70
93
|
saved before the setting existed keeps it on.</p>
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
const theatre_state_contract_1 = require("../lib/theatre-state-contract");
|
|
3
|
+
const publishing_lifecycle_1 = require("./publishing-lifecycle");
|
|
3
4
|
const adapter_1 = require("./adapter");
|
|
4
5
|
const settings_1 = require("../lib/settings");
|
|
6
|
+
const ha_discovery_1 = require("../lib/ha-discovery");
|
|
5
7
|
const registerTheatreStateContract = (RED) => {
|
|
6
8
|
function HomeTheatreStateContract(config) {
|
|
7
9
|
RED.nodes.createNode(this, config);
|
|
@@ -13,6 +15,10 @@ const registerTheatreStateContract = (RED) => {
|
|
|
13
15
|
try {
|
|
14
16
|
const activities = (0, settings_1.jsonSetting)(config.activities, "activities", []);
|
|
15
17
|
controllerConfig = (0, theatre_state_contract_1.normaliseStateContractConfig)({
|
|
18
|
+
nodeId: node.id,
|
|
19
|
+
// Not set: a node saved before node IDs, which keeps its area-named
|
|
20
|
+
// entities. The editor gives new nodes "node".
|
|
21
|
+
idScheme: (0, settings_1.optionalChoiceSetting)(config.idScheme, "ID scheme", ha_discovery_1.ID_SCHEMES) ?? "area",
|
|
16
22
|
area,
|
|
17
23
|
// Optional, with documented meanings: an empty display name is the
|
|
18
24
|
// area, and an empty base topic is home/<area>/theatre.
|
|
@@ -21,7 +27,7 @@ const registerTheatreStateContract = (RED) => {
|
|
|
21
27
|
// Not set (null, or absent from a flow saved before the setting)
|
|
22
28
|
// means on, so an older flow keeps publishing discovery.
|
|
23
29
|
discovery: (0, settings_1.optionalBooleanSetting)(config.discovery, "Home Assistant discovery") ?? true,
|
|
24
|
-
discoveryPrefix: (0,
|
|
30
|
+
discoveryPrefix: (0, ha_discovery_1.discoveryPrefixSetting)(config.discoveryPrefix),
|
|
25
31
|
activities: (Array.isArray(activities) ? activities.map(String) : activities),
|
|
26
32
|
slo: {
|
|
27
33
|
windowSize: (0, settings_1.numberSetting)(config.sloWindowSize, "SLO window", 20),
|
|
@@ -35,6 +41,15 @@ const registerTheatreStateContract = (RED) => {
|
|
|
35
41
|
(0, adapter_1.invalidConfig)(node, error);
|
|
36
42
|
return;
|
|
37
43
|
}
|
|
44
|
+
// Available, and clear of what it no longer publishes, as soon as it
|
|
45
|
+
// runs; offline as it stops; nothing left behind when deleted.
|
|
46
|
+
const lifecycle = (0, publishing_lifecycle_1.publishingLifecycle)(RED, node, {
|
|
47
|
+
retainedTopics: () => (0, theatre_state_contract_1.theatreRetainedTopics)(controllerConfig),
|
|
48
|
+
starting: () => (0, theatre_state_contract_1.theatreStartingPublications)(controllerConfig),
|
|
49
|
+
stopping: () => (0, theatre_state_contract_1.theatreStoppingPublications)(controllerConfig),
|
|
50
|
+
output: 1,
|
|
51
|
+
outputs: 2
|
|
52
|
+
});
|
|
38
53
|
function process(msg, send) {
|
|
39
54
|
const result = (0, adapter_1.runReducer)(node, { area: controllerConfig.area, component: "theatre-state-contract" }, (saved, now) => (0, theatre_state_contract_1.transitionTheatreStateContract)(saved, msg, controllerConfig, now));
|
|
40
55
|
if (result.requests.length) {
|
|
@@ -54,6 +69,10 @@ const registerTheatreStateContract = (RED) => {
|
|
|
54
69
|
return result;
|
|
55
70
|
}
|
|
56
71
|
(0, adapter_1.onInput)(node, (msg, send) => {
|
|
72
|
+
// The startup's own announcement comes from the contract's transition.
|
|
73
|
+
if ((0, adapter_1.objectField)(msg, "theatreStateContract")?.kind === "startup") {
|
|
74
|
+
lifecycle.startup(send);
|
|
75
|
+
}
|
|
57
76
|
process({
|
|
58
77
|
topic: typeof msg.topic === "string" ? msg.topic : undefined,
|
|
59
78
|
payload: msg.payload,
|
|
@@ -62,9 +81,8 @@ const registerTheatreStateContract = (RED) => {
|
|
|
62
81
|
theatreStateContract: (0, adapter_1.objectField)(msg, "theatreStateContract")
|
|
63
82
|
}, send);
|
|
64
83
|
});
|
|
65
|
-
node
|
|
66
|
-
|
|
67
|
-
});
|
|
84
|
+
// Available again as soon as this node runs, as after a redeploy of it
|
|
85
|
+
// alone, rather than waiting for the flow's startup message.
|
|
68
86
|
}
|
|
69
87
|
RED.nodes.registerType("home-theatre-state-contract", HomeTheatreStateContract);
|
|
70
88
|
};
|