From be491451d56c0e8dc33aeadc3a74cd6466f8adad Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Wed, 23 Feb 2022 21:36:25 -0800 Subject: [PATCH] Add run action to dropdown (#11817) --- src/data/config.ts | 13 +++-- src/data/service.ts | 5 +- .../action/ha-automation-action-row.ts | 54 +++++++++++++++++-- src/translations/en.json | 4 ++ 4 files changed, 68 insertions(+), 8 deletions(-) diff --git a/src/data/config.ts b/src/data/config.ts index 281e2debbf..c2273ce72b 100644 --- a/src/data/config.ts +++ b/src/data/config.ts @@ -1,8 +1,13 @@ import { HomeAssistant } from "../types"; -interface ValidationResult { - valid: boolean; - error: string | null; +interface ValidConfig { + valid: true; + error: null; +} + +interface InvalidConfig { + valid: false; + error: string; } type ValidKeys = "trigger" | "action" | "condition"; @@ -12,7 +17,7 @@ export const validateConfig = < >( hass: HomeAssistant, config: T -): Promise> => +): Promise> => hass.callWS({ type: "validate_config", ...config, diff --git a/src/data/service.ts b/src/data/service.ts index 5e83fd6741..b9ca93fe87 100644 --- a/src/data/service.ts +++ b/src/data/service.ts @@ -1,7 +1,10 @@ import { HomeAssistant } from "../types"; import { Action } from "./script"; -export const callExecuteScript = (hass: HomeAssistant, sequence: Action[]) => +export const callExecuteScript = ( + hass: HomeAssistant, + sequence: Action | Action[] +) => hass.callWS({ type: "execute_script", sequence, 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 1844341245..fce39cee2c 100644 --- a/src/panels/config/automation/action/ha-automation-action-row.ts +++ b/src/panels/config/automation/action/ha-automation-action-row.ts @@ -16,10 +16,16 @@ import "../../../../components/ha-icon-button"; import "../../../../components/ha-select"; import type { HaSelect } from "../../../../components/ha-select"; import type { HaYamlEditor } from "../../../../components/ha-yaml-editor"; +import { validateConfig } from "../../../../data/config"; import { Action, getActionType } from "../../../../data/script"; -import { showConfirmationDialog } from "../../../../dialogs/generic/show-dialog-box"; +import { callExecuteScript } from "../../../../data/service"; +import { + showAlertDialog, + showConfirmationDialog, +} from "../../../../dialogs/generic/show-dialog-box"; import { haStyle } from "../../../../resources/styles"; import type { HomeAssistant } from "../../../../types"; +import { showToast } from "../../../../util/toast"; import "./types/ha-automation-action-activate_scene"; import "./types/ha-automation-action-choose"; import "./types/ha-automation-action-condition"; @@ -180,6 +186,11 @@ export default class HaAutomationActionRow extends LitElement { .label=${this.hass.localize("ui.common.menu")} .path=${mdiDotsVertical} > + + ${this.hass.localize( + "ui.panel.config.automation.editor.actions.run_action" + )} + ${yamlMode ? this.hass.localize( @@ -290,17 +301,54 @@ export default class HaAutomationActionRow extends LitElement { private _handleAction(ev: CustomEvent) { switch (ev.detail.index) { case 0: - this._switchYamlMode(); + this._runAction(); break; case 1: - fireEvent(this, "duplicate"); + this._switchYamlMode(); break; case 2: + fireEvent(this, "duplicate"); + break; + case 3: this._onDelete(); break; } } + private async _runAction() { + const validated = await validateConfig(this.hass, { + action: this.action, + }); + + if (!validated.action.valid) { + showAlertDialog(this, { + title: this.hass.localize( + "ui.panel.config.automation.editor.actions.invalid_action" + ), + text: validated.action.error, + }); + return; + } + + try { + await callExecuteScript(this.hass, this.action); + } catch (err: any) { + showAlertDialog(this, { + title: this.hass.localize( + "ui.panel.config.automation.editor.actions.run_action_error" + ), + text: err.message || err, + }); + return; + } + + showToast(this, { + message: this.hass.localize( + "ui.panel.config.automation.editor.actions.run_action_success" + ), + }); + } + private _onDelete() { showConfirmationDialog(this, { text: this.hass.localize( diff --git a/src/translations/en.json b/src/translations/en.json index da653b245d..3cdef6fd63 100755 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -1826,6 +1826,10 @@ "introduction": "The actions are what Home Assistant will do when the automation is triggered.", "learn_more": "Learn more about actions", "add": "Add action", + "invalid_action": "Invalid action", + "run_action": "Run action", + "run_action_error": "Error running action", + "run_action_success": "Action run successfully", "duplicate": "[%key:ui::panel::config::automation::editor::triggers::duplicate%]", "delete": "[%key:ui::panel::mailbox::delete_button%]", "delete_confirm": "[%key:ui::panel::config::automation::editor::triggers::delete_confirm%]",