Allow platforms to specify PLATFORM_SCHEMA

This commit is contained in:
Paulus Schoutsen committed 2016-04-02 20:10:57 -07:00
1 parent 4fba89b789
commit f6d584af09
5 files changed
+86 -21

No files matched your search

+10 -6
View File
@@ -143,12 +143,12 @@ class MockHTTP(object):
class MockModule(object):
"""Representation of a fake module."""
def __init__(self, domain=None, dependencies=[], setup=None,
requirements=[], config_schema=None, platform_schema=None):
def __init__(self, domain=None, dependencies=None, setup=None,
requirements=None, config_schema=None, platform_schema=None):
"""Initialize the mock module."""
self.DOMAIN = domain
self.DEPENDENCIES = dependencies
self.REQUIREMENTS = requirements
self.DEPENDENCIES = dependencies or []
self.REQUIREMENTS = requirements or []
if config_schema is not None:
self.CONFIG_SCHEMA = config_schema
@@ -166,11 +166,15 @@ class MockModule(object):
class MockPlatform(object):
"""Provide a fake platform."""
def __init__(self, setup_platform=None, dependencies=[]):
def __init__(self, setup_platform=None, dependencies=None,
platform_schema=None):
"""Initialize the platform."""
self.DEPENDENCIES = dependencies
self.DEPENDENCIES = dependencies or []
self._setup_platform = setup_platform
if platform_schema is not None:
self.PLATFORM_SCHEMA = platform_schema
def setup_platform(self, hass, config, add_devices, discovery_info=None):
"""Setup the platform."""
if self._setup_platform is not None:
+45 -2
View File
@@ -165,11 +165,14 @@ class TestBootstrap:
"""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))
loader.set_component(
'platform_conf.whatever', MockPlatform('whatever'))
assert not bootstrap._setup_component(self.hass, 'platform_conf', {
'platform_conf': None
})
@@ -196,6 +199,13 @@ class TestBootstrap:
}
})
assert not bootstrap._setup_component(self.hass, 'platform_conf', {
'platform_conf': {
'platform': 'not_existing',
'hello': 'world',
}
})
assert bootstrap._setup_component(self.hass, 'platform_conf', {
'platform_conf': {
'platform': 'whatever',
@@ -318,7 +328,7 @@ class TestBootstrap:
loader.set_component('switch.platform_a', MockPlatform('comp_b',
['comp_a']))
assert bootstrap.setup_component(self.hass, 'switch', {
bootstrap.setup_component(self.hass, 'switch', {
'comp_a': {
'valid': True
},
@@ -327,3 +337,36 @@ class TestBootstrap:
}
})
assert 'comp_a' in self.hass.config.components
def test_platform_specific_config_validation(self):
"""Test platform that specifies config."""
platform_schema = PLATFORM_SCHEMA.extend({
'valid': True,
}, extra=vol.PREVENT_EXTRA)
loader.set_component(
'switch.platform_a',
MockPlatform('comp_b', platform_schema=platform_schema))
assert not bootstrap.setup_component(self.hass, 'switch', {
'switch': {
'platform': 'platform_a',
'invalid': True
}
})
assert not bootstrap.setup_component(self.hass, 'switch', {
'switch': {
'platform': 'platform_a',
'valid': True,
'invalid_extra': True,
}
})
assert bootstrap.setup_component(self.hass, 'switch', {
'switch': {
'platform': 'platform_a',
'valid': True
}
})