Fix numeric_state condition spamming on unavailable (#12550)
This commit is contained in:
parent
7829e61361
commit
3077444d62
2 changed files with 22 additions and 1 deletions
|
@ -13,7 +13,7 @@ from homeassistant.const import (
|
||||||
CONF_ENTITY_ID, CONF_VALUE_TEMPLATE, CONF_CONDITION,
|
CONF_ENTITY_ID, CONF_VALUE_TEMPLATE, CONF_CONDITION,
|
||||||
WEEKDAYS, CONF_STATE, CONF_ZONE, CONF_BEFORE,
|
WEEKDAYS, CONF_STATE, CONF_ZONE, CONF_BEFORE,
|
||||||
CONF_AFTER, CONF_WEEKDAY, SUN_EVENT_SUNRISE, SUN_EVENT_SUNSET,
|
CONF_AFTER, CONF_WEEKDAY, SUN_EVENT_SUNRISE, SUN_EVENT_SUNSET,
|
||||||
CONF_BELOW, CONF_ABOVE)
|
CONF_BELOW, CONF_ABOVE, STATE_UNAVAILABLE, STATE_UNKNOWN)
|
||||||
from homeassistant.exceptions import TemplateError, HomeAssistantError
|
from homeassistant.exceptions import TemplateError, HomeAssistantError
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.sun import get_astral_event_date
|
from homeassistant.helpers.sun import get_astral_event_date
|
||||||
|
@ -160,6 +160,9 @@ def async_numeric_state(hass: HomeAssistant, entity, below=None, above=None,
|
||||||
_LOGGER.error("Template error: %s", ex)
|
_LOGGER.error("Template error: %s", ex)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
if value in (STATE_UNAVAILABLE, STATE_UNKNOWN):
|
||||||
|
return False
|
||||||
|
|
||||||
try:
|
try:
|
||||||
value = float(value)
|
value = float(value)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
|
|
|
@ -146,3 +146,21 @@ class TestConditionHelper:
|
||||||
return_value=dt.now().replace(hour=21)):
|
return_value=dt.now().replace(hour=21)):
|
||||||
assert not condition.time(after=sixam, before=sixpm)
|
assert not condition.time(after=sixam, before=sixpm)
|
||||||
assert condition.time(after=sixpm, before=sixam)
|
assert condition.time(after=sixpm, before=sixam)
|
||||||
|
|
||||||
|
def test_if_numeric_state_not_raise_on_unavailable(self):
|
||||||
|
"""Test numeric_state doesn't raise on unavailable/unknown state."""
|
||||||
|
test = condition.from_config({
|
||||||
|
'condition': 'numeric_state',
|
||||||
|
'entity_id': 'sensor.temperature',
|
||||||
|
'below': 42
|
||||||
|
})
|
||||||
|
|
||||||
|
with patch('homeassistant.helpers.condition._LOGGER.warning') \
|
||||||
|
as logwarn:
|
||||||
|
self.hass.states.set('sensor.temperature', 'unavailable')
|
||||||
|
assert not test(self.hass)
|
||||||
|
assert len(logwarn.mock_calls) == 0
|
||||||
|
|
||||||
|
self.hass.states.set('sensor.temperature', 'unknown')
|
||||||
|
assert not test(self.hass)
|
||||||
|
assert len(logwarn.mock_calls) == 0
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue