node-red-contrib-unifi-ultimate 0.1.5 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,10 +2,6 @@
2
2
  <img src="nodes/icons/unifi-ultimate-logo.svg" alt="Unifi Ultimate" width="720">
3
3
  </p>
4
4
 
5
- <p align="center">
6
- <img src="nodes/icons/readme-overview.svg" alt="Overview of Node-RED UniFi Ultimate architecture" width="980">
7
- </p>
8
-
9
5
  [![NPM version][npm-version-image]][npm-url]
10
6
  [![Node.js version][node-version-image]][npm-url]
11
7
  [![Node-RED Flow Library][flows-image]][flows-url]
@@ -19,16 +15,9 @@
19
15
 
20
16
  Control and monitor `UniFi Network`, `UniFi Protect`, and `UniFi Access` from Node-RED without building API requests by hand.
21
17
 
22
- [View Changelog](CHANGELOG.md)
18
+ > **Beta notice:** until version `1.0.0`, this package should be considered **BETA** and may include **breaking changes** between releases.
23
19
 
24
- ## Why Use It
25
-
26
- - Manage UniFi cameras, sensors, doors, clients, switches, and PoE ports from Node-RED.
27
- - Pick devices and clients from searchable lists instead of copying IDs manually.
28
- - Trigger configured actions with any incoming message.
29
- - Receive live events from Protect and Access.
30
- - Build presence, door, camera, and PoE automations with less custom code.
31
- - Use one package for the main UniFi applications.
20
+ [View Changelog](CHANGELOG.md)
32
21
 
33
22
  ## Install
34
23
 
@@ -51,18 +40,7 @@ In Node-RED:
51
40
 
52
41
  The incoming message is only a trigger. The node uses the item and action configured in the editor.
53
42
 
54
- ## Available Nodes
55
-
56
- | Node | What it does |
57
- |---|---|
58
- | `Unifi Network Config` | Connection settings for UniFi Network |
59
- | `Unifi Network Device` | Read sites, devices, clients, and run supported Network actions |
60
- | `Unifi Network Presence` | Track whether a selected client is present |
61
- | `Unifi Network Control POE` | Enable, disable, or power-cycle PoE on a switch port |
62
- | `Unifi Protect Config` | Connection settings for UniFi Protect |
63
- | `Unifi Protect Device` | Read cameras/sensors/devices, receive events, and run Protect actions |
64
- | `Unifi Access Config` | Connection settings for UniFi Access |
65
- | `Unifi Access Device` | Read doors/devices, receive events, and run Access actions |
43
+ |
66
44
 
67
45
  ## UniFi Network
68
46
 
@@ -154,26 +132,163 @@ Common uses:
154
132
 
155
133
  Most nodes send the result on output 1.
156
134
 
157
- Protect and Access device nodes can also send live events on output 2 when `Receive Events` is selected.
135
+ Protect and Access device nodes emit both state and live event messages on the same output pin when `Receive Events` is selected.
158
136
 
159
137
  Useful metadata is attached to the output message, for example:
160
138
 
