From 9c027ca447c8f17692209e46b01315872dcfb92a Mon Sep 17 00:00:00 2001 From: Dermot Duffy Date: Sun, 7 Nov 2021 16:26:52 -0800 Subject: [PATCH] Use configuration consts. --- src/config-mgmt.ts | 55 ++++++++---- src/const.ts | 41 ++++++++- src/editor.ts | 218 ++++++++++++++++++++++++++------------------- 3 files changed, 206 insertions(+), 108 deletions(-) diff --git a/src/config-mgmt.ts b/src/config-mgmt.ts index 88055dfc..32092cea 100644 --- a/src/config-mgmt.ts +++ b/src/config-mgmt.ts @@ -1,5 +1,22 @@ import delve from 'dlv'; import { dset } from 'dset'; +import { + CONF_EVENT_VIEWER_AUTOPLAY_CLIP, + CONF_EVENT_VIEWER_CONTROLS_NEXT_PREVIOUS_SIZE, + CONF_EVENT_VIEWER_CONTROLS_NEXT_PREVIOUS_STYLE, + CONF_FRIGATE_CAMERA_NAME, + CONF_FRIGATE_CLIENT_ID, + CONF_FRIGATE_LABEL, + CONF_FRIGATE_URL, + CONF_FRIGATE_ZONE, + CONF_IMAGE_SRC, + CONF_LIVE_PRELOAD, + CONF_LIVE_PROVIDER, + CONF_MENU_BUTTON_SIZE, + CONF_MENU_MODE, + CONF_VIEW_DEFAULT, + CONF_VIEW_TIMEOUT, +} from './const'; import { RawFrigateCardConfig } from './types'; /** @@ -22,7 +39,11 @@ export const setConfigValue = ( * @param key The key to the property to retrieve. * @returns The property or undefined if not found. */ -export const getConfigValue = (obj: RawFrigateCardConfig, key: string, def?: unknown): unknown => { +export const getConfigValue = ( + obj: RawFrigateCardConfig, + key: string, + def?: unknown, +): unknown => { return delve(obj, key, def); }; @@ -93,7 +114,7 @@ export const trimConfig = function (obj: RawFrigateCardConfig): void { */ export const copyConfig = function (obj: RawFrigateCardConfig): RawFrigateCardConfig { return JSON.parse(JSON.stringify(obj)); -} +}; /** * Determines if a property is not an object. @@ -132,21 +153,21 @@ const upgradeMoveTo = function ( const UPGRADES = [ // v1.2.1 -> v2.0.0 - upgradeMoveTo('frigate_url', 'frigate.url'), - upgradeMoveTo('frigate_client_id', 'frigate.client_id'), - upgradeMoveTo('frigate_camera_name', 'frigate.camera_name'), - upgradeMoveTo('label', 'frigate.label'), - upgradeMoveTo('zone', 'frigate.zone'), - upgradeMoveTo('view_default', 'view.default'), - upgradeMoveTo('view_timeout', 'view.timeout'), - upgradeMoveTo('live_provider', 'live.provider'), - upgradeMoveTo('live_preload', 'live.preload'), + upgradeMoveTo('frigate_url', CONF_FRIGATE_URL), + upgradeMoveTo('frigate_client_id', CONF_FRIGATE_CLIENT_ID), + upgradeMoveTo('frigate_camera_name', CONF_FRIGATE_CAMERA_NAME), + upgradeMoveTo('label', CONF_FRIGATE_LABEL), + upgradeMoveTo('zone', CONF_FRIGATE_ZONE), + upgradeMoveTo('view_default', CONF_VIEW_DEFAULT), + upgradeMoveTo('view_timeout', CONF_VIEW_TIMEOUT), + upgradeMoveTo('live_provider', CONF_LIVE_PROVIDER), + upgradeMoveTo('live_preload', CONF_LIVE_PRELOAD), upgradeMoveTo('webrtc', 'live.webrtc'), - upgradeMoveTo('autoplay_clip', 'event_viewer.autoplay_clip'), - upgradeMoveTo('controls.nextprev', 'event_viewer.controls.next_previous.style'), - upgradeMoveTo('controls.nextprev_size', 'event_viewer.controls.next_previous.size'), - upgradeMoveTo('menu_mode', 'menu.mode'), + upgradeMoveTo('autoplay_clip', CONF_EVENT_VIEWER_AUTOPLAY_CLIP), + upgradeMoveTo('controls.nextprev', CONF_EVENT_VIEWER_CONTROLS_NEXT_PREVIOUS_STYLE), + upgradeMoveTo('controls.nextprev_size', CONF_EVENT_VIEWER_CONTROLS_NEXT_PREVIOUS_SIZE), + upgradeMoveTo('menu_mode', CONF_MENU_MODE), upgradeMoveTo('menu_buttons', 'menu.buttons'), - upgradeMoveTo('menu_button_size', 'menu.button_size'), - upgradeMoveTo('image', 'image.src', isNotObject), + upgradeMoveTo('menu_button_size', CONF_MENU_BUTTON_SIZE), + upgradeMoveTo('image', CONF_IMAGE_SRC, isNotObject), ]; diff --git a/src/const.ts b/src/const.ts index 058b89a5..09d93f4d 100644 --- a/src/const.ts +++ b/src/const.ts @@ -1,3 +1,42 @@ export const CARD_VERSION = '2.0.0'; -export const REPO_URL = "https://github.com/dermotduffy/frigate-hass-card"; +export const REPO_URL = 'https://github.com/dermotduffy/frigate-hass-card'; export const TROUBLESHOOTING_URL = `${REPO_URL}#troubleshooting`; + +export const CONF_CAMERA_ENTITY = 'camera_entity'; +export const CONF_FRIGATE_CAMERA_NAME = 'frigate.camera_name'; +export const CONF_FRIGATE_CLIENT_ID = 'frigate.client_id'; +export const CONF_FRIGATE_LABEL = 'frigate.label'; +export const CONF_FRIGATE_URL = 'frigate.url'; +export const CONF_FRIGATE_ZONE = 'frigate.zone'; + +export const CONF_VIEW_DEFAULT = 'view.default'; +export const CONF_VIEW_TIMEOUT = 'view.timeout'; + +export const CONF_EVENT_VIEWER_AUTOPLAY_CLIP = 'event_viewer.autoplay_clip'; +export const CONF_EVENT_VIEWER_DRAGGABLE = 'event_viewer.draggable'; +export const CONF_EVENT_VIEWER_LAZY_LOAD = 'event_viewer.lazy_load'; +export const CONF_EVENT_VIEWER_CONTROLS_NEXT_PREVIOUS_STYLE = + 'event_viewer.controls.next_previous.style'; +export const CONF_EVENT_VIEWER_CONTROLS_NEXT_PREVIOUS_SIZE = + 'event_viewer.controls.next_previous.size'; + +export const CONF_LIVE_PRELOAD = 'live.preload'; +export const CONF_LIVE_PROVIDER = 'live.provider'; +export const CONF_LIVE_WEBRTC_ENTITY = 'live.webrtc.entity'; +export const CONF_LIVE_WEBRTC_URL = 'live.webrtc.url'; + +export const CONF_IMAGE_SRC = 'image.src'; + +export const CONF_MENU_BUTTONS_FRIGATE = 'menu.buttons.frigate'; +export const CONF_MENU_BUTTONS_FRIGATE_UI = 'menu.buttons.frigate_ui'; +export const CONF_MENU_BUTTONS_FRIGATE_FULLSCREEN = 'menu.buttons.fullscreen'; +export const CONF_MENU_BUTTONS_FRIGATE_DOWNLOAD = 'menu.buttons.download'; +export const CONF_MENU_BUTTONS_LIVE = 'menu.buttons.live'; +export const CONF_MENU_BUTTONS_CLIPS = 'menu.buttons.clips'; +export const CONF_MENU_BUTTONS_SNAPSHOTS = 'menu.buttons.snapshots'; +export const CONF_MENU_BUTTONS_IMAGE = 'menu.buttons.image'; +export const CONF_MENU_BUTTON_SIZE = 'menu.button_size'; +export const CONF_MENU_MODE = 'menu.mode'; + +export const CONF_DIMENSIONS_ASPECT_RATIO = 'dimensions.aspect_ratio'; +export const CONF_DIMENSIONS_ASPECT_RATIO_MODE = 'dimensions.aspect_ratio_mode'; diff --git a/src/editor.ts b/src/editor.ts index a168b954..d36ac23f 100644 --- a/src/editor.ts +++ b/src/editor.ts @@ -16,6 +16,38 @@ import { trimConfig, upgradeConfig, } from './config-mgmt.js'; +import { + CONF_CAMERA_ENTITY, + CONF_DIMENSIONS_ASPECT_RATIO, + CONF_DIMENSIONS_ASPECT_RATIO_MODE, + CONF_EVENT_VIEWER_AUTOPLAY_CLIP, + CONF_EVENT_VIEWER_CONTROLS_NEXT_PREVIOUS_SIZE, + CONF_EVENT_VIEWER_CONTROLS_NEXT_PREVIOUS_STYLE, + CONF_EVENT_VIEWER_DRAGGABLE, + CONF_EVENT_VIEWER_LAZY_LOAD, + CONF_FRIGATE_CAMERA_NAME, + CONF_FRIGATE_CLIENT_ID, + CONF_FRIGATE_LABEL, + CONF_FRIGATE_URL, + CONF_FRIGATE_ZONE, + CONF_IMAGE_SRC, + CONF_LIVE_PRELOAD, + CONF_LIVE_PROVIDER, + CONF_LIVE_WEBRTC_ENTITY, + CONF_LIVE_WEBRTC_URL, + CONF_MENU_BUTTONS_CLIPS, + CONF_MENU_BUTTONS_FRIGATE, + CONF_MENU_BUTTONS_FRIGATE_DOWNLOAD, + CONF_MENU_BUTTONS_FRIGATE_FULLSCREEN, + CONF_MENU_BUTTONS_FRIGATE_UI, + CONF_MENU_BUTTONS_IMAGE, + CONF_MENU_BUTTONS_LIVE, + CONF_MENU_BUTTONS_SNAPSHOTS, + CONF_MENU_BUTTON_SIZE, + CONF_MENU_MODE, + CONF_VIEW_DEFAULT, + CONF_VIEW_TIMEOUT, +} from './const.js'; interface EditorOptionsSet { icon: string; @@ -138,9 +170,6 @@ export class FrigateCardEditor extends LitElement implements LovelaceCardEditor this._helpers.importMoreInfoControl('climate'); const cameraEntities = this._getEntities('camera'); - const webrtcCameraEntity = String( - getConfigValue(this._config, 'live.webrtc.entity', ''), - ); const viewModes = { '': '', @@ -236,14 +265,14 @@ export class FrigateCardEditor extends LitElement implements LovelaceCardEditor ? html`
${cameraEntities.map((entity) => { @@ -269,33 +298,33 @@ export class FrigateCardEditor extends LitElement implements LovelaceCardEditor ? html`
@@ -312,14 +341,14 @@ export class FrigateCardEditor extends LitElement implements LovelaceCardEditor ? html`
${Object.keys(viewModes).map((key) => { @@ -330,11 +359,11 @@ export class FrigateCardEditor extends LitElement implements LovelaceCardEditor
@@ -351,14 +380,14 @@ export class FrigateCardEditor extends LitElement implements LovelaceCardEditor ? html`
${Object.keys(menuModes).map((key) => { @@ -369,23 +398,23 @@ export class FrigateCardEditor extends LitElement implements LovelaceCardEditor @@ -397,10 +426,10 @@ export class FrigateCardEditor extends LitElement implements LovelaceCardEditor @@ -412,10 +441,10 @@ export class FrigateCardEditor extends LitElement implements LovelaceCardEditor @@ -427,10 +456,10 @@ export class FrigateCardEditor extends LitElement implements LovelaceCardEditor @@ -442,55 +471,55 @@ export class FrigateCardEditor extends LitElement implements LovelaceCardEditor @@ -508,26 +537,26 @@ export class FrigateCardEditor extends LitElement implements LovelaceCardEditor ? html`

- + ${Object.keys(liveProviders).map((key) => { @@ -538,13 +567,15 @@ export class FrigateCardEditor extends LitElement implements LovelaceCardEditor ${cameraEntities.map((entity) => { return html` ${entity} `; @@ -552,9 +583,9 @@ export class FrigateCardEditor extends LitElement implements LovelaceCardEditor
@@ -574,43 +605,47 @@ export class FrigateCardEditor extends LitElement implements LovelaceCardEditor ${options.event_viewer.show ? html`

- + - + - +
` @@ -659,10 +694,10 @@ export class FrigateCardEditor extends LitElement implements LovelaceCardEditor ${options.image.show ? html`
` @@ -681,15 +716,19 @@ export class FrigateCardEditor extends LitElement implements LovelaceCardEditor ${options.dimensions.show ? html`
@@ -701,10 +740,10 @@ export class FrigateCardEditor extends LitElement implements LovelaceCardEditor
` @@ -760,7 +799,6 @@ export class FrigateCardEditor extends LitElement implements LovelaceCardEditor /** * Handle a changed option value. * @param ev Event triggering the change. - * @returns */ protected _valueChangedHandler(ev: { target: (EditorOptionTarget & HTMLElement) | null;