This commit is contained in:
Oscar Calvo 2022-08-09 14:46:22 -06:00 committed by GitHub
parent dc47121f2c
commit 596b7fed34
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -20,31 +20,32 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
def get_service(hass, config, discovery_info=None): def get_service(hass, config, discovery_info=None):
"""Get the SMS notification service.""" """Get the SMS notification service."""
if SMS_GATEWAY not in hass.data[DOMAIN]:
_LOGGER.error("SMS gateway not found, cannot initialize service")
return
gateway = hass.data[DOMAIN][SMS_GATEWAY][GATEWAY]
if discovery_info is None: if discovery_info is None:
number = config[CONF_RECIPIENT] number = config[CONF_RECIPIENT]
else: else:
number = discovery_info[CONF_RECIPIENT] number = discovery_info[CONF_RECIPIENT]
return SMSNotificationService(gateway, number) return SMSNotificationService(hass, number)
class SMSNotificationService(BaseNotificationService): class SMSNotificationService(BaseNotificationService):
"""Implement the notification service for SMS.""" """Implement the notification service for SMS."""
def __init__(self, gateway, number): def __init__(self, hass, number):
"""Initialize the service.""" """Initialize the service."""
self.gateway = gateway
self.hass = hass
self.number = number self.number = number
async def async_send_message(self, message="", **kwargs): async def async_send_message(self, message="", **kwargs):
"""Send SMS message.""" """Send SMS message."""
if SMS_GATEWAY not in self.hass.data[DOMAIN]:
_LOGGER.error("SMS gateway not found, cannot send message")
return
gateway = self.hass.data[DOMAIN][SMS_GATEWAY][GATEWAY]
targets = kwargs.get(CONF_TARGET, [self.number]) targets = kwargs.get(CONF_TARGET, [self.number])
smsinfo = { smsinfo = {
"Class": -1, "Class": -1,
@ -67,6 +68,6 @@ class SMSNotificationService(BaseNotificationService):
encoded_message["Number"] = target encoded_message["Number"] = target
try: try:
# Actually send the message # Actually send the message
await self.gateway.send_sms_async(encoded_message) await gateway.send_sms_async(encoded_message)
except gammu.GSMError as exc: except gammu.GSMError as exc:
_LOGGER.error("Sending to %s failed: %s", target, exc) _LOGGER.error("Sending to %s failed: %s", target, exc)