1
mirror of https://github.com/home-assistant/core synced 2024-07-18 12:02:20 +02:00

Numeric state trigger: validate that above is not above below (#32421)

* Numeric state trigger: validate that above is not above below

* Lint
This commit is contained in:
Paulus Schoutsen 2020-03-03 17:26:44 -08:00 committed by GitHub
parent db7d0eb9b9
commit 4cf86262af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 0 deletions

View File

@ -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__)

View File

@ -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}
)