1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 21:06:19 +00:00

Add COMPONENT_SCHEMA and use it in alarm_control_panel (#20224)

* Add COMPONENT_SCHEMA and use in alarm and mqtt

* Revert MQTT changes

* Lint

* Small tweak

* Add tests

* Rename COMPONENT_SCHEMA to PLATFORM_SCHEMA_BASE

* Fix tests

* Improve tests
This commit is contained in:
emontnemery
2019-01-29 01:14:55 +01:00
committed by Paulus Schoutsen
parent bb4ca1f525
commit d7ba2aad1d
5 changed files with 137 additions and 9 deletions

View File

@@ -14,7 +14,8 @@ from homeassistant.const import (
import homeassistant.config as config_util
from homeassistant import setup, loader
import homeassistant.util.dt as dt_util
from homeassistant.helpers.config_validation import PLATFORM_SCHEMA
from homeassistant.helpers.config_validation import (
PLATFORM_SCHEMA_2 as PLATFORM_SCHEMA, PLATFORM_SCHEMA_BASE)
from homeassistant.helpers import discovery
from tests.common import \
@@ -94,18 +95,24 @@ class TestSetup:
platform_schema = PLATFORM_SCHEMA.extend({
'hello': str,
})
platform_schema_base = PLATFORM_SCHEMA_BASE.extend({
})
loader.set_component(
self.hass,
'platform_conf',
MockModule('platform_conf', platform_schema=platform_schema))
MockModule('platform_conf',
platform_schema_base=platform_schema_base))
loader.set_component(
self.hass,
'platform_conf.whatever', MockPlatform('whatever'))
'platform_conf.whatever',
MockPlatform('whatever',
platform_schema=platform_schema))
with assert_setup_component(0):
assert setup.setup_component(self.hass, 'platform_conf', {
'platform_conf': {
'platform': 'whatever',
'hello': 'world',
'invalid': 'extra',
}
@@ -121,6 +128,7 @@ class TestSetup:
'hello': 'world',
},
'platform_conf 2': {
'platform': 'whatever',
'invalid': True
}
})
@@ -175,6 +183,107 @@ class TestSetup:
assert 'platform_conf' in self.hass.config.components
assert not config['platform_conf'] # empty
def test_validate_platform_config_2(self):
"""Test component PLATFORM_SCHEMA_BASE prio over PLATFORM_SCHEMA."""
platform_schema = PLATFORM_SCHEMA.extend({
'hello': str,
})
platform_schema_base = PLATFORM_SCHEMA_BASE.extend({
'hello': 'world',
})
loader.set_component(
self.hass,
'platform_conf',
MockModule('platform_conf',
platform_schema=platform_schema,
platform_schema_base=platform_schema_base))
loader.set_component(
self.hass,
'platform_conf.whatever',
MockPlatform('whatever',
platform_schema=platform_schema))
with assert_setup_component(0):
assert setup.setup_component(self.hass, 'platform_conf', {
# fail: no extra keys allowed in platform schema
'platform_conf': {
'platform': 'whatever',
'hello': 'world',
'invalid': 'extra',
}
})
self.hass.data.pop(setup.DATA_SETUP)
self.hass.config.components.remove('platform_conf')
with assert_setup_component(1):
assert setup.setup_component(self.hass, 'platform_conf', {
# pass
'platform_conf': {
'platform': 'whatever',
'hello': 'world',
},
# fail: key hello violates component platform_schema_base
'platform_conf 2': {
'platform': 'whatever',
'hello': 'there'
}
})
self.hass.data.pop(setup.DATA_SETUP)
self.hass.config.components.remove('platform_conf')
def test_validate_platform_config_3(self):
"""Test fallback to component PLATFORM_SCHEMA."""
component_schema = PLATFORM_SCHEMA_BASE.extend({
'hello': str,
})
platform_schema = PLATFORM_SCHEMA.extend({
'cheers': str,
'hello': 'world',
})
loader.set_component(
self.hass,
'platform_conf',
MockModule('platform_conf',
platform_schema=component_schema))
loader.set_component(
self.hass,
'platform_conf.whatever',
MockPlatform('whatever',
platform_schema=platform_schema))
with assert_setup_component(0):
assert setup.setup_component(self.hass, 'platform_conf', {
'platform_conf': {
# fail: no extra keys allowed
'hello': 'world',
'invalid': 'extra',
}
})
self.hass.data.pop(setup.DATA_SETUP)
self.hass.config.components.remove('platform_conf')
with assert_setup_component(1):
assert setup.setup_component(self.hass, 'platform_conf', {
# pass
'platform_conf': {
'platform': 'whatever',
'hello': 'world',
},
# fail: key hello violates component platform_schema
'platform_conf 2': {
'platform': 'whatever',
'hello': 'there'
}
})
self.hass.data.pop(setup.DATA_SETUP)
self.hass.config.components.remove('platform_conf')
def test_component_not_found(self):
"""setup_component should not crash if component doesn't exist."""
assert not setup.setup_component(self.hass, 'non_existing')