From 3c45bda0e891826eb67d666722a65c0bc84af78c Mon Sep 17 00:00:00 2001 From: Diogo Gomes Date: Tue, 9 May 2023 21:22:06 +0100 Subject: [PATCH] Don't try to restore unavailable nor unknown states (#92825) --- .../components/integration/sensor.py | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/homeassistant/components/integration/sensor.py b/homeassistant/components/integration/sensor.py index d199b8808d3..d55a1136646 100644 --- a/homeassistant/components/integration/sensor.py +++ b/homeassistant/components/integration/sensor.py @@ -174,23 +174,23 @@ class IntegrationSensor(RestoreEntity, SensorEntity): async def async_added_to_hass(self) -> None: """Handle entity which will be added.""" await super().async_added_to_hass() - if state := await self.async_get_last_state(): - try: - self._state = Decimal(state.state) - except (DecimalException, ValueError) as err: - _LOGGER.warning( - "%s could not restore last state %s: %s", - self.entity_id, - state.state, - err, - ) - else: - self._attr_device_class = state.attributes.get(ATTR_DEVICE_CLASS) - if self._unit_of_measurement is None: - self._unit_of_measurement = state.attributes.get( - ATTR_UNIT_OF_MEASUREMENT + if (state := await self.async_get_last_state()) is not None: + if state.state == STATE_UNAVAILABLE: + self._attr_available = False + elif state.state != STATE_UNKNOWN: + try: + self._state = Decimal(state.state) + except (DecimalException, ValueError) as err: + _LOGGER.warning( + "%s could not restore last state %s: %s", + self.entity_id, + state.state, + err, ) + self._attr_device_class = state.attributes.get(ATTR_DEVICE_CLASS) + self._unit_of_measurement = state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) + @callback def calc_integration(event: Event) -> None: """Handle the sensor state changes."""