node-red-contrib-unifi-ultimate 1.0.1 → 1.0.2

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
@@ -72,6 +72,10 @@ Use Network nodes to work with:
72
72
  Common uses:
73
73
 
74
74
  - Check whether a client is online.
75
+ - Count online clients for a site.
76
+ - Create simple guest vouchers.
77
+ - Read CPU, memory, and uptime from UniFi devices.
78
+ - Read Temperatures when the selected UniFi device exposes thermal sensor data.
75
79
  - Restart a UniFi device.
76
80
  - Power-cycle a PoE port.
77
81
  - Turn PoE on or off for a selected switch port.
@@ -81,6 +85,7 @@ Editor conveniences:
81
85
 
82
86
  - Device and client fields are searchable.
83
87
  - Port lists show port status and connected clients when UniFi exposes that information.
88
+ - Read-only actions can emit periodically to simulate a simple polling stream.
84
89
  - Selecting an item updates the node `Name` automatically.
85
90
 
86
91
  <br/>
@@ -184,6 +189,31 @@ When `msg.payload` is an object, it also includes `payload.deviceName` with the
184
189
  }
185
190
  ```
186
191
 
192
+ `Unifi Network Device` action `Read Temperatures` returns:
193
+
194
+ | Field | Type | Guaranteed | Notes |
195
+ | ---------------------------------- | ------ | ---------- | ----------------------------------------------------------- |
196
+ | `msg.payload` | number | no | First normalized temperature reading in Celsius. |
197
+ | `msg.details.temperature` | object | yes | Full normalized temperature details and all readings found. |
198
+ | `msg.details.temperatureSources[]` | array | yes | Sources used, for example `official` and/or `legacy`. |
199
+
200
+ Some switch models and controller versions do not expose temperature data; in that case the action emits `msg.payload = null` and details include `hasTemperatures: false`.
201
+
202
+ Other simple `Unifi Network Device` actions return compact payloads:
203
+
204
+ | Action | `msg.payload` | Details |
205
+ | ---------------------- | ------------------------------- | ----------------------------- |
206
+ | `Is Client Online` | `true` or `false` | `msg.details.client` |
207
+ | `Count Online Clients` | online client count | `msg.details.onlineClients[]` |
208
+ | `Read CPU` | CPU percentage | `msg.details.statistics` |
209
+ | `Read Memory` | memory percentage | `msg.details.statistics` |
210
+ | `Read Uptime` | uptime in seconds | `msg.details.statistics` |
211
+ | `Create Guest Voucher` | one voucher code, or code array | `msg.details.vouchers[]` |
212
+
213
+ For read-only actions on Network, Protect, and Access device nodes, enable `Emit periodically` to send the configured result at a fixed interval without using an Inject node. This option is hidden for event streams and write actions.
214
+
215
+ Request timeouts are handled internally. Existing nodes do not need timeout settings, and Network PoE power polling uses a 15 second default when the shared config value is left empty.
216
+
187
217
  ### Unifi Network Presence
188
218
 
189
219
  ```json
@@ -294,13 +324,13 @@ Power fields for `Unifi Network Control POE`:
294
324
 
295
325
  Import from `examples/`:
296
326
 
297
- | Flow file | What it demonstrates |
298
- | -------------------------------------------------------------------------------------------- | ---------------------------------------------- |
299
- | [examples/unifi-protect-info.json](examples/unifi-protect-info.json) | Read Protect camera state |
327
+ | Flow file | What it demonstrates |
328
+ | -------------------------------------------------------------------------------------------- | ----------------------------------------------- |
329
+ | [examples/unifi-protect-info.json](examples/unifi-protect-info.json) | Read Protect camera state |
300
330
  | [examples/unifi-protect-sensor-observe.json](examples/unifi-protect-sensor-observe.json) | Receive sensor observables (boolean or numeric) |
301
- | [examples/unifi-protect-camera-actions.json](examples/unifi-protect-camera-actions.json) | Snapshot, PTZ presets, and doorbell messages |
302
- | [examples/unifi-access-door-control.json](examples/unifi-access-door-control.json) | Door state, unlock, and temporary lock rule |
303
- | [examples/unifi-access-intercom-doorbell.json](examples/unifi-access-intercom-doorbell.json) | Intercom observe, trigger, and cancel doorbell |
331
+ | [examples/unifi-protect-camera-actions.json](examples/unifi-protect-camera-actions.json) | Snapshot, PTZ presets, and doorbell messages |
332
+ | [examples/unifi-access-door-control.json](examples/unifi-access-door-control.json) | Door state, unlock, and temporary lock rule |
333
+ | [examples/unifi-access-intercom-doorbell.json](examples/unifi-access-intercom-doorbell.json) | Intercom observe, trigger, and cancel doorbell |
304
334
 
