From 1faef71dcb3dfb06022bebc0b1e7bc0cc16bcca1 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 21 Jun 2023 10:25:53 +0200 Subject: [PATCH] tweak --- ...omation-trigger-persistent_notification.ts | 86 +++++++++++-------- 1 file changed, 49 insertions(+), 37 deletions(-) diff --git a/src/panels/config/automation/trigger/types/ha-automation-trigger-persistent_notification.ts b/src/panels/config/automation/trigger/types/ha-automation-trigger-persistent_notification.ts index 3243f50616..c36fc1a220 100644 --- a/src/panels/config/automation/trigger/types/ha-automation-trigger-persistent_notification.ts +++ b/src/panels/config/automation/trigger/types/ha-automation-trigger-persistent_notification.ts @@ -1,4 +1,6 @@ import type { RequestSelectedDetail } from "@material/mwc-list/mwc-list-item"; +import memoizeOne from "memoize-one"; + import { css, html, LitElement } from "lit"; import { customElement, property } from "lit/decorators"; import { fireEvent } from "../../../../../common/dom/fire_event"; @@ -9,19 +11,49 @@ import "../../../../../components/ha-textfield"; import { PersistentNotificationTrigger } from "../../../../../data/automation"; import { HomeAssistant } from "../../../../../types"; import { handleChangeEvent } from "../ha-automation-trigger-row"; +import type { TriggerElement } from "../ha-automation-trigger-row"; +import type { LocalizeFunc } from "../../../../../common/translations/localize"; +import type { SchemaUnion } from "../../../../../components/ha-form/types"; const SUPPORTED_UPDATE_TYPES = ["added", "removed", "current", "updated"]; const DEFAULT_UPDATE_TYPES = ["added", "removed"]; const DEFAULT_NOTIFICATION_ID = ""; @customElement("ha-automation-trigger-persistent_notification") -export class HaPersistentNotificationTrigger extends LitElement { +export class HaPersistentNotificationTrigger + extends LitElement + implements TriggerElement +{ @property({ attribute: false }) public hass!: HomeAssistant; @property() public trigger!: PersistentNotificationTrigger; @property({ type: Boolean }) public disabled = false; + private _schema = memoizeOne( + (localize: LocalizeFunc) => + [ + { + name: "notification_id", + required: false, + selector: { text: {} }, + }, + { + name: "update_type", + type: "multi_select", + required: true, + options: [ + SUPPORTED_UPDATE_TYPES.map((update_type) => [ + update_type, + localize( + `ui.panel.config.automation.editor.triggers.type.persistent_notification.update_types.${update_type}` + ), + ]), + ], + }, + ] as const + ); + public static get defaultConfig() { return { update_type: [...DEFAULT_UPDATE_TYPES], @@ -30,43 +62,16 @@ export class HaPersistentNotificationTrigger extends LitElement { } protected render() { - const { update_type: updateTypes, notification_id: notificationId } = - this.trigger; - + const schema = this._schema(this.hass.localize); return html` -
- - - ${SUPPORTED_UPDATE_TYPES.map( - (update_type) => html` - - ${this.hass.localize( - `ui.panel.config.automation.editor.triggers.type.persistent_notification.update_types.${update_type}` - )} - - -
- ` - )} - - + `; } @@ -74,6 +79,13 @@ export class HaPersistentNotificationTrigger extends LitElement { handleChangeEvent(this, ev); } + private _computeLabelCallback = ( + schema: SchemaUnion> + ): string => + this.hass.localize( + `ui.panel.config.automation.editor.triggers.type.persistent_notification.${schema.name}` + ); + private _updateTypeChanged(ev: CustomEvent): void { ev.stopPropagation(); const updateType = (ev.target as any).value;