From 915036006d8d897d362eea24dbbdfc796cd1b7c2 Mon Sep 17 00:00:00 2001 From: karwosts <32912880+karwosts@users.noreply.github.com> Date: Thu, 19 Sep 2024 10:48:20 -0700 Subject: [PATCH] Fixes for trace viewer for nested triggers feature (#21765) --- src/components/trace/hat-script-graph.ts | 6 +++--- src/data/automation.ts | 26 ++++++++++++++++++++++++ src/data/trace.ts | 7 ++++++- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/components/trace/hat-script-graph.ts b/src/components/trace/hat-script-graph.ts index 804261d6ff8e..28b1fdf185e6 100644 --- a/src/components/trace/hat-script-graph.ts +++ b/src/components/trace/hat-script-graph.ts @@ -22,7 +22,7 @@ import { LitElement, PropertyValues, css, html, nothing } from "lit"; import { customElement, property } from "lit/decorators"; import { ensureArray } from "../../common/array/ensure-array"; import { fireEvent } from "../../common/dom/fire_event"; -import { Condition, Trigger } from "../../data/automation"; +import { Condition, Trigger, flattenTriggers } from "../../data/automation"; import { Action, ChooseAction, @@ -572,8 +572,8 @@ export class HatScriptGraph extends LitElement { const paths = Object.keys(this.trackedNodes); const trigger_nodes = "trigger" in this.trace.config - ? ensureArray(this.trace.config.trigger).map((trigger, i) => - this.render_trigger(trigger, i) + ? flattenTriggers(ensureArray(this.trace.config.trigger)).map( + (trigger, i) => this.render_trigger(trigger, i) ) : undefined; try { diff --git a/src/data/automation.ts b/src/data/automation.ts index 7f9fb7000b18..f5f66c0a8fdd 100644 --- a/src/data/automation.ts +++ b/src/data/automation.ts @@ -3,6 +3,7 @@ import { HassEntityBase, } from "home-assistant-js-websocket"; import { navigate } from "../common/navigate"; +import { ensureArray } from "../common/array/ensure-array"; import { Context, HomeAssistant } from "../types"; import { BlueprintInput } from "./blueprint"; import { DeviceCondition, DeviceTrigger } from "./device_automation"; @@ -62,6 +63,10 @@ export interface ContextConstraint { user_id?: string | string[]; } +export interface TriggerList { + triggers: Trigger | Trigger[] | undefined; +} + export interface BaseTrigger { alias?: string; platform: string; @@ -373,6 +378,27 @@ export const normalizeAutomationConfig = < return config; }; +export const flattenTriggers = ( + triggers: undefined | (Trigger | TriggerList)[] +): Trigger[] => { + if (!triggers) { + return []; + } + + const flatTriggers: Trigger[] = []; + + triggers.forEach((t) => { + if ("triggers" in t) { + if (t.triggers) { + flatTriggers.push(...ensureArray(t.triggers)); + } + } else { + flatTriggers.push(t); + } + }); + return flatTriggers; +}; + export const showAutomationEditor = (data?: Partial) => { initialAutomationEditorData = data; navigate("/config/automation/edit/new"); diff --git a/src/data/trace.ts b/src/data/trace.ts index 1c64152904b1..2e5d3f1a4fb3 100644 --- a/src/data/trace.ts +++ b/src/data/trace.ts @@ -3,6 +3,7 @@ import { Context, HomeAssistant } from "../types"; import { BlueprintAutomationConfig, ManualAutomationConfig, + flattenTriggers, } from "./automation"; import { BlueprintScriptConfig, ScriptConfig } from "./script"; @@ -190,7 +191,11 @@ export const getDataFromPath = ( if (!tempResult && raw === "sequence") { continue; } - result = tempResult; + if (raw === "trigger") { + result = flattenTriggers(tempResult); + } else { + result = tempResult; + } continue; }