From a88a7c5236c50cca485b27e7335d9605249e666d Mon Sep 17 00:00:00 2001 From: Bram Kragten Date: Wed, 31 Jul 2024 14:34:43 +0200 Subject: [PATCH] Change yaml config of UI service call actions (#21508) --- gallery/src/pages/lovelace/entities-card.ts | 4 +-- src/data/lovelace/config/action.ts | 8 +++-- .../lovelace/cards/hui-entities-card.ts | 4 ++- src/panels/lovelace/cards/types.ts | 2 ++ src/panels/lovelace/common/handle-action.ts | 14 ++++++--- .../lovelace/components/hui-action-editor.ts | 30 ++++++++++++------- .../hui-entities-card-editor.ts | 6 ++-- .../hui-row-element-editor.ts | 4 +++ .../lovelace/editor/structs/action-struct.ts | 9 ++++-- src/panels/lovelace/entity-rows/types.ts | 8 +++-- .../special-rows/hui-call-service-row.ts | 11 +++---- src/translations/en.json | 3 +- 12 files changed, 71 insertions(+), 32 deletions(-) diff --git a/gallery/src/pages/lovelace/entities-card.ts b/gallery/src/pages/lovelace/entities-card.ts index a7131557e6..45842de2a6 100644 --- a/gallery/src/pages/lovelace/entities-card.ts +++ b/gallery/src/pages/lovelace/entities-card.ts @@ -287,11 +287,11 @@ const CONFIGS = [ config: ` - type: entities entities: - - type: call-service + - type: perform-action icon: mdi:power name: Bed light action_name: Toggle light - service: light.toggle + action: light.toggle data: entity_id: light.bed_light - type: section diff --git a/src/data/lovelace/config/action.ts b/src/data/lovelace/config/action.ts index 94f781a209..9ed065bc8d 100644 --- a/src/data/lovelace/config/action.ts +++ b/src/data/lovelace/config/action.ts @@ -5,10 +5,12 @@ export interface ToggleActionConfig extends BaseActionConfig { } export interface CallServiceActionConfig extends BaseActionConfig { - action: "call-service"; - service: string; + action: "call-service" | "perform-action"; + /** @deprecated "service" is kept for backwards compatibility. Replaced by "perform_action". */ + service?: string; + perform_action: string; target?: HassServiceTarget; - // "service_data" is kept for backwards compatibility. Replaced by "data". + /** @deprecated "service_data" is kept for backwards compatibility. Replaced by "data". */ service_data?: Record; data?: Record; } diff --git a/src/panels/lovelace/cards/hui-entities-card.ts b/src/panels/lovelace/cards/hui-entities-card.ts index 4fb12c4661..152c8b1ca4 100644 --- a/src/panels/lovelace/cards/hui-entities-card.ts +++ b/src/panels/lovelace/cards/hui-entities-card.ts @@ -302,7 +302,9 @@ class HuiEntitiesCard extends LitElement implements LovelaceCard { state_color: this._config.state_color, ...(entityConf as EntityConfig), } as EntityConfig) - : entityConf + : entityConf.type === "perform-action" + ? { ...entityConf, type: "call-service" } + : entityConf ); if (this._hass) { element.hass = this._hass; diff --git a/src/panels/lovelace/cards/types.ts b/src/panels/lovelace/cards/types.ts index 38a94f1ce5..8bf731f56d 100644 --- a/src/panels/lovelace/cards/types.ts +++ b/src/panels/lovelace/cards/types.ts @@ -66,6 +66,8 @@ export interface EntitiesCardEntityConfig extends EntityConfig { | "tilt-position" | "brightness"; action_name?: string; + action?: string; + /** @deprecated use "action" instead */ service?: string; // "service_data" is kept for backwards compatibility. Replaced by "data". service_data?: Record; diff --git a/src/panels/lovelace/common/handle-action.ts b/src/panels/lovelace/common/handle-action.ts index f4fdd90653..609e0c162b 100644 --- a/src/panels/lovelace/common/handle-action.ts +++ b/src/panels/lovelace/common/handle-action.ts @@ -56,8 +56,12 @@ export const handleAction = async ( forwardHaptic("warning"); let serviceName; - if (actionConfig.action === "call-service") { - const [domain, service] = actionConfig.service.split(".", 2); + if ( + actionConfig.action === "call-service" || + actionConfig.action === "perform-action" + ) { + const [domain, service] = (actionConfig.perform_action || + actionConfig.service)!.split(".", 2); const serviceDomains = hass.services; if (domain in serviceDomains && service in serviceDomains[domain]) { await hass.loadBackendTranslation("title"); @@ -145,15 +149,17 @@ export const handleAction = async ( } break; } + case "perform-action": case "call-service": { - if (!actionConfig.service) { + if (!actionConfig.perform_action && !actionConfig.service) { showToast(node, { message: hass.localize("ui.panel.lovelace.cards.actions.no_action"), }); forwardHaptic("failure"); return; } - const [domain, service] = actionConfig.service.split(".", 2); + const [domain, service] = (actionConfig.perform_action || + actionConfig.service)!.split(".", 2); hass.callService( domain, service, diff --git a/src/panels/lovelace/components/hui-action-editor.ts b/src/panels/lovelace/components/hui-action-editor.ts index 92fa589987..86ef25eb05 100644 --- a/src/panels/lovelace/components/hui-action-editor.ts +++ b/src/panels/lovelace/components/hui-action-editor.ts @@ -33,7 +33,7 @@ const DEFAULT_ACTIONS: UiAction[] = [ "toggle", "navigate", "url", - "call-service", + "perform-action", "assist", "none", ]; @@ -98,7 +98,7 @@ export class HuiActionEditor extends LitElement { get _service(): string { const config = this.config as CallServiceActionConfig; - return config?.service || ""; + return config?.perform_action || config?.service || ""; } private _serviceAction = memoizeOne( @@ -127,13 +127,19 @@ export class HuiActionEditor extends LitElement { const actions = this.actions ?? DEFAULT_ACTIONS; + let action = this.config?.action || "default"; + + if (action === "call-service") { + action = "perform-action"; + } + return html`