Skip to content
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

Merged
merged 8 commits into from Jan 29, 2019

Conversation

emontnemery
Copy link
Contributor

@emontnemery emontnemery commented Jan 18, 2019

Description:

PLATFORM_SCHEMA in config_validation.py sets extra=vol.ALLOW_EXTRA
This means platforms will silently swallow mistyped configuration variables.

To fix this:

  • Add new base schema PLATFORM_SCHEMA_BASE for use in base platforms. This schema sets extra=vol.ALLOW_EXTRA
  • Temporarily add a PLATFORM_SCHEMA_2 which doesn't set extra=vol.ALLOW_EXTRA
  • Update base platforms to expose both PLATFORM_SCHEMA_BASE and PLATFORM_SCHEMA_2 as PLATFORM_SCHEMA
  • Update config.py to check the component for either PLATFORM_SCHEMA_BASE or PLATFORM_SCHEMA, with priority for PLATFORM_SCHEMA_BASE
  • Start migrating components

When all are migrated:

  • Remove PLATFORM_SCHEMA_2 and remove extra=vol.ALLOW_EXTRA from PLATFORM_SCHEMA
  • Update config.py to only check for PLATFORM_SCHEMA_BASE

This first PR demonstrates the concept and uses it for alarm_control_panel.

Checklist:

  • The code change is tested and works locally.
  • Local tests pass with tox. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.

tests/components/mqtt/test_alarm_control_panel.py Outdated Show resolved Hide resolved
tests/components/mqtt/test_alarm_control_panel.py Outdated Show resolved Hide resolved
tests/components/mqtt/test_alarm_control_panel.py Outdated Show resolved Hide resolved
tests/components/mqtt/test_alarm_control_panel.py Outdated Show resolved Hide resolved
homeassistant/config.py Outdated Show resolved Hide resolved
homeassistant/components/mqtt/light/__init__.py Outdated Show resolved Hide resolved
homeassistant/components/alarm_control_panel/__init__.py Outdated Show resolved Hide resolved
@@ -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
Copy link
Member

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 ?

Copy link
Contributor Author

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({
Copy link
Member

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

Copy link
Contributor Author

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.

Example: https://github.com/home-assistant/home-assistant/blob/e80702a45ce575b89c39a0ca4e8b373bfc33e3e3/homeassistant/components/alarm_control_panel/alarmdotcom.py#L13

Copy link
Member

@MartinHjelmare MartinHjelmare left a 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.

@emontnemery
Copy link
Contributor Author

@MartinHjelmare Tests added. Existing test updated + fixed.

@emontnemery emontnemery changed the title Add COMPONENT_SCHEMA and use it in alarm and mqtt Add COMPONENT_SCHEMA and use it in alarm_control_panel Jan 19, 2019
@@ -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
Copy link
Member

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 ?

Copy link
Contributor Author

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..

Copy link
Member

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.

Copy link
Contributor Author

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)

Copy link
Member

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 Show resolved Hide resolved
tests/test_setup.py Show resolved Hide resolved
'hello': str,
})
platform_schema_base = PLATFORM_SCHEMA.extend({
'hello': str,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eg if we do:

Suggested change
'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.

Copy link
Contributor Author

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!

Copy link
Member

@balloob balloob left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@balloob balloob merged commit d7ba2aa into home-assistant:dev Jan 29, 2019
@ghost ghost removed the in progress label Jan 29, 2019
@emontnemery emontnemery changed the title Add COMPONENT_SCHEMA and use it in alarm_control_panel Add PLATFORM_SCHEMA_BASE and use it in alarm_control_panel Jan 29, 2019
@emontnemery emontnemery deleted the component_schema_alarm branch January 30, 2019 11:52
@emontnemery
Copy link
Contributor Author

Added breaking change label as suggested in discord beta channel.

@balloob balloob mentioned this pull request Feb 6, 2019
@veleek
Copy link
Contributor

veleek commented Feb 15, 2019

@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?

@MartinHjelmare
Copy link
Member

Please open an issue if you suspect a bug. If you need help please use our help channels:
https://home-assistant.io/help/#communication-channels

Merged PRs should not be used for support or bug reports. Thanks!

@home-assistant home-assistant locked as resolved and limited conversation to collaborators Feb 15, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants