@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.
Files changed (91) hide show
  1. package/README.md +413 -77
  2. package/contract-examples/room-lighting.mjs +39 -27
  3. package/dist/lib/activity-lighting-controller.d.ts +7 -2
  4. package/dist/lib/activity-lighting-controller.js +75 -16
  5. package/dist/lib/avr-controller.d.ts +2 -2
  6. package/dist/lib/avr-controller.js +21 -26
  7. package/dist/lib/circadian-policy.d.ts +47 -0
  8. package/dist/lib/circadian-policy.js +111 -0
  9. package/dist/lib/circadian-publishing.d.ts +33 -0
  10. package/dist/lib/circadian-publishing.js +154 -0
  11. package/dist/lib/circadian-schedule.d.ts +4 -0
  12. package/dist/lib/circadian-schedule.js +12 -0
  13. package/dist/lib/command-echo.d.ts +8 -1
  14. package/dist/lib/command-echo.js +37 -6
  15. package/dist/lib/cover-controller.js +4 -0
  16. package/dist/lib/denon-protocol.js +3 -2
  17. package/dist/lib/enable-input.d.ts +3 -0
  18. package/dist/lib/enable-input.js +18 -0
  19. package/dist/lib/house-schedule.js +2 -2
  20. package/dist/lib/light-contract.d.ts +53 -0
  21. package/dist/lib/light-contract.js +11 -0
  22. package/dist/lib/motion-policy.d.ts +0 -1
  23. package/dist/lib/motion-policy.js +9 -8
  24. package/dist/lib/motion-publishing.js +2 -1
  25. package/dist/lib/mqtt-message.d.ts +6 -0
  26. package/dist/lib/mqtt-message.js +21 -0
  27. package/dist/lib/room-controller.d.ts +41 -27
  28. package/dist/lib/room-controller.js +422 -213
  29. package/dist/lib/room-holds.d.ts +34 -0
  30. package/dist/lib/room-holds.js +66 -0
  31. package/dist/lib/room-lighting-discovery.d.ts +0 -14
  32. package/dist/lib/room-lighting-discovery.js +19 -125
  33. package/dist/lib/room-publishing.d.ts +7 -12
  34. package/dist/lib/room-publishing.js +65 -125
  35. package/dist/lib/shelly-cover.d.ts +2 -4
  36. package/dist/lib/shelly-cover.js +30 -70
  37. package/dist/lib/shelly-gen2.d.ts +25 -0
  38. package/dist/lib/shelly-gen2.js +58 -0
  39. package/dist/lib/shelly-switch.d.ts +95 -0
  40. package/dist/lib/shelly-switch.js +237 -0
  41. package/dist/lib/theatre-state-contract.js +12 -4
  42. package/dist/lib/z2m-cover.d.ts +49 -0
  43. package/dist/lib/z2m-cover.js +120 -0
  44. package/dist/lib/z2m-light-adapter.d.ts +62 -0
  45. package/dist/lib/z2m-light-adapter.js +195 -0
  46. package/dist/lib/z2m-light.d.ts +2 -2
  47. package/dist/lib/z2m-light.js +11 -14
  48. package/dist/nodes/activity-lighting-controller.html +15 -6
  49. package/dist/nodes/activity-lighting-controller.js +21 -0
  50. package/dist/nodes/adapter.d.ts +5 -1
  51. package/dist/nodes/adapter.js +35 -2
  52. package/dist/nodes/avr-controller.html +20 -18
  53. package/dist/nodes/avr-controller.js +5 -5
  54. package/dist/nodes/circadian-policy.d.ts +3 -0
  55. package/dist/nodes/circadian-policy.html +145 -0
  56. package/dist/nodes/circadian-policy.js +162 -0
  57. package/dist/nodes/cover-controller.html +9 -3
  58. package/dist/nodes/cover-controller.js +1 -1
  59. package/dist/nodes/display-mode-controller.html +14 -17
  60. package/dist/nodes/display-mode-controller.js +4 -1
  61. package/dist/nodes/house-schedule-mqtt.html +5 -0
  62. package/dist/nodes/house-schedule-mqtt.js +1 -1
  63. package/dist/nodes/house-schedule.html +3 -3
  64. package/dist/nodes/motion-policy.html +10 -2
  65. package/dist/nodes/motion-policy.js +4 -1
  66. package/dist/nodes/occupancy-fusion.html +5 -0
  67. package/dist/nodes/occupancy-fusion.js +1 -1
  68. package/dist/nodes/refused-enable.d.ts +7 -0
  69. package/dist/nodes/refused-enable.js +24 -0
  70. package/dist/nodes/room-controller.html +232 -85
  71. package/dist/nodes/room-controller.js +71 -97
  72. package/dist/nodes/shelly-switch.d.ts +3 -0
  73. package/dist/nodes/shelly-switch.html +87 -0
  74. package/dist/nodes/shelly-switch.js +48 -0
  75. package/dist/nodes/theatre-state-contract.html +9 -3
  76. package/dist/nodes/theatre-state-contract.js +1 -1
  77. package/dist/nodes/z2m-cover.d.ts +3 -0
  78. package/dist/nodes/z2m-cover.html +68 -0
  79. package/dist/nodes/z2m-cover.js +41 -0
  80. package/dist/nodes/z2m-light.d.ts +3 -0
  81. package/dist/nodes/z2m-light.html +76 -0
  82. package/dist/nodes/z2m-light.js +51 -0
  83. package/examples/Cover control.json +28 -71
  84. package/examples/Home theatre activity.json +53 -19
  85. package/examples/House schedule.json +106 -24
  86. package/examples/Room lighting.json +57 -25
  87. package/examples/Shelly cover.json +5 -16
  88. package/examples/Shelly switch.json +197 -0
  89. package/package.json +12 -4
  90. package/dist/lib/room-schedule.d.ts +0 -8
  91. package/dist/lib/room-schedule.js +0 -41
