This commit is contained in:
J. Nick Koston 2023-06-21 10:25:53 +02:00
parent 1e5c35c158
commit 1faef71dcb
No known key found for this signature in database

View File

@ -1,4 +1,6 @@
import type { RequestSelectedDetail } from "@material/mwc-list/mwc-list-item"; import type { RequestSelectedDetail } from "@material/mwc-list/mwc-list-item";
import memoizeOne from "memoize-one";
import { css, html, LitElement } from "lit"; import { css, html, LitElement } from "lit";
import { customElement, property } from "lit/decorators"; import { customElement, property } from "lit/decorators";
import { fireEvent } from "../../../../../common/dom/fire_event"; import { fireEvent } from "../../../../../common/dom/fire_event";
@ -9,19 +11,49 @@ import "../../../../../components/ha-textfield";
import { PersistentNotificationTrigger } from "../../../../../data/automation"; import { PersistentNotificationTrigger } from "../../../../../data/automation";
import { HomeAssistant } from "../../../../../types"; import { HomeAssistant } from "../../../../../types";
import { handleChangeEvent } from "../ha-automation-trigger-row"; 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 SUPPORTED_UPDATE_TYPES = ["added", "removed", "current", "updated"];
const DEFAULT_UPDATE_TYPES = ["added", "removed"]; const DEFAULT_UPDATE_TYPES = ["added", "removed"];
const DEFAULT_NOTIFICATION_ID = ""; const DEFAULT_NOTIFICATION_ID = "";
@customElement("ha-automation-trigger-persistent_notification") @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({ attribute: false }) public hass!: HomeAssistant;
@property() public trigger!: PersistentNotificationTrigger; @property() public trigger!: PersistentNotificationTrigger;
@property({ type: Boolean }) public disabled = false; @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() { public static get defaultConfig() {
return { return {
update_type: [...DEFAULT_UPDATE_TYPES], update_type: [...DEFAULT_UPDATE_TYPES],
@ -30,43 +62,16 @@ export class HaPersistentNotificationTrigger extends LitElement {
} }
protected render() { protected render() {
const { update_type: updateTypes, notification_id: notificationId } = const schema = this._schema(this.hass.localize);
this.trigger;
return html` return html`
<div class="form"> <ha-form
<ha-textfield .schema=${schema}
.label=${this.hass.localize( .data=${this.trigger}
"ui.panel.config.automation.editor.triggers.type.persistent_notification.notification_id" .hass=${this.hass}
)} .disabled=${this.disabled}
name="notification_id" .computeLabel=${this._computeLabelCallback}
.value=${notificationId} @value-changed=${this._valueChanged}
.disabled=${this.disabled} ></ha-form>
@change=${this._valueChanged}
></ha-textfield>
<ha-formfield
.label=${this.hass.localize(
"ui.panel.config.automation.editor.triggers.type.persistent_notification.update_type"
)}
>
${SUPPORTED_UPDATE_TYPES.map(
(update_type) => html`
<ha-check-list-item
left
.value=${update_type}
@request-selected=${this._updateTypeChanged}
.selected=${updateTypes!.includes(update_type)}
>
${this.hass.localize(
`ui.panel.config.automation.editor.triggers.type.persistent_notification.update_types.${update_type}`
)}
</ha-check-list-item>
</ha-formfield>
</div>
`
)}
</ha-formfield>
</div>
`; `;
} }
@ -74,6 +79,13 @@ export class HaPersistentNotificationTrigger extends LitElement {
handleChangeEvent(this, ev); handleChangeEvent(this, ev);
} }
private _computeLabelCallback = (
schema: SchemaUnion<ReturnType<typeof this._schema>>
): string =>
this.hass.localize(
`ui.panel.config.automation.editor.triggers.type.persistent_notification.${schema.name}`
);
private _updateTypeChanged(ev: CustomEvent<RequestSelectedDetail>): void { private _updateTypeChanged(ev: CustomEvent<RequestSelectedDetail>): void {
ev.stopPropagation(); ev.stopPropagation();
const updateType = (ev.target as any).value; const updateType = (ev.target as any).value;