161
- - `msg.unifiNetwork`
162
- - `msg.unifiNetworkPresence`
163
- - `msg.unifiNetworkPoe`
164
- - `msg.unifiProtect`
165
- - `msg.unifiAccess`
139
+ - `msg.topic` (node `Name` from the editor)
140
+ - `msg.deviceName` (remembered/observed client or device name)
141
+ - `msg.eventName` (event/trigger that produced the output message)
142
+ - `msg.details.unifiNetwork`
143
+ - `msg.details.unifiNetworkPresence`
144
+ - `msg.details.unifiNetworkPoe`
145
+ - `msg.details.unifiProtect`
146
+ - `msg.details.unifiAccess`
147
+
148
+ When `msg.payload` is an object, it also includes `payload.deviceName` with the same value.
149
+
150
+ ## Output Examples
151
+
152
+ ### Unifi Network Device
153
+
154
+ ```json
155
+ {
156
+ "topic": "Switch Soffitta",
157
+ "deviceName": "Soffitta",
158
+ "eventName": "request:readState",
159
+ "payload": {
160
+ "id": "8c072de0-d71d-37bd-a6f5-eac67c95a314",
161
+ "name": "Soffitta",
162
+ "state": "READY",
163
+ "deviceName": "Soffitta"
164
+ },
165
+ "details": {
166
+ "unifiNetwork": {
167
+ "nodeType": "device",
168
+ "deviceType": "device",
169
+ "capability": "readState",
170
+ "source": "request"
171
+ }
172
+ }
173
+ }
174
+ ```
175
+
176
+ ### Unifi Network Presence
177
+
178
+ ```json
179
+ {
180
+ "topic": "iPhone Massimo Presence",
181
+ "deviceName": "iPhone-Massimo",
182
+ "eventName": "connected",
183
+ "payload": true,
184
+ "present": true,
185
+ "details": {
186
+ "unifiNetworkPresence": {
187
+ "nodeType": "presence",
188
+ "source": "poll",
189
+ "reason": "connected"
190
+ }
191
+ }
192
+ }
193
+ ```
194
+
195
+ ### Unifi Network Control POE
196
+
197
+ ```json
198
+ {
199
+ "topic": "POE Soffitta Port 2",
200
+ "deviceName": "Soffitta",
201
+ "eventName": "request:enable",
202
+ "payload": {
203
+ "status": "ok",
204
+ "deviceName": "Soffitta",
205
+ "portIdx": 2,
206
+ "portName": "Port 2",
207
+ "portPowerW": 2.6,
208
+ "powerConsumptionSwitchTotal": 36.4
209
+ },
210
+ "details": {
211
+ "unifiNetworkPoe": {
212
+ "nodeType": "poe-control",
213
+ "action": "enable",
214
+ "portIdx": 2,
215
+ "portPowerW": 2.6,
216
+ "powerConsumptionSwitchTotal": 36.4
217
+ }
218
+ }
219
+ }
220
+ ```
221
+
222
+ Power fields for `Unifi Network Control POE`:
223
+
224
+ | Field | Type | Guaranteed | Notes |
225
+ | ------------------------------------------ | -------- | ---------- | --------------------------------------------------------------------- |
226
+ | `msg.payload.portIdx` | number | yes | Selected port index. |
227
+ | `msg.payload.portName` | string | yes | Selected port display name. |
228
+ | `msg.payload.portPowerW` | number | no | Current PoE power draw for the selected port (W), when UniFi exposes it. |
229
+ | `msg.payload.powerConsumptionSwitchTotal` | number | no | Sum of all port PoE consumptions on the switch (W), when available. |
230
+ | `msg.details.unifiNetworkPoe.portPowerW` | number | no | Same value as `msg.payload.portPowerW`. |
231
+ | `msg.details.unifiNetworkPoe.powerConsumptionSwitchTotal` | number | no | Same value as `msg.payload.powerConsumptionSwitchTotal`. |
232
+
233
+ ### Unifi Protect Device
234
+
235
+ ```json
236
+ {
237
+ "topic": "Front Door Camera",
238
+ "deviceName": "Front Door Camera",
239
+ "eventName": "smartDetectZone",
240
+ "payload": {
241
+ "device": {
242
+ "id": "camera-id"
243
+ },
244
+ "event": {
245
+ "type": "smartDetectZone"
246
+ },
247
+ "deviceName": "Front Door Camera"
248
+ },
249
+ "details": {
250
+ "unifiProtect": {
251
+ "nodeType": "device",
252
+ "deviceType": "camera",
253
+ "capability": "observe",
254
+ "source": "events"
255
+ }
256
+ }
257
+ }
258
+ ```
259
+
260
+ ### Unifi Access Device
261
+
262
+ ```json
263
+ {
264
+ "topic": "Main Door",
265
+ "deviceName": "Main Door",
266
+ "eventName": "door.unlock",
267
+ "payload": {
268
+ "event": "door.unlock",
269
+ "deviceName": "Main Door"
270
+ },
271
+ "details": {
272
+ "unifiAccess": {
273
+ "nodeType": "device",
274
+ "deviceType": "door",
275
+ "capability": "observe",
276
+ "source": "events"
277
+ }
278
+ }
279
+ }
280
+ ```
166
281
 
