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; }