node-red-contrib-unifi-ultimate 0.1.10 → 0.1.11

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
@@ -23,8 +23,16 @@ Control and monitor `UniFi Network`, `UniFi Protect`, and `UniFi Access` from No
23
23
  <a href="https://www.youtube.com/watch?v=ZOq7M5mgUBk&list=PL9Yh1bjbLAYrWKtMlopN0swuQXbdJ8MFJ">
24
24
  <img src="resources/logos/youtube-icon.svg" alt="YouTube" width="24" height="18" align="absmiddle">
25
25
  Watch news and tutorials on YouTube
26
+ <br/>
27
+ <br/>
28
+ <p align="center">
29
+ <img src="nodes/readme-assets/unifi-editor-preview.jpeg" alt="UniFi Ultimate editor preview" width="860">
30
+ </p>
26
31
  </a>
27
32
 
33
+ <br/>
34
+ <br/>
35
+
28
36
  ## Install
29
37
 
30
38
  In Node-RED:
@@ -786,8 +786,8 @@
786
786
  if (field.helpText) {
787
787
  $wrapper.append(
788
788
  $("<div></div>")
789
- .addClass("form-tips")
790
- .css({ marginLeft: "152px" })
789
+ .addClass("form-tips unifi-ultimate-info-tip")
790
+ .css({ marginLeft: "0", width: "100%", boxSizing: "border-box" })
791
791
  .text(field.helpText)
792
792
  );
793
793
  }
@@ -1064,7 +1064,7 @@
1064
1064
  </div>
1065
1065
  <div class="form-row">
1066
1066
  <label for="node-input-deviceId"><i class="fa fa-dot-circle-o"></i> Device</label>
1067
- <select id="node-input-deviceId" style="width:70%;"></select>
1067
+ <select id="node-input-deviceId" style="width:62%;"></select>
1068
1068
  <a href="#" id="node-input-refreshDevices" class="editor-button" style="margin-left:6px;" title="Refresh devices" aria-label="Refresh devices">
1069
1069
  <i class="fa fa-refresh"></i>
1070
1070
  </a>
@@ -1074,7 +1074,7 @@
1074
1074
  <select id="node-input-capability"></select>
1075
1075
  </div>
1076
1076
  <div class="form-row" id="node-row-capabilityConfig" style="display:none;">
1077
- <label><i class="fa fa-sliders"></i> Action options</label>
1077
+ <label></label>
1078
1078
  <div id="node-input-capabilityConfigContainer" style="width:70%; display:inline-block;"></div>
1079
1079
  <input type="hidden" id="node-input-capabilityConfig">
1080
1080
  <input type="hidden" id="node-input-deviceName">
@@ -1100,9 +1100,9 @@
1100
1100
  <p><b>Connection</b> selects the shared UniFi Access config node.</p>
1101
1101
  <p><b>Control</b> chooses the Access category. <b>Device</b> chooses the specific door or Access device. Device selectors are searchable and open with the complete list before filtering.</p>
1102
1102
  <p><b>Action</b> only shows actions supported by the selected item.</p>
1103
- <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>
1103
+ <p>Additional fields appear only when the selected action needs extra input, such as a temporary lock rule or a room name for doorbell actions.</p>
1104
1104
  <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>
1105
- <p><code>Receive Events</code> fetches the current state once and then subscribes to events provided by the shared UniFi Access config node. The config node maintains the official notifications WebSocket and fans out events to runtime nodes. 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>
1105
+ <p><code>Receive Events</code> fetches the current state once and then subscribes to events provided by the shared UniFi Access config node. The config node maintains the official notifications WebSocket and fans out events to runtime nodes. In this mode, <code>All</code> emits every event for the selected device/door. The input pin is hidden and listening starts automatically after deploy/restart.</p>
1106
1106
  <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>
1107
1107
  <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>
1108
1108
  <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 API calls.</p>
@@ -100,5 +100,5 @@
100
100
  <p>This config node is used by Network Device, Network Presence, and Control POE nodes.</p>
