Reuse flatten logic for trigger ids condition (#22136)

This commit is contained in:
Paul Bottein 2024-09-27 17:18:06 +02:00 committed by GitHub
parent 7ee5db2be5
commit ead54e445f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 18 deletions

View File

@ -442,7 +442,7 @@ export const migrateAutomationTrigger = (
};
export const flattenTriggers = (
triggers: undefined | Trigger | (Trigger | TriggerList)[]
triggers: undefined | Trigger | Trigger[]
): Trigger[] => {
if (!triggers) {
return [];
@ -453,7 +453,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);

View File

@ -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<string> = 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")