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

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.
@@ -7,6 +7,16 @@
7
7
  defaults: {
8
8
  name: { value: "Unifi Protect Config" },
9
9
  host: { value: "", required: true },
10
+ port: {
11
+ value: "",
12
+ required: false,
13
+ validate: function (value) {
14
+ if (value === undefined || value === null || value === "") {
15
+ return true;
16
+ }
17
+ return /^\d+$/.test(String(value)) && Number(value) >= 1 && Number(value) <= 65535;
18
+ }
19
+ },
10
20
  authHeader: { value: "X-API-Key", required: true },
11
21
  rejectUnauthorized: { value: false }
12
22
  },
@@ -19,14 +29,17 @@
19
29
  oneditprepare: function () {
20
30
  const node = this;
21
31
  const $hostInput = $("#node-config-input-host");
32
+ const $port = $("#node-config-input-port");
22
33
  const $apiKey = $("#node-config-input-apiKey");
23
34
  const $rejectUnauthorized = $("#node-config-input-rejectUnauthorized");
35
+ const $portRow = $port.closest(".form-row");
24
36
  const $apiKeyRow = $apiKey.closest(".form-row");
25
37
  const $securityRow = $rejectUnauthorized.closest(".form-row");
26
38
  const $advancedRow = $("#node-config-row-advanced");
27
39
 
28
40
  function syncProgressiveRows() {
29
41
  const hasHost = String($hostInput.val() || node.host || "").trim() !== "";
42
+ $portRow.toggle(hasHost);
30
43
  $apiKeyRow.toggle(hasHost);
31
44
  $securityRow.toggle(hasHost);
32
45
  $advancedRow.toggle(hasHost);
@@ -63,6 +76,10 @@
63
76
  <label for="node-config-input-host"><i class="fa fa-globe"></i> Controller</label>
64
77
  <input type="text" id="node-config-input-host" placeholder="192.168.1.10">
65
78
  </div>
79
+ <div class="form-row">
80
+ <label for="node-config-input-port"><i class="fa fa-plug"></i> Port</label>
81
+ <input type="text" id="node-config-input-port" placeholder="443 (default)">
82
+ </div>
66
83
  <div class="form-row">
67
84
  <label for="node-config-input-apiKey"><i class="fa fa-key"></i> API Key</label>
68
85
  <input type="password" id="node-config-input-apiKey" placeholder="Paste your UniFi Protect API key">
@@ -91,6 +108,7 @@
91
108
 
92
109
  <h3>Configuration</h3>
93
110
  <p><b>Controller</b> accepts the UniFi OS host or IP address. The node builds the Protect integration URL automatically.</p>
111
+ <p><b>Port</b> is optional. Leave it empty to use the default <code>443</code>. Set it when your UniFi OS server listens on a custom port (for example <code>11443</code>). A port embedded directly in the Controller field (<code>host:port</code>) takes precedence over this field.</p>
94
112
  <p><b>API Key</b> is required for authentication.</p>
95
113
  <p><b>Security</b> controls certificate verification. Disable it only when your controller uses a self-signed certificate.</p>
96
114
  <p><b>Name</b> is the config node name shown in the editor.</p>
@@ -2,6 +2,7 @@
2
2
 
3
3
  const {
4
4
  buildBaseUrlFromHost,
5
+ normalizePort,
5
6
  buildQueryString,
6
7
  doRequest,
7
8
  buildRequestHeaders,
@@ -24,7 +25,8 @@ module.exports = function(RED) {
24
25
  const node = this;
25
26
  node.name = config.name;
26
27
  node.host = String(config.host || "").trim();
27
- node.baseUrl = buildBaseUrlFromHost(node.host);
28
+ node.port = normalizePort(config.port);
29
+ node.baseUrl = buildBaseUrlFromHost(node.host, node.port);
28
30
  node.authHeader = (config.authHeader || "X-API-Key").trim() || "X-API-Key";
29
31
  node.rejectUnauthorized = config.rejectUnauthorized !== false && config.rejectUnauthorized !== "false";
30
32
  node.nodeClients = [];
@@ -18,10 +18,10 @@
18
18
  autoEmitInterval: { value: 60, validate: RED.validators.number() }
19
19
  },
20
20
  inputs: 1,
21
- outputs: 1,
21
+ outputs: 2,
22
22
  icon: "unifi-u-logo-light-protect.svg",
23
23
  paletteLabel: "PROTECT",
24
- outputLabels: ["result"],
24
+ outputLabels: ["result", "error"],
25
25
  label: function () {
26
26
  return this.name || this.deviceId || "Unifi Protect";
27
27
  },
@@ -1162,16 +1162,32 @@
1162
1162
  <li>Input message properties are ignored by this node.</li>
1163
1163
  </ul>
1164
1164
 
1165
- <h3>Output</h3>
1166
- <ul>
1167
- <li>A single output pin emits state and event messages for the selected device.</li>
1168
- <li><code>msg.topic</code>: node name from the editor.</li>
1169
- <li><code>msg.deviceName</code>: remembered/observed selected device name.</li>
1170
- <li><code>msg.eventName</code>: trigger/event that produced the message.</li>
1171
- <li><code>msg.details.unifiProtect</code>: standardized metadata for the device and selected capability.</li>
1172
- <li><code>msg.details.raw</code>: raw event/context payload when available.</li>
1173
- </ul>
1174
- <p>Example output:</p>
1165
+ <h3>Outputs</h3>
1166
+ <dl class="message-properties">
1167
+ <dt>Output 1 — result</dt>
1168
+ <dd>Emits state and live event messages for the selected device. When <code>Receive Events</code> is active, both state and event messages use this pin.</dd>
1169
+ <dt class="optional">msg.topic</dt>
1170
+ <dd>Node name from the editor.</dd>
1171
+ <dt class="optional">msg.deviceName</dt>
1172
+ <dd>Remembered/observed name of the selected device.</dd>
1173
+ <dt class="optional">msg.eventName</dt>
1174
+ <dd>Trigger or event that produced the message.</dd>
1175
+ <dt class="optional">msg.details.unifiProtect</dt>
1176
+ <dd>Standardized metadata for the device and selected capability.</dd>
1177
+ <dt class="optional">msg.details.raw</dt>
1178
+ <dd>Raw event or context payload when available.</dd>
1179
+ </dl>
1180
+ <dl class="message-properties">
1181
+ <dt>Output 2 — error</dt>
1182
+ <dd>Emitted when the action fails (API error, timeout, or configuration problem). The node status also turns red. Errors are additionally forwarded to any connected <code>catch</code> node.</dd>
1183
+ <dt class="optional">msg.payload</dt>
1184
+ <dd><code>null</code></dd>
1185
+ <dt class="optional">msg.topic</dt>
1186
+ <dd>Node name from the editor.</dd>
1187
+ <dt class="optional">msg.error.message</dt>
1188
+ <dd>Human-readable error description.</dd>
1189
+ </dl>
1190
+ <p>Example output (pin 1):</p>
1175
1191
  <pre><code>{
1176
1192
  "topic": "Front Door Camera",
1177
1193
  "deviceName": "Front Door Camera",
@@ -1193,5 +1209,11 @@
1193
1209
  "source": "events"
1194
1210
  }
1195
1211
  }
1212
+ }</code></pre>
1213
+ <p>Example output (pin 2 — error):</p>
1214
+ <pre><code>{
1215
+ "topic": "Front Door Camera",
1216
+ "payload": null,
1217
+ "error": { "message": "Request timed out" }
1196
1218
  }</code></pre>
1197
1219
  </script>
@@ -8,6 +8,18 @@ const {
8
8
  resolveObservableState,
9
9
  resolveObservableEventValue
10
10
  } = require("./utils/unifi-protect-device-registry");
11
+ const {
12
+ parseBoolean,
13
+ parseIntervalSeconds,
14
+ buildStatusTimestampText,
15
+ appendStatusTimestamp,
16
+ resolveNodeName,
17
+ resolveDeviceName,
18
+ extractDeviceNameFromPayload,
19
+ attachDeviceNameToPayload,
20
+ attachDetails,
21
+ buildErrorOutputMessage
22
+ } = require("./utils/common-utils");
11
23
  const DEFAULT_REQUEST_TIMEOUT_MS = 15000;
12
24
 
13
25
  function resolveDeviceType(configuredDeviceType) {
@@ -45,18 +57,6 @@ function resolveCapabilityConfig(configuredCapabilityConfig) {
45
57
  return parseCapabilityConfig(configuredCapabilityConfig);
46
58
  }
47
59
 
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
-
60
60
  function buildNodeStatus(deviceType, payload) {
61
61
  const label = payload && payload.name ? payload.name : deviceType;
62
62
  const state = payload && payload.state ? payload.state : "ready";
@@ -75,70 +75,6 @@ function requiresDeviceSpecificCapabilityValidation(deviceType, capabilityId) {
75
75
  ].includes(String(capabilityId || "").trim());
76
76
  }
77
77
 
78
- function resolveNodeName(value) {
79
- return String(value || "").trim();
80
- }
81
-
82
- function resolveDeviceName(value) {
83
- return String(value || "").trim();
84
- }
85
-
86
- function extractDeviceNameFromPayload(payload) {
87
- if (!payload || typeof payload !== "object" || Array.isArray(payload)) {
88
- return "";
89
- }
90
-
91
- return resolveDeviceName(
92
- payload.name
93
- || payload.displayName
94
- || payload.alias
95
- || payload.full_name
96
- || payload.id
97
- );
98
- }
99
-
100
- function attachDeviceNameToPayload(payload, deviceName) {
101
- if (!deviceName) {
102
- return payload;
103
- }
104
-
105
- if (payload && typeof payload === "object" && !Array.isArray(payload)) {
106
- return {
107
- ...payload,
108
- deviceName
109
- };
110
- }
111
-
112
- return payload;
113
- }
114
-
115
- function attachDetails(outputMsg, details) {
116
- if (!details || typeof details !== "object" || Array.isArray(details)) {
117
- return;
118
- }
119
-
120
- const currentDetails = outputMsg.details && typeof outputMsg.details === "object" && !Array.isArray(outputMsg.details)
121
- ? outputMsg.details
122
- : {};
123
-
124
- outputMsg.details = {
125
- ...currentDetails,
126
- ...details
127
- };
128
- }
129
-
130
- function buildStatusTimestampText() {
131
- const now = new Date();
132
- const time = now.toTimeString().split(" ")[0];
133
- return `(day ${now.getDate()}, ${time})`;
134
- }
135
-
136
- function appendStatusTimestamp(text) {
137
- const normalized = String(text === undefined || text === null ? "" : text).trim();
138
- const suffix = buildStatusTimestampText();
139
- return normalized ? `${normalized} ${suffix}` : suffix;
140
- }
141
-
142
78
  function resolveConfiguredObservable(capabilityConfig) {
143
79
  const observable = String(
144
80
  capabilityConfig && capabilityConfig.observable !== undefined
@@ -446,6 +382,7 @@ module.exports = function(RED) {
446
382
  invokeCapability(node.send.bind(node), "interval")
447
383
  .catch((error) => {
448
384
  setNodeStatus({ fill: "red", shape: "ring", text: "auto error" });
385
+ node.send([null, buildErrorOutputMessage(error, node.name)]);
449
386
  node.error(error);
450
387
  })
451
388
  .finally(() => {
@@ -475,6 +412,7 @@ module.exports = function(RED) {
475
412
  }
476
413
  } catch (error) {
477
414
  setNodeStatus({ fill: "red", shape: "ring", text: "error" });
415
+ node.send([null, buildErrorOutputMessage(error, node.name)]);
478
416
  if (typeof done === "function") {
479
417
  done(error);
480
418
  } else {
@@ -0,0 +1,157 @@
1
+ "use strict";
2
+
3
+ function parseBoolean(value) {
4
+ return value === true || value === "true" || value === 1 || value === "1";
5
+ }
6
+
7
+ function parseIntervalSeconds(value, fallback) {
8
+ const numeric = Number(value);
9
+ if (!Number.isFinite(numeric) || numeric < 5) {
10
+ return fallback;
11
+ }
12
+ return Math.trunc(numeric);
13
+ }
14
+
15
+ function normalizePort(value) {
16
+ // Accept a TCP port from the editor and reduce it to a clean "1".."65535"
17
+ // string, or empty when missing/invalid so callers can fall back to defaults.
18
+ if (value === undefined || value === null) {
19
+ return "";
20
+ }
21
+
22
+ const str = String(value).trim();
23
+ if (!str || !/^\d+$/.test(str)) {
24
+ return "";
25
+ }
26
+
27
+ const num = Number(str);
28
+ if (!Number.isInteger(num) || num < 1 || num > 65535) {
29
+ return "";
30
+ }
31
+
32
+ return String(num);
33
+ }
34
+
35
+ function hostHasExplicitPort(host) {
36
+ if (!host) {
37
+ return false;
38
+ }
39
+
40
+ if (host.startsWith("[")) {
41
+ // IPv6 literal: a port is only present when "]:" appears (e.g. [::1]:443).
42
+ return host.includes("]:");
43
+ }
44
+
45
+ // Bare host or IPv4: a single trailing :<digits> is a port.
46
+ return (host.match(/:/g) || []).length === 1 && /:\d+$/.test(host);
47
+ }
48
+
49
+ function applyPortToHost(host, port) {
50
+ // Combine a normalized host with the dedicated port field. A port already
51
+ // embedded in the host string always wins, so the separate field only fills
52
+ // the gap when the host carries none.
53
+ const normalizedHost = String(host || "").trim();
54
+ if (!normalizedHost) {
55
+ return "";
56
+ }
57
+
58
+ const normalizedPort = normalizePort(port);
59
+ if (!normalizedPort || hostHasExplicitPort(normalizedHost)) {
60
+ return normalizedHost;
61
+ }
62
+
63
+ return `${normalizedHost}:${normalizedPort}`;
64
+ }
65
+
66
+ function buildStatusTimestampText() {
67
+ const now = new Date();
68
+ const time = now.toTimeString().split(" ")[0];
69
+ return `(day ${now.getDate()}, ${time})`;
70
+ }
71
+
72
+ function appendStatusTimestamp(text) {
73
+ const normalized = String(text === undefined || text === null ? "" : text).trim();
74
+ const suffix = buildStatusTimestampText();
75
+ return normalized ? `${normalized} ${suffix}` : suffix;
76
+ }
77
+
78
+ function resolveNodeName(value) {
79
+ return String(value || "").trim();
80
+ }
81
+
82
+ function resolveDeviceName(value) {
83
+ return String(value || "").trim();
84
+ }
85
+
86
+ // Superset version — includes hostname and macAddress for Network nodes; harmless for Protect/Access.
87
+ function extractDeviceNameFromPayload(payload) {
88
+ if (!payload || typeof payload !== "object" || Array.isArray(payload)) {
89
+ return "";
90
+ }
91
+
92
+ return resolveDeviceName(
93
+ payload.name
94
+ || payload.displayName
95
+ || payload.hostname
96
+ || payload.alias
97
+ || payload.full_name
98
+ || payload.macAddress
99
+ || payload.id
100
+ );
101
+ }
102
+
103
+ function attachDeviceNameToPayload(payload, deviceName) {
104
+ if (!deviceName) {
105
+ return payload;
106
+ }
107
+
108
+ if (payload && typeof payload === "object" && !Array.isArray(payload)) {
109
+ return {
110
+ ...payload,
111
+ deviceName
112
+ };
113
+ }
114
+
115
+ return payload;
116
+ }
117
+
118
+ function attachDetails(outputMsg, details) {
119
+ if (!outputMsg || typeof outputMsg !== "object" || Array.isArray(outputMsg)) {
120
+ return;
121
+ }
122
+ if (!details || typeof details !== "object" || Array.isArray(details)) {
123
+ return;
124
+ }
125
+
126
+ outputMsg.details = {
127
+ ...(outputMsg.details && typeof outputMsg.details === "object" && !Array.isArray(outputMsg.details)
128
+ ? outputMsg.details
129
+ : {}),
130
+ ...details
131
+ };
132
+ }
133
+
134
+ function buildErrorOutputMessage(error, nodeName) {
135
+ return {
136
+ topic: String(nodeName || "").trim() || undefined,
137
+ payload: null,
138
+ error: {
139
+ message: String(error && error.message ? error.message : error)
140
+ }
141
+ };
142
+ }
143
+
144
+ module.exports = {
145
+ parseBoolean,
146
+ parseIntervalSeconds,
147
+ normalizePort,
148
+ applyPortToHost,
149
+ buildStatusTimestampText,
150
+ appendStatusTimestamp,
151
+ resolveNodeName,
152
+ resolveDeviceName,
153
+ extractDeviceNameFromPayload,
154
+ attachDeviceNameToPayload,
155
+ attachDetails,
156
+ buildErrorOutputMessage
157
+ };
@@ -3,10 +3,13 @@
3
3
  const http = require("http");
4
4
  const https = require("https");
5
5
  const { maybeParseBody } = require("./http-response-utils");
6
+ const { normalizePort, applyPortToHost } = require("./common-utils");
6
7
 
7
- function normalizeHost(value) {
8
+ function normalizeHost(value, port) {
8
9
  // Access controllers usually live on port 12445. Accept bare hosts, host:port
9
- // pairs and IPv6 literals, then normalize everything to one safe form.
10
+ // pairs and IPv6 literals, then normalize everything to one safe form. The
11
+ // dedicated port field (when set) fills in a missing port before the 12445
12
+ // default is applied.
10
13
  let host = String(value || "").trim();
11
14
  if (!host) {
12
15
  return "";
@@ -20,6 +23,8 @@ function normalizeHost(value) {
20
23
  return "";
21
24
  }
22
25
 
26
+ host = applyPortToHost(host, port);
27
+
23
28
  if (host.startsWith("[") && host.includes("]")) {
24
29
  return host.includes("]:") ? host : `${host}:12445`;
25
30
  }
@@ -36,8 +41,8 @@ function normalizeHost(value) {
36
41
  return host;
37
42
  }
38
43
 
39
- function buildBaseUrlFromHost(value) {
40
- const host = normalizeHost(value);
44
+ function buildBaseUrlFromHost(value, port) {
45
+ const host = normalizeHost(value, port);
41
46
  if (!host) {
42
47
  return "";
43
48
  }
@@ -174,6 +179,8 @@ function normalizeAccessCollection(payload) {
174
179
 
175
180
  module.exports = {
176
181
  buildBaseUrlFromHost,
182
+ normalizePort,
183
+ applyPortToHost,
177
184
  buildQueryString,
178
185
  doRequest,
179
186
  buildRequestHeaders,
@@ -3,6 +3,7 @@
3
3
  const http = require("http");
4
4
  const https = require("https");
5
5
  const { maybeParseBody } = require("./http-response-utils");
6
+ const { normalizePort, applyPortToHost } = require("./common-utils");
6
7
 
7
8
  function normalizeHost(value) {
8
9
  // Accept either a bare host or a full URL from the editor and reduce it to
@@ -17,8 +18,8 @@ function normalizeHost(value) {
17
18
  return host.trim().replace(/\/+$/, "");
18
19
  }
19
20
 
20
- function buildBaseUrlFromHost(value) {
21
- const host = normalizeHost(value);
21
+ function buildBaseUrlFromHost(value, port) {
22
+ const host = applyPortToHost(normalizeHost(value), port);
22
23
  if (!host) {
23
24
  return "";
24
25
  }
@@ -156,6 +157,8 @@ function normalizeNetworkCollection(payload) {
156
157
 
157
158
  module.exports = {
158
159
  buildBaseUrlFromHost,
160
+ normalizePort,
161
+ applyPortToHost,
159
162
  buildQueryString,
160
163
  doRequest,
161
164
  buildRequestHeaders,
@@ -3,6 +3,7 @@
3
3
  const http = require("http");
4
4
  const https = require("https");
5
5
  const { maybeParseBody } = require("./http-response-utils");
6
+ const { normalizePort, applyPortToHost } = require("./common-utils");
6
7
 
7
8
  function normalizeHost(value) {
8
9
  // Protect editor fields may contain a host or a full URL; strip everything
@@ -17,8 +18,8 @@ function normalizeHost(value) {
17
18
  return host.trim().replace(/\/+$/, "");
18
19
  }
19
20
 
20
- function buildBaseUrlFromHost(value) {
21
- const host = normalizeHost(value);
21
+ function buildBaseUrlFromHost(value, port) {
22
+ const host = applyPortToHost(normalizeHost(value), port);
22
23
  if (!host) {
23
24
  return "";
24
25
  }
@@ -131,6 +132,8 @@ function buildRequestBody(headers, method, payload) {
131
132
 
132
133
  module.exports = {
133
134
  buildBaseUrlFromHost,
135
+ normalizePort,
136
+ applyPortToHost,
134
137
  buildQueryString,
135
138
  doRequest,
136
139
  buildRequestHeaders,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-red-contrib-unifi-ultimate",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "Node-RED nodes for Ubiquiti UniFi integrations, including UniFi Protect, UniFi Access, and UniFi Network.",
5
5
  "files": [
6
6
  "nodes/",
@@ -46,7 +46,7 @@
46
46
  }
47
47
  },
48
48
  "scripts": {
49
- "test": "node --check nodes/unifi-access-config.js && node --check nodes/unifi-access-device.js && node --check nodes/unifi-network-config.js && node --check nodes/unifi-network-control-poe.js && node --check nodes/unifi-network-device.js && node --check nodes/unifi-network-presence.js && node --check nodes/unifi-protect-config.js && node --check nodes/unifi-protect-device.js && node --check nodes/utils/http-response-utils.js && node --check nodes/utils/unifi-access-utils.js && node --check nodes/utils/unifi-access-device-registry.js && node --check nodes/utils/unifi-network-utils.js && node --check nodes/utils/unifi-network-device-registry.js && node --check nodes/utils/unifi-protect-utils.js && node --check nodes/utils/unifi-protect-field-metadata.js && node --check nodes/utils/unifi-protect-device-registry.js && node --check scripts/unifi-registry-discovery-common.js && node --check scripts/discover-unifi-network-registry.js && node --check scripts/discover-unifi-protect-registry.js && node --check scripts/discover-unifi-access-registry.js",
49
+ "test": "node --check nodes/unifi-access-config.js && node --check nodes/unifi-access-device.js && node --check nodes/unifi-network-config.js && node --check nodes/unifi-network-control-poe.js && node --check nodes/unifi-network-device.js && node --check nodes/unifi-network-presence.js && node --check nodes/unifi-protect-config.js && node --check nodes/unifi-protect-device.js && node --check nodes/utils/common-utils.js && node --check nodes/utils/http-response-utils.js && node --check nodes/utils/unifi-access-utils.js && node --check nodes/utils/unifi-access-device-registry.js && node --check nodes/utils/unifi-network-utils.js && node --check nodes/utils/unifi-network-device-registry.js && node --check nodes/utils/unifi-protect-utils.js && node --check nodes/utils/unifi-protect-field-metadata.js && node --check nodes/utils/unifi-protect-device-registry.js && node --check scripts/unifi-registry-discovery-common.js && node --check scripts/discover-unifi-network-registry.js && node --check scripts/discover-unifi-protect-registry.js && node --check scripts/discover-unifi-access-registry.js && jest",
50
50
  "registry:discover:network": "node scripts/discover-unifi-network-registry.js",
51
51
  "registry:discover:protect": "node scripts/discover-unifi-protect-registry.js",
52
52
  "registry:discover:access": "node scripts/discover-unifi-access-registry.js"
@@ -55,7 +55,9 @@
55
55
  "directories": {
56
56
  "example": "examples"
57
57
  },
58
- "devDependencies": {},
58
+ "devDependencies": {
59
+ "jest": "^30.4.2"
60
+ },
59
61
  "type": "commonjs",
60
62
  "bugs": {
61
63
  "url": "https://github.com/Supergiovane/node-red-contrib-unifi-ultimate/issues"