-
-
Notifications
You must be signed in to change notification settings - Fork 37.7k
Add ClickSend notify service. #8135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
9a07577
9f2ebb8
918ac7f
75eaf3d
55d0db0
0dc0ee2
80aa663
8e870b9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| """ | ||
| 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 dependencies. | ||
| import logging | ||
| import requests | ||
| import json | ||
| import base64 | ||
|
|
||
| import voluptuous as vol | ||
|
|
||
| from homeassistant.const import (CONF_USERNAME, CONF_API_KEY, CONF_RECIPIENT, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 'homeassistant.const.HTTP_BASIC_AUTHENTICATION' imported but unused |
||
| HTTP_HEADER_CONTENT_TYPE, CONTENT_TYPE_JSON, HTTP_BASIC_AUTHENTICATION) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. continuation line under-indented for visual indent |
||
| import homeassistant.helpers.config_validation as cv | ||
| from homeassistant.components.notify import ( | ||
| PLATFORM_SCHEMA, BaseNotificationService) | ||
|
|
||
| # Get logger instance. | ||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
| # Set platform parameters. | ||
| BASE_API_URL = 'https://rest.clicksend.com/v3' | ||
|
|
||
| # Validate parameter schema. | ||
| 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): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. expected 2 blank lines, found 1 |
||
| """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.""" | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Docstring is missing. |
||
| 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}]}) | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dito |
||
| headers = {HTTP_HEADER_CONTENT_TYPE: CONTENT_TYPE_JSON} | ||
|
|
||
| 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) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. continuation line under-indented for visual indent |
||
|
|
||
| obj = json.loads(resp.text) | ||
| response_msg = obj['response_msg'] | ||
| response_code = obj['response_code'] | ||
|
|
||
| # Display error when failed. | ||
| 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) | ||
| headers = {HTTP_HEADER_CONTENT_TYPE: CONTENT_TYPE_JSON} | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. local variable 'headers' is assigned to but never used |
||
|
|
||
| resp = requests.get(api_url, auth=(config.get(CONF_USERNAME), | ||
| config.get(CONF_API_KEY)), timeout=5) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. continuation line under-indented for visual indent |
||
|
|
||
| if resp.status_code != 200: | ||
| return False | ||
|
|
||
| return True | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'base64' imported but unused