mirror of
https://github.com/home-assistant/core.git
synced 2025-07-27 07:07:28 +00:00
Follow the unavailability of the source sensor (#91975)
This commit is contained in:
parent
63f3767a29
commit
48546c77b5
@ -197,6 +197,15 @@ class IntegrationSensor(RestoreEntity, SensorEntity):
|
|||||||
old_state: State | None = event.data.get("old_state")
|
old_state: State | None = event.data.get("old_state")
|
||||||
new_state: State | None = event.data.get("new_state")
|
new_state: State | None = event.data.get("new_state")
|
||||||
|
|
||||||
|
if (
|
||||||
|
source_state := self.hass.states.get(self._sensor_source_id)
|
||||||
|
) is None or source_state.state == STATE_UNAVAILABLE:
|
||||||
|
self._attr_available = False
|
||||||
|
self.async_write_ha_state()
|
||||||
|
return
|
||||||
|
|
||||||
|
self._attr_available = True
|
||||||
|
|
||||||
if new_state is None or new_state.state in (
|
if new_state is None or new_state.state in (
|
||||||
STATE_UNKNOWN,
|
STATE_UNKNOWN,
|
||||||
STATE_UNAVAILABLE,
|
STATE_UNAVAILABLE,
|
||||||
|
@ -401,13 +401,20 @@ async def test_units(hass: HomeAssistant) -> None:
|
|||||||
|
|
||||||
# When source state goes to None / Unknown, expect an early exit without
|
# When source state goes to None / Unknown, expect an early exit without
|
||||||
# changes to the state or unit_of_measurement
|
# changes to the state or unit_of_measurement
|
||||||
hass.states.async_set(entity_id, STATE_UNAVAILABLE, None)
|
hass.states.async_set(entity_id, None, None)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
new_state = hass.states.get("sensor.integration")
|
new_state = hass.states.get("sensor.integration")
|
||||||
assert state == new_state
|
assert state == new_state
|
||||||
assert state.attributes.get("unit_of_measurement") == UnitOfEnergy.WATT_HOUR
|
assert state.attributes.get("unit_of_measurement") == UnitOfEnergy.WATT_HOUR
|
||||||
|
|
||||||
|
# When source state goes to unavailable, expect sensor to also become unavailable
|
||||||
|
hass.states.async_set(entity_id, STATE_UNAVAILABLE, None)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
new_state = hass.states.get("sensor.integration")
|
||||||
|
assert new_state.state == STATE_UNAVAILABLE
|
||||||
|
|
||||||
|
|
||||||
async def test_device_class(hass: HomeAssistant) -> None:
|
async def test_device_class(hass: HomeAssistant) -> None:
|
||||||
"""Test integration sensor units using a power source."""
|
"""Test integration sensor units using a power source."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user