From 1ba941282cb3f19279b468227f548e88336670af Mon Sep 17 00:00:00 2001 From: Bram Kragten Date: Wed, 30 Apr 2025 15:57:16 +0300 Subject: [PATCH] Allow pasting more automation config formats (#25239) --- .../automation/manual-automation-editor.ts | 55 ++++++++++++++++++- 1 file changed, 52 insertions(+), 3 deletions(-) diff --git a/src/panels/config/automation/manual-automation-editor.ts b/src/panels/config/automation/manual-automation-editor.ts index a064b65a8d..57583f0505 100644 --- a/src/panels/config/automation/manual-automation-editor.ts +++ b/src/panels/config/automation/manual-automation-editor.ts @@ -27,7 +27,7 @@ import type { Trigger, } from "../../../data/automation"; import { normalizeAutomationConfig } from "../../../data/automation"; -import type { Action } from "../../../data/script"; +import { getActionType, type Action } from "../../../data/script"; import { haStyle } from "../../../resources/styles"; import type { HomeAssistant } from "../../../types"; import { documentationUrl } from "../../../util/documentation-url"; @@ -312,10 +312,59 @@ export class HaManualAutomationEditor extends LitElement { const loaded: any = load(paste); if (loaded) { - let normalized: AutomationConfig | undefined; + let config = loaded; + + if ("automation" in config) { + config = config.automation; + if (Array.isArray(config)) { + config = config[0]; + } + } + + if (Array.isArray(config)) { + if (config.length === 1) { + config = config[0]; + } else { + const newConfig: AutomationConfig = { + triggers: [], + conditions: [], + actions: [], + }; + let found = false; + config.forEach((cfg: any) => { + if ("trigger" in cfg) { + found = true; + (newConfig.triggers as Trigger[]).push(cfg); + } + if ("condition" in cfg) { + found = true; + (newConfig.conditions as Condition[]).push(cfg); + } + if (getActionType(cfg) !== "unknown") { + found = true; + (newConfig.actions as Action[]).push(cfg); + } + }); + if (found) { + config = newConfig; + } + } + } + + if ("trigger" in config) { + config = { triggers: [config] }; + } + if ("condition" in config) { + config = { conditions: [config] }; + } + if (getActionType(config) !== "unknown") { + config = { actions: [config] }; + } + + let normalized: AutomationConfig; try { - normalized = normalizeAutomationConfig(loaded); + normalized = normalizeAutomationConfig(config); } catch (_err: any) { return; }