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
Add PLATFORM_SCHEMA_BASE and use it in alarm_control_panel #20224
Add PLATFORM_SCHEMA_BASE and use it in alarm_control_panel #20224
Conversation
@@ -13,7 +13,8 @@ | |||
ATTR_CODE, ATTR_CODE_FORMAT, ATTR_ENTITY_ID, SERVICE_ALARM_TRIGGER, | |||
SERVICE_ALARM_DISARM, SERVICE_ALARM_ARM_HOME, SERVICE_ALARM_ARM_AWAY, | |||
SERVICE_ALARM_ARM_NIGHT, SERVICE_ALARM_ARM_CUSTOM_BYPASS) | |||
from homeassistant.helpers.config_validation import PLATFORM_SCHEMA # noqa | |||
from homeassistant.helpers.config_validation import ( | |||
COMPONENT_SCHEMA, PLATFORM_SCHEMA_2 as PLATFORM_SCHEMA) # noqa |
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.
Importing platform schema here is wrong ? It should have allow_extra set ?
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.
We now expose COMPONENT_SCHEMA with has allow_extra set, and PLATFORM_SCHEMA which does not. PLATFORM_SCHEMA may be used as base schema by platforms implementing the alarm component.
@@ -499,11 +499,22 @@ def validator(value): | |||
|
|||
# Schemas | |||
|
|||
COMPONENT_SCHEMA = vol.Schema({ |
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.
I would expect it to be structured like this:
PLATFORM_SCHEMA_BASE = vol.Schema({
vol.Required(CONF_PLATFORM): string,
vol.Optional(CONF_SCAN_INTERVAL): time_period
})
PLATFORM_SCHEMA = PLATFORM_SCHEMA_BASE.extend({
}, extra=vol.ALLOW_EXTRA)
And components will import PLATFORM_SCHEMA
, platforms will import PLATFORM_SCHEMA_BASE
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.
Fixed as you suggest, except for changing the names.
If also changing names, every single platform module will have to be updated.
Also, platforms are supposed to extend the component schema if I understood @MartinHjelmare correctly.
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.
Looks good! We should add or update the test for config.py.
@MartinHjelmare Tests added. Existing test updated + fixed. |
homeassistant/config.py
Outdated
@@ -743,13 +743,18 @@ def async_process_component_config( | |||
async_log_exception(ex, domain, config, hass) | |||
return None | |||
|
|||
elif hasattr(component, 'PLATFORM_SCHEMA'): | |||
elif (hasattr(component, 'COMPONENT_SCHEMA') or |
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_SCHEMA
triggers 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:
PLATFORM_SCHEMA_BASE = vol.Schema({
vol.Required(CONF_PLATFORM): string,
vol.Optional(CONF_SCAN_INTERVAL): time_period
})
PLATFORM_SCHEMA = PLATFORM_SCHEMA_BASE.extend({
}, extra=vol.ALLOW_EXTRA)
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:
PLATFORM_SCHEMA = vol.Schema({
vol.Required(CONF_PLATFORM): string,
vol.Optional(CONF_SCAN_INTERVAL): time_period
})
PLATFORM_SCHEMA_BASE = PLATFORM_SCHEMA.extend({
}, extra=vol.ALLOW_EXTRA)
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
tests/test_setup.py
Outdated
'hello': str, | ||
}) | ||
platform_schema_base = PLATFORM_SCHEMA.extend({ | ||
'hello': str, |
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.
Eg if we do:
'hello': str, | |
'hello': 'world', |
We can below check if platforms validate this item correctly, instead of adding other keys, since the schema should allow extra.
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.
Thanks for the explanation, fixed now!
549b051
to
08b1e5c
Compare
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.
Looks good!
Added breaking change label as suggested in discord beta channel. |
@emontnemery - Do you have a good example of what I'm supposed to do to update a component to work properly with this change? I'm running into this issue with the telegram_bot component because the telegram_bot.webhooks platform has additional parameters and the config validation fails when checking against the base platforms PLATFORM_SCHEMA. What modifications need to be done to fix this? If the platform specific schema (e.g. PLATFORM_SCHEMA defined in telegram_bot/webhooks.py) extends the PLATFORM_SCHEMA in telegram_bot/init.py, why does the config validation bother validating against the base schema? Or is it just because of a misconfiguration of this component that that's an issue? |
Please open an issue if you suspect a bug. If you need help please use our help channels: Merged PRs should not be used for support or bug reports. Thanks! |
Description:
PLATFORM_SCHEMA in config_validation.py sets
extra=vol.ALLOW_EXTRA
This means platforms will silently swallow mistyped configuration variables.
To fix this:
PLATFORM_SCHEMA_BASE
for use in base platforms. This schema setsextra=vol.ALLOW_EXTRA
PLATFORM_SCHEMA_2
which doesn't set extra=vol.ALLOW_EXTRAPLATFORM_SCHEMA_BASE
andPLATFORM_SCHEMA_2
asPLATFORM_SCHEMA
PLATFORM_SCHEMA_BASE
orPLATFORM_SCHEMA
, with priority forPLATFORM_SCHEMA_BASE
When all are migrated:
PLATFORM_SCHEMA_2
and removeextra=vol.ALLOW_EXTRA
from PLATFORM_SCHEMAconfig.py
to only check forPLATFORM_SCHEMA_BASE
This first PR demonstrates the concept and uses it for alarm_control_panel.
Checklist:
tox
. Your PR cannot be merged unless tests pass