Use voluptuous for message_bird, sendgrid (#3136)

This commit is contained in:
Pascal Vizeli 2016-09-05 07:07:31 +02:00 committed by Fabian Affolter
parent 7bab4055a5
commit e460d8f637
5 changed files with 31 additions and 42 deletions

View file

@ -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

View file

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

View file

@ -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'

View file

@ -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({

View file

@ -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'