305
335
  ## Notes
306
336
 
@@ -15,7 +15,8 @@
15
15
  return this.name || this.host || "Unifi Access Config";
16
16
  },
17
17
  oneditprepare: function () {
18
- const $host = $("#node-config-input-host");
18
+ const node = this;
19
+ const $hostInput = $("#node-config-input-host");
19
20
  const $apiToken = $("#node-config-input-apiToken");
20
21
  const $rejectUnauthorized = $("#node-config-input-rejectUnauthorized");
21
22
  const $apiTokenRow = $apiToken.closest(".form-row");
@@ -23,7 +24,7 @@
23
24
  const $advancedRow = $("#node-config-row-advanced");
24
25
 
25
26
  function syncProgressiveRows() {
26
- const hasHost = String($host.val() || "").trim() !== "";
27
+ const hasHost = String($hostInput.val() || node.host || "").trim() !== "";
27
28
  $apiTokenRow.toggle(hasHost);
28
29
  $securityRow.toggle(hasHost);
29
30
  $advancedRow.toggle(hasHost);
@@ -38,7 +39,7 @@
38
39
 
39
40
  syncProgressiveRows();
40
41
 
41
- $host.on("change input", function () {
42
+ $hostInput.on("change input", function () {
42
43
  syncProgressiveRows();
43
44
  });
44
45
  }
@@ -14,7 +14,8 @@
14
14
  inputs: { value: 1 },
15
15
  capability: { value: "observe", required: true },
16
16
  capabilityConfig: { value: "{}" },
17
- timeout: { value: 15000, required: true, validate: RED.validators.number() }
17
+ autoEmit: { value: false },
18
+ autoEmitInterval: { value: 60, validate: RED.validators.number() }
18
19
  },
19
20
  inputs: 1,
20
21
  outputs: 1,
@@ -28,8 +29,11 @@
28
29
  const selectedCapabilityId = String($("#node-input-capability").val() || this.capability || "observe").trim();
29
30
  const selectedCapabilityOpensStream = $("#node-input-capability option:selected").data("opens-stream") === true
30
31
  || $("#node-input-capability option:selected").attr("data-opens-stream") === "true";
32
+ const selectedCapabilityMethod = String($("#node-input-capability option:selected").data("method") || $("#node-input-capability option:selected").attr("data-method") || "").trim().toUpperCase();
33
+ const supportsPeriodicEmit = !selectedCapabilityOpensStream && (!selectedCapabilityMethod || selectedCapabilityMethod === "GET");
31
34
  // Hide the input pin only when the selected action starts an event stream.
32
35
  this.inputs = (selectedCapabilityOpensStream || selectedCapabilityId === "observe") ? 0 : 1;
36
+ this.autoEmit = supportsPeriodicEmit ? $("#node-input-autoEmit").is(":checked") : false;
33
37
 
34
38
  const $capabilityConfig = $("#node-input-capabilityConfig");
35
39
  const capabilityConfig = {};
@@ -85,6 +89,9 @@
85
89
  const $capabilityConfig = $("#node-input-capabilityConfig");
86
90
  const $capabilityConfigRow = $("#node-row-capabilityConfig");
87
91
  const $capabilityConfigContainer = $("#node-input-capabilityConfigContainer");
92
+ const $autoEmit = $("#node-input-autoEmit");
93
+ const $autoEmitRow = $("#node-row-autoEmit");
94
+ const $autoEmitIntervalRow = $("#node-row-autoEmitInterval");
88
95
  const $refresh = $("#node-input-refreshDevices");
89
96
  const $deviceTypeRow = $deviceType.closest(".form-row");
90
97
  const $deviceIdRow = $deviceId.closest(".form-row");
@@ -95,6 +102,7 @@
95
102
  let capabilityOptionsRequestId = 0;
96
103
  let suspendChangeHandling = false;
97
104
  let isInitializingEditor = true;
105
+ let restoredPersistedAutoEmit = false;
98
106
  let deviceTypeDefinitions = [];
99
107
  let capabilityDefinitions = [];
100
108
 
@@ -109,12 +117,30 @@
109
117
  const hasDevice = hasSelection($deviceId);
110
118
  const hasCapability = hasSelection($capability);
111
119
  const hasCapabilityFields = $capabilityConfigRow.data("has-fields") === true;
120
+ const capabilityDefinition = getSelectedCapabilityDefinition();
121
+ const capabilityMethod = String(capabilityDefinition && capabilityDefinition.method || "").trim().toUpperCase();
122
+ const supportsPeriodicEmit = capabilityDefinition
123
+ && capabilityDefinition.opensEventStream !== true
124
+ && (!capabilityMethod || capabilityMethod === "GET");
125
+ const showPeriodicEmit = Boolean(hasServer && hasDeviceType && hasDevice && hasCapability && supportsPeriodicEmit);
112
126
 
113
127
  $deviceTypeRow.toggle(hasServer);
114
128
  $deviceIdRow.toggle(hasServer && hasDeviceType);
115
129
  $capabilityRow.toggle(hasServer && hasDeviceType && hasDevice);
116
130
  $capabilityConfigRow.toggle(hasServer && hasDeviceType && hasDevice && hasCapability && hasCapabilityFields);
117
131
  $advancedRow.toggle(hasServer && hasDeviceType && hasDevice && hasCapability);
132
+ $autoEmitRow.toggle(showPeriodicEmit);
133
+ $autoEmitIntervalRow.toggle(showPeriodicEmit && $autoEmit.is(":checked"));
134
+
135
+ if (showPeriodicEmit && !restoredPersistedAutoEmit) {
136
+ $autoEmit.prop("checked", node.autoEmit === true || node.autoEmit === "true");
137
+ restoredPersistedAutoEmit = true;
138
+ $autoEmitIntervalRow.toggle($autoEmit.is(":checked"));
139
+ }
140
+
141
+ if (!showPeriodicEmit && !isInitializingEditor) {
142
+ $autoEmit.prop("checked", false);
143
+ }
118
144
  }
119
145
 
120
146
  function parseCapabilityConfig(rawValue) {
@@ -575,6 +601,7 @@
575
601
  $capability.append(
576
602
  $("<option></option>")
577
603
  .val(item.id)
604
+ .attr("data-method", String(item.method || "").trim())
578
605
  .attr("data-opens-stream", item.opensEventStream === true ? "true" : "false")
579
606
  .text(item.label)
580
607
  );
@@ -1032,6 +1059,9 @@
1032
1059
  writeCapabilityConfig();
1033
1060
  refreshAfterCapabilityChange();
1034
1061
  });
