@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
package/dist/lib/shelly-cover.js
CHANGED
|
@@ -3,6 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.normaliseShellyCoverConfig = normaliseShellyCoverConfig;
|
|
4
4
|
exports.shellyCoverTopics = shellyCoverTopics;
|
|
5
5
|
exports.transitionShellyCover = transitionShellyCover;
|
|
6
|
+
const mqtt_message_1 = require("./mqtt-message");
|
|
7
|
+
const shelly_gen2_1 = require("./shelly-gen2");
|
|
6
8
|
// Settings may be editor strings. The prefix names the user's own device,
|
|
7
9
|
// so it has no default.
|
|
8
10
|
function normaliseShellyCoverConfig(config) {
|
|
@@ -17,14 +19,7 @@ function normaliseShellyCoverConfig(config) {
|
|
|
17
19
|
return { topicPrefix, channel };
|
|
18
20
|
}
|
|
19
21
|
function shellyCoverTopics(config) {
|
|
20
|
-
|
|
21
|
-
return {
|
|
22
|
-
command: `${config.topicPrefix}/command/${component}`,
|
|
23
|
-
status: `${config.topicPrefix}/status/${component}`,
|
|
24
|
-
events: `${config.topicPrefix}/events/rpc`,
|
|
25
|
-
online: `${config.topicPrefix}/online`,
|
|
26
|
-
component
|
|
27
|
-
};
|
|
22
|
+
return (0, shelly_gen2_1.shellyTopics)(config.topicPrefix, `cover:${config.channel}`);
|
|
28
23
|
}
|
|
29
24
|
const MOTIONS = {
|
|
30
25
|
opening: "opening",
|
|
@@ -33,25 +28,10 @@ const MOTIONS = {
|
|
|
33
28
|
closed: "stopped",
|
|
34
29
|
stopped: "stopped"
|
|
35
30
|
};
|
|
36
|
-
// An MQTT in node may hand over a parsed object, a string or a Buffer.
|
|
37
|
-
function parsed(payload) {
|
|
38
|
-
const text = Buffer.isBuffer(payload) ? payload.toString("utf8") : payload;
|
|
39
|
-
if (typeof text !== "string")
|
|
40
|
-
return text;
|
|
41
|
-
try {
|
|
42
|
-
return JSON.parse(text);
|
|
43
|
-
}
|
|
44
|
-
catch {
|
|
45
|
-
return text;
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
function isObject(value) {
|
|
49
|
-
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
50
|
-
}
|
|
51
31
|
function transitionShellyCover(saved, msg, config, now) {
|
|
52
32
|
const state = {
|
|
53
|
-
status: isObject(saved?.status) ? { ...saved.status } : null,
|
|
54
|
-
complete: saved?.complete === true && isObject(saved?.status),
|
|
33
|
+
status: (0, mqtt_message_1.isObject)(saved?.status) ? { ...saved.status } : null,
|
|
34
|
+
complete: saved?.complete === true && (0, mqtt_message_1.isObject)(saved?.status),
|
|
55
35
|
command: saved?.command && Number.isInteger(saved.command.generation) ? { ...saved.command } : null
|
|
56
36
|
};
|
|
57
37
|
const topics = shellyCoverTopics(config);
|
|
@@ -100,7 +80,7 @@ function transitionShellyCover(saved, msg, config, now) {
|
|
|
100
80
|
trace("status", reply ? "read" : "changed", null, { position: status.current_pos, state: status.state ?? null });
|
|
101
81
|
}
|
|
102
82
|
const command = msg.cover_command;
|
|
103
|
-
if (isObject(command)) {
|
|
83
|
+
if ((0, mqtt_message_1.isObject)(command)) {
|
|
104
84
|
const operation = String(command.operation);
|
|
105
85
|
const generation = Number(command.generation);
|
|
106
86
|
if (operation === "set_position") {
|
|
@@ -130,59 +110,39 @@ function transitionShellyCover(saved, msg, config, now) {
|
|
|
130
110
|
trace("command", "ignored", "unknown-operation", { operation });
|
|
131
111
|
}
|
|
132
112
|
}
|
|
133
|
-
else
|
|
134
|
-
const
|
|
135
|
-
if (
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
read(now.toISOString(), true);
|
|
140
|
-
}
|
|
141
|
-
else if (msg.topic === topics.events) {
|
|
142
|
-
const received = parsed(msg.payload);
|
|
143
|
-
const frame = isObject(received) && (received.method === "NotifyStatus" || received.method === "NotifyFullStatus") &&
|
|
144
|
-
isObject(received.params) ? { method: received.method, params: received.params } : null;
|
|
145
|
-
const change = frame?.params[topics.component];
|
|
146
|
-
if (!frame || !isObject(change)) {
|
|
147
|
-
trace("event", "ignored", "not-this-cover");
|
|
113
|
+
else {
|
|
114
|
+
const message = (0, shelly_gen2_1.readShellyMessage)(msg, topics, state.status, ["current_pos"]);
|
|
115
|
+
if (message.kind === "status") {
|
|
116
|
+
state.status = message.status;
|
|
117
|
+
state.complete = true;
|
|
118
|
+
read(now.toISOString(), true);
|
|
148
119
|
}
|
|
149
|
-
else {
|
|
150
|
-
|
|
151
|
-
const status = frame.method === "NotifyFullStatus" ? {} : { ...state.status };
|
|
152
|
-
for (const [key, value] of Object.entries(change)) {
|
|
153
|
-
if (value === null && key !== "current_pos")
|
|
154
|
-
delete status[key];
|
|
155
|
-
else
|
|
156
|
-
status[key] = value;
|
|
157
|
-
}
|
|
158
|
-
state.status = status;
|
|
159
|
-
if (frame.method === "NotifyFullStatus")
|
|
160
|
-
state.complete = true;
|
|
120
|
+
else if (message.kind === "change") {
|
|
121
|
+
state.status = message.status;
|
|
161
122
|
// Stamped when received, as a status reply is, never with the
|
|
162
123
|
// device's own ts: a Shelly whose clock is behind (no time server, a
|
|
163
124
|
// drifting clock) would have every change dropped as older than the
|
|
164
125
|
// last reading. MQTT's delay is far below what the controller resolves.
|
|
165
|
-
const observedAt = now.toISOString();
|
|
166
126
|
// Only a change of position or movement is a new reading; a power or
|
|
167
127
|
// temperature update mid-travel isn't.
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
read(
|
|
128
|
+
const { change } = message;
|
|
129
|
+
if (Object.hasOwn(change, "current_pos") || Object.hasOwn(change, "state") || Object.hasOwn(change, "pos_control")) {
|
|
130
|
+
read(now.toISOString(), false);
|
|
171
131
|
}
|
|
172
132
|
}
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
133
|
+
else if (message.kind === "online") {
|
|
134
|
+
readings.push({ cover: { kind: "availability", online: message.online } });
|
|
135
|
+
trace("availability", message.online ? "online" : "offline");
|
|
136
|
+
// Back online, its position may have changed while it was away.
|
|
137
|
+
if (message.online)
|
|
138
|
+
requestStatus("online");
|
|
139
|
+
}
|
|
140
|
+
else if (msg.topic === topics.events) {
|
|
141
|
+
trace("event", "ignored", "not-this-cover");
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
trace("message", "ignored", "unrelated-message", { topic: typeof msg.topic === "string" ? msg.topic : null });
|
|
145
|
+
}
|
|
186
146
|
}
|
|
187
147
|
const status = state.status;
|
|
188
148
|
const text = !status ? "waiting for the device"
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export declare function shellyTopics(topicPrefix: string, component: string): {
|
|
2
|
+
command: string;
|
|
3
|
+
status: string;
|
|
4
|
+
events: string;
|
|
5
|
+
online: string;
|
|
6
|
+
component: string;
|
|
7
|
+
};
|
|
8
|
+
export type ShellyTopics = ReturnType<typeof shellyTopics>;
|
|
9
|
+
export type ShellyMessage = {
|
|
10
|
+
kind: "status";
|
|
11
|
+
status: Record<string, unknown>;
|
|
12
|
+
} | {
|
|
13
|
+
kind: "change";
|
|
14
|
+
status: Record<string, unknown>;
|
|
15
|
+
change: Record<string, unknown>;
|
|
16
|
+
} | {
|
|
17
|
+
kind: "online";
|
|
18
|
+
online: boolean;
|
|
19
|
+
} | {
|
|
20
|
+
kind: "other";
|
|
21
|
+
};
|
|
22
|
+
export declare function readShellyMessage(msg: {
|
|
23
|
+
topic?: unknown;
|
|
24
|
+
payload?: unknown;
|
|
25
|
+
}, topics: ShellyTopics, last: Record<string, unknown> | null, keepNull?: readonly string[]): ShellyMessage;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.shellyTopics = shellyTopics;
|
|
4
|
+
exports.readShellyMessage = readShellyMessage;
|
|
5
|
+
// What the Shelly Gen2+ adapters share: one component's topics on the
|
|
6
|
+
// device's MQTT API, and what a message on them says about its status.
|
|
7
|
+
//
|
|
8
|
+
// From Shelly's Gen2 API documentation (Mqtt and Notifications):
|
|
9
|
+
// - MQTT control commands are text on <prefix>/command/<component>; a
|
|
10
|
+
// "status_update" there makes the device publish the component's full
|
|
11
|
+
// status on <prefix>/status/<component>;
|
|
12
|
+
// - changes arrive as NotifyStatus frames on <prefix>/events/rpc, holding
|
|
13
|
+
// only what changed, to be laid over the last full status; a key that has
|
|
14
|
+
// gone is sent as null;
|
|
15
|
+
// - <prefix>/online is true once connected, and false (the broker's last
|
|
16
|
+
// will) when it drops.
|
|
17
|
+
const mqtt_message_1 = require("./mqtt-message");
|
|
18
|
+
function shellyTopics(topicPrefix, component) {
|
|
19
|
+
return {
|
|
20
|
+
command: `${topicPrefix}/command/${component}`,
|
|
21
|
+
status: `${topicPrefix}/status/${component}`,
|
|
22
|
+
events: `${topicPrefix}/events/rpc`,
|
|
23
|
+
online: `${topicPrefix}/online`,
|
|
24
|
+
component
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function readShellyMessage(msg, topics, last, keepNull = []) {
|
|
28
|
+
if (msg.topic === topics.status) {
|
|
29
|
+
const status = (0, mqtt_message_1.parsedPayload)(msg.payload);
|
|
30
|
+
if (!(0, mqtt_message_1.isObject)(status))
|
|
31
|
+
throw new Error(`${topics.status} must carry the ${topics.component} status object`);
|
|
32
|
+
return { kind: "status", status: { ...status } };
|
|
33
|
+
}
|
|
34
|
+
if (msg.topic === topics.events) {
|
|
35
|
+
const received = (0, mqtt_message_1.parsedPayload)(msg.payload);
|
|
36
|
+
const full = (0, mqtt_message_1.isObject)(received) && received.method === "NotifyFullStatus";
|
|
37
|
+
const change = (0, mqtt_message_1.isObject)(received) && (received.method === "NotifyStatus" || full) && (0, mqtt_message_1.isObject)(received.params)
|
|
38
|
+
? received.params[topics.component] : undefined;
|
|
39
|
+
if (!(0, mqtt_message_1.isObject)(change))
|
|
40
|
+
return { kind: "other" };
|
|
41
|
+
const status = full ? {} : { ...last };
|
|
42
|
+
for (const [key, value] of Object.entries(change)) {
|
|
43
|
+
if (value === null && !keepNull.includes(key))
|
|
44
|
+
delete status[key];
|
|
45
|
+
else
|
|
46
|
+
status[key] = value;
|
|
47
|
+
}
|
|
48
|
+
return full ? { kind: "status", status } : { kind: "change", status, change };
|
|
49
|
+
}
|
|
50
|
+
if (msg.topic === topics.online) {
|
|
51
|
+
const online = (0, mqtt_message_1.parsedPayload)(msg.payload);
|
|
52
|
+
if (online !== true && online !== false)
|
|
53
|
+
throw new Error(`${topics.online} must be true or false`);
|
|
54
|
+
return { kind: "online", online };
|
|
55
|
+
}
|
|
56
|
+
return { kind: "other" };
|
|
57
|
+
}
|
|
58
|
+
//# sourceMappingURL=shelly-gen2.js.map
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import type { TraceEntry } from "./bounded-trace";
|
|
2
|
+
import { type MqttPublication } from "./mqtt-message";
|
|
3
|
+
export declare const SHELLY_SWITCH_MODES: readonly ["auto", "detached", "relay"];
|
|
4
|
+
export type ShellySwitchMode = typeof SHELLY_SWITCH_MODES[number];
|
|
5
|
+
export declare const CONFIG_TIMEOUT_MS = 10000;
|
|
6
|
+
export interface ShellySwitchConfig {
|
|
7
|
+
topicPrefix: string;
|
|
8
|
+
channel: number;
|
|
9
|
+
mode: ShellySwitchMode;
|
|
10
|
+
}
|
|
11
|
+
export interface DeviceSetup {
|
|
12
|
+
inMode: string;
|
|
13
|
+
inputType: string;
|
|
14
|
+
inputId: number;
|
|
15
|
+
}
|
|
16
|
+
export interface ShellySwitchState {
|
|
17
|
+
setup: DeviceSetup | null;
|
|
18
|
+
setupStatus: "reading" | "read" | "unreadable";
|
|
19
|
+
requestedAt: number | null;
|
|
20
|
+
requestId: number;
|
|
21
|
+
online: boolean;
|
|
22
|
+
relayOn: boolean | null;
|
|
23
|
+
inputState: boolean | null;
|
|
24
|
+
}
|
|
25
|
+
export interface ShellySwitchMessage {
|
|
26
|
+
topic?: unknown;
|
|
27
|
+
payload?: unknown;
|
|
28
|
+
shellySwitch?: {
|
|
29
|
+
kind?: unknown;
|
|
30
|
+
};
|
|
31
|
+
switchCommand?: {
|
|
32
|
+
power?: unknown;
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
export type SwitchReading = {
|
|
36
|
+
source: "switch";
|
|
37
|
+
payload: {
|
|
38
|
+
state: boolean;
|
|
39
|
+
};
|
|
40
|
+
baseline?: true;
|
|
41
|
+
observedAt: string;
|
|
42
|
+
} | {
|
|
43
|
+
source: "switch-action";
|
|
44
|
+
payload: {
|
|
45
|
+
action: "single" | "double" | "long";
|
|
46
|
+
};
|
|
47
|
+
observedAt: string;
|
|
48
|
+
} | {
|
|
49
|
+
source: "switch-availability";
|
|
50
|
+
payload: "online" | "offline";
|
|
51
|
+
} | {
|
|
52
|
+
switchState: {
|
|
53
|
+
kind: "observed";
|
|
54
|
+
on: boolean;
|
|
55
|
+
person: boolean;
|
|
56
|
+
observedAt: string;
|
|
57
|
+
};
|
|
58
|
+
} | {
|
|
59
|
+
switchState: {
|
|
60
|
+
kind: "availability";
|
|
61
|
+
online: boolean;
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
export declare function normaliseShellySwitchConfig(config: {
|
|
65
|
+
topicPrefix?: unknown;
|
|
66
|
+
channel?: unknown;
|
|
67
|
+
mode?: unknown;
|
|
68
|
+
}): ShellySwitchConfig;
|
|
69
|
+
export declare function shellySwitchTopics(config: ShellySwitchConfig): {
|
|
70
|
+
command: string;
|
|
71
|
+
status: string;
|
|
72
|
+
events: string;
|
|
73
|
+
online: string;
|
|
74
|
+
component: string;
|
|
75
|
+
rpc: string;
|
|
76
|
+
reply: string;
|
|
77
|
+
source: string;
|
|
78
|
+
deviceCommand: string;
|
|
79
|
+
deviceStatus: string;
|
|
80
|
+
};
|
|
81
|
+
export declare function effectiveMode(config: ShellySwitchConfig, state: Pick<ShellySwitchState, "setup">): "detached" | "relay" | null;
|
|
82
|
+
export declare function transitionShellySwitch(saved: Partial<ShellySwitchState> | undefined, msg: ShellySwitchMessage, config: ShellySwitchConfig, now: Date): {
|
|
83
|
+
state: ShellySwitchState;
|
|
84
|
+
publications: MqttPublication[];
|
|
85
|
+
readings: SwitchReading[];
|
|
86
|
+
traces: TraceEntry<Record<string, unknown>>[];
|
|
87
|
+
status: {
|
|
88
|
+
text: string;
|
|
89
|
+
fill: "green" | "yellow" | "red" | "grey";
|
|
90
|
+
};
|
|
91
|
+
};
|
|
92
|
+
export declare function shellySwitchStatus(config: ShellySwitchConfig, state: ShellySwitchState): {
|
|
93
|
+
text: string;
|
|
94
|
+
fill: "green" | "yellow" | "red" | "grey";
|
|
95
|
+
};
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CONFIG_TIMEOUT_MS = exports.SHELLY_SWITCH_MODES = void 0;
|
|
4
|
+
exports.normaliseShellySwitchConfig = normaliseShellySwitchConfig;
|
|
5
|
+
exports.shellySwitchTopics = shellySwitchTopics;
|
|
6
|
+
exports.effectiveMode = effectiveMode;
|
|
7
|
+
exports.transitionShellySwitch = transitionShellySwitch;
|
|
8
|
+
exports.shellySwitchStatus = shellySwitchStatus;
|
|
9
|
+
const mqtt_message_1 = require("./mqtt-message");
|
|
10
|
+
const shelly_gen2_1 = require("./shelly-gen2");
|
|
11
|
+
exports.SHELLY_SWITCH_MODES = ["auto", "detached", "relay"];
|
|
12
|
+
// How long to wait for the device's configuration before taking it as
|
|
13
|
+
// unreadable.
|
|
14
|
+
exports.CONFIG_TIMEOUT_MS = 10_000;
|
|
15
|
+
const ACTIONS = { single_push: "single", double_push: "double", long_push: "long" };
|
|
16
|
+
// Settings may be editor strings. The prefix names the user's own device,
|
|
17
|
+
// so it has no default.
|
|
18
|
+
function normaliseShellySwitchConfig(config) {
|
|
19
|
+
const topicPrefix = String(config.topicPrefix ?? "").trim().replace(/^\/+|\/+$/g, "");
|
|
20
|
+
if (!topicPrefix)
|
|
21
|
+
throw new Error("the device's MQTT topic prefix is required");
|
|
22
|
+
if (/[+#]/.test(topicPrefix))
|
|
23
|
+
throw new Error("the MQTT topic prefix can't contain + or #");
|
|
24
|
+
const channel = config.channel === undefined || config.channel === "" ? 0 : Number(config.channel);
|
|
25
|
+
if (!Number.isInteger(channel) || channel < 0)
|
|
26
|
+
throw new Error("the switch ID must be a whole number from 0");
|
|
27
|
+
const mode = config.mode === undefined || config.mode === "" ? "auto" : String(config.mode);
|
|
28
|
+
if (!exports.SHELLY_SWITCH_MODES.includes(mode))
|
|
29
|
+
throw new Error("mode must be auto, detached or relay");
|
|
30
|
+
return { topicPrefix, channel, mode: mode };
|
|
31
|
+
}
|
|
32
|
+
function shellySwitchTopics(config) {
|
|
33
|
+
const component = (0, shelly_gen2_1.shellyTopics)(config.topicPrefix, `switch:${config.channel}`);
|
|
34
|
+
// This node's RPC source: under the device's prefix, so the reply on
|
|
35
|
+
// <src>/rpc arrives through the same MQTT in node; one per channel.
|
|
36
|
+
const source = `${config.topicPrefix}/home-automation-switch-${config.channel}`;
|
|
37
|
+
return {
|
|
38
|
+
...component,
|
|
39
|
+
rpc: `${config.topicPrefix}/rpc`,
|
|
40
|
+
reply: `${source}/rpc`,
|
|
41
|
+
source,
|
|
42
|
+
deviceCommand: `${config.topicPrefix}/command`,
|
|
43
|
+
deviceStatus: `${config.topicPrefix}/status`
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
// The mode the node works in: the setting, or with auto, the device's.
|
|
47
|
+
function effectiveMode(config, state) {
|
|
48
|
+
if (config.mode !== "auto")
|
|
49
|
+
return config.mode;
|
|
50
|
+
if (!state.setup)
|
|
51
|
+
return null;
|
|
52
|
+
return state.setup.inMode === "detached" ? "detached" : "relay";
|
|
53
|
+
}
|
|
54
|
+
function transitionShellySwitch(saved, msg, config, now) {
|
|
55
|
+
const state = {
|
|
56
|
+
setup: (0, mqtt_message_1.isObject)(saved?.setup) ? { ...saved.setup } : null,
|
|
57
|
+
setupStatus: saved?.setupStatus === "read" || saved?.setupStatus === "unreadable" ? saved.setupStatus : "reading",
|
|
58
|
+
requestedAt: Number.isFinite(saved?.requestedAt) ? saved.requestedAt : null,
|
|
59
|
+
requestId: Number.isInteger(saved?.requestId) ? saved.requestId : 0,
|
|
60
|
+
online: saved?.online !== false,
|
|
61
|
+
relayOn: typeof saved?.relayOn === "boolean" ? saved.relayOn : null,
|
|
62
|
+
inputState: typeof saved?.inputState === "boolean" ? saved.inputState : null
|
|
63
|
+
};
|
|
64
|
+
const topics = shellySwitchTopics(config);
|
|
65
|
+
const nowMs = now.getTime();
|
|
66
|
+
const observedAt = now.toISOString();
|
|
67
|
+
const publications = [];
|
|
68
|
+
const readings = [];
|
|
69
|
+
const traces = [];
|
|
70
|
+
const trace = (event, outcome, reason = null, details = {}) => {
|
|
71
|
+
traces.push({ event, outcome, reason, details });
|
|
72
|
+
};
|
|
73
|
+
const mode = () => effectiveMode(config, state);
|
|
74
|
+
// Asks for the device's configuration and its whole status (the baseline).
|
|
75
|
+
function readDevice(reason) {
|
|
76
|
+
state.requestId += 1;
|
|
77
|
+
state.requestedAt = nowMs;
|
|
78
|
+
if (state.setupStatus !== "read")
|
|
79
|
+
state.setupStatus = "reading";
|
|
80
|
+
publications.push({ topic: topics.rpc, payload: JSON.stringify({ id: state.requestId, src: topics.source, method: "Shelly.GetConfig" }) });
|
|
81
|
+
publications.push({ topic: topics.deviceCommand, payload: "status_update" });
|
|
82
|
+
trace("device", "reading", reason);
|
|
83
|
+
}
|
|
84
|
+
// The input's on/off state, to the switch policy in detached mode.
|
|
85
|
+
function inputState(value, baseline) {
|
|
86
|
+
if (typeof value !== "boolean")
|
|
87
|
+
return;
|
|
88
|
+
state.inputState = value;
|
|
89
|
+
if (mode() === "detached" && state.setup?.inputType !== "button") {
|
|
90
|
+
readings.push({ source: "switch", payload: { state: value }, ...(baseline ? { baseline: true } : {}), observedAt });
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
// The relay's output: in relay mode the light's state; in detached mode a
|
|
94
|
+
// check that the bulbs still have power.
|
|
95
|
+
function relayOutput(status) {
|
|
96
|
+
if (typeof status.output !== "boolean")
|
|
97
|
+
return;
|
|
98
|
+
state.relayOn = status.output;
|
|
99
|
+
if (mode() === "relay") {
|
|
100
|
+
// The device names the source of the last change: its own input is a person at the switch.
|
|
101
|
+
const person = status.source === "button" || status.source === "switch" || status.source === "input";
|
|
102
|
+
readings.push({ switchState: { kind: "observed", on: status.output, person, observedAt } });
|
|
103
|
+
}
|
|
104
|
+
else if (mode() === "detached" && !status.output) {
|
|
105
|
+
trace("relay", "off", "bulbs-unpowered");
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
const inputKey = () => `input:${state.setup?.inputId ?? config.channel}`;
|
|
109
|
+
const command = msg.switchCommand;
|
|
110
|
+
const own = msg.shellySwitch;
|
|
111
|
+
if ((0, mqtt_message_1.isObject)(own) && own.kind === "startup") {
|
|
112
|
+
readDevice("startup");
|
|
113
|
+
}
|
|
114
|
+
else if ((0, mqtt_message_1.isObject)(own) && own.kind === "tick") {
|
|
115
|
+
if (state.setupStatus === "reading" && state.requestedAt !== null && nowMs - state.requestedAt >= exports.CONFIG_TIMEOUT_MS) {
|
|
116
|
+
state.setupStatus = state.setup ? "read" : "unreadable";
|
|
117
|
+
trace("device", state.setup ? "kept" : "unreadable", "no-reply");
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
else if ((0, mqtt_message_1.isObject)(command)) {
|
|
121
|
+
if (mode() !== "relay") {
|
|
122
|
+
// Detached, the relay powers the bulbs: never switched from here.
|
|
123
|
+
trace("command", "refused", mode() === "detached" ? "detached" : "mode-unknown", { power: String(command.power) });
|
|
124
|
+
}
|
|
125
|
+
else if (command.power !== "on" && command.power !== "off") {
|
|
126
|
+
trace("command", "ignored", "invalid-power", { power: String(command.power) });
|
|
127
|
+
}
|
|
128
|
+
else if (!state.online) {
|
|
129
|
+
trace("command", "refused", "offline", { power: command.power });
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
publications.push({ topic: topics.command, payload: command.power });
|
|
133
|
+
trace("command", "sent", null, { power: command.power });
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
else if (msg.topic === topics.reply) {
|
|
137
|
+
const frame = (0, mqtt_message_1.parsedPayload)(msg.payload);
|
|
138
|
+
if (!(0, mqtt_message_1.isObject)(frame) || frame.id !== state.requestId) {
|
|
139
|
+
trace("device", "ignored", "not-our-request");
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
const result = (0, mqtt_message_1.isObject)(frame.result) ? frame.result : null;
|
|
143
|
+
const switchConfig = result && (0, mqtt_message_1.isObject)(result[`switch:${config.channel}`]) ? result[`switch:${config.channel}`] : null;
|
|
144
|
+
const inputId = switchConfig && Number.isInteger(switchConfig.input_id) ? switchConfig.input_id : config.channel;
|
|
145
|
+
const inputConfig = result && (0, mqtt_message_1.isObject)(result[`input:${inputId}`]) ? result[`input:${inputId}`] : null;
|
|
146
|
+
if (switchConfig && typeof switchConfig.in_mode === "string" && inputConfig && typeof inputConfig.type === "string") {
|
|
147
|
+
state.setup = { inMode: switchConfig.in_mode, inputType: inputConfig.type, inputId };
|
|
148
|
+
state.setupStatus = "read";
|
|
149
|
+
trace("device", "read", null, { ...state.setup });
|
|
150
|
+
// The status may have come first: what it said, now the mode is known.
|
|
151
|
+
if (state.inputState !== null)
|
|
152
|
+
inputState(state.inputState, true);
|
|
153
|
+
if (state.relayOn !== null && mode() === "relay") {
|
|
154
|
+
readings.push({ switchState: { kind: "observed", on: state.relayOn, person: false, observedAt } });
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
state.setupStatus = state.setup ? "read" : "unreadable";
|
|
159
|
+
trace("device", "unreadable", (0, mqtt_message_1.isObject)(frame.error) ? "error-reply" : "no-switch-config");
|
|
160
|
+
}
|
|
161
|
+
state.requestedAt = null;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
else if (msg.topic === topics.deviceStatus) {
|
|
165
|
+
// The whole device status: the baseline for both the input and the relay.
|
|
166
|
+
const status = (0, mqtt_message_1.parsedPayload)(msg.payload);
|
|
167
|
+
if (!(0, mqtt_message_1.isObject)(status))
|
|
168
|
+
throw new Error(`${topics.deviceStatus} must carry the device's status object`);
|
|
169
|
+
const relay = status[topics.component];
|
|
170
|
+
const input = status[inputKey()];
|
|
171
|
+
if ((0, mqtt_message_1.isObject)(relay))
|
|
172
|
+
relayOutput(relay);
|
|
173
|
+
if ((0, mqtt_message_1.isObject)(input))
|
|
174
|
+
inputState(input.state, true);
|
|
175
|
+
}
|
|
176
|
+
else if (msg.topic === topics.events) {
|
|
177
|
+
const frame = (0, mqtt_message_1.parsedPayload)(msg.payload);
|
|
178
|
+
const params = (0, mqtt_message_1.isObject)(frame) && (0, mqtt_message_1.isObject)(frame.params) ? frame.params : null;
|
|
179
|
+
if ((0, mqtt_message_1.isObject)(frame) && frame.method === "NotifyEvent" && params && Array.isArray(params.events)) {
|
|
180
|
+
for (const event of params.events) {
|
|
181
|
+
if (!(0, mqtt_message_1.isObject)(event) || event.component !== inputKey())
|
|
182
|
+
continue;
|
|
183
|
+
const action = typeof event.event === "string" ? ACTIONS[event.event] : undefined;
|
|
184
|
+
if (action && mode() === "detached")
|
|
185
|
+
readings.push({ source: "switch-action", payload: { action }, observedAt });
|
|
186
|
+
trace("input", action ? "action" : "ignored", action ? null : "unmapped-event", { event: String(event.event) });
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
else if ((0, mqtt_message_1.isObject)(frame) && frame.method === "NotifyStatus" && params) {
|
|
190
|
+
const relay = params[topics.component];
|
|
191
|
+
const input = params[inputKey()];
|
|
192
|
+
if ((0, mqtt_message_1.isObject)(relay) && Object.hasOwn(relay, "output"))
|
|
193
|
+
relayOutput(relay);
|
|
194
|
+
if ((0, mqtt_message_1.isObject)(input) && Object.hasOwn(input, "state"))
|
|
195
|
+
inputState(input.state, false);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
else {
|
|
199
|
+
const message = (0, shelly_gen2_1.readShellyMessage)(msg, topics, null);
|
|
200
|
+
if (message.kind === "status") {
|
|
201
|
+
relayOutput(message.status);
|
|
202
|
+
}
|
|
203
|
+
else if (message.kind === "online") {
|
|
204
|
+
state.online = message.online;
|
|
205
|
+
trace("availability", message.online ? "online" : "offline");
|
|
206
|
+
if (mode() === "detached")
|
|
207
|
+
readings.push({ source: "switch-availability", payload: message.online ? "online" : "offline" });
|
|
208
|
+
if (mode() === "relay")
|
|
209
|
+
readings.push({ switchState: { kind: "availability", online: message.online } });
|
|
210
|
+
// Back online: read its configuration again, and a fresh baseline.
|
|
211
|
+
if (message.online)
|
|
212
|
+
readDevice("online");
|
|
213
|
+
}
|
|
214
|
+
else if (msg.topic !== topics.rpc && msg.topic !== topics.command && msg.topic !== topics.deviceCommand) {
|
|
215
|
+
trace("message", "ignored", "unrelated-message", { topic: typeof msg.topic === "string" ? msg.topic : null });
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
return { state, publications, readings, traces, status: shellySwitchStatus(config, state) };
|
|
219
|
+
}
|
|
220
|
+
// What the node's status shows: the mode found, and anything to act on.
|
|
221
|
+
function shellySwitchStatus(config, state) {
|
|
222
|
+
if (!state.online)
|
|
223
|
+
return { text: "offline", fill: "grey" };
|
|
224
|
+
const setup = state.setup ? `${state.setup.inMode}, ${state.setup.inputType} input` : null;
|
|
225
|
+
const mode = effectiveMode(config, state);
|
|
226
|
+
if (!mode) {
|
|
227
|
+
return state.setupStatus === "reading"
|
|
228
|
+
? { text: "reading the device's configuration", fill: "grey" }
|
|
229
|
+
: { text: "configuration unreadable: set Mode", fill: "red" };
|
|
230
|
+
}
|
|
231
|
+
const found = config.mode === "auto" ? setup : `${mode} (set)${setup ? `, device ${setup}` : ""}`;
|
|
232
|
+
if (mode === "detached" && state.relayOn === false)
|
|
233
|
+
return { text: `${found} · relay off: bulbs unpowered`, fill: "yellow" };
|
|
234
|
+
const now = mode === "relay" && state.relayOn !== null ? ` · ${state.relayOn ? "on" : "off"}` : "";
|
|
235
|
+
return { text: `${found ?? mode}${now}`, fill: "green" };
|
|
236
|
+
}
|
|
237
|
+
//# sourceMappingURL=shelly-switch.js.map
|
|
@@ -152,14 +152,22 @@ function theatreRetainedTopics(config) {
|
|
|
152
152
|
: [];
|
|
153
153
|
return [
|
|
154
154
|
...discovery,
|
|
155
|
-
|
|
155
|
+
(0, theatre_ha_discovery_1.theatreTopic)(config.baseTopic, "availability"),
|
|
156
156
|
...STATE_TOPICS.map(([name]) => (0, theatre_ha_discovery_1.theatreTopic)(config.baseTopic, name))
|
|
157
157
|
];
|
|
158
158
|
}
|
|
159
|
-
// What the contract publishes as
|
|
160
|
-
//
|
|
159
|
+
// What the contract publishes as the flows start and as it stops: with
|
|
160
|
+
// discovery on, its entries, as the other publishing nodes announce theirs,
|
|
161
|
+
// so Home Assistant has them without a startup message; and its
|
|
162
|
+
// availability, so Home Assistant shows its entities as available only while
|
|
163
|
+
// it runs. Removing entries when discovery is off stays with the startup
|
|
164
|
+
// message.
|
|
161
165
|
function theatreStartingPublications(config) {
|
|
162
|
-
|
|
166
|
+
const entries = config.discovery !== false ? (0, theatre_ha_discovery_1.theatreDiscovery)(config) : [];
|
|
167
|
+
return [
|
|
168
|
+
...entries.map(({ topic, payload }) => publication(topic, payload)),
|
|
169
|
+
publication((0, theatre_ha_discovery_1.theatreTopic)(config.baseTopic, "availability"), ha_discovery_1.ONLINE)
|
|
170
|
+
];
|
|
163
171
|
}
|
|
164
172
|
function theatreStoppingPublications(config) {
|
|
165
173
|
return [publication((0, theatre_ha_discovery_1.theatreTopic)(config.baseTopic, "availability"), ha_discovery_1.OFFLINE)];
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { TraceEntry } from "./bounded-trace";
|
|
2
|
+
import type { CoverMotion } from "./cover-controller";
|
|
3
|
+
import { type MqttPublication } from "./mqtt-message";
|
|
4
|
+
export interface Z2mCoverConfig {
|
|
5
|
+
baseTopic: string;
|
|
6
|
+
friendlyName: string;
|
|
7
|
+
}
|
|
8
|
+
export interface Z2mCoverState {
|
|
9
|
+
reportsPosition: boolean;
|
|
10
|
+
}
|
|
11
|
+
export interface Z2mCoverMessage {
|
|
12
|
+
topic?: unknown;
|
|
13
|
+
payload?: unknown;
|
|
14
|
+
cover_command?: {
|
|
15
|
+
operation?: unknown;
|
|
16
|
+
position?: unknown;
|
|
17
|
+
generation?: unknown;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
export type Z2mCoverReading = {
|
|
21
|
+
kind: "observed";
|
|
22
|
+
position?: number;
|
|
23
|
+
state?: "open" | "closed";
|
|
24
|
+
motion?: CoverMotion;
|
|
25
|
+
observedAt: string;
|
|
26
|
+
battery?: number;
|
|
27
|
+
charging?: string;
|
|
28
|
+
} | {
|
|
29
|
+
kind: "availability";
|
|
30
|
+
online: boolean;
|
|
31
|
+
};
|
|
32
|
+
export declare function normaliseZ2mCoverConfig(config: {
|
|
33
|
+
baseTopic?: unknown;
|
|
34
|
+
friendlyName?: unknown;
|
|
35
|
+
}): Z2mCoverConfig;
|
|
36
|
+
export declare function z2mCoverTopics(config: Z2mCoverConfig): {
|
|
37
|
+
device: string;
|
|
38
|
+
set: string;
|
|
39
|
+
get: string;
|
|
40
|
+
availability: string;
|
|
41
|
+
};
|
|
42
|
+
export declare function transitionZ2mCover(saved: Partial<Z2mCoverState> | undefined, msg: Z2mCoverMessage, config: Z2mCoverConfig, now: Date): {
|
|
43
|
+
state: Z2mCoverState;
|
|
44
|
+
publications: MqttPublication[];
|
|
45
|
+
readings: {
|
|
46
|
+
cover: Z2mCoverReading;
|
|
47
|
+
}[];
|
|
48
|
+
traces: TraceEntry<Record<string, unknown>>[];
|
|
49
|
+
};
|