Skip to content

Commit

Permalink
Fixes for trace viewer for nested triggers feature (#21765)
Browse files Browse the repository at this point in the history
  • Loading branch information
karwosts authored Sep 19, 2024
1 parent 48887f2 commit 9150360
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/components/trace/hat-script-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
26 changes: 26 additions & 0 deletions src/data/automation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<AutomationConfig>) => {
initialAutomationEditorData = data;
navigate("/config/automation/edit/new");
Expand Down
7 changes: 6 additions & 1 deletion src/data/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Context, HomeAssistant } from "../types";
import {
BlueprintAutomationConfig,
ManualAutomationConfig,
flattenTriggers,
} from "./automation";
import { BlueprintScriptConfig, ScriptConfig } from "./script";

Expand Down Expand Up @@ -190,7 +191,11 @@ export const getDataFromPath = (
if (!tempResult && raw === "sequence") {
continue;
}
result = tempResult;
if (raw === "trigger") {
result = flattenTriggers(tempResult);
} else {
result = tempResult;
}
continue;
}

Expand Down

0 comments on commit 9150360

Please sign in to comment.