Home Assistant Unofficial Reference 2024.12.1
notify.py
Go to the documentation of this file.
1 """Support for Netgear LTE notifications."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 import eternalegypt
8 from eternalegypt.eternalegypt import Modem
9 
10 from homeassistant.components.notify import ATTR_TARGET, BaseNotificationService
11 from homeassistant.const import CONF_RECIPIENT
12 from homeassistant.core import HomeAssistant
13 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
14 
15 from .const import CONF_NOTIFY, LOGGER
16 
17 
19  hass: HomeAssistant,
20  config: ConfigType,
21  discovery_info: DiscoveryInfoType | None = None,
22 ) -> NetgearNotifyService | None:
23  """Get the notification service."""
24  if discovery_info is None:
25  return None
26 
27  return NetgearNotifyService(config, discovery_info)
28 
29 
30 class NetgearNotifyService(BaseNotificationService):
31  """Implementation of a notification service."""
32 
33  def __init__(
34  self,
35  config: ConfigType,
36  discovery_info: dict[str, Any],
37  ) -> None:
38  """Initialize the service."""
39  self.configconfig = config
40  self.modem: Modem = discovery_info["modem"]
41 
42  async def async_send_message(self, message="", **kwargs):
43  """Send a message to a user."""
44 
45  if not self.modem.token:
46  LOGGER.error("Modem not ready")
47  return
48  if not (targets := kwargs.get(ATTR_TARGET)):
49  targets = self.configconfig[CONF_NOTIFY][CONF_RECIPIENT]
50  if not targets:
51  LOGGER.warning("No recipients")
52  return
53 
54  if not message:
55  return
56 
57  for target in targets:
58  try:
59  await self.modem.sms(target, message)
60  except eternalegypt.Error:
61  LOGGER.error("Unable to send to %s", target)
None __init__(self, ConfigType config, dict[str, Any] discovery_info)
Definition: notify.py:37
NetgearNotifyService|None async_get_service(HomeAssistant hass, ConfigType config, DiscoveryInfoType|None discovery_info=None)
Definition: notify.py:22