Skip to content

Commit

Permalink
Fix last_state handling for custom entities (#528)
Browse files Browse the repository at this point in the history
  • Loading branch information
SukramJ committed Jul 12, 2022
1 parent cfc010c commit 2e2bdc4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 19 deletions.
3 changes: 3 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
Version 2022.7.6 (2022-07-12)
- Fix last_state handling for custom entities

Version 2022.7.5 (2022-07-11)
- Rename value_uncertain to state_uncertain
- Add state_uncertain to custom entity
Expand Down
52 changes: 34 additions & 18 deletions hahomematic/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,25 +85,25 @@ class CallbackEntity(ABC):
"""Base class for callback entities."""

def __init__(self) -> None:
self._last_update: datetime = INIT_DATETIME
self._update_callbacks: list[Callable] = []
self._remove_callbacks: list[Callable] = []
self._state_uncertain: bool = True

@property
def last_update(self) -> datetime:
"""Return the last updated datetime value"""
return self._last_update
# override in subclass
return INIT_DATETIME

@property
def state_uncertain(self) -> bool:
"""Return, if the state is uncertain."""
return self._state_uncertain
# override in subclass
return True

@property
def is_valid(self) -> bool:
"""Return, if the value of the entity is valid based on the last updated datetime."""
return self._last_update > INIT_DATETIME
return self.last_update > INIT_DATETIME

def register_update_callback(self, update_callback: Callable) -> None:
"""register update callback"""
Expand Down Expand Up @@ -136,14 +136,9 @@ def remove_entity(self, *args: Any) -> None:
"""
Do what is needed when the entity has been removed.
"""
self._set_last_update()
for _callback in self._remove_callbacks:
_callback(*args)

def _set_last_update(self) -> None:
"""Set last_update to current datetime."""
self._last_update = datetime.now()


class BaseEntity(ABC):
"""Base class for regular entities."""
Expand Down Expand Up @@ -536,6 +531,8 @@ def __init__(
)
CallbackEntity.__init__(self)
self._value: ParameterT | None = None
self._last_update: datetime = INIT_DATETIME
self._state_uncertain: bool = True

# Subscribe for all events of this device
if (
Expand All @@ -549,6 +546,20 @@ def __init__(
(self.channel_address, self._parameter)
].append(self.event)

@property
def last_update(self) -> datetime:
"""Return the last updated datetime value"""
return self._last_update

@property
def state_uncertain(self) -> bool:
"""Return, if the state is uncertain."""
return self._state_uncertain

def _set_last_update(self) -> None:
"""Set last_update to current datetime."""
self._last_update = datetime.now()

async def load_entity_value(self) -> None:
"""Init the entity data."""
if updated_within_seconds(last_update=self.last_update):
Expand Down Expand Up @@ -665,7 +676,8 @@ def attributes(self) -> dict[str, Any]:
"""Return the state attributes of the generic entity."""
state_attr = super().attributes
state_attr[ATTR_ENTITY_TYPE] = HmEntityType.GENERIC.value
state_attr[ATTR_STATE_UNCERTAIN] = self.state_uncertain
if self.is_readable:
state_attr[ATTR_STATE_UNCERTAIN] = self.state_uncertain
return state_attr

def remove_event_subscriptions(self) -> None:
Expand Down Expand Up @@ -721,6 +733,17 @@ def attributes(self) -> dict[str, Any]:
state_attr[ATTR_STATE_UNCERTAIN] = self.state_uncertain
return state_attr

@property
def last_update(self) -> datetime:
"""Return, if the state is uncertain."""
latest_update: datetime = INIT_DATETIME
for hm_entity in self.data_entities.values():
if hm_entity.is_readable:
if entity_last_update := hm_entity.last_update:
if entity_last_update > latest_update:
latest_update = entity_last_update
return latest_update

@property
def state_uncertain(self) -> bool:
"""Return, if the state is uncertain."""
Expand Down Expand Up @@ -774,13 +797,6 @@ async def load_entity_value(self) -> None:
await entity.load_entity_value()
self.update_entity()

def update_entity(self, *args: Any) -> None:
"""
Do what is needed when the value of the entity has been updated.
"""
super().update_entity(*args)
self._set_last_update()

def _init_entities(self) -> None:
"""init entity collection"""

Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = hahomematic
version = 2022.7.5
version = 2022.7.6
author = Daniel Perna
author_email = danielperna84@gmail.com
license = MIT License
Expand Down

0 comments on commit 2e2bdc4

Please sign in to comment.