From e460d8f637914f55b074ec6e453e93c46d50cfbf Mon Sep 17 00:00:00 2001 From: Pascal Vizeli Date: Mon, 5 Sep 2016 07:07:31 +0200 Subject: [PATCH] Use voluptuous for message_bird, sendgrid (#3136) --- .../components/notify/message_bird.py | 37 ++++++------------- homeassistant/components/notify/sendgrid.py | 25 ++++++++----- homeassistant/components/notify/smtp.py | 5 +-- homeassistant/components/notify/xmpp.py | 4 +- homeassistant/const.py | 2 + 5 files changed, 31 insertions(+), 42 deletions(-) diff --git a/homeassistant/components/notify/message_bird.py b/homeassistant/components/notify/message_bird.py index 86bcfe79cd0..2e7f2f9bb07 100644 --- a/homeassistant/components/notify/message_bird.py +++ b/homeassistant/components/notify/message_bird.py @@ -6,26 +6,22 @@ https://home-assistant.io/components/notify.message_bird/ """ import logging -from homeassistant.components.notify import ( - ATTR_TARGET, DOMAIN, BaseNotificationService) -from homeassistant.const import CONF_API_KEY -from homeassistant.helpers import validate_config +import voluptuous as vol -CONF_SENDER = 'sender' +import homeassistant.helpers.config_validation as cv +from homeassistant.components.notify import ( + ATTR_TARGET, PLATFORM_SCHEMA, BaseNotificationService) +from homeassistant.const import CONF_API_KEY, CONF_SENDER _LOGGER = logging.getLogger(__name__) REQUIREMENTS = ['messagebird==1.2.0'] -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 +PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ + vol.Required(CONF_API_KEY): cv.string, + vol.Optional(CONF_SENDER, default='HA'): + vol.Match(r"^(\+?[1-9]\d{1,14}|\w{1,11})$"), +}) # pylint: disable=unused-argument @@ -33,17 +29,6 @@ def get_service(hass, config): """Get the MessageBird notification service.""" import messagebird - 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 ' - 'a string not longer than 11 characters.') - return None - client = messagebird.Client(config[CONF_API_KEY]) try: # validates the api key @@ -52,7 +37,7 @@ def get_service(hass, config): _LOGGER.error('The specified MessageBird API key is invalid.') return None - return MessageBirdNotificationService(sender, client) + return MessageBirdNotificationService(config.get(CONF_SENDER), client) # pylint: disable=too-few-public-methods diff --git a/homeassistant/components/notify/sendgrid.py b/homeassistant/components/notify/sendgrid.py index b0805338844..ac249dc2c97 100644 --- a/homeassistant/components/notify/sendgrid.py +++ b/homeassistant/components/notify/sendgrid.py @@ -6,24 +6,29 @@ https://home-assistant.io/components/notify.sendgrid/ """ import logging +import voluptuous as vol + +import homeassistant.helpers.config_validation as cv from homeassistant.components.notify import ( - ATTR_TITLE, ATTR_TITLE_DEFAULT, DOMAIN, BaseNotificationService) -from homeassistant.helpers import validate_config + ATTR_TITLE, ATTR_TITLE_DEFAULT, PLATFORM_SCHEMA, BaseNotificationService) +from homeassistant.const import CONF_API_KEY, CONF_SENDER, CONF_RECIPIENT REQUIREMENTS = ['sendgrid==3.2.10'] _LOGGER = logging.getLogger(__name__) +PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ + vol.Required(CONF_API_KEY): cv.string, + vol.Required(CONF_SENDER): vol.Email, + vol.Required(CONF_RECIPIENT): cv.string, +}) + + def get_service(hass, config): """Get the SendGrid notification service.""" - if not validate_config({DOMAIN: config}, - {DOMAIN: ['api_key', 'sender', 'recipient']}, - _LOGGER): - return None - - api_key = config['api_key'] - sender = config['sender'] - recipient = config['recipient'] + api_key = config[CONF_API_KEY] + sender = config[CONF_SENDER] + recipient = config[CONF_RECIPIENT] return SendgridNotificationService(api_key, sender, recipient) diff --git a/homeassistant/components/notify/smtp.py b/homeassistant/components/notify/smtp.py index 694058a11ce..8b6ca76a235 100644 --- a/homeassistant/components/notify/smtp.py +++ b/homeassistant/components/notify/smtp.py @@ -16,15 +16,14 @@ import homeassistant.helpers.config_validation as cv from homeassistant.components.notify import ( ATTR_TITLE, ATTR_TITLE_DEFAULT, ATTR_DATA, PLATFORM_SCHEMA, BaseNotificationService) -from homeassistant.const import (CONF_USERNAME, CONF_PASSWORD, CONF_PORT) +from homeassistant.const import (CONF_USERNAME, CONF_PASSWORD, CONF_PORT, + CONF_SENDER, CONF_RECIPIENT) _LOGGER = logging.getLogger(__name__) ATTR_IMAGES = 'images' # optional embedded image file attachments CONF_STARTTLS = 'starttls' -CONF_SENDER = 'sender' -CONF_RECIPIENT = 'recipient' CONF_DEBUG = 'debug' CONF_SERVER = 'server' diff --git a/homeassistant/components/notify/xmpp.py b/homeassistant/components/notify/xmpp.py index 35157a9bd46..ed64a8b4e07 100644 --- a/homeassistant/components/notify/xmpp.py +++ b/homeassistant/components/notify/xmpp.py @@ -11,7 +11,7 @@ import voluptuous as vol import homeassistant.helpers.config_validation as cv from homeassistant.components.notify import ( ATTR_TITLE, ATTR_TITLE_DEFAULT, PLATFORM_SCHEMA, BaseNotificationService) -from homeassistant.const import CONF_PASSWORD +from homeassistant.const import CONF_PASSWORD, CONF_SENDER, CONF_RECIPIENT REQUIREMENTS = ['sleekxmpp==1.3.1', 'dnspython3==1.12.0', @@ -19,8 +19,6 @@ REQUIREMENTS = ['sleekxmpp==1.3.1', 'pyasn1-modules==0.0.8'] -CONF_SENDER = 'sender' -CONF_RECIPIENT = 'recipient' CONF_TLS = 'tls' PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ diff --git a/homeassistant/const.py b/homeassistant/const.py index 311c1a5f166..a07316711d1 100644 --- a/homeassistant/const.py +++ b/homeassistant/const.py @@ -73,9 +73,11 @@ CONF_PIN = 'pin' CONF_PLATFORM = 'platform' CONF_PORT = 'port' CONF_PREFIX = 'prefix' +CONF_RECIPIENT = 'recipient' CONF_RESOURCE = 'resource' CONF_RESOURCES = 'resources' CONF_SCAN_INTERVAL = 'scan_interval' +CONF_SENDER = 'sender' CONF_SENSOR_CLASS = 'sensor_class' CONF_SENSORS = 'sensors' CONF_SSL = 'ssl'