Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions homeassistant/components/notify/clicksend.py
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

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.

"""

# Import dependencies.
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


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'

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.

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):

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


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


# 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

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

# Set variables.
self.username = username
self.api_key = api_key
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

# 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 = 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)

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.


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.


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)

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