Home Assistant Unofficial Reference 2024.12.1
notify.py
Go to the documentation of this file.
1 """Support for Huawei LTE router notifications."""
2 
3 from __future__ import annotations
4 
5 import logging
6 import time
7 from typing import Any
8 
9 from huawei_lte_api.exceptions import ResponseErrorException
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 Router
17 from .const import ATTR_CONFIG_ENTRY_ID, DOMAIN
18 
19 _LOGGER = logging.getLogger(__name__)
20 
21 
23  hass: HomeAssistant,
24  config: ConfigType,
25  discovery_info: DiscoveryInfoType | None = None,
26 ) -> HuaweiLteSmsNotificationService | None:
27  """Get the notification service."""
28  if discovery_info is None:
29  return None
30 
31  router = hass.data[DOMAIN].routers[discovery_info[ATTR_CONFIG_ENTRY_ID]]
32  default_targets = discovery_info[CONF_RECIPIENT] or []
33 
34  return HuaweiLteSmsNotificationService(router, default_targets)
35 
36 
37 class HuaweiLteSmsNotificationService(BaseNotificationService):
38  """Huawei LTE router SMS notification service."""
39 
40  def __init__(self, router: Router, default_targets: list[str]) -> None:
41  """Initialize."""
42  self.routerrouter = router
43  self.default_targetsdefault_targets = default_targets
44 
45  def send_message(self, message: str = "", **kwargs: Any) -> None:
46  """Send message to target numbers."""
47 
48  targets = kwargs.get(ATTR_TARGET, self.default_targetsdefault_targets)
49  if not targets or not message:
50  return
51 
52  if self.routerrouter.suspended:
53  _LOGGER.debug(
54  "Integration suspended, not sending notification to %s", targets
55  )
56  return
57 
58  try:
59  resp = self.routerrouter.client.sms.send_sms(
60  phone_numbers=targets, message=message
61  )
62  _LOGGER.debug("Sent to %s: %s", targets, resp)
63  except ResponseErrorException as ex:
64  _LOGGER.error("Could not send to %s: %s", targets, ex)
65  finally:
66  self.routerrouter.notify_last_attempt = time.monotonic()
None __init__(self, Router router, list[str] default_targets)
Definition: notify.py:40
HuaweiLteSmsNotificationService|None async_get_service(HomeAssistant hass, ConfigType config, DiscoveryInfoType|None discovery_info=None)
Definition: notify.py:26