Home Assistant Unofficial Reference 2024.12.1
notify.py
Go to the documentation of this file.
1 """Support for TP-Link LTE notifications."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 import attr
9 import tp_connected
10 
11 from homeassistant.components.notify import ATTR_TARGET, BaseNotificationService
12 from homeassistant.const import CONF_RECIPIENT
13 from homeassistant.core import HomeAssistant
14 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
15 
16 from . import DATA_KEY, LTEData
17 
18 _LOGGER = logging.getLogger(__name__)
19 
20 
22  hass: HomeAssistant,
23  config: ConfigType,
24  discovery_info: DiscoveryInfoType | None = None,
25 ) -> TplinkNotifyService | None:
26  """Get the notification service."""
27  if discovery_info is None:
28  return None
29  return TplinkNotifyService(hass, discovery_info)
30 
31 
32 @attr.s
33 class TplinkNotifyService(BaseNotificationService):
34  """Implementation of a notification service."""
35 
36  hass: HomeAssistant = attr.ib()
37  config: dict[str, Any] = attr.ib()
38 
39  async def async_send_message(self, message: str = "", **kwargs: Any) -> None:
40  """Send a message to a user."""
41 
42  lte_data: LTEData = self.hass.data[DATA_KEY]
43  modem_data = lte_data.get_modem_data(self.config)
44  if not modem_data:
45  _LOGGER.error("No modem available")
46  return
47 
48  phone = self.config[CONF_RECIPIENT]
49  targets = kwargs.get(ATTR_TARGET, phone)
50  if targets and message:
51  for target in targets:
52  try:
53  await modem_data.modem.sms(target, message)
54  except tp_connected.Error:
55  _LOGGER.error("Unable to send to %s", target)