@avantics/node-red-home-automation 4.0.3 → 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.
Files changed (116) hide show
  1. package/README.md +447 -64
  2. package/contract-examples/room-lighting.mjs +39 -27
  3. package/contract-examples/switch-gestures.mjs +1 -1
  4. package/contract-examples/volume-ownership.mjs +6 -5
  5. package/dist/lib/activity-lighting-controller.d.ts +9 -2
  6. package/dist/lib/activity-lighting-controller.js +107 -19
  7. package/dist/lib/adopted-request.d.ts +17 -0
  8. package/dist/lib/adopted-request.js +61 -7
  9. package/dist/lib/avr-controller.d.ts +9 -3
  10. package/dist/lib/avr-controller.js +71 -24
  11. package/dist/lib/avr-publishing.d.ts +3 -3
  12. package/dist/lib/avr-publishing.js +11 -5
  13. package/dist/lib/backlighting-controller.d.ts +2 -0
  14. package/dist/lib/backlighting-controller.js +27 -7
  15. package/dist/lib/circadian-policy.d.ts +47 -0
  16. package/dist/lib/circadian-policy.js +111 -0
  17. package/dist/lib/circadian-publishing.d.ts +33 -0
  18. package/dist/lib/circadian-publishing.js +154 -0
  19. package/dist/lib/circadian-schedule.d.ts +4 -0
  20. package/dist/lib/circadian-schedule.js +12 -0
  21. package/dist/lib/command-echo.d.ts +8 -1
  22. package/dist/lib/command-echo.js +37 -6
  23. package/dist/lib/cover-controller.js +4 -0
  24. package/dist/lib/cover-publishing.js +1 -1
  25. package/dist/lib/denon-protocol.d.ts +5 -1
  26. package/dist/lib/denon-protocol.js +27 -11
  27. package/dist/lib/display-mode-controller.d.ts +12 -5
  28. package/dist/lib/display-mode-controller.js +181 -87
  29. package/dist/lib/enable-input.d.ts +3 -0
  30. package/dist/lib/enable-input.js +18 -0
  31. package/dist/lib/house-schedule-publishing.js +1 -1
  32. package/dist/lib/house-schedule.js +2 -2
  33. package/dist/lib/light-contract.d.ts +53 -0
  34. package/dist/lib/light-contract.js +11 -0
  35. package/dist/lib/light-styling.d.ts +2 -2
  36. package/dist/lib/light-styling.js +5 -4
  37. package/dist/lib/motion-policy.d.ts +0 -1
  38. package/dist/lib/motion-policy.js +9 -8
  39. package/dist/lib/motion-publishing.js +3 -2
  40. package/dist/lib/mqtt-message.d.ts +6 -0
  41. package/dist/lib/mqtt-message.js +21 -0
  42. package/dist/lib/occupancy-publishing.js +1 -1
  43. package/dist/lib/power-reading.d.ts +5 -0
  44. package/dist/lib/power-reading.js +13 -0
  45. package/dist/lib/room-controller.d.ts +39 -29
  46. package/dist/lib/room-controller.js +314 -192
  47. package/dist/lib/room-lighting-discovery.d.ts +0 -14
  48. package/dist/lib/room-lighting-discovery.js +20 -126
  49. package/dist/lib/room-publishing.d.ts +10 -12
  50. package/dist/lib/room-publishing.js +65 -125
  51. package/dist/lib/shelly-cover.d.ts +44 -0
  52. package/dist/lib/shelly-cover.js +153 -0
  53. package/dist/lib/shelly-gen2.d.ts +25 -0
  54. package/dist/lib/shelly-gen2.js +58 -0
  55. package/dist/lib/shelly-switch.d.ts +95 -0
  56. package/dist/lib/shelly-switch.js +237 -0
  57. package/dist/lib/source-capability-controller.d.ts +2 -0
  58. package/dist/lib/source-capability-controller.js +27 -6
  59. package/dist/lib/switch-policy.d.ts +1 -1
  60. package/dist/lib/switch-policy.js +1 -1
  61. package/dist/lib/theatre-requests.d.ts +29 -0
  62. package/dist/lib/theatre-requests.js +89 -0
  63. package/dist/lib/theatre-state-contract.d.ts +5 -22
  64. package/dist/lib/theatre-state-contract.js +30 -77
  65. package/dist/lib/toggle-power.d.ts +41 -0
  66. package/dist/lib/toggle-power.js +74 -0
  67. package/dist/lib/topic-names.d.ts +1 -0
  68. package/dist/lib/topic-names.js +7 -0
  69. package/dist/lib/{display-transport.d.ts → transports/display-transport.d.ts} +1 -1
  70. package/dist/lib/{display-transport.js → transports/display-transport.js} +32 -7
  71. package/dist/lib/z2m-cover.d.ts +49 -0
  72. package/dist/lib/z2m-cover.js +120 -0
  73. package/dist/lib/z2m-light-adapter.d.ts +62 -0
  74. package/dist/lib/z2m-light-adapter.js +195 -0
  75. package/dist/lib/z2m-light.d.ts +8 -0
  76. package/dist/lib/z2m-light.js +61 -3
  77. package/dist/nodes/activity-lighting-controller.html +20 -3
  78. package/dist/nodes/activity-lighting-controller.js +21 -0
  79. package/dist/nodes/avr-controller.html +25 -6
  80. package/dist/nodes/avr-controller.js +16 -6
  81. package/dist/nodes/circadian-policy.d.ts +3 -0
  82. package/dist/nodes/circadian-policy.html +140 -0
  83. package/dist/nodes/circadian-policy.js +162 -0
  84. package/dist/nodes/cover-controller.html +4 -3
  85. package/dist/nodes/display-mode-controller.html +58 -11
  86. package/dist/nodes/display-mode-controller.js +19 -6
  87. package/dist/nodes/house-schedule.html +3 -3
  88. package/dist/nodes/motion-policy.html +5 -2
  89. package/dist/nodes/motion-policy.js +3 -0
  90. package/dist/nodes/refused-enable.d.ts +7 -0
  91. package/dist/nodes/refused-enable.js +24 -0
  92. package/dist/nodes/room-controller.html +215 -82
  93. package/dist/nodes/room-controller.js +71 -83
  94. package/dist/nodes/shelly-cover.d.ts +3 -0
  95. package/dist/nodes/shelly-cover.html +65 -0
  96. package/dist/nodes/shelly-cover.js +34 -0
  97. package/dist/nodes/shelly-switch.d.ts +3 -0
  98. package/dist/nodes/shelly-switch.html +82 -0
  99. package/dist/nodes/shelly-switch.js +48 -0
  100. package/dist/nodes/theatre-state-contract.html +4 -3
  101. package/dist/nodes/z2m-cover.d.ts +3 -0
  102. package/dist/nodes/z2m-cover.html +68 -0
  103. package/dist/nodes/z2m-cover.js +41 -0
  104. package/dist/nodes/z2m-light.d.ts +3 -0
  105. package/dist/nodes/z2m-light.html +72 -0
  106. package/dist/nodes/z2m-light.js +50 -0
  107. package/examples/Cover control.json +24 -56
  108. package/examples/Home theatre activity.json +54 -9
  109. package/examples/Home theatre equipment.json +4 -4
  110. package/examples/House schedule.json +106 -24
  111. package/examples/Room lighting.json +54 -11
  112. package/examples/Shelly cover.json +252 -0
  113. package/examples/Shelly switch.json +197 -0
  114. package/package.json +13 -4
  115. package/dist/lib/room-schedule.d.ts +0 -8
  116. package/dist/lib/room-schedule.js +0 -41
