From 3077444d629d8290ef75c2d0b506e39eb5621305 Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Tue, 20 Feb 2018 17:02:27 +0100 Subject: [PATCH] Fix numeric_state condition spamming on unavailable (#12550) --- homeassistant/helpers/condition.py | 5 ++++- tests/helpers/test_condition.py | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/homeassistant/helpers/condition.py b/homeassistant/helpers/condition.py index 382a7c27d78..bad6bfe83c3 100644 --- a/homeassistant/helpers/condition.py +++ b/homeassistant/helpers/condition.py @@ -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: diff --git a/tests/helpers/test_condition.py b/tests/helpers/test_condition.py index 2991e07a464..aa7b5170648 100644 --- a/tests/helpers/test_condition.py +++ b/tests/helpers/test_condition.py @@ -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