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..561f709df2 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,11 @@ 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 89ea2257c4..689561b30d 100644 --- a/src/data/automation_i18n.ts +++ b/src/data/automation_i18n.ts @@ -600,6 +600,12 @@ export const describeTrigger = ( return "When a Webhook payload has been received"; } + // 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..4f1975a8c7 --- /dev/null +++ b/src/panels/config/automation/trigger/types/ha-automation-trigger-persistent_notification.ts @@ -0,0 +1,107 @@ +import type { RequestSelectedDetail } from "@material/mwc-list/mwc-list-item"; +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 { handleChangeEvent } from "../ha-automation-trigger-row"; + +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 { + @property({ attribute: false }) public hass!: HomeAssistant; + + @property() public trigger!: PersistentNotificationTrigger; + + @property({ type: Boolean }) public disabled = false; + + public static get defaultConfig() { + return { + update_type: [...DEFAULT_UPDATE_TYPES], + notification_id: DEFAULT_NOTIFICATION_ID, + }; + } + + protected render() { + const { + update_type: updateTypes, + notification_id: notificationId, + } = this.trigger; + + return html` +
+ + + ${SUPPORTED_UPDATE_TYPES.map( + (update_type) => html` + + ${update_type} + + +
+ ` + )} + `; + } + + private _valueChanged(ev: CustomEvent): void { + handleChangeEvent(this, ev); + } + + private _updateTypeChanged(ev: CustomEvent): void { + ev.stopPropagation(); + const updateType = (ev.target as any).value; + const selected = ev.detail.selected; + + if (selected === this.trigger.update_type?.includes(updateType)) { + return; + } + + const updateTypes = this.trigger.update_type ?? []; + const newUpdateTypes = [...updateTypes]; + + if (selected) { + newUpdateTypes.push(updateType); + } else { + newUpdateTypes.splice(newUpdateTypes.indexOf(updateType), 1); + } + const newTrigger = { ...this.trigger, update_type: newUpdateTypes }; + fireEvent(this, "value-changed", { value: newTrigger }); + } + + 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 2b46e38cca..44ff7399b6 100755 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -2325,6 +2325,11 @@ "type_value": "Fixed number", "type_input": "Numeric value of another entity" }, + "persistent_notification": { + "label": "Persistent notification", + "notification_id": "Notification Id", + "update_type": "Update type" + }, "sun": { "label": "Sun", "event": "[%key:ui::panel::config::automation::editor::triggers::type::homeassistant::event%]",