node-red-contrib-unifi-ultimate 0.1.7 → 0.1.9

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.
@@ -111,4 +111,5 @@
111
111
 
112
112
  <h3>Usage</h3>
113
113
  <p>This config node is used by UniFi Access Device nodes for API requests and live notification events.</p>
114
- </script>
114
+ <p>All Access API calls and notification channels are managed by this config node. Runtime nodes subscribe through <code>addClient/removeClient</code> and receive fan-out updates.</p>
115
+ </script>
@@ -120,6 +120,9 @@ module.exports = function(RED) {
120
120
  );
121
121
  };
122
122
 
123
+ // Leaf nodes must delegate outbound UniFi Access calls to the config node.
124
+ node.executeAccessRequest = async (request) => node.apiRequest(request || {});
125
+
123
126
  node.fetchDevices = async (deviceType) => {
124
127
  const definition = getDeviceTypeDefinition(deviceType);
125
128
  if (!definition) {
@@ -1087,10 +1087,10 @@
1087
1087
  <p><b>Action</b> only shows actions supported by the selected item.</p>
1088
1088
  <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>
1089
1089
  <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>
1090
- <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>
1090
+ <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>
1091
1091
  <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>
1092
1092
  <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>
1093
- <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>
1093
+ <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>
1094
1094
 
1095
1095
  <h3>Input</h3>
1096
1096
  <ul>
@@ -115,6 +115,18 @@ function attachDetails(outputMsg, details) {
115
115
  };
116
116
  }
117
117
 
118
+ function buildStatusTimestampText() {
119
+ const now = new Date();
120
+ const time = now.toTimeString().split(" ")[0];
121
+ return `(day ${now.getDate()}, ${time})`;
122
+ }
123
+
124
+ function appendStatusTimestamp(text) {
125
+ const normalized = String(text === undefined || text === null ? "" : text).trim();
126
+ const suffix = buildStatusTimestampText();
127
+ return normalized ? `${normalized} ${suffix}` : suffix;
128
+ }
129
+
118
130
  module.exports = function(RED) {
119
131
  function UnifiAccessDeviceNode(config) {
120
132
  RED.nodes.createNode(this, config);
@@ -131,6 +143,16 @@ module.exports = function(RED) {
131
143
  node.currentDevice = null;
132
144
  node.isObserving = false;
133
145
 
146
+ function setNodeStatus(status) {
147
+ if (!status || typeof status !== "object" || Array.isArray(status)) {
148
+ return;
149
+ }
150
+ node.status({
151
+ ...status,
152
+ text: appendStatusTimestamp(status.text)
153
+ });
154
+ }
155
+
134
156
  function resolveOutputDeviceName(payload) {
135
157
  const extracted = extractDeviceNameFromPayload(payload);
136
158
  if (extracted) {
@@ -194,7 +216,7 @@ module.exports = function(RED) {
194
216
  })
195
217
  });
196
218
 
197
- node.status({ fill: "green", shape: "dot", text: buildNodeStatus(deviceType, payload) });
219
+ setNodeStatus({ fill: "green", shape: "dot", text: buildNodeStatus(deviceType, payload) });
198
220
  sendOutputs(send, stateMsg, null);
199
221
  }
200
222
 
@@ -269,7 +291,7 @@ module.exports = function(RED) {
269
291
  })
270
292
  });
271
293
 
272
- node.status({ fill: "yellow", shape: "ring", text: "no ring" });
294
+ setNodeStatus({ fill: "yellow", shape: "ring", text: "no ring" });
273
295
  sendOutputs(send, skippedMsg, null);
274
296
  return;
275
297
  }
@@ -291,9 +313,9 @@ module.exports = function(RED) {
291
313
  const execution = composeCapabilityExecution(deviceType, capabilityId, capabilityConfig, selectedDevice);
292
314
  const request = buildCapabilityRequest(deviceType, capabilityId, deviceId, selectedDevice);
293
315
 
294
- node.status({ fill: "blue", shape: "dot", text: `${capability.label}` });
316
+ setNodeStatus({ fill: "blue", shape: "dot", text: `${capability.label}` });
295
317
 
296
- const response = await node.server.apiRequest({
318
+ const response = await node.server.executeAccessRequest({
297
319
  path: request.path,
298
320
  method: request.method,
299
321
  query: execution.query,
@@ -303,7 +325,7 @@ module.exports = function(RED) {
303
325
  });
304
326
 
305
327
  if (response.statusCode < 200 || response.statusCode >= 300) {
306
- node.status({ fill: "yellow", shape: "ring", text: `${response.statusCode}` });
328
+ setNodeStatus({ fill: "yellow", shape: "ring", text: `${response.statusCode}` });
307
329
  throw new Error(`UniFi Access request failed with status ${response.statusCode}`);
308
330
  }
309
331
 
@@ -350,7 +372,7 @@ module.exports = function(RED) {
350
372
  })
351
373
  });
352
374
 
353
- node.status({ fill: "green", shape: "dot", text: `${capability.label}` });
375
+ setNodeStatus({ fill: "green", shape: "dot", text: `${capability.label}` });
354
376
  sendOutputs(send, stateMsg, null);
355
377
  }
