diff --git a/src/data/script.ts b/src/data/script.ts index 6b83157bde..81a90d0ed2 100644 --- a/src/data/script.ts +++ b/src/data/script.ts @@ -70,10 +70,15 @@ export interface DelayAction { delay: number | Partial | string; } -export interface SceneAction { +export interface ServiceSceneAction extends ServiceAction { + service: "scene.turn_on"; + metadata: Record; +} +export interface LegacySceneAction { alias?: string; scene: string; } +export type SceneAction = ServiceSceneAction | LegacySceneAction; export interface WaitAction { alias?: string; @@ -153,7 +158,8 @@ export interface ActionTypes { check_condition: Condition; fire_event: EventAction; device_action: DeviceAction; - activate_scene: SceneAction; + legacy_activate_scene: LegacySceneAction; + activate_scene: ServiceSceneAction; repeat: RepeatAction; choose: ChooseAction; wait_for_trigger: WaitForTriggerAction; @@ -218,7 +224,7 @@ export const getActionType = (action: Action): ActionType => { return "device_action"; } if ("scene" in action) { - return "activate_scene"; + return "legacy_activate_scene"; } if ("repeat" in action) { return "repeat"; @@ -233,6 +239,14 @@ export const getActionType = (action: Action): ActionType => { return "variables"; } if ("service" in action) { + if ("metadata" in action) { + if ( + (action as ServiceAction).service === "scene.turn_on" && + !Array.isArray((action as ServiceAction)?.target?.entity_id) + ) { + return "activate_scene"; + } + } return "service"; } return "unknown"; diff --git a/src/data/script_i18n.ts b/src/data/script_i18n.ts index 338cb7c234..a8f2b76356 100644 --- a/src/data/script_i18n.ts +++ b/src/data/script_i18n.ts @@ -11,7 +11,8 @@ import { DelayAction, EventAction, getActionType, - SceneAction, + LegacySceneAction, + ServiceSceneAction, VariablesAction, WaitForTriggerAction, } from "./script"; @@ -102,14 +103,22 @@ export const describeAction = ( return `Delay ${duration}`; } - if (actionType === "activate_scene") { - const config = action as SceneAction; + if (actionType === "legacy_activate_scene") { + const config = action as LegacySceneAction; const sceneStateObj = hass.states[config.scene]; return `Activate scene ${ sceneStateObj ? computeStateName(sceneStateObj) : config.scene }`; } + if (actionType === "activate_scene") { + const config = action as ServiceSceneAction; + const sceneStateObj = hass.states[config.target!.entity_id as string]; + return `Activate scene ${ + sceneStateObj ? computeStateName(sceneStateObj) : config.target!.entity_id + }`; + } + if (actionType === "wait_for_trigger") { const config = action as WaitForTriggerAction; return `Wait for ${ensureArray(config.wait_for_trigger) diff --git a/src/panels/config/automation/action/ha-automation-action-row.ts b/src/panels/config/automation/action/ha-automation-action-row.ts index c4e90994bf..5c103e9e67 100644 --- a/src/panels/config/automation/action/ha-automation-action-row.ts +++ b/src/panels/config/automation/action/ha-automation-action-row.ts @@ -16,7 +16,7 @@ import "../../../../components/ha-card"; import "../../../../components/ha-alert"; import "../../../../components/ha-icon-button"; import type { HaYamlEditor } from "../../../../components/ha-yaml-editor"; -import type { Action } from "../../../../data/script"; +import type { Action, ServiceSceneAction } from "../../../../data/script"; import { showConfirmationDialog } from "../../../../dialogs/generic/show-dialog-box"; import { haStyle } from "../../../../resources/styles"; import type { HomeAssistant } from "../../../../types"; @@ -44,8 +44,28 @@ const OPTIONS = [ "device_id", ]; -const getType = (action: Action | undefined) => - action ? OPTIONS.find((option) => option in action) : undefined; +const getType = (action: Action | undefined) => { + if (!action) { + return undefined; + } + if ("metadata" in action && action.service) { + switch (action.service) { + case "scene.turn_on": + // we dont support arrays of entities + if ( + !Array.isArray( + (action as unknown as ServiceSceneAction).target?.entity_id + ) + ) { + return "scene"; + } + break; + default: + break; + } + } + return OPTIONS.find((option) => option in action); +}; declare global { // for fire event diff --git a/src/panels/config/automation/action/types/ha-automation-action-scene.ts b/src/panels/config/automation/action/types/ha-automation-action-scene.ts index 6af3f06c40..a9d02b83ba 100644 --- a/src/panels/config/automation/action/types/ha-automation-action-scene.ts +++ b/src/panels/config/automation/action/types/ha-automation-action-scene.ts @@ -16,11 +16,23 @@ export class HaSceneAction extends LitElement implements ActionElement { @property() public action!: SceneAction; public static get defaultConfig(): SceneAction { - return { scene: "" }; + return { + service: "scene.turn_on", + target: { + entity_id: "", + }, + metadata: {}, + }; } protected render() { - const { scene } = this.action; + let scene; + + if ("scene" in this.action) { + scene = this.action.scene; + } else { + scene = this.action.target?.entity_id; + } return html` ) { ev.stopPropagation(); fireEvent(this, "value-changed", { - value: { ...this.action, scene: ev.detail.value }, + value: { + service: "scene.turn_on", + target: { + entity_id: ev.detail.value, + }, + metadata: {}, + }, }); } }