@@ -1,26 +1,26 @@
1
1
  "use strict";
2
- const circadian_1 = require("../lib/circadian");
3
- const circadian_schedule_1 = require("../lib/circadian-schedule");
2
+ const room_controller_1 = require("../lib/room-controller");
3
+ const light_contract_1 = require("../lib/light-contract");
4
4
  const publishing_lifecycle_1 = require("./publishing-lifecycle");
5
5
  const room_publishing_1 = require("../lib/room-publishing");
6
6
  const adapter_1 = require("./adapter");
7
- const shared_schedule_1 = require("./shared-schedule");
8
7
  const settings_1 = require("../lib/settings");
9
8
  const ha_discovery_1 = require("../lib/ha-discovery");
10
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.
11
12
  function parseTargets(value) {
12
13
  const targets = (0, settings_1.jsonSetting)(value, "targets", []);
13
- if (!Array.isArray(targets) || targets.some((target) => {
14
- if (!target || typeof target !== "object")
15
- return true;
16
- const candidate = target;
17
- return typeof candidate.id !== "string" ||
18
- typeof candidate.stateTopic !== "string" ||
19
- typeof candidate.commandTopic !== "string";
20
- })) {
21
- 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");
22
17
  }
23
- 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 } : {}) }));
24
24
  if (new Set(typedTargets.map((target) => target.id)).size !== typedTargets.length) {
25
25
  throw new Error("target ids must be unique");
26
26
  }
