From 50a44afb9e926e76ecbee10037c6e92193fa2284 Mon Sep 17 00:00:00 2001 From: Zack Date: Thu, 10 Feb 2022 13:17:50 -0600 Subject: [PATCH 1/6] Conversion of Ha trigger --- .../ha-automation-trigger-homeassistant.ts | 95 +++++++++++-------- 1 file changed, 54 insertions(+), 41 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 c660b8f13260..0653bda42c4e 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,10 @@ +import "../../../../../components/ha-form/ha-form"; import { css, html, LitElement } from "lit"; -import { customElement, property } from "lit/decorators"; +import { customElement, property, state } from "lit/decorators"; 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 { HaFormSchema } from "../../../../../components/ha-form/types"; @customElement("ha-automation-trigger-homeassistant") export class HaHassTrigger extends LitElement { @@ -13,55 +12,69 @@ export class HaHassTrigger extends LitElement { @property({ attribute: false }) public trigger!: HassTrigger; + @state() private _schema?: HaFormSchema[]; + public static get defaultConfig() { return { event: "start" as HassTrigger["event"], }; } + protected firstUpdated(): void { + if (!this.hass) { + return; + } + + this._schema = [ + { + name: "event", + type: "select", + required: true, + options: [ + [ + "start", + this.hass.localize( + "ui.panel.config.automation.editor.triggers.type.homeassistant.start" + ), + ], + [ + "shutdown", + this.hass.localize( + "ui.panel.config.automation.editor.triggers.type.homeassistant.shutdown" + ), + ], + ], + }, + ]; + } + 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; + Object.keys(newTrigger).forEach((key) => + newTrigger[key] === undefined || newTrigger[key] === "" + ? delete newTrigger[key] + : {} + ); + fireEvent(this, "value-changed", { value: newTrigger }); + } + + private _computeLabelCallback(schema: HaFormSchema): string { + return this.hass.localize( + `ui.panel.config.automation.editor.triggers.type.geo_location.${schema.name}` + ); } static styles = css` From 4cad56e06e893c38d6b573c6dec83843dfee00f3 Mon Sep 17 00:00:00 2001 From: Zack Date: Thu, 10 Feb 2022 15:13:50 -0600 Subject: [PATCH 2/6] review --- .../ha-automation-trigger-homeassistant.ts | 61 ++++++++----------- 1 file changed, 25 insertions(+), 36 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 0653bda42c4e..aeef8e996b4a 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,10 +1,12 @@ import "../../../../../components/ha-form/ha-form"; import { css, html, LitElement } from "lit"; -import { customElement, property, state } from "lit/decorators"; +import { customElement, property } from "lit/decorators"; +import memoizeOne from "memoize-one"; import { fireEvent } from "../../../../../common/dom/fire_event"; import type { HassTrigger } from "../../../../../data/automation"; import type { HomeAssistant } from "../../../../../types"; import { HaFormSchema } from "../../../../../components/ha-form/types"; +import { LocalizeFunc } from "../../../../../common/translations/localize"; @customElement("ha-automation-trigger-homeassistant") export class HaHassTrigger extends LitElement { @@ -12,7 +14,27 @@ export class HaHassTrigger extends LitElement { @property({ attribute: false }) public trigger!: HassTrigger; - @state() private _schema?: HaFormSchema[]; + 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 { @@ -20,38 +42,10 @@ export class HaHassTrigger extends LitElement { }; } - protected firstUpdated(): void { - if (!this.hass) { - return; - } - - this._schema = [ - { - name: "event", - type: "select", - required: true, - options: [ - [ - "start", - this.hass.localize( - "ui.panel.config.automation.editor.triggers.type.homeassistant.start" - ), - ], - [ - "shutdown", - this.hass.localize( - "ui.panel.config.automation.editor.triggers.type.homeassistant.shutdown" - ), - ], - ], - }, - ]; - } - protected render() { return html` - newTrigger[key] === undefined || newTrigger[key] === "" - ? delete newTrigger[key] - : {} - ); fireEvent(this, "value-changed", { value: newTrigger }); } From 2dd088f0a923519088b6f4a6f4c677d0b0eee9f0 Mon Sep 17 00:00:00 2001 From: Zack Date: Thu, 10 Feb 2022 15:14:33 -0600 Subject: [PATCH 3/6] review --- .../trigger/types/ha-automation-trigger-homeassistant.ts | 4 +--- 1 file changed, 1 insertion(+), 3 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 aeef8e996b4a..31f0688757fc 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 @@ -60,11 +60,9 @@ export class HaHassTrigger extends LitElement { fireEvent(this, "value-changed", { value: newTrigger }); } - private _computeLabelCallback(schema: HaFormSchema): string { - return this.hass.localize( + private _computeLabelCallback = (schema: HaFormSchema): string => this.hass.localize( `ui.panel.config.automation.editor.triggers.type.geo_location.${schema.name}` ); - } static styles = css` label { From fcedf93ba25ee35e903eb86c5c42d8ce1442208e Mon Sep 17 00:00:00 2001 From: Zack Barett Date: Thu, 10 Feb 2022 15:29:36 -0600 Subject: [PATCH 4/6] Update src/panels/config/automation/trigger/types/ha-automation-trigger-homeassistant.ts Co-authored-by: Paulus Schoutsen --- .../trigger/types/ha-automation-trigger-homeassistant.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 31f0688757fc..c236373790f1 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 @@ -6,7 +6,7 @@ import { fireEvent } from "../../../../../common/dom/fire_event"; import type { HassTrigger } from "../../../../../data/automation"; import type { HomeAssistant } from "../../../../../types"; import { HaFormSchema } from "../../../../../components/ha-form/types"; -import { LocalizeFunc } from "../../../../../common/translations/localize"; +import type { LocalizeFunc } from "../../../../../common/translations/localize"; @customElement("ha-automation-trigger-homeassistant") export class HaHassTrigger extends LitElement { From a257249ca59cb19a408f2b99d397697554cbd610 Mon Sep 17 00:00:00 2001 From: Zack Barett Date: Thu, 10 Feb 2022 15:29:45 -0600 Subject: [PATCH 5/6] Update src/panels/config/automation/trigger/types/ha-automation-trigger-homeassistant.ts Co-authored-by: Paulus Schoutsen --- .../trigger/types/ha-automation-trigger-homeassistant.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 c236373790f1..175b2fb9e165 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 @@ -5,7 +5,7 @@ import memoizeOne from "memoize-one"; import { fireEvent } from "../../../../../common/dom/fire_event"; import type { HassTrigger } from "../../../../../data/automation"; import type { HomeAssistant } from "../../../../../types"; -import { HaFormSchema } from "../../../../../components/ha-form/types"; +import type { HaFormSchema } from "../../../../../components/ha-form/types"; import type { LocalizeFunc } from "../../../../../common/translations/localize"; @customElement("ha-automation-trigger-homeassistant") From c12ed629ecfc2f86a5c708c26b86337a01972fc2 Mon Sep 17 00:00:00 2001 From: Zack Date: Thu, 10 Feb 2022 15:34:46 -0600 Subject: [PATCH 6/6] prettier --- .../trigger/types/ha-automation-trigger-homeassistant.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 31f0688757fc..50f4532b26f7 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 @@ -60,7 +60,8 @@ export class HaHassTrigger extends LitElement { fireEvent(this, "value-changed", { value: newTrigger }); } - private _computeLabelCallback = (schema: HaFormSchema): string => this.hass.localize( + private _computeLabelCallback = (schema: HaFormSchema): string => + this.hass.localize( `ui.panel.config.automation.editor.triggers.type.geo_location.${schema.name}` );