-
-
Notifications
You must be signed in to change notification settings - Fork 37.7k
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
Changes from 2 commits
85a5d4e
aed2c69
a5563e8
f6f8826
93d7dc0
9982608
079a442
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 |
|---|---|---|
|
|
@@ -23,16 +23,22 @@ | |
| SERVICE_TURN_ON, | ||
| STATE_ON, | ||
| ) | ||
| from homeassistant.config import async_log_exception, config_without_domain | ||
| from homeassistant.core import Context, CoreState, HomeAssistant | ||
| from homeassistant.exceptions import HomeAssistantError | ||
| from homeassistant.helpers import condition, extract_domain_configs, script | ||
| from homeassistant.helpers import ( | ||
| condition, | ||
| config_per_platform, | ||
| extract_domain_configs, | ||
| script, | ||
| ) | ||
| import homeassistant.helpers.config_validation as cv | ||
| from homeassistant.helpers.config_validation import ENTITY_SERVICE_SCHEMA | ||
| from homeassistant.helpers.entity import ToggleEntity | ||
| from homeassistant.helpers.entity_component import EntityComponent | ||
| from homeassistant.helpers.restore_state import RestoreEntity | ||
| from homeassistant.helpers.typing import TemplateVarsType | ||
| from homeassistant.loader import bind_hass | ||
| from homeassistant.loader import bind_hass, IntegrationNotFound | ||
| from homeassistant.util.dt import parse_datetime, utcnow | ||
|
|
||
|
|
||
|
|
@@ -375,6 +381,50 @@ def device_state_attributes(self): | |
| return {CONF_ID: self._id} | ||
|
|
||
|
|
||
| async def async_validate_config_item(hass, config, full_config): | ||
| """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, hass) | ||
| return None | ||
|
|
||
| return config | ||
|
|
||
|
|
||
| async def async_validate_config(hass, 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. This too .
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. Fixed |
||
| """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 | ||
|
|
||
|
|
||
| async def _async_process_config(hass, config, component): | ||
| """Process config and add automations. | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,18 +1,28 @@ | ||||||
| """Offer device oriented automation.""" | ||||||
| import voluptuous as vol | ||||||
|
|
||||||
| from homeassistant.const import CONF_DOMAIN, CONF_PLATFORM | ||||||
| import homeassistant.components.device_automation as device_automation | ||||||
| from homeassistant.const import CONF_DEVICE_ID, CONF_DOMAIN, CONF_PLATFORM | ||||||
| from homeassistant.loader import async_get_integration | ||||||
|
|
||||||
|
|
||||||
| # mypy: allow-untyped-defs, no-check-untyped-defs | ||||||
|
|
||||||
| TRIGGER_SCHEMA = vol.Schema( | ||||||
|
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. We should expand the TRIGGER_BASE_SCHEMA from
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.
Suggested change
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. Fixed |
||||||
| {vol.Required(CONF_PLATFORM): "device", vol.Required(CONF_DOMAIN): str}, | ||||||
| { | ||||||
| vol.Required(CONF_PLATFORM): "device", | ||||||
| vol.Required(CONF_DEVICE_ID): str, | ||||||
| vol.Required(CONF_DOMAIN): str, | ||||||
| }, | ||||||
| extra=vol.ALLOW_EXTRA, | ||||||
| ) | ||||||
|
|
||||||
|
|
||||||
| async def async_validate_trigger_config(hass, config): | ||||||
| """Validate config.""" | ||||||
| return await device_automation.async_validate_trigger_config(hass, 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. Let's inline this function here.
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. Fixed |
||||||
|
|
||||||
|
|
||||||
| async def async_attach_trigger(hass, config, action, automation_info): | ||||||
| """Listen for trigger.""" | ||||||
| integration = await async_get_integration(hass, config[CONF_DOMAIN]) | ||||||
|
|
||||||
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 move this into a new file
config.pyThere 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