1 """Support for repeating alerts when conditions are met."""
3 from __future__
import annotations
5 from collections.abc
import Callable
6 from datetime
import timedelta
13 DOMAIN
as DOMAIN_NOTIFY,
20 async_track_point_in_time,
21 async_track_state_change_event,
26 from .const
import DOMAIN, LOGGER
30 """Representation of an alert."""
32 _attr_should_poll =
False
39 watched_entity_id: str,
43 message_template: Template |
None,
44 done_message_template: Template |
None,
47 title_template: Template |
None,
50 """Initialize the alert."""
69 self.
_cancel_cancel: Callable[[],
None] |
None =
None
79 """Return the alert status."""
87 """Determine if the alert should start or stop."""
88 if (to_state := event.data[
"new_state"])
is None:
90 LOGGER.debug(
"Watched entity (%s) has changed", event.data[
"entity_id"])
97 """Begin the alert procedures."""
98 LOGGER.debug(
"Beginning Alert: %s", self.
_attr_name_attr_name)
111 """End the alert procedures."""
112 LOGGER.debug(
"Ending Alert: %s", self.
_attr_name_attr_name)
113 if self.
_cancel_cancel
is not None:
117 self.
_ack_ack =
False
124 """Schedule a notification."""
126 next_msg =
now() + delay
130 self.
_notify_notify, name=
"Schedule notify alert", cancel_on_shutdown=
True
137 """Send the alert notification."""
141 if not self.
_ack_ack:
142 LOGGER.info(
"Alerting: %s", self.
_attr_name_attr_name)
146 message = self.
_message_template_message_template.async_render(parse_result=
False)
154 """Send notification of complete alert."""
169 msg_payload = {ATTR_MESSAGE: message}
172 title = self.
_title_template_title_template.async_render(parse_result=
False)
173 msg_payload[ATTR_TITLE] = title
175 msg_payload[ATTR_DATA] = self.
_data_data
177 LOGGER.debug(msg_payload)
181 await self.
hasshasshass.services.async_call(
182 DOMAIN_NOTIFY, target, msg_payload, context=self.
_context_context
184 except ServiceNotFound:
186 "Failed to call notify.%s, retrying at next notification interval",
191 """Async Unacknowledge alert."""
192 LOGGER.debug(
"Reset Alert: %s", self.
_attr_name_attr_name)
193 self.
_ack_ack =
False
197 """Async Acknowledge alert."""
198 LOGGER.debug(
"Acknowledged Alert: %s", self.
_attr_name_attr_name)
203 """Async toggle alert."""
None _notify(self, *Any args)
None _notify_done_message(self)
None async_toggle(self, **Any kwargs)
None begin_alerting(self)
None _send_notification_message(self, Any message)
None watched_entity_change(self, Event[EventStateChangedData] event)
None _schedule_notify(self)
None async_turn_on(self, **Any kwargs)
None __init__(self, HomeAssistant hass, str entity_id, str name, str watched_entity_id, str state, list[float] repeat, bool skip_first, Template|None message_template, Template|None done_message_template, list[str] notifiers, bool can_ack, Template|None title_template, dict[Any, Any] data)
None async_turn_off(self, **Any kwargs)
None async_write_ha_state(self)
CALLBACK_TYPE async_track_state_change_event(HomeAssistant hass, str|Iterable[str] entity_ids, Callable[[Event[EventStateChangedData]], Any] action, HassJobType|None job_type=None)
CALLBACK_TYPE async_track_point_in_time(HomeAssistant hass, HassJob[[datetime], Coroutine[Any, Any, None]|None]|Callable[[datetime], Coroutine[Any, Any, None]|None] action, datetime point_in_time)
datetime now(HomeAssistant hass)