@@ -1,27 +1,26 @@
1
1
  "use strict";
2
2
  const room_controller_1 = require("../lib/room-controller");
3
- const circadian_1 = require("../lib/circadian");
4
- const circadian_schedule_1 = require("../lib/circadian-schedule");
3
+ const light_contract_1 = require("../lib/light-contract");
5
4
  const publishing_lifecycle_1 = require("./publishing-lifecycle");
6
5
  const room_publishing_1 = require("../lib/room-publishing");
7
6
  const adapter_1 = require("./adapter");
8
- const shared_schedule_1 = require("./shared-schedule");
9
7
  const settings_1 = require("../lib/settings");
10
8
  const ha_discovery_1 = require("../lib/ha-discovery");
11
9
  const topic_names_1 = require("../lib/topic-names");
10
+ // The room's lights: an id each, and onOffOnly for one that only switches.
11
+ // Their devices are reached through their adapters.
12
12
  function parseTargets(value) {
13
13
  const targets = (0, settings_1.jsonSetting)(value, "targets", []);
14
- if (!Array.isArray(targets) || targets.some((target) => {
15
- if (!target || typeof target !== "object")
16
- return true;
17
- const candidate = target;
18
- return typeof candidate.id !== "string" ||
19
- typeof candidate.stateTopic !== "string" ||
20
- typeof candidate.commandTopic !== "string";
21
- })) {
22
- throw new Error("targets must define id, stateTopic and commandTopic strings");
14
+ if (!Array.isArray(targets) || targets.some((target) => !target || typeof target !== "object" ||
15
+ typeof target.id !== "string" || target.id === "")) {
16
+ throw new Error("targets must each have an id");
23
17
  }
24
- const typedTargets = targets;
18
+ if (targets.some((target) => target.onOffOnly !== undefined &&
19
+ typeof target.onOffOnly !== "boolean")) {
20
+ throw new Error("a target's onOffOnly must be true or false");
21
+ }
22
+ const typedTargets = targets
23
+ .map(({ id, onOffOnly }) => ({ id: id, ...(onOffOnly === true ? { onOffOnly: true } : {}) }));
25
24
  if (new Set(typedTargets.map((target) => target.id)).size !== typedTargets.length) {
26
25
  throw new Error("target ids must be unique");
27
26
  }
@@ -32,13 +31,23 @@ const registerRoomController = (RED) => {
32
31
  RED.nodes.createNode(this, config);
33
32
  const node = this;
34
33
  const area = (0, adapter_1.optionalArea)(config);
35
- let refreshTimer;
36
34
  let controllerConfig;
37
- let refreshSeconds;
38
- // A schedule that is set but not running, as when it was deleted: the
39
- // room keeps to its own times and says so.
40
- const { schedule: house, missing: houseMissing } = (0, shared_schedule_1.referencedSchedule)(RED, config.houseSchedule);
41
35
  try {
36
+ // A room saved before 6.0 still has its circadian settings, which moved
37
+ // to the circadian lighting policy: it runs only once migrated, so it
38
+ // can't quietly lose its styling.
39
+ // Its lights' topics moved to their light adapters likewise: one
40
+ // migration covers both.
41
+ const moved = (0, room_controller_1.movedCircadianSettings)(config);
42
+ const lights = (0, room_controller_1.targetsWithTopics)(config.targets);
43
+ if (moved.length || lights.length) {
44
+ throw new Error("this room was saved before 6.0: " + [
45
+ ...(moved.length ? ["circadian lighting moved to its own node (add a circadian lighting policy wired to this room, " +
46
+ "with its " + moved.join(", ") + ")"] : []),
47
+ ...(lights.length ? ["its lights are reached through light adapters (add one for " + lights.join(", ") +
48
+ ": a zigbee2mqtt light, or your own on the neutral light contract, and remove their topics from Targets JSON)"] : [])
49
+ ].join(", and ") + "; then open and save this room");
50
+ }
42
51
  // Not set (null, or absent from a flow saved before these settings)
43
52
  // means on, so an older flow keeps publishing as before.
44
53
  const publishState = (0, settings_1.optionalBooleanSetting)(config.publishState, "publish state") ?? true;
@@ -47,41 +56,31 @@ const registerRoomController = (RED) => {
47
56
  // area is only a label.
48
57
  if (publishState || discovery)
49
58
  (0, topic_names_1.publishingArea)(area, true);
50
- refreshSeconds = Math.max(10, (0, settings_1.numberSetting)(config.refreshSeconds, "refresh interval", 60));
51
- // Saved latitude, longitude and minimum settings from earlier versions
52
- // are ignored.
53
- const levels = {
54
- maxBrightnessPct: (0, settings_1.numberSetting)(config.maxBrightnessPct, "daytime brightness", 100),
55
- maxColorTempKelvin: (0, settings_1.numberSetting)(config.maxColorTempKelvin, "daytime colour temperature", 6500),
56
- sleepBrightnessPct: (0, settings_1.numberSetting)(config.sleepBrightnessPct, "sleep brightness", 1),
57
- sleepColorTempKelvin: (0, settings_1.numberSetting)(config.sleepColorTempKelvin, "sleep colour temperature", 2200)
58
- };
59
- const schedule = (0, circadian_schedule_1.resolveSchedule)((0, settings_1.textSetting)(config.wakeTime, "wake time", "07:00"), (0, settings_1.textSetting)(config.bedTime, "bed time", "23:00"));
60
59
  controllerConfig = {
61
60
  nodeId: node.id,
62
61
  // Not set: a node saved before node IDs, which keeps its area-named
63
62
  // entities. The editor gives new nodes "node".
64
63
  idScheme: (0, settings_1.optionalChoiceSetting)(config.idScheme, "ID scheme", ha_discovery_1.ID_SCHEMES) ?? "area",
65
- timesFrom: house ? "house" : "room",
66
64
  area,
67
65
  displayName: config.displayName || area,
68
66
  discoveryPrefix: (0, ha_discovery_1.discoveryPrefixSetting)(config.discoveryPrefix),
69
67
  publishState,
70
68
  discovery,
71
69
  targets: parseTargets(config.targets),
72
- initialTransitionSeconds: (0, settings_1.numberSetting)(config.initialTransitionSeconds, "initial transition", 1),
73
- refreshTransitionSeconds: (0, settings_1.numberSetting)(config.refreshTransitionSeconds, "refresh transition", 60),
74
- initialSleep: (0, settings_1.booleanSetting)(config.initialSleep, "initial sleep", false),
75
- // The adapter picks the circadian lighting; the controller only asks
76
- // it how a light should look.
77
- lighting: (0, circadian_1.scheduledLighting)(schedule, levels),
78
- schedule
70
+ initialTransitionSeconds: (0, settings_1.numberSetting)(config.initialTransitionSeconds, "initial transition", 1)
79
71
  };
80
72
  }
81
73
  catch (error) {
82
74
  (0, adapter_1.invalidConfig)(node, error);
83
75
  return;
84
76
  }
77
+ // A room saved before 6.0 recorded the circadian and sleep state topics
78
+ // as its own. The circadian lighting policy publishes them now, so they
79
+ // leave the record: neither this room's start nor its deletion clears
80
+ // them.
81
+ const recorded = node.context().get("retainedTopics");
82
+ if (Array.isArray(recorded))
83
+ node.context().set("retainedTopics", (0, room_publishing_1.withoutCircadianState)(recorded));
85
84
  // Available, and clear of what it no longer publishes, as soon as it
86
85
  // runs; offline as it stops; nothing left behind when deleted.
87
86
  const lifecycle = (0, publishing_lifecycle_1.publishingLifecycle)(RED, node, {
@@ -89,89 +88,64 @@ const registerRoomController = (RED) => {
89
88
  starting: () => (0, room_publishing_1.roomStartingOutputs)(controllerConfig),
90
89
  stopping: () => (0, room_publishing_1.roomStoppingOutputs)(controllerConfig),
91
90
  output: 0,
92
- outputs: 1
91
+ outputs: 3
93
92
  });
94
93
  function updateStatus(status) {
95
94
  node.status({
96
- fill: status.manualCount || houseMissing ? "yellow" : status.circadianEnabled ? "green" : "grey",
95
+ fill: status.manualCount ? "yellow" : status.styled ? "green" : "grey",
97
96
  shape: "dot",
98
- text: (houseMissing ? "schedule missing: own times · " : "") + (status.circadianEnabled
99
- ? (status.sleeping ? "sleep" : "circadian")
100
- : "manual only") + " · " + status.onCount + " on · " + status.manualCount + " manual" +
101
- (status.overriddenCount ? " · " + status.overriddenCount + " overridden" : "")
97
+ text: (status.styled ? "styled" : "unstyled") + " · " + status.onCount + " on · " + status.manualCount + " manual" +
98
+ (status.overriddenCount ? " · " + status.overriddenCount + " overridden" : "") +
99
+ (status.offlineCount ? " · " + status.offlineCount + " offline" : "")
102
100
  });
103
101
  }
102
+ // Each once per deploy, so a flow still sending them is told without
103
+ // filling the log.
104
+ const warnings = [
105
+ [room_controller_1.REMOVED_LIGHT_UNITS, "lighting values in brightness (1-254) or color_temp (mireds) were removed in 6.0 and are left out: " +
106
+ "send brightnessPct (0-100) and colorTempKelvin instead"],
107
+ [room_controller_1.INVALID_LIGHT_VALUES, "lighting values left out: brightnessPct must be a number and colorTempKelvin a number above 0"],
108
+ [room_controller_1.ON_OFF_ONLY, "lighting values sent to a light marked onOffOnly are left out: it only switches on and off"],
109
+ [room_controller_1.MOVED_TO_CIRCADIAN, "circadian, sleep and schedule messages go to the circadian lighting policy since 6.0: this room ignores them"]
110
+ ];
111
+ const warned = new Set();
104
112
  function process(msg, send) {
105
113
  const result = (0, adapter_1.runReducer)(node, { area: controllerConfig.area, component: "room-controller" }, (saved, now) => (0, room_publishing_1.publishRoom)(saved, msg, controllerConfig, now));
106
- if (result.outputs.length) {
107
- send([result.outputs.map((output) => ({ ...output, payload: output.payload }))]);
108
- }
109
- // Once per deploy, so a flow still sending Zigbee2MQTT units is told
110
- // without filling the log.
111
- if (!warnedLegacyUnits && result.traces.some(({ reason }) => reason === room_controller_1.LEGACY_LIGHT_UNITS)) {
112
- warnedLegacyUnits = true;
113
- node.warn("lighting values in brightness (1-254) or color_temp (mireds) are deprecated and will stop working in 6.0: " +
114
- "send brightnessPct (0-100) and colorTempKelvin instead");
115
- }
116
- if (!warnedInvalidValues && result.traces.some(({ reason }) => reason === room_controller_1.INVALID_LIGHT_VALUES)) {
117
- warnedInvalidValues = true;
118
- node.warn("lighting values left out: brightnessPct must be a number and colorTempKelvin a number above 0");
114
+ // Output 1: MQTT publications (control states, availability,
115
+ // discovery). Output 2: light commands, for the lights' adapters.
116
+ // Output 3: each light's reading passed on with its control state, for
117
+ // activity lighting.
118
+ const reading = (0, light_contract_1.isLightReading)(msg.light) && msg.light.kind === "observed" &&
119
+ controllerConfig.targets.some(({ id }) => id === msg.light.target) ? msg.light : null;
120
+ const control = reading ? result.status.control[reading.target] : undefined;
121
+ const outputs = [
122
+ result.outputs.length ? result.outputs.map((output) => ({ ...output, payload: output.payload })) : null,
123
+ result.commands.length ? result.commands.map((light) => ({ payload: undefined, light })) : null,
124
+ reading ? { payload: undefined, light: { ...reading, control, manual: control === "manual" } } : null
125
+ ];
126
+ if (outputs.some((output) => output !== null))
127
+ send(outputs);
128
+ for (const [reason, text] of warnings) {
129
+ if (!warned.has(reason) && result.traces.some((entry) => entry.reason === reason)) {
130
+ warned.add(reason);
131
+ node.warn(text);
132
+ }
119
133
  }
120
134
  updateStatus(result.status);
121
135
  return result;
122
136
  }
123
- let warnedLegacyUnits = false;
124
- let warnedInvalidValues = false;
125
137
  (0, adapter_1.onInput)(node, (msg, send) => {
126
138
  // The startup's own announcement comes from publishRoom.
127
139
  if ((0, adapter_1.objectField)(msg, "lighting")?.kind === "startup")
128
140
  lifecycle.startup(send);
129
141
  process({
130
142
  lighting: (0, adapter_1.objectField)(msg, "lighting"),
143
+ light: msg.light,
131
144
  topic: typeof msg.topic === "string" ? msg.topic : undefined,
132
145
  payload: msg.payload,
133
146
  retain: msg.retain === true
134
147
  }, send);
135
- });
136
- // Times taken from a house are the house's: a room that no longer follows
137
- // one, as when its schedule was set to none or deleted, goes back to its
138
- // own times rather than keeping the house's last ones.
139
- const roomTrace = { area: controllerConfig.area, component: "room-controller" };
140
- if (house) {
141
- node.context().set("followsHouse", true);
142
- }
143
- else if (node.context().get("followsHouse") === true) {
144
- node.context().set("followsHouse", false);
145
- (0, adapter_1.runReducer)(node, roomTrace, (saved) => ({
146
- state: { ...saved, schedule: undefined, scheduleSetting: undefined },
147
- traces: [{ event: "house-schedule", outcome: "changed", reason: "own-times", details: {} }]
148
- }));
149
- }
150
- if (house) {
151
- // The house's times: once the flows have started, as this room may
152
- // have restarted alone, and at every change until it stops.
153
- const follow = (0, adapter_1.guarded)(node, () => {
154
- process({ lighting: { kind: "schedule", ...house.schedule() } }, node.send.bind(node));
155
- });
156
- (0, adapter_1.whenFlowsStarted)(RED, node, follow);
157
- node.on("close", house.subscribe(follow));
158
- }
159
- else if (houseMissing) {
160
- (0, adapter_1.runReducer)(node, roomTrace, (state) => ({
161
- state,
162
- traces: [{ event: "house-schedule", outcome: "ignored", reason: "schedule-missing", details: { schedule: config.houseSchedule } }]
163
- }));
164
- node.status({ fill: "yellow", shape: "ring", text: "schedule missing: own times" });
165
- }
166
- refreshTimer = setInterval((0, adapter_1.guarded)(node, () => {
167
- process({ lighting: { kind: "refresh" } }, node.send.bind(node));
168
- }), refreshSeconds * 1000);
169
- // Available again as soon as this node runs, as after a redeploy of it
170
- // alone, rather than waiting for the flow's startup message.
171
- node.on("close", (_removed, done) => {
172
- clearInterval(refreshTimer);
173
- done();
174
- });
148
+ }, { message: { lighting: { kind: "startup" } }, trace: { area: controllerConfig.area, component: "room-controller" } });
175
149
  }
176
150
  RED.nodes.registerType("home-room-lighting-controller", HomeRoomLightingController);
177
151
  };
@@ -0,0 +1,3 @@
1
+ import type { NodeInitializer } from "node-red";
2
+ declare const registerShellySwitch: NodeInitializer;
3
+ export = registerShellySwitch;
@@ -0,0 +1,87 @@
1
+ <script type="text/javascript">
2
+ RED.nodes.registerType("home-shelly-switch", {
3
+ category: "home automation",
4
+ color: "#e2d96e",
5
+ defaults: {
6
+ name: {value: ""},
7
+ // No default: it names the user's own device.
8
+ topicPrefix: {value: "", required: true, validate: function (v) {
9
+ var prefix = String(v || "").trim().replace(/^\/+|\/+$/g, "");
10
+ return prefix !== "" && !/[+#]/.test(prefix);
11
+ }},
12
+ channel: {value: 0, required: true, validate: function (v) {
13
+ return /^\d+$/.test(String(v).trim());
14
+ }},
15
+ // auto: as the device is configured.
16
+ mode: {value: "auto"}
17
+ },
18
+ inputs: 1,
19
+ outputs: 2,
20
+ outputLabels: ["to the device (MQTT out)", "to the switch policy (detached), or the relay's state"],
21
+ icon: "font-awesome/fa-toggle-on",
22
+ label: function () { return this.name || "shelly switch"; },
23
+ paletteLabel: "shelly switch"
24
+ });
25
+ </script>
26
+
27
+ <script type="text/html" data-template-name="home-shelly-switch">
28
+ <div class="form-row"><label for="node-input-name">Name</label><input type="text" id="node-input-name"></div>
29
+ <div class="form-row"><label for="node-input-topicPrefix">MQTT topic prefix</label><input type="text" id="node-input-topicPrefix" placeholder="the device's ID, e.g. shellyplus1-a8032ab12345"></div>
30
+ <div class="form-row"><label for="node-input-channel">Switch ID</label><input type="number" min="0" step="1" id="node-input-channel"></div>
31
+ <div class="form-row">
32
+ <label for="node-input-mode">Mode</label>
33
+ <select id="node-input-mode">
34
+ <option value="auto">As the device is set up</option>
35
+ <option value="detached">Detached: the switch's presses</option>
36
+ <option value="relay">Relay: the relay is the light</option>
37
+ </select>
38
+ </div>
39
+ </script>
40
+
41
+ <script type="text/html" data-help-name="home-shelly-switch">
42
+ <p>Connects one switch channel of a Shelly Gen2 or later relay, over the device's own MQTT API, used the way the
43
+ device is set up: no function nodes, no Home Assistant and no HTTP.</p>
44
+ <h3>Wiring</h3>
45
+ <ol>
46
+ <li>An <b>MQTT in</b> node subscribed to <code>&lt;prefix&gt;/#</code> (output: auto-detect) → this node.</li>
47
+ <li>This node's first output → an <b>MQTT out</b> node on the same broker, with no topic set.</li>
48
+ <li>Detached: this node's second output → a <b>home switch lighting policy</b>, whose intents go to the room
49
+ controller.</li>
50
+ </ol>
51
+ <p>On the device, MQTT must be on with <b>Enable MQTT Control</b> and <b>RPC status notifications</b> (both the
52
+ default).</p>
53
+ <h3>Settings</h3>
54
+ <dl class="message-properties">
55
+ <dt>MQTT topic prefix</dt><dd>the device's topic prefix: its device ID (such as
56
+ <code>shellyplus1-a8032ab12345</code>) unless a custom one is set on the device.</dd>
57
+ <dt>Switch ID</dt><dd><code>0</code>, or the number of another switch on a device with more than one. Use one
58
+ node per switch.</dd>
59
+ <dt>Mode</dt><dd><i>As the device is set up</i> (the default) reads the device's own configuration; the other two
60
+ override it, and are needed when it can't be read.</dd>
61
+ </dl>
62
+ <h3>What it does</h3>
63
+ <ul>
64
+ <li>At startup, and whenever the device comes online, it asks the device for its configuration
65
+ (<code>Shelly.GetConfig</code>, with the reply addressed under the device's prefix, so the same MQTT in node
66
+ receives it) and for its whole status. The node status shows what it found, such as <i>detached, switch
67
+ input</i>. If nothing answers within 10 seconds, the status says <i>configuration unreadable: set Mode</i>, and
68
+ nothing is passed on until <b>Mode</b> says how.</li>
69
+ <li><b>Detached</b>: the relay stays on, powering smart bulbs, and is never switched from here. A switch-type input's
70
+ on/off becomes <code>msg.source = "switch"</code> with <code>msg.payload = {state}</code> (the first, from the
71
+ device's status, marked <code>baseline</code>); a button-type input's pushes become
72
+ <code>msg.source = "switch-action"</code> with <code>single</code>, <code>double</code> or <code>long</code>
73
+ (the switch policy acts on single and double; long is passed on for later use). The device going offline or online becomes
74
+ <code>msg.source = "switch-availability"</code>. If the relay reports off, the status warns: the bulbs have lost
75
+ power.</li>
76
+ <li><b>Relay</b> (follow, flip, momentary and the other modes): the relay is the light. Its state becomes
77
+ <code>msg.switchState = {kind: "observed", on, person, observedAt}</code>, with <code>person</code> true when
78
+ the change came from the device's own input; <code>msg.switchCommand = {power: "on" | "off"}</code> switches it.
79
+ No node of this package reads that state yet.</li>
80
+ <li>Readings are timed when they arrive.</li>
81
+ </ul>
82
+ <h3>When MQTT out connects</h3>
83
+ <p>Wire a <b>Status node</b> whose scope is your MQTT out node into this node. Each time MQTT out shows
84
+ "connected", this node takes it as its startup message (<code>msg.shellySwitch = {kind: "startup"}</code>) and reads the device's configuration again:
85
+ Node-RED's MQTT broker node drops what it is given before it connects, so what the node sends as the flows
86
+ start is lost on a first start, a full deploy or a restart. Any other status message is ignored.</p>
87
+ </script>
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ const shelly_switch_1 = require("../lib/shelly-switch");
3
+ const adapter_1 = require("./adapter");
4
+ const registerShellySwitch = (RED) => {
5
+ function HomeShellySwitch(config) {
6
+ RED.nodes.createNode(this, config);
7
+ const node = this;
8
+ let device;
9
+ try {
10
+ device = (0, shelly_switch_1.normaliseShellySwitchConfig)(config);
11
+ }
12
+ catch (error) {
13
+ (0, adapter_1.invalidConfig)(node, error);
14
+ return;
15
+ }
16
+ const trace = { area: "", component: "shelly-switch" };
17
+ let timer;
18
+ function process(msg, send) {
19
+ const result = (0, adapter_1.runReducer)(node, trace, (saved, now) => (0, shelly_switch_1.transitionShellySwitch)(saved, msg, device, now));
20
+ // MQTT publications for the device on output 1, for an MQTT out node;
21
+ // readings on output 2: for the switch lighting policy when detached,
22
+ // the relay's state otherwise.
23
+ const outputs = [
24
+ result.publications.length ? result.publications.map((publication) => ({ ...publication })) : null,
25
+ result.readings.length ? result.readings.map((reading) => ({ payload: undefined, ...reading })) : null
26
+ ];
27
+ if (outputs.some((output) => output !== null))
28
+ send(outputs);
29
+ node.status({ fill: result.status.fill, shape: result.status.fill === "green" ? "dot" : "ring", text: result.status.text });
30
+ // A configuration request not answered in time is taken as unreadable.
31
+ if (result.state.requestedAt !== null && result.state.setupStatus === "reading") {
32
+ clearTimeout(timer);
33
+ timer = setTimeout((0, adapter_1.guarded)(node, () => process({ shellySwitch: { kind: "tick" } }, node.send.bind(node))), Math.max(0, result.state.requestedAt + shelly_switch_1.CONFIG_TIMEOUT_MS - Date.now()));
34
+ }
35
+ }
36
+ node.status({ fill: "grey", shape: "ring", text: "reading the device's configuration" });
37
+ // Once the flows have started, so the MQTT out node is there to send through.
38
+ (0, adapter_1.whenFlowsStarted)(RED, node, () => process({ shellySwitch: { kind: "startup" } }, node.send.bind(node)));
39
+ (0, adapter_1.onInput)(node, (msg, send) => process(msg, send), { message: { shellySwitch: { kind: "startup" } }, trace: trace });
40
+ node.on("close", (_removed, done) => {
41
+ clearTimeout(timer);
42
+ done();
43
+ });
44
+ }
45
+ RED.nodes.registerType("home-shelly-switch", HomeShellySwitch);
46
+ };
47
+ module.exports = registerShellySwitch;
48
+ //# sourceMappingURL=shelly-switch.js.map
@@ -88,10 +88,16 @@
88
88
  <code>offline</code> as it stops; its Home Assistant entities follow it. These get through only
89
89
  when this node is deployed on its own, so set the MQTT broker node's Birth, Close and Will messages to the same (see the
90
90
  README).</p>
91
- <p>With <b>Home Assistant discovery</b> on, a startup event also publishes discovery for the activity selector and
92
- status sensors; switched off, it clears them, so Home Assistant removes them. New nodes start with it off; a node
93
- saved before the setting existed keeps it on.</p>
91
+ <p>With <b>Home Assistant discovery</b> on, it publishes discovery for the activity selector and status sensors once
92
+ the flows start, before <code>online</code>, and again at a startup event. Switched off, the next startup event
93
+ clears them, so Home Assistant removes them. New nodes start with it off; a node saved before the setting existed
94
+ keeps it on.</p>
94
95
  <p>The <b>Area</b> names the discovery entities and the default base topic <code>home/&lt;area&gt;/theatre</code>,
95
96
  so it is needed when discovery is on or the base topic is empty. With discovery off and a base topic set, it is
96
97
  optional.</p>
98
+ <h3>When MQTT out connects</h3>
99
+ <p>Wire a <b>Status node</b> whose scope is your MQTT out node into this node. Each time MQTT out shows
100
+ "connected", this node takes it as its startup message (<code>msg.theatreStateContract = {kind: "startup"}</code>) and announces itself again:
101
+ Node-RED's MQTT broker node drops what it is given before it connects, so what the node sends as the flows
102
+ start is lost on a first start, a full deploy or a restart. Any other status message is ignored.</p>
97
103
  </script>
@@ -80,7 +80,7 @@ const registerTheatreStateContract = (RED) => {
80
80
  theatre: (0, adapter_1.objectField)(msg, "theatre"),
81
81
  theatreStateContract: (0, adapter_1.objectField)(msg, "theatreStateContract")
82
82
  }, send);
83
- });
83
+ }, { message: { theatreStateContract: { kind: "startup" } }, trace: { area: controllerConfig.area, component: "theatre-state-contract" } });
84
84
  // Available again as soon as this node runs, as after a redeploy of it
