From a855a9bfcdf3c95acbb8023f2eb88fa3af1ec7ed Mon Sep 17 00:00:00 2001 From: Florian Holzapfel Date: Tue, 15 Mar 2016 18:09:19 +0100 Subject: [PATCH 1/2] provide sms notifications via messagebird --- .coveragerc | 1 + .../components/notify/message_bird.py | 77 +++++++++++++++++++ requirements_all.txt | 3 + 3 files changed, 81 insertions(+) create mode 100644 homeassistant/components/notify/message_bird.py diff --git a/.coveragerc b/.coveragerc index a885c5a28a6..4eedf4f0ec2 100644 --- a/.coveragerc +++ b/.coveragerc @@ -109,6 +109,7 @@ omit = homeassistant/components/notify/free_mobile.py homeassistant/components/notify/googlevoice.py homeassistant/components/notify/instapush.py + homeassistant/components/notify/message_bird.py homeassistant/components/notify/nma.py homeassistant/components/notify/pushbullet.py homeassistant/components/notify/pushetta.py diff --git a/homeassistant/components/notify/message_bird.py b/homeassistant/components/notify/message_bird.py new file mode 100644 index 00000000000..6ac1ae6480f --- /dev/null +++ b/homeassistant/components/notify/message_bird.py @@ -0,0 +1,77 @@ +""" +MessageBird platform for notify component. + +For more details about this platform, please refer to the documentation at +https://home-assistant.io/components/notify.message_bird/ +""" +import logging + +from homeassistant.components.notify import ( + ATTR_TARGET, BaseNotificationService) +from homeassistant.const import CONF_API_KEY + +CONF_SENDER = 'sender' + +_LOGGER = logging.getLogger(__name__) +REQUIREMENTS = ['messagebird==1.1.1'] + + +def is_valid_sender(sender): + """Test if the sender config option is valid.""" + length = len(sender) + if length > 1: + if sender[0] == '+': + return sender[1:].isdigit() + elif length <= 11: + return sender.isalpha() + return False + + +# pylint: disable=unused-argument +def get_service(hass, config): + """Get the MessageBird notification service.""" + from messagebird import Client + + if CONF_API_KEY not in config: + _LOGGER.error("Unable to find config key '%s'", CONF_API_KEY) + return None + + sender = config.get(CONF_SENDER, 'HA') + if not is_valid_sender(sender): + _LOGGER.error('Sender is invalid: It must be a phone number or ' + + 'a string not longer than 11 characters.') + return None + + return MessageBirdNotificationService(sender, Client(config[CONF_API_KEY])) + + +# pylint: disable=too-few-public-methods +class MessageBirdNotificationService(BaseNotificationService): + """Implement the notification service for MessageBird.""" + + def __init__(self, sender, client): + """Initialize the service.""" + self.sender = sender + self.client = client + + def send_message(self, message=None, **kwargs): + """Send a message to a specified target.""" + from messagebird.client import ErrorException + + targets = kwargs.get(ATTR_TARGET) + if not targets: + _LOGGER.error('No target specified.') + return + + if not isinstance(targets, list): + targets = [targets] + + for target in targets: + try: + self.client.message_create(self.sender, + target, + message, + {'reference': 'HA'}) + except ErrorException as exception: + _LOGGER.error('Failed to notify %s: %s', target, exception) + continue diff --git a/requirements_all.txt b/requirements_all.txt index ffb53a3ac4b..a37e8de402d 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -114,6 +114,9 @@ liffylights==0.9.4 # homeassistant.components.light.limitlessled limitlessled==1.0.0 +# homeassistant.components.notify.message_bird +messagebird==1.1.1 + # homeassistant.components.sensor.mfi # homeassistant.components.switch.mfi mficlient==0.3.0 From 6e32d99174a9010153485d8b18f96bb2b70cdef3 Mon Sep 17 00:00:00 2001 From: Florian Holzapfel Date: Wed, 16 Mar 2016 10:32:02 +0100 Subject: [PATCH 2/2] provide update message_bird.py to include the suggested changes --- .../components/notify/message_bird.py | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/notify/message_bird.py b/homeassistant/components/notify/message_bird.py index 6ac1ae6480f..f2c8f042c5d 100644 --- a/homeassistant/components/notify/message_bird.py +++ b/homeassistant/components/notify/message_bird.py @@ -7,8 +7,9 @@ https://home-assistant.io/components/notify.message_bird/ import logging from homeassistant.components.notify import ( - ATTR_TARGET, BaseNotificationService) + ATTR_TARGET, DOMAIN, BaseNotificationService) from homeassistant.const import CONF_API_KEY +from homeassistant.helpers import validate_config CONF_SENDER = 'sender' @@ -30,19 +31,28 @@ def is_valid_sender(sender): # pylint: disable=unused-argument def get_service(hass, config): """Get the MessageBird notification service.""" - from messagebird import Client + import messagebird - if CONF_API_KEY not in config: - _LOGGER.error("Unable to find config key '%s'", CONF_API_KEY) + if not validate_config({DOMAIN: config}, + {DOMAIN: [CONF_API_KEY]}, + _LOGGER): return None sender = config.get(CONF_SENDER, 'HA') if not is_valid_sender(sender): - _LOGGER.error('Sender is invalid: It must be a phone number or ' + + _LOGGER.error('Sender is invalid: It must be a phone number or ' 'a string not longer than 11 characters.') return None - return MessageBirdNotificationService(sender, Client(config[CONF_API_KEY])) + client = messagebird.Client(config[CONF_API_KEY]) + try: + # validates the api key + client.balance() + except messagebird.client.ErrorException: + _LOGGER.error('The specified MessageBird API key is invalid.') + return None + + return MessageBirdNotificationService(sender, client) # pylint: disable=too-few-public-methods