diff --git a/homeassistant/helpers/__init__.py b/homeassistant/helpers/__init__.py index 12d15e2bcd4..69777a0ccd4 100644 --- a/homeassistant/helpers/__init__.py +++ b/homeassistant/helpers/__init__.py @@ -40,7 +40,8 @@ def config_per_platform(config, domain): platform_config = [platform_config] for item in platform_config: - yield item.get(CONF_PLATFORM), item + platform = None if item is None else item.get(CONF_PLATFORM) + yield platform, item def extract_domain_configs(config, domain): diff --git a/tests/common.py b/tests/common.py index 91bfc07fbc0..cb7476be828 100644 --- a/tests/common.py +++ b/tests/common.py @@ -143,10 +143,18 @@ class MockHTTP(object): class MockModule(object): """Representation of a fake module.""" - def __init__(self, domain=None, dependencies=[], setup=None): + def __init__(self, domain=None, dependencies=[], setup=None, + config_schema=None, platform_schema=None): """Initialize the mock module.""" self.DOMAIN = domain self.DEPENDENCIES = dependencies + + if config_schema is not None: + self.CONFIG_SCHEMA = config_schema + + if platform_schema is not None: + self.PLATFORM_SCHEMA = platform_schema + # Setup a mock setup if none given. if setup is None: self.setup = lambda hass, config: True diff --git a/tests/test_bootstrap.py b/tests/test_bootstrap.py index fd2265db76e..c2426dbe968 100644 --- a/tests/test_bootstrap.py +++ b/tests/test_bootstrap.py @@ -4,11 +4,14 @@ import os import tempfile import unittest +import voluptuous as vol + from homeassistant import bootstrap, loader from homeassistant.const import (__version__, CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME, CONF_CUSTOMIZE) import homeassistant.util.dt as dt_util from homeassistant.helpers.entity import Entity +from homeassistant.helpers.config_validation import PLATFORM_SCHEMA from tests.common import get_test_home_assistant, MockModule @@ -121,3 +124,93 @@ class TestBootstrap(unittest.TestCase): bootstrap.setup_component(hass, 'comp_a') self.assertEqual(['comp_a'], hass.config.components) hass.stop() + + def test_validate_component_config(self): + """Test validating component configuration.""" + config_schema = vol.Schema({ + 'comp_conf': { + 'hello': str + } + }, required=True) + loader.set_component( + 'comp_conf', MockModule('comp_conf', config_schema=config_schema)) + + hass = get_test_home_assistant() + + assert not bootstrap._setup_component(hass, 'comp_conf', {}) + + assert not bootstrap._setup_component(hass, 'comp_conf', { + 'comp_conf': None + }) + + assert not bootstrap._setup_component(hass, 'comp_conf', { + 'comp_conf': {} + }) + + assert not bootstrap._setup_component(hass, 'comp_conf', { + 'comp_conf': { + 'hello': 'world', + 'invalid': 'extra', + } + }) + + assert bootstrap._setup_component(hass, 'comp_conf', { + 'comp_conf': { + 'hello': 'world', + } + }) + + hass.stop() + + def test_validate_platform_config(self): + """Test validating platform configuration.""" + platform_schema = PLATFORM_SCHEMA.extend({ + 'hello': str, + }, required=True) + loader.set_component( + 'platform_conf', + MockModule('platform_conf', platform_schema=platform_schema)) + + hass = get_test_home_assistant() + + assert not bootstrap._setup_component(hass, 'platform_conf', { + 'platform_conf': None + }) + + assert not bootstrap._setup_component(hass, 'platform_conf', { + 'platform_conf': {} + }) + + assert not bootstrap._setup_component(hass, 'platform_conf', { + 'platform_conf': { + 'hello': 'world', + 'invalid': 'extra', + } + }) + + assert not bootstrap._setup_component(hass, 'platform_conf', { + 'platform_conf': { + 'platform': 'whatever', + 'hello': 'world', + }, + + 'platform_conf 2': { + 'invalid': True + } + }) + + assert bootstrap._setup_component(hass, 'platform_conf', { + 'platform_conf': { + 'platform': 'whatever', + 'hello': 'world', + } + }) + + assert bootstrap._setup_component(hass, 'platform_conf', { + 'platform_conf': [{ + 'platform': 'whatever', + 'hello': 'world', + }] + }) + + hass.stop()