Home Assistant Unofficial Reference 2024.12.1
notify.py
Go to the documentation of this file.
1 """Support for Sinch notifications."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 from clx.xms.api import MtBatchTextSmsResult
8 from clx.xms.client import Client
9 from clx.xms.exceptions import (
10  ErrorResponseException,
11  NotFoundException,
12  UnauthorizedException,
13  UnexpectedResponseException,
14 )
15 import voluptuous as vol
16 
18  ATTR_DATA,
19  ATTR_MESSAGE,
20  ATTR_TARGET,
21  PLATFORM_SCHEMA as NOTIFY_PLATFORM_SCHEMA,
22  BaseNotificationService,
23 )
24 from homeassistant.const import CONF_API_KEY, CONF_SENDER
25 from homeassistant.core import HomeAssistant
27 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
28 
29 DOMAIN = "sinch"
30 
31 CONF_SERVICE_PLAN_ID = "service_plan_id"
32 CONF_DEFAULT_RECIPIENTS = "default_recipients"
33 
34 ATTR_SENDER = CONF_SENDER
35 
36 DEFAULT_SENDER = "Home Assistant"
37 
38 _LOGGER = logging.getLogger(__name__)
39 
40 PLATFORM_SCHEMA = NOTIFY_PLATFORM_SCHEMA.extend(
41  {
42  vol.Required(CONF_API_KEY): cv.string,
43  vol.Required(CONF_SERVICE_PLAN_ID): cv.string,
44  vol.Optional(CONF_SENDER, default=DEFAULT_SENDER): cv.string,
45  vol.Optional(CONF_DEFAULT_RECIPIENTS, default=[]): vol.All(
46  cv.ensure_list, [cv.string]
47  ),
48  }
49 )
50 
51 
53  hass: HomeAssistant,
54  config: ConfigType,
55  discovery_info: DiscoveryInfoType | None = None,
56 ) -> SinchNotificationService:
57  """Get the Sinch notification service."""
58  return SinchNotificationService(config)
59 
60 
61 class SinchNotificationService(BaseNotificationService):
62  """Send Notifications to Sinch SMS recipients."""
63 
64  def __init__(self, config):
65  """Initialize the service."""
66  self.default_recipientsdefault_recipients = config[CONF_DEFAULT_RECIPIENTS]
67  self.sendersender = config[CONF_SENDER]
68  self.clientclient = Client(config[CONF_SERVICE_PLAN_ID], config[CONF_API_KEY])
69 
70  def send_message(self, message="", **kwargs):
71  """Send a message to a user."""
72  targets = kwargs.get(ATTR_TARGET, self.default_recipientsdefault_recipients)
73  data = kwargs.get(ATTR_DATA) or {}
74 
75  clx_args = {ATTR_MESSAGE: message, ATTR_SENDER: self.sendersender}
76 
77  if ATTR_SENDER in data:
78  clx_args[ATTR_SENDER] = data[ATTR_SENDER]
79 
80  if not targets:
81  _LOGGER.error("At least 1 target is required")
82  return
83 
84  try:
85  for target in targets:
86  result: MtBatchTextSmsResult = self.clientclient.create_text_message(
87  clx_args[ATTR_SENDER], target, clx_args[ATTR_MESSAGE]
88  )
89  batch_id = result.batch_id
90  _LOGGER.debug(
91  'Successfully sent SMS to "%s" (batch_id: %s)', target, batch_id
92  )
93  except ErrorResponseException as ex:
94  _LOGGER.error(
95  "Caught ErrorResponseException. Response code: %s (%s)",
96  ex.error_code,
97  ex,
98  )
99  except NotFoundException as ex:
100  _LOGGER.error("Caught NotFoundException (request URL: %s)", ex.url)
101  except UnauthorizedException as ex:
102  _LOGGER.error(
103  "Caught UnauthorizedException (service plan: %s)", ex.service_plan_id
104  )
105  except UnexpectedResponseException as ex:
106  _LOGGER.error("Caught UnexpectedResponseException: %s", ex)
SinchNotificationService get_service(HomeAssistant hass, ConfigType config, DiscoveryInfoType|None discovery_info=None)
Definition: notify.py:56