Follow the unavailability of the source sensor (#91975)

This commit is contained in:
Diogo Gomes 2023-04-25 08:49:58 +01:00 committed by GitHub
parent 63f3767a29
commit 48546c77b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 1 deletions

View File

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

View File

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