@avantics/node-red-home-automation 5.0.0 → 6.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 +340 -70
- 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 +38 -27
- package/dist/lib/room-controller.js +310 -204
- 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 +14 -5
- package/dist/nodes/activity-lighting-controller.js +21 -0
- package/dist/nodes/avr-controller.html +15 -18
- package/dist/nodes/avr-controller.js +4 -4
- package/dist/nodes/circadian-policy.d.ts +3 -0
- package/dist/nodes/circadian-policy.html +140 -0
- package/dist/nodes/circadian-policy.js +162 -0
- package/dist/nodes/cover-controller.html +4 -3
- package/dist/nodes/display-mode-controller.html +14 -17
- package/dist/nodes/display-mode-controller.js +4 -1
- package/dist/nodes/house-schedule.html +3 -3
- package/dist/nodes/motion-policy.html +5 -2
- package/dist/nodes/motion-policy.js +3 -0
- package/dist/nodes/refused-enable.d.ts +7 -0
- package/dist/nodes/refused-enable.js +24 -0
- package/dist/nodes/room-controller.html +215 -82
- package/dist/nodes/room-controller.js +70 -96
- package/dist/nodes/shelly-switch.d.ts +3 -0
- package/dist/nodes/shelly-switch.html +82 -0
- package/dist/nodes/shelly-switch.js +48 -0
- package/dist/nodes/theatre-state-contract.html +4 -3
- 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 +72 -0
- package/dist/nodes/z2m-light.js +50 -0
- package/examples/Cover control.json +24 -56
- package/examples/Home theatre activity.json +49 -4
- package/examples/House schedule.json +106 -24
- package/examples/Room lighting.json +54 -11
- 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,18 +30,27 @@
|
|
|
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
55
|
<code>{kind: "release", owner, targets}</code> for those lights and the room controller restyles them.
|
|
47
56
|
Give each activity-lighting node that feeds the same room controller its own owner.</p>
|
|
@@ -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);
|
|
@@ -2,10 +2,9 @@
|
|
|
2
2
|
(function () {
|
|
3
3
|
function on(value) { return value === true || value === "true"; }
|
|
4
4
|
function given(value) { return value !== undefined && value !== null && String(value).trim() !== ""; }
|
|
5
|
-
// A dB bound
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
}
|
|
5
|
+
// A dB bound. A node saved before 5.0 has none (its 0-98 bounds were
|
|
6
|
+
// removed in 6.0), so it shows as invalid until they are set.
|
|
7
|
+
function volumeBound(value) { return given(value) && Number.isFinite(Number(value)); }
|
|
9
8
|
RED.nodes.registerType("home-avr-controller", {
|
|
10
9
|
category: "home automation",
|
|
11
10
|
color: "#c7d9f1",
|
|
@@ -22,15 +21,16 @@
|
|
|
22
21
|
sources: {value: "{\"shield\":\"SAT/CBL\",\"playstation\":\"GAME\"}", required: true, validate: function (v) { try { JSON.parse(v); return true; } catch (e) { return false; } }},
|
|
23
22
|
retryMs: {value: 2000, required: true, validate: RED.validators.number()},
|
|
24
23
|
maxAttempts: {value: 3, required: true, validate: RED.validators.number()},
|
|
25
|
-
// In dB (the receiver's 80 is 0 dB).
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
//
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
//
|
|
32
|
-
//
|
|
33
|
-
//
|
|
24
|
+
// In dB (the receiver's 80 is 0 dB).
|
|
25
|
+
minVolumeDb: {value: -80, validate: volumeBound},
|
|
26
|
+
maxVolumeDb: {value: -20, validate: volumeBound},
|
|
27
|
+
// The 0-98 bounds of a node saved before 5.0, removed in 6.0 but kept
|
|
28
|
+
// here, with no form row, so the editor carries them: the editor keeps
|
|
29
|
+
// only the properties named here, and without them a deploy from the
|
|
30
|
+
// editor would leave the node with no bounds at all, which the runtime
|
|
31
|
+
// would give the default bounds, possibly louder than the old cap.
|
|
32
|
+
// With them, the node stays an invalid configuration until its dB
|
|
33
|
+
// bounds are set.
|
|
34
34
|
minVolume: {value: ""},
|
|
35
35
|
maxVolume: {value: ""},
|
|
36
36
|
// The receiver published nothing before these settings, so both start off.
|
|
@@ -47,10 +47,6 @@
|
|
|
47
47
|
this.idScheme = "node";
|
|
48
48
|
},
|
|
49
49
|
oneditprepare: function () {
|
|
50
|
-
// A flow saved before 5.0: show its volume bounds in dB, so saving
|
|
51
|
-
// the dialog keeps them.
|
|
52
|
-
if (!given(this.minVolumeDb) && given(this.minVolume)) $("#node-input-minVolumeDb").val(Number(this.minVolume) - 80);
|
|
53
|
-
if (!given(this.maxVolumeDb) && given(this.maxVolume)) $("#node-input-maxVolumeDb").val(Number(this.maxVolume) - 80);
|
|
54
50
|
$("#node-input-publishState").prop("checked", on(this.publishState));
|
|
55
51
|
$("#node-input-discovery").prop("checked", on(this.discovery));
|
|
56
52
|
// Discovery describes the published state, so it needs it.
|
|
@@ -63,6 +59,7 @@
|
|
|
63
59
|
$("#node-input-publishState, #node-input-discovery").on("change", follow);
|
|
64
60
|
follow();
|
|
65
61
|
},
|
|
62
|
+
// Once the dB bounds are set, the removed 0-98 ones go.
|
|
66
63
|
oneditsave: function () {
|
|
67
64
|
if (given($("#node-input-minVolumeDb").val())) this.minVolume = "";
|
|
68
65
|
if (given($("#node-input-maxVolumeDb").val())) this.maxVolume = "";
|
|
@@ -93,7 +90,7 @@
|
|
|
93
90
|
<p>Owns explicit power, main-zone, source and bounded volume commands for a Denon AVR.</p>
|
|
94
91
|
<p>Send protocol observations as <code>msg.avr = {kind: "observed", line: msg.payload}</code>.</p>
|
|
95
92
|
<p>Send activity requests as <code>msg.avr = {kind: "intent", requestId, generation, power: "on", source: "shield"}</code>. Omit <code>source</code> to preserve the current input, or use <code>power: "standby"</code> to stop the AVR.</p>
|
|
96
|
-
<p>Send volume requests as <code>msg.avr = {kind: "volume", requestId, mode: "absolute", valueDb: -50}</code> or use <code>mode: "relative"</code> with a positive or negative change. Volume is in dB, as are the bounds, results and published state; the receiver's 80 is 0 dB. A <code>value</code> in the receiver's own 0-98
|
|
93
|
+
<p>Send volume requests as <code>msg.avr = {kind: "volume", requestId, mode: "absolute", valueDb: -50}</code> or use <code>mode: "relative"</code> with a positive or negative change. Volume is in dB, as are the bounds, results and published state; the receiver's 80 is 0 dB. A <code>value</code> in the receiver's own 0-98 was removed in 6.0: such a request is answered <code>volume_rejected</code> with reason <code>removed-volume-value</code>, with a warning in the log. A node saved before 5.0 with only its 0-98 bounds is an invalid configuration until <b>Minimum volume (dB)</b> and <b>Maximum volume (dB)</b> are set. The controller applies configured bounds, accumulates rapid relative requests, confirms fresh <code>MV</code> evidence and releases ownership so external adjustments are preserved.</p>
|
|
97
94
|
<p>Send <code>kind: "startup"</code> after deployment to discard stale observations and query the receiver; a volume request still pending then is reported as failed with reason <code>restart</code>. Send <code>kind: "refresh"</code> to re-read the receiver without disturbing work in flight. Output 1 connects to a TCP output node; output 2 emits state plus ready, failed, rejected, superseded and volume results.</p>
|
|
98
95
|
<p>While it has a request, output 2 also carries <code>msg.theatre = {kind: "target_state", target: "avr", requestId, generation, status, observedAt, details}</code>, so it can be wired straight back to a theatre activity controller. A ready state is stamped with the oldest receiver reading the request relies on, and a <code>refresh</code> is answered once the receiver reports again.</p>
|
|
99
96
|
<h3>Volume results</h3>
|
|
@@ -87,7 +87,7 @@ const registerAvrController = (RED) => {
|
|
|
87
87
|
output: 2,
|
|
88
88
|
outputs: 3
|
|
89
89
|
});
|
|
90
|
-
let
|
|
90
|
+
let warnedRemovedVolume = false;
|
|
91
91
|
function process(msg, send) {
|
|
92
92
|
const before = observed();
|
|
93
93
|
const result = (0, adapter_1.runReducer)(node, { area: controllerConfig.area, component: "avr-controller" }, (saved, now) => (0, avr_controller_1.transitionAvr)(saved, msg, controllerConfig, now));
|
|
@@ -97,9 +97,9 @@ const registerAvrController = (RED) => {
|
|
|
97
97
|
}));
|
|
98
98
|
// Once per deploy, so a flow still sending the receiver's own units is
|
|
99
99
|
// told without filling the log.
|
|
100
|
-
if (!
|
|
101
|
-
|
|
102
|
-
node.warn("volume requests with value in the receiver's own 0-98
|
|
100
|
+
if (!warnedRemovedVolume && result.events.some(({ avr }) => avr.kind === "volume_rejected" && avr.reason === avr_controller_1.REMOVED_VOLUME_VALUE)) {
|
|
101
|
+
warnedRemovedVolume = true;
|
|
102
|
+
node.warn("volume requests with value in the receiver's own 0-98 were removed in 6.0 and are rejected: " +
|
|
103
103
|
"send valueDb (dB, where the receiver's 80 is 0 dB) instead");
|
|
104
104
|
}
|
|
105
105
|
const outputs = [...result.events, ...result.targetStates];
|