Add Gammu based local SMS notifications (#31233)

* Add sms integration

Committer: ocalvo <oscar@calvonet.com>

* Fix PyLint

* Update requirements
This commit is contained in:
Oscar Calvo 2020-01-28 00:35:41 -08:00 committed by Pascal Vizeli
parent ec2d378a19
commit d813618d0d
8 changed files with 97 additions and 0 deletions

View file

@ -0,0 +1,33 @@
"""The sms component."""
import logging
import gammu # pylint: disable=import-error, no-member
import voluptuous as vol
from homeassistant.const import CONF_DEVICE
from homeassistant.helpers import config_validation as cv
from .const import DOMAIN
_LOGGER = logging.getLogger(__name__)
CONFIG_SCHEMA = vol.Schema(
{DOMAIN: vol.Schema({vol.Required(CONF_DEVICE): cv.isdevice})},
extra=vol.ALLOW_EXTRA,
)
async def async_setup(hass, config):
"""Configure Gammu state machine."""
conf = config[DOMAIN]
device = conf.get(CONF_DEVICE)
gateway = gammu.StateMachine() # pylint: disable=no-member
try:
gateway.SetConfig(0, dict(Device=device, Connection="at"))
gateway.Init()
except gammu.GSMError as exc: # pylint: disable=no-member
_LOGGER.error("Failed to initialize, error %s", exc)
return False
else:
hass.data[DOMAIN] = gateway
return True

View file

@ -0,0 +1,3 @@
"""Constants for sms Component."""
DOMAIN = "sms"

View file

@ -0,0 +1,8 @@
{
"domain": "sms",
"name": "SMS notifications via GSM-modem",
"documentation": "https://www.home-assistant.io/integrations/sms",
"requirements": ["python-gammu==2.12"],
"dependencies": [],
"codeowners": ["@ocalvo"]
}

View file

@ -0,0 +1,47 @@
"""Support for SMS notification services."""
import logging
import gammu # pylint: disable=import-error, no-member
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
from .const import DOMAIN
_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."""
gateway = hass.data[DOMAIN]
number = config[CONF_RECIPIENT]
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
def send_message(self, message="", **kwargs):
"""Send SMS message."""
# Prepare message data
# We tell that we want to use first SMSC number stored in phone
gammu_message = {
"Text": message,
"SMSC": {"Location": 1},
"Number": self.number,
}
try:
self.gateway.SendSMS(gammu_message)
except gammu.GSMError as exc: # pylint: disable=no-member
_LOGGER.error("Sending to %s failed: %s", self.number, exc)