@@ -31,13 +31,23 @@ const registerRoomController = (RED) => {
31
31
  RED.nodes.createNode(this, config);
32
32
  const node = this;
33
33
  const area = (0, adapter_1.optionalArea)(config);
34
- let refreshTimer;
35
34
  let controllerConfig;
36
- let refreshSeconds;
37
- // A schedule that is set but not running, as when it was deleted: the
38
- // room keeps to its own times and says so.
39
- const { schedule: house, missing: houseMissing } = (0, shared_schedule_1.referencedSchedule)(RED, config.houseSchedule);
40
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
+ }
41
51
  // Not set (null, or absent from a flow saved before these settings)
42
52
  // means on, so an older flow keeps publishing as before.
43
53
  const publishState = (0, settings_1.optionalBooleanSetting)(config.publishState, "publish state") ?? true;
@@ -46,41 +56,31 @@ const registerRoomController = (RED) => {
46
56
  // area is only a label.
47
57
  if (publishState || discovery)
48
58
  (0, topic_names_1.publishingArea)(area, true);
49
- refreshSeconds = Math.max(10, (0, settings_1.numberSetting)(config.refreshSeconds, "refresh interval", 60));
50
- // Saved latitude, longitude and minimum settings from earlier versions
51
- // are ignored.
52
- const levels = {
53
- maxBrightnessPct: (0, settings_1.numberSetting)(config.maxBrightnessPct, "daytime brightness", 100),
54
- maxColorTempKelvin: (0, settings_1.numberSetting)(config.maxColorTempKelvin, "daytime colour temperature", 6500),
55
- sleepBrightnessPct: (0, settings_1.numberSetting)(config.sleepBrightnessPct, "sleep brightness", 1),
56
- sleepColorTempKelvin: (0, settings_1.numberSetting)(config.sleepColorTempKelvin, "sleep colour temperature", 2200)
57
- };
58
- 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"));
59
59
  controllerConfig = {
60
60
  nodeId: node.id,
61
61
  // Not set: a node saved before node IDs, which keeps its area-named
62
62
  // entities. The editor gives new nodes "node".
63
63
  idScheme: (0, settings_1.optionalChoiceSetting)(config.idScheme, "ID scheme", ha_discovery_1.ID_SCHEMES) ?? "area",
64
- timesFrom: house ? "house" : "room",
65
64
  area,
66
65
  displayName: config.displayName || area,
67
66
  discoveryPrefix: (0, ha_discovery_1.discoveryPrefixSetting)(config.discoveryPrefix),
68
67
  publishState,
69
68
  discovery,
70
69
  targets: parseTargets(config.targets),
71
- initialTransitionSeconds: (0, settings_1.numberSetting)(config.initialTransitionSeconds, "initial transition", 1),
72
- refreshTransitionSeconds: (0, settings_1.numberSetting)(config.refreshTransitionSeconds, "refresh transition", 60),
73
- initialSleep: (0, settings_1.booleanSetting)(config.initialSleep, "initial sleep", false),
74
- // The adapter picks the circadian lighting; the controller only asks
75
- // it how a light should look.
76
- lighting: (0, circadian_1.scheduledLighting)(schedule, levels),
77
- schedule
70
+ initialTransitionSeconds: (0, settings_1.numberSetting)(config.initialTransitionSeconds, "initial transition", 1)
78
71
  };
79
72
  }
80
73
  catch (error) {
81
74
  (0, adapter_1.invalidConfig)(node, error);
82
75
  return;
83
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));
84
84
  // Available, and clear of what it no longer publishes, as soon as it
85
85
  // runs; offline as it stops; nothing left behind when deleted.
86
86
  const lifecycle = (0, publishing_lifecycle_1.publishingLifecycle)(RED, node, {
@@ -88,22 +88,48 @@ const registerRoomController = (RED) => {
88
88
  starting: () => (0, room_publishing_1.roomStartingOutputs)(controllerConfig),
89
89
  stopping: () => (0, room_publishing_1.roomStoppingOutputs)(controllerConfig),
90
90
  output: 0,
91
- outputs: 1
91
+ outputs: 3
92
92
  });
93
93
  function updateStatus(status) {
94
94
  node.status({
95
- fill: status.manualCount || houseMissing ? "yellow" : status.circadianEnabled ? "green" : "grey",
95
+ fill: status.manualCount ? "yellow" : status.styled ? "green" : "grey",
96
96
  shape: "dot",
97
- text: (houseMissing ? "schedule missing: own times · " : "") + (status.circadianEnabled
98
- ? (status.sleeping ? "sleep" : "circadian")
99
- : "manual only") + " · " + status.onCount + " on · " + status.manualCount + " manual" +
100
- (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" : "")
101
100
  });
102
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();
103
112
  function process(msg, send) {
104
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));
105
- if (result.outputs.length) {
106
- send([result.outputs.map((output) => ({ ...output, payload: output.payload }))]);
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
+ }
107
133
  }
108
134
  updateStatus(result.status);
109
135
  return result;
@@ -114,50 +140,12 @@ const registerRoomController = (RED) => {
114
140
  lifecycle.startup(send);
115
141
  process({
116
142
  lighting: (0, adapter_1.objectField)(msg, "lighting"),
143
+ light: msg.light,
117
144
  topic: typeof msg.topic === "string" ? msg.topic : undefined,
118
145
  payload: msg.payload,
119
146
  retain: msg.retain === true
120
147
  }, send);
121
148
  });
122
- // Times taken from a house are the house's: a room that no longer follows
123
- // one, as when its schedule was set to none or deleted, goes back to its
124
- // own times rather than keeping the house's last ones.
125
- const roomTrace = { area: controllerConfig.area, component: "room-controller" };
126
- if (house) {
127
- node.context().set("followsHouse", true);
128
- }
129
- else if (node.context().get("followsHouse") === true) {
130
- node.context().set("followsHouse", false);
131
- (0, adapter_1.runReducer)(node, roomTrace, (saved) => ({
132
- state: { ...saved, schedule: undefined, scheduleSetting: undefined },
133
- traces: [{ event: "house-schedule", outcome: "changed", reason: "own-times", details: {} }]
134
- }));
135
- }
136
- if (house) {
137
- // The house's times: once the flows have started, as this room may
138
- // have restarted alone, and at every change until it stops.
139
- const follow = (0, adapter_1.guarded)(node, () => {
140
- process({ lighting: { kind: "schedule", ...house.schedule() } }, node.send.bind(node));
141
- });
142
- (0, adapter_1.whenFlowsStarted)(RED, node, follow);
143
- node.on("close", house.subscribe(follow));
144
- }
145
- else if (houseMissing) {
146
- (0, adapter_1.runReducer)(node, roomTrace, (state) => ({
147
- state,
148
- traces: [{ event: "house-schedule", outcome: "ignored", reason: "schedule-missing", details: { schedule: config.houseSchedule } }]
149
- }));
150
- node.status({ fill: "yellow", shape: "ring", text: "schedule missing: own times" });
151
- }
152
- refreshTimer = setInterval((0, adapter_1.guarded)(node, () => {
153
- process({ lighting: { kind: "refresh" } }, node.send.bind(node));
154
- }), refreshSeconds * 1000);
155
- // Available again as soon as this node runs, as after a redeploy of it
156
- // alone, rather than waiting for the flow's startup message.
157
- node.on("close", (_removed, done) => {
158
- clearInterval(refreshTimer);
159
- done();
160
- });
161
149
  }
162
150
  RED.nodes.registerType("home-room-lighting-controller", HomeRoomLightingController);
163
151
  };
