mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +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")
|
||||
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 (
|
||||
STATE_UNKNOWN,
|
||||
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
|
||||
# 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()
|
||||
|
||||
new_state = hass.states.get("sensor.integration")
|
||||
assert state == new_state
|
||||
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:
|
||||
"""Test integration sensor units using a power source."""
|
||||
|
Loading…
x
Reference in New Issue
Block a user