Feature/voluptuous influxdb (#3441)

* Migrate to voluptuous

* Fix voluptuous influxdb
pull/3443/head
Paulus Schoutsen 2016-09-18 15:32:18 -07:00 committed by GitHub
parent da8994e4b5
commit 9a87e5e336
2 changed files with 55 additions and 50 deletions

View File

@ -6,65 +6,71 @@ https://home-assistant.io/components/influxdb/
""" """
import logging import logging
import homeassistant.util as util import voluptuous as vol
from homeassistant.const import (EVENT_STATE_CHANGED, STATE_UNAVAILABLE,
STATE_UNKNOWN) from homeassistant.const import (
EVENT_STATE_CHANGED, STATE_UNAVAILABLE, STATE_UNKNOWN, CONF_HOST,
CONF_PORT, CONF_SSL, CONF_VERIFY_SSL, CONF_USERNAME, CONF_BLACKLIST,
CONF_PASSWORD, CONF_WHITELIST)
from homeassistant.helpers import state as state_helper from homeassistant.helpers import state as state_helper
from homeassistant.helpers import validate_config import homeassistant.helpers.config_validation as cv
_LOGGER = logging.getLogger(__name__)
DOMAIN = "influxdb"
DEPENDENCIES = []
DEFAULT_HOST = 'localhost'
DEFAULT_PORT = 8086
DEFAULT_DATABASE = 'home_assistant'
DEFAULT_SSL = False
DEFAULT_VERIFY_SSL = False
REQUIREMENTS = ['influxdb==3.0.0'] REQUIREMENTS = ['influxdb==3.0.0']
CONF_HOST = 'host' _LOGGER = logging.getLogger(__name__)
CONF_PORT = 'port'
CONF_DB_NAME = 'database' CONF_DB_NAME = 'database'
CONF_USERNAME = 'username'
CONF_PASSWORD = 'password'
CONF_SSL = 'ssl'
CONF_VERIFY_SSL = 'verify_ssl'
CONF_BLACKLIST = 'blacklist'
CONF_WHITELIST = 'whitelist'
CONF_TAGS = 'tags' CONF_TAGS = 'tags'
DEFAULT_DATABASE = 'home_assistant'
DEFAULT_HOST = 'localhost'
DEFAULT_PORT = 8086
DEFAULT_SSL = False
DEFAULT_VERIFY_SSL = False
DOMAIN = 'influxdb'
CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.Schema({
vol.Optional(CONF_HOST, default=DEFAULT_HOST): cv.string,
vol.Required(CONF_USERNAME): cv.string,
vol.Required(CONF_PASSWORD): cv.string,
vol.Optional(CONF_BLACKLIST, default=[]):
vol.All(cv.ensure_list, [cv.entity_id]),
vol.Optional(CONF_DB_NAME, default=DEFAULT_DATABASE): cv.string,
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
vol.Optional(CONF_PORT, default=False): cv.boolean,
vol.Optional(CONF_SSL, default=False): cv.boolean,
vol.Optional(CONF_TAGS, default={}):
vol.Schema({cv.string: cv.string}),
vol.Optional(CONF_WHITELIST, default=[]):
vol.All(cv.ensure_list, [cv.entity_id]),
vol.Optional(CONF_VERIFY_SSL, default=DEFAULT_VERIFY_SSL): cv.boolean,
}),
}, extra=vol.ALLOW_EXTRA)
# pylint: disable=too-many-locals # pylint: disable=too-many-locals
def setup(hass, config): def setup(hass, config):
"""Setup the InfluxDB component.""" """Setup the InfluxDB component."""
from influxdb import InfluxDBClient, exceptions from influxdb import InfluxDBClient, exceptions
if not validate_config(config, {DOMAIN: ['host',
CONF_USERNAME,
CONF_PASSWORD]}, _LOGGER):
return False
conf = config[DOMAIN] conf = config[DOMAIN]
host = conf[CONF_HOST] host = conf.get(CONF_HOST)
port = util.convert(conf.get(CONF_PORT), int, DEFAULT_PORT) port = conf.get(CONF_PORT)
database = util.convert(conf.get(CONF_DB_NAME), str, DEFAULT_DATABASE) database = conf.get(CONF_DB_NAME)
username = util.convert(conf.get(CONF_USERNAME), str) username = conf.get(CONF_USERNAME)
password = util.convert(conf.get(CONF_PASSWORD), str) password = conf.get(CONF_PASSWORD)
ssl = util.convert(conf.get(CONF_SSL), bool, DEFAULT_SSL) ssl = conf.get(CONF_SSL)
verify_ssl = util.convert(conf.get(CONF_VERIFY_SSL), bool, verify_ssl = conf.get(CONF_VERIFY_SSL)
DEFAULT_VERIFY_SSL) blacklist = conf.get(CONF_BLACKLIST)
blacklist = conf.get(CONF_BLACKLIST, []) whitelist = conf.get(CONF_WHITELIST)
whitelist = conf.get(CONF_WHITELIST, []) tags = conf.get(CONF_TAGS)
tags = conf.get(CONF_TAGS, {})
try: try:
influx = InfluxDBClient(host=host, port=port, username=username, influx = InfluxDBClient(
password=password, database=database, host=host, port=port, username=username, password=password,
ssl=ssl, verify_ssl=verify_ssl) database=database, ssl=ssl, verify_ssl=verify_ssl)
influx.query("select * from /.*/ LIMIT 1;") influx.query("select * from /.*/ LIMIT 1;")
except exceptions.InfluxDBClientError as exc: except exceptions.InfluxDBClientError as exc:
_LOGGER.error("Database host is not accessible due to '%s', please " _LOGGER.error("Database host is not accessible due to '%s', please "
@ -106,8 +112,7 @@ def setup(hass, config):
} }
] ]
for tag in tags: json_body[0]['tags'].update(tags)
json_body[0]['tags'][tag] = tags[tag]
try: try:
influx.write_points(json_body) influx.write_points(json_body)

View File

@ -5,6 +5,7 @@ from unittest import mock
import influxdb as influx_client import influxdb as influx_client
from homeassistant.bootstrap import setup_component
import homeassistant.components.influxdb as influxdb import homeassistant.components.influxdb as influxdb
from homeassistant.const import EVENT_STATE_CHANGED, STATE_OFF, STATE_ON from homeassistant.const import EVENT_STATE_CHANGED, STATE_OFF, STATE_ON
@ -31,7 +32,7 @@ class TestInfluxDB(unittest.TestCase):
'verify_ssl': 'False', 'verify_ssl': 'False',
} }
} }
self.assertTrue(influxdb.setup(self.hass, config)) assert setup_component(self.hass, influxdb.DOMAIN, config)
self.assertTrue(self.hass.bus.listen.called) self.assertTrue(self.hass.bus.listen.called)
self.assertEqual(EVENT_STATE_CHANGED, self.assertEqual(EVENT_STATE_CHANGED,
self.hass.bus.listen.call_args_list[0][0][0]) self.hass.bus.listen.call_args_list[0][0][0])
@ -46,7 +47,7 @@ class TestInfluxDB(unittest.TestCase):
'password': 'pass', 'password': 'pass',
} }
} }
self.assertTrue(influxdb.setup(self.hass, config)) assert setup_component(self.hass, influxdb.DOMAIN, config)
self.assertTrue(self.hass.bus.listen.called) self.assertTrue(self.hass.bus.listen.called)
self.assertEqual(EVENT_STATE_CHANGED, self.assertEqual(EVENT_STATE_CHANGED,
self.hass.bus.listen.call_args_list[0][0][0]) self.hass.bus.listen.call_args_list[0][0][0])
@ -55,7 +56,6 @@ class TestInfluxDB(unittest.TestCase):
"""Test the setup with missing keys.""" """Test the setup with missing keys."""
config = { config = {
'influxdb': { 'influxdb': {
'host': 'host',
'username': 'user', 'username': 'user',
'password': 'pass', 'password': 'pass',
} }
@ -63,7 +63,7 @@ class TestInfluxDB(unittest.TestCase):
for missing in config['influxdb'].keys(): for missing in config['influxdb'].keys():
config_copy = copy.deepcopy(config) config_copy = copy.deepcopy(config)
del config_copy['influxdb'][missing] del config_copy['influxdb'][missing]
self.assertFalse(influxdb.setup(self.hass, config_copy)) assert not setup_component(self.hass, influxdb.DOMAIN, config_copy)
def test_setup_query_fail(self, mock_client): def test_setup_query_fail(self, mock_client):
"""Test the setup for query failures.""" """Test the setup for query failures."""
@ -76,7 +76,7 @@ class TestInfluxDB(unittest.TestCase):
} }
mock_client.return_value.query.side_effect = \ mock_client.return_value.query.side_effect = \
influx_client.exceptions.InfluxDBClientError('fake') influx_client.exceptions.InfluxDBClientError('fake')
self.assertFalse(influxdb.setup(self.hass, config)) assert not setup_component(self.hass, influxdb.DOMAIN, config)
def _setup(self): def _setup(self):
"""Setup the client.""" """Setup the client."""
@ -88,7 +88,7 @@ class TestInfluxDB(unittest.TestCase):
'blacklist': ['fake.blacklisted'] 'blacklist': ['fake.blacklisted']
} }
} }
influxdb.setup(self.hass, config) assert setup_component(self.hass, influxdb.DOMAIN, config)
self.handler_method = self.hass.bus.listen.call_args_list[0][0][1] self.handler_method = self.hass.bus.listen.call_args_list[0][0][1]
def test_event_listener(self, mock_client): def test_event_listener(self, mock_client):