1 """Offer persistent_notifications triggered automation rules."""
3 from __future__
import annotations
6 from typing
import Final
8 import voluptuous
as vol
16 from .
import Notification, UpdateType, async_register_callback
18 _LOGGER = logging.getLogger(__name__)
21 CONF_NOTIFICATION_ID: Final =
"notification_id"
22 CONF_UPDATE_TYPE: Final =
"update_type"
24 TRIGGER_SCHEMA = cv.TRIGGER_BASE_SCHEMA.extend(
26 vol.Required(CONF_PLATFORM):
"persistent_notification",
27 vol.Optional(CONF_NOTIFICATION_ID): str,
28 vol.Optional(CONF_UPDATE_TYPE): vol.All(
29 cv.ensure_list, [vol.Coerce(UpdateType)]
38 action: TriggerActionType,
39 trigger_info: TriggerInfo,
41 """Listen for state changes based on configuration."""
42 trigger_data: TriggerData = trigger_info[
"trigger_data"]
45 persistent_notification_id = config.get(CONF_NOTIFICATION_ID)
46 update_types = config.get(CONF_UPDATE_TYPE)
49 def persistent_notification_listener(
50 update_type: UpdateType, notifications: dict[str, Notification]
52 """Listen for persistent_notification updates."""
54 for notification
in notifications.values():
55 if update_types
and update_type
not in update_types:
58 persistent_notification_id
59 and notification[CONF_NOTIFICATION_ID] != persistent_notification_id
63 hass.async_run_hass_job(
68 "platform":
"persistent_notification",
69 "update_type": update_type,
70 "notification": notification,
76 "Attaching persistent_notification trigger for ID: '%s', update_types: %s",
77 persistent_notification_id,
CALLBACK_TYPE async_attach_trigger(HomeAssistant hass, ConfigType config, TriggerActionType action, TriggerInfo trigger_info)
CALLBACK_TYPE async_register_callback(HomeAssistant hass, Callable[[UpdateType, dict[str, Notification]], None] _callback)