@avantics/node-red-home-automation 6.1.0 → 6.3.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 (115) hide show
  1. package/README.md +260 -46
  2. package/dist/lib/activity-lighting-controller.d.ts +1 -0
  3. package/dist/lib/activity-lighting-controller.js +37 -10
  4. package/dist/lib/avr-controller.d.ts +3 -0
  5. package/dist/lib/avr-controller.js +24 -2
  6. package/dist/lib/backlighting-controller.d.ts +5 -2
  7. package/dist/lib/backlighting-controller.js +27 -2
  8. package/dist/lib/cover-publishing.js +20 -6
  9. package/dist/lib/display-mode-controller.d.ts +3 -0
  10. package/dist/lib/display-mode-controller.js +43 -16
  11. package/dist/lib/esphome-sensor.d.ts +73 -0
  12. package/dist/lib/esphome-sensor.js +261 -0
  13. package/dist/lib/home-assistant-setting.d.ts +23 -0
  14. package/dist/lib/home-assistant-setting.js +39 -0
  15. package/dist/lib/motion-policy.d.ts +1 -0
  16. package/dist/lib/motion-policy.js +21 -1
  17. package/dist/lib/occupancy-fusion.d.ts +1 -0
  18. package/dist/lib/occupancy-fusion.js +15 -2
  19. package/dist/lib/room-controller.d.ts +1 -0
  20. package/dist/lib/room-controller.js +5 -0
  21. package/dist/lib/room-publishing.d.ts +1 -0
  22. package/dist/lib/room-publishing.js +11 -0
  23. package/dist/lib/scene-publishing.d.ts +30 -0
  24. package/dist/lib/scene-publishing.js +63 -0
  25. package/dist/lib/scene.d.ts +69 -0
  26. package/dist/lib/scene.js +223 -0
  27. package/dist/lib/shelly-cover.js +5 -0
  28. package/dist/lib/shelly-relay-light.d.ts +24 -0
  29. package/dist/lib/shelly-relay-light.js +93 -0
  30. package/dist/lib/shelly-switch.d.ts +8 -0
  31. package/dist/lib/shelly-switch.js +37 -8
  32. package/dist/lib/source-capability-controller.d.ts +4 -1
  33. package/dist/lib/source-capability-controller.js +49 -2
  34. package/dist/lib/target-observation.d.ts +14 -0
  35. package/dist/lib/target-observation.js +22 -0
  36. package/dist/lib/theatre-activity-controller.d.ts +5 -0
  37. package/dist/lib/theatre-activity-controller.js +91 -12
  38. package/dist/lib/theatre-metrics.js +6 -4
  39. package/dist/lib/theatre-slo.js +3 -0
  40. package/dist/lib/theatre-state-contract.d.ts +4 -2
  41. package/dist/lib/theatre-state-contract.js +51 -8
  42. package/dist/lib/theatre-state.d.ts +2 -0
  43. package/dist/lib/theatre-state.js +13 -1
  44. package/dist/lib/wled.d.ts +48 -0
  45. package/dist/lib/wled.js +105 -0
  46. package/dist/lib/z2m-power.d.ts +38 -0
  47. package/dist/lib/z2m-power.js +83 -0
  48. package/dist/lib/z2m-sensor.d.ts +37 -0
  49. package/dist/lib/z2m-sensor.js +88 -0
  50. package/dist/nodes/apollo-r-pro-1.d.ts +2 -0
  51. package/dist/nodes/apollo-r-pro-1.html +80 -0
  52. package/dist/nodes/apollo-r-pro-1.js +4 -0
  53. package/dist/nodes/avr-controller.html +82 -21
  54. package/dist/nodes/avr-controller.js +24 -12
  55. package/dist/nodes/backlighting-controller.js +3 -2
  56. package/dist/nodes/circadian-policy.html +84 -20
  57. package/dist/nodes/circadian-policy.js +30 -11
  58. package/dist/nodes/cover-controller.html +87 -25
  59. package/dist/nodes/cover-controller.js +25 -12
  60. package/dist/nodes/device-side.d.ts +21 -0
  61. package/dist/nodes/device-side.js +76 -0
  62. package/dist/nodes/display-mode-controller.js +11 -2
  63. package/dist/nodes/esphome-sensor.d.ts +2 -0
  64. package/dist/nodes/esphome-sensor.js +77 -0
  65. package/dist/nodes/everything-presence-one.d.ts +2 -0
  66. package/dist/nodes/everything-presence-one.html +78 -0
  67. package/dist/nodes/everything-presence-one.js +4 -0
  68. package/dist/nodes/home-assistant-discovery.d.ts +26 -0
  69. package/dist/nodes/home-assistant-discovery.js +64 -0
  70. package/dist/nodes/house-schedule-mqtt.html +70 -5
  71. package/dist/nodes/house-schedule-mqtt.js +22 -8
  72. package/dist/nodes/motion-policy.html +87 -22
  73. package/dist/nodes/motion-policy.js +44 -11
  74. package/dist/nodes/mqtt-broker-link.d.ts +41 -0
  75. package/dist/nodes/mqtt-broker-link.js +91 -0
  76. package/dist/nodes/occupancy-fusion.html +86 -20
  77. package/dist/nodes/occupancy-fusion.js +25 -12
  78. package/dist/nodes/publishing-side.d.ts +20 -0
  79. package/dist/nodes/publishing-side.js +66 -0
  80. package/dist/nodes/room-controller.html +106 -35
  81. package/dist/nodes/room-controller.js +63 -16
  82. package/dist/nodes/scene.d.ts +3 -0
  83. package/dist/nodes/scene.html +247 -0
  84. package/dist/nodes/scene.js +128 -0
  85. package/dist/nodes/shelly-cover.html +47 -2
  86. package/dist/nodes/shelly-cover.js +19 -4
  87. package/dist/nodes/shelly-switch.html +76 -6
  88. package/dist/nodes/shelly-switch.js +32 -7
  89. package/dist/nodes/source-capability-controller.js +3 -2
  90. package/dist/nodes/theatre-activity-controller.html +4 -2
  91. package/dist/nodes/theatre-state-contract.html +72 -9
  92. package/dist/nodes/theatre-state-contract.js +33 -14
  93. package/dist/nodes/wled.d.ts +3 -0
  94. package/dist/nodes/wled.html +56 -0
  95. package/dist/nodes/wled.js +53 -0
  96. package/dist/nodes/z2m-cover.html +47 -2
  97. package/dist/nodes/z2m-cover.js +21 -4
  98. package/dist/nodes/z2m-light.html +49 -3
  99. package/dist/nodes/z2m-light.js +40 -4
  100. package/dist/nodes/z2m-power.d.ts +3 -0
  101. package/dist/nodes/z2m-power.html +58 -0
  102. package/dist/nodes/z2m-power.js +53 -0
  103. package/dist/nodes/z2m-sensor.d.ts +3 -0
  104. package/dist/nodes/z2m-sensor.html +69 -0
  105. package/dist/nodes/z2m-sensor.js +62 -0
  106. package/examples/Cover control.json +38 -91
  107. package/examples/Cover pair.json +36 -3
  108. package/examples/Home theatre activity.json +46 -91
  109. package/examples/Home theatre equipment.json +34 -2
  110. package/examples/House schedule.json +58 -80
  111. package/examples/Room lighting.json +45 -44
  112. package/examples/Scene.json +167 -0
  113. package/examples/Shelly cover.json +36 -105
  114. package/examples/Shelly switch.json +52 -148
  115. package/package.json +8 -2
