mirror of
https://github.com/home-assistant/core.git
synced 2025-07-11 23:37:18 +00:00
Track availability of source sensor in utility meter (#91035)
* track availability of source sensor * address review comments
This commit is contained in:
parent
ee1644c24e
commit
86fe0c9683
@ -423,6 +423,15 @@ class UtilityMeterSensor(RestoreSensor):
|
|||||||
@callback
|
@callback
|
||||||
def async_reading(self, event: Event):
|
def async_reading(self, event: Event):
|
||||||
"""Handle the sensor state changes."""
|
"""Handle the sensor state changes."""
|
||||||
|
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
|
||||||
|
|
||||||
old_state: State | None = event.data.get("old_state")
|
old_state: State | None = event.data.get("old_state")
|
||||||
new_state: State = event.data.get("new_state") # type: ignore[assignment] # a state change event always has a new state
|
new_state: State = event.data.get("new_state") # type: ignore[assignment] # a state change event always has a new state
|
||||||
|
|
||||||
|
@ -217,20 +217,19 @@ async def test_state(hass: HomeAssistant, yaml_config, config_entry_config) -> N
|
|||||||
entity_id, "*", {ATTR_UNIT_OF_MEASUREMENT: UnitOfEnergy.KILO_WATT_HOUR}
|
entity_id, "*", {ATTR_UNIT_OF_MEASUREMENT: UnitOfEnergy.KILO_WATT_HOUR}
|
||||||
)
|
)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
state = hass.states.get("sensor.energy_bill_midpeak")
|
state = hass.states.get("sensor.energy_bill_offpeak")
|
||||||
assert state is not None
|
assert state is not None
|
||||||
assert state.state == "0.123"
|
assert state.state == "3"
|
||||||
|
|
||||||
# test unavailable source
|
# test unavailable source
|
||||||
hass.states.async_set(
|
hass.states.async_set(
|
||||||
entity_id,
|
entity_id,
|
||||||
STATE_UNAVAILABLE,
|
STATE_UNAVAILABLE,
|
||||||
{ATTR_UNIT_OF_MEASUREMENT: UnitOfEnergy.KILO_WATT_HOUR},
|
|
||||||
)
|
)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
state = hass.states.get("sensor.energy_bill_midpeak")
|
state = hass.states.get("sensor.energy_bill_offpeak")
|
||||||
assert state is not None
|
assert state is not None
|
||||||
assert state.state == "0.123"
|
assert state.state == "unavailable"
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user