From 141c3f1ea463205bcc69678cf81eb3c5975cc1d3 Mon Sep 17 00:00:00 2001 From: Ian Richardson Date: Wed, 16 Oct 2019 11:03:36 -0500 Subject: [PATCH] add confirmation option to actions (#4006) * add confirmation option to actions * address comments --- src/data/lovelace.ts | 25 ++++++++++++++++------ src/panels/lovelace/common/handle-click.ts | 17 +++++++++++++++ 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/src/data/lovelace.ts b/src/data/lovelace.ts index c3c8222f3c..d5ba052dea 100644 --- a/src/data/lovelace.ts +++ b/src/data/lovelace.ts @@ -37,11 +37,11 @@ export interface LovelaceCardConfig { [key: string]: any; } -export interface ToggleActionConfig { +export interface ToggleActionConfig extends BaseActionConfig { action: "toggle"; } -export interface CallServiceActionConfig { +export interface CallServiceActionConfig extends BaseActionConfig { action: "call-service"; service: string; service_data?: { @@ -50,24 +50,37 @@ export interface CallServiceActionConfig { }; } -export interface NavigateActionConfig { +export interface NavigateActionConfig extends BaseActionConfig { action: "navigate"; navigation_path: string; } -export interface UrlActionConfig { +export interface UrlActionConfig extends BaseActionConfig { action: "url"; url_path: string; } -export interface MoreInfoActionConfig { +export interface MoreInfoActionConfig extends BaseActionConfig { action: "more-info"; } -export interface NoActionConfig { +export interface NoActionConfig extends BaseActionConfig { action: "none"; } +export interface BaseActionConfig { + confirmation?: ConfirmationRestrictionConfig; +} + +export interface ConfirmationRestrictionConfig { + text?: string; + exemptions?: RestrictionConfig[]; +} + +export interface RestrictionConfig { + user: string; +} + export type ActionConfig = | ToggleActionConfig | CallServiceActionConfig diff --git a/src/panels/lovelace/common/handle-click.ts b/src/panels/lovelace/common/handle-click.ts index 30052d557a..27b6d28c14 100644 --- a/src/panels/lovelace/common/handle-click.ts +++ b/src/panels/lovelace/common/handle-click.ts @@ -34,6 +34,23 @@ export const handleClick = ( }; } + if ( + actionConfig.confirmation && + (!actionConfig.confirmation.exemptions || + !actionConfig.confirmation.exemptions.some( + (e) => e.user === hass!.user!.id + )) + ) { + if ( + !confirm( + actionConfig.confirmation.text || + `Are you sure you want to ${actionConfig.action}?` + ) + ) { + return; + } + } + switch (actionConfig.action) { case "more-info": if (config.entity || config.camera_image) {