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
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.components.notify import PLATFORM_SCHEMA, BaseNotificationService
|
|
|
|
from homeassistant.const import CONF_NAME, CONF_RECIPIENT
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
|
2020-06-22 23:07:58 +00:00
|
|
|
from .const import DOMAIN, SMS_GATEWAY
|
2020-01-28 08:35:41 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{vol.Required(CONF_RECIPIENT): cv.string, vol.Optional(CONF_NAME): cv.string}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def get_service(hass, config, discovery_info=None):
|
|
|
|
"""Get the SMS notification service."""
|
2020-06-22 23:07:58 +00:00
|
|
|
|
|
|
|
if SMS_GATEWAY not in hass.data[DOMAIN]:
|
|
|
|
_LOGGER.error("SMS gateway not found, cannot initialize service")
|
|
|
|
return
|
|
|
|
|
|
|
|
gateway = hass.data[DOMAIN][SMS_GATEWAY]
|
|
|
|
|
|
|
|
if discovery_info is None:
|
|
|
|
number = config[CONF_RECIPIENT]
|
|
|
|
else:
|
|
|
|
number = discovery_info[CONF_RECIPIENT]
|
|
|
|
|
2020-01-28 08:35:41 +00:00
|
|
|
return SMSNotificationService(gateway, number)
|
|
|
|
|
|
|
|
|
|
|
|
class SMSNotificationService(BaseNotificationService):
|
|
|
|
"""Implement the notification service for SMS."""
|
|
|
|
|
|
|
|
def __init__(self, gateway, number):
|
|
|
|
"""Initialize the service."""
|
|
|
|
self.gateway = gateway
|
|
|
|
self.number = number
|
|
|
|
|
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."""
|
2020-04-18 20:26:22 +00:00
|
|
|
smsinfo = {
|
|
|
|
"Class": -1,
|
2021-09-21 08:07:14 +00:00
|
|
|
"Unicode": True,
|
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}
|
|
|
|
encoded_message["Number"] = self.number
|
2020-04-18 20:26:22 +00:00
|
|
|
try:
|
|
|
|
# Actually send the message
|
2020-06-22 23:07:58 +00:00
|
|
|
await self.gateway.send_sms_async(encoded_message)
|
2021-03-02 08:02:04 +00:00
|
|
|
except gammu.GSMError as exc:
|
2020-04-18 20:26:22 +00:00
|
|
|
_LOGGER.error("Sending to %s failed: %s", self.number, exc)
|