@avantics/node-red-home-automation 5.0.0 → 6.1.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 +413 -77
- package/contract-examples/room-lighting.mjs +39 -27
- package/dist/lib/activity-lighting-controller.d.ts +7 -2
- package/dist/lib/activity-lighting-controller.js +75 -16
- package/dist/lib/avr-controller.d.ts +2 -2
- package/dist/lib/avr-controller.js +21 -26
- package/dist/lib/circadian-policy.d.ts +47 -0
- package/dist/lib/circadian-policy.js +111 -0
- package/dist/lib/circadian-publishing.d.ts +33 -0
- package/dist/lib/circadian-publishing.js +154 -0
- package/dist/lib/circadian-schedule.d.ts +4 -0
- package/dist/lib/circadian-schedule.js +12 -0
- package/dist/lib/command-echo.d.ts +8 -1
- package/dist/lib/command-echo.js +37 -6
- package/dist/lib/cover-controller.js +4 -0
- package/dist/lib/denon-protocol.js +3 -2
- package/dist/lib/enable-input.d.ts +3 -0
- package/dist/lib/enable-input.js +18 -0
- package/dist/lib/house-schedule.js +2 -2
- package/dist/lib/light-contract.d.ts +53 -0
- package/dist/lib/light-contract.js +11 -0
- package/dist/lib/motion-policy.d.ts +0 -1
- package/dist/lib/motion-policy.js +9 -8
- package/dist/lib/motion-publishing.js +2 -1
- package/dist/lib/mqtt-message.d.ts +6 -0
- package/dist/lib/mqtt-message.js +21 -0
- package/dist/lib/room-controller.d.ts +41 -27
- package/dist/lib/room-controller.js +422 -213
- package/dist/lib/room-holds.d.ts +34 -0
- package/dist/lib/room-holds.js +66 -0
- package/dist/lib/room-lighting-discovery.d.ts +0 -14
- package/dist/lib/room-lighting-discovery.js +19 -125
- package/dist/lib/room-publishing.d.ts +7 -12
- package/dist/lib/room-publishing.js +65 -125
- package/dist/lib/shelly-cover.d.ts +2 -4
- package/dist/lib/shelly-cover.js +30 -70
- package/dist/lib/shelly-gen2.d.ts +25 -0
- package/dist/lib/shelly-gen2.js +58 -0
- package/dist/lib/shelly-switch.d.ts +95 -0
- package/dist/lib/shelly-switch.js +237 -0
- package/dist/lib/theatre-state-contract.js +12 -4
- package/dist/lib/z2m-cover.d.ts +49 -0
- package/dist/lib/z2m-cover.js +120 -0
- package/dist/lib/z2m-light-adapter.d.ts +62 -0
- package/dist/lib/z2m-light-adapter.js +195 -0
- package/dist/lib/z2m-light.d.ts +2 -2
- package/dist/lib/z2m-light.js +11 -14
- package/dist/nodes/activity-lighting-controller.html +15 -6
- package/dist/nodes/activity-lighting-controller.js +21 -0
- package/dist/nodes/adapter.d.ts +5 -1
- package/dist/nodes/adapter.js +35 -2
- package/dist/nodes/avr-controller.html +20 -18
- package/dist/nodes/avr-controller.js +5 -5
- package/dist/nodes/circadian-policy.d.ts +3 -0
- package/dist/nodes/circadian-policy.html +145 -0
- package/dist/nodes/circadian-policy.js +162 -0
- package/dist/nodes/cover-controller.html +9 -3
- package/dist/nodes/cover-controller.js +1 -1
- package/dist/nodes/display-mode-controller.html +14 -17
- package/dist/nodes/display-mode-controller.js +4 -1
- package/dist/nodes/house-schedule-mqtt.html +5 -0
- package/dist/nodes/house-schedule-mqtt.js +1 -1
- package/dist/nodes/house-schedule.html +3 -3
- package/dist/nodes/motion-policy.html +10 -2
- package/dist/nodes/motion-policy.js +4 -1
- package/dist/nodes/occupancy-fusion.html +5 -0
- package/dist/nodes/occupancy-fusion.js +1 -1
- package/dist/nodes/refused-enable.d.ts +7 -0
- package/dist/nodes/refused-enable.js +24 -0
- package/dist/nodes/room-controller.html +232 -85
- package/dist/nodes/room-controller.js +71 -97
- package/dist/nodes/shelly-switch.d.ts +3 -0
- package/dist/nodes/shelly-switch.html +87 -0
- package/dist/nodes/shelly-switch.js +48 -0
- package/dist/nodes/theatre-state-contract.html +9 -3
- package/dist/nodes/theatre-state-contract.js +1 -1
- package/dist/nodes/z2m-cover.d.ts +3 -0
- package/dist/nodes/z2m-cover.html +68 -0
- package/dist/nodes/z2m-cover.js +41 -0
- package/dist/nodes/z2m-light.d.ts +3 -0
- package/dist/nodes/z2m-light.html +76 -0
- package/dist/nodes/z2m-light.js +51 -0
- package/examples/Cover control.json +28 -71
- package/examples/Home theatre activity.json +53 -19
- package/examples/House schedule.json +106 -24
- package/examples/Room lighting.json +57 -25
- package/examples/Shelly cover.json +5 -16
- package/examples/Shelly switch.json +197 -0
- package/package.json +12 -4
- package/dist/lib/room-schedule.d.ts +0 -8
- package/dist/lib/room-schedule.js +0 -41
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.normaliseZ2mCoverConfig = normaliseZ2mCoverConfig;
|
|
4
|
+
exports.z2mCoverTopics = z2mCoverTopics;
|
|
5
|
+
exports.transitionZ2mCover = transitionZ2mCover;
|
|
6
|
+
const mqtt_message_1 = require("./mqtt-message");
|
|
7
|
+
// Settings may be editor strings. The friendly name is the user's own
|
|
8
|
+
// device's, so it has no default.
|
|
9
|
+
function normaliseZ2mCoverConfig(config) {
|
|
10
|
+
const baseTopic = String(config.baseTopic ?? "").trim().replace(/^\/+|\/+$/g, "") || "zigbee2mqtt";
|
|
11
|
+
const friendlyName = String(config.friendlyName ?? "").trim().replace(/^\/+|\/+$/g, "");
|
|
12
|
+
if (!friendlyName)
|
|
13
|
+
throw new Error("the device's friendly name is required");
|
|
14
|
+
if (/[+#]/.test(baseTopic) || /[+#]/.test(friendlyName))
|
|
15
|
+
throw new Error("the base topic and friendly name can't contain + or #");
|
|
16
|
+
return { baseTopic, friendlyName };
|
|
17
|
+
}
|
|
18
|
+
function z2mCoverTopics(config) {
|
|
19
|
+
const device = `${config.baseTopic}/${config.friendlyName}`;
|
|
20
|
+
return { device, set: `${device}/set`, get: `${device}/get`, availability: `${device}/availability` };
|
|
21
|
+
}
|
|
22
|
+
// "pause": stopped part-way, as some motors report it.
|
|
23
|
+
const MOTOR_STATES = { opening: "opening", closing: "closing", stopped: "stopped", pause: "stopped" };
|
|
24
|
+
const MOVING = { UP: "opening", DOWN: "closing", STOP: "stopped" };
|
|
25
|
+
const COMMAND_STATES = { open: "OPEN", close: "CLOSE", stop: "STOP" };
|
|
26
|
+
// The motion a state report gives, if any: running alone says the motor is
|
|
27
|
+
// moving but not which way, so only its false (stopped) counts.
|
|
28
|
+
function motionOf(report) {
|
|
29
|
+
if (typeof report.motor_state === "string" && MOTOR_STATES[report.motor_state])
|
|
30
|
+
return MOTOR_STATES[report.motor_state];
|
|
31
|
+
if (typeof report.moving === "string" && MOVING[report.moving])
|
|
32
|
+
return MOVING[report.moving];
|
|
33
|
+
if (report.running === false)
|
|
34
|
+
return "stopped";
|
|
35
|
+
return undefined;
|
|
36
|
+
}
|
|
37
|
+
function transitionZ2mCover(saved, msg, config, now) {
|
|
38
|
+
const state = { reportsPosition: saved?.reportsPosition !== false };
|
|
39
|
+
const topics = z2mCoverTopics(config);
|
|
40
|
+
const publications = [];
|
|
41
|
+
const readings = [];
|
|
42
|
+
const traces = [];
|
|
43
|
+
const trace = (event, outcome, reason = null, details = {}) => {
|
|
44
|
+
traces.push({ event, outcome, reason, details });
|
|
45
|
+
};
|
|
46
|
+
const publish = (topic, payload) => publications.push({ topic, payload: JSON.stringify(payload) });
|
|
47
|
+
const command = msg.cover_command;
|
|
48
|
+
if ((0, mqtt_message_1.isObject)(command)) {
|
|
49
|
+
const operation = String(command.operation);
|
|
50
|
+
if (operation === "set_position") {
|
|
51
|
+
const position = Number(command.position);
|
|
52
|
+
if (!Number.isFinite(position))
|
|
53
|
+
throw new Error("set_position needs a position");
|
|
54
|
+
publish(topics.set, { position: Math.round(Math.max(0, Math.min(100, position))) });
|
|
55
|
+
trace("command", "sent", null, { operation, position });
|
|
56
|
+
}
|
|
57
|
+
else if (Object.hasOwn(COMMAND_STATES, operation)) {
|
|
58
|
+
publish(topics.set, { state: COMMAND_STATES[operation] });
|
|
59
|
+
trace("command", "sent", null, { operation });
|
|
60
|
+
}
|
|
61
|
+
else if (operation === "observe") {
|
|
62
|
+
// A fresh read, not the last report: its position, or for a cover with
|
|
63
|
+
// none, its state. The device answers on its state topic.
|
|
64
|
+
publish(topics.get, state.reportsPosition ? { position: "" } : { state: "" });
|
|
65
|
+
trace("command", "sent", null, { operation, read: state.reportsPosition ? "position" : "state" });
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
trace("command", "ignored", "unknown-operation", { operation });
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
else if (msg.topic === topics.device) {
|
|
72
|
+
const report = (0, mqtt_message_1.parsedPayload)(msg.payload);
|
|
73
|
+
if (!(0, mqtt_message_1.isObject)(report))
|
|
74
|
+
throw new Error(`${topics.device} must carry the device's state object`);
|
|
75
|
+
const position = typeof report.position === "number" && Number.isFinite(report.position) && report.position >= 0 && report.position <= 100
|
|
76
|
+
? report.position : undefined;
|
|
77
|
+
const motion = motionOf(report);
|
|
78
|
+
// With a position, an end state only at the ends: Zigbee2MQTT's OPEN
|
|
79
|
+
// can mean no more than "not closed". Without one, its OPEN or CLOSE,
|
|
80
|
+
// but only once the motor reports it has stopped: the state can change
|
|
81
|
+
// as soon as a move is asked for, before the cover gets there.
|
|
82
|
+
const end = position !== undefined
|
|
83
|
+
? (position === 100 ? "open" : position === 0 ? "closed" : undefined)
|
|
84
|
+
: motion !== "stopped" ? undefined
|
|
85
|
+
: report.state === "OPEN" ? "open" : report.state === "CLOSE" ? "closed" : undefined;
|
|
86
|
+
if (position === undefined && motion === undefined && end === undefined) {
|
|
87
|
+
trace("report", "ignored", "no-cover-state");
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
state.reportsPosition = position !== undefined;
|
|
91
|
+
readings.push({ cover: {
|
|
92
|
+
kind: "observed",
|
|
93
|
+
...(position !== undefined ? { position } : {}),
|
|
94
|
+
...(end ? { state: end } : {}),
|
|
95
|
+
...(motion ? { motion } : {}),
|
|
96
|
+
// When received, never a time the device gives: its clock may be off.
|
|
97
|
+
observedAt: now.toISOString(),
|
|
98
|
+
...(typeof report.battery === "number" ? { battery: report.battery } : {}),
|
|
99
|
+
// A string, or a binary's true or false: passed on as text.
|
|
100
|
+
...(typeof report.charging_status === "string" || typeof report.charging_status === "boolean"
|
|
101
|
+
? { charging: String(report.charging_status) } : {})
|
|
102
|
+
} });
|
|
103
|
+
trace("report", "read", null, { position: position ?? null, motion: motion ?? null, state: end ?? null });
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
else if (msg.topic === topics.availability) {
|
|
107
|
+
// {"state": "online"}, or the plain "online" of older setups.
|
|
108
|
+
const received = (0, mqtt_message_1.parsedPayload)(msg.payload);
|
|
109
|
+
const value = (0, mqtt_message_1.isObject)(received) ? received.state : received;
|
|
110
|
+
if (value !== "online" && value !== "offline")
|
|
111
|
+
throw new Error(`${topics.availability} must say online or offline`);
|
|
112
|
+
readings.push({ cover: { kind: "availability", online: value === "online" } });
|
|
113
|
+
trace("availability", value);
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
trace("message", "ignored", "unrelated-message", { topic: typeof msg.topic === "string" ? msg.topic : null });
|
|
117
|
+
}
|
|
118
|
+
return { state, publications, readings, traces };
|
|
119
|
+
}
|
|
120
|
+
//# sourceMappingURL=z2m-cover.js.map
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { TraceEntry } from "./bounded-trace";
|
|
2
|
+
import { ECHO_WINDOW_MS, type ExpectedCommand } from "./command-echo";
|
|
3
|
+
import type { LightCommandMessage, LightField, LightReading } from "./light-contract";
|
|
4
|
+
export { ECHO_WINDOW_MS };
|
|
5
|
+
export interface Z2mLightConfig {
|
|
6
|
+
target: string;
|
|
7
|
+
baseTopic: string;
|
|
8
|
+
friendlyName: string;
|
|
9
|
+
group: boolean;
|
|
10
|
+
}
|
|
11
|
+
interface DeviceReport {
|
|
12
|
+
on: boolean;
|
|
13
|
+
brightness: unknown;
|
|
14
|
+
color_temp: unknown;
|
|
15
|
+
observedAt: string;
|
|
16
|
+
}
|
|
17
|
+
export interface Z2mLightState {
|
|
18
|
+
expected: ExpectedCommand[];
|
|
19
|
+
last: {
|
|
20
|
+
payload: Record<string, unknown>;
|
|
21
|
+
id: string;
|
|
22
|
+
} | null;
|
|
23
|
+
report: DeviceReport | null;
|
|
24
|
+
fields: LightField[];
|
|
25
|
+
online: boolean | null;
|
|
26
|
+
}
|
|
27
|
+
export interface Z2mLightMessage {
|
|
28
|
+
topic?: unknown;
|
|
29
|
+
payload?: unknown;
|
|
30
|
+
retain?: unknown;
|
|
31
|
+
light?: unknown;
|
|
32
|
+
}
|
|
33
|
+
export interface Publication {
|
|
34
|
+
topic: string;
|
|
35
|
+
payload: unknown;
|
|
36
|
+
retain: false;
|
|
37
|
+
}
|
|
38
|
+
export declare function normaliseZ2mLightConfig(config: {
|
|
39
|
+
target?: unknown;
|
|
40
|
+
baseTopic?: unknown;
|
|
41
|
+
friendlyName?: unknown;
|
|
42
|
+
group?: unknown;
|
|
43
|
+
}): Z2mLightConfig;
|
|
44
|
+
export declare function z2mLightTopics(config: Z2mLightConfig): {
|
|
45
|
+
device: string;
|
|
46
|
+
set: string;
|
|
47
|
+
get: string;
|
|
48
|
+
availability: string;
|
|
49
|
+
};
|
|
50
|
+
export declare function parseZ2mTargetTopics(stateTopic: unknown, commandTopic: unknown): {
|
|
51
|
+
baseTopic: string;
|
|
52
|
+
friendlyName: string;
|
|
53
|
+
} | null;
|
|
54
|
+
export declare function createZ2mLightState(saved?: Partial<Z2mLightState>): Z2mLightState;
|
|
55
|
+
export declare function z2mLightPayload(command: LightCommandMessage): Record<string, unknown>;
|
|
56
|
+
export declare function transitionZ2mLight(saved: Partial<Z2mLightState> | undefined, msg: Z2mLightMessage, config: Z2mLightConfig, nowValue?: Date | string | number): {
|
|
57
|
+
state: Z2mLightState;
|
|
58
|
+
publications: Publication[];
|
|
59
|
+
readings: LightReading[];
|
|
60
|
+
traces: TraceEntry<Record<string, unknown>>[];
|
|
61
|
+
ignored: boolean;
|
|
62
|
+
};
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ECHO_WINDOW_MS = void 0;
|
|
4
|
+
exports.normaliseZ2mLightConfig = normaliseZ2mLightConfig;
|
|
5
|
+
exports.z2mLightTopics = z2mLightTopics;
|
|
6
|
+
exports.parseZ2mTargetTopics = parseZ2mTargetTopics;
|
|
7
|
+
exports.createZ2mLightState = createZ2mLightState;
|
|
8
|
+
exports.z2mLightPayload = z2mLightPayload;
|
|
9
|
+
exports.transitionZ2mLight = transitionZ2mLight;
|
|
10
|
+
const command_echo_1 = require("./command-echo");
|
|
11
|
+
Object.defineProperty(exports, "ECHO_WINDOW_MS", { enumerable: true, get: function () { return command_echo_1.ECHO_WINDOW_MS; } });
|
|
12
|
+
const settings_1 = require("./settings");
|
|
13
|
+
const z2m_light_1 = require("./z2m-light");
|
|
14
|
+
function normaliseZ2mLightConfig(config) {
|
|
15
|
+
const target = (0, settings_1.textSetting)(config.target, "target", "");
|
|
16
|
+
const friendlyName = (0, settings_1.textSetting)(config.friendlyName, "friendly name", "");
|
|
17
|
+
const baseTopic = (0, settings_1.textSetting)(config.baseTopic, "base topic", "zigbee2mqtt").replace(/^\/+|\/+$/g, "");
|
|
18
|
+
if (!target)
|
|
19
|
+
throw new Error("target is required: the room's id for this light");
|
|
20
|
+
if (!friendlyName)
|
|
21
|
+
throw new Error("friendly name is required");
|
|
22
|
+
if (!baseTopic)
|
|
23
|
+
throw new Error("base topic is required");
|
|
24
|
+
if (/[+#]/.test(baseTopic + friendlyName))
|
|
25
|
+
throw new Error("base topic and friendly name cannot contain + or #");
|
|
26
|
+
return { target, baseTopic, friendlyName, group: config.group === true || config.group === "true" };
|
|
27
|
+
}
|
|
28
|
+
function z2mLightTopics(config) {
|
|
29
|
+
const device = `${config.baseTopic}/${config.friendlyName}`;
|
|
30
|
+
return { device, set: `${device}/set`, get: `${device}/get`, availability: `${device}/availability` };
|
|
31
|
+
}
|
|
32
|
+
// Where a 5.x room target's topics point, when they are Zigbee2MQTT's own:
|
|
33
|
+
// <base>/<name> and <base>/<name>/set. Null for anything else, which is set
|
|
34
|
+
// up by hand on the neutral contract.
|
|
35
|
+
function parseZ2mTargetTopics(stateTopic, commandTopic) {
|
|
36
|
+
if (typeof stateTopic !== "string" || typeof commandTopic !== "string")
|
|
37
|
+
return null;
|
|
38
|
+
if (commandTopic !== `${stateTopic}/set`)
|
|
39
|
+
return null;
|
|
40
|
+
const slash = stateTopic.indexOf("/");
|
|
41
|
+
if (slash <= 0 || slash === stateTopic.length - 1)
|
|
42
|
+
return null;
|
|
43
|
+
return { baseTopic: stateTopic.slice(0, slash), friendlyName: stateTopic.slice(slash + 1) };
|
|
44
|
+
}
|
|
45
|
+
function createZ2mLightState(saved = {}) {
|
|
46
|
+
return {
|
|
47
|
+
expected: Array.isArray(saved.expected) ? saved.expected.map((entry) => ({ ...entry })) : [],
|
|
48
|
+
last: saved.last && typeof saved.last === "object" ? { payload: { ...saved.last.payload }, id: String(saved.last.id) } : null,
|
|
49
|
+
report: saved.report && typeof saved.report === "object" ? { ...saved.report } : null,
|
|
50
|
+
fields: Array.isArray(saved.fields) ? saved.fields.filter((field) => field === "brightness" || field === "colorTemp") : [],
|
|
51
|
+
online: typeof saved.online === "boolean" ? saved.online : null
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
// A command as Zigbee2MQTT's /set payload, field for field and in the order
|
|
55
|
+
// the room built it before 6.0: the state, any other fields the intent gave,
|
|
56
|
+
// brightness and colour temperature, then the transition unless an extra
|
|
57
|
+
// field already set one.
|
|
58
|
+
function z2mLightPayload(command) {
|
|
59
|
+
if (command.state === "OFF")
|
|
60
|
+
return { state: "OFF" };
|
|
61
|
+
const payload = { state: "ON", ...(command.extra ?? {}) };
|
|
62
|
+
if (typeof command.brightnessPct === "number")
|
|
63
|
+
payload.brightness = (0, z2m_light_1.zigbeeBrightness)(command.brightnessPct);
|
|
64
|
+
if (typeof command.colorTempKelvin === "number")
|
|
65
|
+
payload.color_temp = (0, z2m_light_1.zigbeeColorTemp)(command.colorTempKelvin);
|
|
66
|
+
if (typeof command.transitionSeconds === "number" && !Object.hasOwn(payload, "transition"))
|
|
67
|
+
payload.transition = command.transitionSeconds;
|
|
68
|
+
return payload;
|
|
69
|
+
}
|
|
70
|
+
function sameValues(payload, report) {
|
|
71
|
+
return (payload.brightness === undefined || Number(report.brightness) === payload.brightness) &&
|
|
72
|
+
(payload.color_temp === undefined || Number(report.color_temp) === payload.color_temp);
|
|
73
|
+
}
|
|
74
|
+
function reportedFields(payload) {
|
|
75
|
+
const fields = [];
|
|
76
|
+
if (payload.brightness !== undefined && payload.brightness !== null)
|
|
77
|
+
fields.push("brightness");
|
|
78
|
+
if (payload.color_temp !== undefined && payload.color_temp !== null)
|
|
79
|
+
fields.push("colorTemp");
|
|
80
|
+
return fields;
|
|
81
|
+
}
|
|
82
|
+
function transitionZ2mLight(saved, msg, config, nowValue = new Date()) {
|
|
83
|
+
const now = nowValue instanceof Date ? nowValue : new Date(nowValue);
|
|
84
|
+
const nowMs = now.getTime();
|
|
85
|
+
const state = createZ2mLightState(saved);
|
|
86
|
+
const topics = z2mLightTopics(config);
|
|
87
|
+
const publications = [];
|
|
88
|
+
const readings = [];
|
|
89
|
+
const traces = [];
|
|
90
|
+
const trace = (event, outcome, reason = null, details = {}) => traces.push({ event, outcome, reason, details });
|
|
91
|
+
const target = config.target;
|
|
92
|
+
const light = msg.light && typeof msg.light === "object" ? msg.light : null;
|
|
93
|
+
if (light && (light.kind === "command" || light.kind === "observe")) {
|
|
94
|
+
// The room's messages for every light arrive here; only this one's count.
|
|
95
|
+
if (light.target !== target)
|
|
96
|
+
return { state, publications, readings, traces, ignored: true };
|
|
97
|
+
if (light.kind === "observe") {
|
|
98
|
+
if (config.group) {
|
|
99
|
+
// A group can't be asked: Zigbee2MQTT sends its state as it changes.
|
|
100
|
+
trace("observe", "ignored", "group-has-no-get");
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
publications.push({ topic: topics.get, payload: { state: "" }, retain: false });
|
|
104
|
+
trace("observe", "sent");
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
const command = light;
|
|
109
|
+
const payload = z2mLightPayload(command);
|
|
110
|
+
if (command.restyle === true && state.report?.on === true && payload.state === "ON" && sameValues(payload, state.report)) {
|
|
111
|
+
// Already there: nothing to send, as the room skipped it before 6.0.
|
|
112
|
+
readings.push({ kind: "skipped", target, id: String(command.id), observedAt: state.report.observedAt });
|
|
113
|
+
trace("command", "skipped", "already-at-level", { id: command.id });
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
state.expected = (0, command_echo_1.rememberCommand)(state.expected, payload, nowMs);
|
|
117
|
+
state.last = { payload, id: String(command.id) };
|
|
118
|
+
publications.push({ topic: topics.set, payload, retain: false });
|
|
119
|
+
if (command.restyle === true)
|
|
120
|
+
readings.push({ kind: "sent", target, id: String(command.id) });
|
|
121
|
+
trace("command", "sent", null, { id: command.id, source: command.source, state: payload.state });
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return { state, publications, readings, traces, ignored: false };
|
|
125
|
+
}
|
|
126
|
+
const topic = typeof msg.topic === "string" ? msg.topic : "";
|
|
127
|
+
const parsed = (0, command_echo_1.parsePayload)(msg.payload);
|
|
128
|
+
if (topic === topics.device) {
|
|
129
|
+
const payload = parsed && typeof parsed === "object" ? parsed : null;
|
|
130
|
+
const report = payload ? (0, z2m_light_1.readLightReport)(payload) : null;
|
|
131
|
+
if (!payload || !report) {
|
|
132
|
+
trace("report", "ignored", "no-state");
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
state.report = { ...report, observedAt: now.toISOString() };
|
|
136
|
+
for (const field of reportedFields(payload))
|
|
137
|
+
if (!state.fields.includes(field))
|
|
138
|
+
state.fields.push(field);
|
|
139
|
+
readings.push(observedReading(state.report));
|
|
140
|
+
trace("report", "read", null, { on: report.on });
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
else if (topic === topics.set) {
|
|
144
|
+
const payload = parsed && typeof parsed === "object" ? parsed : null;
|
|
145
|
+
if (msg.retain === true) {
|
|
146
|
+
trace("set", "ignored", "retained-message");
|
|
147
|
+
}
|
|
148
|
+
else if (payload) {
|
|
149
|
+
const echo = (0, command_echo_1.checkCommandEcho)(state.expected, payload, nowMs);
|
|
150
|
+
state.expected = echo.queue;
|
|
151
|
+
const requested = String(payload.state ?? "").toUpperCase();
|
|
152
|
+
if (echo.changed) {
|
|
153
|
+
readings.push({ kind: "echo-changed", target, id: state.last?.id ?? null, state: requested });
|
|
154
|
+
trace("set", "own", "mutated-own-command", { state: requested });
|
|
155
|
+
}
|
|
156
|
+
else if (echo.own) {
|
|
157
|
+
trace("set", "own", "echo");
|
|
158
|
+
}
|
|
159
|
+
else {
|
|
160
|
+
readings.push({ kind: "external", target, state: requested, setsValues: (0, z2m_light_1.setsLightValues)(payload), observedAt: now.toISOString() });
|
|
161
|
+
trace("set", "external", null, { state: requested });
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
else if (topic === topics.availability && !config.group) {
|
|
166
|
+
const value = parsed && typeof parsed === "object" ? parsed.state : parsed;
|
|
167
|
+
const online = String(value).toLowerCase() === "online";
|
|
168
|
+
if (String(value).toLowerCase() === "online" || String(value).toLowerCase() === "offline") {
|
|
169
|
+
state.online = online;
|
|
170
|
+
readings.push({ kind: "availability", target, online });
|
|
171
|
+
trace("availability", online ? "online" : "offline");
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
else if (topic !== "") {
|
|
175
|
+
trace("message", "ignored", "other-topic", { topic });
|
|
176
|
+
}
|
|
177
|
+
return { state, publications, readings, traces, ignored: false };
|
|
178
|
+
function observedReading(report) {
|
|
179
|
+
const brightness = Number(report.brightness);
|
|
180
|
+
const colorTemp = Number(report.color_temp);
|
|
181
|
+
const last = state.last?.payload;
|
|
182
|
+
const showsLast = last !== undefined && report.on === (last.state === "ON") && (last.state === "OFF" || sameValues(last, report));
|
|
183
|
+
return {
|
|
184
|
+
kind: "observed",
|
|
185
|
+
target,
|
|
186
|
+
on: report.on,
|
|
187
|
+
...(report.brightness !== null && Number.isFinite(brightness) ? { brightnessPct: brightness * 100 / 254 } : {}),
|
|
188
|
+
...(report.color_temp !== null && Number.isFinite(colorTemp) && colorTemp > 0 ? { colorTempKelvin: 1_000_000 / colorTemp } : {}),
|
|
189
|
+
fields: [...state.fields],
|
|
190
|
+
showsLast,
|
|
191
|
+
observedAt: report.observedAt
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
//# sourceMappingURL=z2m-light-adapter.js.map
|
package/dist/lib/z2m-light.d.ts
CHANGED
|
@@ -14,10 +14,10 @@ export type LightCommand = {
|
|
|
14
14
|
export declare function zigbeeBrightness(percent: number): number;
|
|
15
15
|
export declare function zigbeeColorTemp(kelvin: number): number;
|
|
16
16
|
export declare function lightCommand(state: string, style: LightStyle, transitionSeconds: number): LightCommand;
|
|
17
|
+
export declare const REMOVED_LIGHT_FIELDS: readonly ["brightness", "color_temp"];
|
|
17
18
|
export declare function lightValues(values: Record<string, unknown>): {
|
|
18
19
|
values: Record<string, unknown>;
|
|
19
|
-
|
|
20
|
-
superseded: string[];
|
|
20
|
+
removed: string[];
|
|
21
21
|
invalid: string[];
|
|
22
22
|
};
|
|
23
23
|
export declare function setsLightValues(command: Record<string, unknown>): boolean;
|
package/dist/lib/z2m-light.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.REMOVED_LIGHT_FIELDS = void 0;
|
|
3
4
|
exports.readLightReport = readLightReport;
|
|
4
5
|
exports.zigbeeBrightness = zigbeeBrightness;
|
|
5
6
|
exports.zigbeeColorTemp = zigbeeColorTemp;
|
|
@@ -42,18 +43,17 @@ function numberValue(value) {
|
|
|
42
43
|
}
|
|
43
44
|
// The package's own value fields and the Zigbee2MQTT field each becomes.
|
|
44
45
|
const NEUTRAL_FIELDS = { brightnessPct: "brightness", colorTempKelvin: "color_temp" };
|
|
46
|
+
// The fields in Zigbee2MQTT's own units that 5.x still took, removed in 6.0.
|
|
47
|
+
exports.REMOVED_LIGHT_FIELDS = ["brightness", "color_temp"];
|
|
45
48
|
// An intent's or profile's values as Zigbee2MQTT fields: brightnessPct and
|
|
46
49
|
// colorTempKelvin converted; anything else (an effect, say) passed on as it
|
|
47
|
-
// is. brightness and color_temp
|
|
48
|
-
// they are
|
|
49
|
-
//
|
|
50
|
-
// one is `superseded`. An unusable brightnessPct or colorTempKelvin is
|
|
51
|
-
// `invalid` and never reaches the device.
|
|
50
|
+
// is. brightness and color_temp, Zigbee2MQTT's own units, are no longer
|
|
51
|
+
// taken: they are `removed`, and never reach the device. An unusable
|
|
52
|
+
// brightnessPct or colorTempKelvin is `invalid`, and never reaches it either.
|
|
52
53
|
function lightValues(values) {
|
|
53
54
|
const converted = {};
|
|
54
|
-
const
|
|
55
|
+
const removed = [];
|
|
55
56
|
const invalid = [];
|
|
56
|
-
let legacy = false;
|
|
57
57
|
const neutral = {};
|
|
58
58
|
for (const [field, device] of Object.entries(NEUTRAL_FIELDS)) {
|
|
59
59
|
if (!Object.hasOwn(values, field))
|
|
@@ -67,16 +67,13 @@ function lightValues(values) {
|
|
|
67
67
|
for (const [key, value] of Object.entries(values)) {
|
|
68
68
|
if (Object.hasOwn(NEUTRAL_FIELDS, key))
|
|
69
69
|
continue;
|
|
70
|
-
if (key
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
continue;
|
|
74
|
-
}
|
|
75
|
-
legacy = true;
|
|
70
|
+
if (exports.REMOVED_LIGHT_FIELDS.includes(key)) {
|
|
71
|
+
removed.push(key);
|
|
72
|
+
continue;
|
|
76
73
|
}
|
|
77
74
|
converted[key] = value;
|
|
78
75
|
}
|
|
79
|
-
return { values: { ...converted, ...neutral },
|
|
76
|
+
return { values: { ...converted, ...neutral }, removed, invalid };
|
|
80
77
|
}
|
|
81
78
|
// Command fields that set a light's values rather than just its power.
|
|
82
79
|
const VALUE_FIELDS = [
|
|
@@ -30,19 +30,28 @@
|
|
|
30
30
|
</script>
|
|
31
31
|
|
|
32
32
|
<script type="text/html" data-help-name="home-activity-lighting-controller">
|
|
33
|
-
<p>Applies configuration-owned activity styles only to room lights that are already on and not manually owned
|
|
34
|
-
|
|
33
|
+
<p>Applies configuration-owned activity styles only to room lights that are already on and not manually owned. If
|
|
34
|
+
a person changes a light the activity holds, the node reports <code>manual_override</code>, as the display,
|
|
35
|
+
receiver and backlight do; lighting is optional, so the theatre is degraded, not failed, and the next request takes
|
|
36
|
+
the light back.</p>
|
|
37
|
+
<p>Input <code>msg.activityLighting</code> carries generation-scoped intent, refresh, result or tick events. Wire the room lighting controller's third output here too: it passes on each light's reading (<code>msg.light</code>, from the light's adapter) with whether a person set it, which the node takes as an observation of the request in hand. Output 1 emits standard <code>msg.lighting</code> intents for a room controller. Output 2 emits theatre <code>target_state</code> evidence. Room-light failure is optional degradation, not activity success.</p>
|
|
35
38
|
<p>Each profile is <code>{targets: [...], values: {...}}</code>, with <code>values</code> in the package's own units:
|
|
36
39
|
<code>brightnessPct</code> (0-100) and <code>colorTempKelvin</code>, for example
|
|
37
40
|
<code>{"tv": {"targets": ["main"], "values": {"brightnessPct": 25, "colorTempKelvin": 2500}}}</code>.
|
|
38
41
|
<code>brightnessPct</code> runs from 0 (the dimmest the light goes, not off) to 100, and
|
|
39
42
|
<code>colorTempKelvin</code> must be above 0; anything else is an invalid configuration. The room
|
|
40
43
|
controller converts them for the light, and a light's report (in its own units) is compared after the same
|
|
41
|
-
conversion. Zigbee2MQTT's <code>brightness</code> and <code>color_temp</code>
|
|
42
|
-
|
|
44
|
+
conversion. Zigbee2MQTT's own <code>brightness</code> and <code>color_temp</code> were removed in 6.0: a profile
|
|
45
|
+
using them is an invalid configuration.</p>
|
|
46
|
+
<p>Each light is judged by what it reports. A light that has reported none of a profile's fields since the node
|
|
47
|
+
started (no brightness, no colour temperature), as one that only switches on and off, is confirmed by being on;
|
|
48
|
+
once it has reported one, it is held to the profile's values: it confirms when its adapter says it shows the last
|
|
49
|
+
command sent to it, which is the profile's while the activity holds it. A profile that gives values to a light that has never
|
|
50
|
+
reported them gets one warning per deploy: they may not apply. Mark such a light <code>onOffOnly</code> on the
|
|
51
|
+
room, and the room won't send them.</p>
|
|
43
52
|
<p>While a profile is active, its lights are overridden in the room controller under <b>Owner</b>
|
|
44
|
-
(default <code>activity</code>), so circadian
|
|
53
|
+
(default <code>activity</code>), so the room's style (from a circadian lighting policy) leaves the activity's alone. When a new profile
|
|
45
54
|
no longer includes a light, including a switch to an off profile with no targets, the node emits
|
|
46
|
-
<code>{kind: "release", owner, targets}</code> for those lights and the room controller
|
|
55
|
+
<code>{kind: "release", owner, targets}</code> for those lights, and the room controller gives each back what it was before the activity took it: one the room was running is restyled.
|
|
47
56
|
Give each activity-lighting node that feeds the same room controller its own owner.</p>
|
|
48
57
|
</script>
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
const activity_lighting_controller_1 = require("../lib/activity-lighting-controller");
|
|
3
|
+
const light_contract_1 = require("../lib/light-contract");
|
|
3
4
|
const adapter_1 = require("./adapter");
|
|
4
5
|
const settings_1 = require("../lib/settings");
|
|
5
6
|
const registerActivityLightingController = (RED) => {
|
|
@@ -59,8 +60,17 @@ const registerActivityLightingController = (RED) => {
|
|
|
59
60
|
}
|
|
60
61
|
updateStatus(result.status);
|
|
61
62
|
schedule(result.state);
|
|
63
|
+
// Once per deploy: a light the profile gives values to that reports
|
|
64
|
+
// none of them, confirmed by being on.
|
|
65
|
+
const unreported = result.traces.find(({ reason }) => reason === activity_lighting_controller_1.VALUES_NOT_REPORTED);
|
|
66
|
+
if (unreported && !warnedUnreported) {
|
|
67
|
+
warnedUnreported = true;
|
|
68
|
+
node.warn(`${String(unreported.details.target)} reports no brightness or colour, so the activity confirms it ` +
|
|
69
|
+
"by its being on, and the profile's values for it may not apply: a light that only switches on and off can be marked onOffOnly on the room");
|
|
70
|
+
}
|
|
62
71
|
return result;
|
|
63
72
|
}
|
|
73
|
+
let warnedUnreported = false;
|
|
64
74
|
// A pending transition is here only if the node's context survived its
|
|
65
75
|
// stop, which needs a configured context store (see the README); timers
|
|
66
76
|
// never survive. Recover it with the reducer's own startup (re-observe,
|
|
@@ -72,6 +82,17 @@ const registerActivityLightingController = (RED) => {
|
|
|
72
82
|
}), 0);
|
|
73
83
|
}
|
|
74
84
|
(0, adapter_1.onInput)(node, (msg, send) => {
|
|
85
|
+
// A light's reading, as the room controller passes it on (its third
|
|
86
|
+
// output): an observation for the request in hand.
|
|
87
|
+
const light = (0, light_contract_1.isLightReading)(msg.light) && msg.light.kind === "observed" ? msg.light : null;
|
|
88
|
+
if (light) {
|
|
89
|
+
const generation = context.get("state")?.generation ?? 0;
|
|
90
|
+
if (generation < 1)
|
|
91
|
+
return;
|
|
92
|
+
process({ activityLighting: { kind: "observed", generation, target: light.target, on: light.on, manual: light.manual === true,
|
|
93
|
+
fields: light.fields, showsLast: light.showsLast, observedAt: light.observedAt } }, send);
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
75
96
|
process({
|
|
76
97
|
activityLighting: (0, adapter_1.objectField)(msg, "activityLighting")
|
|
77
98
|
}, send);
|
package/dist/nodes/adapter.d.ts
CHANGED
|
@@ -4,7 +4,11 @@ import { type RetainedClear } from "../lib/retained-topics";
|
|
|
4
4
|
export type Send = (msg: NodeMessage | Array<NodeMessage | NodeMessage[] | null>) => void;
|
|
5
5
|
export type InputMessage = NodeMessage & Record<string, unknown>;
|
|
6
6
|
export declare function objectField<T>(msg: InputMessage, key: string): T | undefined;
|
|
7
|
-
export declare function
|
|
7
|
+
export declare function mqttConnected(msg: InputMessage): boolean;
|
|
8
|
+
export declare function onInput(node: Node, handle: (msg: InputMessage, send: Send) => void, startup?: {
|
|
9
|
+
message: Record<string, unknown>;
|
|
10
|
+
trace: TraceMetadata;
|
|
11
|
+
}): void;
|
|
8
12
|
export declare function guarded(node: Node, work: () => void): () => void;
|
|
9
13
|
export declare function runReducer<Saved, Result extends {
|
|
10
14
|
state: unknown;
|
package/dist/nodes/adapter.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.objectField = objectField;
|
|
4
|
+
exports.mqttConnected = mqttConnected;
|
|
4
5
|
exports.onInput = onInput;
|
|
5
6
|
exports.guarded = guarded;
|
|
6
7
|
exports.runReducer = runReducer;
|
|
@@ -18,11 +19,43 @@ function objectField(msg, key) {
|
|
|
18
19
|
const value = msg[key];
|
|
19
20
|
return value && typeof value === "object" ? value : undefined;
|
|
20
21
|
}
|
|
22
|
+
// The status an MQTT out node shows each time its broker connects, as
|
|
23
|
+
// Node-RED's Status node passes it on (Node-RED 5.0.10:
|
|
24
|
+
// nodes/core/network/10-mqtt.js, setStatusConnected; runtime/lib/flows/Flow.js,
|
|
25
|
+
// handleStatus, which adds status.source).
|
|
26
|
+
const MQTT_CONNECTED = "node-red:common.status.connected";
|
|
27
|
+
// A Status node's message: msg.status with the node it came from as
|
|
28
|
+
// status.source.
|
|
29
|
+
function fromStatusNode(msg) {
|
|
30
|
+
const status = objectField(msg, "status");
|
|
31
|
+
return Boolean(status?.source && typeof status.source === "object");
|
|
32
|
+
}
|
|
33
|
+
// Whether a message is a Status node's report that an MQTT out node has
|
|
34
|
+
// connected: until then, the broker drops what it is given to publish.
|
|
35
|
+
function mqttConnected(msg) {
|
|
36
|
+
const status = objectField(msg, "status");
|
|
37
|
+
return fromStatusNode(msg) && status?.text === MQTT_CONNECTED && status.source?.type === "mqtt out";
|
|
38
|
+
}
|
|
21
39
|
// Handle each input; an error is reported against the message and ends it.
|
|
22
|
-
|
|
40
|
+
// With a startup message, a node that announces itself takes a Status
|
|
41
|
+
// node's "MQTT out connected" as that startup message, so it announces again
|
|
42
|
+
// once the broker can take it; any other Status node message is ignored, and
|
|
43
|
+
// traced. Any other message is handled as it is.
|
|
44
|
+
function onInput(node, handle, startup) {
|
|
23
45
|
node.on("input", (msg, send, done) => {
|
|
24
46
|
try {
|
|
25
|
-
|
|
47
|
+
const input = msg;
|
|
48
|
+
if (startup && mqttConnected(input)) {
|
|
49
|
+
handle(structuredClone(startup.message), send);
|
|
50
|
+
}
|
|
51
|
+
else if (startup && fromStatusNode(input)) {
|
|
52
|
+
const status = input.status;
|
|
53
|
+
(0, bounded_trace_1.appendTrace)(node.context(), startup.trace, [{ event: "input", outcome: "ignored", reason: "status-message",
|
|
54
|
+
details: { text: status.text, source: status.source.type } }]);
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
handle(input, send);
|
|
58
|
+
}
|
|
26
59
|
done();
|
|
27
60
|
}
|
|
28
61
|
catch (error) {
|