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

Migrate trigger platform key to trigger #22054

Merged
merged 4 commits into from
Sep 25, 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
2 changes: 1 addition & 1 deletion gallery/src/data/traces/basic_trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ export const basicTrace: DemoTrace = {
description: "",
triggers: [
{
platform: "state",
trigger: "state",
entity_id: "input_boolean.toggle_1",
},
],
Expand Down
2 changes: 1 addition & 1 deletion gallery/src/data/traces/motion-light-trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export const motionLightTrace: DemoTrace = {
max_exceeded: "silent",
triggers: [
{
platform: "state",
trigger: "state",
entity_id: "binary_sensor.pauluss_macbook_pro_camera_in_use",
from: "off",
to: "on",
bramkragten marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
2 changes: 1 addition & 1 deletion gallery/src/pages/automation/describe-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const ACTIONS = [
{
wait_for_trigger: [
{
platform: "state",
trigger: "state",
entity_id: "input_boolean.toggle_1",
},
],
Expand Down
36 changes: 18 additions & 18 deletions gallery/src/pages/automation/describe-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,46 +22,46 @@ const ENTITIES = [
];

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

const initialTrigger: Trigger = {
platform: "state",
trigger: "state",
entity_id: "light.kitchen",
};

Expand Down
2 changes: 1 addition & 1 deletion gallery/src/pages/automation/editor-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ const SCHEMAS: { name: string; triggers: Trigger[] }[] = [
triggers: [
{ ...HaConversationTrigger.defaultConfig },
{
platform: "conversation",
trigger: "conversation",
command: ["Turn on the lights", "Turn the lights on"],
},
],
Expand Down
2 changes: 1 addition & 1 deletion src/components/device/ha-device-trigger-picker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class HaDeviceTriggerPicker extends HaDeviceAutomationPicker<DeviceTrigger> {
fetchDeviceTriggers,
(deviceId?: string) => ({
device_id: deviceId || "",
platform: "device",
trigger: "device",
domain: "",
entity_id: "",
})
Expand Down
12 changes: 10 additions & 2 deletions src/components/ha-selector/ha-selector-action.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { css, CSSResultGroup, html, LitElement, nothing } from "lit";
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 "../../panels/config/automation/action/ha-automation-action";
import { HomeAssistant } from "../../types";
Expand All @@ -17,12 +18,19 @@ export class HaActionSelector extends LitElement {

@property({ type: Boolean, reflect: true }) public disabled = false;

private _actions = memoizeOne((action: Action | undefined) => {
if (!action) {
return [];
}
return migrateAutomationAction(action);
});

protected render() {
return html`
${this.label ? html`<label>${this.label}</label>` : nothing}
<ha-automation-action
.disabled=${this.disabled}
.actions=${this.value || []}
.actions=${this._actions(this.value)}
.hass=${this.hass}
.path=${this.selector.action?.path}
></ha-automation-action>
Expand Down
7 changes: 1 addition & 6 deletions src/components/ha-selector/ha-selector-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@ import "../ha-code-editor";
import "../ha-input-helper-text";
import "../ha-alert";

const WARNING_STRINGS = [
"template:",
"sensor:",
"state:",
"platform: template",
];
const WARNING_STRINGS = ["template:", "sensor:", "state:", "trigger: template"];

@customElement("ha-selector-template")
export class HaTemplateSelector extends LitElement {
Expand Down
12 changes: 10 additions & 2 deletions src/components/ha-selector/ha-selector-trigger.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { css, CSSResultGroup, html, LitElement, nothing } from "lit";
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 "../../panels/config/automation/trigger/ha-automation-trigger";
import { HomeAssistant } from "../../types";
Expand All @@ -17,12 +18,19 @@ export class HaTriggerSelector extends LitElement {

@property({ type: Boolean, reflect: true }) public disabled = false;

private _triggers = memoizeOne((trigger: Trigger | undefined) => {
if (!trigger) {
return [];
}
return migrateAutomationTrigger(trigger);
});

protected render() {
return html`
${this.label ? html`<label>${this.label}</label>` : nothing}
<ha-automation-trigger
.disabled=${this.disabled}
.triggers=${this.value || []}
.triggers=${this._triggers(this.value)}
.hass=${this.hass}
.path=${this.selector.trigger?.path}
></ha-automation-trigger>
Expand Down
66 changes: 45 additions & 21 deletions src/data/automation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,16 @@ export interface TriggerList {

export interface BaseTrigger {
alias?: string;
platform: string;
/** @deprecated Use `trigger` instead */
platform?: string;
trigger: string;
id?: string;
variables?: Record<string, unknown>;
enabled?: boolean;
}

export interface StateTrigger extends BaseTrigger {
platform: "state";
trigger: "state";
entity_id: string | string[];
attribute?: string;
from?: string | string[];
Expand All @@ -91,25 +93,25 @@ export interface StateTrigger extends BaseTrigger {
}

export interface MqttTrigger extends BaseTrigger {
platform: "mqtt";
trigger: "mqtt";
topic: string;
payload?: string;
}

export interface GeoLocationTrigger extends BaseTrigger {
platform: "geo_location";
trigger: "geo_location";
source: string;
zone: string;
event: "enter" | "leave";
}

export interface HassTrigger extends BaseTrigger {
platform: "homeassistant";
trigger: "homeassistant";
event: "start" | "shutdown";
}

export interface NumericStateTrigger extends BaseTrigger {
platform: "numeric_state";
trigger: "numeric_state";
entity_id: string | string[];
attribute?: string;
above?: number;
Expand All @@ -119,69 +121,69 @@ export interface NumericStateTrigger extends BaseTrigger {
}

export interface ConversationTrigger extends BaseTrigger {
platform: "conversation";
trigger: "conversation";
command: string | string[];
}

export interface SunTrigger extends BaseTrigger {
platform: "sun";
trigger: "sun";
offset: number;
event: "sunrise" | "sunset";
}

export interface TimePatternTrigger extends BaseTrigger {
platform: "time_pattern";
trigger: "time_pattern";
hours?: number | string;
minutes?: number | string;
seconds?: number | string;
}

export interface WebhookTrigger extends BaseTrigger {
platform: "webhook";
trigger: "webhook";
webhook_id: string;
allowed_methods?: string[];
local_only?: boolean;
}

export interface PersistentNotificationTrigger extends BaseTrigger {
platform: "persistent_notification";
trigger: "persistent_notification";
notification_id?: string;
update_type?: string[];
}

export interface ZoneTrigger extends BaseTrigger {
platform: "zone";
trigger: "zone";
entity_id: string;
zone: string;
event: "enter" | "leave";
}

export interface TagTrigger extends BaseTrigger {
platform: "tag";
trigger: "tag";
tag_id: string;
device_id?: string;
}

export interface TimeTrigger extends BaseTrigger {
platform: "time";
trigger: "time";
at: string;
}

export interface TemplateTrigger extends BaseTrigger {
platform: "template";
trigger: "template";
value_template: string;
for?: string | number | ForDict;
}

export interface EventTrigger extends BaseTrigger {
platform: "event";
trigger: "event";
event_type: string;
event_data?: any;
context?: ContextConstraint;
}

export interface CalendarTrigger extends BaseTrigger {
platform: "calendar";
trigger: "calendar";
event: "start" | "end";
entity_id: string;
offset: string;
Expand Down Expand Up @@ -379,10 +381,6 @@ export const normalizeAutomationConfig = <
}
}

if (config.actions) {
config.actions = migrateAutomationAction(config.actions);
}

return config;
};

Expand All @@ -409,9 +407,35 @@ export const migrateAutomationConfig = <
}
delete config.action;
}

if (config.triggers) {
config.triggers = migrateAutomationTrigger(config.triggers);
}

if (config.actions) {
config.actions = migrateAutomationAction(config.actions);
}

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;
piitaya marked this conversation as resolved.
Show resolved Hide resolved
}
delete trigger.platform;
piitaya marked this conversation as resolved.
Show resolved Hide resolved
}
return trigger;
};

export const flattenTriggers = (
triggers: undefined | (Trigger | TriggerList)[]
): Trigger[] => {
Expand Down
Loading
Loading