Small cleanups and README.

This commit is contained in:
Dermot Duffy
2021-11-14 07:42:47 -08:00
parent fc3033a0fe
commit 256fee9061
4 changed files with 85 additions and 16 deletions
+14 -6
View File
@@ -126,7 +126,7 @@ All variables listed are under a `live:` section.
### Event Viewer options ### Event Viewer options
The `event_viewer` is used for viewing all `clip` and `snapshot` media, in a media carousel. All variables listed are under a `event_viewer:` section. The `event_viewer` is used for viewing all `clip` and `snapshot` media, in a media carousel. All variables listed are under an `event_viewer:` section.
| Option | Default | Description | | Option | Default | Description |
| - | - | - | | - | - | - |
@@ -503,10 +503,10 @@ This card supports several different views:
| Key | Description | | Key | Description |
| ------------- | --------------------------------------------- | | ------------- | --------------------------------------------- |
|`live` (default)| Shows the live camera view, either the name Frigate view or [WebRTC](#webrtc) if configured.| |`live` (default)| Shows the live camera view, either the name Frigate view or [WebRTC](#webrtc) if configured.|
|`snapshots`|Shows the snapshot gallery for this camera/zone/label.| |`snapshots`|Shows an event gallery of snapshots for this camera/zone/label.|
|`snapshot`|Shows the most recent snapshot for this camera/zone/label. Can also be accessed by holding down the `snapshots` menu icon.| |`snapshot`|Shows an event viewer for the most recent snapshot for this camera/zone/label. Can also be accessed by holding down the `snapshots` menu icon.|
|`clips`|Shows the clip gallery for this camera/zone/label.| |`clips`|Shows an event gallery of clips for this camera/zone/label.|
|`clip`|Shows the most recent clip for this camera/zone/label. Can also be accessed by holding down the `clips` menu icon.| |`clip`|Shows an event viewer for the most recent clip for this camera/zone/label. Can also be accessed by holding down the `clips` menu icon.|
|`image`|Shows a static image specified by the `image` parameter, can be used as a discrete default view or a screensaver (via `view_timeout`).| |`image`|Shows a static image specified by the `image` parameter, can be used as a discrete default view or a screensaver (via `view_timeout`).|
### Automatic updates in the `clip` or `snapshot` view ### Automatic updates in the `clip` or `snapshot` view
@@ -552,7 +552,15 @@ specific overriding the less specific (see example below).
The format for actions is the standard Home Assistant [action The format for actions is the standard Home Assistant [action
format](https://www.home-assistant.io/lovelace/actions/#tap-action) as well as format](https://www.home-assistant.io/lovelace/actions/#tap-action) as well as
the custom [Frigate card action](#frigate-card-action) to trigger card changes. the custom [Frigate card action](#frigate-card-action) to trigger Frigate card
changes.
**Note:** The card itself obviously relies on human interactions to function
(e.g. `tap` on the menu should activate that button, `tap` on a gallery thumbnail
should open that piece of media, etc). These internal actions are executed
_also_, which means that a card-wide `tap` action probably isn't that useful as
it may be disorienting to the user and will trigger on all kinds of basic
interaction on the card (e.g. tapping/clicking a menu button).
### Example ### Example
+20 -3
View File
@@ -212,6 +212,11 @@ export class FrigateCard extends LitElement {
} as FrigateCardConfig; } as FrigateCardConfig;
} }
/**
* Get a FrigateCard MenuButton given a set of parameters.
* @param params Menu button parameters.
* @returns A MenuButton.
*/
protected _getFrigateCardMenuButton( protected _getFrigateCardMenuButton(
params: GetFrigateCardMenuButtonParameters, params: GetFrigateCardMenuButtonParameters,
): MenuButton { ): MenuButton {
@@ -598,10 +603,14 @@ export class FrigateCard extends LitElement {
/** /**
* Handle a request for a card action. * Handle a request for a card action.
* @param action The action requested (e.g. clips, fullscreen) * @param ev The action requested.
*/ */
protected _cardActionHandler(event: CustomEvent<ActionType>): void { protected _cardActionHandler(ev: CustomEvent<ActionType>): void {
const frigateCardAction = convertActionToFrigateCardCustomAction(event.detail); // These interactions should only be handled by the card, as nothing
// upstream has the user-provided configuration.
ev.stopPropagation();
const frigateCardAction = convertActionToFrigateCardCustomAction(ev.detail);
if (!frigateCardAction) { if (!frigateCardAction) {
return; return;
} }
@@ -670,6 +679,10 @@ export class FrigateCard extends LitElement {
}, this.config.view.timeout * 1000); }, this.config.view.timeout * 1000);
} }
/**
* Handle an action called on an element.
* @param ev The actionHandler event.
*/
protected _actionHandler( protected _actionHandler(
ev: CustomEvent, ev: CustomEvent,
config?: { config?: {
@@ -866,6 +879,10 @@ export class FrigateCard extends LitElement {
} }
} }
/**
* Merge card-wide and view-specific actions.
* @returns A combined set of action.
*/
protected _getMergedActions(): Actions { protected _getMergedActions(): Actions {
let specificActions: Actions | undefined = undefined; let specificActions: Actions | undefined = undefined;
+16
View File
@@ -258,6 +258,11 @@ export function isValidMediaShowInfo(info: MediaShowInfo): boolean {
); );
} }
/**
* Convert a generic Action to a FrigateCardCustomAction if it parses correctly.
* @param action The generic action configuration.
* @returns A FrigateCardCustomAction or null if it cannot be converted.
*/
export function convertActionToFrigateCardCustomAction( export function convertActionToFrigateCardCustomAction(
action: ActionType, action: ActionType,
): FrigateCardCustomAction | null { ): FrigateCardCustomAction | null {
@@ -267,6 +272,11 @@ export function convertActionToFrigateCardCustomAction(
return parseResult.success ? parseResult.data : null; return parseResult.success ? parseResult.data : null;
} }
/**
* Create a Frigate card custom action.
* @param action The Frigate card action string (e.g. 'fullscreen')
* @returns A FrigateCardCustomAction for that action string.
*/
export function createFrigateCardCustomAction(action: string): FrigateCardCustomAction { export function createFrigateCardCustomAction(action: string): FrigateCardCustomAction {
return { return {
action: 'fire-dom-event', action: 'fire-dom-event',
@@ -274,6 +284,12 @@ export function createFrigateCardCustomAction(action: string): FrigateCardCustom
}; };
} }
/**
* Get an action configuration given a config and an interaction (e.g. 'tap').
* @param interaction The interaction: `tap`, `hold` or `double_tap`
* @param config The configuration containing multiple actions.
* @returns The relevant action configuration or null if none found.
*/
export function getActionConfigGivenAction( export function getActionConfigGivenAction(
interaction?: string, interaction?: string,
config?: { config?: {
+34 -6
View File
@@ -30,7 +30,9 @@ import menuStyle from '../scss/menu.scss';
export const MENU_HEIGHT = 46; export const MENU_HEIGHT = 46;
export const FRIGATE_BUTTON_MENU_ICON = 'frigate'; export const FRIGATE_BUTTON_MENU_ICON = 'frigate';
// A menu for the Frigate card. /**
* A menu for the FrigateCard.
*/
@customElement('frigate-card-menu') @customElement('frigate-card-menu')
export class FrigateCardMenu extends LitElement { export class FrigateCardMenu extends LitElement {
@property({ attribute: false }) @property({ attribute: false })
@@ -51,14 +53,23 @@ export class FrigateCardMenu extends LitElement {
@property({ attribute: false }) @property({ attribute: false })
public buttons: MenuButton[] = []; public buttons: MenuButton[] = [];
/**
* Handle an action on a menu button.
* @param ev The action event.
* @param button The button configuration.
*/
protected _actionHandler(ev: CustomEvent, button: MenuButton): void { protected _actionHandler(ev: CustomEvent, button: MenuButton): void {
if (!ev) { if (!ev) {
return; return;
} }
// These interactions should only be handled by the card, as nothing
// upstream has the user-provided configuration.
ev.stopPropagation();
const interaction: string = ev.detail.action; const interaction: string = ev.detail.action;
const action = getActionConfigGivenAction(interaction, button); const action = getActionConfigGivenAction(interaction, button);
if (!action) { if (!action || !interaction) {
return; return;
} }
@@ -81,7 +92,11 @@ export class FrigateCardMenu extends LitElement {
handleAction(this, this.hass as HomeAssistant, button, interaction); handleAction(this, this.hass as HomeAssistant, button, interaction);
} }
// Determine whether the menu should be updated. /**
* Determine whether the menu should be updated.
* @param changedProps The changed properties.
* @returns `true` if the menu should be updated, otherwise `false`.
*/
protected shouldUpdate(changedProps: PropertyValues): boolean { protected shouldUpdate(changedProps: PropertyValues): boolean {
const oldHass = changedProps.get('hass') as HomeAssistant | undefined; const oldHass = changedProps.get('hass') as HomeAssistant | undefined;
@@ -100,13 +115,21 @@ export class FrigateCardMenu extends LitElement {
return shouldUpdateBasedOnHass(this.hass, oldHass, entities); return shouldUpdateBasedOnHass(this.hass, oldHass, entities);
} }
/**
* Get the style of emphasized menu items.
* @returns A StyleInfo.
*/
public static getEmphasizedStyle(): StyleInfo { public static getEmphasizedStyle(): StyleInfo {
return { return {
color: 'var(--primary-color, white)', color: 'var(--primary-color, white)',
}; };
} }
// Render a menu button. /**
* Render a button.
* @param button The button configuration to render.
* @returns A rendered template or void.
*/
protected _renderButton(button: MenuButton): TemplateResult | void { protected _renderButton(button: MenuButton): TemplateResult | void {
let state: HassEntity | null = null; let state: HassEntity | null = null;
let title = button.title; let title = button.title;
@@ -163,7 +186,10 @@ export class FrigateCardMenu extends LitElement {
</ha-icon-button>`; </ha-icon-button>`;
} }
// Render the menu. /**
* Render the menu.
* @returns A rendered template or void.
*/
protected render(): TemplateResult | void { protected render(): TemplateResult | void {
if (!this._menuConfig) { if (!this._menuConfig) {
return; return;
@@ -200,7 +226,9 @@ export class FrigateCardMenu extends LitElement {
`; `;
} }
// Return compiled CSS styles (thus safe to use with unsafeCSS). /**
* Get styles.
*/
static get styles(): CSSResultGroup { static get styles(): CSSResultGroup {
return unsafeCSS(menuStyle); return unsafeCSS(menuStyle);
} }