Fix unavailable entity capable of triggering non-numerical warning in Threshold sensor (#52563)

This commit is contained in:
Franck Nijhof 2021-07-06 09:33:00 +02:00 committed by GitHub
parent 2a48fe5199
commit b496469a2f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 3 deletions

View file

@ -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

View file

@ -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."""