mirror of
https://github.com/home-assistant/core.git
synced 2025-11-10 11:29:46 +00:00
Enhance automation integration to use new features in script helper (#37479)
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
"""Config validation helper for the automation integration."""
|
||||
import asyncio
|
||||
import importlib
|
||||
import logging
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
@@ -8,13 +9,20 @@ from homeassistant.components.device_automation.exceptions import (
|
||||
InvalidDeviceAutomationConfig,
|
||||
)
|
||||
from homeassistant.config import async_log_exception, config_without_domain
|
||||
from homeassistant.const import CONF_PLATFORM
|
||||
from homeassistant.const import CONF_ALIAS, CONF_ID, CONF_MODE, CONF_PLATFORM
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.helpers import condition, config_per_platform, script
|
||||
from homeassistant.helpers import condition, config_per_platform
|
||||
from homeassistant.helpers.script import (
|
||||
SCRIPT_MODE_LEGACY,
|
||||
async_validate_action_config,
|
||||
warn_deprecated_legacy,
|
||||
)
|
||||
from homeassistant.loader import IntegrationNotFound
|
||||
|
||||
from . import CONF_ACTION, CONF_CONDITION, CONF_TRIGGER, DOMAIN, PLATFORM_SCHEMA
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
# mypy: allow-untyped-calls, allow-untyped-defs
|
||||
# mypy: no-check-untyped-defs, no-warn-return-any
|
||||
|
||||
@@ -44,10 +52,7 @@ async def async_validate_config_item(hass, config, full_config=None):
|
||||
)
|
||||
|
||||
config[CONF_ACTION] = await asyncio.gather(
|
||||
*[
|
||||
script.async_validate_action_config(hass, action)
|
||||
for action in config[CONF_ACTION]
|
||||
]
|
||||
*[async_validate_action_config(hass, action) for action in config[CONF_ACTION]]
|
||||
)
|
||||
|
||||
return config
|
||||
@@ -69,24 +74,54 @@ async def _try_async_validate_config_item(hass, config, full_config=None):
|
||||
return config
|
||||
|
||||
|
||||
def _deprecated_legacy_mode(config):
|
||||
legacy_names = []
|
||||
legacy_unnamed_found = False
|
||||
|
||||
for cfg in config[DOMAIN]:
|
||||
mode = cfg.get(CONF_MODE)
|
||||
if mode is None:
|
||||
cfg[CONF_MODE] = SCRIPT_MODE_LEGACY
|
||||
name = cfg.get(CONF_ID) or cfg.get(CONF_ALIAS)
|
||||
if name:
|
||||
legacy_names.append(name)
|
||||
else:
|
||||
legacy_unnamed_found = True
|
||||
|
||||
if legacy_names or legacy_unnamed_found:
|
||||
msgs = []
|
||||
if legacy_unnamed_found:
|
||||
msgs.append("unnamed automations")
|
||||
if legacy_names:
|
||||
if len(legacy_names) == 1:
|
||||
base_msg = "this automation"
|
||||
else:
|
||||
base_msg = "these automations"
|
||||
msgs.append(f"{base_msg}: {', '.join(legacy_names)}")
|
||||
warn_deprecated_legacy(_LOGGER, " and ".join(msgs))
|
||||
|
||||
return config
|
||||
|
||||
|
||||
async def async_validate_config(hass, config):
|
||||
"""Validate config."""
|
||||
validated_automations = await asyncio.gather(
|
||||
*(
|
||||
_try_async_validate_config_item(hass, p_config, config)
|
||||
for _, p_config in config_per_platform(config, DOMAIN)
|
||||
automations = list(
|
||||
filter(
|
||||
lambda x: x is not None,
|
||||
await asyncio.gather(
|
||||
*(
|
||||
_try_async_validate_config_item(hass, p_config, config)
|
||||
for _, p_config in config_per_platform(config, DOMAIN)
|
||||
)
|
||||
),
|
||||
)
|
||||
)
|
||||
|
||||
automations = [
|
||||
validated_automation
|
||||
for validated_automation in validated_automations
|
||||
if validated_automation is not None
|
||||
]
|
||||
|
||||
# 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
|
||||
|
||||
_deprecated_legacy_mode(config)
|
||||
|
||||
return config
|
||||
|
||||
Reference in New Issue
Block a user