From acbbef1bdb79bbae26b0407f3c3eb95b41c23dee Mon Sep 17 00:00:00 2001 From: Dermot Duffy Date: Sat, 11 Jun 2022 16:06:48 -0700 Subject: [PATCH] Deduplicate Frigate cameras on the timeline. --- src/components/timeline.ts | 13 ++++++++++--- src/utils/frigate.ts | 26 +++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/components/timeline.ts b/src/components/timeline.ts index e2410794..51d46e48 100644 --- a/src/components/timeline.ts +++ b/src/components/timeline.ts @@ -50,6 +50,8 @@ import { getCameraTitle } from '../utils/camera.js'; import { getRecordingSegments, getRecordingsSummary, + getUniqueFrigateCameraEventsID, + getUniqueFrigateCameraID, RecordingSegments, RecordingSummary } from '../utils/frigate'; @@ -597,9 +599,9 @@ export class FrigateCardTimelineCore extends LitElement { // There is a single set of recordings for a given Frigate camera name. // Zones on that same camera do not get separate recordings. The card may - // have multiple instances of the same camera for different zoness, so + // have multiple instances of the same camera for different zones, so // need to enforce uniqueness here. - const uniqueID = `${config.frigate.client_id}/${config.frigate.camera_name}`; + const uniqueID = getUniqueFrigateCameraID(config); if (processedCameras.has(uniqueID)) { continue; } @@ -886,11 +888,16 @@ export class FrigateCardTimelineCore extends LitElement { */ protected _getGroups(): DataGroupCollectionType { const groups: FrigateCardGroupData[] = []; + const processedCameras: Set = new Set(); + this.cameras?.forEach((cameraConfig, camera) => { + const frigateCameraID = getUniqueFrigateCameraEventsID(cameraConfig); if ( cameraConfig.frigate.camera_name && - cameraConfig.frigate.camera_name !== CAMERA_BIRDSEYE + cameraConfig.frigate.camera_name !== CAMERA_BIRDSEYE && + !processedCameras.has(frigateCameraID) ) { + processedCameras.add(frigateCameraID); groups.push({ id: camera, content: getCameraTitle(this.hass, cameraConfig), diff --git a/src/utils/frigate.ts b/src/utils/frigate.ts index 0910ab36..162cf3c6 100644 --- a/src/utils/frigate.ts +++ b/src/utils/frigate.ts @@ -1,5 +1,5 @@ import { z } from 'zod'; -import { ExtendedHomeAssistant } from '../types'; +import { CameraConfig, ExtendedHomeAssistant } from '../types'; import { homeAssistantHTTPRequest } from './ha'; export const FRIGATE_ICON_SVG_PATH = @@ -95,3 +95,27 @@ export const getRecordingSegments = async ( }), ); }; + +/** + * Get an id that unique identifies a particular camera (not zone, object, etc) + * within a particular Frigate instance. ID will not (necessarily) be unique + * within the card. + * @param cameraConfig The camera config. + */ +export const getUniqueFrigateCameraID = (config: CameraConfig): string => { + return [config.frigate.client_id, config.frigate.camera_name].join('/'); +}; + +/** + * Get an id that unique identifies a source of Frigate events. ID will not + * (necessarily) be unique within the card. + * @param cameraConfig The camera config. + */ +export const getUniqueFrigateCameraEventsID = (config: CameraConfig): string => { + return [ + config.frigate.client_id, + config.frigate.camera_name, + config.frigate.label, + config.frigate.zone, + ].join('/'); +};