85
85
  // alone, rather than waiting for the flow's startup message.
86
86
  }
@@ -0,0 +1,3 @@
1
+ import type { NodeInitializer } from "node-red";
2
+ declare const registerZ2mCover: NodeInitializer;
3
+ export = registerZ2mCover;
@@ -0,0 +1,68 @@
1
+ <script type="text/javascript">
2
+ RED.nodes.registerType("home-z2m-cover", {
3
+ category: "home automation",
4
+ color: "#e2d96e",
5
+ defaults: {
6
+ name: {value: ""},
7
+ baseTopic: {value: "zigbee2mqtt", required: true, validate: function (v) {
8
+ var base = String(v || "").trim().replace(/^\/+|\/+$/g, "");
9
+ return base !== "" && !/[+#]/.test(base);
10
+ }},
11
+ // No default: it names the user's own device.
12
+ friendlyName: {value: "", required: true, validate: function (v) {
13
+ var name = String(v || "").trim().replace(/^\/+|\/+$/g, "");
14
+ return name !== "" && !/[+#]/.test(name);
15
+ }}
16
+ },
17
+ inputs: 1,
18
+ outputs: 2,
19
+ outputLabels: ["to the device (MQTT out)", "to the cover controller"],
20
+ icon: "font-awesome/fa-exchange",
21
+ label: function () { return this.name || this.friendlyName || "zigbee2mqtt cover"; },
22
+ paletteLabel: "zigbee2mqtt cover"
23
+ });
24
+ </script>
25
+
26
+ <script type="text/html" data-template-name="home-z2m-cover">
27
+ <div class="form-row"><label for="node-input-name">Name</label><input type="text" id="node-input-name"></div>
28
+ <div class="form-row"><label for="node-input-baseTopic">Base topic</label><input type="text" id="node-input-baseTopic"></div>
29
+ <div class="form-row"><label for="node-input-friendlyName">Friendly name</label><input type="text" id="node-input-friendlyName" placeholder="the device's name in Zigbee2MQTT"></div>
30
+ </script>
31
+
32
+ <script type="text/html" data-help-name="home-z2m-cover">
33
+ <p>Connects a Zigbee2MQTT cover (a blind, shutter or curtain motor) to a <b>home cover controller</b>, over
34
+ Zigbee2MQTT's MQTT messages: no function nodes, and never Home Assistant.</p>
35
+ <h3>Wiring</h3>
36
+ <ol>
37
+ <li>An <b>MQTT in</b> node subscribed to <code>&lt;base&gt;/&lt;friendly name&gt;</code> and another to
38
+ <code>&lt;base&gt;/&lt;friendly name&gt;/availability</code> (output: auto-detect) → this node.</li>
39
+ <li>The cover controller's first output (its device commands) → this node.</li>
40
+ <li>This node's first output → an <b>MQTT out</b> node on the same broker, with no topic set.</li>
41
+ <li>This node's second output → the cover controller.</li>
42
+ </ol>
43
+ <p>Availability needs Zigbee2MQTT's <b>availability</b> setting on; without it the cover is taken as available.</p>
44
+ <h3>Settings</h3>
45
+ <dl class="message-properties">
46
+ <dt>Base topic</dt><dd>Zigbee2MQTT's base topic: <code>zigbee2mqtt</code> unless changed in its settings.</dd>
47
+ <dt>Friendly name</dt><dd>the device's friendly name in Zigbee2MQTT (or its IEEE address if it has none).</dd>
48
+ </dl>
49
+ <h3>What it does</h3>
50
+ <ul>
51
+ <li><code>set_position</code> is sent as <code>{"position": 0-100}</code>; <code>open</code>, <code>close</code> and
52
+ <code>stop</code> as <code>{"state": "OPEN"}</code>, <code>"CLOSE"</code> or <code>"STOP"</code>, on
53
+ <code>&lt;base&gt;/&lt;friendly name&gt;/set</code>. <code>observe</code> asks the device to read its position (or,
54
+ for a cover with no position, its state) on <code>…/get</code>, so the answer is fresh. Some devices can't be read
55
+ this way; they still report their position whenever it changes.</li>
56
+ <li>Each state report becomes <code>msg.cover = {kind: "observed", position, motion, observedAt}</code>, with
57
+ 0 closed and 100 open as Zigbee2MQTT reports by default (set <b>invert</b> on the controller, or the device's
58
+ <code>invert_cover</code> option, if yours runs the other way). Motion comes from <code>motor_state</code> or
59
+ <code>moving</code> where the device reports one; a cover that reports none is confirmed by the controller's
60
+ settle time. <code>observedAt</code> is when the report arrived. Battery and charging status are passed on.</li>
61
+ <li>A cover with no position (open, close and stop only) reports its end state from Zigbee2MQTT's
62
+ <code>OPEN</code> or <code>CLOSE</code> once its motion says it has stopped: before that, the state can show
63
+ where it is going rather than where it is. Set the cover node's <b>Cover</b> to <i>Open, close and stop only</i>.
64
+ Such a cover that reports no motion either can't be confirmed, and the controller says so.</li>
65
+ <li><code>…/availability</code> (<code>{"state": "online"}</code>, or plain <code>online</code>) becomes
66
+ <code>{kind: "availability", online}</code>.</li>
67
+ </ul>
68
+ </script>
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ const z2m_cover_1 = require("../lib/z2m-cover");
3
+ const adapter_1 = require("./adapter");
4
+ const registerZ2mCover = (RED) => {
5
+ function HomeZ2mCover(config) {
6
+ RED.nodes.createNode(this, config);
7
+ const node = this;
8
+ let device;
9
+ try {
10
+ device = (0, z2m_cover_1.normaliseZ2mCoverConfig)(config);
11
+ }
12
+ catch (error) {
13
+ (0, adapter_1.invalidConfig)(node, error);
14
+ return;
15
+ }
16
+ const trace = { area: "", component: "z2m-cover" };
17
+ node.status({ fill: "grey", shape: "ring", text: "waiting for the device" });
18
+ (0, adapter_1.onInput)(node, (msg, send) => {
19
+ const result = (0, adapter_1.runReducer)(node, trace, (saved, now) => (0, z2m_cover_1.transitionZ2mCover)(saved, msg, device, now));
20
+ // MQTT publications for the device on output 1, for an MQTT out node;
21
+ // readings for the cover controller on output 2.
22
+ const outputs = [
23
+ result.publications.length ? result.publications.map((publication) => ({ ...publication })) : null,
24
+ result.readings.length ? result.readings.map((reading) => ({ payload: undefined, ...reading })) : null
25
+ ];
26
+ if (outputs.some((output) => output !== null))
27
+ send(outputs);
28
+ const last = result.readings.at(-1)?.cover;
29
+ if (last?.kind === "availability") {
30
+ node.status({ fill: last.online ? "green" : "grey", shape: last.online ? "dot" : "ring", text: last.online ? "online" : "offline" });
31
+ }
32
+ else if (last) {
33
+ const where = last.position !== undefined ? `${last.position}%` : last.state ?? "position unknown";
34
+ node.status({ fill: "green", shape: "dot", text: last.motion && last.motion !== "stopped" ? `${last.motion} · ${where}` : where });
35
+ }
36
+ });
37
+ }
38
+ RED.nodes.registerType("home-z2m-cover", HomeZ2mCover);
39
+ };
40
+ module.exports = registerZ2mCover;
41
+ //# sourceMappingURL=z2m-cover.js.map
@@ -0,0 +1,3 @@
1
+ import type { NodeInitializer } from "node-red";
2
+ declare const registerZ2mLight: NodeInitializer;
3
+ export = registerZ2mLight;
@@ -0,0 +1,76 @@
1
+ <script type="text/javascript">
2
+ RED.nodes.registerType("home-z2m-light", {
3
+ category: "home automation",
4
+ color: "#e2d96e",
5
+ defaults: {
6
+ name: {value: ""},
7
+ // The room's id for this light: no default, it names the user's own.
8
+ target: {value: "", required: true, validate: function (v) { return String(v || "").trim() !== ""; }},
9
+ baseTopic: {value: "zigbee2mqtt", required: true, validate: function (v) {
10
+ var base = String(v || "").trim().replace(/^\/+|\/+$/g, "");
11
+ return base !== "" && !/[+#]/.test(base);
12
+ }},
13
+ // No default: it names the user's own light or group.
14
+ friendlyName: {value: "", required: true, validate: function (v) {
15
+ var name = String(v || "").trim().replace(/^\/+|\/+$/g, "");
16
+ return name !== "" && !/[+#]/.test(name);
17
+ }},
18
+ group: {value: false}
19
+ },
20
+ inputs: 1,
21
+ outputs: 2,
22
+ outputLabels: ["to the light (MQTT out)", "to the room lighting controller"],
23
+ icon: "font-awesome/fa-lightbulb-o",
24
+ label: function () { return this.name || this.friendlyName || "zigbee2mqtt light"; },
25
+ paletteLabel: "zigbee2mqtt light"
26
+ });
27
+ </script>
28
+
29
+ <script type="text/html" data-template-name="home-z2m-light">
30
+ <div class="form-row"><label for="node-input-name">Name</label><input type="text" id="node-input-name"></div>
31
+ <div class="form-row"><label for="node-input-target">Target</label><input type="text" id="node-input-target" placeholder="the room's id for this light"></div>
32
+ <div class="form-row"><label for="node-input-baseTopic">Base topic</label><input type="text" id="node-input-baseTopic"></div>
33
+ <div class="form-row"><label for="node-input-friendlyName">Friendly name</label><input type="text" id="node-input-friendlyName" placeholder="the light's or group's name in Zigbee2MQTT"></div>
34
+ <div class="form-row"><label for="node-input-group">Zigbee2MQTT group</label><input type="checkbox" id="node-input-group" style="display:inline-block; width:auto;"></div>
35
+ </script>
36
+
37
+ <script type="text/html" data-help-name="home-z2m-light">
38
+ <p>Connects one Zigbee2MQTT light, or a Zigbee2MQTT group of lights, to a <b>room lighting controller</b>, over
39
+ Zigbee2MQTT's MQTT messages: no function nodes, and never Home Assistant. Use one per light in the room's targets.</p>
40
+ <h3>Wiring</h3>
41
+ <ol>
42
+ <li>An <b>MQTT in</b> node subscribed to <code>&lt;base&gt;/&lt;friendly name&gt;/#</code> (output: auto-detect)
43
+ → this node. That covers the light's state, its <code>/set</code> topic (to see other people's commands) and its
44
+ availability.</li>
45
+ <li>The room controller's second output (its light commands) → this node. One wire can feed every light's node:
46
+ each acts only on its own <b>Target</b>.</li>
47
+ <li>This node's first output → an <b>MQTT out</b> node on the same broker, with no topic set.</li>
48
+ <li>This node's second output (readings) → the room controller.</li>
49
+ </ol>
50
+ <h3>What it does</h3>
51
+ <p>It turns the room's commands into Zigbee2MQTT's <code>/set</code> payloads (brightness 1-254, colour temperature
52
+ in mireds, the transition), exactly as the room sent them before 6.0, and the light's reports into readings in
53
+ percent and kelvin. It tells its own commands from someone else's: an exact echo on <code>/set</code> is its own;
54
+ a bare ON or OFF arriving while one of its own with that power hasn't come back is its own, changed on the way;
55
+ anything else is someone else's, which the room may take as a person's change. When the room restyles a light that
56
+ already shows that style, it sends nothing and tells the room so. When the room starts, it asks the light to report
57
+ (<code>/get</code>).</p>
58
+ <p>Availability (<code>/availability</code>) is passed on and shown by the room, but changes nothing.</p>
59
+ <h3>Groups</h3>
60
+ <p>Tick <b>Zigbee2MQTT group</b> for a group. Commands are the same. Zigbee2MQTT works out a group's state itself,
61
+ from what it sent and its members' reports, so a group's report follows a command at once. A group has no
62
+ availability and can't be asked to report, so at the room's startup the node sends nothing: the room waits for
63
+ Zigbee2MQTT's next state for the group. (Passing on an old state could settle a command still on its way.)</p>
64
+ <p>Switch on the group's <b>retain</b> in Zigbee2MQTT: <code>retain: true</code> under the group in Zigbee2MQTT's
65
+ <code>configuration.yaml</code> (it is off by default). Zigbee2MQTT doesn't retain a group's state by default, so after a restart the
66
+ room has no reading for the group until it changes, and an activity that holds it waits on it. With retain, the broker
67
+ gives this node the group's last state as soon as its MQTT in node subscribes.</p>
68
+ <h3>Settings</h3>
69
+ <dl class="message-properties">
70
+ <dt>Target</dt><dd>The light's id in the room controller's Targets JSON.</dd>
71
+ <dt>Base topic</dt><dd>Zigbee2MQTT's base topic, <code>zigbee2mqtt</code> unless you changed it.</dd>
72
+ <dt>Friendly name</dt><dd>The light's or group's name in Zigbee2MQTT.</dd>
73
+ </dl>
74
+ <p>For a light that isn't Zigbee2MQTT's, see the neutral light contract in the README: any flow that answers the
75
+ room's <code>msg.light</code> commands with <code>msg.light</code> readings works in this node's place.</p>
76
+ </script>