Remove optional validation when creating conditions ()

pull/60493/head
Erik Montnemery 2021-11-28 14:54:07 +01:00 committed by GitHub
parent 76b047dd12
commit caf5ee2fab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
24 changed files with 486 additions and 580 deletions
script/scaffold/templates/device_condition/integration
tests

View File

@ -104,12 +104,8 @@ async def async_get_conditions(
return conditions
def async_condition_from_config(
config: ConfigType, config_validation: bool
) -> condition.ConditionCheckerType:
def async_condition_from_config(config: ConfigType) -> condition.ConditionCheckerType:
"""Create a function to test a device condition."""
if config_validation:
config = CONDITION_SCHEMA(config)
if config[CONF_TYPE] == CONDITION_TRIGGERED:
state = STATE_ALARM_TRIGGERED
elif config[CONF_TYPE] == CONDITION_DISARMED:

View File

@ -714,7 +714,7 @@ async def _async_process_if(hass, name, config, p_config):
checks = []
for if_config in if_configs:
try:
checks.append(await condition.async_from_config(hass, if_config, False))
checks.append(await condition.async_from_config(hass, if_config))
except HomeAssistantError as ex:
LOGGER.warning("Invalid condition: %s", ex)
return None

View File

@ -264,12 +264,8 @@ async def async_get_conditions(
@callback
def async_condition_from_config(
config: ConfigType, config_validation: bool
) -> condition.ConditionCheckerType:
def async_condition_from_config(config: ConfigType) -> condition.ConditionCheckerType:
"""Evaluate state based on configuration."""
if config_validation:
config = CONDITION_SCHEMA(config)
condition_type = config[CONF_TYPE]
if condition_type in IS_ON:
stat = "on"
@ -282,6 +278,7 @@ def async_condition_from_config(
}
if CONF_FOR in config:
state_config[CONF_FOR] = config[CONF_FOR]
state_config = cv.STATE_CONDITION_SCHEMA(state_config)
return condition.state_from_config(state_config)

View File

@ -70,13 +70,8 @@ async def async_get_conditions(
@callback
def async_condition_from_config(
config: ConfigType, config_validation: bool
) -> condition.ConditionCheckerType:
def async_condition_from_config(config: ConfigType) -> condition.ConditionCheckerType:
"""Create a function to test a device condition."""
if config_validation:
config = CONDITION_SCHEMA(config)
if config[CONF_TYPE] == "is_hvac_mode":
attribute = const.ATTR_HVAC_MODE
else:

View File

@ -119,13 +119,8 @@ async def async_get_condition_capabilities(
@callback
def async_condition_from_config(
config: ConfigType, config_validation: bool
) -> condition.ConditionCheckerType:
def async_condition_from_config(config: ConfigType) -> condition.ConditionCheckerType:
"""Create a function to test a device condition."""
if config_validation:
config = CONDITION_SCHEMA(config)
if config[CONF_TYPE] in STATE_CONDITION_TYPES:
if config[CONF_TYPE] == "is_open":
state = STATE_OPEN

View File

@ -141,6 +141,7 @@ def async_condition_from_config(config: ConfigType) -> condition.ConditionChecke
if CONF_FOR in config:
state_config[CONF_FOR] = config[CONF_FOR]
state_config = cv.STATE_CONDITION_SCHEMA(state_config)
return condition.state_from_config(state_config)

View File

@ -55,13 +55,8 @@ async def async_get_conditions(
@callback
def async_condition_from_config(
config: ConfigType, config_validation: bool
) -> condition.ConditionCheckerType:
def async_condition_from_config(config: ConfigType) -> condition.ConditionCheckerType:
"""Create a function to test a device condition."""
if config_validation:
config = CONDITION_SCHEMA(config)
reverse = config[CONF_TYPE] == "is_not_home"
@callback

View File

@ -55,12 +55,8 @@ async def async_get_conditions(
@callback
def async_condition_from_config(
config: ConfigType, config_validation: bool
) -> condition.ConditionCheckerType:
def async_condition_from_config(config: ConfigType) -> condition.ConditionCheckerType:
"""Create a function to test a device condition."""
if config_validation:
config = CONDITION_SCHEMA(config)
if config[CONF_TYPE] == "is_on":
state = STATE_ON
else:

View File

@ -65,13 +65,8 @@ async def async_get_conditions(
@callback
def async_condition_from_config(
config: ConfigType, config_validation: bool
) -> condition.ConditionCheckerType:
def async_condition_from_config(config: ConfigType) -> condition.ConditionCheckerType:
"""Create a function to test a device condition."""
if config_validation:
config = CONDITION_SCHEMA(config)
if config[CONF_TYPE] == "is_mode":
attribute = ATTR_MODE
else:

View File

@ -19,12 +19,8 @@ CONDITION_SCHEMA = toggle_entity.CONDITION_SCHEMA.extend(
@callback
def async_condition_from_config(
config: ConfigType, config_validation: bool
) -> ConditionCheckerType:
def async_condition_from_config(config: ConfigType) -> ConditionCheckerType:
"""Evaluate state based on configuration."""
if config_validation:
config = CONDITION_SCHEMA(config)
return toggle_entity.async_condition_from_config(config)

View File

@ -67,12 +67,8 @@ async def async_get_conditions(
@callback
def async_condition_from_config(
config: ConfigType, config_validation: bool
) -> condition.ConditionCheckerType:
def async_condition_from_config(config: ConfigType) -> condition.ConditionCheckerType:
"""Create a function to test a device condition."""
if config_validation:
config = CONDITION_SCHEMA(config)
if config[CONF_TYPE] == "is_jammed":
state = STATE_JAMMED
elif config[CONF_TYPE] == "is_locking":

View File

@ -59,12 +59,8 @@ async def async_get_conditions(
@callback
def async_condition_from_config(
config: ConfigType, config_validation: bool
) -> condition.ConditionCheckerType:
def async_condition_from_config(config: ConfigType) -> condition.ConditionCheckerType:
"""Create a function to test a device condition."""
if config_validation:
config = CONDITION_SCHEMA(config)
if config[CONF_TYPE] == "is_playing":
state = STATE_PLAYING
elif config[CONF_TYPE] == "is_idle":

View File

@ -19,12 +19,8 @@ CONDITION_SCHEMA = toggle_entity.CONDITION_SCHEMA.extend(
@callback
def async_condition_from_config(
config: ConfigType, config_validation: bool
) -> ConditionCheckerType:
def async_condition_from_config(config: ConfigType) -> ConditionCheckerType:
"""Evaluate state based on configuration."""
if config_validation:
config = CONDITION_SCHEMA(config)
return toggle_entity.async_condition_from_config(config)

View File

@ -52,12 +52,8 @@ async def async_get_conditions(
@callback
def async_condition_from_config(
config: ConfigType, config_validation: bool
) -> condition.ConditionCheckerType:
def async_condition_from_config(config: ConfigType) -> condition.ConditionCheckerType:
"""Create a function to test a device condition."""
if config_validation:
config = CONDITION_SCHEMA(config)
@callback
def test_is_state(hass: HomeAssistant, variables: TemplateVarsType) -> bool:

View File

@ -186,12 +186,8 @@ async def async_get_conditions(
@callback
def async_condition_from_config(
config: ConfigType, config_validation: bool
) -> condition.ConditionCheckerType:
def async_condition_from_config(config: ConfigType) -> condition.ConditionCheckerType:
"""Evaluate state based on configuration."""
if config_validation:
config = CONDITION_SCHEMA(config)
numeric_state_config = {
condition.CONF_CONDITION: "numeric_state",
condition.CONF_ENTITY_ID: config[CONF_ENTITY_ID],
@ -201,6 +197,7 @@ def async_condition_from_config(
if CONF_BELOW in config:
numeric_state_config[condition.CONF_BELOW] = config[CONF_BELOW]
numeric_state_config = cv.NUMERIC_STATE_CONDITION_SCHEMA(numeric_state_config)
return condition.async_numeric_state_from_config(numeric_state_config)

View File

@ -19,12 +19,8 @@ CONDITION_SCHEMA = toggle_entity.CONDITION_SCHEMA.extend(
@callback
def async_condition_from_config(
config: ConfigType, config_validation: bool
) -> ConditionCheckerType:
def async_condition_from_config(config: ConfigType) -> ConditionCheckerType:
"""Evaluate state based on configuration."""
if config_validation:
config = CONDITION_SCHEMA(config)
return toggle_entity.async_condition_from_config(config)

View File

@ -53,12 +53,8 @@ async def async_get_conditions(
@callback
def async_condition_from_config(
config: ConfigType, config_validation: bool
) -> condition.ConditionCheckerType:
def async_condition_from_config(config: ConfigType) -> condition.ConditionCheckerType:
"""Create a function to test a device condition."""
if config_validation:
config = CONDITION_SCHEMA(config)
if config[CONF_TYPE] == "is_docked":
test_states = [STATE_DOCKED]
else:

View File

@ -495,7 +495,11 @@ async def handle_test_condition(
# pylint: disable=import-outside-toplevel
from homeassistant.helpers import condition
check_condition = await condition.async_from_config(hass, msg["condition"])
# Do static + dynamic validation of the condition
config = cv.CONDITION_SCHEMA(msg["condition"])
config = await condition.async_validate_condition_config(hass, config)
# Test the condition
check_condition = await condition.async_from_config(hass, config)
connection.send_result(
msg["id"], {"result": check_condition(hass, msg.get("variables"))}
)

View File

@ -147,13 +147,8 @@ async def async_get_conditions(
@callback
def async_condition_from_config(
config: ConfigType, config_validation: bool
) -> condition.ConditionCheckerType:
def async_condition_from_config(config: ConfigType) -> condition.ConditionCheckerType:
"""Create a function to test a device condition."""
if config_validation:
config = CONDITION_SCHEMA(config)
condition_type = config[CONF_TYPE]
device_id = config[CONF_DEVICE_ID]

View File

@ -152,7 +152,6 @@ def trace_condition_function(condition: ConditionCheckerType) -> ConditionChecke
async def async_from_config(
hass: HomeAssistant,
config: ConfigType | Template,
config_validation: bool = True,
) -> ConditionCheckerType:
"""Turn a condition configuration into a method.
@ -181,21 +180,15 @@ async def async_from_config(
check_factory = check_factory.func
if asyncio.iscoroutinefunction(check_factory):
return cast(
ConditionCheckerType, await factory(hass, config, config_validation)
)
return cast(ConditionCheckerType, factory(config, config_validation))
return cast(ConditionCheckerType, await factory(hass, config))
return cast(ConditionCheckerType, factory(config))
async def async_and_from_config(
hass: HomeAssistant, config: ConfigType, config_validation: bool = True
hass: HomeAssistant, config: ConfigType
) -> ConditionCheckerType:
"""Create multi condition matcher using 'AND'."""
if config_validation:
config = cv.AND_CONDITION_SCHEMA(config)
checks = [
await async_from_config(hass, entry, False) for entry in config["conditions"]
]
checks = [await async_from_config(hass, entry) for entry in config["conditions"]]
@trace_condition_function
def if_and_condition(
@ -223,14 +216,10 @@ async def async_and_from_config(
async def async_or_from_config(
hass: HomeAssistant, config: ConfigType, config_validation: bool = True
hass: HomeAssistant, config: ConfigType
) -> ConditionCheckerType:
"""Create multi condition matcher using 'OR'."""
if config_validation:
config = cv.OR_CONDITION_SCHEMA(config)
checks = [
await async_from_config(hass, entry, False) for entry in config["conditions"]
]
checks = [await async_from_config(hass, entry) for entry in config["conditions"]]
@trace_condition_function
def if_or_condition(
@ -258,14 +247,10 @@ async def async_or_from_config(
async def async_not_from_config(
hass: HomeAssistant, config: ConfigType, config_validation: bool = True
hass: HomeAssistant, config: ConfigType
) -> ConditionCheckerType:
"""Create multi condition matcher using 'NOT'."""
if config_validation:
config = cv.NOT_CONDITION_SCHEMA(config)
checks = [
await async_from_config(hass, entry, False) for entry in config["conditions"]
]
checks = [await async_from_config(hass, entry) for entry in config["conditions"]]
@trace_condition_function
def if_not_condition(
@ -433,12 +418,8 @@ def async_numeric_state( # noqa: C901
return True
def async_numeric_state_from_config(
config: ConfigType, config_validation: bool = True
) -> ConditionCheckerType:
def async_numeric_state_from_config(config: ConfigType) -> ConditionCheckerType:
"""Wrap action method with state based condition."""
if config_validation:
config = cv.NUMERIC_STATE_CONDITION_SCHEMA(config)
entity_ids = config.get(CONF_ENTITY_ID, [])
attribute = config.get(CONF_ATTRIBUTE)
below = config.get(CONF_BELOW)
@ -548,12 +529,8 @@ def state(
return duration_ok
def state_from_config(
config: ConfigType, config_validation: bool = True
) -> ConditionCheckerType:
def state_from_config(config: ConfigType) -> ConditionCheckerType:
"""Wrap action method with state based condition."""
if config_validation:
config = cv.STATE_CONDITION_SCHEMA(config)
entity_ids = config.get(CONF_ENTITY_ID, [])
req_states: str | list[str] = config.get(CONF_STATE, [])
for_period = config.get("for")
@ -656,12 +633,8 @@ def sun(
return True
def sun_from_config(
config: ConfigType, config_validation: bool = True
) -> ConditionCheckerType:
def sun_from_config(config: ConfigType) -> ConditionCheckerType:
"""Wrap action method with sun based condition."""
if config_validation:
config = cv.SUN_CONDITION_SCHEMA(config)
before = config.get("before")
after = config.get("after")
before_offset = config.get("before_offset")
@ -703,12 +676,8 @@ def async_template(
return result
def async_template_from_config(
config: ConfigType, config_validation: bool = True
) -> ConditionCheckerType:
def async_template_from_config(config: ConfigType) -> ConditionCheckerType:
"""Wrap action method with state based condition."""
if config_validation:
config = cv.TEMPLATE_CONDITION_SCHEMA(config)
value_template = cast(Template, config.get(CONF_VALUE_TEMPLATE))
@trace_condition_function
@ -808,12 +777,8 @@ def time(
return True
def time_from_config(
config: ConfigType, config_validation: bool = True
) -> ConditionCheckerType:
def time_from_config(config: ConfigType) -> ConditionCheckerType:
"""Wrap action method with time based condition."""
if config_validation:
config = cv.TIME_CONDITION_SCHEMA(config)
before = config.get(CONF_BEFORE)
after = config.get(CONF_AFTER)
weekday = config.get(CONF_WEEKDAY)
@ -873,12 +838,8 @@ def zone(
)
def zone_from_config(
config: ConfigType, config_validation: bool = True
) -> ConditionCheckerType:
def zone_from_config(config: ConfigType) -> ConditionCheckerType:
"""Wrap action method with zone based condition."""
if config_validation:
config = cv.ZONE_CONDITION_SCHEMA(config)
entity_ids = config.get(CONF_ENTITY_ID, [])
zone_entity_ids = config.get(CONF_ZONE, [])
@ -915,28 +876,24 @@ def zone_from_config(
async def async_device_from_config(
hass: HomeAssistant, config: ConfigType, config_validation: bool = True
hass: HomeAssistant, config: ConfigType
) -> ConditionCheckerType:
"""Test a device condition."""
if config_validation:
config = cv.DEVICE_CONDITION_SCHEMA(config)
platform = await async_get_device_automation_platform(
hass, config[CONF_DOMAIN], "condition"
)
return trace_condition_function(
cast(
ConditionCheckerType,
platform.async_condition_from_config(config, config_validation), # type: ignore
platform.async_condition_from_config(config), # type: ignore
)
)
async def async_trigger_from_config(
hass: HomeAssistant, config: ConfigType, config_validation: bool = True
hass: HomeAssistant, config: ConfigType
) -> ConditionCheckerType:
"""Test a trigger condition."""
if config_validation:
config = cv.TRIGGER_CONDITION_SCHEMA(config)
trigger_id = config[CONF_ID]
@trace_condition_function

View File

@ -1275,7 +1275,7 @@ class Script:
else:
config_cache_key = frozenset((k, str(v)) for k, v in config.items())
if not (cond := self._config_cache.get(config_cache_key)):
cond = await condition.async_from_config(self._hass, config, False)
cond = await condition.async_from_config(self._hass, config)
self._config_cache[config_cache_key] = cond
return cond

View File

@ -58,12 +58,8 @@ async def async_get_conditions(
@callback
def async_condition_from_config(
config: ConfigType, config_validation: bool
) -> condition.ConditionCheckerType:
def async_condition_from_config(config: ConfigType) -> condition.ConditionCheckerType:
"""Create a function to test a device condition."""
if config_validation:
config = CONDITION_SCHEMA(config)
if config[CONF_TYPE] == "is_on":
state = STATE_ON
else:

View File

@ -552,7 +552,7 @@ async def test_failure_scenarios(hass, client, hank_binary_switch, integration):
with pytest.raises(HomeAssistantError):
await device_condition.async_condition_from_config(
{"type": "failed.test", "device_id": device.id}, False
{"type": "failed.test", "device_id": device.id}
)
with patch(

File diff suppressed because it is too large Load Diff