@@ -0,0 +1,3 @@
1
+ import type { NodeInitializer } from "node-red";
2
+ declare const registerShellyCover: NodeInitializer;
3
+ export = registerShellyCover;
@@ -0,0 +1,65 @@
1
+ <script type="text/javascript">
2
+ RED.nodes.registerType("home-shelly-cover", {
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
+ },
16
+ inputs: 1,
17
+ outputs: 2,
18
+ outputLabels: ["to the device (MQTT out)", "to the cover controller"],
19
+ icon: "font-awesome/fa-exchange",
20
+ label: function () { return this.name || "shelly cover"; },
21
+ paletteLabel: "shelly cover"
22
+ });
23
+ </script>
24
+
25
+ <script type="text/html" data-template-name="home-shelly-cover">
26
+ <div class="form-row"><label for="node-input-name">Name</label><input type="text" id="node-input-name"></div>
27
+ <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. shellyplus2pm-a8032ab12345"></div>
28
+ <div class="form-row"><label for="node-input-channel">Cover ID</label><input type="number" min="0" step="1" id="node-input-channel"></div>
29
+ </script>
30
+
31
+ <script type="text/html" data-help-name="home-shelly-cover">
32
+ <p>Connects a Shelly Gen2 or later device in cover mode to a <b>home cover controller</b>, over the device's own
33
+ MQTT API: no function nodes, no Home Assistant and no HTTP.</p>
34
+ <h3>Wiring</h3>
35
+ <ol>
36
+ <li>An <b>MQTT in</b> node subscribed to <code>&lt;prefix&gt;/#</code> (output: auto-detect) → this node.</li>
37
+ <li>The cover controller's first output (its device commands) → this node.</li>
38
+ <li>This node's first output → an <b>MQTT out</b> node on the same broker, with no topic set.</li>
39
+ <li>This node's second output → the cover controller.</li>
40
+ </ol>
41
+ <p>On the device, MQTT must be on with <b>Enable MQTT Control</b> (the default): without it the device ignores
42
+ the commands this node sends. <b>RPC status notifications</b> (also the default) report each change; with
43
+ <b>Generic status update</b> it also reports a full status on every significant change. Calibrate the cover
44
+ on the device first.</p>
45
+ <h3>Settings</h3>
46
+ <dl class="message-properties">
47
+ <dt>MQTT topic prefix</dt><dd>the device's topic prefix: its device ID (such as
48
+ <code>shellyplus2pm-a8032ab12345</code>) unless a custom one is set on the device.</dd>
49
+ <dt>Cover ID</dt><dd><code>0</code>, or <code>1</code> for a device's second cover.</dd>
50
+ </dl>
51
+ <h3>What it does</h3>
52
+ <ul>
53
+ <li><code>set_position</code> is sent as <code>pos,&lt;0-100&gt;</code>, <code>stop</code> as <code>stop</code>,
54
+ and <code>observe</code> as <code>status_update</code>, which makes the device report a fresh status, on
55
+ <code>&lt;prefix&gt;/command/cover:&lt;id&gt;</code>.</li>
56
+ <li>The device's status, from <code>&lt;prefix&gt;/status/cover:&lt;id&gt;</code> and its change notifications on
57
+ <code>&lt;prefix&gt;/events/rpc</code>, becomes <code>msg.cover = {kind: "observed", position, motion,
58
+ observedAt}</code>, with 0 closed and 100 open (set <b>invert</b> on the controller if yours runs the other
59
+ way). <code>&lt;prefix&gt;/online</code> becomes <code>{kind: "availability", online}</code>.</li>
60
+ <li>A cover that isn't calibrated, or doesn't know its position, has no position to report: the controller's
61
+ command in hand fails with reason <code>not-calibrated</code> or <code>position-unknown</code>, and a
62
+ position isn't sent to an uncalibrated cover, which would refuse it.</li>
63
+ <li>Anything else on the input is ignored. The node's status shows the cover's last state and position.</li>
64
+ </ul>
65
+ </script>
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ const shelly_cover_1 = require("../lib/shelly-cover");
3
+ const adapter_1 = require("./adapter");
4
+ const registerShellyCover = (RED) => {
5
+ function HomeShellyCover(config) {
6
+ RED.nodes.createNode(this, config);
7
+ const node = this;
8
+ let device;
9
+ try {
10
+ device = (0, shelly_cover_1.normaliseShellyCoverConfig)(config);
11
+ }
12
+ catch (error) {
13
+ (0, adapter_1.invalidConfig)(node, error);
14
+ return;
15
+ }
16
+ const trace = { area: "", component: "shelly-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, shelly_cover_1.transitionShellyCover)(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
+ node.status({ fill: result.status.calibrated ? "green" : "yellow", shape: "dot", text: result.status.text });
29
+ });
30
+ }
31
+ RED.nodes.registerType("home-shelly-cover", HomeShellyCover);
32
+ };
33
+ module.exports = registerShellyCover;
34
+ //# sourceMappingURL=shelly-cover.js.map
@@ -0,0 +1,3 @@
1
+ import type { NodeInitializer } from "node-red";
2
+ declare const registerShellySwitch: NodeInitializer;
3
+ export = registerShellySwitch;
@@ -0,0 +1,82 @@
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
+ </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));
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,9 +88,10 @@
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>
@@ -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;