1062
+ $autoEmit.on("change", function () {
1063
+ syncProgressiveRows();
1064
+ });
1035
1065
  $refresh.on("click", function (event) {
1036
1066
  event.preventDefault();
1037
1067
  refreshAfterServerChange();
@@ -1084,9 +1114,15 @@
1084
1114
  <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
1085
1115
  <input type="text" id="node-input-name" placeholder="Node name">
1086
1116
  </div>
1087
- <div class="form-row">
1088
- <label for="node-input-timeout"><i class="fa fa-clock-o"></i> Timeout</label>
1089
- <input type="text" id="node-input-timeout" placeholder="15000">
1117
+ <div class="form-row" id="node-row-autoEmit">
1118
+ <label for="node-input-autoEmit"><i class="fa fa-repeat"></i> Repeat</label>
1119
+ <input type="checkbox" id="node-input-autoEmit" style="display:inline-block; width:auto; vertical-align:top;">
1120
+ <span style="margin-left:8px;">Emit periodically</span>
1121
+ </div>
1122
+ <div class="form-row" id="node-row-autoEmitInterval">
1123
+ <label for="node-input-autoEmitInterval"><i class="fa fa-clock-o"></i> Every</label>
1124
+ <input type="number" id="node-input-autoEmitInterval" min="5" step="1" placeholder="60" style="width:120px;">
1125
+ <span style="margin-left:8px;">seconds</span>
1090
1126
  </div>
1091
1127
  </div>
1092
1128
  </script>
@@ -1105,7 +1141,9 @@
1105
1141
  <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, while specific <code>Event</code> choices filter to official UniFi Access event families (for example doorbell, door unlock, DPS, emergency, schedule, and temporary unlock events). The input pin is hidden and listening starts automatically after deploy/restart.</p>
1106
1142
  <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
1143
  <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
- <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>
1144
+ <p><b>Emit periodically</b> repeats read-only actions at the selected interval and sends each result to the flow. It is hidden for <code>Receive Events</code> and write actions.</p>
1145
+ <p>Request timeout is handled automatically by the node.</p>
1146
+ <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.</p>
1109
1147
 
1110
1148
  <h3>Input</h3>
1111
1149
  <ul>
@@ -9,6 +9,7 @@ const {
9
9
  matchesObservable
10
10
  } = require("./utils/unifi-access-device-registry");
11
11
  const { extractAccessData } = require("./utils/unifi-access-utils");
12
+ const DEFAULT_REQUEST_TIMEOUT_MS = 15000;
12
13
 
13
14
  function resolveDeviceType(configuredDeviceType) {
14
15
  return String(configuredDeviceType || "").trim();
@@ -45,6 +46,18 @@ function resolveCapabilityConfig(configuredCapabilityConfig) {
45
46
  return parseCapabilityConfig(configuredCapabilityConfig);
46
47
  }
47
48
 
49
+ function parseBoolean(value) {
50
+ return value === true || value === "true" || value === 1 || value === "1";
51
+ }
52
+
53
+ function parseIntervalSeconds(value, fallback) {
54
+ const numeric = Number(value);
55
+ if (!Number.isFinite(numeric) || numeric < 5) {
56
+ return fallback;
57
+ }
58
+ return Math.trunc(numeric);
59
+ }
60
+
48
61
  function buildNodeStatus(deviceType, payload) {
49
62
  const normalizedType = String(deviceType || "").trim();
50
63
  const item = payload && typeof payload === "object" ? payload : {};
@@ -147,8 +160,12 @@ module.exports = function(RED) {
147
160
  node.deviceId = config.deviceId || "";
148
161
  node.capability = config.capability || "observe";
149
162
  node.capabilityConfig = config.capabilityConfig || "{}";
163
+ node.autoEmit = parseBoolean(config.autoEmit);
164
+ node.autoEmitIntervalSeconds = parseIntervalSeconds(config.autoEmitInterval, 60);
165
+ node.autoEmitTimer = null;
166
+ node.autoEmitInFlight = false;
150
167
  node.deviceName = resolveDeviceName(config.deviceName);
151
- node.timeout = Number(config.timeout) > 0 ? Number(config.timeout) : 15000;
168
+ node.timeout = DEFAULT_REQUEST_TIMEOUT_MS;
152
169
  node.currentDevice = null;
153
170
  node.isObserving = false;
154
171
 
@@ -229,7 +246,7 @@ module.exports = function(RED) {
229
246
  sendOutputs(send, stateMsg, null);
230
247
  }
231
248
 
232
- async function invokeCapability(send) {
249
+ async function invokeCapability(send, triggerSource) {
233
250
  if (!node.server) {
234
251
  throw new Error("Unifi Access configuration is missing.");
235
252
  }
@@ -314,7 +331,7 @@ module.exports = function(RED) {
314
331
  deviceId,
315
332
  capabilityId,
316
333
  send,
317
- capability.mode === "fetch" ? "fetch" : "manual-refresh"
334
+ triggerSource || (capability.mode === "fetch" ? "fetch" : "manual-refresh")
318
335
  );
319
336
  return;
320
337
  }
@@ -399,6 +416,16 @@ module.exports = function(RED) {
399
416
  return resolveCapabilityId(node.capability) === "observe";
400
417
  }
401
418
 
419
+ function configuredCapabilitySupportsAutoEmit() {
420
+ const capability = resolveConfiguredCapabilityDefinition();
421
+ if (!capability || capability.opensEventStream === true) {
422
+ return false;
423
+ }
424
+
425
+ const method = String(capability.method || "").trim().toUpperCase();
426
+ return !method || method === "GET";
427
+ }
428
+
402
429
  function shouldObserveConfiguredDevice() {
403
430
  return node.server && configuredCapabilityOpensEventStream() && node.deviceType && node.deviceId;
404
431
  }
@@ -416,13 +443,57 @@ module.exports = function(RED) {
416
443
  });
417
444
  }
418
445
 
446
+ function stopAutoEmitTimer() {
447
+ if (node.autoEmitTimer) {
448
+ clearInterval(node.autoEmitTimer);
449
+ node.autoEmitTimer = null;
450
+ }
451
+ node.autoEmitInFlight = false;
452
+ }
453
+
454
+ function shouldStartAutoEmitTimer() {
455
+ return Boolean(
456
+ node.server
457
+ && node.autoEmit
458
+ && node.deviceType
459
+ && node.deviceId
460
+ && configuredCapabilitySupportsAutoEmit()
461
+ );
462
+ }
463
+
464
+ function runAutoEmit() {
465
+ if (node.autoEmitInFlight || !shouldStartAutoEmitTimer()) {
466
+ return;
467
+ }
468
+
469
+ node.autoEmitInFlight = true;
470
+ invokeCapability(node.send.bind(node), "interval")
471
+ .catch((error) => {
472
+ setNodeStatus({ fill: "red", shape: "ring", text: "auto error" });
473
+ node.error(error);
474
+ })
475
+ .finally(() => {
476
+ node.autoEmitInFlight = false;
477
+ });
478
+ }
479
+
480
+ function startAutoEmitTimer() {
481
+ stopAutoEmitTimer();
482
+ if (!shouldStartAutoEmitTimer()) {
483
+ return;
484
+ }
485
+
486
+ node.autoEmitTimer = setInterval(runAutoEmit, node.autoEmitIntervalSeconds * 1000);
487
+ setNodeStatus({ fill: "grey", shape: "ring", text: `every ${node.autoEmitIntervalSeconds}s` });
488
+ }
489
+
419
490
  node.on("input", async function(_msg, send, done) {
420
491
  send = send || function() {
421
492
  node.send.apply(node, arguments);
422
493
  };
423
494
 
424
495
  try {
425
- await invokeCapability(send);
496
+ await invokeCapability(send, "manual-refresh");
426
497
  if (typeof done === "function") {
427
498
  done();
428
499
  }
@@ -481,10 +552,12 @@ module.exports = function(RED) {
481
552
  node.server.addClient(node);
482
553
  }
483
554
  startObservation();
555
+ startAutoEmitTimer();
484
556
  }
485
557
 
486
558
  node.on("close", function(done) {
487
559
  try {
560
+ stopAutoEmitTimer();
488
561
  if (node.server && typeof node.server.removeClient === "function") {
489
562
  node.server.removeClient(node);
490
563
  }
@@ -8,7 +8,16 @@
8
8
  name: { value: "Unifi Network Config" },
9
9
  host: { value: "", required: true },
10
10
  rejectUnauthorized: { value: false },
11
- powerObservationIntervalSeconds: { value: 15, required: true, validate: RED.validators.number() }
11
+ powerObservationIntervalSeconds: {
12
+ value: 15,
13
+ required: false,
14
+ validate: function (value) {
15
+ return value === undefined
16
+ || value === null
17
+ || value === ""
18
+ || RED.validators.number()(value);
19
+ }
20
+ }
12
21
  },
13
22
  credentials: {
14
23
  apiKey: { type: "password", required: true }
@@ -17,7 +26,8 @@
17
26
  return this.name || this.host || "Unifi Network Config";
18
27
  },
19
28
  oneditprepare: function () {
20
- const $host = $("#node-config-input-host");
29
+ const node = this;
30
+ const $hostInput = $("#node-config-input-host");
21
31
  const $apiKey = $("#node-config-input-apiKey");
22
32
  const $rejectUnauthorized = $("#node-config-input-rejectUnauthorized");
23
33
  const $powerObservationIntervalSeconds = $("#node-config-input-powerObservationIntervalSeconds");
@@ -27,7 +37,7 @@
27
37
  const $advancedRow = $("#node-config-row-advanced");
28
38
 
29
39
  function syncProgressiveRows() {
30
- const hasHost = String($host.val() || "").trim() !== "";
40
+ const hasHost = String($hostInput.val() || node.host || "").trim() !== "";
31
41
  $apiKeyRow.toggle(hasHost);
32
42
  $securityRow.toggle(hasHost);
33
43
  $powerObservationRow.toggle(hasHost);
@@ -42,7 +52,7 @@
42
52
  }
43
53
 
44
54
  syncProgressiveRows();
45
- $host.on("change input", function () {
55
+ $hostInput.on("change input", function () {
46
56
  syncProgressiveRows();
47
57
  });
48
58
  }
@@ -93,7 +103,7 @@
93
103
  <p><b>Controller</b> accepts the UniFi OS host or IP address. The node builds the local UniFi Network proxy URL automatically.</p>
94
104
  <p><b>API Key</b> is required for authentication and is sent as <code>X-API-Key</code>.</p>
95
105
  <p><b>Security</b> controls certificate verification. Disable it only when your controller uses a self-signed certificate.</p>
96
- <p><b>Power Poll (s)</b> is the global polling interval used by all <code>Control POE</code> nodes when action is <code>Emit Power Consumption</code>.</p>
106
+ <p><b>Power Poll (s)</b> is the global polling interval used by all <code>Control POE</code> nodes when action is <code>Emit Power Consumption</code>. It is optional and defaults to 15 seconds.</p>
97
107
  <p><b>Name</b> is the config node name shown in the editor.</p>
98
108
 
99
109
  <h3>Usage</h3>