101
101
  <p>All UniFi Network requests are executed by this config node. Runtime nodes subscribe through <code>addClient/removeClient</code> and receive updates/events from here.</p>
102
102
  <p>Event streams and observation timers are centralized here (shared scheduler per config node), so runtime nodes do not open their own websocket channels or polling timers.</p>
103
- <p>The node uses the official Network integration API where available. For switch port/client mapping and PoE enable/disable, it also uses the local Network legacy API endpoints exposed through the UniFi OS proxy.</p>
103
+ <p>The node uses the official Network integration API where available and falls back automatically to local legacy endpoints when the official <code>/v1</code> routes are not exposed (for example on older local releases). For switch port/client mapping and PoE enable/disable, legacy endpoints are also used where needed.</p>
104
104
  </script>
@@ -216,7 +216,9 @@ module.exports = function(RED) {
216
216
  });
217
217
 
218
218
  if (response.statusCode < 200 || response.statusCode >= 300) {
219
- throw new Error(`Failed to load collection (${response.statusCode})`);
219
+ const statusError = new Error(`Failed to load collection (${response.statusCode})`);
220
+ statusError.statusCode = response.statusCode;
221
+ throw statusError;
220
222
  }
221
223
 
222
224
  const payload = response.payload && typeof response.payload === "object" && !Array.isArray(response.payload)
@@ -255,11 +257,95 @@ module.exports = function(RED) {
255
257
  return collected;
256
258
  };
257
259
 
260
+ function getResponseStatusFromError(error) {
261
+ const directStatus = Number(error && error.statusCode);
262
+ if (Number.isFinite(directStatus)) {
263
+ return Math.trunc(directStatus);
264
+ }
265
+
266
+ const message = String(error && error.message || "");
267
+ const match = message.match(/\((\d{3})\)/);
268
+ if (!match) {
269
+ return NaN;
270
+ }
271
+
272
+ const parsed = Number(match[1]);
273
+ return Number.isFinite(parsed) ? Math.trunc(parsed) : NaN;
274
+ }
275
+
276
+ function shouldUseLegacyNetworkFallback(error) {
277
+ const statusCode = getResponseStatusFromError(error);
278
+ if (!Number.isFinite(statusCode)) {
279
+ return false;
280
+ }
281
+
282
+ // Older local Network releases may not expose the official
283
+ // integration endpoints and return explicit route/method errors.
284
+ return [403, 404, 405, 410, 501].includes(statusCode);
285
+ }
286
+
287
+ function mapLegacySiteToNormalizedShape(site) {
288
+ const item = site && typeof site === "object" && !Array.isArray(site)
289
+ ? site
290
+ : {};
291
+ const siteName = normalizeString(item.name || item.desc || "default");
292
+ const siteId = normalizeString(
293
+ item.external_id
294
+ || item.id
295
+ || item._id
296
+ || item.name
297
+ );
298
+
299
+ if (!siteId) {
300
+ return null;
301
+ }
302
+
303
+ return {
304
+ ...item,
305
+ id: siteId,
306
+ siteId,
307
+ name: siteName || siteId,
308
+ displayName: normalizeString(item.desc || item.displayName || siteName || siteId)
309
+ };
310
+ }
311
+
312
+ node.fetchLegacySites = async () => {
313
+ const response = await node.legacyApiRequest({
314
+ path: "/api/self/sites",
315
+ method: "GET"
316
+ });
317
+ if (response.statusCode < 200 || response.statusCode >= 300) {
318
+ const statusError = new Error(`Failed to load legacy sites (${response.statusCode})`);
319
+ statusError.statusCode = response.statusCode;
320
+ throw statusError;
321
+ }
322
+
323
+ const sites = normalizeNetworkCollection(response.payload)
324
+ .map((site) => mapLegacySiteToNormalizedShape(site))
325
+ .filter(Boolean);
326
+
327
+ const dedupById = new Map();
328
+ sites.forEach((site) => {
329
+ const key = normalizeString(site && site.id);
330
+ if (key && !dedupById.has(key)) {
331
+ dedupById.set(key, site);
332
+ }
333
+ });
334
+ return Array.from(dedupById.values());
335
+ };
336
+
258
337
  node.fetchSites = async () => {
259
338
  // Sites are the root scope for nearly all other Network resources.
260
- return node.fetchPagedCollection({
261
- path: "/v1/sites"
262
- });
339
+ try {
340
+ return await node.fetchPagedCollection({
341
+ path: "/v1/sites"
342
+ });
343
+ } catch (error) {
344
+ if (!shouldUseLegacyNetworkFallback(error)) {
345
+ throw error;
346
+ }
347
+ return node.fetchLegacySites();
348
+ }
263
349
  };
