Don't raise on known non-matching states in numeric state condition (#47378)

This commit is contained in:
Franck Nijhof 2021-03-04 22:09:08 +01:00 committed by GitHub
parent f05f60c4c4
commit 7a8b7224c8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 106 deletions

View file

@ -10,12 +10,7 @@ import homeassistant.components.automation as automation
from homeassistant.components.homeassistant.triggers import (
numeric_state as numeric_state_trigger,
)
from homeassistant.const import (
ATTR_ENTITY_ID,
ENTITY_MATCH_ALL,
SERVICE_TURN_OFF,
STATE_UNAVAILABLE,
)
from homeassistant.const import ATTR_ENTITY_ID, ENTITY_MATCH_ALL, SERVICE_TURN_OFF
from homeassistant.core import Context
from homeassistant.setup import async_setup_component
import homeassistant.util.dt as dt_util
@ -347,52 +342,6 @@ async def test_if_fires_on_entity_unavailable_at_startup(hass, calls):
assert len(calls) == 0
async def test_if_not_fires_on_entity_unavailable(hass, calls):
"""Test the firing with entity changing to unavailable."""
# set initial state
hass.states.async_set("test.entity", 9)
await hass.async_block_till_done()
assert await async_setup_component(
hass,
automation.DOMAIN,
{
automation.DOMAIN: {
"trigger": {
"platform": "numeric_state",
"entity_id": "test.entity",
"above": 10,
},
"action": {"service": "test.automation"},
}
},
)
# 11 is above 10
hass.states.async_set("test.entity", 11)
await hass.async_block_till_done()
assert len(calls) == 1
# Going to unavailable and back should not fire
hass.states.async_set("test.entity", STATE_UNAVAILABLE)
await hass.async_block_till_done()
assert len(calls) == 1
hass.states.async_set("test.entity", 11)
await hass.async_block_till_done()
assert len(calls) == 1
# Crossing threshold via unavailable should fire
hass.states.async_set("test.entity", 9)
await hass.async_block_till_done()
assert len(calls) == 1
hass.states.async_set("test.entity", STATE_UNAVAILABLE)
await hass.async_block_till_done()
assert len(calls) == 1
hass.states.async_set("test.entity", 11)
await hass.async_block_till_done()
assert len(calls) == 2
@pytest.mark.parametrize("above", (10, "input_number.value_10"))
async def test_if_fires_on_entity_change_below_to_above(hass, calls, above):
"""Test the firing with changed entity."""
@ -1522,7 +1471,7 @@ async def test_if_fires_on_change_with_for_template_3(hass, calls, above, below)
assert len(calls) == 1
async def test_if_not_fires_on_error_with_for_template(hass, caplog, calls):
async def test_if_not_fires_on_error_with_for_template(hass, calls):
"""Test for not firing on error with for template."""
hass.states.async_set("test.entity", 0)
await hass.async_block_till_done()
@ -1547,17 +1496,11 @@ async def test_if_not_fires_on_error_with_for_template(hass, caplog, calls):
await hass.async_block_till_done()
assert len(calls) == 0
caplog.clear()
caplog.set_level(logging.WARNING)
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=3))
hass.states.async_set("test.entity", "unavailable")
await hass.async_block_till_done()
assert len(calls) == 0
assert len(caplog.record_tuples) == 1
assert caplog.record_tuples[0][1] == logging.WARNING
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=3))
hass.states.async_set("test.entity", 101)
await hass.async_block_till_done()