2016-02-09 06:24:11 +00:00
|
|
|
"""
|
2016-08-20 23:28:45 +00:00
|
|
|
RESTful platform for notify component.
|
2016-02-09 06:24:11 +00:00
|
|
|
|
|
|
|
For more details about this platform, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/notify.rest/
|
|
|
|
"""
|
|
|
|
import logging
|
2016-02-19 05:27:50 +00:00
|
|
|
|
2016-02-09 06:24:11 +00:00
|
|
|
import requests
|
2016-08-20 23:28:45 +00:00
|
|
|
import voluptuous as vol
|
2016-02-09 06:24:11 +00:00
|
|
|
|
|
|
|
from homeassistant.components.notify import (
|
2016-09-01 13:35:46 +00:00
|
|
|
ATTR_TARGET, ATTR_TITLE, ATTR_TITLE_DEFAULT, BaseNotificationService,
|
|
|
|
PLATFORM_SCHEMA)
|
2016-08-20 23:28:45 +00:00
|
|
|
from homeassistant.const import (CONF_RESOURCE, CONF_METHOD, CONF_NAME)
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
2016-02-09 06:24:11 +00:00
|
|
|
|
2016-08-20 23:28:45 +00:00
|
|
|
CONF_MESSAGE_PARAMETER_NAME = 'message_param_name'
|
|
|
|
CONF_TARGET_PARAMETER_NAME = 'target_param_name'
|
|
|
|
CONF_TITLE_PARAMETER_NAME = 'title_param_name'
|
2016-02-10 04:48:17 +00:00
|
|
|
DEFAULT_MESSAGE_PARAM_NAME = 'message'
|
2016-08-20 23:28:45 +00:00
|
|
|
DEFAULT_METHOD = 'GET'
|
2016-02-10 04:48:17 +00:00
|
|
|
DEFAULT_TARGET_PARAM_NAME = None
|
2016-08-20 23:28:45 +00:00
|
|
|
DEFAULT_TITLE_PARAM_NAME = None
|
2016-02-09 06:24:11 +00:00
|
|
|
|
2016-08-20 23:28:45 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
|
|
vol.Required(CONF_RESOURCE): cv.url,
|
|
|
|
vol.Optional(CONF_MESSAGE_PARAMETER_NAME,
|
|
|
|
default=DEFAULT_MESSAGE_PARAM_NAME): cv.string,
|
|
|
|
vol.Optional(CONF_METHOD, default=DEFAULT_METHOD):
|
|
|
|
vol.In(['POST', 'GET', 'POST_JSON']),
|
|
|
|
vol.Optional(CONF_NAME): cv.string,
|
|
|
|
vol.Optional(CONF_TARGET_PARAMETER_NAME,
|
|
|
|
default=DEFAULT_TARGET_PARAM_NAME): cv.string,
|
|
|
|
vol.Optional(CONF_TITLE_PARAMETER_NAME,
|
|
|
|
default=DEFAULT_TITLE_PARAM_NAME): cv.string,
|
|
|
|
})
|
2016-02-09 06:24:11 +00:00
|
|
|
|
2016-08-20 23:28:45 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2016-02-09 06:24:11 +00:00
|
|
|
|
|
|
|
|
2017-01-15 02:53:14 +00:00
|
|
|
def get_service(hass, config, discovery_info=None):
|
2016-08-20 23:28:45 +00:00
|
|
|
"""Get the RESTful notification service."""
|
|
|
|
resource = config.get(CONF_RESOURCE)
|
|
|
|
method = config.get(CONF_METHOD)
|
|
|
|
message_param_name = config.get(CONF_MESSAGE_PARAMETER_NAME)
|
|
|
|
title_param_name = config.get(CONF_TITLE_PARAMETER_NAME)
|
|
|
|
target_param_name = config.get(CONF_TARGET_PARAMETER_NAME)
|
|
|
|
|
|
|
|
return RestNotificationService(
|
|
|
|
resource, method, message_param_name, title_param_name,
|
|
|
|
target_param_name)
|
2016-02-09 06:24:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RestNotificationService(BaseNotificationService):
|
2016-08-20 23:28:45 +00:00
|
|
|
"""Implementation of a notification service for REST."""
|
2016-02-09 06:24:11 +00:00
|
|
|
|
2016-08-20 23:28:45 +00:00
|
|
|
def __init__(self, resource, method, message_param_name, title_param_name,
|
|
|
|
target_param_name):
|
2016-03-08 10:46:32 +00:00
|
|
|
"""Initialize the service."""
|
2016-02-09 06:24:11 +00:00
|
|
|
self._resource = resource
|
2016-02-10 06:27:54 +00:00
|
|
|
self._method = method.upper()
|
2016-02-10 04:48:17 +00:00
|
|
|
self._message_param_name = message_param_name
|
|
|
|
self._title_param_name = title_param_name
|
|
|
|
self._target_param_name = target_param_name
|
2016-02-09 06:24:11 +00:00
|
|
|
|
|
|
|
def send_message(self, message="", **kwargs):
|
2016-03-08 10:46:32 +00:00
|
|
|
"""Send a message to a user."""
|
2016-02-09 06:24:11 +00:00
|
|
|
data = {
|
2016-02-10 04:48:17 +00:00
|
|
|
self._message_param_name: message
|
2016-02-09 06:24:11 +00:00
|
|
|
}
|
|
|
|
|
2016-02-10 04:48:17 +00:00
|
|
|
if self._title_param_name is not None:
|
2017-05-02 16:18:47 +00:00
|
|
|
data[self._title_param_name] = kwargs.get(
|
|
|
|
ATTR_TITLE, ATTR_TITLE_DEFAULT)
|
2016-02-09 06:24:11 +00:00
|
|
|
|
2016-10-02 05:19:17 +00:00
|
|
|
if self._target_param_name is not None and ATTR_TARGET in kwargs:
|
|
|
|
# Target is a list as of 0.29 and we don't want to break existing
|
|
|
|
# integrations, so just return the first target in the list.
|
|
|
|
data[self._target_param_name] = kwargs[ATTR_TARGET][0]
|
2016-02-10 04:48:17 +00:00
|
|
|
|
2016-02-10 06:27:54 +00:00
|
|
|
if self._method == 'POST':
|
2016-02-10 04:48:17 +00:00
|
|
|
response = requests.post(self._resource, data=data, timeout=10)
|
2016-02-10 06:27:54 +00:00
|
|
|
elif self._method == 'POST_JSON':
|
2016-02-10 04:48:17 +00:00
|
|
|
response = requests.post(self._resource, json=data, timeout=10)
|
|
|
|
else: # default GET
|
|
|
|
response = requests.get(self._resource, params=data, timeout=10)
|
|
|
|
|
2016-02-10 06:27:54 +00:00
|
|
|
if response.status_code not in (200, 201):
|
2016-02-09 06:24:11 +00:00
|
|
|
_LOGGER.exception(
|
2016-02-10 06:51:44 +00:00
|
|
|
"Error sending message. Response %d: %s:",
|
|
|
|
response.status_code, response.reason)
|