Skip to content
Merged
Changes from all commits
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
49 changes: 36 additions & 13 deletions homeassistant/components/notify/clicksend.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
from homeassistant.components.notify import (
PLATFORM_SCHEMA, BaseNotificationService)
from homeassistant.const import (
CONF_API_KEY, CONF_USERNAME, CONF_RECIPIENT, CONTENT_TYPE_JSON)
CONF_API_KEY, CONF_RECIPIENT, CONF_SENDER, CONF_USERNAME,
CONTENT_TYPE_JSON)
import homeassistant.helpers.config_validation as cv

_LOGGER = logging.getLogger(__name__)
Expand All @@ -23,15 +24,27 @@

HEADERS = {CONTENT_TYPE: CONTENT_TYPE_JSON}

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 validate_sender(config):
"""Set the optional sender name if sender name is not provided."""
if CONF_SENDER in config:
return config
config[CONF_SENDER] = config[CONF_RECIPIENT]
return config


PLATFORM_SCHEMA = vol.Schema(
vol.All(PLATFORM_SCHEMA.extend({
vol.Required(CONF_USERNAME): cv.string,
vol.Required(CONF_API_KEY): cv.string,
vol.Required(CONF_RECIPIENT): cv.string,
vol.Optional(CONF_SENDER): cv.string,
}), validate_sender))


def get_service(hass, config, discovery_info=None):
"""Get the ClickSend notification service."""
print("#### ", config)

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.

This was left.

if _authenticate(config) is False:
_LOGGER.exception("You are not authorized to access ClickSend")
return None
Expand All @@ -47,16 +60,26 @@ def __init__(self, config):
self.username = config.get(CONF_USERNAME)
self.api_key = config.get(CONF_API_KEY)
self.recipient = config.get(CONF_RECIPIENT)
self.sender = config.get(CONF_SENDER, CONF_RECIPIENT)

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.

It's better to add a schema validator that sets the default value during config validation.

def validate_sender(config):
    if CONF_SENDER in config:
        return config
    config[CONF_SENDER] = config[CONF_RECIPIENT]
    return config

Then use this validator like this in the schema:

PLATFORM_SCHEMA = vol.Schema(
    vol.All(PLATFORM_SCHEMA.extend({...}), validate_sender))


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}]})
data = ({
'messages': [
{
'source': 'hass.notify',
'from': self.sender,
'to': self.recipient,
'body': message,
}
]
})

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)
resp = requests.post(
api_url, data=json.dumps(data), headers=HEADERS,
auth=(self.username, self.api_key), timeout=5)

obj = json.loads(resp.text)
response_msg = obj['response_msg']
Expand All @@ -70,9 +93,9 @@ def send_message(self, message="", **kwargs):
def _authenticate(config):
"""Authenticate with ClickSend."""
api_url = '{}/account'.format(BASE_API_URL)
resp = requests.get(api_url, headers=HEADERS,
auth=(config.get(CONF_USERNAME),
config.get(CONF_API_KEY)), timeout=5)
resp = requests.get(
api_url, headers=HEADERS, auth=(config.get(CONF_USERNAME),
config.get(CONF_API_KEY)), timeout=5)

if resp.status_code != 200:
return False
Expand Down