356
378
 
@@ -396,7 +418,7 @@ module.exports = function(RED) {
396
418
  done();
397
419
  }
398
420
  } catch (error) {
399
- node.status({ fill: "red", shape: "ring", text: "error" });
421
+ setNodeStatus({ fill: "red", shape: "ring", text: "error" });
400
422
  if (typeof done === "function") {
401
423
  done(error);
402
424
  } else {
@@ -414,7 +436,7 @@ module.exports = function(RED) {
414
436
  }
415
437
  const eventName = String(eventPayload.event || "").trim();
416
438
 
417
- node.status({ fill: "blue", shape: "ring", text: eventName || "event" });
439
+ setNodeStatus({ fill: "blue", shape: "ring", text: eventName || "event" });
418
440
  const resolvedDeviceName = resolveOutputDeviceName(node.currentDevice);
419
441
  const eventMsg = {
420
442
  payload: attachDeviceNameToPayload(eventPayload, resolvedDeviceName),
@@ -436,9 +458,9 @@ module.exports = function(RED) {
436
458
  };
437
459
 
438
460
  if (!node.server) {
439
- node.status({ fill: "red", shape: "ring", text: "config missing" });
461
+ setNodeStatus({ fill: "red", shape: "ring", text: "config missing" });
440
462
  } else if (configuredCapabilityOpensEventStream() && (!node.deviceType || !node.deviceId)) {
441
- node.status({ fill: "yellow", shape: "ring", text: "select device" });
463
+ setNodeStatus({ fill: "yellow", shape: "ring", text: "select device" });
442
464
  } else {
443
465
  if (typeof node.server.addClient === "function") {
444
466
  node.server.addClient(node);
@@ -7,7 +7,8 @@
7
7
  defaults: {
8
8
  name: { value: "Unifi Network Config" },
9
9
  host: { value: "", required: true },
10
- rejectUnauthorized: { value: false }
10
+ rejectUnauthorized: { value: false },
11
+ powerObservationIntervalSeconds: { value: 15, required: true, validate: RED.validators.number() }
11
12
  },
12
13
  credentials: {
13
14
  apiKey: { type: "password", required: true }
@@ -87,6 +88,10 @@
87
88
  <input type="checkbox" id="node-config-input-rejectUnauthorized" style="display:inline-block; width:auto; vertical-align:top;">
88
89
  <span style="margin-left:8px;">Secure connection only</span>
89
90
  </div>
91
+ <div class="form-row">
92
+ <label for="node-config-input-powerObservationIntervalSeconds"><i class="fa fa-repeat"></i> Power Poll (s)</label>
93
+ <input type="text" id="node-config-input-powerObservationIntervalSeconds" placeholder="15">
94
+ </div>
90
95
  <div class="form-row">
91
96
  <div id="node-config-input-connectionHint" class="unifi-ultimate-card"></div>
92
97
  </div>
@@ -105,9 +110,12 @@
105
110
  <p><b>Controller</b> accepts the UniFi OS host or IP address. The node builds the local UniFi Network proxy URL automatically.</p>
106
111
  <p><b>API Key</b> is required for authentication and is sent as <code>X-API-Key</code>.</p>
107
112
  <p><b>Security</b> controls certificate verification. Disable it only when your controller uses a self-signed certificate.</p>
113
+ <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>
108
114
  <p><b>Name</b> is the config node name shown in the editor.</p>
109
115
 
110
116
  <h3>Usage</h3>
111
117
  <p>This config node is used by Network Device, Network Presence, and Control POE nodes.</p>
118
+ <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>
119
+ <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>
112
120
  <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>
113
121
  </script>