Mark trend sensor unavailable when source entity is unknown/unavailable (#132080)
parent
32b8c8985e
commit
755d36d82f
|
@ -227,9 +227,14 @@ class SensorTrend(BinarySensorEntity, RestoreEntity):
|
|||
state = new_state.attributes.get(self._attribute)
|
||||
else:
|
||||
state = new_state.state
|
||||
if state not in (STATE_UNKNOWN, STATE_UNAVAILABLE):
|
||||
|
||||
if state in (STATE_UNKNOWN, STATE_UNAVAILABLE):
|
||||
self._attr_available = False
|
||||
else:
|
||||
self._attr_available = True
|
||||
sample = (new_state.last_updated.timestamp(), float(state)) # type: ignore[arg-type]
|
||||
self.samples.append(sample)
|
||||
|
||||
self.async_schedule_update_ha_state(True)
|
||||
except (ValueError, TypeError) as ex:
|
||||
_LOGGER.error(ex)
|
||||
|
|
|
@ -9,7 +9,7 @@ import pytest
|
|||
|
||||
from homeassistant import setup
|
||||
from homeassistant.components.trend.const import DOMAIN
|
||||
from homeassistant.const import STATE_OFF, STATE_ON, STATE_UNKNOWN
|
||||
from homeassistant.const import STATE_OFF, STATE_ON, STATE_UNAVAILABLE, STATE_UNKNOWN
|
||||
from homeassistant.core import HomeAssistant, State
|
||||
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
@ -395,3 +395,45 @@ async def test_device_id(
|
|||
trend_entity = entity_registry.async_get("binary_sensor.trend")
|
||||
assert trend_entity is not None
|
||||
assert trend_entity.device_id == source_entity.device_id
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"error_state",
|
||||
[
|
||||
STATE_UNKNOWN,
|
||||
STATE_UNAVAILABLE,
|
||||
],
|
||||
)
|
||||
async def test_unavailable_source(
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
setup_component: ComponentSetup,
|
||||
error_state: str,
|
||||
) -> None:
|
||||
"""Test for unavailable source."""
|
||||
await setup_component(
|
||||
{
|
||||
"sample_duration": 10000,
|
||||
"min_gradient": 1,
|
||||
"max_samples": 25,
|
||||
"min_samples": 5,
|
||||
},
|
||||
)
|
||||
|
||||
for val in (10, 20, 30, 40, 50, 60):
|
||||
freezer.tick(timedelta(seconds=2))
|
||||
hass.states.async_set("sensor.test_state", val)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.states.get("binary_sensor.test_trend_sensor").state == "on"
|
||||
|
||||
hass.states.async_set("sensor.test_state", error_state)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.states.get("binary_sensor.test_trend_sensor").state == STATE_UNAVAILABLE
|
||||
|
||||
hass.states.async_set("sensor.test_state", 50)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.states.get("binary_sensor.test_trend_sensor").state == "on"
|
||||
|
|
Loading…
Reference in New Issue