264
350
 
265
351
  node.fetchDevices = async (deviceType) => {
@@ -293,6 +379,55 @@ module.exports = function(RED) {
293
379
  siteName
294
380
  }));
295
381
  } catch (error) {
382
+ if (!shouldUseLegacyNetworkFallback(error)) {
383
+ return [];
384
+ }
385
+ try {
386
+ if (definition.type === "device") {
387
+ const legacyDevices = await fetchLegacyDevices(siteId);
388
+ return legacyDevices.map((item) => ({
389
+ ...item,
390
+ id: firstNonEmptyString([
391
+ item && item.id,
392
+ item && item.device_id,
393
+ item && item._id,
394
+ item && item.mac
395
+ ]),
396
+ siteId,
397
+ siteName
398
+ }));
399
+ }
400
+
401
+ if (definition.type === "client") {
402
+ const legacyClients = await fetchLegacyKnownClients(siteId);
403
+ return legacyClients.map((item) => ({
404
+ ...item,
405
+ id: firstNonEmptyString([
406
+ item && item.id,
407
+ item && item.clientId,
408
+ item && item.client_id,
409
+ item && item.user_id,
410
+ item && item._id,
411
+ item && item.macAddress,
412
+ item && item.mac
413
+ ]),
414
+ clientId: firstNonEmptyString([
415
+ item && item.clientId,
416
+ item && item.id,
417
+ item && item.client_id,
418
+ item && item.user_id,
419
+ item && item._id,
420
+ item && item.macAddress,
421
+ item && item.mac
422
+ ]),
423
+ siteId,
424
+ siteName
425
+ }));
426
+ }
427
+ } catch (legacyError) {
428
+ return [];
429
+ }
430
+
296
431
  return [];
297
432
  }
298
433
  }));
