2019-04-03 15:40:03 +00:00
|
|
|
"""LlamaLab Automate notification service."""
|
2016-08-20 22:11:37 +00:00
|
|
|
import logging
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2016-08-20 22:11:37 +00:00
|
|
|
import requests
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2017-05-02 16:18:47 +00:00
|
|
|
from homeassistant.const import CONF_API_KEY, CONF_DEVICE
|
2016-08-20 22:11:37 +00:00
|
|
|
from homeassistant.helpers import config_validation as cv
|
|
|
|
|
2019-03-28 03:36:13 +00:00
|
|
|
from homeassistant.components.notify import (PLATFORM_SCHEMA,
|
|
|
|
BaseNotificationService)
|
2017-05-02 16:18:47 +00:00
|
|
|
|
2016-08-20 22:11:37 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2017-05-02 16:18:47 +00:00
|
|
|
_RESOURCE = 'https://llamalab.com/automate/cloud/message'
|
2016-08-20 22:11:37 +00:00
|
|
|
|
|
|
|
CONF_TO = 'to'
|
|
|
|
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
2017-01-15 02:53:14 +00:00
|
|
|
vol.Required(CONF_API_KEY): cv.string,
|
|
|
|
vol.Required(CONF_TO): cv.string,
|
|
|
|
vol.Optional(CONF_DEVICE): cv.string,
|
2016-08-20 22:11:37 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
|
2017-01-15 02:53:14 +00:00
|
|
|
def get_service(hass, config, discovery_info=None):
|
2016-08-20 22:11:37 +00:00
|
|
|
"""Get the LlamaLab Automate notification service."""
|
|
|
|
secret = config.get(CONF_API_KEY)
|
|
|
|
recipient = config.get(CONF_TO)
|
|
|
|
device = config.get(CONF_DEVICE)
|
|
|
|
|
|
|
|
return AutomateNotificationService(secret, recipient, device)
|
|
|
|
|
|
|
|
|
|
|
|
class AutomateNotificationService(BaseNotificationService):
|
|
|
|
"""Implement the notification service for LlamaLab Automate."""
|
|
|
|
|
|
|
|
def __init__(self, secret, recipient, device=None):
|
|
|
|
"""Initialize the service."""
|
|
|
|
self._secret = secret
|
|
|
|
self._recipient = recipient
|
|
|
|
self._device = device
|
|
|
|
|
|
|
|
def send_message(self, message="", **kwargs):
|
|
|
|
"""Send a message to a user."""
|
|
|
|
_LOGGER.debug("Sending to: %s, %s", self._recipient, str(self._device))
|
|
|
|
data = {
|
|
|
|
"secret": self._secret,
|
|
|
|
"to": self._recipient,
|
|
|
|
"device": self._device,
|
|
|
|
"payload": message,
|
|
|
|
}
|
|
|
|
|
|
|
|
response = requests.post(_RESOURCE, json=data)
|
|
|
|
if response.status_code != 200:
|
2018-02-11 17:20:28 +00:00
|
|
|
_LOGGER.error("Error sending message: %s", response)
|