Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reuse flatten logic for trigger ids condition #22136

Merged
merged 1 commit into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/data/automation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ export const migrateAutomationTrigger = (
};

export const flattenTriggers = (
triggers: undefined | Trigger | (Trigger | TriggerList)[]
triggers: undefined | Trigger | Trigger[]
): Trigger[] => {
if (!triggers) {
return [];
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Loading