-
-
Notifications
You must be signed in to change notification settings - Fork 37.6k
Improve validation of device trigger config #26910
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
Merged
emontnemery
merged 7 commits into
home-assistant:dev
from
emontnemery:device_trigger_config_validation
Sep 27, 2019
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
85a5d4e
Improve validation of device trigger config
emontnemery aed2c69
Remove action and condition checks
emontnemery a5563e8
Move config validation to own file
emontnemery f6f8826
Fix tests
emontnemery 93d7dc0
Fixes
emontnemery 9982608
Fixes
emontnemery 079a442
Small tweak
emontnemery File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| """Config validation helper for the automation integration.""" | ||
| import asyncio | ||
| import importlib | ||
|
|
||
| import voluptuous as vol | ||
|
|
||
| from homeassistant.const import CONF_PLATFORM | ||
| from homeassistant.config import async_log_exception, config_without_domain | ||
| from homeassistant.exceptions import HomeAssistantError | ||
| from homeassistant.helpers import config_per_platform | ||
| from homeassistant.loader import IntegrationNotFound | ||
|
|
||
| from . import CONF_TRIGGER, DOMAIN, PLATFORM_SCHEMA | ||
|
|
||
| # mypy: allow-untyped-calls, allow-untyped-defs | ||
| # mypy: no-check-untyped-defs, no-warn-return-any | ||
|
|
||
|
|
||
| async def async_validate_config_item(hass, config, full_config=None): | ||
| """Validate config item.""" | ||
| try: | ||
| config = PLATFORM_SCHEMA(config) | ||
|
|
||
| triggers = [] | ||
| for trigger in config[CONF_TRIGGER]: | ||
| trigger_platform = importlib.import_module( | ||
| "..{}".format(trigger[CONF_PLATFORM]), __name__ | ||
| ) | ||
| if hasattr(trigger_platform, "async_validate_trigger_config"): | ||
| trigger = await trigger_platform.async_validate_trigger_config( | ||
| hass, trigger | ||
| ) | ||
| triggers.append(trigger) | ||
| config[CONF_TRIGGER] = triggers | ||
| except (vol.Invalid, HomeAssistantError, IntegrationNotFound) as ex: | ||
| async_log_exception(ex, DOMAIN, full_config or config, hass) | ||
| return None | ||
|
|
||
| return config | ||
|
|
||
|
|
||
| async def async_validate_config(hass, config): | ||
| """Validate config.""" | ||
| automations = [] | ||
| validated_automations = await asyncio.gather( | ||
| *( | ||
| async_validate_config_item(hass, p_config, config) | ||
| for _, p_config in config_per_platform(config, DOMAIN) | ||
| ) | ||
| ) | ||
| for validated_automation in validated_automations: | ||
| if validated_automation is not None: | ||
| automations.append(validated_automation) | ||
|
|
||
| # Create a copy of the configuration with all config for current | ||
| # component removed and add validated config back in. | ||
| config = config_without_domain(config, DOMAIN) | ||
| config[DOMAIN] = automations | ||
|
|
||
| return config |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,24 @@ | ||
| """Offer device oriented automation.""" | ||
| import voluptuous as vol | ||
|
|
||
| from homeassistant.const import CONF_DOMAIN, CONF_PLATFORM | ||
| from homeassistant.loader import async_get_integration | ||
| from homeassistant.components.device_automation import ( | ||
| TRIGGER_BASE_SCHEMA, | ||
| async_get_device_automation_platform, | ||
| ) | ||
|
|
||
|
|
||
| # mypy: allow-untyped-defs, no-check-untyped-defs | ||
|
|
||
| TRIGGER_SCHEMA = vol.Schema( | ||
| {vol.Required(CONF_PLATFORM): "device", vol.Required(CONF_DOMAIN): str}, | ||
| extra=vol.ALLOW_EXTRA, | ||
| ) | ||
| TRIGGER_SCHEMA = TRIGGER_BASE_SCHEMA.extend({}, extra=vol.ALLOW_EXTRA) | ||
|
|
||
|
|
||
| async def async_validate_trigger_config(hass, config): | ||
| """Validate config.""" | ||
| platform = await async_get_device_automation_platform(hass, config, "trigger") | ||
| return platform.TRIGGER_SCHEMA(config) | ||
|
|
||
|
|
||
| async def async_attach_trigger(hass, config, action, automation_info): | ||
| """Listen for trigger.""" | ||
| integration = await async_get_integration(hass, config[CONF_DOMAIN]) | ||
| platform = integration.get_platform("device_trigger") | ||
| platform = await async_get_device_automation_platform(hass, config, "trigger") | ||
| return await platform.async_attach_trigger(hass, config, action, automation_info) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Note, we should add dev docs for this right away.