Add ClickSend notify service.#8135
Conversation
|
Hi @OmarUsman, It seems you haven't yet signed a CLA. Please do so here. Once you do that we will be able to review and accept this pull request. Thanks! |
| # Display error when failed. | ||
| if resp.status_code != 200: | ||
| _LOGGER.error("Error %s : %s (Code %s)", resp.status_code, | ||
| response_msg, response_code) No newline at end of file |
| data = {'messages': [{'source': 'hass.notify', 'from': self.to_no, 'to': self.to_no, 'body': message}]} | ||
| headers = {'Content-type': 'application/json', 'Authorization': auth} | ||
|
|
||
| resp = requests.post(CONF_API_URL, data=json.dumps(data), headers=headers) |
There was a problem hiding this comment.
line too long (82 > 79 characters)
| auth = base64.b64encode(bytes(auth, 'utf-8')) | ||
| auth = 'Basic ' + auth.decode('utf-8') | ||
|
|
||
| data = {'messages': [{'source': 'hass.notify', 'from': self.to_no, 'to': self.to_no, 'body': message}]} |
There was a problem hiding this comment.
line too long (111 > 79 characters)
| return ClicksendNotificationService(config[CONF_USERNAME], config[CONF_API_KEY], config[CONF_TO_NO]) | ||
|
|
||
| # Implement the notification service. | ||
| class ClicksendNotificationService(BaseNotificationService): |
| def get_service(hass, config, discovery_info=None): | ||
|
|
||
| # Set notification service instance. | ||
| return ClicksendNotificationService(config[CONF_USERNAME], config[CONF_API_KEY], config[CONF_TO_NO]) |
There was a problem hiding this comment.
line too long (104 > 79 characters)
There was a problem hiding this comment.
Would be nice if you could check if the credentials are valid and make setup fails if not. Otherwise the users end-up with a non-working platform in their setup.
| }) | ||
|
|
||
| # Define service instance. | ||
| def get_service(hass, config, discovery_info=None): |
| """ | ||
| Clicksend platform for notify component. | ||
| For more details about this platform, please refer to the documentation at | ||
| https://clicksend.com/help |
There was a problem hiding this comment.
Please point to the documentation at home-assistant.io.
There was a problem hiding this comment.
Is this value fine: https://home-assistant.io/docs/
Because I haven't made a specific link for the documentation yet?
There was a problem hiding this comment.
The link will be https://home-assistant.io/components/notify.clicksend/ for the platform documentation.
If the configuration is not passing the validation then a log entry is created which is composed on-the-fly and takes the platform name into account.
There was a problem hiding this comment.
Thanks just made the changes.
| # Set platform parameters. | ||
| CONF_API_URL = 'https://rest.clicksend.com/v3/sms/send' | ||
| CONF_USERNAME = 'username' | ||
| CONF_API_KEY = 'api_key' |
There was a problem hiding this comment.
Please use the constants defined in const.py.
| def get_service(hass, config, discovery_info=None): | ||
|
|
||
| # Set notification service instance. | ||
| return ClicksendNotificationService(config[CONF_USERNAME], config[CONF_API_KEY], config[CONF_TO_NO]) |
There was a problem hiding this comment.
Would be nice if you could check if the credentials are valid and make setup fails if not. Otherwise the users end-up with a non-working platform in their setup.
| """Implementation of a notification service for the Twitter service.""" | ||
|
|
||
| def __init__(self, username, api_key, to_no): | ||
|
|
| self.to_no = to_no | ||
|
|
||
| def send_message(self, message="", **kwargs): | ||
|
|
| def send_message(self, message="", **kwargs): | ||
|
|
||
| # Send request. | ||
| auth = self.username + ':' + self.api_key |
| auth = 'Basic ' + auth.decode('utf-8') | ||
|
|
||
| data = {'messages': [{'source': 'hass.notify', 'from': self.to_no, 'to': self.to_no, 'body': message}]} | ||
| headers = {'Content-type': 'application/json', 'Authorization': auth} |
There was a problem hiding this comment.
As far as I remember are there constants available for Content-type and application/json.
| data = {'messages': [{'source': 'hass.notify', 'from': self.to_no, 'to': self.to_no, 'body': message}]} | ||
| headers = {'Content-type': 'application/json', 'Authorization': auth} | ||
|
|
||
| resp = requests.post(CONF_API_URL, data=json.dumps(data), headers=headers) |
| 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.
local variable 'headers' is assigned to but never used
| 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.
continuation line under-indented for visual indent
| import voluptuous as vol | ||
|
|
||
| from homeassistant.const import (CONF_USERNAME, CONF_API_KEY, CONF_RECIPIENT, | ||
| HTTP_HEADER_CONTENT_TYPE, CONTENT_TYPE_JSON, HTTP_BASIC_AUTHENTICATION) |
There was a problem hiding this comment.
continuation line under-indented for visual indent
|
|
||
| import voluptuous as vol | ||
|
|
||
| from homeassistant.const import (CONF_USERNAME, CONF_API_KEY, CONF_RECIPIENT, |
There was a problem hiding this comment.
'homeassistant.const.HTTP_BASIC_AUTHENTICATION' imported but unused
| import logging | ||
| import requests | ||
| import json | ||
| import base64 |
|
|
||
| resp = requests.get(api_url, headers=headers, | ||
| auth=(config.get(CONF_USERNAME), | ||
| config.get(CONF_API_KEY)), timeout=5) |
There was a problem hiding this comment.
continuation line under-indented for visual indent
| _LOGGER.error("Error %s : %s (Code %s)", resp.status_code, | ||
| response_msg, response_code) | ||
|
|
||
|
|
| import voluptuous as vol | ||
|
|
||
| from homeassistant.const import (CONF_USERNAME, CONF_API_KEY, | ||
| CONF_RECIPIENT,HTTP_HEADER_CONTENT_TYPE, |
* Add ClickSend notify service. * PR home-assistant#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 home-assistant#8135 changes. - Remove unused variables. - Continuation line under-indented for visual indent. * PR home-assistant#8135 changes. - Format code based on PEP8. * PR home-assistant#8135 changes. - Remove unused base64 dependency. * PR home-assistant#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 home-assistant#8135 changes. - Add files to .coveragerc * Remove obvious comments and set constant
The
clicksendplatform uses ClickSend to deliver notifications from Home Assistant.Get your ClickSend API Credentials
Go to your ClickSend Dashboard section and create your new project. After creating your project, you should now be able to obtain your
usernameandapi_key.Checklist:
If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools: