Skip to content

Commit

Permalink
Add REST notify component
Browse files Browse the repository at this point in the history
  • Loading branch information
Theb-1 committed Feb 9, 2016
1 parent 23c5159 commit 0f5487b
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
63 changes: 63 additions & 0 deletions homeassistant/components/notify/rest.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 0f5487b

Please sign in to comment.