diff --git a/homeassistant/components/threshold/binary_sensor.py b/homeassistant/components/threshold/binary_sensor.py index 5bd6f77253b..1a53a599394 100644 --- a/homeassistant/components/threshold/binary_sensor.py +++ b/homeassistant/components/threshold/binary_sensor.py @@ -13,6 +13,7 @@ from homeassistant.const import ( CONF_DEVICE_CLASS, CONF_ENTITY_ID, CONF_NAME, + STATE_UNAVAILABLE, STATE_UNKNOWN, ) from homeassistant.core import callback @@ -100,7 +101,9 @@ class ThresholdSensor(BinarySensorEntity): try: self.sensor_value = ( - None if new_state.state == STATE_UNKNOWN else float(new_state.state) + None + if new_state.state in [STATE_UNKNOWN, STATE_UNAVAILABLE] + else float(new_state.state) ) except (ValueError, TypeError): self.sensor_value = None diff --git a/tests/components/threshold/test_binary_sensor.py b/tests/components/threshold/test_binary_sensor.py index af8c32a1549..b7c4a871068 100644 --- a/tests/components/threshold/test_binary_sensor.py +++ b/tests/components/threshold/test_binary_sensor.py @@ -1,6 +1,11 @@ """The test for the threshold sensor platform.""" -from homeassistant.const import ATTR_UNIT_OF_MEASUREMENT, STATE_UNKNOWN, TEMP_CELSIUS +from homeassistant.const import ( + ATTR_UNIT_OF_MEASUREMENT, + STATE_UNAVAILABLE, + STATE_UNKNOWN, + TEMP_CELSIUS, +) from homeassistant.setup import async_setup_component @@ -283,7 +288,7 @@ async def test_sensor_in_range_with_hysteresis(hass): assert state.state == "on" -async def test_sensor_in_range_unknown_state(hass): +async def test_sensor_in_range_unknown_state(hass, caplog): """Test if source is within the range.""" config = { "binary_sensor": { @@ -322,6 +327,16 @@ async def test_sensor_in_range_unknown_state(hass): assert state.attributes.get("position") == "unknown" assert state.state == "off" + hass.states.async_set("sensor.test_monitored", STATE_UNAVAILABLE) + await hass.async_block_till_done() + + state = hass.states.get("binary_sensor.threshold") + + assert state.attributes.get("position") == "unknown" + assert state.state == "off" + + assert "State is not numerical" not in caplog.text + async def test_sensor_lower_zero_threshold(hass): """Test if a lower threshold of zero is set."""