Wrap shorthand template conditions during schema validation (#62485)
This commit is contained in:
parent
6d1c4a4f5f
commit
e48f567176
3 changed files with 17 additions and 17 deletions
|
@ -153,19 +153,12 @@ def trace_condition_function(condition: ConditionCheckerType) -> ConditionChecke
|
|||
|
||||
async def async_from_config(
|
||||
hass: HomeAssistant,
|
||||
config: ConfigType | Template,
|
||||
config: ConfigType,
|
||||
) -> ConditionCheckerType:
|
||||
"""Turn a condition configuration into a method.
|
||||
|
||||
Should be run on the event loop.
|
||||
"""
|
||||
if isinstance(config, Template):
|
||||
# We got a condition template, wrap it in a configuration to pass along.
|
||||
config = {
|
||||
CONF_CONDITION: "template",
|
||||
CONF_VALUE_TEMPLATE: config,
|
||||
}
|
||||
|
||||
condition = config.get(CONF_CONDITION)
|
||||
for fmt in (ASYNC_FROM_CONFIG_FORMAT, FROM_CONFIG_FORMAT):
|
||||
factory = getattr(sys.modules[__name__], fmt.format(condition), None)
|
||||
|
@ -935,12 +928,9 @@ def state_validate_config(hass: HomeAssistant, config: ConfigType) -> ConfigType
|
|||
|
||||
|
||||
async def async_validate_condition_config(
|
||||
hass: HomeAssistant, config: ConfigType | Template
|
||||
) -> ConfigType | Template:
|
||||
hass: HomeAssistant, config: ConfigType
|
||||
) -> ConfigType:
|
||||
"""Validate config."""
|
||||
if isinstance(config, Template):
|
||||
return config
|
||||
|
||||
condition = config[CONF_CONDITION]
|
||||
if condition in ("and", "not", "or"):
|
||||
conditions = []
|
||||
|
@ -951,7 +941,6 @@ async def async_validate_condition_config(
|
|||
|
||||
if condition == "device":
|
||||
config = cv.DEVICE_CONDITION_SCHEMA(config)
|
||||
assert not isinstance(config, Template)
|
||||
platform = await async_get_device_automation_platform(
|
||||
hass, config[CONF_DOMAIN], DeviceAutomationType.CONDITION
|
||||
)
|
||||
|
@ -969,7 +958,7 @@ async def async_validate_condition_config(
|
|||
|
||||
|
||||
async def async_validate_conditions_config(
|
||||
hass: HomeAssistant, conditions: list[ConfigType | Template]
|
||||
hass: HomeAssistant, conditions: list[ConfigType]
|
||||
) -> list[ConfigType | Template]:
|
||||
"""Validate config."""
|
||||
return await asyncio.gather(
|
||||
|
|
|
@ -1195,6 +1195,16 @@ DEVICE_CONDITION_BASE_SCHEMA = vol.Schema(
|
|||
|
||||
DEVICE_CONDITION_SCHEMA = DEVICE_CONDITION_BASE_SCHEMA.extend({}, extra=vol.ALLOW_EXTRA)
|
||||
|
||||
dynamic_template_condition_action = vol.All(
|
||||
# Wrap a shorthand template condition in a template condition
|
||||
dynamic_template,
|
||||
lambda config: {
|
||||
CONF_VALUE_TEMPLATE: config,
|
||||
CONF_CONDITION: "template",
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
CONDITION_SCHEMA: vol.Schema = vol.Schema(
|
||||
vol.Any(
|
||||
key_value_schemas(
|
||||
|
@ -1213,12 +1223,13 @@ CONDITION_SCHEMA: vol.Schema = vol.Schema(
|
|||
"zone": ZONE_CONDITION_SCHEMA,
|
||||
},
|
||||
),
|
||||
dynamic_template,
|
||||
dynamic_template_condition_action,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
dynamic_template_condition_action = vol.All(
|
||||
# Wrap a shorthand template condition action in a template condition
|
||||
vol.Schema(
|
||||
{**CONDITION_BASE_SCHEMA, vol.Required(CONF_CONDITION): dynamic_template}
|
||||
),
|
||||
|
|
|
@ -262,7 +262,7 @@ async def async_validate_action_config(
|
|||
config = platform.ACTION_SCHEMA(config) # type: ignore
|
||||
|
||||
elif action_type == cv.SCRIPT_ACTION_CHECK_CONDITION:
|
||||
config = await condition.async_validate_condition_config(hass, config) # type: ignore
|
||||
config = await condition.async_validate_condition_config(hass, config)
|
||||
|
||||
elif action_type == cv.SCRIPT_ACTION_WAIT_FOR_TRIGGER:
|
||||
config[CONF_WAIT_FOR_TRIGGER] = await async_validate_trigger_config(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue