From cc4cfe1b5c63e1b5b83951e1431a6c6b940b6892 Mon Sep 17 00:00:00 2001 From: Bram Kragten Date: Tue, 23 Jan 2024 13:16:49 +0100 Subject: [PATCH] Add `set_conversation_response` action (#19512) --- src/components/trace/hat-script-graph.ts | 112 +----------------- src/data/action.ts | 3 + src/data/script.ts | 8 ++ src/data/script_i18n.ts | 9 ++ .../action/ha-automation-action-row.ts | 1 + ...mation-action-set_conversation_response.ts | 54 +++++++++ src/translations/en.json | 7 ++ 7 files changed, 85 insertions(+), 109 deletions(-) create mode 100644 src/panels/config/automation/action/types/ha-automation-action-set_conversation_response.ts diff --git a/src/components/trace/hat-script-graph.ts b/src/components/trace/hat-script-graph.ts index 40afe60a62..6dcfa4b5ee 100644 --- a/src/components/trace/hat-script-graph.ts +++ b/src/components/trace/hat-script-graph.ts @@ -13,14 +13,9 @@ import { mdiClose, mdiCodeBraces, mdiCodeBrackets, - mdiDevices, - mdiGestureDoubleTap, - mdiHandBackRight, - mdiPalette, mdiRefresh, mdiRoomService, mdiShuffleDisabled, - mdiTimerOutline, } from "@mdi/js"; import { css, html, LitElement, PropertyValues } from "lit"; import { customElement, property } from "lit/decorators"; @@ -30,17 +25,14 @@ import { Condition, Trigger } from "../../data/automation"; import { Action, ChooseAction, - DelayAction, - DeviceAction, - EventAction, IfAction, ManualScriptConfig, ParallelAction, RepeatAction, - SceneAction, ServiceAction, WaitAction, WaitForTriggerAction, + getActionType, } from "../../data/script"; import { ChooseActionTraceStep, @@ -53,6 +45,7 @@ import "./hat-graph-branch"; import { BRANCH_HEIGHT, NODE_SIZE, SPACING } from "./hat-graph-const"; import "./hat-graph-node"; import "./hat-graph-spacer"; +import { ACTION_ICONS } from "../../data/action"; export interface NodeInfo { path: string; @@ -115,17 +108,12 @@ export class HatScriptGraph extends LitElement { and: this.render_condition_node, or: this.render_condition_node, not: this.render_condition_node, - delay: this.render_delay_node, - event: this.render_event_node, - scene: this.render_scene_node, service: this.render_service_node, wait_template: this.render_wait_node, wait_for_trigger: this.render_wait_node, repeat: this.render_repeat_node, choose: this.render_choose_node, - device_id: this.render_device_node, if: this.render_if_node, - stop: this.render_stop_node, parallel: this.render_parallel_node, other: this.render_other_node, }; @@ -371,63 +359,6 @@ export class HatScriptGraph extends LitElement { `; } - private render_delay_node( - node: DelayAction, - path: string, - graphStart = false, - disabled = false - ) { - return html` - - `; - } - - private render_device_node( - node: DeviceAction, - path: string, - graphStart = false, - disabled = false - ) { - return html` - - `; - } - - private render_event_node( - node: EventAction, - path: string, - graphStart = false, - disabled = false - ) { - return html` - - `; - } - private render_repeat_node( node: RepeatAction, path: string, @@ -475,25 +406,6 @@ export class HatScriptGraph extends LitElement { `; } - private render_scene_node( - node: SceneAction, - path: string, - graphStart = false, - disabled = false - ) { - return html` - - `; - } - private render_service_node( node: ServiceAction, path: string, @@ -580,24 +492,6 @@ export class HatScriptGraph extends LitElement { `; } - private render_stop_node( - node: Action, - path: string, - graphStart = false, - disabled = false - ) { - return html` - - `; - } - private render_other_node( node: Action, path: string, @@ -607,7 +501,7 @@ export class HatScriptGraph extends LitElement { return html` ([ @@ -68,6 +70,7 @@ export const ACTION_GROUPS: AutomationElementGroup = { members: { event: {}, service: {}, + set_conversation_response: {}, }, }, } as const; diff --git a/src/data/script.ts b/src/data/script.ts index 08910dc88f..0f585cf1e0 100644 --- a/src/data/script.ts +++ b/src/data/script.ts @@ -248,6 +248,10 @@ export interface ParallelAction extends BaseAction { parallel: ManualScriptConfig | Action | (ManualScriptConfig | Action)[]; } +export interface SetConversationResponseAction extends BaseAction { + set_conversation_response: string; +} + interface UnknownAction extends BaseAction { [key: string]: unknown; } @@ -292,6 +296,7 @@ export interface ActionTypes { play_media: PlayMediaAction; stop: StopAction; parallel: ParallelAction; + set_conversation_response: SetConversationResponseAction; unknown: UnknownAction; } @@ -383,6 +388,9 @@ export const getActionType = (action: Action): ActionType => { if ("parallel" in action) { return "parallel"; } + if ("set_conversation_response" in action) { + return "set_conversation_response"; + } if ("service" in action) { if ("metadata" in action) { if (is(action, activateSceneActionStruct)) { diff --git a/src/data/script_i18n.ts b/src/data/script_i18n.ts index 40c823bc86..22d5e3c0a7 100644 --- a/src/data/script_i18n.ts +++ b/src/data/script_i18n.ts @@ -27,6 +27,7 @@ import { PlayMediaAction, RepeatAction, SceneAction, + SetConversationResponseAction, StopAction, VariablesAction, WaitForTriggerAction, @@ -443,5 +444,13 @@ const tryDescribeAction = ( ); } + if (actionType === "set_conversation_response") { + const config = action as SetConversationResponseAction; + return hass.localize( + `${actionTranslationBaseKey}.set_conversation_response.description.full`, + { response: config.set_conversation_response } + ); + } + return actionType; }; 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 64c469a0db..69e811b0ba 100644 --- a/src/panels/config/automation/action/ha-automation-action-row.ts +++ b/src/panels/config/automation/action/ha-automation-action-row.ts @@ -77,6 +77,7 @@ import "./types/ha-automation-action-service"; import "./types/ha-automation-action-stop"; import "./types/ha-automation-action-wait_for_trigger"; import "./types/ha-automation-action-wait_template"; +import "./types/ha-automation-action-set_conversation_response"; export const getType = (action: Action | undefined) => { if (!action) { diff --git a/src/panels/config/automation/action/types/ha-automation-action-set_conversation_response.ts b/src/panels/config/automation/action/types/ha-automation-action-set_conversation_response.ts new file mode 100644 index 0000000000..b4c9b9c51d --- /dev/null +++ b/src/panels/config/automation/action/types/ha-automation-action-set_conversation_response.ts @@ -0,0 +1,54 @@ +import { html, LitElement } from "lit"; +import { customElement, property } from "lit/decorators"; +import "../../../../../components/ha-form/ha-form"; +import type { SetConversationResponseAction } from "../../../../../data/script"; +import type { HomeAssistant } from "../../../../../types"; +import type { ActionElement } from "../ha-automation-action-row"; + +const SCHEMA = [ + { + name: "set_conversation_response", + selector: { + template: {}, + }, + }, +] as const; + +@customElement("ha-automation-action-set_conversation_response") +export class HaSetConversationResponseAction + extends LitElement + implements ActionElement +{ + @property({ attribute: false }) public hass!: HomeAssistant; + + @property({ attribute: false }) public action!: SetConversationResponseAction; + + @property({ type: Boolean }) public disabled = false; + + public static get defaultConfig() { + return { set_conversation_response: "" }; + } + + protected render() { + return html` + + `; + } + + private _computeLabelCallback = (): string => + this.hass.localize( + "ui.panel.config.automation.editor.actions.type.set_conversation_response.label" + ); +} + +declare global { + interface HTMLElementTagNameMap { + "ha-automation-action-set_conversation_response": HaSetConversationResponseAction; + } +} diff --git a/src/translations/en.json b/src/translations/en.json index 0bb3f0844d..c55dd89633 100644 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -3117,6 +3117,13 @@ "full": "Test {condition}" } }, + "set_conversation_response": { + "label": "Set conversation response", + "description": { + "picker": "Set response of conversation when automation was triggered by conversation trigger.", + "full": "Set response of conversation to {response}" + } + }, "unknown": { "label": "Unknown" }