@avantics/node-red-home-automation 2.2.1 → 3.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 +76 -89
- package/contract-examples/room-lighting.mjs +12 -9
- package/dist/lib/activity-lighting-controller.d.ts +13 -25
- package/dist/lib/activity-lighting-controller.js +15 -11
- package/dist/lib/avr-controller.d.ts +17 -28
- package/dist/lib/avr-controller.js +165 -116
- package/dist/lib/backlighting-controller.d.ts +12 -25
- package/dist/lib/backlighting-controller.js +11 -18
- package/dist/lib/bounded-trace.d.ts +6 -0
- package/dist/lib/circadian-schedule.d.ts +11 -0
- package/dist/lib/circadian-schedule.js +63 -0
- package/dist/lib/circadian.d.ts +6 -0
- package/dist/lib/circadian.js +15 -0
- package/dist/lib/command-echo.d.ts +20 -0
- package/dist/lib/command-echo.js +82 -0
- package/dist/lib/cover-controller.d.ts +15 -28
- package/dist/lib/cover-controller.js +6 -8
- package/dist/lib/denon-protocol.d.ts +29 -0
- package/dist/lib/denon-protocol.js +76 -0
- package/dist/lib/display-mode-controller.d.ts +11 -6
- package/dist/lib/display-mode-controller.js +90 -39
- package/dist/lib/light-styling.d.ts +11 -0
- package/dist/lib/light-styling.js +15 -0
- package/dist/lib/motion-policy.d.ts +12 -22
- package/dist/lib/motion-policy.js +53 -56
- package/dist/lib/occupancy-fusion.d.ts +8 -12
- package/dist/lib/occupancy-fusion.js +3 -2
- package/dist/lib/room-controller.d.ts +13 -38
- package/dist/lib/room-controller.js +85 -180
- package/dist/lib/room-lighting-discovery.d.ts +9 -0
- package/dist/lib/room-lighting-discovery.js +45 -0
- package/dist/lib/settings.d.ts +5 -0
- package/dist/lib/settings.js +68 -0
- package/dist/lib/source-capability-controller.d.ts +32 -35
- package/dist/lib/source-capability-controller.js +51 -35
- package/dist/lib/switch-policy.d.ts +2 -7
- package/dist/lib/target-state.d.ts +24 -0
- package/dist/lib/target-state.js +38 -0
- package/dist/lib/theatre-activity-controller.d.ts +6 -108
- package/dist/lib/theatre-activity-controller.js +67 -318
- package/dist/lib/theatre-ha-discovery.d.ts +14 -0
- package/dist/lib/theatre-ha-discovery.js +163 -0
- package/dist/lib/theatre-metrics.d.ts +4 -0
- package/dist/lib/theatre-metrics.js +66 -0
- package/dist/lib/theatre-profiles.d.ts +23 -0
- package/dist/lib/theatre-profiles.js +73 -0
- package/dist/lib/theatre-refresh.d.ts +20 -0
- package/dist/lib/theatre-refresh.js +46 -0
- package/dist/lib/theatre-slo.js +7 -9
- package/dist/lib/theatre-state-contract.d.ts +7 -19
- package/dist/lib/theatre-state-contract.js +33 -169
- package/dist/lib/theatre-state.d.ts +110 -0
- package/dist/lib/theatre-state.js +139 -0
- package/dist/lib/wall-input.d.ts +2 -7
- package/dist/lib/z2m-light.d.ts +16 -0
- package/dist/lib/z2m-light.js +36 -0
- package/dist/nodes/activity-lighting-controller.html +5 -3
- package/dist/nodes/activity-lighting-controller.js +21 -52
- package/dist/nodes/adapter.d.ts +15 -0
- package/dist/nodes/adapter.js +60 -0
- package/dist/nodes/avr-controller.html +13 -4
- package/dist/nodes/avr-controller.js +23 -62
- package/dist/nodes/backlighting-controller.html +1 -1
- package/dist/nodes/backlighting-controller.js +25 -47
- package/dist/nodes/display-mode-controller.html +4 -4
- package/dist/nodes/display-mode-controller.js +22 -59
- package/dist/nodes/motion-policy.html +23 -10
- package/dist/nodes/motion-policy.js +14 -135
- package/dist/nodes/occupancy-fusion.html +2 -2
- package/dist/nodes/occupancy-fusion.js +23 -29
- package/dist/nodes/room-controller.html +92 -34
- package/dist/nodes/room-controller.js +46 -64
- package/dist/nodes/source-capability-controller.html +4 -13
- package/dist/nodes/source-capability-controller.js +17 -52
- package/dist/nodes/switch-policy.html +3 -3
- package/dist/nodes/switch-policy.js +23 -42
- package/dist/nodes/theatre-activity-controller.html +5 -3
- package/dist/nodes/theatre-activity-controller.js +23 -43
- package/dist/nodes/theatre-state-contract.html +55 -22
- package/dist/nodes/theatre-state-contract.js +30 -41
- package/examples/Home theatre activity.json +307 -43
- package/examples/Home theatre equipment.json +4 -4
- package/examples/Room lighting.json +15 -17
- package/package.json +3 -7
- package/THIRD-PARTY-NOTICES.md +0 -218
|
@@ -1,21 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
const bounded_trace_1 = require("../lib/bounded-trace");
|
|
3
2
|
const room_controller_1 = require("../lib/room-controller");
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
function requiredNumber(value, name) {
|
|
9
|
-
if (value === "" || value === null || value === undefined) {
|
|
10
|
-
throw new Error(`${name} is required`);
|
|
11
|
-
}
|
|
12
|
-
const parsed = Number(value);
|
|
13
|
-
if (!Number.isFinite(parsed))
|
|
14
|
-
throw new Error(`${name} must be a number`);
|
|
15
|
-
return parsed;
|
|
16
|
-
}
|
|
3
|
+
const circadian_1 = require("../lib/circadian");
|
|
4
|
+
const circadian_schedule_1 = require("../lib/circadian-schedule");
|
|
5
|
+
const adapter_1 = require("./adapter");
|
|
6
|
+
const settings_1 = require("../lib/settings");
|
|
17
7
|
function parseTargets(value) {
|
|
18
|
-
const targets =
|
|
8
|
+
const targets = (0, settings_1.jsonSetting)(value, "targets", []);
|
|
19
9
|
if (!Array.isArray(targets) || targets.some((target) => {
|
|
20
10
|
if (!target || typeof target !== "object")
|
|
21
11
|
return true;
|
|
@@ -36,30 +26,44 @@ const registerRoomController = (RED) => {
|
|
|
36
26
|
function HomeRoomLightingController(config) {
|
|
37
27
|
RED.nodes.createNode(this, config);
|
|
38
28
|
const node = this;
|
|
39
|
-
const
|
|
29
|
+
const area = (0, adapter_1.optionalArea)(config);
|
|
40
30
|
let refreshTimer;
|
|
41
31
|
let controllerConfig;
|
|
32
|
+
let refreshSeconds;
|
|
42
33
|
try {
|
|
34
|
+
// Not set (null, or absent from a flow saved before these settings)
|
|
35
|
+
// means on, so an older flow keeps publishing as before.
|
|
36
|
+
const publishState = (0, settings_1.optionalBooleanSetting)(config.publishState, "publish state") ?? true;
|
|
37
|
+
const discovery = (0, settings_1.optionalBooleanSetting)(config.discovery, "Home Assistant discovery") ?? true;
|
|
38
|
+
// Both publish under topics and IDs named after the area.
|
|
39
|
+
if ((publishState || discovery) && !area)
|
|
40
|
+
throw new Error("area is required");
|
|
41
|
+
refreshSeconds = Math.max(10, (0, settings_1.numberSetting)(config.refreshSeconds, "refresh interval", 60));
|
|
42
|
+
// Saved latitude, longitude and minimum settings from earlier versions
|
|
43
|
+
// are ignored.
|
|
44
|
+
const levels = {
|
|
45
|
+
maxBrightnessPct: (0, settings_1.numberSetting)(config.maxBrightnessPct, "daytime brightness", 100),
|
|
46
|
+
maxColorTempKelvin: (0, settings_1.numberSetting)(config.maxColorTempKelvin, "daytime colour temperature", 6500),
|
|
47
|
+
sleepBrightnessPct: (0, settings_1.numberSetting)(config.sleepBrightnessPct, "sleep brightness", 1),
|
|
48
|
+
sleepColorTempKelvin: (0, settings_1.numberSetting)(config.sleepColorTempKelvin, "sleep colour temperature", 2200)
|
|
49
|
+
};
|
|
50
|
+
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"));
|
|
43
51
|
controllerConfig = {
|
|
44
|
-
area
|
|
45
|
-
displayName: config.displayName ||
|
|
52
|
+
area,
|
|
53
|
+
displayName: config.displayName || area,
|
|
54
|
+
publishState,
|
|
55
|
+
discovery,
|
|
46
56
|
targets: parseTargets(config.targets),
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
sleepBrightnessPct: number(config.sleepBrightnessPct, 1),
|
|
54
|
-
sleepColorTempKelvin: number(config.sleepColorTempKelvin, 1000),
|
|
55
|
-
initialTransitionSeconds: number(config.initialTransitionSeconds, 1),
|
|
56
|
-
refreshTransitionSeconds: number(config.refreshTransitionSeconds, 45),
|
|
57
|
-
initialSleep: config.initialSleep === true || config.initialSleep === "true"
|
|
57
|
+
initialTransitionSeconds: (0, settings_1.numberSetting)(config.initialTransitionSeconds, "initial transition", 1),
|
|
58
|
+
refreshTransitionSeconds: (0, settings_1.numberSetting)(config.refreshTransitionSeconds, "refresh transition", 60),
|
|
59
|
+
initialSleep: (0, settings_1.booleanSetting)(config.initialSleep, "initial sleep", false),
|
|
60
|
+
// The adapter picks the circadian lighting; the controller only asks
|
|
61
|
+
// it how a light should look.
|
|
62
|
+
lighting: (0, circadian_1.scheduledLighting)(schedule, levels)
|
|
58
63
|
};
|
|
59
64
|
}
|
|
60
65
|
catch (error) {
|
|
61
|
-
|
|
62
|
-
node.error(error);
|
|
66
|
+
(0, adapter_1.invalidConfig)(node, error);
|
|
63
67
|
return;
|
|
64
68
|
}
|
|
65
69
|
function updateStatus(status) {
|
|
@@ -73,46 +77,24 @@ const registerRoomController = (RED) => {
|
|
|
73
77
|
});
|
|
74
78
|
}
|
|
75
79
|
function process(msg, send) {
|
|
76
|
-
const
|
|
77
|
-
const saved = context.get("state");
|
|
78
|
-
const result = (0, room_controller_1.transitionRoom)(saved, msg, controllerConfig, now);
|
|
79
|
-
context.set("state", result.state);
|
|
80
|
-
(0, bounded_trace_1.appendTrace)(context, {
|
|
81
|
-
area: controllerConfig.area,
|
|
82
|
-
component: "room-controller"
|
|
83
|
-
}, result.traces, now);
|
|
80
|
+
const result = (0, adapter_1.runReducer)(node, { area: controllerConfig.area, component: "room-controller" }, (saved, now) => (0, room_controller_1.transitionRoom)(saved, msg, controllerConfig, now));
|
|
84
81
|
if (result.outputs.length) {
|
|
85
82
|
send([result.outputs.map((output) => ({ ...output, payload: output.payload }))]);
|
|
86
83
|
}
|
|
87
84
|
updateStatus(result.status);
|
|
88
85
|
return result;
|
|
89
86
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
payload: msg.payload,
|
|
98
|
-
retain: msg.retain === true
|
|
99
|
-
}, send);
|
|
100
|
-
done();
|
|
101
|
-
}
|
|
102
|
-
catch (error) {
|
|
103
|
-
node.error(error, msg);
|
|
104
|
-
done(error instanceof Error ? error : new Error(String(error)));
|
|
105
|
-
}
|
|
87
|
+
(0, adapter_1.onInput)(node, (msg, send) => {
|
|
88
|
+
process({
|
|
89
|
+
lighting: (0, adapter_1.objectField)(msg, "lighting"),
|
|
90
|
+
topic: typeof msg.topic === "string" ? msg.topic : undefined,
|
|
91
|
+
payload: msg.payload,
|
|
92
|
+
retain: msg.retain === true
|
|
93
|
+
}, send);
|
|
106
94
|
});
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
process({ lighting: { kind: "refresh" } }, node.send.bind(node));
|
|
111
|
-
}
|
|
112
|
-
catch (error) {
|
|
113
|
-
node.error(error);
|
|
114
|
-
}
|
|
115
|
-
}, refreshSeconds * 1000);
|
|
95
|
+
refreshTimer = setInterval((0, adapter_1.guarded)(node, () => {
|
|
96
|
+
process({ lighting: { kind: "refresh" } }, node.send.bind(node));
|
|
97
|
+
}), refreshSeconds * 1000);
|
|
116
98
|
node.on("close", (_removed, done) => {
|
|
117
99
|
clearInterval(refreshTimer);
|
|
118
100
|
done();
|
|
@@ -4,19 +4,10 @@
|
|
|
4
4
|
color: "#c7d9f1",
|
|
5
5
|
defaults: {
|
|
6
6
|
name: {value: ""},
|
|
7
|
-
area: {value: "
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
validate: function(value) {
|
|
12
|
-
try {
|
|
13
|
-
const parsed = JSON.parse(value);
|
|
14
|
-
return parsed && typeof parsed === "object" && !Array.isArray(parsed);
|
|
15
|
-
} catch (_) {
|
|
16
|
-
return false;
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
},
|
|
7
|
+
area: {value: ""},
|
|
8
|
+
// User data with no sensible default: empty until the user fills it in,
|
|
9
|
+
// and marked invalid until then, as the runtime rejects it.
|
|
10
|
+
sources: {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; } }},
|
|
20
11
|
pollMs: {value: 2000, required: true, validate: RED.validators.number()},
|
|
21
12
|
confirmationTimeoutMs: {value: 15000, required: true, validate: RED.validators.number()},
|
|
22
13
|
maxAttempts: {value: 2, required: true, validate: RED.validators.number()}
|
|
@@ -1,31 +1,22 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
const bounded_trace_1 = require("../lib/bounded-trace");
|
|
3
2
|
const source_capability_controller_1 = require("../lib/source-capability-controller");
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
return Number.isFinite(parsed) ? parsed : fallback;
|
|
7
|
-
}
|
|
3
|
+
const adapter_1 = require("./adapter");
|
|
4
|
+
const settings_1 = require("../lib/settings");
|
|
8
5
|
const registerSourceCapabilityController = (RED) => {
|
|
9
6
|
function HomeSourceCapabilityController(config) {
|
|
10
7
|
RED.nodes.createNode(this, config);
|
|
11
8
|
const node = this;
|
|
12
9
|
const context = node.context();
|
|
13
10
|
let timer;
|
|
14
|
-
let
|
|
11
|
+
let controllerConfig;
|
|
15
12
|
try {
|
|
16
|
-
|
|
13
|
+
// Defaults and limits come from the reducer's own normalisation.
|
|
14
|
+
controllerConfig = (0, source_capability_controller_1.normaliseConfig)({ ...config, sources: (0, settings_1.jsonSetting)(config.sources, "sources", {}) });
|
|
17
15
|
}
|
|
18
16
|
catch (error) {
|
|
19
|
-
|
|
20
|
-
node.error(error);
|
|
17
|
+
(0, adapter_1.invalidConfig)(node, error);
|
|
21
18
|
return;
|
|
22
19
|
}
|
|
23
|
-
const controllerConfig = {
|
|
24
|
-
sources,
|
|
25
|
-
pollMs: number(config.pollMs, 2000),
|
|
26
|
-
confirmationTimeoutMs: number(config.confirmationTimeoutMs, 15000),
|
|
27
|
-
maxAttempts: number(config.maxAttempts, 2)
|
|
28
|
-
};
|
|
29
20
|
function updateStatus(status) {
|
|
30
21
|
const appearances = {
|
|
31
22
|
idle: { fill: "grey", shape: "dot" },
|
|
@@ -45,24 +36,12 @@ const registerSourceCapabilityController = (RED) => {
|
|
|
45
36
|
if (!state.pending?.nextAt)
|
|
46
37
|
return;
|
|
47
38
|
const delay = Math.max(0, state.pending.nextAt - Date.now());
|
|
48
|
-
timer = setTimeout(() => {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
}
|
|
52
|
-
catch (error) {
|
|
53
|
-
node.error(error);
|
|
54
|
-
}
|
|
55
|
-
}, delay);
|
|
39
|
+
timer = setTimeout((0, adapter_1.guarded)(node, () => {
|
|
40
|
+
process({ sourceCapability: { kind: "tick", generation: state.generation } }, node.send.bind(node));
|
|
41
|
+
}), delay);
|
|
56
42
|
}
|
|
57
43
|
function process(msg, send) {
|
|
58
|
-
const
|
|
59
|
-
const saved = context.get("state");
|
|
60
|
-
const result = (0, source_capability_controller_1.transitionSource)(saved, msg, controllerConfig, now);
|
|
61
|
-
context.set("state", result.state);
|
|
62
|
-
(0, bounded_trace_1.appendTrace)(context, {
|
|
63
|
-
area: config.area || "lounge",
|
|
64
|
-
component: "source-capability-controller"
|
|
65
|
-
}, result.traces, now);
|
|
44
|
+
const result = (0, adapter_1.runReducer)(node, { area: (0, adapter_1.optionalArea)(config), component: "source-capability-controller" }, (saved, now) => (0, source_capability_controller_1.transitionSource)(saved, msg, controllerConfig, now));
|
|
66
45
|
if (result.commands.length) {
|
|
67
46
|
send([
|
|
68
47
|
result.commands.map((sourceCommand) => ({ payload: undefined, sourceCommand })),
|
|
@@ -80,28 +59,14 @@ const registerSourceCapabilityController = (RED) => {
|
|
|
80
59
|
// transition with the reducer's own startup (re-observe, never replay)
|
|
81
60
|
// rather than ticking old state; a later startup from the flow repeats it.
|
|
82
61
|
if (context.get("state")?.pending) {
|
|
83
|
-
timer = setTimeout(() => {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
}
|
|
87
|
-
catch (error) {
|
|
88
|
-
node.error(error);
|
|
89
|
-
}
|
|
90
|
-
}, 0);
|
|
62
|
+
timer = setTimeout((0, adapter_1.guarded)(node, () => {
|
|
63
|
+
process({ sourceCapability: { kind: "startup" } }, node.send.bind(node));
|
|
64
|
+
}), 0);
|
|
91
65
|
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
? msg.sourceCapability
|
|
97
|
-
: undefined
|
|
98
|
-
}, send);
|
|
99
|
-
done();
|
|
100
|
-
}
|
|
101
|
-
catch (error) {
|
|
102
|
-
node.error(error, msg);
|
|
103
|
-
done(error instanceof Error ? error : new Error(String(error)));
|
|
104
|
-
}
|
|
66
|
+
(0, adapter_1.onInput)(node, (msg, send) => {
|
|
67
|
+
process({
|
|
68
|
+
sourceCapability: (0, adapter_1.objectField)(msg, "sourceCapability")
|
|
69
|
+
}, send);
|
|
105
70
|
});
|
|
106
71
|
node.on("close", (_removed, done) => {
|
|
107
72
|
clearTimeout(timer);
|
|
@@ -4,15 +4,15 @@
|
|
|
4
4
|
color: "#d8bfd8",
|
|
5
5
|
defaults: {
|
|
6
6
|
name: {value: ""},
|
|
7
|
-
area: {value: "
|
|
8
|
-
targets: {value: "[\"main\"]", required: true},
|
|
7
|
+
area: {value: ""},
|
|
8
|
+
targets: {value: "[\"main\"]", required: true, validate: function (v) { try { JSON.parse(v); return true; } catch (e) { return false; } }},
|
|
9
9
|
switchWindowMs: {value: 500, required: true, validate: RED.validators.number()},
|
|
10
10
|
switchFreshnessSeconds: {value: 3600, required: true, validate: RED.validators.number()}
|
|
11
11
|
},
|
|
12
12
|
inputs: 1,
|
|
13
13
|
outputs: 1,
|
|
14
14
|
icon: "font-awesome/fa-toggle-on",
|
|
15
|
-
label: function () { return this.name || this.area + " switch policy"; }
|
|
15
|
+
label: function () { return this.name || (this.area ? this.area + " " : "") + "switch policy"; }
|
|
16
16
|
});
|
|
17
17
|
</script>
|
|
18
18
|
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
const bounded_trace_1 = require("../lib/bounded-trace");
|
|
3
2
|
const wall_input_1 = require("../lib/wall-input");
|
|
4
3
|
const switch_policy_1 = require("../lib/switch-policy");
|
|
4
|
+
const adapter_1 = require("./adapter");
|
|
5
|
+
const settings_1 = require("../lib/settings");
|
|
5
6
|
function ageText(ageMs) {
|
|
6
7
|
if (ageMs === null)
|
|
7
8
|
return "age ?";
|
|
@@ -12,7 +13,7 @@ function ageText(ageMs) {
|
|
|
12
13
|
return `${Math.floor(ageMs / 3_600_000)}h`;
|
|
13
14
|
}
|
|
14
15
|
function parseTargets(value) {
|
|
15
|
-
const parsed =
|
|
16
|
+
const parsed = (0, settings_1.jsonSetting)(value, "targets", ["main"]);
|
|
16
17
|
if (!Array.isArray(parsed) || parsed.length === 0 || parsed.some((item) => typeof item !== "string")) {
|
|
17
18
|
throw new Error("targets must be a non-empty JSON array of strings");
|
|
18
19
|
}
|
|
@@ -28,10 +29,10 @@ const registerSwitchPolicy = (RED) => {
|
|
|
28
29
|
let policyConfig;
|
|
29
30
|
try {
|
|
30
31
|
policyConfig = {
|
|
31
|
-
area: config
|
|
32
|
+
area: (0, adapter_1.optionalArea)(config),
|
|
32
33
|
targets: parseTargets(config.targets),
|
|
33
|
-
switchWindowMs:
|
|
34
|
-
switchFreshnessMs:
|
|
34
|
+
switchWindowMs: (0, settings_1.numberSetting)(config.switchWindowMs, "switch window", 500),
|
|
35
|
+
switchFreshnessMs: (0, settings_1.numberSetting)(config.switchFreshnessSeconds, "switch freshness", 3600) * 1000
|
|
35
36
|
};
|
|
36
37
|
if (!Number.isFinite(policyConfig.switchWindowMs) || policyConfig.switchWindowMs < 0) {
|
|
37
38
|
throw new Error("switch window must be a non-negative number");
|
|
@@ -41,8 +42,7 @@ const registerSwitchPolicy = (RED) => {
|
|
|
41
42
|
}
|
|
42
43
|
}
|
|
43
44
|
catch (error) {
|
|
44
|
-
|
|
45
|
-
node.error(error);
|
|
45
|
+
(0, adapter_1.invalidConfig)(node, error);
|
|
46
46
|
return;
|
|
47
47
|
}
|
|
48
48
|
function updateStatus(status) {
|
|
@@ -60,14 +60,7 @@ const registerSwitchPolicy = (RED) => {
|
|
|
60
60
|
}
|
|
61
61
|
}
|
|
62
62
|
function process(input, send) {
|
|
63
|
-
const
|
|
64
|
-
const saved = context.get("state");
|
|
65
|
-
const result = (0, switch_policy_1.transitionSwitchPolicy)(saved, input, policyConfig, now.getTime());
|
|
66
|
-
context.set("state", result.state);
|
|
67
|
-
(0, bounded_trace_1.appendTrace)(context, {
|
|
68
|
-
area: policyConfig.area,
|
|
69
|
-
component: "switch-policy"
|
|
70
|
-
}, result.traces, now);
|
|
63
|
+
const result = (0, adapter_1.runReducer)(node, { area: policyConfig.area, component: "switch-policy" }, (saved, now) => (0, switch_policy_1.transitionSwitchPolicy)(saved, input, policyConfig, now.getTime()));
|
|
71
64
|
for (const effect of result.effects) {
|
|
72
65
|
if (effect.kind === "cancel-switch-timer") {
|
|
73
66
|
clearTimeout(timer);
|
|
@@ -75,19 +68,14 @@ const registerSwitchPolicy = (RED) => {
|
|
|
75
68
|
}
|
|
76
69
|
else if (effect.kind === "schedule-switch-timer") {
|
|
77
70
|
clearTimeout(timer);
|
|
78
|
-
timer = setTimeout(() => {
|
|
71
|
+
timer = setTimeout((0, adapter_1.guarded)(node, () => {
|
|
79
72
|
timer = undefined;
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
}
|
|
87
|
-
catch (error) {
|
|
88
|
-
node.error(error);
|
|
89
|
-
}
|
|
90
|
-
}, effect.delayMs);
|
|
73
|
+
process({
|
|
74
|
+
source: "switch-timer",
|
|
75
|
+
generation: effect.generation,
|
|
76
|
+
switchAt: effect.switchAt
|
|
77
|
+
}, node.send.bind(node));
|
|
78
|
+
}), effect.delayMs);
|
|
91
79
|
}
|
|
92
80
|
else {
|
|
93
81
|
send({ payload: undefined, ...effect.message });
|
|
@@ -101,21 +89,14 @@ const registerSwitchPolicy = (RED) => {
|
|
|
101
89
|
const saved = context.get("state");
|
|
102
90
|
updateStatus((0, wall_input_1.wallInputStatus)(saved?.wallInput, policyConfig.switchFreshnessMs));
|
|
103
91
|
}, Math.max(1000, Math.min(60_000, policyConfig.switchFreshnessMs)));
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
}, send);
|
|
113
|
-
done();
|
|
114
|
-
}
|
|
115
|
-
catch (error) {
|
|
116
|
-
node.error(error, msg);
|
|
117
|
-
done(error instanceof Error ? error : new Error(String(error)));
|
|
118
|
-
}
|
|
92
|
+
(0, adapter_1.onInput)(node, (msg, send) => {
|
|
93
|
+
process({
|
|
94
|
+
source: String(msg.source ?? ""),
|
|
95
|
+
payload: msg.payload,
|
|
96
|
+
retain: msg.retain === true,
|
|
97
|
+
baseline: msg.baseline === true,
|
|
98
|
+
observedAt: msg.observedAt
|
|
99
|
+
}, send);
|
|
119
100
|
});
|
|
120
101
|
node.on("close", (_removed, done) => {
|
|
121
102
|
clearTimeout(timer);
|
|
@@ -4,10 +4,12 @@
|
|
|
4
4
|
color: "#d9c7f1",
|
|
5
5
|
defaults: {
|
|
6
6
|
name: {value: ""},
|
|
7
|
-
area: {value: "
|
|
7
|
+
area: {value: ""},
|
|
8
8
|
freshnessMs: {value: 30000, required: true, validate: RED.validators.number()},
|
|
9
9
|
recoveryOffActivity: {value: "off", required: true},
|
|
10
|
-
|
|
10
|
+
// User data with no sensible default: empty until the user fills it in,
|
|
11
|
+
// and marked invalid until then, as the runtime rejects it.
|
|
12
|
+
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; } }}
|
|
11
13
|
},
|
|
12
14
|
inputs: 1,
|
|
13
15
|
outputs: 2,
|
|
@@ -27,6 +29,6 @@
|
|
|
27
29
|
|
|
28
30
|
<script type="text/html" data-help-name="home-theatre-activity-controller">
|
|
29
31
|
<p>Owns latest-request-wins whole-theatre activity state and aggregates generation-scoped target evidence.</p>
|
|
30
|
-
<p>Input <code>msg.theatre</code> accepts activity intent, target-state, startup, refresh, recovery and tick events. 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>
|
|
32
|
+
<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>
|
|
31
33
|
<p>Startup without persisted intent probes physical state. One fresh profile match is adopted without commands; zero or multiple matches invoke the configured coordinated Off activity. Manual overrides are held without corrective commands and adopted only when the physical state uniquely matches a profile.</p>
|
|
32
34
|
</script>
|
|
@@ -1,31 +1,26 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
const bounded_trace_1 = require("../lib/bounded-trace");
|
|
3
2
|
const theatre_activity_controller_1 = require("../lib/theatre-activity-controller");
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
return Number.isFinite(parsed) ? parsed : fallback;
|
|
7
|
-
}
|
|
3
|
+
const adapter_1 = require("./adapter");
|
|
4
|
+
const settings_1 = require("../lib/settings");
|
|
8
5
|
const registerTheatreActivityController = (RED) => {
|
|
9
6
|
function HomeTheatreActivityController(config) {
|
|
10
7
|
RED.nodes.createNode(this, config);
|
|
11
8
|
const node = this;
|
|
12
9
|
const context = node.context();
|
|
13
10
|
let timer;
|
|
14
|
-
let
|
|
11
|
+
let controllerConfig;
|
|
15
12
|
try {
|
|
16
|
-
|
|
13
|
+
controllerConfig = {
|
|
14
|
+
area: (0, adapter_1.optionalArea)(config),
|
|
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")
|
|
18
|
+
};
|
|
17
19
|
}
|
|
18
20
|
catch (error) {
|
|
19
|
-
|
|
20
|
-
node.error(error);
|
|
21
|
+
(0, adapter_1.invalidConfig)(node, error);
|
|
21
22
|
return;
|
|
22
23
|
}
|
|
23
|
-
const controllerConfig = {
|
|
24
|
-
area: config.area || "lounge",
|
|
25
|
-
freshnessMs: Math.max(1000, number(config.freshnessMs, 30000)),
|
|
26
|
-
activityProfiles,
|
|
27
|
-
recoveryOffActivity: config.recoveryOffActivity || "off"
|
|
28
|
-
};
|
|
29
24
|
function updateStatus(status) {
|
|
30
25
|
const appearances = {
|
|
31
26
|
idle: { fill: "grey", shape: "dot" },
|
|
@@ -41,14 +36,7 @@ const registerTheatreActivityController = (RED) => {
|
|
|
41
36
|
node.status({ ...appearance, text: status.text });
|
|
42
37
|
}
|
|
43
38
|
function process(msg, send) {
|
|
44
|
-
const
|
|
45
|
-
const saved = context.get("state");
|
|
46
|
-
const result = (0, theatre_activity_controller_1.transitionTheatre)(saved, msg, controllerConfig, now);
|
|
47
|
-
context.set("state", result.state);
|
|
48
|
-
(0, bounded_trace_1.appendTrace)(context, {
|
|
49
|
-
area: controllerConfig.area,
|
|
50
|
-
component: "theatre-activity-controller"
|
|
51
|
-
}, result.traces, now);
|
|
39
|
+
const result = (0, adapter_1.runReducer)(node, { area: controllerConfig.area, component: "theatre-activity-controller" }, (saved, now) => (0, theatre_activity_controller_1.transitionTheatre)(saved, msg, controllerConfig, now));
|
|
52
40
|
if (result.commands.length) {
|
|
53
41
|
send([
|
|
54
42
|
result.commands.map((theatreCommand) => ({ payload: undefined, theatreCommand })),
|
|
@@ -61,32 +49,24 @@ const registerTheatreActivityController = (RED) => {
|
|
|
61
49
|
updateStatus(result.status);
|
|
62
50
|
return result;
|
|
63
51
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
}, send);
|
|
71
|
-
done();
|
|
72
|
-
}
|
|
73
|
-
catch (error) {
|
|
74
|
-
node.error(error, msg);
|
|
75
|
-
done(error instanceof Error ? error : new Error(String(error)));
|
|
52
|
+
(0, adapter_1.onInput)(node, (msg, send) => {
|
|
53
|
+
// Target controllers' output 2 also carries their own events (such as
|
|
54
|
+
// msg.avr and msg.display) beside target state; only msg.theatre is
|
|
55
|
+
// for this controller.
|
|
56
|
+
if (!msg.theatre || typeof msg.theatre !== "object") {
|
|
57
|
+
return;
|
|
76
58
|
}
|
|
59
|
+
process({
|
|
60
|
+
theatre: (0, adapter_1.objectField)(msg, "theatre")
|
|
61
|
+
}, send);
|
|
77
62
|
});
|
|
78
|
-
timer = setInterval(() => {
|
|
63
|
+
timer = setInterval((0, adapter_1.guarded)(node, () => {
|
|
79
64
|
const saved = context.get("state");
|
|
80
65
|
// Recovering has no desired activity yet but still needs ticks to time out.
|
|
81
66
|
if (!saved?.desired && saved?.phase !== "recovering")
|
|
82
67
|
return;
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
}
|
|
86
|
-
catch (error) {
|
|
87
|
-
node.error(error);
|
|
88
|
-
}
|
|
89
|
-
}, Math.max(1000, Math.floor(controllerConfig.freshnessMs / 2)));
|
|
68
|
+
process({ theatre: { kind: "tick", generation: saved.generation } }, node.send.bind(node));
|
|
69
|
+
}), Math.max(1000, Math.floor(controllerConfig.freshnessMs / 2)));
|
|
90
70
|
node.on("close", (_removed, done) => {
|
|
91
71
|
clearInterval(timer);
|
|
92
72
|
done();
|
|
@@ -1,25 +1,51 @@
|
|
|
1
1
|
<script type="text/javascript">
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
2
|
+
(function () {
|
|
3
|
+
// A setting is on unless it was explicitly switched off; null ("not set")
|
|
4
|
+
// shows as ticked.
|
|
5
|
+
function on(value) { return value !== false && value !== "false"; }
|
|
6
|
+
RED.nodes.registerType("home-theatre-state-contract", {
|
|
7
|
+
category: "home automation",
|
|
8
|
+
color: "#d9c7f1",
|
|
9
|
+
defaults: {
|
|
10
|
+
name: {value: ""},
|
|
11
|
+
// Required for discovery, and for the default base topic home/<area>/theatre.
|
|
12
|
+
area: {value: "", validate: function (v) {
|
|
13
|
+
var dialog = $("#node-input-discovery").length > 0;
|
|
14
|
+
var discovery = dialog ? $("#node-input-discovery").prop("checked") : on(this.discovery);
|
|
15
|
+
var baseTopic = dialog ? $("#node-input-baseTopic").val() : this.baseTopic;
|
|
16
|
+
return !(discovery || !baseTopic) || Boolean(v && v.trim());
|
|
17
|
+
}},
|
|
18
|
+
// null means "not set": Node-RED fills it into a saved flow from before
|
|
19
|
+
// this setting, which keeps publishing discovery. New nodes start with
|
|
20
|
+
// it off (onadd).
|
|
21
|
+
discovery: {value: null},
|
|
22
|
+
displayName: {value: ""},
|
|
23
|
+
baseTopic: {value: ""},
|
|
24
|
+
discoveryPrefix: {value: "homeassistant", required: true},
|
|
25
|
+
// User data with no sensible default: empty until the user fills it in,
|
|
26
|
+
// and marked invalid until then, as the runtime rejects it.
|
|
27
|
+
activities: {value: "[]", required: true, validate: function (v) { try { var parsed = JSON.parse(v); return Array.isArray(parsed) && parsed.length > 0; } catch (e) { return false; } }},
|
|
28
|
+
sloWindowSize: {value: 20, required: true, validate: RED.validators.number()},
|
|
29
|
+
sloMinimumSamples: {value: 5, required: true, validate: RED.validators.number()},
|
|
30
|
+
sloLatencyBudgetMs: {value: 60000, required: true, validate: RED.validators.number()},
|
|
31
|
+
sloSuccessObjectivePct: {value: 95, required: true, validate: RED.validators.number()}
|
|
32
|
+
},
|
|
33
|
+
onadd: function () {
|
|
34
|
+
this.discovery = false;
|
|
35
|
+
},
|
|
36
|
+
oneditprepare: function () {
|
|
37
|
+
$("#node-input-discovery").prop("checked", on(this.discovery));
|
|
38
|
+
$("#node-input-discovery, #node-input-baseTopic").on("change keyup", function () {
|
|
39
|
+
$("#node-input-area").trigger("change");
|
|
40
|
+
});
|
|
41
|
+
},
|
|
42
|
+
inputs: 1,
|
|
43
|
+
outputs: 2,
|
|
44
|
+
outputLabels: ["activity requests", "retained MQTT state and HA discovery"],
|
|
45
|
+
icon: "font-awesome/fa-exchange",
|
|
46
|
+
label: function() { return this.name || "theatre state contract"; }
|
|
47
|
+
});
|
|
48
|
+
})();
|
|
23
49
|
</script>
|
|
24
50
|
|
|
25
51
|
<script type="text/html" data-template-name="home-theatre-state-contract">
|
|
@@ -27,6 +53,7 @@
|
|
|
27
53
|
<div class="form-row"><label for="node-input-area">Area</label><input type="text" id="node-input-area"></div>
|
|
28
54
|
<div class="form-row"><label for="node-input-displayName">Display name</label><input type="text" id="node-input-displayName"></div>
|
|
29
55
|
<div class="form-row"><label for="node-input-baseTopic">Base topic</label><input type="text" id="node-input-baseTopic"></div>
|
|
56
|
+
<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>
|
|
30
57
|
<div class="form-row"><label for="node-input-discoveryPrefix">Discovery prefix</label><input type="text" id="node-input-discoveryPrefix"></div>
|
|
31
58
|
<div class="form-row"><label for="node-input-activities">Activities JSON</label><input type="text" id="node-input-activities"></div>
|
|
32
59
|
<div class="form-row"><label for="node-input-sloWindowSize">SLO window</label><input type="number" id="node-input-sloWindowSize"></div>
|
|
@@ -37,5 +64,11 @@
|
|
|
37
64
|
|
|
38
65
|
<script type="text/html" data-help-name="home-theatre-state-contract">
|
|
39
66
|
<p>Bridges non-retained MQTT activity requests to the theatre controller and publishes retained controller truth.</p>
|
|
40
|
-
<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, failure, availability, operational SLO and per-target health topics.
|
|
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, failure, availability, operational SLO and per-target health topics. </p>
|
|
68
|
+
<p>With <b>Home Assistant discovery</b> on, a startup event also publishes discovery for the activity selector and
|
|
69
|
+
status sensors; switched off, it clears them, so Home Assistant removes them. New nodes start with it off; a node
|
|
70
|
+
saved before the setting existed keeps it on.</p>
|
|
71
|
+
<p>The <b>Area</b> names the discovery entities and the default base topic <code>home/<area>/theatre</code>,
|
|
72
|
+
so it is needed when discovery is on or the base topic is empty. With discovery off and a base topic set, it is
|
|
73
|
+
optional.</p>
|
|
41
74
|
</script>
|