From 36d30266e30cff522a6c7efd6f4880ac831e9656 Mon Sep 17 00:00:00 2001 From: Allen Porter Date: Fri, 22 Apr 2022 14:53:45 -0700 Subject: [PATCH] Add automation editor for calendar trigger (#12343) --- src/data/automation.ts | 9 ++- .../trigger/ha-automation-trigger-row.ts | 2 + .../types/ha-automation-trigger-calendar.ts | 73 +++++++++++++++++++ src/translations/en.json | 5 ++ 4 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 src/panels/config/automation/trigger/types/ha-automation-trigger-calendar.ts diff --git a/src/data/automation.ts b/src/data/automation.ts index dda0711a2f..23158f6566 100644 --- a/src/data/automation.ts +++ b/src/data/automation.ts @@ -152,6 +152,12 @@ export interface EventTrigger extends BaseTrigger { context?: ContextConstraint; } +export interface CalendarTrigger extends BaseTrigger { + platform: "calendar"; + event: "start"; + entity_id: string; +} + export type Trigger = | StateTrigger | MqttTrigger @@ -166,7 +172,8 @@ export type Trigger = | TimeTrigger | TemplateTrigger | EventTrigger - | DeviceTrigger; + | DeviceTrigger + | CalendarTrigger; interface BaseCondition { condition: string; diff --git a/src/panels/config/automation/trigger/ha-automation-trigger-row.ts b/src/panels/config/automation/trigger/ha-automation-trigger-row.ts index b10e3a63ee..cb821f6758 100644 --- a/src/panels/config/automation/trigger/ha-automation-trigger-row.ts +++ b/src/panels/config/automation/trigger/ha-automation-trigger-row.ts @@ -28,6 +28,7 @@ import { } from "../../../../dialogs/generic/show-dialog-box"; import { haStyle } from "../../../../resources/styles"; import type { HomeAssistant } from "../../../../types"; +import "./types/ha-automation-trigger-calendar"; import "./types/ha-automation-trigger-device"; import "./types/ha-automation-trigger-event"; import "./types/ha-automation-trigger-geo_location"; @@ -44,6 +45,7 @@ import "./types/ha-automation-trigger-webhook"; import "./types/ha-automation-trigger-zone"; const OPTIONS = [ + "calendar", "device", "event", "state", diff --git a/src/panels/config/automation/trigger/types/ha-automation-trigger-calendar.ts b/src/panels/config/automation/trigger/types/ha-automation-trigger-calendar.ts new file mode 100644 index 0000000000..069e88794e --- /dev/null +++ b/src/panels/config/automation/trigger/types/ha-automation-trigger-calendar.ts @@ -0,0 +1,73 @@ +import { html, LitElement } from "lit"; +import { customElement, property } from "lit/decorators"; +import memoizeOne from "memoize-one"; +import { fireEvent } from "../../../../../common/dom/fire_event"; +import type { CalendarTrigger } from "../../../../../data/automation"; +import type { HomeAssistant } from "../../../../../types"; +import type { TriggerElement } from "../ha-automation-trigger-row"; +import type { HaFormSchema } from "../../../../../components/ha-form/types"; +import type { LocalizeFunc } from "../../../../../common/translations/localize"; + +@customElement("ha-automation-trigger-calendar") +export class HaCalendarTrigger extends LitElement implements TriggerElement { + @property({ attribute: false }) public hass!: HomeAssistant; + + @property({ attribute: false }) public trigger!: CalendarTrigger; + + private _schema = memoizeOne((localize: LocalizeFunc) => [ + { + name: "entity_id", + required: true, + selector: { entity: { domain: "calendar" } }, + }, + { + name: "event", + type: "select", + required: true, + options: [ + [ + "start", + localize( + "ui.panel.config.automation.editor.triggers.type.calendar.start" + ), + ], + ], + }, + ]); + + public static get defaultConfig() { + return { + event: "start" as CalendarTrigger["event"], + }; + } + + protected render() { + const schema = this._schema(this.hass.localize); + return html` + + `; + } + + private _valueChanged(ev: CustomEvent): void { + ev.stopPropagation(); + const newTrigger = ev.detail.value; + fireEvent(this, "value-changed", { value: newTrigger }); + } + + private _computeLabelCallback = (schema: HaFormSchema): string => + this.hass.localize( + `ui.panel.config.automation.editor.triggers.type.calendar.${schema.name}` + ); +} + +declare global { + interface HTMLElementTagNameMap { + "ha-automation-trigger-calendar": HaCalendarTrigger; + } +} diff --git a/src/translations/en.json b/src/translations/en.json index e0ffddcfb8..f7c083cdf7 100755 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -1751,6 +1751,11 @@ "unsupported_platform": "No visual editor support for platform: {platform}", "type_select": "Trigger type", "type": { + "calendar": { + "label": "Calendar", + "event": "[%key:ui::panel::config::automation::editor::triggers::type::homeassistant::event%]", + "start": "Event Start" + }, "device": { "label": "Device", "trigger": "Trigger",