core/homeassistant/components/notify/rest.py

79 lines
2.8 KiB
Python
Raw Normal View History

2016-02-09 06:24:11 +00:00
"""
REST platform for notify component.
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
from homeassistant.components.notify import (
2016-02-19 05:27:50 +00:00
ATTR_TARGET, ATTR_TITLE, DOMAIN, BaseNotificationService)
from homeassistant.helpers import validate_config
2016-02-09 06:24:11 +00:00
_LOGGER = logging.getLogger(__name__)
DEFAULT_METHOD = 'GET'
DEFAULT_MESSAGE_PARAM_NAME = 'message'
DEFAULT_TITLE_PARAM_NAME = None
DEFAULT_TARGET_PARAM_NAME = None
2016-02-09 06:24:11 +00:00
def get_service(hass, config):
2016-03-08 10:46:32 +00:00
"""Get the REST notification service."""
2016-02-09 06:24:11 +00:00
if not validate_config({DOMAIN: config},
{DOMAIN: ['resource', ]},
_LOGGER):
return None
method = config.get('method', DEFAULT_METHOD)
message_param_name = config.get('message_param_name',
DEFAULT_MESSAGE_PARAM_NAME)
title_param_name = config.get('title_param_name',
DEFAULT_TITLE_PARAM_NAME)
target_param_name = config.get('target_param_name',
DEFAULT_TARGET_PARAM_NAME)
2016-02-09 06:24:11 +00:00
return RestNotificationService(config['resource'], method,
message_param_name, title_param_name,
target_param_name)
2016-02-09 06:24:11 +00:00
2016-02-10 05:12:33 +00:00
# pylint: disable=too-few-public-methods, too-many-arguments
2016-02-09 06:24:11 +00:00
class RestNotificationService(BaseNotificationService):
2016-03-08 10:46:32 +00:00
"""Implement the notification service for REST."""
2016-02-09 06:24:11 +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()
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 = {
self._message_param_name: message
2016-02-09 06:24:11 +00:00
}
if self._title_param_name is not None:
2016-02-10 06:27:54 +00:00
data[self._title_param_name] = kwargs.get(ATTR_TITLE)
2016-02-09 06:24:11 +00:00
if self._target_param_name is not None:
data[self._target_param_name] = kwargs.get(ATTR_TARGET)
2016-02-10 06:27:54 +00:00
if self._method == 'POST':
response = requests.post(self._resource, data=data, timeout=10)
2016-02-10 06:27:54 +00:00
elif self._method == 'POST_JSON':
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)