mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Simplify parsing of script and automation config (#80465)
This commit is contained in:
parent
b6a59b282f
commit
627bd82766
@ -53,7 +53,7 @@ from homeassistant.exceptions import (
|
|||||||
ServiceNotFound,
|
ServiceNotFound,
|
||||||
TemplateError,
|
TemplateError,
|
||||||
)
|
)
|
||||||
from homeassistant.helpers import condition, extract_domain_configs
|
from homeassistant.helpers import condition
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.entity import ToggleEntity
|
from homeassistant.helpers.entity import ToggleEntity
|
||||||
from homeassistant.helpers.entity_component import EntityComponent
|
from homeassistant.helpers.entity_component import EntityComponent
|
||||||
@ -670,7 +670,6 @@ class AutomationEntityConfig:
|
|||||||
"""Container for prepared automation entity configuration."""
|
"""Container for prepared automation entity configuration."""
|
||||||
|
|
||||||
config_block: ConfigType
|
config_block: ConfigType
|
||||||
config_key: str
|
|
||||||
list_no: int
|
list_no: int
|
||||||
raw_blueprint_inputs: ConfigType | None
|
raw_blueprint_inputs: ConfigType | None
|
||||||
raw_config: ConfigType | None
|
raw_config: ConfigType | None
|
||||||
@ -683,38 +682,37 @@ async def _prepare_automation_config(
|
|||||||
"""Parse configuration and prepare automation entity configuration."""
|
"""Parse configuration and prepare automation entity configuration."""
|
||||||
automation_configs: list[AutomationEntityConfig] = []
|
automation_configs: list[AutomationEntityConfig] = []
|
||||||
|
|
||||||
for config_key in extract_domain_configs(config, DOMAIN):
|
conf: list[ConfigType | blueprint.BlueprintInputs] = config[DOMAIN]
|
||||||
conf: list[ConfigType | blueprint.BlueprintInputs] = config[config_key]
|
|
||||||
|
|
||||||
for list_no, config_block in enumerate(conf):
|
for list_no, config_block in enumerate(conf):
|
||||||
raw_blueprint_inputs = None
|
raw_blueprint_inputs = None
|
||||||
raw_config = None
|
raw_config = None
|
||||||
if isinstance(config_block, blueprint.BlueprintInputs):
|
if isinstance(config_block, blueprint.BlueprintInputs):
|
||||||
blueprint_inputs = config_block
|
blueprint_inputs = config_block
|
||||||
raw_blueprint_inputs = blueprint_inputs.config_with_inputs
|
raw_blueprint_inputs = blueprint_inputs.config_with_inputs
|
||||||
|
|
||||||
try:
|
try:
|
||||||
raw_config = blueprint_inputs.async_substitute()
|
raw_config = blueprint_inputs.async_substitute()
|
||||||
config_block = cast(
|
config_block = cast(
|
||||||
dict[str, Any],
|
dict[str, Any],
|
||||||
await async_validate_config_item(hass, raw_config),
|
await async_validate_config_item(hass, raw_config),
|
||||||
)
|
|
||||||
except vol.Invalid as err:
|
|
||||||
LOGGER.error(
|
|
||||||
"Blueprint %s generated invalid automation with inputs %s: %s",
|
|
||||||
blueprint_inputs.blueprint.name,
|
|
||||||
blueprint_inputs.inputs,
|
|
||||||
humanize_error(config_block, err),
|
|
||||||
)
|
|
||||||
continue
|
|
||||||
else:
|
|
||||||
raw_config = cast(AutomationConfig, config_block).raw_config
|
|
||||||
|
|
||||||
automation_configs.append(
|
|
||||||
AutomationEntityConfig(
|
|
||||||
config_block, config_key, list_no, raw_blueprint_inputs, raw_config
|
|
||||||
)
|
)
|
||||||
|
except vol.Invalid as err:
|
||||||
|
LOGGER.error(
|
||||||
|
"Blueprint %s generated invalid automation with inputs %s: %s",
|
||||||
|
blueprint_inputs.blueprint.name,
|
||||||
|
blueprint_inputs.inputs,
|
||||||
|
humanize_error(config_block, err),
|
||||||
|
)
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
raw_config = cast(AutomationConfig, config_block).raw_config
|
||||||
|
|
||||||
|
automation_configs.append(
|
||||||
|
AutomationEntityConfig(
|
||||||
|
config_block, list_no, raw_blueprint_inputs, raw_config
|
||||||
)
|
)
|
||||||
|
)
|
||||||
|
|
||||||
return automation_configs
|
return automation_configs
|
||||||
|
|
||||||
@ -722,9 +720,8 @@ async def _prepare_automation_config(
|
|||||||
def _automation_name(automation_config: AutomationEntityConfig) -> str:
|
def _automation_name(automation_config: AutomationEntityConfig) -> str:
|
||||||
"""Return the configured name of an automation."""
|
"""Return the configured name of an automation."""
|
||||||
config_block = automation_config.config_block
|
config_block = automation_config.config_block
|
||||||
config_key = automation_config.config_key
|
|
||||||
list_no = automation_config.list_no
|
list_no = automation_config.list_no
|
||||||
return config_block.get(CONF_ALIAS) or f"{config_key} {list_no}"
|
return config_block.get(CONF_ALIAS) or f"{DOMAIN} {list_no}"
|
||||||
|
|
||||||
|
|
||||||
async def _create_automation_entities(
|
async def _create_automation_entities(
|
||||||
@ -855,8 +852,7 @@ async def _async_process_config(
|
|||||||
if idx not in config_matches
|
if idx not in config_matches
|
||||||
]
|
]
|
||||||
entities = await _create_automation_entities(hass, updated_automation_configs)
|
entities = await _create_automation_entities(hass, updated_automation_configs)
|
||||||
if entities:
|
await component.async_add_entities(entities)
|
||||||
await component.async_add_entities(entities)
|
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@ from homeassistant.const import (
|
|||||||
STATE_ON,
|
STATE_ON,
|
||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant, ServiceCall, callback
|
from homeassistant.core import HomeAssistant, ServiceCall, callback
|
||||||
from homeassistant.helpers import entity_registry as er, extract_domain_configs
|
from homeassistant.helpers import entity_registry as er
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.config_validation import make_entity_service_schema
|
from homeassistant.helpers.config_validation import make_entity_service_schema
|
||||||
from homeassistant.helpers.entity import ToggleEntity
|
from homeassistant.helpers.entity import ToggleEntity
|
||||||
@ -238,37 +238,36 @@ async def _async_process_config(hass, config, component) -> None:
|
|||||||
"""
|
"""
|
||||||
entities = []
|
entities = []
|
||||||
|
|
||||||
for config_key in extract_domain_configs(config, DOMAIN):
|
conf: dict[str, dict[str, Any] | BlueprintInputs] = config[DOMAIN]
|
||||||
conf: dict[str, dict[str, Any] | BlueprintInputs] = config[config_key]
|
|
||||||
|
|
||||||
for key, config_block in conf.items():
|
for key, config_block in conf.items():
|
||||||
raw_blueprint_inputs = None
|
raw_blueprint_inputs = None
|
||||||
raw_config = None
|
raw_config = None
|
||||||
|
|
||||||
if isinstance(config_block, BlueprintInputs):
|
if isinstance(config_block, BlueprintInputs):
|
||||||
blueprint_inputs = config_block
|
blueprint_inputs = config_block
|
||||||
raw_blueprint_inputs = blueprint_inputs.config_with_inputs
|
raw_blueprint_inputs = blueprint_inputs.config_with_inputs
|
||||||
|
|
||||||
try:
|
try:
|
||||||
raw_config = blueprint_inputs.async_substitute()
|
raw_config = blueprint_inputs.async_substitute()
|
||||||
config_block = cast(
|
config_block = cast(
|
||||||
dict[str, Any],
|
dict[str, Any],
|
||||||
await async_validate_config_item(hass, raw_config),
|
await async_validate_config_item(hass, raw_config),
|
||||||
)
|
)
|
||||||
except vol.Invalid as err:
|
except vol.Invalid as err:
|
||||||
LOGGER.error(
|
LOGGER.error(
|
||||||
"Blueprint %s generated invalid script with input %s: %s",
|
"Blueprint %s generated invalid script with input %s: %s",
|
||||||
blueprint_inputs.blueprint.name,
|
blueprint_inputs.blueprint.name,
|
||||||
blueprint_inputs.inputs,
|
blueprint_inputs.inputs,
|
||||||
humanize_error(config_block, err),
|
humanize_error(config_block, err),
|
||||||
)
|
)
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
raw_config = cast(ScriptConfig, config_block).raw_config
|
raw_config = cast(ScriptConfig, config_block).raw_config
|
||||||
|
|
||||||
entities.append(
|
entities.append(
|
||||||
ScriptEntity(hass, key, config_block, raw_config, raw_blueprint_inputs)
|
ScriptEntity(hass, key, config_block, raw_config, raw_blueprint_inputs)
|
||||||
)
|
)
|
||||||
|
|
||||||
await component.async_add_entities(entities)
|
await component.async_add_entities(entities)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user