Skip to content

Add ClickSend notify service.#8135

Merged
fabaff merged 8 commits into
home-assistant:devfrom
ClickSend:dev
Jun 23, 2017
Merged

Add ClickSend notify service.#8135
fabaff merged 8 commits into
home-assistant:devfrom
ClickSend:dev

Conversation

@oozman
Copy link
Copy Markdown
Contributor

@oozman oozman commented Jun 21, 2017

The clicksend platform 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 username and api_key.

Checklist:

If user exposed functionality or configuration variables are added/changed:

  • Documentation added/updated in home-assistant.github.io. See: PR #2868

If the code communicates with devices, web services, or third-party tools:

  • Local tests with tox run successfully. Your PR cannot be merged unless tests pass
  • New files were added to .coveragerc.

@homeassistant
Copy link
Copy Markdown
Contributor

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
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (82 > 79 characters)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a timeout to the request.

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}]}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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):
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expected 2 blank lines, found 1

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])
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (104 > 79 characters)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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.

})

# Define service instance.
def get_service(hass, config, discovery_info=None):
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expected 2 blank lines, found 1

"""
Clicksend platform for notify component.
For more details about this platform, please refer to the documentation at
https://clicksend.com/help
Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
Contributor Author

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?

Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
Contributor Author

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.

# Set platform parameters.
CONF_API_URL = 'https://rest.clicksend.com/v3/sms/send'
CONF_USERNAME = 'username'
CONF_API_KEY = 'api_key'
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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])
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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.

"""Implementation of a notification service for the Twitter service."""

def __init__(self, username, api_key, to_no):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docstring is missing.

self.to_no = to_no

def send_message(self, message="", **kwargs):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dito

def send_message(self, message="", **kwargs):

# Send request.
auth = self.username + ':' + self.api_key
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use string formatting.

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}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a timeout to the request.

- 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.
def _authenticate(config):
"""Authenticate with ClickSend."""
api_url = '{}/account'.format(BASE_API_URL)
headers = {HTTP_HEADER_CONTENT_TYPE: CONTENT_TYPE_JSON}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

continuation line under-indented for visual indent


import voluptuous as vol

from homeassistant.const import (CONF_USERNAME, CONF_API_KEY, CONF_RECIPIENT,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'homeassistant.const.HTTP_BASIC_AUTHENTICATION' imported but unused

import logging
import requests
import json
import base64
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'base64' imported but unused

- Remove unused variables.
- Continuation line under-indented for visual indent.

resp = requests.get(api_url, headers=headers,
auth=(config.get(CONF_USERNAME),
config.get(CONF_API_KEY)), timeout=5)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

continuation line under-indented for visual indent

_LOGGER.error("Error %s : %s (Code %s)", resp.status_code,
response_msg, response_code)


Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blank line contains whitespace

import voluptuous as vol

from homeassistant.const import (CONF_USERNAME, CONF_API_KEY,
CONF_RECIPIENT,HTTP_HEADER_CONTENT_TYPE,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing whitespace after ','

- Format code based on PEP8.
- Remove unused base64 dependency.
- Fix: D205: 1 blank line required between summary line and description (found 0)
- Fix: standard import "import json" comes before "import requests"
- Add files to .coveragerc
@fabaff fabaff merged commit 746aae5 into home-assistant:dev Jun 23, 2017
@balloob balloob mentioned this pull request Jul 1, 2017
dethpickle pushed a commit to dethpickle/home-assistant that referenced this pull request Aug 18, 2017
* 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
@home-assistant home-assistant locked and limited conversation to collaborators Oct 20, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants