1
mirror of https://github.com/home-assistant/core synced 2024-08-02 23:40:32 +02:00
ha-core/homeassistant/components/sms/notify.py
Oscar Calvo e4b288ef7c
Load sms notify via discovery (#76733)
* Fix #76283

Fix #76283

* Update notify.py

* Support sending SMS as ANSI

* Put back load via discovery

* Update homeassistant/components/sms/const.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update homeassistant/components/sms/__init__.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update homeassistant/components/sms/__init__.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update homeassistant/components/sms/notify.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Fix typo

* Apply PR feedback

* Fix bad reference

* Update homeassistant/components/sms/notify.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update homeassistant/components/sms/notify.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update homeassistant/components/sms/notify.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update homeassistant/components/sms/notify.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update homeassistant/components/sms/notify.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Apply PR feedback

* Add back schema

* Update homeassistant/components/sms/notify.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Fix pylint

* Remove platform schema

* Fix after merge

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
2022-08-22 20:03:57 +02:00

70 lines
2.1 KiB
Python

"""Support for SMS notification services."""
import logging
import gammu # pylint: disable=import-error
from homeassistant.components.notify import BaseNotificationService
from homeassistant.const import CONF_TARGET
from .const import CONF_UNICODE, DOMAIN, GATEWAY, SMS_GATEWAY
_LOGGER = logging.getLogger(__name__)
async def async_get_service(hass, config, discovery_info=None):
"""Get the SMS notification service."""
if discovery_info is None:
return None
return SMSNotificationService(hass)
class SMSNotificationService(BaseNotificationService):
"""Implement the notification service for SMS."""
def __init__(self, hass):
"""Initialize the service."""
self.hass = hass
async def async_send_message(self, message="", **kwargs):
"""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)
if targets is None:
_LOGGER.error("No target number specified, cannot send message")
return
is_unicode = kwargs.get(CONF_UNICODE, True)
smsinfo = {
"Class": -1,
"Unicode": is_unicode,
"Entries": [{"ID": "ConcatenatedTextLong", "Buffer": message}],
}
try:
# Encode messages
encoded = gammu.EncodeSMS(smsinfo)
except gammu.GSMError as exc:
_LOGGER.error("Encoding message %s failed: %s", message, exc)
return
# Send messages
for encoded_message in encoded:
# Fill in numbers
encoded_message["SMSC"] = {"Location": 1}
for target in targets:
encoded_message["Number"] = target
try:
# Actually send the message
await gateway.send_sms_async(encoded_message)
except gammu.GSMError as exc:
_LOGGER.error("Sending to %s failed: %s", target, exc)