167
282
  ## Example Flows
168
283
 
169
284
  Import from `examples/`:
170
285
 
171
- | Flow file | What it demonstrates |
172
- |---|---|
173
- | [examples/unifi-protect-info.json](examples/unifi-protect-info.json) | Read Protect camera state |
174
- | [examples/unifi-protect-sensor-observe.json](examples/unifi-protect-sensor-observe.json) | Receive boolean sensor events |
175
- | [examples/unifi-protect-camera-actions.json](examples/unifi-protect-camera-actions.json) | Snapshot, PTZ presets, and doorbell messages |
176
- | [examples/unifi-access-door-control.json](examples/unifi-access-door-control.json) | Door state, unlock, and temporary lock rule |
286
+ | Flow file | What it demonstrates |
287
+ | -------------------------------------------------------------------------------------------- | ---------------------------------------------- |
288
+ | [examples/unifi-protect-info.json](examples/unifi-protect-info.json) | Read Protect camera state |
289
+ | [examples/unifi-protect-sensor-observe.json](examples/unifi-protect-sensor-observe.json) | Receive boolean sensor events |
290
+ | [examples/unifi-protect-camera-actions.json](examples/unifi-protect-camera-actions.json) | Snapshot, PTZ presets, and doorbell messages |
291
+ | [examples/unifi-access-door-control.json](examples/unifi-access-door-control.json) | Door state, unlock, and temporary lock rule |
177
292
  | [examples/unifi-access-intercom-doorbell.json](examples/unifi-access-intercom-doorbell.json) | Intercom observe, trigger, and cancel doorbell |
178
293
 
179
294
  ## Notes
@@ -512,7 +512,11 @@ module.exports = function(RED) {
512
512
  node.doorbellLogPollCursor = Math.floor(Date.now() / 1000) - DOORBELL_LOG_POLL_LOOKBACK_SECONDS;
513
513
  node.runDoorbellLogPoll();
514
514
  node.doorbellLogPollTimer = setInterval(() => {
515
- node.runDoorbellLogPoll();
515
+ try {
516
+ node.runDoorbellLogPoll();
517
+ } catch (error) {
518
+ node.warn(`Access doorbell log poll timer failed: ${error && error.message ? error.message : error}`);
519
+ }
516
520
  }, DOORBELL_LOG_POLL_INTERVAL_MS);
517
521
  };
518
522
 
@@ -645,10 +649,13 @@ module.exports = function(RED) {
645
649
  });
646
650
 
