From ed84ce9692439a5f7420b4d63268d6a0c0cdbe4b Mon Sep 17 00:00:00 2001 From: Zack Barett Date: Thu, 10 Feb 2022 16:12:12 -0600 Subject: [PATCH] HA Trigger to HA Form (#11645) Co-authored-by: Paulus Schoutsen --- .../ha-automation-trigger-homeassistant.ts | 81 ++++++++++--------- 1 file changed, 41 insertions(+), 40 deletions(-) diff --git a/src/panels/config/automation/trigger/types/ha-automation-trigger-homeassistant.ts b/src/panels/config/automation/trigger/types/ha-automation-trigger-homeassistant.ts index c660b8f132..fceb9bd515 100644 --- a/src/panels/config/automation/trigger/types/ha-automation-trigger-homeassistant.ts +++ b/src/panels/config/automation/trigger/types/ha-automation-trigger-homeassistant.ts @@ -1,11 +1,12 @@ +import "../../../../../components/ha-form/ha-form"; import { css, html, LitElement } from "lit"; import { customElement, property } from "lit/decorators"; +import memoizeOne from "memoize-one"; import { fireEvent } from "../../../../../common/dom/fire_event"; -import type { HaRadio } from "../../../../../components/ha-radio"; import type { HassTrigger } from "../../../../../data/automation"; import type { HomeAssistant } from "../../../../../types"; -import "../../../../../components/ha-formfield"; -import "../../../../../components/ha-radio"; +import type { HaFormSchema } from "../../../../../components/ha-form/types"; +import type { LocalizeFunc } from "../../../../../common/translations/localize"; @customElement("ha-automation-trigger-homeassistant") export class HaHassTrigger extends LitElement { @@ -13,6 +14,28 @@ export class HaHassTrigger extends LitElement { @property({ attribute: false }) public trigger!: HassTrigger; + private _schema = memoizeOne((localize: LocalizeFunc) => [ + { + name: "event", + type: "select", + required: true, + options: [ + [ + "start", + localize( + "ui.panel.config.automation.editor.triggers.type.homeassistant.start" + ), + ], + [ + "shutdown", + localize( + "ui.panel.config.automation.editor.triggers.type.homeassistant.shutdown" + ), + ], + ], + }, + ]); + public static get defaultConfig() { return { event: "start" as HassTrigger["event"], @@ -20,50 +43,28 @@ export class HaHassTrigger extends LitElement { } protected render() { - const { event } = this.trigger; return html` - + `; } - private _radioGroupPicked(ev) { + private _valueChanged(ev: CustomEvent): void { ev.stopPropagation(); - fireEvent(this, "value-changed", { - value: { - ...this.trigger, - event: (ev.target as HaRadio).value, - }, - }); + 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.geo_location.${schema.name}` + ); + static styles = css` label { display: flex;