2019-04-03 15:40:03 +00:00
|
|
|
"""Clickatell platform for notify component."""
|
2017-10-29 08:14:40 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
import requests
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2019-03-21 05:56:46 +00:00
|
|
|
from homeassistant.const import CONF_API_KEY, CONF_RECIPIENT
|
2017-10-29 08:14:40 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
from homeassistant.components.notify import PLATFORM_SCHEMA, BaseNotificationService
|
2017-10-29 08:14:40 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_NAME = "clickatell"
|
2017-10-29 08:14:40 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
BASE_API_URL = "https://platform.clickatell.com/messages/http/send"
|
2017-10-29 08:14:40 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{vol.Required(CONF_API_KEY): cv.string, vol.Required(CONF_RECIPIENT): cv.string}
|
|
|
|
)
|
2017-10-29 08:14:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_service(hass, config, discovery_info=None):
|
|
|
|
"""Get the Clickatell notification service."""
|
|
|
|
return ClickatellNotificationService(config)
|
|
|
|
|
|
|
|
|
|
|
|
class ClickatellNotificationService(BaseNotificationService):
|
|
|
|
"""Implementation of a notification service for the Clickatell service."""
|
|
|
|
|
|
|
|
def __init__(self, config):
|
|
|
|
"""Initialize the service."""
|
|
|
|
self.api_key = config.get(CONF_API_KEY)
|
|
|
|
self.recipient = config.get(CONF_RECIPIENT)
|
|
|
|
|
|
|
|
def send_message(self, message="", **kwargs):
|
|
|
|
"""Send a message to a user."""
|
2019-07-31 19:25:30 +00:00
|
|
|
data = {"apiKey": self.api_key, "to": self.recipient, "content": message}
|
2017-10-29 08:14:40 +00:00
|
|
|
|
|
|
|
resp = requests.get(BASE_API_URL, params=data, timeout=5)
|
|
|
|
if (resp.status_code != 200) or (resp.status_code != 201):
|
|
|
|
_LOGGER.error("Error %s : %s", resp.status_code, resp.text)
|