Add editor and migration support.

This commit is contained in:
Dermot Duffy
2024-01-21 11:30:23 -08:00
parent 278f4f372d
commit 64a909ead0
16 changed files with 385 additions and 230 deletions
+19 -17
View File
@@ -75,8 +75,8 @@ export class TriggersManager {
public updateView(oldView?: View | null): void {
if (oldView?.camera !== this._api.getViewManager().getView()?.camera) {
// If the view changes, a new camera may have been selected, which may
// mean a trigger is required (in the case that `trigger_filter_camera`
// has been set to `selected`).
// mean a trigger is required (in the case that `filter_selected_camera`
// is true).
this._evaluateTriggers();
}
}
@@ -91,12 +91,11 @@ export class TriggersManager {
for (const cameraID of this._triggeredState.keys()) {
if (
!this._triggeredCameras.has(cameraID) &&
(scanConfig.trigger_filter_camera === 'all' ||
(scanConfig.trigger_filter_camera === 'selected' &&
cameraID === this._api.getViewManager().getView()?.camera))
(!scanConfig.filter_selected_camera ||
cameraID === this._api.getViewManager().getView()?.camera)
) {
this._triggeredCameras.set(cameraID, now);
this._triggerAction();
this._triggerAction(cameraID);
}
}
@@ -107,23 +106,26 @@ export class TriggersManager {
}
}
protected _hasAllowableInteractionState(): boolean {
protected _hasAllowableInteractionStateForAction(): boolean {
const scanConfig = this._api.getConfigManager().getConfig()?.view.scan;
const hasInteraction = this._api.getInteractionManager().hasInteraction();
return (
!!scanConfig &&
(scanConfig.interaction_mode === 'all' ||
(scanConfig.interaction_mode === 'active' && hasInteraction) ||
(scanConfig.interaction_mode === 'inactive' && !hasInteraction))
(scanConfig.actions.interaction_mode === 'all' ||
(scanConfig.actions.interaction_mode === 'active' && hasInteraction) ||
(scanConfig.actions.interaction_mode === 'inactive' && !hasInteraction))
);
}
protected _triggerAction(): void {
const action = this._api.getConfigManager().getConfig()?.view.scan.trigger_action;
protected _triggerAction(cameraID: string): void {
const action = this._api.getConfigManager().getConfig()?.view.scan.actions.trigger;
if (action && this._hasAllowableInteractionState()) {
this._api.getActionsManager().executeActions(action);
if (action === 'live' && this._hasAllowableInteractionStateForAction()) {
this._api.getViewManager().setViewByParameters({
viewName: 'live',
cameraID: cameraID,
});
}
// Must update master element to add border pulsing.
@@ -131,10 +133,10 @@ export class TriggersManager {
}
protected _untriggerAction(cameraID: string): void {
const action = this._api.getConfigManager().getConfig()?.view.scan.untrigger_action;
const action = this._api.getConfigManager().getConfig()?.view.scan.actions.untrigger;
if (action && this._hasAllowableInteractionState()) {
this._api.getActionsManager().executeActions(action);
if (action === 'default' && this._hasAllowableInteractionStateForAction()) {
this._api.getViewManager().setViewDefault();
}
this._triggeredCameras.delete(cameraID);
this._deleteTimer(cameraID);
-1
View File
@@ -197,7 +197,6 @@ export interface CardStyleAPI {
}
export interface CardTriggersAPI {
getActionsManager(): ActionsManager;
getCameraManager(): CameraManager;
getCardElementManager(): CardElementManager;
getConfigManager(): ConfigManager;
+1 -1
View File
@@ -241,7 +241,7 @@ class FrigateCard extends LitElement {
const cardClasses = {
triggered:
!!this._controller.getTriggersManager().isTriggered() &&
!!this._config?.view.scan.trigger_show_status,
!!this._config?.view.scan.show_trigger_status,
};
const mainClasses = {
main: true,
+9
View File
@@ -13,6 +13,7 @@ import {
CONF_MEDIA_GALLERY,
CONF_MENU_BUTTONS_CAMERA_UI,
CONF_OVERRIDES,
CONF_VIEW_SCAN_ACTIONS_UNTRIGGER,
} from './const';
import { arrayify } from './utils/basic';
@@ -474,4 +475,12 @@ const UPGRADES = [
);
},
upgradePTZElementsToLive(),
upgradeMoveToWithOverrides(
'view.scan.untrigger_reset',
CONF_VIEW_SCAN_ACTIONS_UNTRIGGER,
{
// Delete the value if it's set to the default.
transform: (val) => (val ? 'default' : null),
},
),
];
+22 -72
View File
@@ -1,42 +1,3 @@
// TODO: Config migration
// TODO: Editor support for scan mode changes
// TODO: Consider this:
/*
// NOW ===>
view:
scan:
enabled: true
trigger_show_status: true,
trigger_action:
- action: custom:frigate-card-action
frigate_card_action: camera_select
triggered: true
- action: custom:frigate-card-action
frigate_card_action: live
untrigger_action:
- action: custom:frigate-card-action
interaction_mode: inactive
trigger_filter_camera: all
untrigger_seconds: 0
view:
scan:
enabled: true
// TO
view:
scan:
enabled: true
trigger_show_status: true
actions:
live | live-reset | none
interaction: inactive | active | all
trigger_filter_camera: all
untrigger_seconds: 0
automations:
*/
import {
CallServiceActionConfig,
@@ -1160,47 +1121,36 @@ const viewConfigDefault = {
dark_mode: 'off' as const,
scan: {
enabled: false,
trigger_show_status: true,
trigger_action: [
{
action: 'custom:frigate-card-action' as const,
frigate_card_action: 'camera_select' as const,
triggered: true,
},
{
action: 'custom:frigate-card-action' as const,
frigate_card_action: 'live' as const,
},
],
untrigger_action: {
action: 'custom:frigate-card-action' as const,
frigate_card_action: 'default' as const,
show_trigger_status: true,
filter_selected_camera: false,
actions: {
interaction_mode: 'inactive' as const,
trigger: 'live' as const,
untrigger: 'default' as const,
},
interaction_mode: 'inactive' as const,
trigger_filter_camera: 'all' as const,
untrigger_seconds: 0,
},
};
const scanSchema = z.object({
export const scanSchema = z.object({
enabled: z.boolean().default(viewConfigDefault.scan.enabled),
interaction_mode: z
.enum(['all', 'inactive', 'active'])
.default(viewConfigDefault.scan.interaction_mode),
trigger_filter_camera: z
.enum(['all', 'selected'])
.default(viewConfigDefault.scan.trigger_filter_camera),
trigger_show_status: z.boolean().default(viewConfigDefault.scan.trigger_show_status),
trigger_action: actionSchema
.or(actionSchema.array())
.nullable()
.default(viewConfigDefault.scan.trigger_action),
filter_selected_camera: z
.boolean()
.default(viewConfigDefault.scan.filter_selected_camera),
show_trigger_status: z.boolean().default(viewConfigDefault.scan.show_trigger_status),
untrigger_action: actionSchema
.or(actionSchema.array())
.nullable()
.default(viewConfigDefault.scan.untrigger_action),
actions: z
.object({
interaction_mode: z
.enum(['all', 'inactive', 'active'])
.default(viewConfigDefault.scan.actions.interaction_mode),
trigger: z.enum(['live', 'none']).default(viewConfigDefault.scan.actions.trigger),
untrigger: z
.enum(['default', 'none'])
.default(viewConfigDefault.scan.actions.untrigger),
})
.default(viewConfigDefault.scan.actions),
untrigger_seconds: z.number().default(viewConfigDefault.scan.untrigger_seconds),
});
export type ScanOptions = z.infer<typeof scanSchema>;
+11 -2
View File
@@ -77,10 +77,19 @@ export const CONF_VIEW_UPDATE_FORCE = `${CONF_VIEW}.update_force` as const;
export const CONF_VIEW_UPDATE_SECONDS = `${CONF_VIEW}.update_seconds` as const;
export const CONF_VIEW_SCAN = `${CONF_VIEW}.scan` as const;
export const CONF_VIEW_SCAN_ENABLED = `${CONF_VIEW_SCAN}.enabled` as const;
export const CONF_VIEW_SCAN_TRIGGER_SHOW_STATUS =
`${CONF_VIEW_SCAN}.trigger_show_status` as const;
export const CONF_VIEW_SCAN_SHOW_TRIGGER_STATUS =
`${CONF_VIEW_SCAN}.show_trigger_status` as const;
export const CONF_VIEW_SCAN_FILTER_SELECTED_CAMERA =
`${CONF_VIEW_SCAN}.filter_selected_camera` as const;
export const CONF_VIEW_SCAN_UNTRIGGER_SECONDS =
`${CONF_VIEW_SCAN}.untrigger_seconds` as const;
export const CONF_VIEW_SCAN_ACTIONS = `${CONF_VIEW_SCAN}.actions` as const;
export const CONF_VIEW_SCAN_ACTIONS_TRIGGER =
`${CONF_VIEW_SCAN_ACTIONS}.trigger` as const;
export const CONF_VIEW_SCAN_ACTIONS_UNTRIGGER =
`${CONF_VIEW_SCAN_ACTIONS}.untrigger` as const;
export const CONF_VIEW_SCAN_ACTIONS_INTERACTION_MODE =
`${CONF_VIEW_SCAN_ACTIONS}.interaction_mode` as const;
export const CONF_MEDIA_GALLERY = 'media_gallery' as const;
export const CONF_MEDIA_GALLERY_CONTROLS_FILTER_MODE =
+109 -39
View File
@@ -181,8 +181,13 @@ import {
CONF_VIEW_DARK_MODE,
CONF_VIEW_DEFAULT,
CONF_VIEW_SCAN,
CONF_VIEW_SCAN_ACTIONS,
CONF_VIEW_SCAN_ACTIONS_INTERACTION_MODE,
CONF_VIEW_SCAN_ACTIONS_TRIGGER,
CONF_VIEW_SCAN_ACTIONS_UNTRIGGER,
CONF_VIEW_SCAN_ENABLED,
CONF_VIEW_SCAN_TRIGGER_SHOW_STATUS,
CONF_VIEW_SCAN_FILTER_SELECTED_CAMERA,
CONF_VIEW_SCAN_SHOW_TRIGGER_STATUS,
CONF_VIEW_SCAN_UNTRIGGER_SECONDS,
CONF_VIEW_TIMEOUT_SECONDS,
CONF_VIEW_UPDATE_CYCLE_CAMERA,
@@ -237,6 +242,7 @@ const MENU_PERFORMANCE_FEATURES = 'performance.features';
const MENU_PERFORMANCE_STYLE = 'performance.style';
const MENU_TIMELINE_CONTROLS_THUMBNAILS = 'timeline.controls.thumbnails';
const MENU_VIEW_SCAN = 'scan';
const MENU_VIEW_SCAN_ACTIONS = 'scan.actions';
interface EditorOptionsSet {
icon: string;
@@ -620,6 +626,46 @@ export class FrigateCardEditor extends LitElement implements LovelaceCardEditor
},
];
protected _scanActionsInteractionModes: EditorSelectOption[] = [
{ value: '', label: '' },
{
value: 'all',
label: localize('config.view.scan.actions.interaction_modes.all'),
},
{
value: 'inactive',
label: localize('config.view.scan.actions.interaction_modes.inactive'),
},
{
value: 'active',
label: localize('config.view.scan.actions.interaction_modes.active'),
},
];
protected _scanActionsTrigger: EditorSelectOption[] = [
{ value: '', label: '' },
{
value: 'live',
label: localize('config.view.scan.actions.triggers.live'),
},
{
value: 'none',
label: localize('config.view.scan.actions.triggers.none'),
},
];
protected _scanActionsUntrigger: EditorSelectOption[] = [
{ value: '', label: '' },
{
value: 'default',
label: localize('config.view.scan.actions.untriggers.default'),
},
{
value: 'none',
label: localize('config.view.scan.actions.untriggers.none'),
},
];
public setConfig(config: RawFrigateCardConfig): void {
// Note: This does not use Zod to parse the configuration, so it may be
// partially or completely invalid. It's more useful to have a partially
@@ -876,44 +922,68 @@ export class FrigateCardEditor extends LitElement implements LovelaceCardEditor
}
protected _renderViewScanMenu(): TemplateResult {
const submenuClasses = {
submenu: true,
selected: !!this._expandedMenus[MENU_VIEW_SCAN],
};
return html`
<div class="${classMap(submenuClasses)}">
<div
class="submenu-header"
@click=${this._toggleMenu}
.domain=${MENU_VIEW_SCAN}
.key=${true}
>
<ha-icon .icon=${'mdi:target-account'}></ha-icon>
<span>${localize(`config.${CONF_VIEW_SCAN}.scan_mode`)}</span>
</div>
${this._expandedMenus[MENU_VIEW_SCAN]
? html` <div class="values">
${this._renderSwitch(
CONF_VIEW_SCAN_ENABLED,
this._defaults.view.scan.enabled,
{
label: localize(`config.${CONF_VIEW_SCAN_ENABLED}`),
},
)}
${this._renderSwitch(
CONF_VIEW_SCAN_TRIGGER_SHOW_STATUS,
this._defaults.view.scan.trigger_show_status,
{
label: localize(`config.${CONF_VIEW_SCAN_TRIGGER_SHOW_STATUS}`),
},
)}
${this._renderNumberInput(CONF_VIEW_SCAN_UNTRIGGER_SECONDS, {
default: this._defaults.view.scan.untrigger_seconds,
})}
</div>`
: ''}
</div>
`;
return this._putInSubmenu(
MENU_VIEW_SCAN,
true,
`config.${CONF_VIEW_SCAN}.editor_label`,
{ name: 'mdi:target-account' },
html`
${this._renderSwitch(CONF_VIEW_SCAN_ENABLED, this._defaults.view.scan.enabled, {
label: localize(`config.${CONF_VIEW_SCAN_ENABLED}`),
})}
${this._renderSwitch(
CONF_VIEW_SCAN_FILTER_SELECTED_CAMERA,
this._defaults.view.scan.filter_selected_camera,
{
label: localize(`config.${CONF_VIEW_SCAN_FILTER_SELECTED_CAMERA}`),
},
)}
${this._renderSwitch(
CONF_VIEW_SCAN_SHOW_TRIGGER_STATUS,
this._defaults.view.scan.show_trigger_status,
{
label: localize(`config.${CONF_VIEW_SCAN_SHOW_TRIGGER_STATUS}`),
},
)}
${this._renderNumberInput(CONF_VIEW_SCAN_UNTRIGGER_SECONDS, {
default: this._defaults.view.scan.untrigger_seconds,
})}
${this._renderOptionSelector(
CONF_VIEW_SCAN_ACTIONS_TRIGGER,
this._scanActionsTrigger,
{
label: localize('config.view.scan.actions.trigger'),
},
)}
${this._putInSubmenu(
MENU_VIEW_SCAN_ACTIONS,
true,
`config.${CONF_VIEW_SCAN_ACTIONS}.editor_label`,
{ name: 'mdi:cogs' },
html` ${this._renderOptionSelector(
CONF_VIEW_SCAN_ACTIONS_TRIGGER,
this._scanActionsTrigger,
{
label: localize('config.view.scan.actions.trigger'),
},
)}
${this._renderOptionSelector(
CONF_VIEW_SCAN_ACTIONS_UNTRIGGER,
this._scanActionsUntrigger,
{
label: localize('config.view.scan.actions.untrigger'),
},
)}
${this._renderOptionSelector(
CONF_VIEW_SCAN_ACTIONS_INTERACTION_MODE,
this._scanActionsInteractionModes,
{
label: localize('config.view.scan.actions.interaction_mode'),
},
)}`,
)}
`,
);
}
/**
+22 -3
View File
@@ -385,10 +385,29 @@
},
"default": "Default view",
"scan": {
"actions": {
"editor_label": "Scan mode actions",
"interaction_mode": "How to handle actions when the card has human interaction",
"interaction_modes": {
"active": "Only trigger actions when card has human interaction",
"all": "Trigger actions regardless of human interaction",
"inactive": "Only trigger actions when card has no human interaction"
},
"trigger": "Trigger action",
"triggers": {
"live": "Change to live",
"none": "Take no action"
},
"untrigger": "Untrigger action",
"untriggers": {
"default": "Change to default view/camera",
"none": "Take no action"
}
},
"editor_label": "Scan mode",
"filter_selected_camera": "Only trigger on selected camera",
"enabled": "Scan mode enabled",
"scan_mode": "Scan mode",
"trigger_show_status": "Show pulsing border when triggered",
"untrigger_reset": "Reset the view to default after untrigger",
"show_trigger_status": "Show pulsing border when triggered",
"untrigger_seconds": "Seconds after inactive state change to untrigger"
},
"timeout_seconds": "Reset to default view X seconds after user action (0=never)",
+22 -3
View File
@@ -381,10 +381,29 @@
},
"default": "Visualizzazione predefinita",
"scan": {
"actions": {
"editor_label": "",
"interaction_mode": "",
"interaction_modes": {
"active": "",
"all": "",
"inactive": ""
},
"trigger": "",
"triggers": {
"live": "",
"none": ""
},
"untrigger": "",
"untriggers": {
"default": "",
"none": ""
}
},
"editor_label": "Modalità di scansione",
"enabled": "Modalità di scansione abilitata",
"scan_mode": "Modalità di scansione",
"trigger_show_status": "Mostra bordo pulsante quando attivato",
"untrigger_reset": "Reset the view to default after untrigger",
"filter_selected_camera": "",
"show_trigger_status": "Mostra bordo pulsante quando attivato",
"untrigger_seconds": "Reimposta la vista ai valori predefiniti dopo aver annullato l'attivazione"
},
"timeout_seconds": "Ripristina la vista predefinita x secondi dopo l'azione dell'utente (0 = mai)",
+22 -3
View File
@@ -384,10 +384,29 @@
},
"default": "Visualização padrão",
"scan": {
"actions": {
"editor_label": "",
"interaction_mode": "",
"interaction_modes": {
"active": "",
"all": "",
"inactive": ""
},
"trigger": "",
"triggers": {
"live": "",
"none": ""
},
"untrigger": "",
"untriggers": {
"default": "",
"none": ""
}
},
"editor_label": "Modo scan",
"enabled": "Modo scan ativado",
"scan_mode": "Modo scan",
"trigger_show_status": "Pulsar borda quando acionado",
"untrigger_reset": "Redefinir a visualização para o padrão após desacionar",
"filter_selected_camera": "",
"show_trigger_status": "Pulsar borda quando acionado",
"untrigger_seconds": "Segundos após a mudar para o estado inativo para desacionar"
},
"timeout_seconds": "Redefinir para a visualização padrão X segundos após a ação do usuário (0 = nunca)",
+22 -3
View File
@@ -374,10 +374,29 @@
},
"default": "Visualização padrão",
"scan": {
"actions": {
"editor_label": "",
"interaction_mode": "",
"interaction_modes": {
"active": "",
"all": "",
"inactive": ""
},
"trigger": "",
"triggers": {
"live": "",
"none": ""
},
"untrigger": "",
"untriggers": {
"default": "",
"none": ""
}
},
"editor_label": "Modo scan",
"enabled": "Modo scan ativado",
"scan_mode": "Modo scan",
"trigger_show_status": "Exibir estado do gatilho",
"untrigger_reset": "Redefinir a visualização para o padrão após desacionar",
"filter_selected_camera": "",
"show_trigger_status": "Exibir estado do gatilho",
"untrigger_seconds": "Segundos após a mudar para o estado inativo para desacionar"
},
"timeout_seconds": "Redefinir para a visualização padrão X segundos após a ação do usuário (0 = nunca)",