Use voluptuous for message_bird, sendgrid (#3136)
This commit is contained in:
1 parent
7bab4055a5
commit
e460d8f637
5 files changed
+31
-42
No files matched your search
@@ -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
|
||||
|
||||
Reference in new issue
Block a user