@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,37 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.parsePayload =
|
|
4
|
-
exports.fingerprint = fingerprint;
|
|
3
|
+
exports.parsePayload = exports.fingerprint = void 0;
|
|
5
4
|
exports.createRoomState = createRoomState;
|
|
6
|
-
exports.sunPosition = sunPosition;
|
|
7
5
|
exports.adaptivePayload = adaptivePayload;
|
|
8
6
|
exports.transitionRoom = transitionRoom;
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
function
|
|
14
|
-
|
|
15
|
-
return payload;
|
|
16
|
-
try {
|
|
17
|
-
return JSON.parse(payload);
|
|
18
|
-
}
|
|
19
|
-
catch {
|
|
20
|
-
return payload;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
function stable(value) {
|
|
24
|
-
if (Array.isArray(value))
|
|
25
|
-
return value.map(stable);
|
|
26
|
-
if (value && typeof value === "object") {
|
|
27
|
-
const record = value;
|
|
28
|
-
return Object.fromEntries(Object.keys(record).sort().map((key) => [key, stable(record[key])]));
|
|
29
|
-
}
|
|
30
|
-
return value;
|
|
31
|
-
}
|
|
32
|
-
function fingerprint(payload) {
|
|
33
|
-
return JSON.stringify(stable(payload));
|
|
34
|
-
}
|
|
7
|
+
const z2m_light_1 = require("./z2m-light");
|
|
8
|
+
const room_lighting_discovery_1 = require("./room-lighting-discovery");
|
|
9
|
+
const command_echo_1 = require("./command-echo");
|
|
10
|
+
var command_echo_2 = require("./command-echo");
|
|
11
|
+
Object.defineProperty(exports, "fingerprint", { enumerable: true, get: function () { return command_echo_2.fingerprint; } });
|
|
12
|
+
Object.defineProperty(exports, "parsePayload", { enumerable: true, get: function () { return command_echo_2.parsePayload; } });
|
|
35
13
|
function savedControl(saved) {
|
|
36
14
|
if (saved.control) {
|
|
37
15
|
return Object.fromEntries(Object.entries(saved.control).map(([target, entry]) => [target, { ...entry }]));
|
|
@@ -48,69 +26,16 @@ function createRoomState(config, saved = {}) {
|
|
|
48
26
|
control: savedControl(saved),
|
|
49
27
|
expected: Object.fromEntries(Object.entries(saved.expected || {}).map(([target, entries]) => [target, entries.map((entry) => ({ ...entry }))])),
|
|
50
28
|
circadianEnabled: saved.circadianEnabled !== false,
|
|
51
|
-
sleeping: saved.sleeping === undefined ? config.initialSleep : saved.sleeping === true
|
|
29
|
+
sleeping: saved.sleeping === undefined ? config.initialSleep : saved.sleeping === true,
|
|
30
|
+
personAt: Object.fromEntries(Object.entries(saved.personAt || {})
|
|
31
|
+
.filter(([, at]) => Number.isFinite(at)))
|
|
52
32
|
};
|
|
53
33
|
}
|
|
54
|
-
//
|
|
55
|
-
// in adaptivePayload are adapted (ported to TypeScript and modified) from
|
|
56
|
-
// Adaptive Lighting by basnijholt, https://github.com/basnijholt/adaptive-lighting
|
|
57
|
-
// (custom_components/adaptive_lighting/color_and_brightness.py), licensed under
|
|
58
|
-
// Apache-2.0; see THIRD-PARTY-NOTICES.md. They are due to be replaced by an
|
|
59
|
-
// original implementation.
|
|
60
|
-
function sunPosition(now, config) {
|
|
61
|
-
const events = [];
|
|
62
|
-
for (const dayOffset of [-1, 0, 1]) {
|
|
63
|
-
const date = new Date(now);
|
|
64
|
-
date.setHours(12, 0, 0, 0);
|
|
65
|
-
date.setDate(date.getDate() + dayOffset);
|
|
66
|
-
const times = SunCalc.getTimes(date, config.latitude, config.longitude);
|
|
67
|
-
for (const [name, at] of [
|
|
68
|
-
["sunrise", times.sunrise],
|
|
69
|
-
["noon", times.solarNoon],
|
|
70
|
-
["sunset", times.sunset],
|
|
71
|
-
["midnight", times.nadir]
|
|
72
|
-
]) {
|
|
73
|
-
if (at instanceof Date && Number.isFinite(at.getTime()))
|
|
74
|
-
events.push({ name, at: at.getTime() });
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
events.sort((left, right) => left.at - right.at);
|
|
78
|
-
const target = now.getTime();
|
|
79
|
-
const nextIndex = events.findIndex((event) => event.at > target);
|
|
80
|
-
if (nextIndex < 1)
|
|
81
|
-
throw new Error("cannot bracket the current solar event");
|
|
82
|
-
const previous = events[nextIndex - 1];
|
|
83
|
-
const next = events[nextIndex];
|
|
84
|
-
if (!previous || !next)
|
|
85
|
-
throw new Error("cannot bracket the current solar event");
|
|
86
|
-
const nextIsHorizon = next.name === "sunrise" || next.name === "sunset";
|
|
87
|
-
const h = nextIsHorizon ? previous.at : next.at;
|
|
88
|
-
const x = nextIsHorizon ? next.at : previous.at;
|
|
89
|
-
const sign = next.name === "sunset" || next.name === "noon" ? 1 : -1;
|
|
90
|
-
return clamp(sign * (1 - Math.pow((target - h) / (h - x), 2)), -1, 1);
|
|
91
|
-
}
|
|
92
|
-
// Brightness and colour-temperature rules adapted from Adaptive Lighting
|
|
93
|
-
// (Apache-2.0); see the note above sunPosition.
|
|
34
|
+
// A circadian light command for this moment.
|
|
94
35
|
function adaptivePayload(lightState, isRefresh, sleeping, now, config) {
|
|
95
36
|
if (lightState === "OFF")
|
|
96
37
|
return { state: lightState };
|
|
97
|
-
|
|
98
|
-
const brightnessPct = sleeping
|
|
99
|
-
? config.sleepBrightnessPct
|
|
100
|
-
: position > 0
|
|
101
|
-
? config.maxBrightnessPct
|
|
102
|
-
: (config.maxBrightnessPct - config.minBrightnessPct) * (1 + position) + config.minBrightnessPct;
|
|
103
|
-
const kelvin = sleeping
|
|
104
|
-
? config.sleepColorTempKelvin
|
|
105
|
-
: position > 0
|
|
106
|
-
? 5 * Math.round((config.minColorTempKelvin + (config.maxColorTempKelvin - config.minColorTempKelvin) * position) / 5)
|
|
107
|
-
: config.minColorTempKelvin;
|
|
108
|
-
return {
|
|
109
|
-
state: lightState,
|
|
110
|
-
brightness: clamp(Math.round(brightnessPct * 254 / 100), 1, 254),
|
|
111
|
-
color_temp: Math.floor(1000000 / kelvin),
|
|
112
|
-
transition: isRefresh ? config.refreshTransitionSeconds : config.initialTransitionSeconds
|
|
113
|
-
};
|
|
38
|
+
return (0, z2m_light_1.lightCommand)(lightState, config.lighting.styleAt(now, sleeping), isRefresh ? config.refreshTransitionSeconds : config.initialTransitionSeconds);
|
|
114
39
|
}
|
|
115
40
|
function transitionRoom(saved, msg, config, nowValue = new Date()) {
|
|
116
41
|
const now = nowValue instanceof Date ? nowValue : new Date(nowValue);
|
|
@@ -158,34 +83,11 @@ function transitionRoom(saved, msg, config, nowValue = new Date()) {
|
|
|
158
83
|
return state.desired[targetId] === true;
|
|
159
84
|
return state.observed[targetId]?.on === true;
|
|
160
85
|
}
|
|
161
|
-
function rememberExpected(targetId, payload) {
|
|
162
|
-
const queue = (state.expected[targetId] || []).filter((entry) => entry.expires > nowMs);
|
|
163
|
-
const entry = { fingerprint: fingerprint(payload), expires: nowMs + 5000 };
|
|
164
|
-
const requested = payload && typeof payload === "object"
|
|
165
|
-
? String(payload.state || "").toUpperCase()
|
|
166
|
-
: "";
|
|
167
|
-
if (requested === "ON" || requested === "OFF")
|
|
168
|
-
entry.on = requested === "ON";
|
|
169
|
-
queue.push(entry);
|
|
170
|
-
state.expected[targetId] = queue.slice(-8);
|
|
171
|
-
}
|
|
172
|
-
// Whether a state report is stale against the controller's own command in
|
|
173
|
-
// flight. A report that matches the newest unconfirmed command confirms it.
|
|
174
|
-
function contradictsOwnCommand(targetId, observedOn) {
|
|
175
|
-
const queue = (state.expected[targetId] || []).filter((entry) => entry.expires > nowMs);
|
|
176
|
-
const latest = queue.filter((entry) => entry.on !== undefined).at(-1);
|
|
177
|
-
if (latest && latest.on === observedOn) {
|
|
178
|
-
for (const entry of queue)
|
|
179
|
-
delete entry.on;
|
|
180
|
-
}
|
|
181
|
-
state.expected[targetId] = queue.filter((entry) => entry.on !== undefined || entry.echoed !== true);
|
|
182
|
-
return latest !== undefined && latest.on !== observedOn;
|
|
183
|
-
}
|
|
184
86
|
function command(targetId, payload, source) {
|
|
185
87
|
const target = targetById[targetId];
|
|
186
88
|
if (!target)
|
|
187
89
|
return null;
|
|
188
|
-
|
|
90
|
+
state.expected[targetId] = (0, command_echo_1.rememberCommand)(state.expected[targetId], payload, nowMs);
|
|
189
91
|
return {
|
|
190
92
|
topic: target.commandTopic,
|
|
191
93
|
payload,
|
|
@@ -193,37 +95,11 @@ function transitionRoom(saved, msg, config, nowValue = new Date()) {
|
|
|
193
95
|
lighting_command: { area: config.area, target: targetId, source }
|
|
194
96
|
};
|
|
195
97
|
}
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
payload: {
|
|
202
|
-
name: config.displayName + " Circadian Lighting",
|
|
203
|
-
unique_id: objectId,
|
|
204
|
-
default_entity_id: "switch." + objectId,
|
|
205
|
-
icon: "mdi:theme-light-dark",
|
|
206
|
-
command_topic: "home/" + config.area + "/circadian/set",
|
|
207
|
-
state_topic: "home/" + config.area + "/circadian/state",
|
|
208
|
-
payload_on: "ON",
|
|
209
|
-
payload_off: "OFF",
|
|
210
|
-
optimistic: false,
|
|
211
|
-
device: {
|
|
212
|
-
identifiers: ["nodered_" + config.area + "_lighting"],
|
|
213
|
-
name: config.displayName + " Lighting Controller",
|
|
214
|
-
manufacturer: "Node-RED",
|
|
215
|
-
model: "Composable circadian lighting"
|
|
216
|
-
}
|
|
217
|
-
},
|
|
218
|
-
retain: true
|
|
219
|
-
},
|
|
220
|
-
{
|
|
221
|
-
topic: "home/" + config.area + "/circadian/state",
|
|
222
|
-
payload: state.circadianEnabled ? "ON" : "OFF",
|
|
223
|
-
retain: true
|
|
224
|
-
}
|
|
225
|
-
];
|
|
226
|
-
}
|
|
98
|
+
// What the room publishes for MQTT and Home Assistant to see is named after
|
|
99
|
+
// its area, so it needs one, and is switched on by its settings; light
|
|
100
|
+
// control depends on neither.
|
|
101
|
+
const publishesArea = config.area !== "" && config.publishState !== false;
|
|
102
|
+
const publishesDiscovery = publishesArea && config.discovery !== false;
|
|
227
103
|
function refreshCommands(source) {
|
|
228
104
|
if (!state.circadianEnabled)
|
|
229
105
|
return [];
|
|
@@ -231,6 +107,11 @@ function transitionRoom(saved, msg, config, nowValue = new Date()) {
|
|
|
231
107
|
for (const target of config.targets) {
|
|
232
108
|
if (!intendedOn(target.id) || controlOf(target.id) !== "in-control")
|
|
233
109
|
continue;
|
|
110
|
+
// Nothing to change when the light last reported exactly this style.
|
|
111
|
+
if ((0, z2m_light_1.alreadyStyled)(config.lighting.styleAt(now, state.sleeping), state.observed[target.id])) {
|
|
112
|
+
trace("refresh", "skipped", "already-at-level", { target: target.id });
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
234
115
|
commands.push(command(target.id, adaptivePayload("ON", true, state.sleeping, now, config), source));
|
|
235
116
|
}
|
|
236
117
|
return commands.filter((entry) => entry !== null);
|
|
@@ -259,6 +140,16 @@ function transitionRoom(saved, msg, config, nowValue = new Date()) {
|
|
|
259
140
|
}
|
|
260
141
|
const wasOn = intendedOn(targetId);
|
|
261
142
|
const resolvedState = requestedState === "TOGGLE" ? (wasOn ? "OFF" : "ON") : requestedState;
|
|
143
|
+
// Motion switches off a vacant room, but not a light a person set after
|
|
144
|
+
// the room went vacant: they are using it.
|
|
145
|
+
const vacantSince = typeof event.vacantSince === "string" ? Date.parse(event.vacantSince) : NaN;
|
|
146
|
+
if (event.source === "motion" && resolvedState === "OFF" && Number.isFinite(vacantSince) &&
|
|
147
|
+
(state.personAt[targetId] ?? -Infinity) >= vacantSince) {
|
|
148
|
+
trace("intent", "ignored", "person-set-after-vacancy", { target: targetId, vacantSince: event.vacantSince });
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
if (person)
|
|
152
|
+
state.personAt[targetId] = now.getTime();
|
|
262
153
|
if (event.updateOnOnly === true && !wasOn)
|
|
263
154
|
continue;
|
|
264
155
|
if (event.updateOffOnly === true && wasOn)
|
|
@@ -296,33 +187,63 @@ function transitionRoom(saved, msg, config, nowValue = new Date()) {
|
|
|
296
187
|
}
|
|
297
188
|
return commands.filter((entry) => entry !== null);
|
|
298
189
|
}
|
|
299
|
-
|
|
190
|
+
let event = msg.lighting || {};
|
|
191
|
+
// The circadian switch's set topic: Home Assistant sends ON or OFF there.
|
|
192
|
+
// Read only while the room publishes, as the switch is only discovered then;
|
|
193
|
+
// a bare MQTT message, or one a flow tagged as observed, is accepted.
|
|
194
|
+
if (publishesArea && msg.topic === (0, room_lighting_discovery_1.circadianCommandTopic)(config.area) &&
|
|
195
|
+
(!event.kind || event.kind === "observed" || event.kind === "command-observed")) {
|
|
196
|
+
const requested = typeof msg.payload === "string" ? msg.payload.trim().toUpperCase() : "";
|
|
197
|
+
if (msg.retain === true || event.retained === true) {
|
|
198
|
+
trace("circadian-set", "ignored", "retained-message");
|
|
199
|
+
event = { kind: "ignored" };
|
|
200
|
+
}
|
|
201
|
+
else if (requested === "ON" || requested === "OFF") {
|
|
202
|
+
event = { kind: "circadian", enabled: requested === "ON" };
|
|
203
|
+
}
|
|
204
|
+
else {
|
|
205
|
+
trace("circadian-set", "ignored", "not-on-or-off", { payload: typeof msg.payload === "string" ? msg.payload : null });
|
|
206
|
+
event = { kind: "ignored" };
|
|
207
|
+
}
|
|
208
|
+
}
|
|
300
209
|
if (event.kind === "startup") {
|
|
301
|
-
|
|
210
|
+
if (config.area !== "") {
|
|
211
|
+
const entry = (0, room_lighting_discovery_1.roomLightingDiscovery)(config.area, config.displayName, state.circadianEnabled)[0];
|
|
212
|
+
// Discovery switched off: an empty retained entry removes the switch
|
|
213
|
+
// from Home Assistant.
|
|
214
|
+
outputs.push(publishesDiscovery ? entry : { topic: entry.topic, payload: "", retain: true });
|
|
215
|
+
}
|
|
216
|
+
if (publishesArea)
|
|
217
|
+
outputs.push((0, room_lighting_discovery_1.circadianStateMessage)(config.area, state.circadianEnabled));
|
|
302
218
|
for (const target of config.targets)
|
|
303
219
|
controlChanged.add(target.id);
|
|
304
220
|
}
|
|
305
221
|
else if (event.kind === "observed") {
|
|
306
222
|
const target = event.target ? targetById[event.target] : targetForTopic(msg.topic, "stateTopic");
|
|
307
|
-
const parsed = parsePayload(msg.payload);
|
|
223
|
+
const parsed = (0, command_echo_1.parsePayload)(msg.payload);
|
|
308
224
|
const payload = parsed && typeof parsed === "object" ? parsed : null;
|
|
309
|
-
|
|
225
|
+
const light = payload ? (0, z2m_light_1.readLightReport)(payload) : null;
|
|
226
|
+
if (target && light) {
|
|
310
227
|
const previousOn = state.observed[target.id]?.on;
|
|
311
|
-
const nextOn =
|
|
312
|
-
state.observed[target.id] = {
|
|
313
|
-
on: nextOn,
|
|
314
|
-
brightness: payload.brightness ?? null,
|
|
315
|
-
color_temp: payload.color_temp ?? null,
|
|
316
|
-
observedAt: now.toISOString()
|
|
317
|
-
};
|
|
228
|
+
const nextOn = light.on;
|
|
229
|
+
state.observed[target.id] = { ...light, observedAt: now.toISOString() };
|
|
318
230
|
if (previousOn !== nextOn) {
|
|
319
231
|
trace("state-observed", "changed", null, {
|
|
320
232
|
target: target.id,
|
|
321
233
|
state: nextOn ? "ON" : "OFF"
|
|
322
234
|
});
|
|
323
235
|
}
|
|
324
|
-
|
|
325
|
-
|
|
236
|
+
const report = (0, command_echo_1.checkStateReport)(state.expected[target.id], nextOn, nowMs);
|
|
237
|
+
state.expected[target.id] = report.queue;
|
|
238
|
+
if (report.verdict === "confirms") {
|
|
239
|
+
// Our own command, now done: the intended state already says so.
|
|
240
|
+
// A confirmed OFF deliberately leaves control alone: our OFFs come
|
|
241
|
+
// from OFF intents, which already returned the light to in-control.
|
|
242
|
+
trace("state-observed", "confirmed", "own-command", { target: target.id, state: nextOn ? "ON" : "OFF" });
|
|
243
|
+
}
|
|
244
|
+
else if (report.verdict === "stale") {
|
|
245
|
+
// Older than our pending command; the light will report again.
|
|
246
|
+
trace("state-observed", "ignored", "stale-report", { target: target.id, state: nextOn ? "ON" : "OFF" });
|
|
326
247
|
}
|
|
327
248
|
else {
|
|
328
249
|
// A change the controller didn't cause becomes the intended state.
|
|
@@ -334,34 +255,20 @@ function transitionRoom(saved, msg, config, nowValue = new Date()) {
|
|
|
334
255
|
}
|
|
335
256
|
else if (event.kind === "command-observed") {
|
|
336
257
|
const target = event.target ? targetById[event.target] : targetForTopic(msg.topic, "commandTopic");
|
|
337
|
-
const parsed = parsePayload(msg.payload);
|
|
258
|
+
const parsed = (0, command_echo_1.parsePayload)(msg.payload);
|
|
338
259
|
const payload = parsed && typeof parsed === "object" ? parsed : null;
|
|
339
260
|
if (msg.retain !== true && event.retained !== true && target && payload) {
|
|
340
|
-
const
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
if (own) {
|
|
344
|
-
// Keep an unconfirmed power command as in flight for state reports.
|
|
345
|
-
if (own.on === undefined)
|
|
346
|
-
queue.splice(ownIndex, 1);
|
|
347
|
-
else
|
|
348
|
-
own.echoed = true;
|
|
349
|
-
state.expected[target.id] = queue;
|
|
350
|
-
}
|
|
351
|
-
else {
|
|
352
|
-
state.expected[target.id] = queue;
|
|
261
|
+
const echo = (0, command_echo_1.checkCommandEcho)(state.expected[target.id], payload, nowMs);
|
|
262
|
+
state.expected[target.id] = echo.queue;
|
|
263
|
+
if (!echo.own) {
|
|
353
264
|
const requestedState = String(payload.state || "").toUpperCase();
|
|
354
|
-
const fields = [
|
|
355
|
-
"brightness", "brightness_percent", "color", "color_temp", "color_temp_kelvin",
|
|
356
|
-
"effect", "hue", "saturation", "x", "y"
|
|
357
|
-
];
|
|
358
265
|
if (requestedState === "OFF") {
|
|
359
266
|
state.desired[target.id] = false;
|
|
360
267
|
setControl(target.id, null, "external-off");
|
|
361
268
|
}
|
|
362
269
|
else if (requestedState === "ON") {
|
|
363
270
|
state.desired[target.id] = true;
|
|
364
|
-
if (
|
|
271
|
+
if ((0, z2m_light_1.setsLightValues)(payload)) {
|
|
365
272
|
setControl(target.id, { state: "manual" }, "external-values");
|
|
366
273
|
}
|
|
367
274
|
else if (state.circadianEnabled && controlOf(target.id) === "in-control") {
|
|
@@ -373,6 +280,7 @@ function transitionRoom(saved, msg, config, nowValue = new Date()) {
|
|
|
373
280
|
else if (requestedState === "TOGGLE") {
|
|
374
281
|
state.desired[target.id] = !intendedOn(target.id);
|
|
375
282
|
}
|
|
283
|
+
state.personAt[target.id] = now.getTime();
|
|
376
284
|
trace("external-command", "observed", null, {
|
|
377
285
|
target: target.id,
|
|
378
286
|
state: requestedState || "UNKNOWN",
|
|
@@ -387,11 +295,8 @@ function transitionRoom(saved, msg, config, nowValue = new Date()) {
|
|
|
387
295
|
if (previous !== state.circadianEnabled) {
|
|
388
296
|
trace("circadian", "changed", null, { enabled: state.circadianEnabled });
|
|
389
297
|
}
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
payload: state.circadianEnabled ? "ON" : "OFF",
|
|
393
|
-
retain: true
|
|
394
|
-
});
|
|
298
|
+
if (publishesArea)
|
|
299
|
+
outputs.push((0, room_lighting_discovery_1.circadianStateMessage)(config.area, state.circadianEnabled));
|
|
395
300
|
if (state.circadianEnabled) {
|
|
396
301
|
// Enabling circadian takes back manual lights; other owners keep theirs.
|
|
397
302
|
for (const target of config.targets) {
|
|
@@ -450,7 +355,7 @@ function transitionRoom(saved, msg, config, nowValue = new Date()) {
|
|
|
450
355
|
});
|
|
451
356
|
}
|
|
452
357
|
for (const target of config.targets) {
|
|
453
|
-
if (controlChanged.has(target.id))
|
|
358
|
+
if (publishesArea && controlChanged.has(target.id))
|
|
454
359
|
outputs.push(controlMessage(target.id));
|
|
455
360
|
}
|
|
456
361
|
const onCount = Object.values(state.desired).filter(Boolean).length;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export interface DiscoveryMessage {
|
|
2
|
+
topic: string;
|
|
3
|
+
payload: unknown;
|
|
4
|
+
retain: boolean;
|
|
5
|
+
}
|
|
6
|
+
export declare function circadianStateTopic(area: string): string;
|
|
7
|
+
export declare function circadianCommandTopic(area: string): string;
|
|
8
|
+
export declare function circadianStateMessage(area: string, enabled: boolean): DiscoveryMessage;
|
|
9
|
+
export declare function roomLightingDiscovery(area: string, displayName: string, circadianEnabled: boolean): DiscoveryMessage[];
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.circadianStateTopic = circadianStateTopic;
|
|
4
|
+
exports.circadianCommandTopic = circadianCommandTopic;
|
|
5
|
+
exports.circadianStateMessage = circadianStateMessage;
|
|
6
|
+
exports.roomLightingDiscovery = roomLightingDiscovery;
|
|
7
|
+
function circadianStateTopic(area) {
|
|
8
|
+
return "home/" + area + "/circadian/state";
|
|
9
|
+
}
|
|
10
|
+
function circadianCommandTopic(area) {
|
|
11
|
+
return "home/" + area + "/circadian/set";
|
|
12
|
+
}
|
|
13
|
+
// The retained circadian state, as Home Assistant reads it.
|
|
14
|
+
function circadianStateMessage(area, enabled) {
|
|
15
|
+
return { topic: circadianStateTopic(area), payload: enabled ? "ON" : "OFF", retain: true };
|
|
16
|
+
}
|
|
17
|
+
// The discovery entry for the room's circadian switch, then its state.
|
|
18
|
+
function roomLightingDiscovery(area, displayName, circadianEnabled) {
|
|
19
|
+
const objectId = area + "_circadian_lighting";
|
|
20
|
+
return [
|
|
21
|
+
{
|
|
22
|
+
topic: "homeassistant/switch/" + objectId + "/config",
|
|
23
|
+
payload: {
|
|
24
|
+
name: displayName + " Circadian Lighting",
|
|
25
|
+
unique_id: objectId,
|
|
26
|
+
default_entity_id: "switch." + objectId,
|
|
27
|
+
icon: "mdi:clock-time-seven-outline",
|
|
28
|
+
command_topic: circadianCommandTopic(area),
|
|
29
|
+
state_topic: circadianStateTopic(area),
|
|
30
|
+
payload_on: "ON",
|
|
31
|
+
payload_off: "OFF",
|
|
32
|
+
optimistic: false,
|
|
33
|
+
device: {
|
|
34
|
+
identifiers: ["nodered_" + area + "_lighting"],
|
|
35
|
+
name: displayName + " Lighting Controller",
|
|
36
|
+
manufacturer: "Node-RED",
|
|
37
|
+
model: "Composable circadian lighting"
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
retain: true
|
|
41
|
+
},
|
|
42
|
+
circadianStateMessage(area, circadianEnabled)
|
|
43
|
+
];
|
|
44
|
+
}
|
|
45
|
+
//# sourceMappingURL=room-lighting-discovery.js.map
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare function numberSetting(value: unknown, label: string, fallback: number): number;
|
|
2
|
+
export declare function booleanSetting(value: unknown, label: string, fallback: boolean): boolean;
|
|
3
|
+
export declare function optionalBooleanSetting(value: unknown, label: string): boolean | null;
|
|
4
|
+
export declare function textSetting(value: unknown, label: string, fallback?: string): string;
|
|
5
|
+
export declare function jsonSetting(value: unknown, label: string, fallback: unknown): unknown;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.numberSetting = numberSetting;
|
|
4
|
+
exports.booleanSetting = booleanSetting;
|
|
5
|
+
exports.optionalBooleanSetting = optionalBooleanSetting;
|
|
6
|
+
exports.textSetting = textSetting;
|
|
7
|
+
exports.jsonSetting = jsonSetting;
|
|
8
|
+
// A numeric node setting, read from the editor's value. A setting absent from
|
|
9
|
+
// a flow saved before it existed takes its default. Anything else must be a
|
|
10
|
+
// number: an emptied field, null or text is an error naming the setting, so
|
|
11
|
+
// the node reports it instead of running with a value nobody chose.
|
|
12
|
+
function numberSetting(value, label, fallback) {
|
|
13
|
+
if (value === undefined)
|
|
14
|
+
return fallback;
|
|
15
|
+
const parsed = typeof value === "number" ? value
|
|
16
|
+
: typeof value === "string" && value.trim() !== "" ? Number(value)
|
|
17
|
+
: NaN;
|
|
18
|
+
if (!Number.isFinite(parsed))
|
|
19
|
+
throw new Error(`${label} must be a number`);
|
|
20
|
+
return parsed;
|
|
21
|
+
}
|
|
22
|
+
// An on/off setting. A setting absent from an older saved flow takes its
|
|
23
|
+
// default; true/"true" and false/"false" are read as such; anything else is
|
|
24
|
+
// an error naming the setting.
|
|
25
|
+
function booleanSetting(value, label, fallback) {
|
|
26
|
+
if (value === undefined)
|
|
27
|
+
return fallback;
|
|
28
|
+
const read = optionalBooleanSetting(value, label);
|
|
29
|
+
return read ?? fallback;
|
|
30
|
+
}
|
|
31
|
+
// An on/off setting that can be "not set": null (what the editor stores for
|
|
32
|
+
// it) or absent. Returns null for "not set"; the caller applies its documented
|
|
33
|
+
// meaning. An empty string is not "not set" and is an error.
|
|
34
|
+
function optionalBooleanSetting(value, label) {
|
|
35
|
+
if (value === undefined || value === null)
|
|
36
|
+
return null;
|
|
37
|
+
if (value === true || value === "true")
|
|
38
|
+
return true;
|
|
39
|
+
if (value === false || value === "false")
|
|
40
|
+
return false;
|
|
41
|
+
throw new Error(`${label} must be on or off`);
|
|
42
|
+
}
|
|
43
|
+
// A text setting. Absent from an older saved flow: its default (or, with no
|
|
44
|
+
// default, an error). Otherwise it must not be empty.
|
|
45
|
+
function textSetting(value, label, fallback) {
|
|
46
|
+
if (value === undefined && fallback !== undefined)
|
|
47
|
+
return fallback;
|
|
48
|
+
const text = typeof value === "string" ? value.trim() : "";
|
|
49
|
+
if (!text)
|
|
50
|
+
throw new Error(`${label} is required`);
|
|
51
|
+
return text;
|
|
52
|
+
}
|
|
53
|
+
// A JSON setting, parsed. Absent from an older saved flow: its default.
|
|
54
|
+
// Otherwise it must be JSON text that parses; the caller checks its shape.
|
|
55
|
+
function jsonSetting(value, label, fallback) {
|
|
56
|
+
if (value === undefined)
|
|
57
|
+
return fallback;
|
|
58
|
+
if (typeof value === "string" && value.trim() !== "") {
|
|
59
|
+
try {
|
|
60
|
+
return JSON.parse(value);
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
// Reported below.
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
throw new Error(`${label} must be valid JSON`);
|
|
67
|
+
}
|
|
68
|
+
//# sourceMappingURL=settings.js.map
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import type { TraceEntry } from "./bounded-trace";
|
|
2
|
+
import { type TargetStateEvent } from "./target-state";
|
|
1
3
|
export type SourceAction = "activate" | "deactivate";
|
|
2
4
|
export type SourceControlMode = "command" | "external" | "unavailable";
|
|
3
5
|
export type SourceConfirmation = "active" | "available" | "application" | "playing" | "unavailable";
|
|
@@ -26,6 +28,7 @@ export interface SourceObservation {
|
|
|
26
28
|
sourceId: string;
|
|
27
29
|
status: SourceObservationStatus;
|
|
28
30
|
observedAt: string;
|
|
31
|
+
confirmedAt?: string;
|
|
29
32
|
application: string | null;
|
|
30
33
|
playback: SourcePlaybackState;
|
|
31
34
|
}
|
|
@@ -47,10 +50,10 @@ export interface SourceState {
|
|
|
47
50
|
observed: SourceObservation | null;
|
|
48
51
|
pending: SourcePending | null;
|
|
49
52
|
failure: SourceFailure | null;
|
|
50
|
-
blockedGeneration: number | null;
|
|
51
53
|
classification: "manual_override" | null;
|
|
52
54
|
lastAttempt: number;
|
|
53
55
|
fallbackUsed: boolean;
|
|
56
|
+
refreshAsked: boolean;
|
|
54
57
|
}
|
|
55
58
|
export type SourceEvent = {
|
|
56
59
|
kind: "intent";
|
|
@@ -82,8 +85,8 @@ export interface SourceMessage {
|
|
|
82
85
|
sourceCapability?: SourceEvent;
|
|
83
86
|
}
|
|
84
87
|
export declare function normaliseCapabilities(value: unknown): SourceCapabilities;
|
|
85
|
-
export declare function normaliseConfig(config?:
|
|
86
|
-
|
|
88
|
+
export declare function normaliseConfig(config?: {
|
|
89
|
+
[K in keyof SourceConfig]?: unknown;
|
|
87
90
|
}): SourceConfig;
|
|
88
91
|
export interface SourceCommand {
|
|
89
92
|
sourceId: string | null;
|
|
@@ -93,48 +96,42 @@ export interface SourceCommand {
|
|
|
93
96
|
attempt: number;
|
|
94
97
|
}
|
|
95
98
|
export interface SourceOutputEvent {
|
|
96
|
-
theatre: {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
99
|
+
theatre: TargetStateEvent<"source", {
|
|
100
|
+
observedStatus: SourceObservationStatus | null;
|
|
101
|
+
readiness: {
|
|
102
|
+
required: SourceConfirmation;
|
|
103
|
+
available: boolean;
|
|
104
|
+
active: boolean;
|
|
105
|
+
application: string | null;
|
|
106
|
+
playback: SourcePlaybackState;
|
|
107
|
+
satisfied: boolean;
|
|
108
|
+
};
|
|
109
|
+
recovery: Record<string, unknown>;
|
|
110
|
+
metrics: {
|
|
111
|
+
attempts: number;
|
|
112
|
+
retries: number;
|
|
113
|
+
fallbackUsed: boolean;
|
|
114
|
+
};
|
|
115
|
+
}, SourceFailure> & {
|
|
103
116
|
sourceId: string | null;
|
|
104
117
|
action: SourceAction;
|
|
105
|
-
classification?: "manual_override";
|
|
106
|
-
details: {
|
|
107
|
-
observedStatus: SourceObservationStatus | null;
|
|
108
|
-
readiness: {
|
|
109
|
-
required: SourceConfirmation;
|
|
110
|
-
available: boolean;
|
|
111
|
-
active: boolean;
|
|
112
|
-
application: string | null;
|
|
113
|
-
playback: SourcePlaybackState;
|
|
114
|
-
satisfied: boolean;
|
|
115
|
-
};
|
|
116
|
-
recovery: Record<string, unknown>;
|
|
117
|
-
metrics: {
|
|
118
|
-
attempts: number;
|
|
119
|
-
retries: number;
|
|
120
|
-
fallbackUsed: boolean;
|
|
121
|
-
};
|
|
122
|
-
};
|
|
123
|
-
failure?: SourceFailure;
|
|
124
118
|
};
|
|
125
119
|
}
|
|
120
|
+
export declare function readinessEvidence(observation: SourceObservation | null, required: SourceConfirmation): {
|
|
121
|
+
available: boolean;
|
|
122
|
+
active: boolean;
|
|
123
|
+
application: string | null;
|
|
124
|
+
playback: SourcePlaybackState;
|
|
125
|
+
required: SourceConfirmation;
|
|
126
|
+
satisfied: boolean;
|
|
127
|
+
};
|
|
126
128
|
export declare function transitionSource(saved: Partial<SourceState> | undefined, msg: SourceMessage, suppliedConfig?: Partial<SourceConfig> & {
|
|
127
129
|
sources?: unknown;
|
|
128
130
|
}, nowValue?: Date | string | number): {
|
|
129
131
|
state: SourceState;
|
|
130
132
|
commands: SourceCommand[];
|
|
131
133
|
events: SourceOutputEvent[];
|
|
132
|
-
traces:
|
|
133
|
-
event: string;
|
|
134
|
-
outcome: string;
|
|
135
|
-
reason: string | null;
|
|
136
|
-
details: Record<string, unknown>;
|
|
137
|
-
}[];
|
|
134
|
+
traces: TraceEntry<Record<string, unknown>>[];
|
|
138
135
|
status: {
|
|
139
136
|
phase: string;
|
|
140
137
|
text: string;
|