-
-
Notifications
You must be signed in to change notification settings - Fork 37.6k
Improve validation of device automation config #26830
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
Closed
emontnemery
wants to merge
5
commits into
home-assistant:dev
from
emontnemery:device_automation_config_validation
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8c03f80
Improve validation of device automation config
emontnemery 1778c33
Fix lint, typing, tests, review comments
emontnemery 7498c65
Add tests
emontnemery 4ecc6d7
Add missing file
emontnemery bb2f287
Improve tests, handle device condition in action
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,8 @@ | |
| from homeassistant.helpers.typing import ConfigType | ||
| from homeassistant.loader import async_get_integration, IntegrationNotFound | ||
|
|
||
| from .exceptions import InvalidDeviceAutomationConfig | ||
|
|
||
| DOMAIN = "device_automation" | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
@@ -32,6 +34,68 @@ async def async_setup(hass, config): | |
| return True | ||
|
|
||
|
|
||
| async def async_get_device_automation_platform(hass, config): | ||
| """Load device automation platform for integration. | ||
|
|
||
| Throws InvalidDeviceAutomationConfig if the integration is not found or does not support device automation. | ||
| """ | ||
| try: | ||
| integration = await async_get_integration(hass, config[CONF_DOMAIN]) | ||
| platform = integration.get_platform("device_automation") | ||
| except IntegrationNotFound: | ||
| raise InvalidDeviceAutomationConfig( | ||
| f"Integration '{config[CONF_DOMAIN]}' not found" | ||
| ) | ||
| except ImportError: | ||
| raise InvalidDeviceAutomationConfig( | ||
| f"Integration '{config[CONF_DOMAIN]}' does not support device automations" | ||
| ) | ||
|
|
||
| return platform | ||
|
|
||
|
|
||
| async def async_validate_action_config(hass, config): | ||
| """Validate config.""" | ||
| platform = await async_get_device_automation_platform(hass, config) | ||
| if not hasattr(platform, "async_get_actions"): | ||
| raise InvalidDeviceAutomationConfig( | ||
| f"Integration '{config[CONF_DOMAIN]}' does not support device automation actions" | ||
| ) | ||
|
|
||
| return platform.ACTION_SCHEMA(config) | ||
|
|
||
|
|
||
| async def async_validate_condition_config( | ||
| hass: HomeAssistant, config: ConfigType | ||
| ) -> ConfigType: | ||
| """Validate config.""" | ||
| platform = await async_get_device_automation_platform(hass, config) | ||
| if not hasattr(platform, "async_get_conditions"): | ||
| raise InvalidDeviceAutomationConfig( | ||
| f"Integration '{config[CONF_DOMAIN]}' does not support device automation conditions" | ||
| ) | ||
|
|
||
| return platform.CONDITION_SCHEMA(config) | ||
|
|
||
|
|
||
| async def async_validate_trigger_config(hass, config): | ||
| """Validate config.""" | ||
| platform = await async_get_device_automation_platform(hass, config) | ||
| if not hasattr(platform, "async_get_triggers"): | ||
| raise InvalidDeviceAutomationConfig( | ||
| f"Integration '{config[CONF_DOMAIN]}' does not support device automation triggers" | ||
| ) | ||
|
|
||
| return platform.TRIGGER_SCHEMA(config) | ||
|
|
||
|
|
||
| async def async_handle_action(hass, action, variables, context): | ||
| """Perform the device automation specified in the action.""" | ||
| integration = await async_get_integration(hass, action[CONF_DOMAIN]) | ||
| platform = integration.get_platform("device_automation") | ||
| await platform.async_call_action_from_config(hass, action, variables, context) | ||
|
|
||
|
|
||
| async def async_device_condition_from_config( | ||
| hass: HomeAssistant, config: ConfigType, config_validation: bool = True | ||
| ) -> Callable[..., bool]: | ||
|
|
@@ -46,6 +110,13 @@ async def async_device_condition_from_config( | |
| ) | ||
|
|
||
|
|
||
| async def async_trigger(hass, config, action, automation_info): | ||
|
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. I am renaming this in #26871 to async_attach_trigger |
||
| """Listen for trigger.""" | ||
| integration = await async_get_integration(hass, config[CONF_DOMAIN]) | ||
| platform = integration.get_platform("device_automation") | ||
| return await platform.async_trigger(hass, config, action, automation_info) | ||
|
|
||
|
|
||
| async def _async_get_device_automations_from_domain(hass, domain, fname, device_id): | ||
| """List device automations.""" | ||
| integration = None | ||
|
|
||
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 |
|---|---|---|
|
|
@@ -416,7 +416,7 @@ def process_ha_config_upgrade(hass: HomeAssistant) -> None: | |
|
|
||
| @callback | ||
| def async_log_exception( | ||
| ex: vol.Invalid, domain: str, config: Dict, hass: HomeAssistant | ||
| ex: Exception, domain: str, config: Dict, hass: HomeAssistant | ||
| ) -> None: | ||
| """Log an error for configuration validation. | ||
|
|
||
|
|
@@ -428,23 +428,26 @@ def async_log_exception( | |
|
|
||
|
|
||
| @callback | ||
| def _format_config_error(ex: vol.Invalid, domain: str, config: Dict) -> str: | ||
| def _format_config_error(ex: Exception, domain: str, config: Dict) -> str: | ||
| """Generate log exception for configuration validation. | ||
|
|
||
| This method must be run in the event loop. | ||
| """ | ||
| message = f"Invalid config for [{domain}]: " | ||
| if "extra keys not allowed" in ex.error_message: | ||
| message += ( | ||
| "[{option}] is an invalid option for [{domain}]. " | ||
| "Check: {domain}->{path}.".format( | ||
| option=ex.path[-1], | ||
| domain=domain, | ||
| path="->".join(str(m) for m in ex.path), | ||
| if isinstance(ex, vol.Invalid): | ||
| if "extra keys not allowed" in ex.error_message: | ||
| message += ( | ||
| "[{option}] is an invalid option for [{domain}]. " | ||
| "Check: {domain}->{path}.".format( | ||
| option=ex.path[-1], | ||
| domain=domain, | ||
| path="->".join(str(m) for m in ex.path), | ||
| ) | ||
| ) | ||
| ) | ||
| else: | ||
| message += "{}.".format(humanize_error(config, ex)) | ||
| else: | ||
| message += "{}.".format(humanize_error(config, ex)) | ||
| message += str(ex) | ||
|
|
||
| try: | ||
| domain_config = config.get(domain, config) | ||
|
|
@@ -717,6 +720,13 @@ async def async_process_component_config( | |
| _LOGGER.error("Unable to import %s: %s", domain, ex) | ||
| return None | ||
|
|
||
| if hasattr(component, "async_validate_config"): | ||
|
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. Where is the test for this? |
||
| try: | ||
| return await component.async_validate_config(hass, config) # type: ignore | ||
| except (vol.Invalid, HomeAssistantError) as ex: | ||
| async_log_exception(ex, domain, config, hass) | ||
| return None | ||
|
|
||
| if hasattr(component, "CONFIG_SCHEMA"): | ||
| try: | ||
| return component.CONFIG_SCHEMA(config) # type: ignore | ||
|
|
||
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
Oops, something went wrong.
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.
Let's use
asyncio.gatherto run in parallel.