From 9a87e5e336ca6846656de178a81f734818bce9ba Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sun, 18 Sep 2016 15:32:18 -0700 Subject: [PATCH] Feature/voluptuous influxdb (#3441) * Migrate to voluptuous * Fix voluptuous influxdb --- homeassistant/components/influxdb.py | 93 ++++++++++--------- .../{test_influx.py => test_influxdb.py} | 12 +-- 2 files changed, 55 insertions(+), 50 deletions(-) rename tests/components/{test_influx.py => test_influxdb.py} (95%) diff --git a/homeassistant/components/influxdb.py b/homeassistant/components/influxdb.py index ca2ba5d2bfb..dc790cd54b0 100644 --- a/homeassistant/components/influxdb.py +++ b/homeassistant/components/influxdb.py @@ -6,65 +6,71 @@ https://home-assistant.io/components/influxdb/ """ import logging -import homeassistant.util as util -from homeassistant.const import (EVENT_STATE_CHANGED, STATE_UNAVAILABLE, - STATE_UNKNOWN) +import voluptuous as vol + +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 validate_config - -_LOGGER = logging.getLogger(__name__) - -DOMAIN = "influxdb" -DEPENDENCIES = [] - -DEFAULT_HOST = 'localhost' -DEFAULT_PORT = 8086 -DEFAULT_DATABASE = 'home_assistant' -DEFAULT_SSL = False -DEFAULT_VERIFY_SSL = False +import homeassistant.helpers.config_validation as cv REQUIREMENTS = ['influxdb==3.0.0'] -CONF_HOST = 'host' -CONF_PORT = 'port' +_LOGGER = logging.getLogger(__name__) + 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' +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 def setup(hass, config): """Setup the InfluxDB component.""" from influxdb import InfluxDBClient, exceptions - if not validate_config(config, {DOMAIN: ['host', - CONF_USERNAME, - CONF_PASSWORD]}, _LOGGER): - return False - conf = config[DOMAIN] - host = conf[CONF_HOST] - port = util.convert(conf.get(CONF_PORT), int, DEFAULT_PORT) - database = util.convert(conf.get(CONF_DB_NAME), str, DEFAULT_DATABASE) - username = util.convert(conf.get(CONF_USERNAME), str) - password = util.convert(conf.get(CONF_PASSWORD), str) - ssl = util.convert(conf.get(CONF_SSL), bool, DEFAULT_SSL) - verify_ssl = util.convert(conf.get(CONF_VERIFY_SSL), bool, - DEFAULT_VERIFY_SSL) - blacklist = conf.get(CONF_BLACKLIST, []) - whitelist = conf.get(CONF_WHITELIST, []) - tags = conf.get(CONF_TAGS, {}) + host = conf.get(CONF_HOST) + port = conf.get(CONF_PORT) + database = conf.get(CONF_DB_NAME) + username = conf.get(CONF_USERNAME) + password = conf.get(CONF_PASSWORD) + ssl = conf.get(CONF_SSL) + verify_ssl = conf.get(CONF_VERIFY_SSL) + blacklist = conf.get(CONF_BLACKLIST) + whitelist = conf.get(CONF_WHITELIST) + tags = conf.get(CONF_TAGS) try: - influx = InfluxDBClient(host=host, port=port, username=username, - password=password, database=database, - ssl=ssl, verify_ssl=verify_ssl) + influx = InfluxDBClient( + host=host, port=port, username=username, password=password, + database=database, ssl=ssl, verify_ssl=verify_ssl) influx.query("select * from /.*/ LIMIT 1;") except exceptions.InfluxDBClientError as exc: _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'][tag] = tags[tag] + json_body[0]['tags'].update(tags) try: influx.write_points(json_body) diff --git a/tests/components/test_influx.py b/tests/components/test_influxdb.py similarity index 95% rename from tests/components/test_influx.py rename to tests/components/test_influxdb.py index 0d7d0d147b2..21aae5b0b04 100644 --- a/tests/components/test_influx.py +++ b/tests/components/test_influxdb.py @@ -5,6 +5,7 @@ from unittest import mock import influxdb as influx_client +from homeassistant.bootstrap import setup_component import homeassistant.components.influxdb as influxdb from homeassistant.const import EVENT_STATE_CHANGED, STATE_OFF, STATE_ON @@ -31,7 +32,7 @@ class TestInfluxDB(unittest.TestCase): '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.assertEqual(EVENT_STATE_CHANGED, self.hass.bus.listen.call_args_list[0][0][0]) @@ -46,7 +47,7 @@ class TestInfluxDB(unittest.TestCase): '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.assertEqual(EVENT_STATE_CHANGED, self.hass.bus.listen.call_args_list[0][0][0]) @@ -55,7 +56,6 @@ class TestInfluxDB(unittest.TestCase): """Test the setup with missing keys.""" config = { 'influxdb': { - 'host': 'host', 'username': 'user', 'password': 'pass', } @@ -63,7 +63,7 @@ class TestInfluxDB(unittest.TestCase): for missing in config['influxdb'].keys(): config_copy = copy.deepcopy(config) 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): """Test the setup for query failures.""" @@ -76,7 +76,7 @@ class TestInfluxDB(unittest.TestCase): } mock_client.return_value.query.side_effect = \ influx_client.exceptions.InfluxDBClientError('fake') - self.assertFalse(influxdb.setup(self.hass, config)) + assert not setup_component(self.hass, influxdb.DOMAIN, config) def _setup(self): """Setup the client.""" @@ -88,7 +88,7 @@ class TestInfluxDB(unittest.TestCase): '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] def test_event_listener(self, mock_client):