From 746aae51ec79e545f08ddee23769e6e0b2020be0 Mon Sep 17 00:00:00 2001 From: Omar Usman Date: Sat, 24 Jun 2017 04:51:41 +0800 Subject: [PATCH] Add ClickSend notify service. (#8135) * Add ClickSend notify service. * PR #8135 changes. - Some code spacing fixes. - Add timeout to requests. - Change doc url. - Use const.py as much as possible. - Check credentials to determine if setup fails or not. - Add docstrings. - Use string formatting. * PR #8135 changes. - Remove unused variables. - Continuation line under-indented for visual indent. * PR #8135 changes. - Format code based on PEP8. * PR #8135 changes. - Remove unused base64 dependency. * PR #8135 changes. - Fix: D205: 1 blank line required between summary line and description (found 0) - Fix: standard import "import json" comes before "import requests" * PR #8135 changes. - Add files to .coveragerc * Remove obvious comments and set constant --- .coveragerc | 1 + homeassistant/components/notify/clicksend.py | 80 ++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 homeassistant/components/notify/clicksend.py diff --git a/.coveragerc b/.coveragerc index c57a58ea733f2..3e6b71c00e73f 100644 --- a/.coveragerc +++ b/.coveragerc @@ -342,6 +342,7 @@ omit = homeassistant/components/notify/aws_sns.py homeassistant/components/notify/aws_sqs.py homeassistant/components/notify/ciscospark.py + homeassistant/components/notify/clicksend.py homeassistant/components/notify/discord.py homeassistant/components/notify/facebook.py homeassistant/components/notify/free_mobile.py diff --git a/homeassistant/components/notify/clicksend.py b/homeassistant/components/notify/clicksend.py new file mode 100644 index 0000000000000..663f689a9752d --- /dev/null +++ b/homeassistant/components/notify/clicksend.py @@ -0,0 +1,80 @@ +""" +Clicksend platform for notify component. + +For more details about this platform, please refer to the documentation at +https://home-assistant.io/components/notify.clicksend/ +""" +import json +import logging +import requests + +import voluptuous as vol + +import homeassistant.helpers.config_validation as cv +from homeassistant.const import ( + CONF_USERNAME, CONF_API_KEY, CONF_RECIPIENT, HTTP_HEADER_CONTENT_TYPE, + CONTENT_TYPE_JSON) +from homeassistant.components.notify import ( + PLATFORM_SCHEMA, BaseNotificationService) + +_LOGGER = logging.getLogger(__name__) + +BASE_API_URL = 'https://rest.clicksend.com/v3' + +HEADERS = {HTTP_HEADER_CONTENT_TYPE: CONTENT_TYPE_JSON} + +PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ + vol.Required(CONF_USERNAME): cv.string, + vol.Required(CONF_API_KEY): cv.string, + vol.Required(CONF_RECIPIENT): cv.string, +}) + + +def get_service(hass, config, discovery_info=None): + """Get the ClickSend notification service.""" + if _authenticate(config) is False: + _LOGGER.exception("You are not authorized to access ClickSend") + return None + + return ClicksendNotificationService(config) + + +class ClicksendNotificationService(BaseNotificationService): + """Implementation of a notification service for the ClickSend service.""" + + def __init__(self, config): + """Initialize the service.""" + self.username = config.get(CONF_USERNAME) + self.api_key = config.get(CONF_API_KEY) + self.recipient = config.get(CONF_RECIPIENT) + + def send_message(self, message="", **kwargs): + """Send a message to a user.""" + data = ({'messages': [{'source': 'hass.notify', 'from': self.recipient, + 'to': self.recipient, 'body': message}]}) + + api_url = "{}/sms/send".format(BASE_API_URL) + + resp = requests.post(api_url, data=json.dumps(data), headers=HEADERS, + auth=(self.username, self.api_key), timeout=5) + + obj = json.loads(resp.text) + response_msg = obj['response_msg'] + response_code = obj['response_code'] + + if resp.status_code != 200: + _LOGGER.error("Error %s : %s (Code %s)", resp.status_code, + response_msg, response_code) + + +def _authenticate(config): + """Authenticate with ClickSend.""" + api_url = '{}/account'.format(BASE_API_URL) + resp = requests.get(api_url, headers=HEADERS, + auth=(config.get(CONF_USERNAME), + config.get(CONF_API_KEY)), timeout=5) + + if resp.status_code != 200: + return False + + return True