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.
@@ -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) {
@@ -564,6 +590,7 @@
564
590
  $capability.append(
565
591
  $("<option></option>")
566
592
  .val(item.id)
593
+ .attr("data-method", String(item.method || "").trim())
567
594
  .attr("data-opens-stream", item.opensEventStream === true ? "true" : "false")
568
595
  .text(item.label)
569
596
  );
@@ -1043,6 +1070,9 @@
1043
1070
  writeCapabilityConfig();
1044
1071
  refreshAfterCapabilityChange();
1045
1072
  });
1073
+ $autoEmit.on("change", function () {
1074
+ syncProgressiveRows();
1075
+ });
1046
1076
  $refresh.on("click", function (event) {
1047
1077
  event.preventDefault();
1048
1078
  refreshAfterServerChange();
@@ -1095,9 +1125,15 @@
1095
1125
  <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
1096
1126
  <input type="text" id="node-input-name" placeholder="Node name">
1097
1127
  </div>
1098
- <div class="form-row">
1099
- <label for="node-input-timeout"><i class="fa fa-clock-o"></i> Timeout</label>
1100
- <input type="text" id="node-input-timeout" placeholder="15000">
1128
+ <div class="form-row" id="node-row-autoEmit">
1129
+ <label for="node-input-autoEmit"><i class="fa fa-repeat"></i> Repeat</label>
1130
+ <input type="checkbox" id="node-input-autoEmit" style="display:inline-block; width:auto; vertical-align:top;">
1131
+ <span style="margin-left:8px;">Emit periodically</span>
1132
+ </div>
1133
+ <div class="form-row" id="node-row-autoEmitInterval">
1134
+ <label for="node-input-autoEmitInterval"><i class="fa fa-clock-o"></i> Every</label>
1135
+ <input type="number" id="node-input-autoEmitInterval" min="5" step="1" placeholder="60" style="width:120px;">
1136
+ <span style="margin-left:8px;">seconds</span>
1101
1137
  </div>
1102
1138
  </div>
1103
1139
  </script>
@@ -1115,7 +1151,9 @@
1115
1151
  <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
1152
  <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
1153
  <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 a derived value and keeps the original context in <code>msg.details.raw</code>. Event observables emit <code>true/false</code>; sensor metrics such as <code>Temperature</code>, <code>Humidity</code>, <code>Light Level</code>, and <code>Battery Level</code> emit numeric values.</p>
1118
- <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>
1154
+ <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>
1155
+ <p>Request timeout is handled automatically by the node.</p>
1156
+ <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>
1119
1157
 
1120
1158
  <h3>Input</h3>
1121
1159
  <ul>
@@ -8,6 +8,7 @@ const {
8
8
  resolveObservableState,
9
9
  resolveObservableEventValue
10
10
  } = require("./utils/unifi-protect-device-registry");
11
+ const DEFAULT_REQUEST_TIMEOUT_MS = 15000;
11
12
 
12
13
  function resolveDeviceType(configuredDeviceType) {
13
14
  return String(configuredDeviceType || "").trim();
@@ -44,6 +45,18 @@ function resolveCapabilityConfig(configuredCapabilityConfig) {
44
45
  return parseCapabilityConfig(configuredCapabilityConfig);
45
46
  }
46
47
 
48
+ function parseBoolean(value) {
49
+ return value === true || value === "true" || value === 1 || value === "1";
50
+ }
51
+
52
+ function parseIntervalSeconds(value, fallback) {
53
+ const numeric = Number(value);
54
+ if (!Number.isFinite(numeric) || numeric < 5) {
55
+ return fallback;
56
+ }
57
+ return Math.trunc(numeric);
58
+ }
59
+
47
60
  function buildNodeStatus(deviceType, payload) {
48
61
  const label = payload && payload.name ? payload.name : deviceType;
49
62
  const state = payload && payload.state ? payload.state : "ready";
@@ -154,8 +167,12 @@ module.exports = function(RED) {
154
167
  node.deviceId = config.deviceId || "";
155
168
  node.capability = config.capability || "observe";
156
169
  node.capabilityConfig = config.capabilityConfig || "{}";
170
+ node.autoEmit = parseBoolean(config.autoEmit);
171
+ node.autoEmitIntervalSeconds = parseIntervalSeconds(config.autoEmitInterval, 60);
172
+ node.autoEmitTimer = null;
173
+ node.autoEmitInFlight = false;
157
174
  node.deviceName = resolveDeviceName(config.deviceName);
158
- node.timeout = Number(config.timeout) > 0 ? Number(config.timeout) : 15000;
175
+ node.timeout = DEFAULT_REQUEST_TIMEOUT_MS;
159
176
  node.currentDevice = null;
160
177
  node.currentObservableValue = undefined;
161
178
  node.isObserving = false;
@@ -273,7 +290,7 @@ module.exports = function(RED) {
273
290
  sendOutputs(send, stateMsg, null);
274
291
  }
275
292
 
276
- async function invokeCapability(send) {
293
+ async function invokeCapability(send, triggerSource) {
277
294
  if (!node.server) {
278
295
  throw new Error("Unifi Protect configuration is missing.");
279
296
  }
@@ -303,7 +320,7 @@ module.exports = function(RED) {
303
320
 
304
321
  if (capability.mode === "observe") {
305
322
  // Manual input on an observe node acts like a forced refresh.
306
- await fetchDeviceState(deviceType, deviceId, capabilityConfig, send, "manual-refresh");
323
+ await fetchDeviceState(deviceType, deviceId, capabilityConfig, send, triggerSource || "manual-refresh");
307
324
  return;
308
325
  }
309
326
 
@@ -369,6 +386,16 @@ module.exports = function(RED) {
369
386
  return resolveCapabilityId(node.capability) === "observe";
370
387
  }
371
388
 
389
+ function configuredCapabilitySupportsAutoEmit() {
390
+ const capability = resolveConfiguredCapabilityDefinition();
391
+ if (!capability || capability.opensEventStream === true) {
392
+ return false;
393
+ }
394
+
395
+ const method = String(capability.method || "").trim().toUpperCase();
396
+ return !method || method === "GET";
397
+ }
398
+
372
399
  function shouldObserveConfiguredDevice() {
373
400
  return node.server && configuredCapabilityOpensEventStream() && node.deviceType && node.deviceId;
374
401
  }
@@ -392,13 +419,57 @@ module.exports = function(RED) {
392
419
  });
393
420
  }
394
421
 
422
+ function stopAutoEmitTimer() {
423
+ if (node.autoEmitTimer) {
424
+ clearInterval(node.autoEmitTimer);
425
+ node.autoEmitTimer = null;
426
+ }
427
+ node.autoEmitInFlight = false;
428
+ }
429
+
430
+ function shouldStartAutoEmitTimer() {
431
+ return Boolean(
432
+ node.server
433
+ && node.autoEmit
434
+ && node.deviceType
435
+ && node.deviceId
436
+ && configuredCapabilitySupportsAutoEmit()
437
+ );
438
+ }
439
+
440
+ function runAutoEmit() {
441
+ if (node.autoEmitInFlight || !shouldStartAutoEmitTimer()) {
442
+ return;
443
+ }
444
+
445
+ node.autoEmitInFlight = true;
446
+ invokeCapability(node.send.bind(node), "interval")
447
+ .catch((error) => {
448
+ setNodeStatus({ fill: "red", shape: "ring", text: "auto error" });
449
+ node.error(error);
450
+ })
451
+ .finally(() => {
452
+ node.autoEmitInFlight = false;
453
+ });
454
+ }
455
+
456
+ function startAutoEmitTimer() {
457
+ stopAutoEmitTimer();
458
+ if (!shouldStartAutoEmitTimer()) {
459
+ return;
460
+ }
461
+
462
+ node.autoEmitTimer = setInterval(runAutoEmit, node.autoEmitIntervalSeconds * 1000);
463
+ setNodeStatus({ fill: "grey", shape: "ring", text: `every ${node.autoEmitIntervalSeconds}s` });
464
+ }
465
+
395
466
  node.on("input", async function(_msg, send, done) {
396
467
  send = send || function() {
397
468
  node.send.apply(node, arguments);
398
469
  };
399
470
 
400
471
  try {
401
- await invokeCapability(send);
472
+ await invokeCapability(send, "manual-refresh");
402
473
  if (typeof done === "function") {
403
474
  done();
404
475
  }
@@ -543,10 +614,12 @@ module.exports = function(RED) {
543
614
  setNodeStatus({ fill: "yellow", shape: "ring", text: "select device" });
544
615
  } else {
545
616
  startObservation();
617
+ startAutoEmitTimer();
546
618
  }
547
619
 
548
620
  node.on("close", function(done) {
549
621
  try {
622
+ stopAutoEmitTimer();
550
623
  if (node.server && typeof node.server.removeClient === "function") {
551
624
  node.server.removeClient(node);
552
625
  }
@@ -93,9 +93,74 @@ const TYPE_CAPABILITIES = {
93
93
  method: "GET",
94
94
  path: "/v1/sites/{siteId}/clients",
95
95
  mode: "request"
96
+ },
97
+ {
98
+ id: "countOnlineClients",
99
+ label: "Count Online Clients",
100
+ description: "Count currently online clients for the selected site.",
101
+ mode: "custom"
102
+ },
103
+ {
104
+ id: "createGuestVoucher",
105
+ label: "Create Guest Voucher",
106
+ description: "Create one or more simple Hotspot guest vouchers.",
107
+ method: "POST",
108
+ path: "/v1/sites/{siteId}/hotspot/vouchers",
109
+ mode: "custom",
110
+ editor: {
111
+ fields: [
112
+ {
113
+ id: "name",
114
+ label: "Name",
115
+ type: "text",
116
+ placeholder: "Guest Voucher",
117
+ defaultValue: "Guest Voucher"
118
+ },
119
+ {
120
+ id: "timeLimitMinutes",
121
+ label: "Duration (minutes)",
122
+ type: "number",
123
+ placeholder: "1440",
124
+ defaultValue: 1440
125
+ },
126
+ {
127
+ id: "count",
128
+ label: "How Many",
129
+ type: "number",
130
+ placeholder: "1",
131
+ defaultValue: 1
132
+ }
133
+ ]
134
+ },
135
+ requestComposer: ({ capabilityConfig }) => {
136
+ const normalizedName = String(capabilityConfig && capabilityConfig.name || "Guest Voucher").trim() || "Guest Voucher";
137
+ const timeLimitMinutes = resolveIntegerValue(undefined, capabilityConfig && capabilityConfig.timeLimitMinutes, 1) || 1440;
138
+ const count = resolveIntegerValue(undefined, capabilityConfig && capabilityConfig.count, 1) || 1;
139
+
140
+ if (timeLimitMinutes > 1000000) {
141
+ throw new Error("Guest voucher duration must be between 1 and 1000000 minutes.");
142
+ }
143
+ if (count > 1000) {
144
+ throw new Error("Guest voucher count must be between 1 and 1000.");
145
+ }
146
+
147
+ return {
148
+ payload: {
149
+ name: normalizedName,
150
+ timeLimitMinutes,
151
+ count
152
+ }
153
+ };
154
+ }
96
155
  }
97
156
  ],
98
157
  device: [
158
+ {
159
+ id: "readSwitchTemperatures",
160
+ label: "Read Temperatures",
161
+ description: "Fetch temperature readings exposed by the selected UniFi switch or device. Some models/controllers do not expose temperature data.",
162
+ mode: "custom"
163
+ },
99
164
  {
100
165
  id: "getLatestStatistics",
101
166
  label: "Read Latest Statistics",
@@ -104,6 +169,30 @@ const TYPE_CAPABILITIES = {
104
169
  path: "/v1/sites/{siteId}/devices/{deviceId}/statistics/latest",
105
170
  mode: "request"
106
171
  },
172
+ {
173
+ id: "readDeviceCpu",
174
+ label: "Read CPU",
175
+ description: "Read the selected UniFi device CPU usage percentage.",
176
+ method: "GET",
177
+ path: "/v1/sites/{siteId}/devices/{deviceId}/statistics/latest",
178
+ mode: "custom"
179
+ },
180
+ {
181
+ id: "readDeviceMemory",
182
+ label: "Read Memory",
183
+ description: "Read the selected UniFi device memory usage percentage.",
184
+ method: "GET",
185
+ path: "/v1/sites/{siteId}/devices/{deviceId}/statistics/latest",
186
+ mode: "custom"
187
+ },
188
+ {
189
+ id: "readDeviceUptime",
190
+ label: "Read Uptime",
191
+ description: "Read the selected UniFi device uptime in seconds.",
192
+ method: "GET",
193
+ path: "/v1/sites/{siteId}/devices/{deviceId}/statistics/latest",
194
+ mode: "custom"
195
+ },
107
196
  {
108
197
  id: "restartDevice",
109
198
  label: "Restart Device",
@@ -164,6 +253,12 @@ const TYPE_CAPABILITIES = {
164
253
  }
165
254
  ],
166
255
  client: [
256
+ {
257
+ id: "isClientOnline",
258
+ label: "Is Client Online",
259
+ description: "Return true when the selected client is online.",
260
+ mode: "custom"
261
+ },
167
262
  {
168
263
  id: "authorizeGuestAccess",
169
264
  label: "Authorize Guest Access",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-red-contrib-unifi-ultimate",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "Node-RED nodes for Ubiquiti UniFi integrations, including UniFi Protect, UniFi Access, and UniFi Network.",
5
5
  "files": [
6
6
  "nodes/",