node-red-contrib-unifi-ultimate 1.1.25 → 1.1.27
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/nodes/unifi-protect-config.js +64 -15
- package/nodes/unifi-protect-device.html +2 -1
- package/nodes/unifi-protect-device.js +84 -3
- package/nodes/utils/unifi-protect-alarm.js +22 -2
- package/nodes/utils/unifi-protect-device-registry.js +193 -3
- package/nodes/utils/unifi-protect-field-metadata.js +16 -1
- package/package.json +2 -2
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
const { registerConnectionCheck } = require("./utils/unifi-connection-check");
|
|
4
4
|
const { actionGuidance } = require("./utils/unifi-action-guidance");
|
|
5
|
-
const { buildRequestError } = require("./utils/unifi-protect-alarm");
|
|
5
|
+
const { buildRequestError, expandModelUpdate } = require("./utils/unifi-protect-alarm");
|
|
6
6
|
const { attachHeartbeat, stopHeartbeat, createReconnectBackoff } = require("./utils/unifi-websocket-resilience");
|
|
7
7
|
|
|
8
8
|
const {
|
|
@@ -179,6 +179,9 @@ function createProtectCameraEventDeduper({
|
|
|
179
179
|
};
|
|
180
180
|
}
|
|
181
181
|
|
|
182
|
+
const SOCKET_PROPERTIES = { devices: "wsDevices", events: "wsEvents", updates: "wsUpdates" };
|
|
183
|
+
const UPDATES_STREAM_MODEL_KEYS = ["externalArmProfile"];
|
|
184
|
+
|
|
182
185
|
module.exports = function(RED) {
|
|
183
186
|
registerConnectionCheck(RED, "protect");
|
|
184
187
|
const knxAiCameraRegistry = getKnxAiCameraRegistry();
|
|
@@ -204,6 +207,7 @@ module.exports = function(RED) {
|
|
|
204
207
|
node.nodeClients = [];
|
|
205
208
|
node.wsDevices = null;
|
|
206
209
|
node.wsEvents = null;
|
|
210
|
+
node.wsUpdates = null;
|
|
207
211
|
node.reconnectTimer = null;
|
|
208
212
|
node.reconnectBackoff = createReconnectBackoff();
|
|
209
213
|
node.isClosing = false;
|
|
@@ -454,6 +458,21 @@ module.exports = function(RED) {
|
|
|
454
458
|
: [];
|
|
455
459
|
};
|
|
456
460
|
|
|
461
|
+
// Protect 7.4+: profiles of UniFi OS's external Alarm Manager.
|
|
462
|
+
node.fetchExternalArmProfiles = async () => {
|
|
463
|
+
const response = await node.apiRequest({
|
|
464
|
+
path: "/v1/external/arm-profiles",
|
|
465
|
+
method: "GET"
|
|
466
|
+
});
|
|
467
|
+
if (response.statusCode < 200 || response.statusCode >= 300) {
|
|
468
|
+
throw buildRequestError(response, "GET", "/v1/external/arm-profiles", [node.getApiKey()]);
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
return Array.isArray(response.payload)
|
|
472
|
+
? response.payload
|
|
473
|
+
: [];
|
|
474
|
+
};
|
|
475
|
+
|
|
457
476
|
node.fetchCapabilityOptions = async (deviceType, deviceId, capabilityId, capabilityConfig) => {
|
|
458
477
|
const selectedDevice = deviceId
|
|
459
478
|
? await node.fetchDeviceByTypeAndId(deviceType, deviceId)
|
|
@@ -466,7 +485,8 @@ module.exports = function(RED) {
|
|
|
466
485
|
fetchDevice: node.fetchDeviceByTypeAndId,
|
|
467
486
|
fetchDevices: node.fetchDevices,
|
|
468
487
|
fetchAssetFiles: node.fetchAssetFiles,
|
|
469
|
-
fetchArmProfiles: node.fetchArmProfiles
|
|
488
|
+
fetchArmProfiles: node.fetchArmProfiles,
|
|
489
|
+
fetchExternalArmProfiles: node.fetchExternalArmProfiles
|
|
470
490
|
});
|
|
471
491
|
return { ...result, requirements: actionGuidance("protect", capabilityId, capabilityConfig, selectedDevice, node.credentials) };
|
|
472
492
|
};
|
|
@@ -851,13 +871,30 @@ module.exports = function(RED) {
|
|
|
851
871
|
node.broadcastDeviceUpdate = (update) => {
|
|
852
872
|
// The config node is the single websocket consumer; individual
|
|
853
873
|
// runtime nodes subscribe through addClient/removeClient.
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
client.handleProtectDeviceUpdate
|
|
874
|
+
expandModelUpdate(update).forEach((singleUpdate) => {
|
|
875
|
+
node.nodeClients.forEach((client) => {
|
|
876
|
+
try {
|
|
877
|
+
if (client && typeof client.handleProtectDeviceUpdate === "function") {
|
|
878
|
+
client.handleProtectDeviceUpdate(singleUpdate);
|
|
879
|
+
}
|
|
880
|
+
} catch (error) {
|
|
858
881
|
}
|
|
859
|
-
}
|
|
860
|
-
|
|
882
|
+
});
|
|
883
|
+
});
|
|
884
|
+
};
|
|
885
|
+
|
|
886
|
+
// Protect 7.4+ /v1/subscribe/updates stream, filtered by model key.
|
|
887
|
+
// Only clients that ask for the "updates" stream receive it.
|
|
888
|
+
node.broadcastModelUpdate = (update) => {
|
|
889
|
+
expandModelUpdate(update).forEach((singleUpdate) => {
|
|
890
|
+
node.nodeClients.forEach((client) => {
|
|
891
|
+
try {
|
|
892
|
+
if (client && typeof client.handleProtectModelUpdate === "function") {
|
|
893
|
+
client.handleProtectModelUpdate(singleUpdate);
|
|
894
|
+
}
|
|
895
|
+
} catch (error) {
|
|
896
|
+
}
|
|
897
|
+
});
|
|
861
898
|
});
|
|
862
899
|
};
|
|
863
900
|
|
|
@@ -947,6 +984,10 @@ module.exports = function(RED) {
|
|
|
947
984
|
node.wsEvents = null;
|
|
948
985
|
wasActive = true;
|
|
949
986
|
}
|
|
987
|
+
if (kind === "updates" && node.wsUpdates === ws) {
|
|
988
|
+
node.wsUpdates = null;
|
|
989
|
+
wasActive = true;
|
|
990
|
+
}
|
|
950
991
|
if (wasActive) node.scheduleReconnect();
|
|
951
992
|
} catch (error) {
|
|
952
993
|
}
|
|
@@ -959,21 +1000,19 @@ module.exports = function(RED) {
|
|
|
959
1000
|
}
|
|
960
1001
|
});
|
|
961
1002
|
|
|
962
|
-
|
|
963
|
-
node.wsDevices = ws;
|
|
964
|
-
} else {
|
|
965
|
-
node.wsEvents = ws;
|
|
966
|
-
}
|
|
1003
|
+
node[SOCKET_PROPERTIES[kind]] = ws;
|
|
967
1004
|
};
|
|
968
1005
|
|
|
969
1006
|
const clientNeedsProtectStream = (client, kind, handlerName) => {
|
|
970
1007
|
if (!client || typeof client[handlerName] !== "function") return false;
|
|
971
|
-
|
|
1008
|
+
// Clients without a stream list get the original devices and
|
|
1009
|
+
// events streams; the 7.4 updates stream must be asked for.
|
|
1010
|
+
if (!Array.isArray(client.protectStreams)) return kind !== "updates";
|
|
972
1011
|
return client.protectStreams.some((stream) => String(stream || "").trim().toLowerCase() === kind);
|
|
973
1012
|
};
|
|
974
1013
|
|
|
975
1014
|
node.closeWebSocket = (kind) => {
|
|
976
|
-
const propertyName = kind
|
|
1015
|
+
const propertyName = SOCKET_PROPERTIES[kind];
|
|
977
1016
|
const socket = node[propertyName];
|
|
978
1017
|
if (!socket) return;
|
|
979
1018
|
// Clear ownership before close so the close callback knows this was
|
|
@@ -1005,6 +1044,15 @@ module.exports = function(RED) {
|
|
|
1005
1044
|
} else if (!needsEvents) {
|
|
1006
1045
|
node.closeWebSocket("events");
|
|
1007
1046
|
}
|
|
1047
|
+
|
|
1048
|
+
// Protect 7.4+ stream. Today only external arm profiles need it;
|
|
1049
|
+
// device and event updates keep their own 7.3-compatible streams.
|
|
1050
|
+
const needsUpdates = node.nodeClients.some((client) => clientNeedsProtectStream(client, "updates", "handleProtectModelUpdate"));
|
|
1051
|
+
if (needsUpdates && !node.wsUpdates) {
|
|
1052
|
+
node.attachSocket("updates", `/v1/subscribe/updates?modelKeys=${UPDATES_STREAM_MODEL_KEYS.join(",")}`, node.broadcastModelUpdate);
|
|
1053
|
+
} else if (!needsUpdates) {
|
|
1054
|
+
node.closeWebSocket("updates");
|
|
1055
|
+
}
|
|
1008
1056
|
};
|
|
1009
1057
|
|
|
1010
1058
|
node.closeWebSockets = () => {
|
|
@@ -1015,6 +1063,7 @@ module.exports = function(RED) {
|
|
|
1015
1063
|
|
|
1016
1064
|
node.closeWebSocket("devices");
|
|
1017
1065
|
node.closeWebSocket("events");
|
|
1066
|
+
node.closeWebSocket("updates");
|
|
1018
1067
|
};
|
|
1019
1068
|
|
|
1020
1069
|
node.addClient = (client) => {
|
|
@@ -1206,10 +1206,11 @@
|
|
|
1206
1206
|
<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. A saved selection that is temporarily unavailable is preserved instead of being cleared.</p>
|
|
1207
1207
|
<p><b>Action</b> only shows actions supported by the selected device.</p>
|
|
1208
1208
|
<p>Additional fields appear only when the selected action needs extra input. For example, events, PTZ presets, live views, or discovered properties.</p>
|
|
1209
|
-
<p>Action highlights: <code>Receive Events</code>, <code>Read Device State</code>, <code>Take Snapshot</code>, <code>Show Doorbell Message</code>, siren play/stop/test, relay output control, speaker sound test, and application/alarm actions on <code>NVR</code>.</p>
|
|
1209
|
+
<p>Action highlights: <code>Receive Events</code>, <code>Read Device State</code>, <code>Take Snapshot</code>, <code>Show Doorbell Message</code>, siren play/stop/test, relay output control, speaker sound test and speaker siren stop (Protect 7.4+), and application/alarm actions on <code>NVR</code>.</p>
|
|
1210
1210
|
<p>Protect resources include cameras, sensors, lights, chimes, viewers, live views, NVRs, bridges, Link Stations, alarm hubs, key fobs, relays, sirens, and speakers. The newer device families expose read state and events without the generic raw-update action.</p>
|
|
1211
1211
|
<p><code>Play Siren</code> uses a duration in seconds. <code>Control Relay Output</code> requires an explicit output and on/off state; auto-off applies only when turning an output on. <code>Trigger Alarm Hub Output</code> can toggle an output or set it on/off, with an optional delay and duration. Sound-test actions use a configured volume and never read control values from the incoming message.</p>
|
|
1212
1212
|
<p>The <code>NVR</code> arm profile commands control Protect's local Alarm Manager; they do not necessarily control the alarm managed by an Alarm Hub or a global Alarm Manager. Use <code>Read Arm Profiles</code>, <code>Select Arm Profile</code>, <code>Arm Alarm</code>, and <code>Disarm Alarm</code>. These endpoints require Protect's local Alarm Manager.</p>
|
|
1213
|
+
<p>When the NVR uses UniFi OS's external Alarm Manager (Protect 7.4 or later, <code>featureFlags.useExternalAlarmManager</code> is <code>true</code>), use the external arm profile actions instead: <code>Read External Arm Profiles</code>, <code>Arm External Profile</code>, <code>Disarm External Profile</code>, <code>Create External Arm Profile</code> and <code>Update External Arm Profile</code> (fields from <code>msg.payload</code>: <code>title</code>, <code>activation_delay</code>, <code>deactivation_delay</code>, <code>schedule</code>; in an update, <code>null</code> clears a delay or the schedule), and <code>Delete External Arm Profile</code>. <code>Receive External Alarm Status</code> emits the state of one or all profiles (<code>disarmed</code>, <code>arming</code>, <code>armed</code>, <code>breaching</code>, <code>breached</code>) in <code>msg.payload</code> each time it changes, with <code>msg.armProfileId</code>, <code>msg.armProfileTitle</code> and the profile in <code>msg.details.externalArmProfile</code>. In this mode <code>Read Alarm State</code> on the NVR reports an error instead of the stale local status. The console may need a UniFi OS update before external arm profiles are available.</p>
|
|
1213
1214
|
<p><code>NVR → Trigger Alarm Manager Webhook</code> triggers the Alarm Manager alarms whose webhook trigger uses the same <b>Webhook ID</b>. Configure the alarm in Protect first; the ID is case-sensitive text, for example <code>garage-door</code>. A successful request returns HTTP 204 with an empty <code>msg.payload</code>.</p>
|
|
1214
1215
|
<p><code>Read Alarm State</code> is available on <code>Alarm Hub</code> and <code>NVR</code>. On an Alarm Hub it reads <code>alarmHub.armed</code> and emits <code>true</code> for <code>on</code> or <code>false</code> for <code>off</code>. On an NVR it emits the local Alarm Manager's <code>armMode.status</code> string without converting transitional or unknown statuses to a boolean. The full device remains in <code>msg.details.device</code>. Missing or unsupported state goes to output 2; it never means disarmed. Trigger this action with an Inject node or enable periodic reads for recovery after missed updates or controller restarts.</p>
|
|
1215
1216
|
<p>For live state, choose <code>Receive Events → Armed</code> on an Alarm Hub, or <code>Receive Events → Alarm status (local)</code> on an NVR. These values follow device updates, rather than assuming a dedicated arm/disarm event exists. Enable startup emission if you need an initial value after deploy. Delivery depends on Protect publishing device updates; use periodic <code>Read Alarm State</code> for recovery. NVR state and Hub state are separate sources and can differ.</p>
|
|
@@ -24,7 +24,7 @@ const {
|
|
|
24
24
|
} = require("./utils/common-utils");
|
|
25
25
|
const { parseKnownPlates, namePlate, matchesDetection, describeDetection } = require("./utils/unifi-protect-detections");
|
|
26
26
|
const { LICENSE_PLATE_EVENT_TYPES, extractLicensePlates, isLicensePlateEvent } = require("./utils/unifi-protect-lpr");
|
|
27
|
-
const { readAlarmState, matchesDeviceUpdate, mergeDeviceUpdate, buildRequestError } = require("./utils/unifi-protect-alarm");
|
|
27
|
+
const { readAlarmState, usesExternalAlarmManager, matchesDeviceUpdate, mergeDeviceUpdate, buildRequestError } = require("./utils/unifi-protect-alarm");
|
|
28
28
|
const DEFAULT_REQUEST_TIMEOUT_MS = 15000;
|
|
29
29
|
const LPR_FOLLOW_UP_DELAYS_MS = [150, 350, 750, 1500, 3000];
|
|
30
30
|
const LPR_OPEN_EVENT_POLL_MS = 1000;
|
|
@@ -136,6 +136,12 @@ module.exports = function(RED) {
|
|
|
136
136
|
|
|
137
137
|
if (isLicensePlateObservation() || isPhotoObservation()) node.protectStreams = ["events"];
|
|
138
138
|
|
|
139
|
+
// Protect 7.4+: state of UniFi OS's external Alarm Manager profiles,
|
|
140
|
+
// read from /v1/external/arm-profiles and the /v1/subscribe/updates stream.
|
|
141
|
+
const isExternalArmObservation = () => node.deviceType === "nvr" && node.capability === "observeExternalArmProfiles";
|
|
142
|
+
const externalArmProfiles = new Map();
|
|
143
|
+
if (isExternalArmObservation()) node.protectStreams = ["updates"];
|
|
144
|
+
|
|
139
145
|
function setNodeStatus(status) {
|
|
140
146
|
if (!status || typeof status !== "object" || Array.isArray(status)) {
|
|
141
147
|
return;
|
|
@@ -246,7 +252,75 @@ module.exports = function(RED) {
|
|
|
246
252
|
return outputMsg;
|
|
247
253
|
}
|
|
248
254
|
|
|
255
|
+
function emitExternalArmProfile(send, profile, source, updateType) {
|
|
256
|
+
node.currentObservableValue = profile.state;
|
|
257
|
+
const outputMsg = {
|
|
258
|
+
payload: profile.state,
|
|
259
|
+
topic: resolveNodeName(node.name),
|
|
260
|
+
deviceName: resolveOutputDeviceName(node.currentDevice) || undefined,
|
|
261
|
+
eventName: "externalArmProfile",
|
|
262
|
+
armProfileId: profile.id,
|
|
263
|
+
armProfileTitle: profile.title
|
|
264
|
+
};
|
|
265
|
+
attachDetails(outputMsg, {
|
|
266
|
+
externalArmProfile: profile,
|
|
267
|
+
unifiProtect: buildBaseMetadata(node.deviceType, node.deviceId, "observeExternalArmProfiles", {
|
|
268
|
+
source,
|
|
269
|
+
updateType: updateType || ""
|
|
270
|
+
})
|
|
271
|
+
});
|
|
272
|
+
setNodeStatus({ fill: "green", shape: "dot", text: `${profile.title || profile.id}: ${profile.state}` });
|
|
273
|
+
sendWithPayload(send, outputMsg, node.emitStartupAndUndefined);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
async function fetchExternalArmProfileStates(capabilityConfig, send, source) {
|
|
277
|
+
if (typeof node.server.fetchExternalArmProfiles !== "function") {
|
|
278
|
+
throw new Error("This Protect configuration node cannot read external arm profiles.");
|
|
279
|
+
}
|
|
280
|
+
const profiles = await node.server.fetchExternalArmProfiles();
|
|
281
|
+
if (!node.isObserving && source === "startup") return;
|
|
282
|
+
const selectedId = String(capabilityConfig.externalArmProfileId || "").trim();
|
|
283
|
+
externalArmProfiles.clear();
|
|
284
|
+
profiles.filter((profile) => profile && profile.id && (!selectedId || profile.id === selectedId))
|
|
285
|
+
.forEach((profile) => externalArmProfiles.set(profile.id, profile));
|
|
286
|
+
if (selectedId && !externalArmProfiles.has(selectedId)) {
|
|
287
|
+
throw new Error(`External arm profile ${selectedId} was not found.`);
|
|
288
|
+
}
|
|
289
|
+
if (source === "startup" && !node.emitStartupAndUndefined) {
|
|
290
|
+
setNodeStatus({ fill: "green", shape: "dot", text: `${externalArmProfiles.size} external arm profile(s)` });
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
externalArmProfiles.forEach((profile) => emitExternalArmProfile(send, profile, source, ""));
|
|
294
|
+
if (externalArmProfiles.size === 0) setNodeStatus({ fill: "yellow", shape: "ring", text: "no external arm profiles" });
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
node.handleProtectModelUpdate = (update) => {
|
|
298
|
+
try {
|
|
299
|
+
const item = update && update.item;
|
|
300
|
+
if (!item || item.modelKey !== "externalArmProfile" || !node.isObserving || !isExternalArmObservation()) return;
|
|
301
|
+
const selectedId = String(parseCapabilityConfig(node.capabilityConfig).externalArmProfileId || "").trim();
|
|
302
|
+
if (!item.id || (selectedId && item.id !== selectedId)) return;
|
|
303
|
+
if (update.type === "remove") {
|
|
304
|
+
externalArmProfiles.delete(item.id);
|
|
305
|
+
setNodeStatus({ fill: "yellow", shape: "ring", text: "external arm profile removed" });
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
const profile = mergeDeviceUpdate(externalArmProfiles.get(item.id), item);
|
|
309
|
+
externalArmProfiles.set(item.id, profile);
|
|
310
|
+
// A partial update without a state (e.g. a renamed profile)
|
|
311
|
+
// must not replay the previous state as a new change.
|
|
312
|
+
if (update.type !== "add" && !Object.prototype.hasOwnProperty.call(item, "state")) return;
|
|
313
|
+
if (typeof profile.state !== "string") return;
|
|
314
|
+
emitExternalArmProfile(node.send.bind(node), profile, "updates", update.type || "");
|
|
315
|
+
} catch (error) {
|
|
316
|
+
}
|
|
317
|
+
};
|
|
318
|
+
|
|
249
319
|
async function fetchDeviceState(deviceType, deviceId, capabilityConfig, send, source) {
|
|
320
|
+
if (isExternalArmObservation()) {
|
|
321
|
+
await fetchExternalArmProfileStates(capabilityConfig, send, source);
|
|
322
|
+
return;
|
|
323
|
+
}
|
|
250
324
|
const payload = await node.server.fetchDeviceByTypeAndId(deviceType, deviceId);
|
|
251
325
|
node.currentDevice = payload;
|
|
252
326
|
|
|
@@ -265,6 +339,10 @@ module.exports = function(RED) {
|
|
|
265
339
|
}
|
|
266
340
|
|
|
267
341
|
const stateMsg = buildObservedStateMessage(deviceType, deviceId, capabilityConfig, payload, source);
|
|
342
|
+
if (deviceType === "nvr" && resolveConfiguredObservable(capabilityConfig) === "armStatus" && usesExternalAlarmManager(payload)) {
|
|
343
|
+
setNodeStatus({ fill: "yellow", shape: "ring", text: "external Alarm Manager: use Receive External Alarm Status" });
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
268
346
|
|
|
269
347
|
setNodeStatus({ fill: "green", shape: "dot", text: buildNodeStatus(deviceType, payload) });
|
|
270
348
|
if (source === "startup" && !node.emitStartupAndUndefined) return;
|
|
@@ -349,6 +427,9 @@ module.exports = function(RED) {
|
|
|
349
427
|
decorateOutputMessage(stateMsg, response.payload, `request:${capabilityId}`);
|
|
350
428
|
if (capabilityId === "getAlarmState") {
|
|
351
429
|
const alarmState = readAlarmState(deviceType, response.payload);
|
|
430
|
+
if (alarmState === undefined && deviceType === "nvr" && usesExternalAlarmManager(response.payload)) {
|
|
431
|
+
throw new Error("This NVR uses UniFi OS's external Alarm Manager, so armMode is not the alarm state. Use Read External Arm Profiles or Receive External Alarm Status.");
|
|
432
|
+
}
|
|
352
433
|
if (alarmState === undefined) {
|
|
353
434
|
throw new Error(`The ${deviceType} response has no supported alarm state. Check the Protect version and alarm mode; missing state does not mean disarmed.`);
|
|
354
435
|
}
|
|
@@ -506,7 +587,7 @@ module.exports = function(RED) {
|
|
|
506
587
|
node.handleProtectDeviceUpdate = (update) => {
|
|
507
588
|
try {
|
|
508
589
|
const item = update && update.item;
|
|
509
|
-
if (!item || !node.isObserving) {
|
|
590
|
+
if (!item || !node.isObserving || isExternalArmObservation()) {
|
|
510
591
|
return;
|
|
511
592
|
}
|
|
512
593
|
|
|
@@ -763,7 +844,7 @@ module.exports = function(RED) {
|
|
|
763
844
|
node.handleProtectEventUpdate = (update) => {
|
|
764
845
|
try {
|
|
765
846
|
const item = update && update.item;
|
|
766
|
-
if (!item || item.modelKey !== "event" || !node.isObserving || item.device !== node.deviceId) {
|
|
847
|
+
if (!item || item.modelKey !== "event" || !node.isObserving || item.device !== node.deviceId || isExternalArmObservation()) {
|
|
767
848
|
return;
|
|
768
849
|
}
|
|
769
850
|
|
|
@@ -7,11 +7,28 @@ function readAlarmState(deviceType, device) {
|
|
|
7
7
|
if (value === false || value === "off") return false;
|
|
8
8
|
return undefined;
|
|
9
9
|
}
|
|
10
|
+
// Protect 7.4+: with UniFi OS's external Alarm Manager, armMode.status is
|
|
11
|
+
// stale local state. External profiles carry their own state instead.
|
|
12
|
+
if (deviceType === "nvr" && usesExternalAlarmManager(device)) return undefined;
|
|
10
13
|
const status = device && device.armMode && device.armMode.status;
|
|
11
14
|
return deviceType === "nvr" && typeof status === "string" && status.trim()
|
|
12
15
|
? status : undefined;
|
|
13
16
|
}
|
|
14
17
|
|
|
18
|
+
function usesExternalAlarmManager(device) {
|
|
19
|
+
return Boolean(device && device.featureFlags && device.featureFlags.useExternalAlarmManager === true);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Protect 7.4+ may send one update for several models, with item.id as an
|
|
23
|
+
// array of IDs. Split it so consumers keep matching a single item.id.
|
|
24
|
+
function expandModelUpdate(update) {
|
|
25
|
+
const item = update && update.item;
|
|
26
|
+
if (!item || !Array.isArray(item.id)) return [update];
|
|
27
|
+
return item.id
|
|
28
|
+
.filter((id) => typeof id === "string" && id)
|
|
29
|
+
.map((id) => ({ ...update, item: { ...item, id } }));
|
|
30
|
+
}
|
|
31
|
+
|
|
15
32
|
function matchesDeviceUpdate(deviceType, deviceId, item, modelKey) {
|
|
16
33
|
if (!item || item.id !== deviceId) return false;
|
|
17
34
|
// Partial updates may omit the model key. IDs still scope them to the
|
|
@@ -49,11 +66,14 @@ function buildRequestError(response, method, path, secrets) {
|
|
|
49
66
|
if (statusCode === 401) hint = " Check the Protect API key and its validity.";
|
|
50
67
|
if (statusCode === 403) hint = " Check the Protect permissions of the account associated with the API key.";
|
|
51
68
|
if (statusCode === 400 && path.startsWith("/v1/arm-profiles")) {
|
|
52
|
-
hint = " Arm profile commands require Protect's local Alarm Manager; check the alarm mode and selected profile. A 400 response alone does not identify a permission failure.";
|
|
69
|
+
hint = " Arm profile commands require Protect's local Alarm Manager; check the alarm mode and selected profile. If the NVR uses UniFi OS's external Alarm Manager, use the external arm profile actions. A 400 response alone does not identify a permission failure.";
|
|
70
|
+
}
|
|
71
|
+
if (statusCode === 400 && path.startsWith("/v1/external/arm-profiles")) {
|
|
72
|
+
hint = " External arm profiles require Protect 7.4 or later with UniFi OS's external Alarm Manager; the console may also need a UniFi OS update.";
|
|
53
73
|
}
|
|
54
74
|
const error = new Error(`UniFi Protect request failed with status ${statusCode}.${hint}${apiMessage ? ` API: ${apiMessage}` : ""}`);
|
|
55
75
|
error.protectResponse = { statusCode, method, path, ...(apiMessage ? { apiMessage } : {}) };
|
|
56
76
|
return error;
|
|
57
77
|
}
|
|
58
78
|
|
|
59
|
-
module.exports = { readAlarmState, matchesDeviceUpdate, mergeDeviceUpdate, buildRequestError };
|
|
79
|
+
module.exports = { readAlarmState, usesExternalAlarmManager, expandModelUpdate, matchesDeviceUpdate, mergeDeviceUpdate, buildRequestError };
|
|
@@ -152,6 +152,134 @@ const COMMON_CAPABILITIES = [
|
|
|
152
152
|
}
|
|
153
153
|
];
|
|
154
154
|
|
|
155
|
+
// Protect 7.4+: arm profiles of UniFi OS's external Alarm Manager, used when
|
|
156
|
+
// nvr.featureFlags.useExternalAlarmManager is true.
|
|
157
|
+
const EXTERNAL_ARM_PROFILE_ID_PATTERN = /^[0-9A-Za-z-]{1,64}$/;
|
|
158
|
+
const EXTERNAL_ARM_PROFILE_FIELD = {
|
|
159
|
+
id: "externalArmProfileId",
|
|
160
|
+
label: "External arm profile",
|
|
161
|
+
type: "select",
|
|
162
|
+
placeholder: "Select an external arm profile"
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
function readExternalArmProfileId(capabilityConfig, required = true) {
|
|
166
|
+
const externalArmProfileId = String(normalizeObject(capabilityConfig).externalArmProfileId || "").trim();
|
|
167
|
+
if (!externalArmProfileId) {
|
|
168
|
+
if (!required) return "";
|
|
169
|
+
throw new Error("Select an external arm profile before using this action.");
|
|
170
|
+
}
|
|
171
|
+
if (!EXTERNAL_ARM_PROFILE_ID_PATTERN.test(externalArmProfileId)) {
|
|
172
|
+
throw new Error("The external arm profile ID may contain only letters, digits and hyphens.");
|
|
173
|
+
}
|
|
174
|
+
return externalArmProfileId;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function readExternalArmProfileBody(inputPayload, requireTitle) {
|
|
178
|
+
if (!inputPayload || typeof inputPayload !== "object" || Array.isArray(inputPayload)) {
|
|
179
|
+
throw new Error("msg.payload must be an object with the external arm profile fields (title, activation_delay, deactivation_delay, schedule).");
|
|
180
|
+
}
|
|
181
|
+
if (requireTitle && !String(inputPayload.title || "").trim()) {
|
|
182
|
+
throw new Error("msg.payload.title is required to create an external arm profile.");
|
|
183
|
+
}
|
|
184
|
+
return inputPayload;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const EXTERNAL_ARM_PROFILE_CAPABILITIES = [
|
|
188
|
+
{
|
|
189
|
+
id: "getExternalArmProfiles",
|
|
190
|
+
label: "Read External Arm Profiles",
|
|
191
|
+
description: "Fetch the arm profiles of UniFi OS's external Alarm Manager, each with its state (Protect 7.4 or later).",
|
|
192
|
+
method: "GET",
|
|
193
|
+
path: "/v1/external/arm-profiles",
|
|
194
|
+
mode: "request"
|
|
195
|
+
},
|
|
196
|
+
{
|
|
197
|
+
id: "observeExternalArmProfiles",
|
|
198
|
+
label: "Receive External Alarm Status",
|
|
199
|
+
description: "Emit the state of external arm profiles (disarmed, arming, armed, breaching, breached) as it changes (Protect 7.4 or later).",
|
|
200
|
+
method: "GET",
|
|
201
|
+
path: "/v1/external/arm-profiles",
|
|
202
|
+
mode: "observe",
|
|
203
|
+
opensEventStream: true,
|
|
204
|
+
editor: {
|
|
205
|
+
fields: [{
|
|
206
|
+
...EXTERNAL_ARM_PROFILE_FIELD,
|
|
207
|
+
allowEmpty: true,
|
|
208
|
+
emptyLabel: "All external arm profiles"
|
|
209
|
+
}]
|
|
210
|
+
}
|
|
211
|
+
},
|
|
212
|
+
{
|
|
213
|
+
id: "armExternalProfile",
|
|
214
|
+
label: "Arm External Profile",
|
|
215
|
+
description: "Arm one profile of UniFi OS's external Alarm Manager (Protect 7.4 or later).",
|
|
216
|
+
method: "POST",
|
|
217
|
+
path: "/v1/external/arm-profiles/:externalArmProfileId/actions/arm",
|
|
218
|
+
mode: "request",
|
|
219
|
+
parameterNames: ["externalArmProfileId"],
|
|
220
|
+
ignoreInputPayload: true,
|
|
221
|
+
editor: { fields: [EXTERNAL_ARM_PROFILE_FIELD] },
|
|
222
|
+
requestComposer: ({ capabilityConfig }) => ({
|
|
223
|
+
params: { externalArmProfileId: readExternalArmProfileId(capabilityConfig) }
|
|
224
|
+
})
|
|
225
|
+
},
|
|
226
|
+
{
|
|
227
|
+
id: "disarmExternalProfile",
|
|
228
|
+
label: "Disarm External Profile",
|
|
229
|
+
description: "Disarm one profile of UniFi OS's external Alarm Manager (Protect 7.4 or later).",
|
|
230
|
+
method: "POST",
|
|
231
|
+
path: "/v1/external/arm-profiles/:externalArmProfileId/actions/disarm",
|
|
232
|
+
mode: "request",
|
|
233
|
+
parameterNames: ["externalArmProfileId"],
|
|
234
|
+
ignoreInputPayload: true,
|
|
235
|
+
editor: { fields: [EXTERNAL_ARM_PROFILE_FIELD] },
|
|
236
|
+
requestComposer: ({ capabilityConfig }) => ({
|
|
237
|
+
params: { externalArmProfileId: readExternalArmProfileId(capabilityConfig) }
|
|
238
|
+
})
|
|
239
|
+
},
|
|
240
|
+
{
|
|
241
|
+
id: "createExternalArmProfile",
|
|
242
|
+
label: "Create External Arm Profile",
|
|
243
|
+
description: "Create an external arm profile from msg.payload (title, optional activation_delay, deactivation_delay and schedule).",
|
|
244
|
+
method: "POST",
|
|
245
|
+
path: "/v1/external/arm-profiles",
|
|
246
|
+
mode: "request",
|
|
247
|
+
usesInputPayload: true,
|
|
248
|
+
requestComposer: ({ inputPayload }) => ({
|
|
249
|
+
payload: readExternalArmProfileBody(inputPayload, true)
|
|
250
|
+
})
|
|
251
|
+
},
|
|
252
|
+
{
|
|
253
|
+
id: "updateExternalArmProfile",
|
|
254
|
+
label: "Update External Arm Profile",
|
|
255
|
+
description: "Change the selected external arm profile with the fields in msg.payload. Omitted fields stay unchanged; null clears a delay or the schedule.",
|
|
256
|
+
method: "PATCH",
|
|
257
|
+
path: "/v1/external/arm-profiles/:externalArmProfileId",
|
|
258
|
+
mode: "request",
|
|
259
|
+
parameterNames: ["externalArmProfileId"],
|
|
260
|
+
usesInputPayload: true,
|
|
261
|
+
editor: { fields: [EXTERNAL_ARM_PROFILE_FIELD] },
|
|
262
|
+
requestComposer: ({ capabilityConfig, inputPayload }) => ({
|
|
263
|
+
params: { externalArmProfileId: readExternalArmProfileId(capabilityConfig) },
|
|
264
|
+
payload: readExternalArmProfileBody(inputPayload, false)
|
|
265
|
+
})
|
|
266
|
+
},
|
|
267
|
+
{
|
|
268
|
+
id: "deleteExternalArmProfile",
|
|
269
|
+
label: "Delete External Arm Profile",
|
|
270
|
+
description: "Delete the selected external arm profile.",
|
|
271
|
+
method: "DELETE",
|
|
272
|
+
path: "/v1/external/arm-profiles/:externalArmProfileId",
|
|
273
|
+
mode: "request",
|
|
274
|
+
parameterNames: ["externalArmProfileId"],
|
|
275
|
+
ignoreInputPayload: true,
|
|
276
|
+
editor: { fields: [EXTERNAL_ARM_PROFILE_FIELD] },
|
|
277
|
+
requestComposer: ({ capabilityConfig }) => ({
|
|
278
|
+
params: { externalArmProfileId: readExternalArmProfileId(capabilityConfig) }
|
|
279
|
+
})
|
|
280
|
+
}
|
|
281
|
+
];
|
|
282
|
+
|
|
155
283
|
const RTSPS_QUALITY_OPTIONS = ["high", "medium", "low", "package"].map((quality) => ({
|
|
156
284
|
value: quality,
|
|
157
285
|
label: quality
|
|
@@ -253,6 +381,11 @@ const SENSOR_OBSERVABLE_DEFINITIONS = [
|
|
|
253
381
|
id: "batteryLevel",
|
|
254
382
|
label: "Battery level",
|
|
255
383
|
eventTypes: []
|
|
384
|
+
},
|
|
385
|
+
{
|
|
386
|
+
id: "mountedIncorrectly",
|
|
387
|
+
label: "Mounted incorrectly",
|
|
388
|
+
eventTypes: []
|
|
256
389
|
}
|
|
257
390
|
];
|
|
258
391
|
|
|
@@ -779,7 +912,7 @@ const TYPE_CAPABILITIES = {
|
|
|
779
912
|
createSetPropertyCapability()
|
|
780
913
|
],
|
|
781
914
|
nvr: [
|
|
782
|
-
{ id: "getAlarmState", label: "Read Alarm State", description: "Read the local Alarm Manager status from the NVR.", method: "GET", pathKind: "detail", mode: "request" },
|
|
915
|
+
{ id: "getAlarmState", label: "Read Alarm State", description: "Read the local Alarm Manager status from the NVR. With UniFi OS's external Alarm Manager, use Read External Arm Profiles.", method: "GET", pathKind: "detail", mode: "request" },
|
|
783
916
|
{
|
|
784
917
|
id: "getApplicationInfo",
|
|
785
918
|
label: "Read Application Info",
|
|
@@ -873,7 +1006,8 @@ const TYPE_CAPABILITIES = {
|
|
|
873
1006
|
params: { triggerId }
|
|
874
1007
|
};
|
|
875
1008
|
}
|
|
876
|
-
}
|
|
1009
|
+
},
|
|
1010
|
+
...EXTERNAL_ARM_PROFILE_CAPABILITIES
|
|
877
1011
|
],
|
|
878
1012
|
bridge: [],
|
|
879
1013
|
linkStation: [],
|
|
@@ -1095,6 +1229,15 @@ const TYPE_CAPABILITIES = {
|
|
|
1095
1229
|
volume: parseConfiguredInteger(capabilityConfig.volume, 0, 100, 50)
|
|
1096
1230
|
}
|
|
1097
1231
|
})
|
|
1232
|
+
},
|
|
1233
|
+
{
|
|
1234
|
+
id: "stopSpeakerSiren",
|
|
1235
|
+
label: "Stop Speaker Siren",
|
|
1236
|
+
description: "Stop a siren playing on the speaker (Protect 7.4 or later).",
|
|
1237
|
+
method: "POST",
|
|
1238
|
+
path: "/v1/speakers/:id/stop-siren",
|
|
1239
|
+
mode: "request",
|
|
1240
|
+
ignoreInputPayload: true
|
|
1098
1241
|
}
|
|
1099
1242
|
]
|
|
1100
1243
|
};
|
|
@@ -1301,6 +1444,27 @@ async function getCapabilityOptions(deviceType, capabilityId, context) {
|
|
|
1301
1444
|
};
|
|
1302
1445
|
}
|
|
1303
1446
|
|
|
1447
|
+
if (deviceType === "nvr" && EXTERNAL_ARM_PROFILE_CAPABILITIES.some((entry) => entry.id === capabilityId)) {
|
|
1448
|
+
let externalArmProfiles = [];
|
|
1449
|
+
let loadError = "";
|
|
1450
|
+
try {
|
|
1451
|
+
externalArmProfiles = context && typeof context.fetchExternalArmProfiles === "function"
|
|
1452
|
+
? await context.fetchExternalArmProfiles()
|
|
1453
|
+
: [];
|
|
1454
|
+
} catch (error) {
|
|
1455
|
+
loadError = error && error.message ? error.message : String(error);
|
|
1456
|
+
}
|
|
1457
|
+
return {
|
|
1458
|
+
capabilityId,
|
|
1459
|
+
fields: [buildExternalArmProfileField(
|
|
1460
|
+
externalArmProfiles,
|
|
1461
|
+
capability.editor.fields[0],
|
|
1462
|
+
context && context.capabilityConfig,
|
|
1463
|
+
loadError
|
|
1464
|
+
)]
|
|
1465
|
+
};
|
|
1466
|
+
}
|
|
1467
|
+
|
|
1304
1468
|
if (deviceType === "camera" && capabilityId === "setDoorbellMessage") {
|
|
1305
1469
|
return buildDoorbellMessageFields(context, capability);
|
|
1306
1470
|
}
|
|
@@ -1449,6 +1613,27 @@ function buildArmProfileField(armProfiles, baseField, capabilityConfig, currentA
|
|
|
1449
1613
|
};
|
|
1450
1614
|
}
|
|
1451
1615
|
|
|
1616
|
+
function buildExternalArmProfileField(externalArmProfiles, baseField, capabilityConfig, loadError) {
|
|
1617
|
+
const options = (Array.isArray(externalArmProfiles) ? externalArmProfiles : []).map((profile) => ({
|
|
1618
|
+
value: String(profile && profile.id ? profile.id : ""),
|
|
1619
|
+
label: `${String((profile && profile.title) || (profile && profile.id) || "External arm profile")}${profile && profile.state ? ` (${profile.state})` : ""}`
|
|
1620
|
+
})).filter((option) => option.value);
|
|
1621
|
+
const discoveredProfileCount = options.length;
|
|
1622
|
+
const configuredId = String(normalizeObject(capabilityConfig).externalArmProfileId || "").trim();
|
|
1623
|
+
if (configuredId && !options.some((option) => option.value === configuredId)) {
|
|
1624
|
+
options.push({ value: configuredId, label: `${configuredId} (saved; currently unavailable)` });
|
|
1625
|
+
}
|
|
1626
|
+
|
|
1627
|
+
return {
|
|
1628
|
+
...baseField,
|
|
1629
|
+
options,
|
|
1630
|
+
defaultValue: configuredId,
|
|
1631
|
+
helpText: discoveredProfileCount > 0
|
|
1632
|
+
? "Profiles of UniFi OS's external Alarm Manager."
|
|
1633
|
+
: `No external arm profiles were returned. They require Protect 7.4 or later with UniFi OS's external Alarm Manager.${loadError ? ` ${loadError}` : ""}`
|
|
1634
|
+
};
|
|
1635
|
+
}
|
|
1636
|
+
|
|
1452
1637
|
function isCapabilitySupportedForDevice(deviceType, capability, device) {
|
|
1453
1638
|
// Protect exposes one broad family of camera APIs, but not every camera
|
|
1454
1639
|
// model supports every feature. Filter those actions in the editor.
|
|
@@ -2073,6 +2258,10 @@ function getObservableOptions(deviceType, device, configuredObservable) {
|
|
|
2073
2258
|
}
|
|
2074
2259
|
|
|
2075
2260
|
function isObservableSupportedForDevice(deviceType, device, observable) {
|
|
2261
|
+
// Protect 7.4 added sensor.isMountedIncorrectly; hide it on older versions.
|
|
2262
|
+
if (deviceType === "sensor" && observable === "mountedIncorrectly" && device && typeof device === "object") {
|
|
2263
|
+
return Object.prototype.hasOwnProperty.call(device, "isMountedIncorrectly");
|
|
2264
|
+
}
|
|
2076
2265
|
// Older Protect versions do not report sensor feature flags: keep every
|
|
2077
2266
|
// observable available unless the sensor explicitly describes its features.
|
|
2078
2267
|
const featureFlags = deviceType === "sensor" ? getValueAtPath(device, "featureFlags") : undefined;
|
|
@@ -2230,7 +2419,8 @@ function resolveSensorObservableState(sensor, observable, fallbackValue) {
|
|
|
2230
2419
|
"status.batteryPercentage",
|
|
2231
2420
|
"stats.battery.percentage",
|
|
2232
2421
|
"stats.batteryLevel"
|
|
2233
|
-
])
|
|
2422
|
+
]),
|
|
2423
|
+
mountedIncorrectly: () => firstDefinedValue(sensor, ["isMountedIncorrectly"])
|
|
2234
2424
|
};
|
|
2235
2425
|
|
|
2236
2426
|
const rawValue = typeof directValueResolvers[observable] === "function"
|
|
@@ -31,11 +31,21 @@ const TYPE_FIELD_METADATA = {
|
|
|
31
31
|
nvr: {
|
|
32
32
|
armStatus: {
|
|
33
33
|
label: "Alarm Status (Local)",
|
|
34
|
-
description: "Local Alarm Manager status from armMode.status. Kept as a string, including transitional states; it may differ from an Alarm Hub's armed state.",
|
|
34
|
+
description: "Local Alarm Manager status from armMode.status. Kept as a string, including transitional states; it may differ from an Alarm Hub's armed state. Not used when the NVR runs UniFi OS's external Alarm Manager.",
|
|
35
|
+
source: "official"
|
|
36
|
+
},
|
|
37
|
+
"featureFlags.useExternalAlarmManager": {
|
|
38
|
+
label: "Uses External Alarm Manager",
|
|
39
|
+
description: "True when UniFi OS runs the external Alarm Manager instead of Protect's local one (Protect 7.4 or later).",
|
|
35
40
|
source: "official"
|
|
36
41
|
}
|
|
37
42
|
},
|
|
38
43
|
camera: {
|
|
44
|
+
videoMode: {
|
|
45
|
+
label: "Video Mode",
|
|
46
|
+
description: "Camera video mode: default, highFps, sport, slowShutter, lprReflex, lprNoneReflex or antiFlicker (Protect 7.4 or later). Supported values are in featureFlags.videoModes.",
|
|
47
|
+
source: "official"
|
|
48
|
+
},
|
|
39
49
|
isMicEnabled: {
|
|
40
50
|
label: "Microphone Enabled",
|
|
41
51
|
description: "Controls whether the camera microphone is active.",
|
|
@@ -324,6 +334,11 @@ const OBSERVABLE_METADATA = {
|
|
|
324
334
|
label: "Battery Level",
|
|
325
335
|
description: "Current battery percentage reported by the sensor.",
|
|
326
336
|
source: "inferred"
|
|
337
|
+
},
|
|
338
|
+
mountedIncorrectly: {
|
|
339
|
+
label: "Mounted Incorrectly",
|
|
340
|
+
description: "True when Protect reports that the sensor is mounted incorrectly (isMountedIncorrectly, Protect 7.4 or later).",
|
|
341
|
+
source: "official"
|
|
327
342
|
}
|
|
328
343
|
},
|
|
329
344
|
light: {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "node-red-contrib-unifi-ultimate",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.27",
|
|
4
4
|
"description": "Node-RED nodes for Ubiquiti UniFi integrations, including UniFi Protect, UniFi Access, and UniFi Network.",
|
|
5
5
|
"files": [
|
|
6
6
|
"nodes/",
|
|
@@ -81,4 +81,4 @@
|
|
|
81
81
|
"bugs": {
|
|
82
82
|
"url": "https://github.com/Supergiovane/node-red-contrib-unifi-ultimate/issues"
|
|
83
83
|
}
|
|
84
|
-
}
|
|
84
|
+
}
|