From 0f5487b95a77568308d94c0162e9683278f516bd Mon Sep 17 00:00:00 2001 From: Theb-1 Date: Mon, 8 Feb 2016 22:24:11 -0800 Subject: [PATCH 1/5] Add REST notify component --- .coveragerc | 1 + homeassistant/components/notify/rest.py | 63 +++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 homeassistant/components/notify/rest.py diff --git a/.coveragerc b/.coveragerc index 8fefa8a44b8..3e24b33718c 100644 --- a/.coveragerc +++ b/.coveragerc @@ -106,6 +106,7 @@ omit = homeassistant/components/notify/pushbullet.py homeassistant/components/notify/pushetta.py homeassistant/components/notify/pushover.py + homeassistant/components/notify/rest.py homeassistant/components/notify/slack.py homeassistant/components/notify/smtp.py homeassistant/components/notify/syslog.py diff --git a/homeassistant/components/notify/rest.py b/homeassistant/components/notify/rest.py new file mode 100644 index 00000000000..10b7f067f63 --- /dev/null +++ b/homeassistant/components/notify/rest.py @@ -0,0 +1,63 @@ +""" +homeassistant.components.notify.rest +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +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 +import requests + +from homeassistant.helpers import validate_config +from homeassistant.components.notify import ( + DOMAIN, BaseNotificationService) + +_LOGGER = logging.getLogger(__name__) + +DEFAULT_METHOD = 'GET' +DEFAULT_PARAM_NAME = 'message' + + +def get_service(hass, config): + """ Get the REST notification service. """ + + if not validate_config({DOMAIN: config}, + {DOMAIN: ['resource', ]}, + _LOGGER): + return None + + method = config.get('method', DEFAULT_METHOD) + param_name = config.get('param_name', DEFAULT_PARAM_NAME) + + return RestNotificationService(config['resource'], method, param_name) + + +# pylint: disable=too-few-public-methods +class RestNotificationService(BaseNotificationService): + """ Implements notification service for REST. """ + + def __init__(self, resource, method=DEFAULT_METHOD, + param_name=DEFAULT_PARAM_NAME): + self._resource = resource + self._method = method + self.param_name = param_name + + def send_message(self, message="", **kwargs): + """ Send a message to a user. """ + + data = { + self.param_name: message + } + + if self._method.upper() == 'GET': + response = requests.get(self._resource, params=data) + elif self._method.upper() == 'POST': + response = requests.post(self._resource, data=data) + elif self._method.upper() == 'JSON': + response = requests.post(self._resource, json=data) + + if (response.status_code != requests.codes.ok and + response.status_code != requests.codes.created): + _LOGGER.exception( + "Error sending message. Response: %s", response.reason) From b7ff79da24e7392e877b84a8ea15be074323b2b2 Mon Sep 17 00:00:00 2001 From: Theb-1 Date: Tue, 9 Feb 2016 20:48:17 -0800 Subject: [PATCH 2/5] Fixes and updates Remove setting defaults twice Add timeout Add optional title and target --- homeassistant/components/notify/rest.py | 50 ++++++++++++++++--------- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/homeassistant/components/notify/rest.py b/homeassistant/components/notify/rest.py index 10b7f067f63..2dfeeb311a4 100644 --- a/homeassistant/components/notify/rest.py +++ b/homeassistant/components/notify/rest.py @@ -11,12 +11,14 @@ import requests from homeassistant.helpers import validate_config from homeassistant.components.notify import ( - DOMAIN, BaseNotificationService) + DOMAIN, ATTR_TITLE, ATTR_TARGET, BaseNotificationService) _LOGGER = logging.getLogger(__name__) DEFAULT_METHOD = 'GET' -DEFAULT_PARAM_NAME = 'message' +DEFAULT_MESSAGE_PARAM_NAME = 'message' +DEFAULT_TITLE_PARAM_NAME = None +DEFAULT_TARGET_PARAM_NAME = None def get_service(hass, config): @@ -28,36 +30,50 @@ def get_service(hass, config): return None method = config.get('method', DEFAULT_METHOD) - param_name = config.get('param_name', DEFAULT_PARAM_NAME) + 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) - return RestNotificationService(config['resource'], method, param_name) + return RestNotificationService(config['resource'], method, + message_param_name, title_param_name, + target_param_name) -# pylint: disable=too-few-public-methods +# pylint: disable=too-few-public-methods, disable-msg=too-many-arguments class RestNotificationService(BaseNotificationService): """ Implements notification service for REST. """ - def __init__(self, resource, method=DEFAULT_METHOD, - param_name=DEFAULT_PARAM_NAME): + def __init__(self, resource, method, message_param_name, + title_param_name, target_param_name): self._resource = resource self._method = method - self.param_name = param_name + self._message_param_name = message_param_name + self._title_param_name = title_param_name + self._target_param_name = target_param_name def send_message(self, message="", **kwargs): """ Send a message to a user. """ data = { - self.param_name: message + self._message_param_name: message } - if self._method.upper() == 'GET': - response = requests.get(self._resource, params=data) - elif self._method.upper() == 'POST': - response = requests.post(self._resource, data=data) - elif self._method.upper() == 'JSON': - response = requests.post(self._resource, json=data) + if self._title_param_name is not None: + data.update({self._title_param_name: kwargs.get(ATTR_TITLE)}) - if (response.status_code != requests.codes.ok and - response.status_code != requests.codes.created): + if self._target_param_name is not None: + data.update({self._title_param_name: kwargs.get(ATTR_TARGET)}) + + if self._method.upper() == 'POST': + response = requests.post(self._resource, data=data, timeout=10) + elif self._method.upper() == 'POST_JSON': + response = requests.post(self._resource, json=data, timeout=10) + else: # default GET + response = requests.get(self._resource, params=data, timeout=10) + + if response.status_code != 200 and response.status_code != 201: _LOGGER.exception( "Error sending message. Response: %s", response.reason) From b3a1491482ee8f4915a5513b527ab9fb6183fce4 Mon Sep 17 00:00:00 2001 From: Theb-1 Date: Tue, 9 Feb 2016 21:12:33 -0800 Subject: [PATCH 3/5] pylint: ignore 'too-many-arguments' --- homeassistant/components/notify/rest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/notify/rest.py b/homeassistant/components/notify/rest.py index 2dfeeb311a4..3bc724ab1d4 100644 --- a/homeassistant/components/notify/rest.py +++ b/homeassistant/components/notify/rest.py @@ -42,7 +42,7 @@ def get_service(hass, config): target_param_name) -# pylint: disable=too-few-public-methods, disable-msg=too-many-arguments +# pylint: disable=too-few-public-methods, too-many-arguments class RestNotificationService(BaseNotificationService): """ Implements notification service for REST. """ From c5c4085ad4b5119b5b1c07c14ddaf91a2e6bd50d Mon Sep 17 00:00:00 2001 From: Theb-1 Date: Tue, 9 Feb 2016 22:27:54 -0800 Subject: [PATCH 4/5] upper/arg list/response improvements --- homeassistant/components/notify/rest.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/notify/rest.py b/homeassistant/components/notify/rest.py index 3bc724ab1d4..e78b0740482 100644 --- a/homeassistant/components/notify/rest.py +++ b/homeassistant/components/notify/rest.py @@ -49,7 +49,7 @@ class RestNotificationService(BaseNotificationService): def __init__(self, resource, method, message_param_name, title_param_name, target_param_name): self._resource = resource - self._method = method + 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 @@ -62,18 +62,18 @@ class RestNotificationService(BaseNotificationService): } if self._title_param_name is not None: - data.update({self._title_param_name: kwargs.get(ATTR_TITLE)}) + data[self._title_param_name] = kwargs.get(ATTR_TITLE) if self._target_param_name is not None: - data.update({self._title_param_name: kwargs.get(ATTR_TARGET)}) + data[self._title_param_name] = kwargs.get(ATTR_TARGET) - if self._method.upper() == 'POST': + if self._method == 'POST': response = requests.post(self._resource, data=data, timeout=10) - elif self._method.upper() == 'POST_JSON': + 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) - if response.status_code != 200 and response.status_code != 201: + if response.status_code not in (200, 201): _LOGGER.exception( - "Error sending message. Response: %s", response.reason) + "Error sending message. Response %d: %s:", response.status_code, response.reason) From d796625098f1e68c6361d4f1cc05efef2ae0a975 Mon Sep 17 00:00:00 2001 From: Theb-1 Date: Tue, 9 Feb 2016 22:51:44 -0800 Subject: [PATCH 5/5] flake8 fix --- homeassistant/components/notify/rest.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/notify/rest.py b/homeassistant/components/notify/rest.py index e78b0740482..e0bb50216bd 100644 --- a/homeassistant/components/notify/rest.py +++ b/homeassistant/components/notify/rest.py @@ -76,4 +76,5 @@ class RestNotificationService(BaseNotificationService): if response.status_code not in (200, 201): _LOGGER.exception( - "Error sending message. Response %d: %s:", response.status_code, response.reason) + "Error sending message. Response %d: %s:", + response.status_code, response.reason)