@@ -3,7 +3,21 @@
3
3
  category: "home automation",
4
4
  color: "#e2d96e",
5
5
  defaults: {
6
- name: {value: ""},
6
+ // A marker only a node new from the palette has, as core nodes do: import
7
+ // and paste keep the saved name, so onadd leaves a saved node's settings.
8
+ name: {value: "_DEFAULT_"},
9
+ // Node-RED's MQTT broker node, through which it subscribes to its device's
10
+ // topics and publishes on them itself. The editor offers the first broker
11
+ // when the dialog is opened and saved, not when the node is dropped.
12
+ broker: {value: "", type: "mqtt-broker", required: false, validate: function (v) {
13
+ return (Boolean(v) && v !== "_ADD_") || this.publishesThroughOutput !== false ? true : "choose a broker";
14
+ }},
15
+ // Whether it reaches its device through MQTT in and out nodes, as before
16
+ // 6.2, and so needs no broker until one is chosen. A node saved before
17
+ // this setting loads without it (the editor applies no defaults to the
18
+ // deployed flows), which counts as true; a new node sets false (onadd).
19
+ // No row.
20
+ publishesThroughOutput: {value: false},
7
21
  baseTopic: {value: "zigbee2mqtt", required: true, validate: function (v) {
8
22
  var base = String(v || "").trim().replace(/^\/+|\/+$/g, "");
9
23
  return base !== "" && !/[+#]/.test(base);
@@ -14,9 +28,31 @@
14
28
  return name !== "" && !/[+#]/.test(name);
15
29
  }}
16
30
  },
31
+ onadd: function () {
32
+ if (this.name !== "_DEFAULT_") return;
33
+ this.name = "";
34
+ this.publishesThroughOutput = false;
35
+ },
36
+ oneditprepare: function () {
37
+ var node = this;
38
+ // While it reaches its device through MQTT in and out nodes, that it does
39
+ // until given a broker.
40
+ function rows() {
41
+ var broker = $("#node-input-broker").val();
42
+ $(".device-through-output").toggle(node.publishesThroughOutput !== false && (!broker || broker === "_ADD_"));
43
+ }
44
+ $("#node-input-broker").on("change", rows);
45
+ rows();
46
+ },
47
+ oneditsave: function () {
48
+ // A broker chosen: it publishes itself from now on.
49
+ var broker = $("#node-input-broker").val();
50
+ if (broker && broker !== "_ADD_") this.publishesThroughOutput = false;
51
+ else if (this.publishesThroughOutput !== false) this.publishesThroughOutput = true;
52
+ },
17
53
  inputs: 1,
18
54
  outputs: 2,
19
- outputLabels: ["to the device (MQTT out)", "to the cover controller"],
55
+ outputLabels: ["to the device (MQTT out, without a broker)", "to the cover controller"],
20
56
  icon: "font-awesome/fa-exchange",
21
57
  label: function () { return this.name || this.friendlyName || "zigbee2mqtt cover"; },
22
58
  paletteLabel: "zigbee2mqtt cover"
@@ -25,6 +61,11 @@
25
61
 
26
62
  <script type="text/html" data-template-name="home-z2m-cover">
27
63
  <div class="form-row"><label for="node-input-name">Name</label><input type="text" id="node-input-name"></div>
64
+ <div class="form-row"><label for="node-input-broker">Broker</label><input type="text" id="node-input-broker"></div>
65
+ <div class="form-tips">With a broker, it subscribes to its device's topics and publishes on them itself: the MQTT in and
66
+ MQTT out nodes around it are no longer needed and can be deleted.</div>
67
+ <div class="form-tips device-through-output">This node reaches its device through MQTT in and out nodes, as before 6.2,
68
+ and keeps working that way. Choose a Broker to let it do so itself.</div>
28
69
  <div class="form-row"><label for="node-input-baseTopic">Base topic</label><input type="text" id="node-input-baseTopic"></div>
29
70
  <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
71
  </script>
@@ -32,7 +73,11 @@
32
73
  <script type="text/html" data-help-name="home-z2m-cover">
33
74
  <p>Connects a Zigbee2MQTT cover (a blind, shutter or curtain motor) to a <b>home cover controller</b>, over
34
75
  Zigbee2MQTT's MQTT messages: no function nodes, and never Home Assistant.</p>
76
+ <p>The devices: <a href="https://www.zigbee2mqtt.io/supported-devices/" target="_blank">Zigbee2MQTT's supported devices</a>.</p>
35
77
  <h3>Wiring</h3>
78
+ <p>With a <b>Broker</b>, it subscribes to <code>&lt;base&gt;/&lt;friendly name&gt;</code> and its <code>/availability</code> (exactly these: a friendly name can contain <code>/</code>) and publishes on its device's topics itself: leave out the MQTT
79
+ in and MQTT out nodes below (still wired, they change nothing). Its first output then sends nothing. It reads its cover on each connect, as a Status node's startup did through the cover controller. A node saved
80
+ before 6.2 keeps using them until you choose a broker.</p>
36
81
  <ol>
37
82
  <li>An <b>MQTT in</b> node subscribed to <code>&lt;base&gt;/&lt;friendly name&gt;</code> and another to
38
83
  <code>&lt;base&gt;/&lt;friendly name&gt;/availability</code> (output: auto-detect) → this node.</li>
@@ -1,13 +1,16 @@
1
1
  "use strict";
2
2
  const z2m_cover_1 = require("../lib/z2m-cover");
3
3
  const adapter_1 = require("./adapter");
4
+ const device_side_1 = require("./device-side");
4
5
  const registerZ2mCover = (RED) => {
5
6
  function HomeZ2mCover(config) {
6
7
  RED.nodes.createNode(this, config);
7
8
  const node = this;
8
9
  let device;
10
+ let broker;
9
11
  try {
10
12
  device = (0, z2m_cover_1.normaliseZ2mCoverConfig)(config);
13
+ broker = (0, device_side_1.deviceBroker)(RED, config.broker);
11
14
  }
12
15
  catch (error) {
13
16
  (0, adapter_1.invalidConfig)(node, error);
@@ -15,12 +18,20 @@ const registerZ2mCover = (RED) => {
15
18
  }
16
19
  const trace = { area: "", component: "z2m-cover" };
17
20
  node.status({ fill: "grey", shape: "ring", text: "waiting for the device" });
18
- (0, adapter_1.onInput)(node, (msg, send) => {
21
+ // With a broker, it reads its device on each connect, reconnects
22
+ // included, through its own observe: after a restart nothing reported it
23
+ // while it was away, and the controller acts on the reading as on one it
24
+ // asked for.
25
+ // Exactly its own topics: a friendly name can contain "/", so <name>/#
26
+ // would take in other devices' too.
27
+ const topics = (0, z2m_cover_1.z2mCoverTopics)(device);
28
+ const side = (0, device_side_1.deviceSide)(node, broker, topics.device, [topics.device, topics.availability], trace, (msg, send) => handle(msg, send), () => handle({ cover_command: { operation: "observe" } }, node.send.bind(node)));
29
+ function handle(msg, send) {
19
30
  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.
31
+ // MQTT publications for the device: to the broker, or on output 1 for
32
+ // an MQTT out node; readings for the cover controller on output 2.
22
33
  const outputs = [
23
- result.publications.length ? result.publications.map((publication) => ({ ...publication })) : null,
34
+ side.publish(result.publications),
24
35
  result.readings.length ? result.readings.map((reading) => ({ payload: undefined, ...reading })) : null
25
36
  ];
26
37
  if (outputs.some((output) => output !== null))
@@ -33,6 +44,12 @@ const registerZ2mCover = (RED) => {
33
44
  const where = last.position !== undefined ? `${last.position}%` : last.state ?? "position unknown";
34
45
  node.status({ fill: "green", shape: "dot", text: last.motion && last.motion !== "stopped" ? `${last.motion} · ${where}` : where });
35
46
  }
47
+ }
48
+ (0, adapter_1.onInput)(node, (msg, send) => {
49
+ // With a broker, its device's messages come from the broker: an MQTT
50
+ // in or a Status node still wired in changes nothing.
51
+ if (!side.fromBroker(msg))
52
+ handle(msg, send);
36
53
  });
37
54
  }
38
55
  RED.nodes.registerType("home-z2m-cover", HomeZ2mCover);
@@ -3,7 +3,21 @@
3
3
  category: "home automation",
4
4
  color: "#e2d96e",
5
5
  defaults: {
6
- name: {value: ""},
6
+ // A marker only a node new from the palette has, as core nodes do: import
7
+ // and paste keep the saved name, so onadd leaves a saved node's settings.
8
+ name: {value: "_DEFAULT_"},
9
+ // Node-RED's MQTT broker node, through which it subscribes to its device's
10
+ // topics and publishes on them itself. The editor offers the first broker
11
+ // when the dialog is opened and saved, not when the node is dropped.
12
+ broker: {value: "", type: "mqtt-broker", required: false, validate: function (v) {
13
+ return (Boolean(v) && v !== "_ADD_") || this.publishesThroughOutput !== false ? true : "choose a broker";
14
+ }},
15
+ // Whether it reaches its device through MQTT in and out nodes, as before
16
+ // 6.2, and so needs no broker until one is chosen. A node saved before
17
+ // this setting loads without it (the editor applies no defaults to the
18
+ // deployed flows), which counts as true; a new node sets false (onadd).
19
+ // No row.
20
+ publishesThroughOutput: {value: false},
7
21
  // The room's id for this light: no default, it names the user's own.
8
22
  target: {value: "", required: true, validate: function (v) { return String(v || "").trim() !== ""; }},
9
23
  baseTopic: {value: "zigbee2mqtt", required: true, validate: function (v) {
@@ -17,9 +31,31 @@
17
31
  }},
18
32
  group: {value: false}
19
33
  },
34
+ onadd: function () {
35
+ if (this.name !== "_DEFAULT_") return;
36
+ this.name = "";
37
+ this.publishesThroughOutput = false;
38
+ },
39
+ oneditprepare: function () {
40
+ var node = this;
41
+ // While it reaches its device through MQTT in and out nodes, that it does
42
+ // until given a broker.
43
+ function rows() {
44
+ var broker = $("#node-input-broker").val();
45
+ $(".device-through-output").toggle(node.publishesThroughOutput !== false && (!broker || broker === "_ADD_"));
46
+ }
47
+ $("#node-input-broker").on("change", rows);
48
+ rows();
49
+ },
50
+ oneditsave: function () {
51
+ // A broker chosen: it publishes itself from now on.
52
+ var broker = $("#node-input-broker").val();
53
+ if (broker && broker !== "_ADD_") this.publishesThroughOutput = false;
54
+ else if (this.publishesThroughOutput !== false) this.publishesThroughOutput = true;
55
+ },
20
56
  inputs: 1,
21
57
  outputs: 2,
22
- outputLabels: ["to the light (MQTT out)", "to the room lighting controller"],
58
+ outputLabels: ["to the light (MQTT out, without a broker)", "to the room lighting controller"],
23
59
  icon: "font-awesome/fa-lightbulb-o",
24
60
  label: function () { return this.name || this.friendlyName || "zigbee2mqtt light"; },
25
61
  paletteLabel: "zigbee2mqtt light"
@@ -28,6 +64,11 @@
28
64
 
29
65
  <script type="text/html" data-template-name="home-z2m-light">
30
66
  <div class="form-row"><label for="node-input-name">Name</label><input type="text" id="node-input-name"></div>
67
+ <div class="form-row"><label for="node-input-broker">Broker</label><input type="text" id="node-input-broker"></div>
68
+ <div class="form-tips">With a broker, it subscribes to its device's topics and publishes on them itself: the MQTT in and
69
+ MQTT out nodes around it are no longer needed and can be deleted.</div>
70
+ <div class="form-tips device-through-output">This node reaches its device through MQTT in and out nodes, as before 6.2,
71
+ and keeps working that way. Choose a Broker to let it do so itself.</div>
31
72
  <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
73
  <div class="form-row"><label for="node-input-baseTopic">Base topic</label><input type="text" id="node-input-baseTopic"></div>
33
74
  <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>
@@ -37,7 +78,11 @@
37
78
  <script type="text/html" data-help-name="home-z2m-light">
38
79
  <p>Connects one Zigbee2MQTT light, or a Zigbee2MQTT group of lights, to a <b>room lighting controller</b>, over
39
80
  Zigbee2MQTT's MQTT messages: no function nodes, and never Home Assistant. Use one per light in the room's targets.</p>
81
+ <p>The devices: <a href="https://www.zigbee2mqtt.io/supported-devices/" target="_blank">Zigbee2MQTT's supported devices</a>.</p>
40
82
  <h3>Wiring</h3>
83
+ <p>With a <b>Broker</b>, it subscribes to <code>&lt;base&gt;/&lt;friendly name&gt;</code>, its <code>/set</code> and its <code>/availability</code> (exactly these: a friendly name can contain <code>/</code>) and publishes on its device's topics itself: leave out the MQTT
84
+ in and MQTT out nodes below (still wired, they change nothing). Its first output then sends nothing. It reads its light on each connect (not a group, which Zigbee2MQTT can't be asked), as a Status node's startup did through the room. A node saved
85
+ before 6.2 keeps using them until you choose a broker.</p>
41
86
  <ol>
42
87
  <li>An <b>MQTT in</b> node subscribed to <code>&lt;base&gt;/&lt;friendly name&gt;/#</code> (output: auto-detect)
43
88
  → this node. That covers the light's state, its <code>/set</code> topic (to see other people's commands) and its
@@ -64,7 +109,8 @@
64
109
  <p>Switch on the group's <b>retain</b> in Zigbee2MQTT: <code>retain: true</code> under the group in Zigbee2MQTT's
65
110
  <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
111
  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>
112
+ gives this node the group's last state as soon as it (or its MQTT in node) subscribes. A group that has had no state 30 seconds
113
+ after it started shows <i>group · no state yet: set retain: true for it in Zigbee2MQTT</i> on its status.</p>
68
114
  <h3>Settings</h3>
69
115
  <dl class="message-properties">
70
116
  <dt>Target</dt><dd>The light's id in the room controller's Targets JSON.</dd>
@@ -1,13 +1,19 @@
1
1
  "use strict";
2
2
  const z2m_light_adapter_1 = require("../lib/z2m-light-adapter");
3
3
  const adapter_1 = require("./adapter");
4
+ const device_side_1 = require("./device-side");
5
+ // How long a group waits for its state before its status says why it may
6
+ // have none.
7
+ const GROUP_HINT_MS = 30_000;
4
8
  const registerZ2mLight = (RED) => {
5
9
  function HomeZ2mLight(config) {
6
10
  RED.nodes.createNode(this, config);
7
11
  const node = this;
8
12
  let light;
13
+ let broker;
9
14
  try {
10
15
  light = (0, z2m_light_adapter_1.normaliseZ2mLightConfig)(config);
16
+ broker = (0, device_side_1.deviceBroker)(RED, config.broker);
11
17
  }
12
18
  catch (error) {
13
19
  (0, adapter_1.invalidConfig)(node, error);
@@ -15,7 +21,29 @@ const registerZ2mLight = (RED) => {
15
21
  }
16
22
  const trace = { area: "", component: "z2m-light" };
17
23
  node.status({ fill: "grey", shape: "ring", text: light.group ? "group · waiting for its state" : "waiting for the device" });
18
- (0, adapter_1.onInput)(node, (msg, send) => {
24
+ // A group can't be asked for its state, so after a restart it has none
25
+ // until it changes, unless Zigbee2MQTT retains it: say so if nothing
26
+ // has come by then.
27
+ let reported = false;
28
+ const hint = light.group ? setTimeout((0, adapter_1.guarded)(node, () => {
29
+ if (!reported)
30
+ node.status({ fill: "yellow", shape: "ring", text: "group · no state yet: set retain: true for it in Zigbee2MQTT" });
31
+ }), GROUP_HINT_MS) : undefined;
32
+ node.on("close", (_removed, done) => {
33
+ clearTimeout(hint);
34
+ done();
35
+ });
36
+ // With a broker, it reads its device on each connect, reconnects
37
+ // included, through its own observe: after a restart nothing reported it
38
+ // while it was away, and the controller acts on the reading as on one it
39
+ // asked for.
40
+ // Exactly its own topics: a friendly name can contain "/", so <name>/#
41
+ // would take in other devices' too.
42
+ const topics = (0, z2m_light_adapter_1.z2mLightTopics)(light);
43
+ const side = (0, device_side_1.deviceSide)(node, broker, topics.device, [topics.device, topics.set, ...(light.group ? [] : [topics.availability])], trace, (msg, send) => handle(msg, send),
44
+ // A group can't be asked (traced, as for the room's observe).
45
+ () => handle({ light: { kind: "observe", target: light.target } }, node.send.bind(node)));
46
+ function handle(msg, send) {
19
47
  const result = (0, adapter_1.runReducer)(node, trace, (saved, now) => {
20
48
  const decided = (0, z2m_light_adapter_1.transitionZ2mLight)(saved, msg, light, now);
21
49
  // Another light's command: nothing to keep or trace, and the result
@@ -24,14 +52,16 @@ const registerZ2mLight = (RED) => {
24
52
  });
25
53
  if (result.ignored)
26
54
  return;
27
- // MQTT publications for the device on output 1, for an MQTT out node;
28
- // readings for the room controller on output 2.
55
+ // MQTT publications for the device: to the broker, or on output 1 for
56
+ // an MQTT out node; readings for the room controller on output 2.
29
57
  const outputs = [
30
- result.publications.length ? result.publications.map((publication) => ({ ...publication })) : null,
58
+ side.publish(result.publications),
31
59
  result.readings.length ? result.readings.map((reading) => ({ payload: undefined, light: reading })) : null
32
60
  ];
33
61
  if (outputs.some((output) => output !== null))
34
62
  send(outputs);
63
+ if (result.readings.some((reading) => reading.kind === "observed"))
64
+ reported = true;
35
65
  const state = result.state;
36
66
  if (state.online === false) {
37
67
  node.status({ fill: "grey", shape: "ring", text: "offline" });
@@ -43,6 +73,12 @@ const registerZ2mLight = (RED) => {
43
73
  ].filter(Boolean).join(" ");
44
74
  node.status({ fill: state.report.on ? "green" : "grey", shape: "dot", text: (state.report.on ? "on" : "off") + (state.report.on && values ? " · " + values : "") });
45
75
  }
76
+ }
77
+ (0, adapter_1.onInput)(node, (msg, send) => {
78
+ // With a broker, its device's messages come from the broker: an MQTT
79
+ // in or a Status node still wired in changes nothing.
80
+ if (!side.fromBroker(msg))
81
+ handle(msg, send);
46
82
  });
47
83
  }
48
84
  RED.nodes.registerType("home-z2m-light", HomeZ2mLight);
@@ -0,0 +1,3 @@
1
+ import type { NodeInitializer } from "node-red";
2
+ declare const registerZ2mPower: NodeInitializer;
3
+ export = registerZ2mPower;
@@ -0,0 +1,58 @@
1
+ <script type="text/javascript">
2
+ RED.nodes.registerType("home-z2m-power", {
3
+ category: "home automation",
4
+ color: "#e2d96e",
5
+ defaults: {
6
+ name: {value: ""},
7
+ // Node-RED's MQTT broker node, through which it subscribes to its
8
+ // device's topics. The editor offers the first broker when the dialog is
9
+ // opened and saved.
10
+ broker: {value: "", type: "mqtt-broker", required: true},
11
+ baseTopic: {value: "zigbee2mqtt", required: true, validate: function (v) {
12
+ var base = String(v || "").trim().replace(/^\/+|\/+$/g, "");
13
+ return base !== "" && !/[+#]/.test(base);
14
+ }},
15
+ // No default: it names the user's own device.
16
+ friendlyName: {value: "", required: true, validate: function (v) {
17
+ var name = String(v || "").trim().replace(/^\/+|\/+$/g, "");
18
+ return name !== "" && !/[+#]/.test(name);
19
+ }}
20
+ },
21
+ inputs: 1,
22
+ outputs: 1,
23
+ outputLabels: ["power readings (msg.power), to a display controller or a scene"],
24
+ icon: "font-awesome/fa-bolt",
25
+ label: function () { return this.name || this.friendlyName || "zigbee2mqtt power"; },
26
+ paletteLabel: "zigbee2mqtt power"
27
+ });
28
+ </script>
29
+
30
+ <script type="text/html" data-template-name="home-z2m-power">
31
+ <div class="form-row"><label for="node-input-name">Name</label><input type="text" id="node-input-name"></div>
32
+ <div class="form-row"><label for="node-input-broker">Broker</label><input type="text" id="node-input-broker"></div>
33
+ <div class="form-row"><label for="node-input-baseTopic">Base topic</label><input type="text" id="node-input-baseTopic"></div>
34
+ <div class="form-row"><label for="node-input-friendlyName">Friendly name</label><input type="text" id="node-input-friendlyName" placeholder="the plug's name in Zigbee2MQTT"></div>
35
+ <div class="form-tips">It subscribes to the plug's topic and its availability itself: no MQTT in or change nodes.</div>
36
+ </script>
37
+
38
+ <script type="text/html" data-help-name="home-z2m-power">
39
+ <p>Reads a Zigbee2MQTT plug or meter's power, for whatever judges it: a <b>home display mode controller</b> (its
40
+ projector's power) or a <b>home scene</b> (its <b>End below</b>). Over Zigbee2MQTT's MQTT messages: no function or
41
+ change nodes, and never Home Assistant.</p>
42
+ <p>The devices: <a href="https://www.zigbee2mqtt.io/supported-devices/" target="_blank">Zigbee2MQTT's supported devices</a>.</p>
43
+ <h3>Wiring</h3>
44
+ <ol>
45
+ <li>Its output → a display controller's input (as its projector's power) or a scene's input (for End below).</li>
46
+ <li>Optional: a display controller's third output (its reading requests) → this node.</li>
47
+ </ol>
48
+ <p>Choose the <b>Broker</b> Zigbee2MQTT uses, and set the <b>Friendly name</b> (and the <b>Base topic</b> if you changed
49
+ Zigbee2MQTT's). It subscribes to <code>&lt;base&gt;/&lt;friendly name&gt;</code> and its <code>/availability</code>.</p>
50
+ <h3>Readings</h3>
51
+ <ul>
52
+ <li>Output: <code>msg.power = {kind: "observed", watts, observedAt}</code> (and <code>msg.payload</code> the watts),
53
+ from the plug's <code>power</code> only, and only when it changes. A retained (old) report is never passed on.</li>
54
+ <li>Input: <code>msg.power = {kind: "read"}</code> asks the plug for its power (<code>/get</code>), as it also does
55
+ each time the broker connects. Plugs that don't answer report on their next change.</li>
56
+ <li>Availability shows on the status (<i>offline</i>); nothing is sent for it.</li>
57
+ </ul>
58
+ </script>
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ const z2m_power_1 = require("../lib/z2m-power");
3
+ const adapter_1 = require("./adapter");
4
+ const device_side_1 = require("./device-side");
5
+ const registerZ2mPower = (RED) => {
6
+ function HomeZ2mPower(config) {
7
+ RED.nodes.createNode(this, config);
8
+ const node = this;
9
+ let device;
10
+ let broker;
11
+ try {
12
+ device = (0, z2m_power_1.normaliseZ2mPowerConfig)(config);
13
+ // Newer than the MQTT in and out way of wiring adapters: it needs its
14
+ // broker.
15
+ broker = (0, device_side_1.deviceBroker)(RED, config.broker);
16
+ if (!broker)
17
+ throw new Error("choose its MQTT broker");
18
+ }
19
+ catch (error) {
20
+ (0, adapter_1.invalidConfig)(node, error);
21
+ return;
22
+ }
23
+ const trace = { area: "", component: "z2m-power" };
24
+ node.status({ fill: "grey", shape: "ring", text: "waiting for the device" });
25
+ // Exactly its own topics: a friendly name can contain "/". On each
26
+ // connect it asks the plug for its power, which plugs that support
27
+ // /get answer; the rest report on their next change.
28
+ const topics = (0, z2m_power_1.z2mPowerTopics)(device);
29
+ const side = (0, device_side_1.deviceSide)(node, broker, topics.device, [topics.device, topics.availability], trace, (msg, send) => handle(msg, send), () => handle({ power: { kind: "read" } }, node.send.bind(node)));
30
+ function handle(msg, send) {
31
+ const result = (0, adapter_1.runReducer)(node, trace, (saved, now) => (0, z2m_power_1.transitionZ2mPower)(saved, msg, device, now));
32
+ // Its read requests to the plug through the broker; its readings, as
33
+ // msg.power, on its output.
34
+ side.publish(result.publications);
35
+ if (result.readings.length)
36
+ send([result.readings.map((reading) => ({ payload: reading.power.watts, ...reading }))]);
37
+ const state = result.state;
38
+ if (state.online === false)
39
+ node.status({ fill: "grey", shape: "ring", text: "offline" });
40
+ else if (state.watts !== null)
41
+ node.status({ fill: state.watts > 0 ? "green" : "grey", shape: "dot", text: `${state.watts} W` });
42
+ }
43
+ (0, adapter_1.onInput)(node, (msg, send) => {
44
+ // Its device's messages come from the broker: an MQTT in node wired in
45
+ // changes nothing.
46
+ if (!side.fromBroker(msg))
47
+ handle(msg, send);
48
+ });
49
+ }
50
+ RED.nodes.registerType("home-z2m-power", HomeZ2mPower);
51
+ };
52
+ module.exports = registerZ2mPower;
53
+ //# sourceMappingURL=z2m-power.js.map
@@ -0,0 +1,3 @@
1
+ import type { NodeInitializer } from "node-red";
2
+ declare const registerZ2mSensor: NodeInitializer;
3
+ export = registerZ2mSensor;
@@ -0,0 +1,69 @@
1
+ <script type="text/javascript">
2
+ RED.nodes.registerType("home-z2m-sensor", {
3
+ category: "home automation",
4
+ color: "#e2d96e",
5
+ defaults: {
6
+ name: {value: ""},
7
+ // Node-RED's MQTT broker node, through which it subscribes to its
8
+ // device's topics. The editor offers the first broker when the dialog is
9
+ // opened and saved.
10
+ broker: {value: "", type: "mqtt-broker", required: true},
11
+ baseTopic: {value: "zigbee2mqtt", required: true, validate: function (v) {
12
+ var base = String(v || "").trim().replace(/^\/+|\/+$/g, "");
13
+ return base !== "" && !/[+#]/.test(base);
14
+ }},
15
+ // No default: it names the user's own device.
16
+ friendlyName: {value: "", required: true, validate: function (v) {
17
+ var name = String(v || "").trim().replace(/^\/+|\/+$/g, "");
18
+ return name !== "" && !/[+#]/.test(name);
19
+ }},
20
+ // What the device's "occupancy" is: a PIR's motion, or on some mmWave
21
+ // sensors, presence.
22
+ occupancyIs: {value: "motion", required: true, validate: function (v) { return v === "motion" || v === "presence"; }}
23
+ },
24
+ inputs: 1,
25
+ outputs: 2,
26
+ outputLabels: ["motion and presence, to occupancy fusion", "light level, to the motion lighting policy"],
27
+ icon: "font-awesome/fa-eye",
28
+ label: function () { return this.name || this.friendlyName || "zigbee2mqtt sensor"; },
29
+ paletteLabel: "zigbee2mqtt sensor"
30
+ });
31
+ </script>
32
+
33
+ <script type="text/html" data-template-name="home-z2m-sensor">
34
+ <div class="form-row"><label for="node-input-name">Name</label><input type="text" id="node-input-name"></div>
35
+ <div class="form-row"><label for="node-input-broker">Broker</label><input type="text" id="node-input-broker"></div>
36
+ <div class="form-row"><label for="node-input-baseTopic">Base topic</label><input type="text" id="node-input-baseTopic"></div>
37
+ <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>
38
+ <div class="form-row"><label for="node-input-occupancyIs">Occupancy is</label><select id="node-input-occupancyIs">
39
+ <option value="motion">motion (a PIR sensor)</option>
40
+ <option value="presence">presence (an mmWave sensor that reports it as occupancy)</option>
41
+ </select></div>
42
+ <div class="form-tips">It subscribes to the device's topic and its availability itself: no MQTT in or change nodes.</div>
43
+ </script>
44
+
45
+ <script type="text/html" data-help-name="home-z2m-sensor">
46
+ <p>Connects a Zigbee2MQTT motion, presence or light sensor to a room: its reports become the messages <b>home occupancy
47
+ fusion</b> and the <b>home motion lighting policy</b> take, over Zigbee2MQTT's MQTT messages: no function or change
48
+ nodes, and never Home Assistant.</p>
49
+ <p>The devices: <a href="https://www.zigbee2mqtt.io/supported-devices/" target="_blank">Zigbee2MQTT's supported devices</a>.</p>
50
+ <h3>Wiring</h3>
51
+ <ol>
52
+ <li>Output 1 (motion and presence) → occupancy fusion.</li>
53
+ <li>Output 2 (light level) → the motion lighting policy.</li>
54
+ </ol>
55
+ <p>Choose the <b>Broker</b> Zigbee2MQTT uses, and set the <b>Friendly name</b> (and the <b>Base topic</b> if you changed
56
+ Zigbee2MQTT's). It subscribes to <code>&lt;base&gt;/&lt;friendly name&gt;</code> and its <code>/availability</code>.</p>
57
+ <h3>Readings</h3>
58
+ <ul>
59
+ <li><code>occupancy</code> → <code>{source: "motion", payload: true | false}</code> on output 1, or <code>"presence"</code>
60
+ with <b>Occupancy is</b> set to presence; <code>presence</code> → <code>{source: "presence", payload}</code>.</li>
61
+ <li><code>illuminance_lux</code>, or else <code>illuminance</code>, → <code>{source: "illuminance", payload: &lt;lux&gt;}</code>
62
+ on output 2.</li>
63
+ <li>Only a value that changed is sent, so a battery or temperature report sends nothing.</li>
64
+ <li>A retained occupancy or presence (with retain set for the device in Zigbee2MQTT) is never acted on: after a
65
+ restart it is an old reading. The next report the sensor sends is passed on.</li>
66
+ <li>Availability shows on the status (<i>offline</i>); nothing is sent for it. Battery sensors can't be asked for a
67
+ reading, so after a restart the room learns the sensor's state on its next change.</li>
68
+ </ul>
69
+ </script>
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ const z2m_sensor_1 = require("../lib/z2m-sensor");
3
+ const adapter_1 = require("./adapter");
4
+ const device_side_1 = require("./device-side");
5
+ const registerZ2mSensor = (RED) => {
6
+ function HomeZ2mSensor(config) {
7
+ RED.nodes.createNode(this, config);
8
+ const node = this;
9
+ let device;
10
+ let broker;
11
+ try {
12
+ device = (0, z2m_sensor_1.normaliseZ2mSensorConfig)(config);
13
+ // Newer than the MQTT in and out way of wiring adapters: it needs its
14
+ // broker.
15
+ broker = (0, device_side_1.deviceBroker)(RED, config.broker);
16
+ if (!broker)
17
+ throw new Error("choose its MQTT broker");
18
+ }
19
+ catch (error) {
20
+ (0, adapter_1.invalidConfig)(node, error);
21
+ return;
22
+ }
23
+ const trace = { area: "", component: "z2m-sensor" };
24
+ node.status({ fill: "grey", shape: "ring", text: "waiting for the device" });
25
+ // Exactly its own topics: a friendly name can contain "/". Battery
26
+ // sensors can't be asked for a reading, so there is nothing to read on
27
+ // connect: the room learns the sensor's state on its next change.
28
+ const topics = (0, z2m_sensor_1.z2mSensorTopics)(device);
29
+ const side = (0, device_side_1.deviceSide)(node, broker, topics.device, [topics.device, topics.availability], trace, (msg, send) => handle(msg, send));
30
+ function handle(msg, send) {
31
+ const result = (0, adapter_1.runReducer)(node, trace, (saved) => (0, z2m_sensor_1.transitionZ2mSensor)(saved, msg, device));
32
+ // Output 1: motion and presence, for occupancy fusion. Output 2: the
33
+ // light level, for the motion lighting policy.
34
+ const outputs = [
35
+ result.occupancy.length ? result.occupancy.map((reading) => ({ ...reading })) : null,
36
+ result.light.length ? result.light.map((reading) => ({ ...reading })) : null
37
+ ];
38
+ if (outputs.some((output) => output !== null))
39
+ send(outputs);
40
+ const state = result.state;
41
+ if (state.online === false) {
42
+ node.status({ fill: "grey", shape: "ring", text: "offline" });
43
+ }
44
+ else {
45
+ const occupied = state.motion === true || state.presence === true;
46
+ const known = state.motion !== null || state.presence !== null;
47
+ const parts = [known ? (occupied ? "occupied" : "clear") : "", state.illuminance !== null ? `${state.illuminance} lx` : ""].filter(Boolean);
48
+ if (parts.length)
49
+ node.status({ fill: occupied ? "green" : "grey", shape: "dot", text: parts.join(" · ") });
50
+ }
51
+ }
52
+ (0, adapter_1.onInput)(node, (msg, send) => {
53
+ // Its device's messages come from the broker: an MQTT in node wired in
54
+ // changes nothing.
55
+ if (!side.fromBroker(msg))
56
+ handle(msg, send);
57
+ });
58
+ }
59
+ RED.nodes.registerType("home-z2m-sensor", HomeZ2mSensor);
60
+ };
61
+ module.exports = registerZ2mSensor;
62
+ //# sourceMappingURL=z2m-sensor.js.map