From 110db5ddc37f6a20d11691db5a250ab798b7ef0e Mon Sep 17 00:00:00 2001 From: Felipe Santos Date: Sun, 15 May 2022 15:31:24 -0300 Subject: [PATCH 1/4] Always switch non-Frigate cameras to live view --- src/card.ts | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/card.ts b/src/card.ts index 59aaa443..a17c889a 100644 --- a/src/card.ts +++ b/src/card.ts @@ -5,7 +5,7 @@ import { LitElement, PropertyValues, TemplateResult, - unsafeCSS + unsafeCSS, } from 'lit'; import { customElement, property, state } from 'lit/decorators.js'; import { classMap } from 'lit/directives/class-map.js'; @@ -20,7 +20,7 @@ import { ConditionState, conditionStateRequestHandler, getOverriddenConfig, - getOverridesByKey + getOverridesByKey, } from './card-condition.js'; import './components/elements.js'; import { FrigateCardElements } from './components/elements.js'; @@ -40,7 +40,7 @@ import { CAMERA_BIRDSEYE, CARD_VERSION, MEDIA_PLAYER_SUPPORT_BROWSE_MEDIA, - REPO_URL + REPO_URL, } from './const.js'; import './editor.js'; import { localize } from './localize/localize.js'; @@ -54,7 +54,7 @@ import type { FrigateCardConfig, MediaShowInfo, MenuButton, - Message + Message, } from './types.js'; import { Actions, @@ -65,14 +65,14 @@ import { FrigateCardCustomAction, FrigateCardView, FRIGATE_CARD_VIEWS_USER_SPECIFIED, - RawFrigateCardConfig + RawFrigateCardConfig, } from './types.js'; import { convertActionToFrigateCardCustomAction, createFrigateCardCustomAction, frigateCardHandleAction, frigateCardHasAction, - getActionConfigGivenAction + getActionConfigGivenAction, } from './utils/action.js'; import { contentsChanged } from './utils/basic.js'; import { getCameraIcon, getCameraID, getCameraTitle } from './utils/camera.js'; @@ -82,7 +82,7 @@ import { homeAssistantSignPath, homeAssistantWSRequest, shouldUpdateBasedOnHass, - sideLoadHomeAssistantElements + sideLoadHomeAssistantElements, } from './utils/ha'; import { getEventID } from './utils/ha/browse-media.js'; import { supportsFeature } from './utils/ha/update.js'; @@ -1081,7 +1081,10 @@ export class FrigateCard extends LitElement { view: new View({ view: this._getConfig().view.camera_select === 'current' - ? this._view.view + ? // Always go to live view when switching to a non-Frigate camera + this._cameras?.get(camera)?.camera_name + ? this._view.view + : 'live' : (this._getConfig().view.camera_select as FrigateCardView), camera: camera, }), From e1c509b4c467ee31de7612f59eedc515f0c7f609 Mon Sep 17 00:00:00 2001 From: Felipe Santos Date: Mon, 16 May 2022 13:02:59 -0300 Subject: [PATCH 2/4] Try harder to retain current view For non-Frigate cameras when switching. --- src/card.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/card.ts b/src/card.ts index a17c889a..cba2ea2b 100644 --- a/src/card.ts +++ b/src/card.ts @@ -1081,10 +1081,16 @@ export class FrigateCard extends LitElement { view: new View({ view: this._getConfig().view.camera_select === 'current' - ? // Always go to live view when switching to a non-Frigate camera + ? // When switching to a Frigate camera, this._cameras?.get(camera)?.camera_name + ? // always retain the current view. + this._view.view + : // But when switching to a non-Frigate camera, + // only retain the current view if it's supported + ['timeline', 'image', 'live'].includes(this._view.view) ? this._view.view - : 'live' + : // And fallback to the live view otherwise. + 'live' : (this._getConfig().view.camera_select as FrigateCardView), camera: camera, }), From 8c9513a1a1ab9f86d1a231f269b8fe20185b3117 Mon Sep 17 00:00:00 2001 From: Felipe Santos Date: Tue, 24 May 2022 13:42:19 -0300 Subject: [PATCH 3/4] Fallback to supported view even when action --- src/card.ts | 24 +++++++++++------------- src/view.ts | 9 +++++++++ 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/card.ts b/src/card.ts index 1c7559f1..027088b9 100644 --- a/src/card.ts +++ b/src/card.ts @@ -1246,21 +1246,19 @@ export class FrigateCard extends LitElement { case 'camera_select': const camera = frigateCardAction.camera; if (this._cameras?.has(camera) && this._view) { + let targetView = + this._getConfig().view.camera_select === 'current' + ? this._view.view + : (this._getConfig().view.camera_select as FrigateCardView); + // Fallback to supported views for non-Frigate cameras. + if (!this._cameras?.get(camera)?.camera_name) { + targetView = View.supportsNonFrigateCameras(targetView) + ? targetView + : 'live'; + } this._changeView({ view: new View({ - view: - this._getConfig().view.camera_select === 'current' - ? // When switching to a Frigate camera, - this._cameras?.get(camera)?.camera_name - ? // always retain the current view. - this._view.view - : // But when switching to a non-Frigate camera, - // only retain the current view if it's supported - ['timeline', 'image', 'live'].includes(this._view.view) - ? this._view.view - : // And fallback to the live view otherwise. - 'live' - : (this._getConfig().view.camera_select as FrigateCardView), + view: targetView, camera: camera, }), }); diff --git a/src/view.ts b/src/view.ts index e73577cb..bb01afc4 100644 --- a/src/view.ts +++ b/src/view.ts @@ -35,6 +35,15 @@ export class View { this.context = params.context ?? null; } + /** + * Determines whether a view is supported on non-Frigate cameras. + * @param view The view to determine support for. + * @returns Whether the view is supported by non-Frigate cameras. + */ + public static supportsNonFrigateCameras(view: FrigateCardView): boolean { + return ['timeline', 'image', 'live'].includes(view); + } + /** * Clone a view. */ From 6a5c3e3f5b65624acdb681cb43d9ffef85e964a2 Mon Sep 17 00:00:00 2001 From: Felipe Santos Date: Tue, 24 May 2022 13:59:24 -0300 Subject: [PATCH 4/4] Implement selectBestViewForNonFrigateCameras --- src/card.ts | 13 +++++-------- src/view.ts | 10 +++++----- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/src/card.ts b/src/card.ts index 027088b9..e0a2f98c 100644 --- a/src/card.ts +++ b/src/card.ts @@ -1246,19 +1246,16 @@ export class FrigateCard extends LitElement { case 'camera_select': const camera = frigateCardAction.camera; if (this._cameras?.has(camera) && this._view) { - let targetView = + const targetView = this._getConfig().view.camera_select === 'current' ? this._view.view : (this._getConfig().view.camera_select as FrigateCardView); - // Fallback to supported views for non-Frigate cameras. - if (!this._cameras?.get(camera)?.camera_name) { - targetView = View.supportsNonFrigateCameras(targetView) - ? targetView - : 'live'; - } this._changeView({ view: new View({ - view: targetView, + view: this._cameras?.get(camera)?.camera_name + ? targetView + : // Fallback to supported views for non-Frigate cameras. + View.selectBestViewForNonFrigateCameras(targetView), camera: camera, }), }); diff --git a/src/view.ts b/src/view.ts index bb01afc4..86bcda83 100644 --- a/src/view.ts +++ b/src/view.ts @@ -36,12 +36,12 @@ export class View { } /** - * Determines whether a view is supported on non-Frigate cameras. - * @param view The view to determine support for. - * @returns Whether the view is supported by non-Frigate cameras. + * Selects the best view for a non-Frigate camera. + * @param view The wanted view. + * @returns The closest view supported by the non-Frigate camera. */ - public static supportsNonFrigateCameras(view: FrigateCardView): boolean { - return ['timeline', 'image', 'live'].includes(view); + public static selectBestViewForNonFrigateCameras(view: FrigateCardView) { + return ['timeline', 'image'].includes(view) ? view : 'live'; } /**