2020-01-28 08:35:41 +00:00
|
|
|
"""Support for SMS notification services."""
|
|
|
|
import logging
|
|
|
|
|
2021-03-02 08:02:04 +00:00
|
|
|
import gammu # pylint: disable=import-error
|
2020-01-28 08:35:41 +00:00
|
|
|
|
2022-08-22 18:03:57 +00:00
|
|
|
from homeassistant.components.notify import BaseNotificationService
|
|
|
|
from homeassistant.const import CONF_TARGET
|
2020-01-28 08:35:41 +00:00
|
|
|
|
2022-08-22 07:22:43 +00:00
|
|
|
from .const import CONF_UNICODE, DOMAIN, GATEWAY, SMS_GATEWAY
|
2020-01-28 08:35:41 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2022-08-22 18:03:57 +00:00
|
|
|
async def async_get_service(hass, config, discovery_info=None):
|
2020-01-28 08:35:41 +00:00
|
|
|
"""Get the SMS notification service."""
|
2020-06-22 23:07:58 +00:00
|
|
|
|
|
|
|
if discovery_info is None:
|
2022-08-22 18:03:57 +00:00
|
|
|
return None
|
2020-06-22 23:07:58 +00:00
|
|
|
|
2022-08-22 18:03:57 +00:00
|
|
|
return SMSNotificationService(hass)
|
2020-01-28 08:35:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SMSNotificationService(BaseNotificationService):
|
|
|
|
"""Implement the notification service for SMS."""
|
|
|
|
|
2022-08-22 18:03:57 +00:00
|
|
|
def __init__(self, hass):
|
2020-01-28 08:35:41 +00:00
|
|
|
"""Initialize the service."""
|
2022-08-09 20:46:22 +00:00
|
|
|
|
|
|
|
self.hass = hass
|
2020-01-28 08:35:41 +00:00
|
|
|
|
2020-07-07 18:35:30 +00:00
|
|
|
async def async_send_message(self, message="", **kwargs):
|
2020-01-28 08:35:41 +00:00
|
|
|
"""Send SMS message."""
|
2022-06-28 20:57:47 +00:00
|
|
|
|
2022-08-09 20:46:22 +00:00
|
|
|
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]
|
|
|
|
|
2022-08-22 18:03:57 +00:00
|
|
|
targets = kwargs.get(CONF_TARGET)
|
|
|
|
if targets is None:
|
|
|
|
_LOGGER.error("No target number specified, cannot send message")
|
|
|
|
return
|
|
|
|
|
2022-08-22 07:22:43 +00:00
|
|
|
is_unicode = kwargs.get(CONF_UNICODE, True)
|
2020-04-18 20:26:22 +00:00
|
|
|
smsinfo = {
|
|
|
|
"Class": -1,
|
2022-08-22 07:22:43 +00:00
|
|
|
"Unicode": is_unicode,
|
2020-04-18 20:26:22 +00:00
|
|
|
"Entries": [{"ID": "ConcatenatedTextLong", "Buffer": message}],
|
2020-01-28 08:35:41 +00:00
|
|
|
}
|
|
|
|
try:
|
2020-04-18 20:26:22 +00:00
|
|
|
# Encode messages
|
2021-03-02 08:02:04 +00:00
|
|
|
encoded = gammu.EncodeSMS(smsinfo)
|
|
|
|
except gammu.GSMError as exc:
|
2020-04-18 20:26:22 +00:00
|
|
|
_LOGGER.error("Encoding message %s failed: %s", message, exc)
|
|
|
|
return
|
|
|
|
|
|
|
|
# Send messages
|
|
|
|
for encoded_message in encoded:
|
2020-04-21 19:03:15 +00:00
|
|
|
# Fill in numbers
|
|
|
|
encoded_message["SMSC"] = {"Location": 1}
|
2022-06-28 20:57:47 +00:00
|
|
|
|
|
|
|
for target in targets:
|
|
|
|
encoded_message["Number"] = target
|
|
|
|
try:
|
|
|
|
# Actually send the message
|
2022-08-09 20:46:22 +00:00
|
|
|
await gateway.send_sms_async(encoded_message)
|
2022-06-28 20:57:47 +00:00
|
|
|
except gammu.GSMError as exc:
|
|
|
|
_LOGGER.error("Sending to %s failed: %s", target, exc)
|