From 7772edd36e1e1989b8a5c414583fb6bf306d27a7 Mon Sep 17 00:00:00 2001 From: Paul Bottein Date: Fri, 27 Sep 2024 17:05:06 +0200 Subject: [PATCH] Reuse flatten logic for trigger ids condition --- src/data/automation.ts | 4 +-- .../types/ha-automation-condition-trigger.ts | 25 +++++++------------ 2 files changed, 11 insertions(+), 18 deletions(-) diff --git a/src/data/automation.ts b/src/data/automation.ts index f08170c2cc86..2137535b42ca 100644 --- a/src/data/automation.ts +++ b/src/data/automation.ts @@ -438,7 +438,7 @@ export const migrateAutomationTrigger = ( }; export const flattenTriggers = ( - triggers: undefined | Trigger | (Trigger | TriggerList)[] + triggers: undefined | Trigger | Trigger[] ): Trigger[] => { if (!triggers) { return []; @@ -449,7 +449,7 @@ export const flattenTriggers = ( ensureArray(triggers).forEach((t) => { if ("triggers" in t) { if (t.triggers) { - flatTriggers.push(...ensureArray(t.triggers)); + flatTriggers.push(...flattenTriggers(t.triggers)); } } else { flatTriggers.push(t); diff --git a/src/panels/config/automation/condition/types/ha-automation-condition-trigger.ts b/src/panels/config/automation/condition/types/ha-automation-condition-trigger.ts index 4f8462b9f0ec..7adf81b0f8c6 100644 --- a/src/panels/config/automation/condition/types/ha-automation-condition-trigger.ts +++ b/src/panels/config/automation/condition/types/ha-automation-condition-trigger.ts @@ -8,26 +8,19 @@ import { fireEvent } from "../../../../../common/dom/fire_event"; import "../../../../../components/ha-form/ha-form"; import type { SchemaUnion } from "../../../../../components/ha-form/types"; import "../../../../../components/ha-select"; -import type { - AutomationConfig, - Trigger, - TriggerCondition, +import { + flattenTriggers, + type AutomationConfig, + type Trigger, + type TriggerCondition, } from "../../../../../data/automation"; import type { HomeAssistant } from "../../../../../types"; const getTriggersIds = (triggers: Trigger[]): string[] => { - const ids: Set = new Set(); - triggers.forEach((trigger) => { - if ("triggers" in trigger) { - const newIds = getTriggersIds(ensureArray(trigger.triggers)); - for (const id of newIds) { - ids.add(id); - } - } else if (trigger.id) { - ids.add(trigger.id); - } - }); - return Array.from(ids); + const triggerIds = flattenTriggers(triggers) + .map((t) => ("id" in t ? t.id : undefined)) + .filter(Boolean) as string[]; + return Array.from(new Set(triggerIds)); }; @customElement("ha-automation-condition-trigger")