Skip to content

Commit

Permalink
Merge pull request #29 from Taraman17/dev
Browse files Browse the repository at this point in the history
fix: notify node of changed attributes
  • Loading branch information
Taraman17 authored Jan 25, 2025
2 parents 6986e1f + e2d6004 commit 26468be
Showing 1 changed file with 7 additions and 22 deletions.
29 changes: 7 additions & 22 deletions pyHomee/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,9 @@ def __init__(self, data: dict) -> None:
self._data = data
self.attributes: list[HomeeAttribute] = []
for a in self.attributes_raw:
self.attributes.append(HomeeAttribute(a))
new_attribute = HomeeAttribute(a)
new_attribute.add_on_changed_listener(self._on_attribute_changed)
self.attributes.append(new_attribute)
self._attribute_map: dict[AttributeType, HomeeAttribute] = None
self.remap_attributes()
self._on_changed_listeners = []
Expand Down Expand Up @@ -380,6 +382,10 @@ def get_attribute_by_id(self, attribute_id: int) -> HomeeAttribute | None:
index = self.get_attribute_index(attribute_id)
return self.attributes[index] if index != -1 else None

def _on_attribute_changed(self, attribute: HomeeAttribute) -> None:
for listener in self._on_changed_listeners:
listener(self)

def add_on_changed_listener(self, listener: Callable[[Self], None]) -> Callable[[], None]:
"""Add on_changed listener to node."""
self._on_changed_listeners.append(listener)
Expand All @@ -389,38 +395,17 @@ def remove_listener():

return remove_listener

def _update_attribute(self, attribute_data: dict) -> None:
# TODO: Remove in a future release.
_LOGGER.warning(
"_update_attribute() is deprecated - use update_attribute() instead"
)
self.update_attribute(attribute_data)

def update_attribute(self, attribute_data: dict) -> None:
"""Update a single attribute of a node."""
attribute = self.get_attribute_by_id(attribute_data["id"])
if attribute is not None:
attribute.set_data(attribute_data)

def _update_attributes(self, attributes: list[dict]) -> None:
# TODO: Remove in a future release.
_LOGGER.warning(
"_update_attributes() is deprecated - use update_attributes() instead"
)
self.update_attributes(attributes)

def update_attributes(self, attributes: list[dict]) -> None:
"""Update the given attributes."""
for attr in attributes:
self.update_attribute(attr)

def _remap_attributes(self):
# TODO: Remove in a future release.
_LOGGER.warning(
"_remap_attributes() is deprecated - use remap_attributes() instead"
)
self.remap_attributes()

def remap_attributes(self) -> None:
"""Remap the node's attributes."""
if self._attribute_map is not None:
Expand Down

0 comments on commit 26468be

Please sign in to comment.