1 """Support for Sinch notifications."""
3 from __future__
import annotations
7 from clx.xms.api
import MtBatchTextSmsResult
8 from clx.xms.client
import Client
9 from clx.xms.exceptions
import (
10 ErrorResponseException,
12 UnauthorizedException,
13 UnexpectedResponseException,
15 import voluptuous
as vol
21 PLATFORM_SCHEMA
as NOTIFY_PLATFORM_SCHEMA,
22 BaseNotificationService,
31 CONF_SERVICE_PLAN_ID =
"service_plan_id"
32 CONF_DEFAULT_RECIPIENTS =
"default_recipients"
34 ATTR_SENDER = CONF_SENDER
36 DEFAULT_SENDER =
"Home Assistant"
38 _LOGGER = logging.getLogger(__name__)
40 PLATFORM_SCHEMA = NOTIFY_PLATFORM_SCHEMA.extend(
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]
55 discovery_info: DiscoveryInfoType |
None =
None,
56 ) -> SinchNotificationService:
57 """Get the Sinch notification service."""
62 """Send Notifications to Sinch SMS recipients."""
65 """Initialize the service."""
67 self.
sendersender = config[CONF_SENDER]
68 self.
clientclient = Client(config[CONF_SERVICE_PLAN_ID], config[CONF_API_KEY])
71 """Send a message to a user."""
73 data = kwargs.get(ATTR_DATA)
or {}
75 clx_args = {ATTR_MESSAGE: message, ATTR_SENDER: self.
sendersender}
77 if ATTR_SENDER
in data:
78 clx_args[ATTR_SENDER] = data[ATTR_SENDER]
81 _LOGGER.error(
"At least 1 target is required")
85 for target
in targets:
86 result: MtBatchTextSmsResult = self.
clientclient.create_text_message(
87 clx_args[ATTR_SENDER], target, clx_args[ATTR_MESSAGE]
89 batch_id = result.batch_id
91 'Successfully sent SMS to "%s" (batch_id: %s)', target, batch_id
93 except ErrorResponseException
as ex:
95 "Caught ErrorResponseException. Response code: %s (%s)",
99 except NotFoundException
as ex:
100 _LOGGER.error(
"Caught NotFoundException (request URL: %s)", ex.url)
101 except UnauthorizedException
as ex:
103 "Caught UnauthorizedException (service plan: %s)", ex.service_plan_id
105 except UnexpectedResponseException
as ex:
106 _LOGGER.error(
"Caught UnexpectedResponseException: %s", ex)
def send_message(self, message="", **kwargs)
def __init__(self, config)
SinchNotificationService get_service(HomeAssistant hass, ConfigType config, DiscoveryInfoType|None discovery_info=None)