-
-
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 1 commit
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,71 @@ | ||
| """ | ||
| Clicksend platform for notify component. | ||
| For more details about this platform, please refer to the documentation at | ||
| https://clicksend.com/help | ||
| """ | ||
|
|
||
| # Import dependencies. | ||
| import logging | ||
| import requests | ||
| import json | ||
| import base64 | ||
|
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. 'base64' imported but unused |
||
|
|
||
| import voluptuous as vol | ||
|
|
||
| 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. | ||
| CONF_API_URL = 'https://rest.clicksend.com/v3/sms/send' | ||
| CONF_USERNAME = 'username' | ||
| CONF_API_KEY = 'api_key' | ||
|
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. Please use the constants defined in |
||
| CONF_TO_NO = 'to_no' | ||
|
|
||
| # Validate parameter schema. | ||
| PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ | ||
| vol.Required(CONF_USERNAME): cv.string, | ||
| vol.Required(CONF_API_KEY): cv.string, | ||
| vol.Required(CONF_TO_NO): cv.string, | ||
| }) | ||
|
|
||
| # Define service instance. | ||
| 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 |
||
|
|
||
| # Set notification service instance. | ||
| return ClicksendNotificationService(config[CONF_USERNAME], config[CONF_API_KEY], config[CONF_TO_NO]) | ||
|
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. line too long (104 > 79 characters)
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. 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. |
||
|
|
||
| # Implement the notification service. | ||
| class ClicksendNotificationService(BaseNotificationService): | ||
|
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 |
||
| """Implementation of a notification service for the Twitter service.""" | ||
|
|
||
| def __init__(self, username, api_key, to_no): | ||
|
|
||
|
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. |
||
| # Set variables. | ||
| self.username = username | ||
| self.api_key = api_key | ||
| self.to_no = to_no | ||
|
|
||
| def send_message(self, message="", **kwargs): | ||
|
|
||
|
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 |
||
| # Send request. | ||
| auth = self.username + ':' + self.api_key | ||
|
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. Please use string formatting. |
||
| 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. line too long (111 > 79 characters) |
||
| headers = {'Content-type': 'application/json', 'Authorization': auth} | ||
|
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. As far as I remember are there constants available for |
||
|
|
||
| resp = requests.post(CONF_API_URL, data=json.dumps(data), headers=headers) | ||
|
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. line too long (82 > 79 characters)
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. Add a timeout to the request. |
||
|
|
||
| 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) | ||
|
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. no newline at end of file |
||
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.
Please point to the documentation at home-assistant.io.
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks just made the changes.