From c7057367390a9d0a249821c52491c0dc6b9dbd19 Mon Sep 17 00:00:00 2001 From: Erik Date: Tue, 13 May 2025 08:22:16 +0200 Subject: [PATCH] Improve condition schema validation --- homeassistant/helpers/config_validation.py | 2 +- tests/helpers/test_condition.py | 33 ++++++++++++++++++---- tests/helpers/test_config_validation.py | 6 ++++ 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/homeassistant/helpers/config_validation.py b/homeassistant/helpers/config_validation.py index 31a3e365071..69fbc7e4310 100644 --- a/homeassistant/helpers/config_validation.py +++ b/homeassistant/helpers/config_validation.py @@ -1758,7 +1758,7 @@ def _base_condition_validator(value: Any) -> Any: vol.Schema( { **CONDITION_BASE_SCHEMA, - CONF_CONDITION: vol.NotIn(BUILT_IN_CONDITIONS), + CONF_CONDITION: vol.All(str, vol.NotIn(BUILT_IN_CONDITIONS)), }, extra=vol.ALLOW_EXTRA, )(value) diff --git a/tests/helpers/test_condition.py b/tests/helpers/test_condition.py index 7285301f12b..579212d6622 100644 --- a/tests/helpers/test_condition.py +++ b/tests/helpers/test_condition.py @@ -70,11 +70,26 @@ def assert_condition_trace(expected): assert_element(condition_trace[key][index], element, path) -async def test_invalid_condition(hass: HomeAssistant) -> None: - """Test if invalid condition raises.""" - with pytest.raises(HomeAssistantError): - await condition.async_from_config( - hass, +@pytest.mark.parametrize( + ("config", "error"), + [ + ( + {"condition": 123}, + "Unexpected value for condition: '123'. Expected and, device, not, " + "numeric_state, or, state, template, time, trigger, zone", + ) + ], +) +async def test_invalid_condition(hass: HomeAssistant, config: dict, error: str) -> None: + """Test if validating an invalid condition raises.""" + with pytest.raises(vol.Invalid, match=error): + cv.CONDITION_SCHEMA(config) + + +@pytest.mark.parametrize( + ("config", "error"), + [ + ( { "condition": "invalid", "conditions": [ @@ -85,7 +100,15 @@ async def test_invalid_condition(hass: HomeAssistant) -> None: }, ], }, + 'Invalid condition "invalid" specified', ) + ], +) +async def test_unknown_condition(hass: HomeAssistant, config: dict, error: str) -> None: + """Test if creating an unknown condition raises.""" + config = cv.CONDITION_SCHEMA(config) + with pytest.raises(HomeAssistantError, match=error): + await condition.async_from_config(hass, config) async def test_and_condition(hass: HomeAssistant) -> None: diff --git a/tests/helpers/test_config_validation.py b/tests/helpers/test_config_validation.py index aec687be40a..38520b11527 100644 --- a/tests/helpers/test_config_validation.py +++ b/tests/helpers/test_config_validation.py @@ -1462,6 +1462,12 @@ def test_key_value_schemas_with_default() -> None: ({"wait_template": "{{ invalid"}, "invalid template"), # The validation error message could be improved to explain that this is not # a valid shorthand template + ( + {"condition": 123}, + "Unexpected value for condition: '123'. Expected and, device, not, " + "numeric_state, or, state, template, time, trigger, zone, a list of " + "conditions or a valid template", + ), ( {"condition": "not", "conditions": "not a dynamic template"}, "Expected a dictionary",