From 9b35c06eefc6e9942aa9757fe9b550b9552a987c Mon Sep 17 00:00:00 2001 From: RoboMagus <68224306+RoboMagus@users.noreply.github.com> Date: Wed, 21 Jun 2023 11:55:55 +0200 Subject: [PATCH] Add persistent_notification trigger (#16967) Co-authored-by: J. Nick Koston --- .../src/pages/automation/describe-trigger.ts | 1 + .../src/pages/automation/editor-trigger.ts | 11 ++ src/data/automation.ts | 7 ++ src/data/automation_i18n.ts | 6 + src/data/trigger.ts | 2 + .../trigger/ha-automation-trigger-row.ts | 1 + .../trigger/ha-automation-trigger.ts | 1 + ...omation-trigger-persistent_notification.ts | 117 ++++++++++++++++++ src/translations/en.json | 11 ++ 9 files changed, 157 insertions(+) create mode 100644 src/panels/config/automation/trigger/types/ha-automation-trigger-persistent_notification.ts diff --git a/gallery/src/pages/automation/describe-trigger.ts b/gallery/src/pages/automation/describe-trigger.ts index 6ed8163912..3ebb26f017 100644 --- a/gallery/src/pages/automation/describe-trigger.ts +++ b/gallery/src/pages/automation/describe-trigger.ts @@ -41,6 +41,7 @@ const triggers = [ { platform: "sun", event: "sunset" }, { platform: "time_pattern" }, { platform: "webhook" }, + { platform: "persistent_notification" }, { platform: "zone", entity_id: "person.person", diff --git a/gallery/src/pages/automation/editor-trigger.ts b/gallery/src/pages/automation/editor-trigger.ts index bff2947bb3..5dccee4fa0 100644 --- a/gallery/src/pages/automation/editor-trigger.ts +++ b/gallery/src/pages/automation/editor-trigger.ts @@ -19,6 +19,7 @@ import { HaTemplateTrigger } from "../../../../src/panels/config/automation/trig import { HaTimeTrigger } from "../../../../src/panels/config/automation/trigger/types/ha-automation-trigger-time"; import { HaTimePatternTrigger } from "../../../../src/panels/config/automation/trigger/types/ha-automation-trigger-time_pattern"; import { HaWebhookTrigger } from "../../../../src/panels/config/automation/trigger/types/ha-automation-trigger-webhook"; +import { HaPersistentNotificationTrigger } from "../../../../src/panels/config/automation/trigger/types/ha-automation-trigger-persistent_notification"; import { HaZoneTrigger } from "../../../../src/panels/config/automation/trigger/types/ha-automation-trigger-zone"; import { HaDeviceTrigger } from "../../../../src/panels/config/automation/trigger/types/ha-automation-trigger-device"; import { HaStateTrigger } from "../../../../src/panels/config/automation/trigger/types/ha-automation-trigger-state"; @@ -72,6 +73,16 @@ const SCHEMAS: { name: string; triggers: Trigger[] }[] = [ triggers: [{ platform: "webhook", ...HaWebhookTrigger.defaultConfig }], }, + { + name: "Persistent Notification", + triggers: [ + { + platform: "persistent_notification", + ...HaPersistentNotificationTrigger.defaultConfig, + }, + ], + }, + { name: "Zone", triggers: [{ platform: "zone", ...HaZoneTrigger.defaultConfig }], diff --git a/src/data/automation.ts b/src/data/automation.ts index aeeaa356d2..6ff6eca45f 100644 --- a/src/data/automation.ts +++ b/src/data/automation.ts @@ -127,6 +127,12 @@ export interface WebhookTrigger extends BaseTrigger { local_only?: boolean; } +export interface PersistentNotificationTrigger extends BaseTrigger { + platform: "persistent_notification"; + notification_id?: string; + update_type?: string[]; +} + export interface ZoneTrigger extends BaseTrigger { platform: "zone"; entity_id: string; @@ -174,6 +180,7 @@ export type Trigger = | SunTrigger | TimePatternTrigger | WebhookTrigger + | PersistentNotificationTrigger | ZoneTrigger | TagTrigger | TimeTrigger diff --git a/src/data/automation_i18n.ts b/src/data/automation_i18n.ts index b7ea6dcaab..854d8501f7 100644 --- a/src/data/automation_i18n.ts +++ b/src/data/automation_i18n.ts @@ -590,6 +590,12 @@ export const describeTrigger = ( ); } + // Persistent Notification Trigger + if (trigger.platform === "persistent_notification") { + return "When a persistent notification is updated"; + } + + // Device Trigger if (trigger.platform === "device") { if (!trigger.device_id) { return "Device trigger"; diff --git a/src/data/trigger.ts b/src/data/trigger.ts index 847c4ee50b..3d6cf2558b 100644 --- a/src/data/trigger.ts +++ b/src/data/trigger.ts @@ -8,6 +8,7 @@ import { mdiHomeAssistant, mdiMapMarker, mdiMapMarkerRadius, + mdiMessageAlert, mdiNfcVariant, mdiNumeric, mdiStateMachine, @@ -31,5 +32,6 @@ export const TRIGGER_TYPES = { time: mdiClockOutline, time_pattern: mdiAvTimer, webhook: mdiWebhook, + persistent_notification: mdiMessageAlert, zone: mdiMapMarkerRadius, }; 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 52b51686bc..fc8ce54e79 100644 --- a/src/panels/config/automation/trigger/ha-automation-trigger-row.ts +++ b/src/panels/config/automation/trigger/ha-automation-trigger-row.ts @@ -50,6 +50,7 @@ import "./types/ha-automation-trigger-geo_location"; import "./types/ha-automation-trigger-homeassistant"; import "./types/ha-automation-trigger-mqtt"; import "./types/ha-automation-trigger-numeric_state"; +import "./types/ha-automation-trigger-persistent_notification"; import "./types/ha-automation-trigger-state"; import "./types/ha-automation-trigger-sun"; import "./types/ha-automation-trigger-tag"; diff --git a/src/panels/config/automation/trigger/ha-automation-trigger.ts b/src/panels/config/automation/trigger/ha-automation-trigger.ts index 66d59f3552..d30e42ba98 100644 --- a/src/panels/config/automation/trigger/ha-automation-trigger.ts +++ b/src/panels/config/automation/trigger/ha-automation-trigger.ts @@ -42,6 +42,7 @@ import "./types/ha-automation-trigger-geo_location"; import "./types/ha-automation-trigger-homeassistant"; import "./types/ha-automation-trigger-mqtt"; import "./types/ha-automation-trigger-numeric_state"; +import "./types/ha-automation-trigger-persistent_notification"; import "./types/ha-automation-trigger-state"; import "./types/ha-automation-trigger-sun"; import "./types/ha-automation-trigger-tag"; 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 new file mode 100644 index 0000000000..0e942c1860 --- /dev/null +++ b/src/panels/config/automation/trigger/types/ha-automation-trigger-persistent_notification.ts @@ -0,0 +1,117 @@ +import memoizeOne from "memoize-one"; + +import { css, html, LitElement } from "lit"; +import { customElement, property } from "lit/decorators"; +import { fireEvent } from "../../../../../common/dom/fire_event"; +import "../../../../../components/ha-button-menu"; +import "../../../../../components/ha-check-list-item"; +import "../../../../../components/ha-icon-button"; +import "../../../../../components/ha-textfield"; +import { PersistentNotificationTrigger } from "../../../../../data/automation"; +import { HomeAssistant } from "../../../../../types"; +import type { TriggerElement } from "../ha-automation-trigger-row"; +import type { LocalizeFunc } from "../../../../../common/translations/localize"; +import type { SchemaUnion } from "../../../../../components/ha-form/types"; + +const DEFAULT_UPDATE_TYPES = ["added", "removed"]; +const DEFAULT_NOTIFICATION_ID = ""; + +@customElement("ha-automation-trigger-persistent_notification") +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: false, + options: [ + [ + "added", + localize( + "ui.panel.config.automation.editor.triggers.type.persistent_notification.update_types.added" + ), + ], + [ + "removed", + localize( + "ui.panel.config.automation.editor.triggers.type.persistent_notification.update_types.removed" + ), + ], + [ + "current", + localize( + "ui.panel.config.automation.editor.triggers.type.persistent_notification.update_types.current" + ), + ], + [ + "updated", + localize( + "ui.panel.config.automation.editor.triggers.type.persistent_notification.update_types.updated" + ), + ], + ], + }, + ] as const + ); + + public static get defaultConfig() { + return { + update_type: [...DEFAULT_UPDATE_TYPES], + notification_id: DEFAULT_NOTIFICATION_ID, + }; + } + + 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: SchemaUnion> + ): string => + this.hass.localize( + `ui.panel.config.automation.editor.triggers.type.persistent_notification.${schema.name}` + ); + + static styles = css` + ha-textfield { + display: block; + } + `; +} + +declare global { + interface HTMLElementTagNameMap { + "ha-automation-trigger-persistent_notification": HaPersistentNotificationTrigger; + } +} diff --git a/src/translations/en.json b/src/translations/en.json index 16ca1df024..8e46f03ee8 100644 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -2341,6 +2341,17 @@ "type_value": "Fixed number", "type_input": "Numeric value of another entity" }, + "persistent_notification": { + "label": "Persistent notification", + "notification_id": "Notification Id", + "update_type": "Update type", + "update_types": { + "added": "added", + "removed": "removed", + "current": "current", + "updated": "updated" + } + }, "sun": { "label": "Sun", "event": "[%key:ui::panel::config::automation::editor::triggers::type::homeassistant::event%]",