Skip to content

Commit

Permalink
Fix error when last state was unavailable
Browse files Browse the repository at this point in the history
  • Loading branch information
Aohzan committed Oct 3, 2024
1 parent f319e50 commit 53940d8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 5.3.1

- Fix error when last state was unavailable

## 5.3.0

- Fix deprecations
Expand Down
2 changes: 1 addition & 1 deletion custom_components/ecodevices/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
"xmltodict==0.13.0",
"pyecodevices==1.7.0"
],
"version": "5.3.0"
"version": "5.3.1"
}
18 changes: 16 additions & 2 deletions custom_components/ecodevices/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import UnitOfApparentPower, UnitOfEnergy
from homeassistant.const import (
STATE_UNAVAILABLE,
STATE_UNKNOWN,
UnitOfApparentPower,
UnitOfEnergy,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType
Expand Down Expand Up @@ -336,7 +341,10 @@ async def async_added_to_hass(self) -> None:

last_state = await self.async_get_last_state()

if last_state:
if last_state is not None and last_state.state not in (
STATE_UNKNOWN,
STATE_UNAVAILABLE,
):
self._last_state = last_state.state


Expand Down Expand Up @@ -375,6 +383,7 @@ def native_value(self) -> float | None:
self.entity_id,
self._last_state,
)
return None
_LOGGER.debug("Value for %s equal to 0, ignore", self.entity_id)
return None

Expand All @@ -397,6 +406,7 @@ def native_value(self) -> float | None:
self.entity_id,
self._last_state,
)
return None
_LOGGER.debug("Value for %s equal to 0, ignore", self.entity_id)
return None

Expand All @@ -417,6 +427,7 @@ def native_value(self) -> float | None:
self.entity_id,
self._last_state,
)
return None
_LOGGER.debug("Value for %s equal to 0, ignore", self.entity_id)
return None

Expand All @@ -437,6 +448,7 @@ def native_value(self) -> float | None:
self.entity_id,
self._last_state,
)
return None
_LOGGER.debug("Value for %s equal to 0, ignore", self.entity_id)
return None

Expand All @@ -460,6 +472,7 @@ def native_value(self) -> float | None:
self.entity_id,
self._last_state,
)
return None
_LOGGER.debug("Value for %s equal to 0, ignore", self.entity_id)
return None

Expand All @@ -480,6 +493,7 @@ def native_value(self) -> float | None:
self.entity_id,
self._last_state,
)
return None
_LOGGER.debug("Value for %s equal to 0, ignore", self.entity_id)
return None

Expand Down

0 comments on commit 53940d8

Please sign in to comment.