mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Add tests for bootstrap config validation
This commit is contained in:
parent
a35173a5ff
commit
25269cdb6b
@ -40,7 +40,8 @@ def config_per_platform(config, domain):
|
|||||||
platform_config = [platform_config]
|
platform_config = [platform_config]
|
||||||
|
|
||||||
for item in 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):
|
def extract_domain_configs(config, domain):
|
||||||
|
@ -143,10 +143,18 @@ class MockHTTP(object):
|
|||||||
class MockModule(object):
|
class MockModule(object):
|
||||||
"""Representation of a fake module."""
|
"""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."""
|
"""Initialize the mock module."""
|
||||||
self.DOMAIN = domain
|
self.DOMAIN = domain
|
||||||
self.DEPENDENCIES = dependencies
|
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.
|
# Setup a mock setup if none given.
|
||||||
if setup is None:
|
if setup is None:
|
||||||
self.setup = lambda hass, config: True
|
self.setup = lambda hass, config: True
|
||||||
|
@ -4,11 +4,14 @@ import os
|
|||||||
import tempfile
|
import tempfile
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant import bootstrap, loader
|
from homeassistant import bootstrap, loader
|
||||||
from homeassistant.const import (__version__, CONF_LATITUDE, CONF_LONGITUDE,
|
from homeassistant.const import (__version__, CONF_LATITUDE, CONF_LONGITUDE,
|
||||||
CONF_NAME, CONF_CUSTOMIZE)
|
CONF_NAME, CONF_CUSTOMIZE)
|
||||||
import homeassistant.util.dt as dt_util
|
import homeassistant.util.dt as dt_util
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
|
from homeassistant.helpers.config_validation import PLATFORM_SCHEMA
|
||||||
|
|
||||||
from tests.common import get_test_home_assistant, MockModule
|
from tests.common import get_test_home_assistant, MockModule
|
||||||
|
|
||||||
@ -121,3 +124,93 @@ class TestBootstrap(unittest.TestCase):
|
|||||||
bootstrap.setup_component(hass, 'comp_a')
|
bootstrap.setup_component(hass, 'comp_a')
|
||||||
self.assertEqual(['comp_a'], hass.config.components)
|
self.assertEqual(['comp_a'], hass.config.components)
|
||||||
hass.stop()
|
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()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user