1 """Provides functionality to notify people."""
3 from __future__
import annotations
5 from datetime
import timedelta
6 from enum
import IntFlag
7 from functools
import partial
9 from typing
import Any, final, override
11 from propcache
import cached_property
12 import voluptuous
as vol
33 NOTIFY_SERVICE_SCHEMA,
35 SERVICE_PERSISTENT_NOTIFICATION,
39 BaseNotificationService,
44 from .repairs
import migrate_notify_issue
49 ATTR_TITLE_DEFAULT =
"Home Assistant"
51 DATA_COMPONENT: HassKey[EntityComponent[NotifyEntity]] =
HassKey(DOMAIN)
52 ENTITY_ID_FORMAT = DOMAIN +
".{}"
56 _LOGGER = logging.getLogger(__name__)
58 PLATFORM_SCHEMA = vol.Schema(
59 {vol.Required(CONF_PLATFORM): cv.string, vol.Optional(CONF_NAME): cv.string},
60 extra=vol.ALLOW_EXTRA,
65 """Supported features of a notify entity."""
70 async
def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
71 """Set up the notify services."""
80 hass.async_create_task(setup, eager_start=
True)
82 component = hass.data[DATA_COMPONENT] = EntityComponent[NotifyEntity](
85 component.async_register_entity_service(
88 vol.Required(ATTR_MESSAGE): cv.string,
89 vol.Optional(ATTR_TITLE): cv.string,
91 "_async_send_message",
94 async
def persistent_notification(service: ServiceCall) ->
None:
95 """Send notification via the built-in persistent_notify integration."""
96 message: str = service.data[ATTR_MESSAGE]
97 title: str |
None = service.data.get(ATTR_TITLE)
99 notification_id =
None
100 if data := service.data.get(ATTR_DATA):
101 notification_id = data.get(pn.ATTR_NOTIFICATION_ID)
103 pn.async_create(hass, message, title, notification_id)
105 hass.services.async_register(
107 SERVICE_PERSISTENT_NOTIFICATION,
108 persistent_notification,
109 schema=NOTIFY_SERVICE_SCHEMA,
116 """A class that describes button entities."""
120 """Set up a config entry."""
125 """Unload a config entry."""
130 """Representation of a notify entity."""
132 entity_description: NotifyEntityDescription
134 _attr_should_poll =
False
135 _attr_device_class:
None
136 _attr_state:
None =
None
137 __last_notified_isoformat: str |
None =
None
143 """Return the entity state."""
147 """Invalidate the cache of the cached property."""
148 self.__dict__.pop(
"state",
None)
152 """Call when the notify entity is added to hass."""
155 if state
is not None and state.state
not in (STATE_UNAVAILABLE,
None):
160 """Send a notification message (from e.g., service call).
162 Should not be overridden, handle setting last notification timestamp.
164 self.
__set_state__set_state(dt_util.utcnow().isoformat())
168 def send_message(self, message: str, title: str |
None =
None) ->
None:
169 """Send a message."""
170 raise NotImplementedError
173 """Send a message."""
174 kwargs: dict[str, Any] = {}
180 kwargs[ATTR_TITLE] = title
181 await self.
hasshass.async_add_executor_job(
182 partial(self.
send_messagesend_message, message, **kwargs)
__last_notified_isoformat
None __set_state(self, str|None state)
None async_internal_added_to_hass(self)
None async_send_message(self, str message, str|None title=None)
None _async_send_message(self, **Any kwargs)
None send_message(self, str message, str|None title=None)
None async_write_ha_state(self)
int|None supported_features(self)
State|None async_get_last_state(self)
list[Coroutine[Any, Any, None]] async_setup_legacy(HomeAssistant hass, ConfigType config)
bool async_setup(HomeAssistant hass, ConfigType config)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)