-
-
Notifications
You must be signed in to change notification settings - Fork 37.7k
Add PLATFORM_SCHEMA_BASE and use it in alarm_control_panel #20224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
848d391
7dfa147
b1107be
b41dd09
0dd6114
b261106
74b726c
08b1e5c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -14,7 +14,8 @@ | |||||
| 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 ( | ||||||
| COMPONENT_SCHEMA, PLATFORM_SCHEMA_2 as PLATFORM_SCHEMA) | ||||||
| from homeassistant.helpers import discovery | ||||||
|
|
||||||
| from tests.common import \ | ||||||
|
|
@@ -94,18 +95,24 @@ def test_validate_platform_config(self): | |||||
| platform_schema = PLATFORM_SCHEMA.extend({ | ||||||
| 'hello': str, | ||||||
| }) | ||||||
| component_schema = COMPONENT_SCHEMA.extend({ | ||||||
| }) | ||||||
| loader.set_component( | ||||||
| self.hass, | ||||||
| 'platform_conf', | ||||||
| MockModule('platform_conf', platform_schema=platform_schema)) | ||||||
| MockModule('platform_conf', | ||||||
| component_schema=component_schema)) | ||||||
|
|
||||||
| 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 @@ def test_validate_platform_config(self): | |||||
| 'hello': 'world', | ||||||
| }, | ||||||
| 'platform_conf 2': { | ||||||
| 'platform': 'whatever', | ||||||
| 'invalid': True | ||||||
| } | ||||||
| }) | ||||||
|
|
@@ -175,6 +183,99 @@ def test_validate_platform_config(self): | |||||
| assert 'platform_conf' in self.hass.config.components | ||||||
| assert not config['platform_conf'] # empty | ||||||
|
|
||||||
| def test_validate_platform_config_2(self): | ||||||
| """Test component COMPONENT_SCHEMA prioritized over PLATFORM_SCHEMA.""" | ||||||
| platform_schema = PLATFORM_SCHEMA.extend({ | ||||||
| 'cheers': str, | ||||||
| 'hello': str, | ||||||
| }) | ||||||
| component_schema = PLATFORM_SCHEMA.extend({ | ||||||
| 'hello': str, | ||||||
|
emontnemery marked this conversation as resolved.
Outdated
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Eg if we do:
Suggested change
We can below check if platforms validate this item correctly, instead of adding other keys, since the schema should allow extra.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the explanation, fixed now! |
||||||
| }) | ||||||
| loader.set_component( | ||||||
| self.hass, | ||||||
| 'platform_conf', | ||||||
| MockModule('platform_conf', | ||||||
| component_schema=component_schema, | ||||||
| platform_schema=platform_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': { | ||||||
| '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', { | ||||||
| 'platform_conf': { | ||||||
| 'platform': 'whatever', | ||||||
| 'hello': 'world', | ||||||
| }, | ||||||
| 'platform_conf 2': { | ||||||
| 'platform': 'whatever', | ||||||
| 'hello': 'world', | ||||||
| 'cheers': 'mate' | ||||||
| } | ||||||
| }) | ||||||
|
|
||||||
| 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.""" | ||||||
|
emontnemery marked this conversation as resolved.
|
||||||
| platform_schema = PLATFORM_SCHEMA.extend({ | ||||||
| 'hello': str, | ||||||
| }) | ||||||
| loader.set_component( | ||||||
| self.hass, | ||||||
| 'platform_conf', | ||||||
| MockModule('platform_conf', | ||||||
| platform_schema=platform_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': { | ||||||
| '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', { | ||||||
| 'platform_conf': { | ||||||
| 'platform': 'whatever', | ||||||
| 'hello': 'world', | ||||||
| }, | ||||||
| 'platform_conf 2': { | ||||||
| 'platform': 'whatever', | ||||||
| 'hello': 'world', | ||||||
| 'cheers': 'mate' | ||||||
| } | ||||||
| }) | ||||||
|
|
||||||
| 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') | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's weird that the word
COMPONENT_SCHEMAtriggers platform validation.The idea was: Oh it has a platform schema, let's treat the config as being a list of platforms that all have to validate. Can't we call it
PLATFORM_BASE_SCHEMA, given that it is the schema that is the basis for all platform schemas of that entity component ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So you're suggesting as before that the component will export:
If you do like that, a lot of modules implementing a platform have to be updated to import and extend PLATFORM_SCHEMA_BASE instead of PLATFORM_SCHEMA.
I don't really mind, but it's a lot of changes..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no no, we flip it. The BASE is not what normal platform extends from, BASE is validating that the bare minimum of a valid platform config is validated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, that will be easier!
Just to double confirm, this is what you mean:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes