Fix numeric_state condition spamming on unavailable (#12550)

pull/12558/head
Otto Winter 2018-02-20 17:02:27 +01:00 committed by Pascal Vizeli
parent 7829e61361
commit 3077444d62
2 changed files with 22 additions and 1 deletions

View File

@ -13,7 +13,7 @@ from homeassistant.const import (
CONF_ENTITY_ID, CONF_VALUE_TEMPLATE, CONF_CONDITION,
WEEKDAYS, CONF_STATE, CONF_ZONE, CONF_BEFORE,
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
import homeassistant.helpers.config_validation as cv
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)
return False
if value in (STATE_UNAVAILABLE, STATE_UNKNOWN):
return False
try:
value = float(value)
except ValueError:

View File

@ -146,3 +146,21 @@ class TestConditionHelper:
return_value=dt.now().replace(hour=21)):
assert not condition.time(after=sixam, before=sixpm)
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