mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Fix unavailable entity capable of triggering non-numerical warning in Threshold sensor (#52563)
This commit is contained in:
parent
2a48fe5199
commit
b496469a2f
@ -13,6 +13,7 @@ from homeassistant.const import (
|
|||||||
CONF_DEVICE_CLASS,
|
CONF_DEVICE_CLASS,
|
||||||
CONF_ENTITY_ID,
|
CONF_ENTITY_ID,
|
||||||
CONF_NAME,
|
CONF_NAME,
|
||||||
|
STATE_UNAVAILABLE,
|
||||||
STATE_UNKNOWN,
|
STATE_UNKNOWN,
|
||||||
)
|
)
|
||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
@ -100,7 +101,9 @@ class ThresholdSensor(BinarySensorEntity):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
self.sensor_value = (
|
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):
|
except (ValueError, TypeError):
|
||||||
self.sensor_value = None
|
self.sensor_value = None
|
||||||
|
@ -1,6 +1,11 @@
|
|||||||
"""The test for the threshold sensor platform."""
|
"""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
|
from homeassistant.setup import async_setup_component
|
||||||
|
|
||||||
|
|
||||||
@ -283,7 +288,7 @@ async def test_sensor_in_range_with_hysteresis(hass):
|
|||||||
assert state.state == "on"
|
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."""
|
"""Test if source is within the range."""
|
||||||
config = {
|
config = {
|
||||||
"binary_sensor": {
|
"binary_sensor": {
|
||||||
@ -322,6 +327,16 @@ async def test_sensor_in_range_unknown_state(hass):
|
|||||||
assert state.attributes.get("position") == "unknown"
|
assert state.attributes.get("position") == "unknown"
|
||||||
assert state.state == "off"
|
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):
|
async def test_sensor_lower_zero_threshold(hass):
|
||||||
"""Test if a lower threshold of zero is set."""
|
"""Test if a lower threshold of zero is set."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user