@@ -419,13 +554,55 @@ module.exports = function(RED) {
419
554
  .replace(/\{deviceId\}/g, encodeURIComponent(scoped.resourceId))
420
555
  .replace(/\{clientId\}/g, encodeURIComponent(scoped.resourceId));
421
556
 
422
- const response = await node.apiRequest({
423
- path: detailPath,
424
- method: "GET"
425
- });
557
+ let response;
558
+ try {
559
+ response = await node.apiRequest({
560
+ path: detailPath,
561
+ method: "GET"
562
+ });
563
+ } catch (error) {
564
+ if (!shouldUseLegacyNetworkFallback(error)) {
565
+ throw error;
566
+ }
567
+ }
426
568
 
427
- if (response.statusCode < 200 || response.statusCode >= 300) {
428
- throw new Error(`Failed to load ${deviceType} ${scoped.resourceId} (${response.statusCode})`);
569
+ if (!response || response.statusCode < 200 || response.statusCode >= 300) {
570
+ const statusError = new Error(`Failed to load ${deviceType} ${scoped.resourceId} (${response && response.statusCode ? response.statusCode : "n/a"})`);
571
+ statusError.statusCode = response && response.statusCode ? response.statusCode : undefined;
572
+ if (!shouldUseLegacyNetworkFallback(statusError)) {
573
+ throw statusError;
574
+ }
575
+
576
+ if (definition.type === "device") {
577
+ const legacyDevices = await fetchLegacyDevices(scoped.siteId);
578
+ return legacyDevices.find((legacyDevice) => {
579
+ const candidates = [
580
+ legacyDevice && legacyDevice.id,
581
+ legacyDevice && legacyDevice.device_id,
582
+ legacyDevice && legacyDevice._id,
583
+ legacyDevice && legacyDevice.mac
584
+ ];
585
+ return candidates.some((candidate) => normalizeIdentifierKey(candidate) === normalizeIdentifierKey(scoped.resourceId));
586
+ }) || null;
587
+ }
588
+
589
+ if (definition.type === "client") {
590
+ const legacyClients = await fetchLegacyKnownClients(scoped.siteId);
591
+ return legacyClients.find((legacyClient) => {
592
+ const candidates = [
593
+ legacyClient && legacyClient.id,
594
+ legacyClient && legacyClient.clientId,
595
+ legacyClient && legacyClient.client_id,
596
+ legacyClient && legacyClient.user_id,
597
+ legacyClient && legacyClient._id,
598
+ legacyClient && legacyClient.macAddress,
599
+ legacyClient && legacyClient.mac
600
+ ];
601
+ return candidates.some((candidate) => normalizeIdentifierKey(candidate) === normalizeIdentifierKey(scoped.resourceId));
602
+ }) || null;
603
+ }
604
+
605
+ throw statusError;
429
606
  }
430
607
 
431
608
  const payload = extractNetworkData(response.payload);
@@ -1058,14 +1058,14 @@
1058
1058
  </div>
1059
1059
  <div class="form-row" id="node-row-deviceId">
1060
1060
  <label for="node-input-deviceId"><i class="fa fa-exchange"></i> Switch</label>
1061
- <select id="node-input-deviceId" style="width:70%;"></select>
1061
+ <select id="node-input-deviceId" style="width:62%;"></select>
1062
1062
  <a href="#" id="node-input-refreshDevices" class="editor-button" style="margin-left:6px;" title="Refresh switches and ports" aria-label="Refresh switches and ports">
1063
1063
  <i class="fa fa-refresh"></i>
1064
1064
  </a>
1065
1065
  </div>
1066
1066
  <div class="form-row" id="node-row-clientLookupId">
1067
1067
  <label for="node-input-clientLookupId"><i class="fa fa-laptop"></i> Client</label>
1068
- <select id="node-input-clientLookupId" style="width:70%;"></select>
1068
+ <select id="node-input-clientLookupId" style="width:62%;"></select>
1069
1069
  <a href="#" id="node-input-refreshClients" class="editor-button" style="margin-left:6px;" title="Refresh clients" aria-label="Refresh clients">
1070
1070
  <i class="fa fa-refresh"></i>
1071
1071
  </a>
@@ -839,8 +839,8 @@
839
839
  if (field.helpText) {
840
840
  $wrapper.append(
841
841
  $("<div></div>")
842
- .addClass("form-tips")
843
- .css({ marginLeft: "152px" })
842
+ .addClass("form-tips unifi-ultimate-info-tip")
843
+ .css({ marginLeft: "0", width: "100%", boxSizing: "border-box" })
844
844
  .text(field.helpText)
845
845
  );
846
846
  }
@@ -1165,7 +1165,7 @@
1165
1165
  </div>
1166
1166
  <div class="form-row">
1167
1167
  <label for="node-input-deviceId"><i class="fa fa-dot-circle-o"></i> Item</label>
1168
- <select id="node-input-deviceId" style="width:70%;"></select>
1168
+ <select id="node-input-deviceId" style="width:62%;"></select>
1169
1169
  <a href="#" id="node-input-refreshDevices" class="editor-button" style="margin-left:6px;" title="Refresh devices" aria-label="Refresh devices">
1170
1170
  <i class="fa fa-refresh"></i>
1171
1171
  </a>
@@ -1175,7 +1175,7 @@
1175
1175
  <select id="node-input-capability"></select>
1176
1176
  </div>
1177
1177
  <div class="form-row" id="node-row-capabilityConfig" style="display:none;">
1178
- <label><i class="fa fa-sliders"></i> Action options</label>
1178
+ <label></label>
1179
1179
  <div id="node-input-capabilityConfigContainer" style="width:70%; display:inline-block;"></div>
1180
1180
  <input type="hidden" id="node-input-capabilityConfig">
1181
1181
  <input type="hidden" id="node-input-deviceName">
@@ -1201,10 +1201,10 @@
1201
1201
  <p><b>Connection</b> selects the shared UniFi Network config node.</p>
1202
1202
  <p><b>Resource</b> chooses what you want to work with: Site, UniFi Device, or Client.</p>
1203
1203
  <p><b>Item</b> chooses the specific site, device, or client. Device and client selectors are searchable and open with the complete list before filtering.</p>
1204
- <p><b>Action</b> only shows actions supported by the selected resource. <b>Action options</b> appears only when the selected action needs extra input.</p>
1204
+ <p><b>Action</b> only shows actions supported by the selected resource. Additional fields appear only when the selected action needs extra input.</p>
1205
1205
  <p>Action highlights include <code>Read Application Info</code>, <code>Authorize Guest Access</code>, <code>Revoke Guest Access</code>, <code>Restart Device</code>, and <code>Power Cycle Port</code>.</p>
1206
1206
  <p><code>Read Application Info</code> is available on the <code>Site</code> resource type and returns Network application metadata.</p>
1207
- <p><code>Receive Events (Unofficial Stream)</code> uses a non-official websocket endpoint and is marked experimental. The websocket is opened once by the shared Network config node and events are fanned out to subscribed runtime nodes. In this mode, <b>Action options</b> exposes <code>All</code> only, and the node emits all received Network events because fine-grained filtering is not currently available.</p>
1207
+ <p><code>Receive Events (Unofficial Stream)</code> uses a non-official websocket endpoint and is marked experimental. The websocket is opened once by the shared Network config node and events are fanned out to subscribed runtime nodes. In this mode, only <code>All</code> is available and the node emits all received Network events because fine-grained filtering is not currently available.</p>
1208
1208
  <p>When selecting a switch port, the port list shows connected clients or downstream UniFi devices when the controller exposes that information.</p>
1209
1209
  <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 API calls.</p>
1210
1210
 
@@ -491,7 +491,7 @@
491
491
  </div>
492
492
  <div class="form-row">
493
493
  <label for="node-input-clientId"><i class="fa fa-wifi"></i> Client</label>
494
- <select id="node-input-clientId" style="width:70%;"></select>
494
+ <select id="node-input-clientId" style="width:62%;"></select>
495
495
  <a href="#" id="node-input-refreshClients" class="editor-button" style="margin-left:6px;" title="Refresh clients" aria-label="Refresh clients">
496
496
  <i class="fa fa-refresh"></i>
497
497
  </a>
@@ -797,8 +797,8 @@
797
797
  if (field.helpText) {
798
798
  $wrapper.append(
799
799
  $("<div></div>")
800
- .addClass("form-tips")
801
- .css({ marginLeft: "152px" })
800
+ .addClass("form-tips unifi-ultimate-info-tip")
801
+ .css({ marginLeft: "0", width: "100%", boxSizing: "border-box" })
802
802
  .text(field.helpText)
803
803
  );
804
804
  }
@@ -1075,7 +1075,7 @@
1075
1075
  </div>
1076
1076
  <div class="form-row">
1077
1077
  <label for="node-input-deviceId"><i class="fa fa-dot-circle-o"></i> Device</label>
1078
- <select id="node-input-deviceId" style="width:70%;"></select>
1078
+ <select id="node-input-deviceId" style="width:62%;"></select>
1079
1079
  <a href="#" id="node-input-refreshDevices" class="editor-button" style="margin-left:6px;" title="Refresh devices" aria-label="Refresh devices">
1080
1080
  <i class="fa fa-refresh"></i>
1081
1081
  </a>
@@ -1085,7 +1085,7 @@
1085
1085
  <select id="node-input-capability"></select>
1086
1086
  </div>
1087
1087
  <div class="form-row" id="node-row-capabilityConfig" style="display:none;">
1088
- <label><i class="fa fa-sliders"></i> Action options</label>
1088
+ <label></label>
1089
1089
  <div id="node-input-capabilityConfigContainer" style="width:70%; display:inline-block;"></div>
1090
1090
  <input type="hidden" id="node-input-capabilityConfig">
1091
1091
  <input type="hidden" id="node-input-deviceName">
@@ -1111,7 +1111,7 @@
1111
1111
  <p><b>Connection</b> selects the shared UniFi Protect config node.</p>
1112
1112
  <p><b>Control</b> chooses the Protect device category. <b>Device</b> chooses the specific item. Device selectors are searchable and open with the complete list before filtering.</p>
1113
1113
  <p><b>Action</b> only shows actions supported by the selected device.</p>
1114
- <p><b>Action options</b> appears only when the selected action needs extra input. For example, observables, PTZ presets, live views, or discovered properties.</p>
1114
+ <p>Additional fields appear only when the selected action needs extra input. For example, events, PTZ presets, live views, or discovered properties.</p>
1115
1115
  <p>Action highlights: <code>Receive Events</code>, <code>Read Device State</code>, <code>Take Snapshot</code>, <code>Show Doorbell Message</code>, and <code>Read Application Info</code> on <code>NVR</code>.</p>
1116
1116
  <p><code>Receive Events</code> performs an initial fetch and then subscribes to streams managed by the shared Protect config node. The config node keeps websocket channels and forwards matching updates to runtime nodes. In this mode, the input pin is hidden and listening starts automatically after deploy/restart.</p>
1117
1117
  <p>For <code>Sensor</code>, <code>Camera</code>, and <code>Light</code> observables, <code>All</code> emits every event for the selected device. Choosing a specific observable normalizes the main output to <code>true</code> or <code>false</code> and keeps the original context in <code>msg.details.raw</code>. Sensor observables include <code>Extreme Values</code>.</p>
@@ -271,7 +271,7 @@ async function getCapabilityOptions(deviceType, capabilityId, context) {
271
271
  fields: [
272
272
  {
273
273
  id: "observable",
274
- label: "Observable",
274
+ label: "Event",
275
275
  type: "select",
276
276
  options: [
277
277
  { value: "all", label: "All" }
@@ -50,7 +50,7 @@ const COMMON_CAPABILITIES = [
50
50
  fields: [
51
51
  {
52
52
  id: "observable",
53
- label: "Observable",
53
+ label: "Event",
54
54
  type: "select",
55
55
  options: [
56
56
  {
@@ -956,7 +956,7 @@ async function buildObservableFields(deviceType, context) {
956
956
  const fields = [
957
957
  {
958
958
  id: "observable",
959
- label: "Observable",
959
+ label: "Event",
960
960
  type: "select",
961
961
  reloadOnChange: true,
962
962
  options: observableOptions,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-red-contrib-unifi-ultimate",
3
- "version": "0.1.10",
3
+ "version": "0.1.11",
4
4
  "description": "Node-RED nodes for Ubiquiti UniFi integrations, including UniFi Protect, UniFi Access, and UniFi Network.",
5
5
  "files": [
6
6
  "nodes/",
@@ -66,6 +66,13 @@
66
66
  color: #9a3412;
67
67
  }
68
68
 
69
+ .unifi-ultimate-info-tip.form-tips {
70
+ border: 1px solid #b8daf3;
71
+ border-left: 3px solid #7db7e8;
72
+ background: #edf6ff;
73
+ color: #1f3f5b;
74
+ }
75
+
69
76
  .unifi-ultimate-wait-overlay {
70
77
  position: fixed;
71
78
  inset: 0;