diff --git a/homeassistant/components/automation/numeric_state.py b/homeassistant/components/automation/numeric_state.py index e944b66751bc..d8f71f5bdf30 100644 --- a/homeassistant/components/automation/numeric_state.py +++ b/homeassistant/components/automation/numeric_state.py @@ -19,6 +19,23 @@ from homeassistant.helpers.event import async_track_same_state, async_track_stat # mypy: allow-incomplete-defs, allow-untyped-calls, allow-untyped-defs # mypy: no-check-untyped-defs + +def validate_above_below(value): + """Validate that above and below can co-exist.""" + above = value.get(CONF_ABOVE) + below = value.get(CONF_BELOW) + + if above is None or below is None: + return value + + if above > below: + raise vol.Invalid( + f"A value can never be above {above} and below {below} at the same time. You probably want two different triggers.", + ) + + return value + + TRIGGER_SCHEMA = vol.All( vol.Schema( { @@ -35,6 +52,7 @@ TRIGGER_SCHEMA = vol.All( } ), cv.has_at_least_one_key(CONF_BELOW, CONF_ABOVE), + validate_above_below, ) _LOGGER = logging.getLogger(__name__) diff --git a/tests/components/automation/test_numeric_state.py b/tests/components/automation/test_numeric_state.py index 17cb8e38136b..f779f022e65d 100644 --- a/tests/components/automation/test_numeric_state.py +++ b/tests/components/automation/test_numeric_state.py @@ -3,8 +3,10 @@ from datetime import timedelta from unittest.mock import patch import pytest +import voluptuous as vol import homeassistant.components.automation as automation +from homeassistant.components.automation import numeric_state from homeassistant.core import Context from homeassistant.setup import async_setup_component import homeassistant.util.dt as dt_util @@ -1229,3 +1231,11 @@ async def test_if_fires_on_entities_change_overlap_for_template(hass, calls): await hass.async_block_till_done() assert 2 == len(calls) assert "test.entity_2 - 0:00:10" == calls[1].data["some"] + + +def test_below_above(): + """Test above cannot be above below.""" + with pytest.raises(vol.Invalid): + numeric_state.TRIGGER_SCHEMA( + {"platform": "numeric_state", "above": 1200, "below": 1000} + )