647
651
  ws.on("close", () => {
648
- if (node.wsNotifications === ws) {
649
- node.wsNotifications = null;
652
+ try {
653
+ if (node.wsNotifications === ws) {
654
+ node.wsNotifications = null;
655
+ }
656
+ node.scheduleReconnect();
657
+ } catch (error) {
650
658
  }
651
- node.scheduleReconnect();
652
659
  });
653
660
 
654
661
  ws.on("error", () => {
@@ -702,13 +709,17 @@ module.exports = function(RED) {
702
709
  };
703
710
 
704
711
  node.on("close", function(done) {
705
- node.isClosing = true;
706
- node.activeDoorbells.clear();
707
- node.activeDoorbellRequests.clear();
708
- node.closeWebSocket();
709
- node.stopDoorbellLogPolling();
710
- if (typeof done === "function") {
711
- done();
712
+ try {
713
+ node.isClosing = true;
714
+ node.activeDoorbells.clear();
715
+ node.activeDoorbellRequests.clear();
716
+ node.closeWebSocket();
717
+ node.stopDoorbellLogPolling();
718
+ } catch (error) {
719
+ } finally {
720
+ if (typeof done === "function") {
721
+ done();
722
+ }
712
723
  }
713
724
  });
714
725
  }
@@ -78,19 +78,27 @@
78
78
  server: { value: "", type: "unifi-access-config", required: true },
79
79
  deviceType: { value: "", required: true },
80
80
  deviceId: { value: "", required: true },
81
+ deviceName: { value: "" },
82
+ inputs: { value: 1 },
81
83
  capability: { value: "observe", required: true },
82
84
  capabilityConfig: { value: "{}" },
83
85
  timeout: { value: 15000, required: true, validate: RED.validators.number() }
84
86
  },
85
87
  inputs: 1,
86
- outputs: 2,
88
+ outputs: 1,
87
89
  icon: "unifi-access-icon.svg",
88
90
  paletteLabel: "ACCESS",
89
- outputLabels: ["state", "events"],
91
+ outputLabels: ["result"],
90
92
  label: function () {
91
93
  return this.name || this.deviceId || "Unifi Access";
92
94
  },
93
95
  oneditsave: function () {
96
+ const selectedCapabilityId = String($("#node-input-capability").val() || this.capability || "observe").trim();
97
+ const selectedCapabilityOpensStream = $("#node-input-capability option:selected").data("opens-stream") === true
98
+ || $("#node-input-capability option:selected").attr("data-opens-stream") === "true";
99
+ // Hide the input pin only when the selected action starts an event stream.
100
+ this.inputs = (selectedCapabilityOpensStream || selectedCapabilityId === "observe") ? 0 : 1;
101
+
94
102
  const $capabilityConfig = $("#node-input-capabilityConfig");
95
103
  const capabilityConfig = {};
96
104
  if ($capabilityConfig.length === 0) {
@@ -140,6 +148,7 @@
140
148
  const $server = $("#node-input-server");
141
149
  const $deviceType = $("#node-input-deviceType");
142
150
  const $deviceId = $("#node-input-deviceId");
151
+ const $deviceName = $("#node-input-deviceName");
143
152
  const $capability = $("#node-input-capability");
144
153
  const $capabilityConfig = $("#node-input-capabilityConfig");
145
154
  const $capabilityConfigRow = $("#node-row-capabilityConfig");
@@ -398,6 +407,7 @@
398
407
  return false;
399
408
  }
400
409
  if (ui.item) {
410
+ $select.data("unifi-user-selection", true);
401
411
  setSelectedValue(ui.item.optionValue, true);
402
412
  }
403
413
  return true;
@@ -571,6 +581,14 @@
571
581
  $name.val(selectedDeviceName);
572
582
  }
573
583
 
584
+ function rememberSelectedDeviceName() {
585
+ const selectedDeviceName = getSelectedDeviceName();
586
+ if (!selectedDeviceName) {
587
+ return;
588
+ }
589
+ $deviceName.val(selectedDeviceName);
590
+ }
591
+
574
592
  function renderDeviceTypes(items) {
575
593
  deviceTypeDefinitions = Array.isArray(items) ? items.slice() : [];
576
594
  const currentValue = getCurrentOrNodeValue($deviceType, node.deviceType, "");
@@ -605,6 +623,7 @@
605
623
  $capability.append(
606
624
  $("<option></option>")
607
625
  .val(item.id)
626
+ .attr("data-opens-stream", item.opensEventStream === true ? "true" : "false")
608
627
  .text(item.label)
609
628
  );
610
629
  });
@@ -857,7 +876,7 @@
857
876
  });
858
877
 
859
878
  makeSelectSearchable($deviceId, "Search devices");
860
- syncNameFromSelection();
879
+ rememberSelectedDeviceName();
861
880
  refreshSelectionSummary();
862
881
  }
863
882
 
@@ -1041,7 +1060,12 @@
1041
1060
  if (suspendChangeHandling || isInitializingEditor) {
1042
1061
  return;
1043
1062
  }
1044
- syncNameFromSelection();
1063
+ rememberSelectedDeviceName();
1064
+ const isUserSelection = $deviceId.data("unifi-user-selection") === true;
1065
+ $deviceId.removeData("unifi-user-selection");
1066
+ if (isUserSelection) {
1067
+ syncNameFromSelection();
1068
+ }
1045
1069
  capabilityConfig = {};
1046
1070
  writeCapabilityConfig();
1047
1071
  refreshAfterDeviceChange();
@@ -1106,6 +1130,7 @@
1106
1130
  <label><i class="fa fa-sliders"></i> Action options</label>
1107
1131
  <div id="node-input-capabilityConfigContainer" style="width:70%; display:inline-block;"></div>
1108
1132
  <input type="hidden" id="node-input-capabilityConfig">
1133
+ <input type="hidden" id="node-input-deviceName">
1109
1134
  </div>
1110
1135
  <div id="node-row-advanced" class="unifi-ultimate-advanced">
1111
1136
  <div class="form-row">
@@ -1128,22 +1153,43 @@
1128
1153
  <p><b>Action</b> only shows actions supported by the selected item.</p>
1129
1154
  <p><b>Action options</b> appears only when the selected action needs extra input, such as a temporary lock rule or a room name for doorbell actions.</p>
1130
1155
  <p>For <code>Set Temporary Lock Rule</code>, choose <code>Custom Duration</code> to reveal the minutes field. Legacy <code>ended_time</code> is still available only as fallback compatibility.</p>
1131
- <p><code>Receive Events</code> fetches the current state once and then listens to the official UniFi Access notifications WebSocket.</p>
1156
+ <p><code>Receive Events</code> fetches the current state once and then listens to the official UniFi Access notifications WebSocket. In <b>Action options</b>, <code>All</code> emits every event for the selected device/door. In this mode, the input pin is hidden and listening starts automatically after deploy/restart.</p>
1132
1157
  <p><code>Trigger Doorbell</code> and <code>Cancel Doorbell</code> use a manual <code>Room Name</code> field when needed. The node does not currently discover Intercom directory names automatically.</p>
1133
1158
  <p><code>Cancel Doorbell</code> uses a shared safe-cancel guard in the UniFi Access config node with a 3 minute tracking window. Tracking is updated by websocket events and a fallback system-log poller.</p>
1134
- <p><b>Name</b> is the node name shown in the editor and is updated automatically when you select a different item. <b>Timeout</b> controls the request timeout in milliseconds for direct API calls.</p>
1159
+ <p><b>Name</b> is the node name shown in the editor and is updated only when you manually select a different item from the list. <b>Timeout</b> controls the request timeout in milliseconds for direct API calls.</p>
1135
1160
 
1136
1161
  <h3>Input</h3>
1137
1162
  <ul>
1163
+ <li>When <code>Receive Events</code> is selected, the input pin is hidden.</li>
1138
1164
  <li>Any incoming message triggers the configured action.</li>
1139
1165
  <li>Input message properties are ignored by this node.</li>
1140
1166
  </ul>
1141
1167
 
1142
1168
  <h3>Output</h3>
1143
1169
  <ul>
1144
- <li>Output 1 emits a new message containing the current state or primary capability response.</li>
1145
- <li>Output 2: Access notification events related to the selected item</li>
1146
- <li><code>msg.unifiAccess</code>: standardized metadata for the device and selected capability</li>
1170
+ <li>A single output pin emits state and event messages for the selected item.</li>
1171
+ <li><code>msg.topic</code>: node name from the editor.</li>
1172
+ <li><code>msg.deviceName</code>: remembered/observed selected device name.</li>
1173
+ <li><code>msg.eventName</code>: trigger/event that produced the message.</li>
1174
+ <li><code>msg.details.unifiAccess</code>: standardized metadata for the device and selected capability.</li>
1147
1175
  <li>Safe-cancel skip: <code>msg.payload.skipped = true</code> when <code>Cancel Doorbell</code> is blocked because no active ring is currently tracked</li>
1148
1176
  </ul>
1177
+ <p>Example output:</p>
1178
+ <pre><code>{
1179
+ "topic": "Main Door",
1180
+ "deviceName": "Main Door",
1181
+ "eventName": "door.unlock",
1182
+ "payload": {
1183
+ "event": "door.unlock",
1184
+ "deviceName": "Main Door"
1185
+ },
1186
+ "details": {
1187
+ "unifiAccess": {
1188
+ "nodeType": "device",
1189
+ "deviceType": "door",
1190
+ "capability": "observe",
1191
+ "source": "events"
1192
+ }
1193
+ }
1194
+ }</code></pre>
1149
1195
  </script>