From 04723c13321b337a81133d0a4c32a746c994dcf5 Mon Sep 17 00:00:00 2001 From: Michael Heydon Date: Sat, 9 Dec 2017 22:02:40 +0800 Subject: [PATCH 1/4] Add ability to specify a sender in the clicksend notification --- homeassistant/components/notify/clicksend.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/notify/clicksend.py b/homeassistant/components/notify/clicksend.py index 543ce434a8dd6..44df9046e3626 100644 --- a/homeassistant/components/notify/clicksend.py +++ b/homeassistant/components/notify/clicksend.py @@ -14,7 +14,7 @@ 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_USERNAME, CONF_RECIPIENT, CONF_SENDER, CONTENT_TYPE_JSON) import homeassistant.helpers.config_validation as cv _LOGGER = logging.getLogger(__name__) @@ -27,6 +27,7 @@ vol.Required(CONF_USERNAME): cv.string, vol.Required(CONF_API_KEY): cv.string, vol.Required(CONF_RECIPIENT): cv.string, + vol.Optional(CONF_SENDER, default=""): cv.string, }) @@ -47,10 +48,14 @@ 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) + if (self.sender == ""): + self.sender = self.recipient + def send_message(self, message="", **kwargs): """Send a message to a user.""" - data = ({'messages': [{'source': 'hass.notify', 'from': self.recipient, + data = ({'messages': [{'source': 'hass.notify', 'from': self.sender, 'to': self.recipient, 'body': message}]}) api_url = "{}/sms/send".format(BASE_API_URL) From 16ca193c5f53ac62506422b42ea01ab41eebcd00 Mon Sep 17 00:00:00 2001 From: Michael Heydon Date: Sat, 9 Dec 2017 23:05:59 +0800 Subject: [PATCH 2/4] Style fixes --- homeassistant/components/notify/clicksend.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/notify/clicksend.py b/homeassistant/components/notify/clicksend.py index 44df9046e3626..df0903ba3f3c4 100644 --- a/homeassistant/components/notify/clicksend.py +++ b/homeassistant/components/notify/clicksend.py @@ -14,7 +14,8 @@ from homeassistant.components.notify import ( PLATFORM_SCHEMA, BaseNotificationService) from homeassistant.const import ( - CONF_API_KEY, CONF_USERNAME, CONF_RECIPIENT, CONF_SENDER, CONTENT_TYPE_JSON) + CONF_API_KEY, CONF_USERNAME, CONF_RECIPIENT, CONF_SENDER, + CONTENT_TYPE_JSON) import homeassistant.helpers.config_validation as cv _LOGGER = logging.getLogger(__name__) @@ -52,7 +53,6 @@ def __init__(self, config): if (self.sender == ""): self.sender = self.recipient - def send_message(self, message="", **kwargs): """Send a message to a user.""" data = ({'messages': [{'source': 'hass.notify', 'from': self.sender, From 400b5a9ccae977538bdace9dd2e3ef851da69d31 Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Thu, 21 Dec 2017 21:42:20 +0100 Subject: [PATCH 3/4] Fix remaining issue --- homeassistant/components/notify/clicksend.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/notify/clicksend.py b/homeassistant/components/notify/clicksend.py index df0903ba3f3c4..bb0ec5f13dbf4 100644 --- a/homeassistant/components/notify/clicksend.py +++ b/homeassistant/components/notify/clicksend.py @@ -28,7 +28,7 @@ vol.Required(CONF_USERNAME): cv.string, vol.Required(CONF_API_KEY): cv.string, vol.Required(CONF_RECIPIENT): cv.string, - vol.Optional(CONF_SENDER, default=""): cv.string, + vol.Optional(CONF_SENDER): cv.string, }) @@ -49,9 +49,7 @@ 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) - if (self.sender == ""): - self.sender = self.recipient + self.sender = config.get(CONF_SENDER, CONF_RECIPIENT) def send_message(self, message="", **kwargs): """Send a message to a user.""" From 9cee69c8efe4ae4ddd99da4405589d50c226746d Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Mon, 15 Jan 2018 17:50:10 +0100 Subject: [PATCH 4/4] Add sender validation --- homeassistant/components/notify/clicksend.py | 48 ++++++++++++++------ 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/homeassistant/components/notify/clicksend.py b/homeassistant/components/notify/clicksend.py index bb0ec5f13dbf4..2b2cb4e7f22c5 100644 --- a/homeassistant/components/notify/clicksend.py +++ b/homeassistant/components/notify/clicksend.py @@ -14,7 +14,7 @@ from homeassistant.components.notify import ( PLATFORM_SCHEMA, BaseNotificationService) from homeassistant.const import ( - CONF_API_KEY, CONF_USERNAME, CONF_RECIPIENT, CONF_SENDER, + CONF_API_KEY, CONF_RECIPIENT, CONF_SENDER, CONF_USERNAME, CONTENT_TYPE_JSON) import homeassistant.helpers.config_validation as cv @@ -24,16 +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, - vol.Optional(CONF_SENDER): 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) if _authenticate(config) is False: _LOGGER.exception("You are not authorized to access ClickSend") return None @@ -53,13 +64,22 @@ def __init__(self, config): def send_message(self, message="", **kwargs): """Send a message to a user.""" - data = ({'messages': [{'source': 'hass.notify', 'from': self.sender, - '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'] @@ -73,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