Migrate trigger platform key to trigger (#22054)

* Migrate trigger platform key to trigger

* fix gallery configs

* Update ha-automation-editor.ts

* migrate device automation triggers
This commit is contained in:
Bram Kragten 2024-09-25 14:20:27 +02:00 committed by GitHub
parent 2793ca65cd
commit dc940f248c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
37 changed files with 167 additions and 120 deletions

View File

@ -219,7 +219,7 @@ export const basicTrace: DemoTrace = {
description: "", description: "",
triggers: [ triggers: [
{ {
platform: "state", trigger: "state",
entity_id: "input_boolean.toggle_1", entity_id: "input_boolean.toggle_1",
}, },
], ],

View File

@ -135,7 +135,7 @@ export const motionLightTrace: DemoTrace = {
max_exceeded: "silent", max_exceeded: "silent",
triggers: [ triggers: [
{ {
platform: "state", trigger: "state",
entity_id: "binary_sensor.pauluss_macbook_pro_camera_in_use", entity_id: "binary_sensor.pauluss_macbook_pro_camera_in_use",
from: "off", from: "off",
to: "on", to: "on",

View File

@ -48,7 +48,7 @@ const ACTIONS = [
{ {
wait_for_trigger: [ wait_for_trigger: [
{ {
platform: "state", trigger: "state",
entity_id: "input_boolean.toggle_1", entity_id: "input_boolean.toggle_1",
}, },
], ],

View File

@ -22,46 +22,46 @@ const ENTITIES = [
]; ];
const triggers = [ const triggers = [
{ platform: "state", entity_id: "light.kitchen", from: "off", to: "on" }, { trigger: "state", entity_id: "light.kitchen", from: "off", to: "on" },
{ platform: "mqtt" }, { trigger: "mqtt" },
{ {
platform: "geo_location", trigger: "geo_location",
source: "test_source", source: "test_source",
zone: "zone.home", zone: "zone.home",
event: "enter", event: "enter",
}, },
{ platform: "homeassistant", event: "start" }, { trigger: "homeassistant", event: "start" },
{ {
platform: "numeric_state", trigger: "numeric_state",
entity_id: "light.kitchen", entity_id: "light.kitchen",
attribute: "brightness", attribute: "brightness",
below: 80, below: 80,
above: 20, above: 20,
}, },
{ platform: "sun", event: "sunset" }, { trigger: "sun", event: "sunset" },
{ platform: "time_pattern" }, { trigger: "time_pattern" },
{ platform: "time_pattern", hours: "*", minutes: "/5", seconds: "10" }, { trigger: "time_pattern", hours: "*", minutes: "/5", seconds: "10" },
{ platform: "webhook" }, { trigger: "webhook" },
{ platform: "persistent_notification" }, { trigger: "persistent_notification" },
{ {
platform: "zone", trigger: "zone",
entity_id: "person.person", entity_id: "person.person",
zone: "zone.home", zone: "zone.home",
event: "enter", event: "enter",
}, },
{ platform: "tag" }, { trigger: "tag" },
{ platform: "time", at: "15:32" }, { trigger: "time", at: "15:32" },
{ platform: "template" }, { trigger: "template" },
{ platform: "conversation", command: "Turn on the lights" }, { trigger: "conversation", command: "Turn on the lights" },
{ {
platform: "conversation", trigger: "conversation",
command: ["Turn on the lights", "Turn the lights on"], command: ["Turn on the lights", "Turn the lights on"],
}, },
{ platform: "event", event_type: "homeassistant_started" }, { trigger: "event", event_type: "homeassistant_started" },
]; ];
const initialTrigger: Trigger = { const initialTrigger: Trigger = {
platform: "state", trigger: "state",
entity_id: "light.kitchen", entity_id: "light.kitchen",
}; };

View File

@ -111,7 +111,7 @@ const SCHEMAS: { name: string; triggers: Trigger[] }[] = [
triggers: [ triggers: [
{ ...HaConversationTrigger.defaultConfig }, { ...HaConversationTrigger.defaultConfig },
{ {
platform: "conversation", trigger: "conversation",
command: ["Turn on the lights", "Turn the lights on"], command: ["Turn on the lights", "Turn the lights on"],
}, },
], ],

View File

@ -26,7 +26,7 @@ class HaDeviceTriggerPicker extends HaDeviceAutomationPicker<DeviceTrigger> {
fetchDeviceTriggers, fetchDeviceTriggers,
(deviceId?: string) => ({ (deviceId?: string) => ({
device_id: deviceId || "", device_id: deviceId || "",
platform: "device", trigger: "device",
domain: "", domain: "",
entity_id: "", entity_id: "",
}) })

View File

@ -1,6 +1,7 @@
import { css, CSSResultGroup, html, LitElement, nothing } from "lit"; import { css, CSSResultGroup, html, LitElement, nothing } from "lit";
import { customElement, property } from "lit/decorators"; import { customElement, property } from "lit/decorators";
import { Action } from "../../data/script"; import memoizeOne from "memoize-one";
import { Action, migrateAutomationAction } from "../../data/script";
import { ActionSelector } from "../../data/selector"; import { ActionSelector } from "../../data/selector";
import "../../panels/config/automation/action/ha-automation-action"; import "../../panels/config/automation/action/ha-automation-action";
import { HomeAssistant } from "../../types"; import { HomeAssistant } from "../../types";
@ -17,12 +18,19 @@ export class HaActionSelector extends LitElement {
@property({ type: Boolean, reflect: true }) public disabled = false; @property({ type: Boolean, reflect: true }) public disabled = false;
private _actions = memoizeOne((action: Action | undefined) => {
if (!action) {
return [];
}
return migrateAutomationAction(action);
});
protected render() { protected render() {
return html` return html`
${this.label ? html`<label>${this.label}</label>` : nothing} ${this.label ? html`<label>${this.label}</label>` : nothing}
<ha-automation-action <ha-automation-action
.disabled=${this.disabled} .disabled=${this.disabled}
.actions=${this.value || []} .actions=${this._actions(this.value)}
.hass=${this.hass} .hass=${this.hass}
.path=${this.selector.action?.path} .path=${this.selector.action?.path}
></ha-automation-action> ></ha-automation-action>

View File

@ -7,12 +7,7 @@ import "../ha-code-editor";
import "../ha-input-helper-text"; import "../ha-input-helper-text";
import "../ha-alert"; import "../ha-alert";
const WARNING_STRINGS = [ const WARNING_STRINGS = ["template:", "sensor:", "state:", "trigger: template"];
"template:",
"sensor:",
"state:",
"platform: template",
];
@customElement("ha-selector-template") @customElement("ha-selector-template")
export class HaTemplateSelector extends LitElement { export class HaTemplateSelector extends LitElement {

View File

@ -1,6 +1,7 @@
import { css, CSSResultGroup, html, LitElement, nothing } from "lit"; import { css, CSSResultGroup, html, LitElement, nothing } from "lit";
import { customElement, property } from "lit/decorators"; import { customElement, property } from "lit/decorators";
import { Trigger } from "../../data/automation"; import memoizeOne from "memoize-one";
import { migrateAutomationTrigger, Trigger } from "../../data/automation";
import { TriggerSelector } from "../../data/selector"; import { TriggerSelector } from "../../data/selector";
import "../../panels/config/automation/trigger/ha-automation-trigger"; import "../../panels/config/automation/trigger/ha-automation-trigger";
import { HomeAssistant } from "../../types"; import { HomeAssistant } from "../../types";
@ -17,12 +18,19 @@ export class HaTriggerSelector extends LitElement {
@property({ type: Boolean, reflect: true }) public disabled = false; @property({ type: Boolean, reflect: true }) public disabled = false;
private _triggers = memoizeOne((trigger: Trigger | undefined) => {
if (!trigger) {
return [];
}
return migrateAutomationTrigger(trigger);
});
protected render() { protected render() {
return html` return html`
${this.label ? html`<label>${this.label}</label>` : nothing} ${this.label ? html`<label>${this.label}</label>` : nothing}
<ha-automation-trigger <ha-automation-trigger
.disabled=${this.disabled} .disabled=${this.disabled}
.triggers=${this.value || []} .triggers=${this._triggers(this.value)}
.hass=${this.hass} .hass=${this.hass}
.path=${this.selector.trigger?.path} .path=${this.selector.trigger?.path}
></ha-automation-trigger> ></ha-automation-trigger>

View File

@ -75,14 +75,16 @@ export interface TriggerList {
export interface BaseTrigger { export interface BaseTrigger {
alias?: string; alias?: string;
platform: string; /** @deprecated Use `trigger` instead */
platform?: string;
trigger: string;
id?: string; id?: string;
variables?: Record<string, unknown>; variables?: Record<string, unknown>;
enabled?: boolean; enabled?: boolean;
} }
export interface StateTrigger extends BaseTrigger { export interface StateTrigger extends BaseTrigger {
platform: "state"; trigger: "state";
entity_id: string | string[]; entity_id: string | string[];
attribute?: string; attribute?: string;
from?: string | string[]; from?: string | string[];
@ -91,25 +93,25 @@ export interface StateTrigger extends BaseTrigger {
} }
export interface MqttTrigger extends BaseTrigger { export interface MqttTrigger extends BaseTrigger {
platform: "mqtt"; trigger: "mqtt";
topic: string; topic: string;
payload?: string; payload?: string;
} }
export interface GeoLocationTrigger extends BaseTrigger { export interface GeoLocationTrigger extends BaseTrigger {
platform: "geo_location"; trigger: "geo_location";
source: string; source: string;
zone: string; zone: string;
event: "enter" | "leave"; event: "enter" | "leave";
} }
export interface HassTrigger extends BaseTrigger { export interface HassTrigger extends BaseTrigger {
platform: "homeassistant"; trigger: "homeassistant";
event: "start" | "shutdown"; event: "start" | "shutdown";
} }
export interface NumericStateTrigger extends BaseTrigger { export interface NumericStateTrigger extends BaseTrigger {
platform: "numeric_state"; trigger: "numeric_state";
entity_id: string | string[]; entity_id: string | string[];
attribute?: string; attribute?: string;
above?: number; above?: number;
@ -119,69 +121,69 @@ export interface NumericStateTrigger extends BaseTrigger {
} }
export interface ConversationTrigger extends BaseTrigger { export interface ConversationTrigger extends BaseTrigger {
platform: "conversation"; trigger: "conversation";
command: string | string[]; command: string | string[];
} }
export interface SunTrigger extends BaseTrigger { export interface SunTrigger extends BaseTrigger {
platform: "sun"; trigger: "sun";
offset: number; offset: number;
event: "sunrise" | "sunset"; event: "sunrise" | "sunset";
} }
export interface TimePatternTrigger extends BaseTrigger { export interface TimePatternTrigger extends BaseTrigger {
platform: "time_pattern"; trigger: "time_pattern";
hours?: number | string; hours?: number | string;
minutes?: number | string; minutes?: number | string;
seconds?: number | string; seconds?: number | string;
} }
export interface WebhookTrigger extends BaseTrigger { export interface WebhookTrigger extends BaseTrigger {
platform: "webhook"; trigger: "webhook";
webhook_id: string; webhook_id: string;
allowed_methods?: string[]; allowed_methods?: string[];
local_only?: boolean; local_only?: boolean;
} }
export interface PersistentNotificationTrigger extends BaseTrigger { export interface PersistentNotificationTrigger extends BaseTrigger {
platform: "persistent_notification"; trigger: "persistent_notification";
notification_id?: string; notification_id?: string;
update_type?: string[]; update_type?: string[];
} }
export interface ZoneTrigger extends BaseTrigger { export interface ZoneTrigger extends BaseTrigger {
platform: "zone"; trigger: "zone";
entity_id: string; entity_id: string;
zone: string; zone: string;
event: "enter" | "leave"; event: "enter" | "leave";
} }
export interface TagTrigger extends BaseTrigger { export interface TagTrigger extends BaseTrigger {
platform: "tag"; trigger: "tag";
tag_id: string; tag_id: string;
device_id?: string; device_id?: string;
} }
export interface TimeTrigger extends BaseTrigger { export interface TimeTrigger extends BaseTrigger {
platform: "time"; trigger: "time";
at: string; at: string;
} }
export interface TemplateTrigger extends BaseTrigger { export interface TemplateTrigger extends BaseTrigger {
platform: "template"; trigger: "template";
value_template: string; value_template: string;
for?: string | number | ForDict; for?: string | number | ForDict;
} }
export interface EventTrigger extends BaseTrigger { export interface EventTrigger extends BaseTrigger {
platform: "event"; trigger: "event";
event_type: string; event_type: string;
event_data?: any; event_data?: any;
context?: ContextConstraint; context?: ContextConstraint;
} }
export interface CalendarTrigger extends BaseTrigger { export interface CalendarTrigger extends BaseTrigger {
platform: "calendar"; trigger: "calendar";
event: "start" | "end"; event: "start" | "end";
entity_id: string; entity_id: string;
offset: string; offset: string;
@ -379,10 +381,6 @@ export const normalizeAutomationConfig = <
} }
} }
if (config.actions) {
config.actions = migrateAutomationAction(config.actions);
}
return config; return config;
}; };
@ -409,9 +407,35 @@ export const migrateAutomationConfig = <
} }
delete config.action; delete config.action;
} }
if (config.triggers) {
config.triggers = migrateAutomationTrigger(config.triggers);
}
if (config.actions) {
config.actions = migrateAutomationAction(config.actions);
}
return config; return config;
}; };
export const migrateAutomationTrigger = (
trigger: Trigger | Trigger[]
): Trigger | Trigger[] => {
if (Array.isArray(trigger)) {
return trigger.map(migrateAutomationTrigger) as Trigger[];
}
if ("platform" in trigger) {
if (!("trigger" in trigger)) {
// @ts-ignore
trigger.trigger = trigger.platform;
}
delete trigger.platform;
}
return trigger;
};
export const flattenTriggers = ( export const flattenTriggers = (
triggers: undefined | Trigger | (Trigger | TriggerList)[] triggers: undefined | Trigger | (Trigger | TriggerList)[]
): Trigger[] => { ): Trigger[] => {

View File

@ -103,7 +103,7 @@ const tryDescribeTrigger = (
} }
// Event Trigger // Event Trigger
if (trigger.platform === "event" && trigger.event_type) { if (trigger.trigger === "event" && trigger.event_type) {
const eventTypes: string[] = []; const eventTypes: string[] = [];
if (Array.isArray(trigger.event_type)) { if (Array.isArray(trigger.event_type)) {
@ -122,7 +122,7 @@ const tryDescribeTrigger = (
} }
// Home Assistant Trigger // Home Assistant Trigger
if (trigger.platform === "homeassistant" && trigger.event) { if (trigger.trigger === "homeassistant" && trigger.event) {
return hass.localize( return hass.localize(
trigger.event === "start" trigger.event === "start"
? `${triggerTranslationBaseKey}.homeassistant.description.started` ? `${triggerTranslationBaseKey}.homeassistant.description.started`
@ -131,7 +131,7 @@ const tryDescribeTrigger = (
} }
// Numeric State Trigger // Numeric State Trigger
if (trigger.platform === "numeric_state" && trigger.entity_id) { if (trigger.trigger === "numeric_state" && trigger.entity_id) {
const entities: string[] = []; const entities: string[] = [];
const states = hass.states; const states = hass.states;
@ -206,7 +206,7 @@ const tryDescribeTrigger = (
} }
// State Trigger // State Trigger
if (trigger.platform === "state") { if (trigger.trigger === "state") {
const entities: string[] = []; const entities: string[] = [];
const states = hass.states; const states = hass.states;
@ -329,7 +329,7 @@ const tryDescribeTrigger = (
} }
// Sun Trigger // Sun Trigger
if (trigger.platform === "sun" && trigger.event) { if (trigger.trigger === "sun" && trigger.event) {
let duration = ""; let duration = "";
if (trigger.offset) { if (trigger.offset) {
if (typeof trigger.offset === "number") { if (typeof trigger.offset === "number") {
@ -350,12 +350,12 @@ const tryDescribeTrigger = (
} }
// Tag Trigger // Tag Trigger
if (trigger.platform === "tag") { if (trigger.trigger === "tag") {
return hass.localize(`${triggerTranslationBaseKey}.tag.description.full`); return hass.localize(`${triggerTranslationBaseKey}.tag.description.full`);
} }
// Time Trigger // Time Trigger
if (trigger.platform === "time" && trigger.at) { if (trigger.trigger === "time" && trigger.at) {
const result = ensureArray(trigger.at).map((at) => const result = ensureArray(trigger.at).map((at) =>
typeof at !== "string" typeof at !== "string"
? at ? at
@ -370,7 +370,7 @@ const tryDescribeTrigger = (
} }
// Time Pattern Trigger // Time Pattern Trigger
if (trigger.platform === "time_pattern") { if (trigger.trigger === "time_pattern") {
if (!trigger.seconds && !trigger.minutes && !trigger.hours) { if (!trigger.seconds && !trigger.minutes && !trigger.hours) {
return hass.localize( return hass.localize(
`${triggerTranslationBaseKey}.time_pattern.description.initial` `${triggerTranslationBaseKey}.time_pattern.description.initial`
@ -547,7 +547,7 @@ const tryDescribeTrigger = (
} }
// Zone Trigger // Zone Trigger
if (trigger.platform === "zone" && trigger.entity_id && trigger.zone) { if (trigger.trigger === "zone" && trigger.entity_id && trigger.zone) {
const entities: string[] = []; const entities: string[] = [];
const zones: string[] = []; const zones: string[] = [];
@ -590,7 +590,7 @@ const tryDescribeTrigger = (
} }
// Geo Location Trigger // Geo Location Trigger
if (trigger.platform === "geo_location" && trigger.source && trigger.zone) { if (trigger.trigger === "geo_location" && trigger.source && trigger.zone) {
const sources: string[] = []; const sources: string[] = [];
const zones: string[] = []; const zones: string[] = [];
const states = hass.states; const states = hass.states;
@ -629,12 +629,12 @@ const tryDescribeTrigger = (
} }
// MQTT Trigger // MQTT Trigger
if (trigger.platform === "mqtt") { if (trigger.trigger === "mqtt") {
return hass.localize(`${triggerTranslationBaseKey}.mqtt.description.full`); return hass.localize(`${triggerTranslationBaseKey}.mqtt.description.full`);
} }
// Template Trigger // Template Trigger
if (trigger.platform === "template") { if (trigger.trigger === "template") {
let duration = ""; let duration = "";
if (trigger.for) { if (trigger.for) {
duration = describeDuration(hass.locale, trigger.for) ?? ""; duration = describeDuration(hass.locale, trigger.for) ?? "";
@ -647,14 +647,14 @@ const tryDescribeTrigger = (
} }
// Webhook Trigger // Webhook Trigger
if (trigger.platform === "webhook") { if (trigger.trigger === "webhook") {
return hass.localize( return hass.localize(
`${triggerTranslationBaseKey}.webhook.description.full` `${triggerTranslationBaseKey}.webhook.description.full`
); );
} }
// Conversation Trigger // Conversation Trigger
if (trigger.platform === "conversation") { if (trigger.trigger === "conversation") {
if (!trigger.command) { if (!trigger.command) {
return hass.localize( return hass.localize(
`${triggerTranslationBaseKey}.conversation.description.empty` `${triggerTranslationBaseKey}.conversation.description.empty`
@ -673,14 +673,14 @@ const tryDescribeTrigger = (
} }
// Persistent Notification Trigger // Persistent Notification Trigger
if (trigger.platform === "persistent_notification") { if (trigger.trigger === "persistent_notification") {
return hass.localize( return hass.localize(
`${triggerTranslationBaseKey}.persistent_notification.description.full` `${triggerTranslationBaseKey}.persistent_notification.description.full`
); );
} }
// Device Trigger // Device Trigger
if (trigger.platform === "device" && trigger.device_id) { if (trigger.trigger === "device" && trigger.device_id) {
const config = trigger as DeviceTrigger; const config = trigger as DeviceTrigger;
const localized = localizeDeviceAutomationTrigger( const localized = localizeDeviceAutomationTrigger(
hass, hass,
@ -698,7 +698,7 @@ const tryDescribeTrigger = (
return ( return (
hass.localize( hass.localize(
`ui.panel.config.automation.editor.triggers.type.${trigger.platform}.label` `ui.panel.config.automation.editor.triggers.type.${trigger.trigger}.label`
) || ) ||
hass.localize(`ui.panel.config.automation.editor.triggers.unknown_trigger`) hass.localize(`ui.panel.config.automation.editor.triggers.unknown_trigger`)
); );

View File

@ -1,7 +1,7 @@
import { computeStateName } from "../common/entity/compute_state_name"; import { computeStateName } from "../common/entity/compute_state_name";
import type { HaFormSchema } from "../components/ha-form/types"; import type { HaFormSchema } from "../components/ha-form/types";
import { HomeAssistant } from "../types"; import { HomeAssistant } from "../types";
import { BaseTrigger } from "./automation"; import { BaseTrigger, migrateAutomationTrigger } from "./automation";
import { import {
computeEntityRegistryName, computeEntityRegistryName,
entityRegistryByEntityId, entityRegistryByEntityId,
@ -31,7 +31,7 @@ export interface DeviceCondition extends DeviceAutomation {
export type DeviceTrigger = DeviceAutomation & export type DeviceTrigger = DeviceAutomation &
BaseTrigger & { BaseTrigger & {
platform: "device"; trigger: "device";
}; };
export interface DeviceCapabilities { export interface DeviceCapabilities {
@ -51,10 +51,12 @@ export const fetchDeviceConditions = (hass: HomeAssistant, deviceId: string) =>
}); });
export const fetchDeviceTriggers = (hass: HomeAssistant, deviceId: string) => export const fetchDeviceTriggers = (hass: HomeAssistant, deviceId: string) =>
hass.callWS<DeviceTrigger[]>({ hass
type: "device_automation/trigger/list", .callWS<DeviceTrigger[]>({
device_id: deviceId, type: "device_automation/trigger/list",
}); device_id: deviceId,
})
.then((triggers) => migrateAutomationTrigger(triggers) as DeviceTrigger[]);
export const fetchDeviceActionCapabilities = ( export const fetchDeviceActionCapabilities = (
hass: HomeAssistant, hass: HomeAssistant,
@ -91,7 +93,7 @@ const deviceAutomationIdentifiers = [
"subtype", "subtype",
"event", "event",
"condition", "condition",
"platform", "trigger",
]; ];
export const deviceAutomationsEqual = ( export const deviceAutomationsEqual = (

View File

@ -20,6 +20,7 @@ import { navigate } from "../common/navigate";
import { HomeAssistant } from "../types"; import { HomeAssistant } from "../types";
import { import {
Condition, Condition,
migrateAutomationTrigger,
ShorthandAndCondition, ShorthandAndCondition,
ShorthandNotCondition, ShorthandNotCondition,
ShorthandOrCondition, ShorthandOrCondition,
@ -480,5 +481,10 @@ export const migrateAutomationAction = (
} }
} }
if (actionType === "wait_for_trigger") {
const _action = action as WaitForTriggerAction;
migrateAutomationTrigger(_action.wait_for_trigger);
}
return action; return action;
}; };

View File

@ -55,6 +55,7 @@ import {
Action, Action,
NonConditionAction, NonConditionAction,
getActionType, getActionType,
migrateAutomationAction,
} from "../../../../data/script"; } from "../../../../data/script";
import { describeAction } from "../../../../data/script_i18n"; import { describeAction } from "../../../../data/script_i18n";
import { callExecuteScript } from "../../../../data/service"; import { callExecuteScript } from "../../../../data/service";
@ -73,10 +74,10 @@ import "./types/ha-automation-action-delay";
import "./types/ha-automation-action-device_id"; import "./types/ha-automation-action-device_id";
import "./types/ha-automation-action-event"; import "./types/ha-automation-action-event";
import "./types/ha-automation-action-if"; import "./types/ha-automation-action-if";
import "./types/ha-automation-action-sequence";
import "./types/ha-automation-action-parallel"; import "./types/ha-automation-action-parallel";
import "./types/ha-automation-action-play_media"; import "./types/ha-automation-action-play_media";
import "./types/ha-automation-action-repeat"; import "./types/ha-automation-action-repeat";
import "./types/ha-automation-action-sequence";
import "./types/ha-automation-action-service"; import "./types/ha-automation-action-service";
import "./types/ha-automation-action-set_conversation_response"; import "./types/ha-automation-action-set_conversation_response";
import "./types/ha-automation-action-stop"; import "./types/ha-automation-action-stop";
@ -564,7 +565,9 @@ export default class HaAutomationActionRow extends LitElement {
if (!ev.detail.isValid) { if (!ev.detail.isValid) {
return; return;
} }
fireEvent(this, "value-changed", { value: ev.detail.value }); fireEvent(this, "value-changed", {
value: migrateAutomationAction(ev.detail.value),
});
} }
private _onUiChanged(ev: CustomEvent) { private _onUiChanged(ev: CustomEvent) {

View File

@ -19,7 +19,7 @@ import "../../../../components/ha-sortable";
import "../../../../components/ha-svg-icon"; import "../../../../components/ha-svg-icon";
import { getService, isService } from "../../../../data/action"; import { getService, isService } from "../../../../data/action";
import type { AutomationClipboard } from "../../../../data/automation"; import type { AutomationClipboard } from "../../../../data/automation";
import { Action, migrateAutomationAction } from "../../../../data/script"; import { Action } from "../../../../data/script";
import { HomeAssistant, ItemPath } from "../../../../types"; import { HomeAssistant, ItemPath } from "../../../../types";
import { import {
PASTE_VALUE, PASTE_VALUE,
@ -243,10 +243,7 @@ export default class HaAutomationAction extends LitElement {
private _actionChanged(ev: CustomEvent) { private _actionChanged(ev: CustomEvent) {
ev.stopPropagation(); ev.stopPropagation();
const actions = [...this.actions]; const actions = [...this.actions];
const newValue = const newValue = ev.detail.value;
ev.detail.value === null
? ev.detail.value
: (migrateAutomationAction(ev.detail.value) as Action);
const index = (ev.target as any).index; const index = (ev.target as any).index;
if (newValue === null) { if (newValue === null) {

View File

@ -46,12 +46,12 @@ import {
fetchAutomationFileConfig, fetchAutomationFileConfig,
getAutomationEditorInitData, getAutomationEditorInitData,
getAutomationStateConfig, getAutomationStateConfig,
migrateAutomationConfig,
normalizeAutomationConfig, normalizeAutomationConfig,
saveAutomationConfig, saveAutomationConfig,
showAutomationEditor, showAutomationEditor,
triggerAutomationActions, triggerAutomationActions,
} from "../../../data/automation"; } from "../../../data/automation";
import { substituteBlueprint } from "../../../data/blueprint";
import { validateConfig } from "../../../data/config"; import { validateConfig } from "../../../data/config";
import { UNAVAILABLE } from "../../../data/entity"; import { UNAVAILABLE } from "../../../data/entity";
import { fetchEntityRegistry } from "../../../data/entity_registry"; import { fetchEntityRegistry } from "../../../data/entity_registry";
@ -69,7 +69,6 @@ import { showAutomationModeDialog } from "./automation-mode-dialog/show-dialog-a
import { showAutomationRenameDialog } from "./automation-rename-dialog/show-dialog-automation-rename"; import { showAutomationRenameDialog } from "./automation-rename-dialog/show-dialog-automation-rename";
import "./blueprint-automation-editor"; import "./blueprint-automation-editor";
import "./manual-automation-editor"; import "./manual-automation-editor";
import { substituteBlueprint } from "../../../data/blueprint";
declare global { declare global {
interface HTMLElementTagNameMap { interface HTMLElementTagNameMap {
@ -640,7 +639,7 @@ export class HaAutomationEditor extends KeyboardShortcutMixin(LitElement) {
} }
this._config = { this._config = {
id: this._config?.id, id: this._config?.id,
...migrateAutomationConfig(ev.detail.value), ...normalizeAutomationConfig(ev.detail.value),
}; };
this._errors = undefined; this._errors = undefined;
this._dirty = true; this._dirty = true;

View File

@ -1,7 +1,7 @@
import { object, optional, number, string, boolean } from "superstruct"; import { object, optional, number, string, boolean } from "superstruct";
export const baseTriggerStruct = object({ export const baseTriggerStruct = object({
platform: string(), trigger: string(),
id: optional(string()), id: optional(string()),
enabled: optional(boolean()), enabled: optional(boolean()),
}); });

View File

@ -41,7 +41,11 @@ import "../../../../components/ha-icon-button";
import "../../../../components/ha-textfield"; import "../../../../components/ha-textfield";
import { HaYamlEditor } from "../../../../components/ha-yaml-editor"; import { HaYamlEditor } from "../../../../components/ha-yaml-editor";
import type { AutomationClipboard } from "../../../../data/automation"; import type { AutomationClipboard } from "../../../../data/automation";
import { Trigger, subscribeTrigger } from "../../../../data/automation"; import {
Trigger,
migrateAutomationTrigger,
subscribeTrigger,
} from "../../../../data/automation";
import { describeTrigger } from "../../../../data/automation_i18n"; import { describeTrigger } from "../../../../data/automation_i18n";
import { validateConfig } from "../../../../data/config"; import { validateConfig } from "../../../../data/config";
import { fullEntitiesContext } from "../../../../data/context"; import { fullEntitiesContext } from "../../../../data/context";
@ -71,6 +75,7 @@ import "./types/ha-automation-trigger-time";
import "./types/ha-automation-trigger-time_pattern"; import "./types/ha-automation-trigger-time_pattern";
import "./types/ha-automation-trigger-webhook"; import "./types/ha-automation-trigger-webhook";
import "./types/ha-automation-trigger-zone"; import "./types/ha-automation-trigger-zone";
import { preventDefault } from "../../../../common/dom/prevent_default";
export interface TriggerElement extends LitElement { export interface TriggerElement extends LitElement {
trigger: Trigger; trigger: Trigger;
@ -98,8 +103,6 @@ export const handleChangeEvent = (element: TriggerElement, ev: CustomEvent) => {
fireEvent(element, "value-changed", { value: newTrigger }); fireEvent(element, "value-changed", { value: newTrigger });
}; };
const preventDefault = (ev) => ev.preventDefault();
@customElement("ha-automation-trigger-row") @customElement("ha-automation-trigger-row")
export default class HaAutomationTriggerRow extends LitElement { export default class HaAutomationTriggerRow extends LitElement {
@property({ attribute: false }) public hass!: HomeAssistant; @property({ attribute: false }) public hass!: HomeAssistant;
@ -144,7 +147,7 @@ export default class HaAutomationTriggerRow extends LitElement {
if (!this.trigger) return nothing; if (!this.trigger) return nothing;
const supported = const supported =
customElements.get(`ha-automation-trigger-${this.trigger.platform}`) !== customElements.get(`ha-automation-trigger-${this.trigger.trigger}`) !==
undefined; undefined;
const yamlMode = this._yamlMode || !supported; const yamlMode = this._yamlMode || !supported;
const showId = "id" in this.trigger || this._requestShowId; const showId = "id" in this.trigger || this._requestShowId;
@ -165,7 +168,7 @@ export default class HaAutomationTriggerRow extends LitElement {
<h3 slot="header"> <h3 slot="header">
<ha-svg-icon <ha-svg-icon
class="trigger-icon" class="trigger-icon"
.path=${TRIGGER_ICONS[this.trigger.platform]} .path=${TRIGGER_ICONS[this.trigger.trigger]}
></ha-svg-icon> ></ha-svg-icon>
${describeTrigger(this.trigger, this.hass, this._entityReg)} ${describeTrigger(this.trigger, this.hass, this._entityReg)}
</h3> </h3>
@ -333,7 +336,7 @@ export default class HaAutomationTriggerRow extends LitElement {
? html` ? html`
${this.hass.localize( ${this.hass.localize(
"ui.panel.config.automation.editor.triggers.unsupported_platform", "ui.panel.config.automation.editor.triggers.unsupported_platform",
{ platform: this.trigger.platform } { platform: this.trigger.trigger }
)} )}
` `
: ""} : ""}
@ -363,7 +366,7 @@ export default class HaAutomationTriggerRow extends LitElement {
@value-changed=${this._onUiChanged} @value-changed=${this._onUiChanged}
> >
${dynamicElement( ${dynamicElement(
`ha-automation-trigger-${this.trigger.platform}`, `ha-automation-trigger-${this.trigger.trigger}`,
{ {
hass: this.hass, hass: this.hass,
trigger: this.trigger, trigger: this.trigger,
@ -574,7 +577,9 @@ export default class HaAutomationTriggerRow extends LitElement {
return; return;
} }
this._warnings = undefined; this._warnings = undefined;
fireEvent(this, "value-changed", { value: ev.detail.value }); fireEvent(this, "value-changed", {
value: migrateAutomationTrigger(ev.detail.value),
});
} }
private _onUiChanged(ev: CustomEvent) { private _onUiChanged(ev: CustomEvent) {

View File

@ -130,7 +130,7 @@ export default class HaAutomationTrigger extends LitElement {
showAddAutomationElementDialog(this, { showAddAutomationElementDialog(this, {
type: "trigger", type: "trigger",
add: this._addTrigger, add: this._addTrigger,
clipboardItem: this._clipboard?.trigger?.platform, clipboardItem: this._clipboard?.trigger?.trigger,
}); });
} }
@ -139,9 +139,9 @@ export default class HaAutomationTrigger extends LitElement {
if (value === PASTE_VALUE) { if (value === PASTE_VALUE) {
triggers = this.triggers.concat(deepClone(this._clipboard!.trigger)); triggers = this.triggers.concat(deepClone(this._clipboard!.trigger));
} else { } else {
const platform = value as Trigger["platform"]; const trigger = value as Trigger["trigger"];
const elClass = customElements.get( const elClass = customElements.get(
`ha-automation-trigger-${platform}` `ha-automation-trigger-${trigger}`
) as CustomElementConstructor & { ) as CustomElementConstructor & {
defaultConfig: Trigger; defaultConfig: Trigger;
}; };

View File

@ -71,7 +71,7 @@ export class HaCalendarTrigger extends LitElement implements TriggerElement {
public static get defaultConfig(): CalendarTrigger { public static get defaultConfig(): CalendarTrigger {
return { return {
platform: "calendar", trigger: "calendar",
entity_id: "", entity_id: "",
event: "start" as CalendarTrigger["event"], event: "start" as CalendarTrigger["event"],
offset: "0", offset: "0",

View File

@ -26,7 +26,7 @@ export class HaConversationTrigger
@query("#option_input", true) private _optionInput?: HaTextField; @query("#option_input", true) private _optionInput?: HaTextField;
public static get defaultConfig(): ConversationTrigger { public static get defaultConfig(): ConversationTrigger {
return { platform: "conversation", command: "" }; return { trigger: "conversation", command: "" };
} }
protected render() { protected render() {

View File

@ -40,7 +40,7 @@ export class HaDeviceTrigger extends LitElement {
public static get defaultConfig(): DeviceTrigger { public static get defaultConfig(): DeviceTrigger {
return { return {
platform: "device", trigger: "device",
device_id: "", device_id: "",
domain: "", domain: "",
entity_id: "", entity_id: "",
@ -155,7 +155,7 @@ export class HaDeviceTrigger extends LitElement {
this._deviceId = ev.target.value; this._deviceId = ev.target.value;
if (this._deviceId === undefined) { if (this._deviceId === undefined) {
fireEvent(this, "value-changed", { fireEvent(this, "value-changed", {
value: { ...HaDeviceTrigger.defaultConfig, platform: "device" }, value: { ...HaDeviceTrigger.defaultConfig, trigger: "device" },
}); });
} }
} }

View File

@ -20,7 +20,7 @@ export class HaEventTrigger extends LitElement implements TriggerElement {
@property({ type: Boolean }) public disabled = false; @property({ type: Boolean }) public disabled = false;
public static get defaultConfig(): EventTrigger { public static get defaultConfig(): EventTrigger {
return { platform: "event", event_type: "" }; return { trigger: "event", event_type: "" };
} }
protected render() { protected render() {

View File

@ -45,7 +45,7 @@ export class HaGeolocationTrigger extends LitElement {
public static get defaultConfig(): GeoLocationTrigger { public static get defaultConfig(): GeoLocationTrigger {
return { return {
platform: "geo_location", trigger: "geo_location",
source: "", source: "",
zone: "", zone: "",
event: "enter" as GeoLocationTrigger["event"], event: "enter" as GeoLocationTrigger["event"],

View File

@ -43,7 +43,7 @@ export class HaHassTrigger extends LitElement {
public static get defaultConfig(): HassTrigger { public static get defaultConfig(): HassTrigger {
return { return {
platform: "homeassistant", trigger: "homeassistant",
event: "start" as HassTrigger["event"], event: "start" as HassTrigger["event"],
}; };
} }

View File

@ -21,7 +21,7 @@ export class HaMQTTTrigger extends LitElement implements TriggerElement {
@property({ type: Boolean }) public disabled = false; @property({ type: Boolean }) public disabled = false;
public static get defaultConfig(): MqttTrigger { public static get defaultConfig(): MqttTrigger {
return { platform: "mqtt", topic: "" }; return { trigger: "mqtt", topic: "" };
} }
protected render() { protected render() {

View File

@ -239,7 +239,7 @@ export class HaNumericStateTrigger extends LitElement {
public static get defaultConfig(): NumericStateTrigger { public static get defaultConfig(): NumericStateTrigger {
return { return {
platform: "numeric_state", trigger: "numeric_state",
entity_id: [], entity_id: [],
}; };
} }

View File

@ -72,7 +72,7 @@ export class HaPersistentNotificationTrigger
public static get defaultConfig(): PersistentNotificationTrigger { public static get defaultConfig(): PersistentNotificationTrigger {
return { return {
platform: "persistent_notification", trigger: "persistent_notification",
update_type: [...DEFAULT_UPDATE_TYPES], update_type: [...DEFAULT_UPDATE_TYPES],
notification_id: DEFAULT_NOTIFICATION_ID, notification_id: DEFAULT_NOTIFICATION_ID,
}; };

View File

@ -29,7 +29,7 @@ const stateTriggerStruct = assign(
baseTriggerStruct, baseTriggerStruct,
object({ object({
alias: optional(string()), alias: optional(string()),
platform: literal("state"), trigger: literal("state"),
entity_id: optional(union([string(), array(string())])), entity_id: optional(union([string(), array(string())])),
attribute: optional(string()), attribute: optional(string()),
from: optional(nullable(string())), from: optional(nullable(string())),
@ -49,7 +49,7 @@ export class HaStateTrigger extends LitElement implements TriggerElement {
@property({ type: Boolean }) public disabled = false; @property({ type: Boolean }) public disabled = false;
public static get defaultConfig(): StateTrigger { public static get defaultConfig(): StateTrigger {
return { platform: "state", entity_id: [] }; return { trigger: "state", entity_id: [] };
} }
private _schema = memoizeOne( private _schema = memoizeOne(

View File

@ -45,7 +45,7 @@ export class HaSunTrigger extends LitElement implements TriggerElement {
public static get defaultConfig(): SunTrigger { public static get defaultConfig(): SunTrigger {
return { return {
platform: "sun", trigger: "sun",
event: "sunrise" as SunTrigger["event"], event: "sunrise" as SunTrigger["event"],
offset: 0, offset: 0,
}; };

View File

@ -20,7 +20,7 @@ export class HaTagTrigger extends LitElement implements TriggerElement {
@state() private _tags?: Tag[]; @state() private _tags?: Tag[];
public static get defaultConfig(): TagTrigger { public static get defaultConfig(): TagTrigger {
return { platform: "tag", tag_id: "" }; return { trigger: "tag", tag_id: "" };
} }
protected firstUpdated(changedProperties: PropertyValues) { protected firstUpdated(changedProperties: PropertyValues) {

View File

@ -23,7 +23,7 @@ export class HaTemplateTrigger extends LitElement {
@property({ type: Boolean }) public disabled = false; @property({ type: Boolean }) public disabled = false;
public static get defaultConfig(): TemplateTrigger { public static get defaultConfig(): TemplateTrigger {
return { platform: "template", value_template: "" }; return { trigger: "template", value_template: "" };
} }
public willUpdate(changedProperties: PropertyValues) { public willUpdate(changedProperties: PropertyValues) {

View File

@ -20,7 +20,7 @@ export class HaTimeTrigger extends LitElement implements TriggerElement {
@state() private _inputMode?: boolean; @state() private _inputMode?: boolean;
public static get defaultConfig(): TimeTrigger { public static get defaultConfig(): TimeTrigger {
return { platform: "time", at: "" }; return { trigger: "time", at: "" };
} }
private _schema = memoizeOne( private _schema = memoizeOne(

View File

@ -22,7 +22,7 @@ export class HaTimePatternTrigger extends LitElement implements TriggerElement {
@property({ type: Boolean }) public disabled = false; @property({ type: Boolean }) public disabled = false;
public static get defaultConfig(): TimePatternTrigger { public static get defaultConfig(): TimePatternTrigger {
return { platform: "time_pattern" }; return { trigger: "time_pattern" };
} }
protected render() { protected render() {

View File

@ -38,7 +38,7 @@ export class HaWebhookTrigger extends LitElement {
public static get defaultConfig(): WebhookTrigger { public static get defaultConfig(): WebhookTrigger {
return { return {
platform: "webhook", trigger: "webhook",
allowed_methods: [...DEFAULT_METHODS], allowed_methods: [...DEFAULT_METHODS],
local_only: true, local_only: true,
webhook_id: DEFAULT_WEBHOOK_ID, webhook_id: DEFAULT_WEBHOOK_ID,

View File

@ -25,7 +25,7 @@ export class HaZoneTrigger extends LitElement {
public static get defaultConfig(): ZoneTrigger { public static get defaultConfig(): ZoneTrigger {
return { return {
platform: "zone", trigger: "zone",
entity_id: "", entity_id: "",
zone: "", zone: "",
event: "enter" as ZoneTrigger["event"], event: "enter" as ZoneTrigger["event"],

View File

@ -213,7 +213,7 @@ export class HaConfigTags extends SubscribeMixin(LitElement) {
alias: this.hass.localize("ui.panel.config.tag.automation_title", { alias: this.hass.localize("ui.panel.config.tag.automation_title", {
name: tag.name || tag.id, name: tag.name || tag.id,
}), }),
trigger: [{ platform: "tag", tag_id: tag.id } as TagTrigger], trigger: [{ trigger: "tag", tag_id: tag.id } as TagTrigger],
}; };
showAutomationEditor(data); showAutomationEditor(data);
}; };