Skip to content

Commit

Permalink
Only Poll individual attributes in custom Eve cluster (#724)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelveldt committed May 29, 2024
1 parent cbf3a7a commit dd8f9bf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 24 deletions.
18 changes: 13 additions & 5 deletions matter_server/common/custom_clusters.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ class EveCluster(Cluster, CustomClusterMixin):
"""Custom (vendor-specific) cluster for Eve - Vendor ID 4874 (0x130a)."""

id: ClassVar[int] = 0x130AFC01
should_poll = True

@ChipUtility.classproperty
def descriptor(cls) -> ClusterObjectDescriptor:
Expand Down Expand Up @@ -98,6 +97,8 @@ class Attributes:
class Watt(ClusterAttributeDescriptor, CustomClusterAttributeMixin):
"""Watt Attribute within the Eve Cluster."""

should_poll = True

@ChipUtility.classproperty
def cluster_id(cls) -> int:
"""Return cluster id."""
Expand All @@ -119,6 +120,8 @@ def attribute_type(cls) -> ClusterObjectFieldDescriptor:
class WattAccumulated(ClusterAttributeDescriptor, CustomClusterAttributeMixin):
"""WattAccumulated Attribute within the Eve Cluster."""

should_poll = True

@ChipUtility.classproperty
def cluster_id(cls) -> int:
"""Return cluster id."""
Expand All @@ -137,11 +140,13 @@ def attribute_type(cls) -> ClusterObjectFieldDescriptor:
value: float32 = 0

@dataclass
class wattAccumulatedControlPoint(
class WattAccumulatedControlPoint(
ClusterAttributeDescriptor, CustomClusterAttributeMixin
):
"""wattAccumulatedControlPoint Attribute within the Eve Cluster."""

should_poll = True

@ChipUtility.classproperty
def cluster_id(cls) -> int:
"""Return cluster id."""
Expand All @@ -163,6 +168,8 @@ def attribute_type(cls) -> ClusterObjectFieldDescriptor:
class Voltage(ClusterAttributeDescriptor, CustomClusterAttributeMixin):
"""Voltage Attribute within the Eve Cluster."""

should_poll = True

@ChipUtility.classproperty
def cluster_id(cls) -> int:
"""Return cluster id."""
Expand All @@ -184,6 +191,8 @@ def attribute_type(cls) -> ClusterObjectFieldDescriptor:
class Current(ClusterAttributeDescriptor, CustomClusterAttributeMixin):
"""Current Attribute within the Eve Cluster."""

should_poll = True

@ChipUtility.classproperty
def cluster_id(cls) -> int:
"""Return cluster id."""
Expand Down Expand Up @@ -213,9 +222,8 @@ def check_polled_attributes(node_data: MatterNodeData) -> set[str]:
# the entire cluster needs to be polled
attributes_to_poll.add(f"{endpoint_id}/{cluster_id}/*")
continue
if (
custom_attribute := ALL_CUSTOM_ATTRIBUTES[cluster_id].get(attribute_id)
) and custom_attribute.should_poll:
custom_attribute = ALL_CUSTOM_ATTRIBUTES[cluster_id].get(attribute_id)
if custom_attribute and custom_attribute.should_poll:
# this attribute needs to be polled
attributes_to_poll.add(attr_path)
return attributes_to_poll
36 changes: 17 additions & 19 deletions matter_server/server/device_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -1404,25 +1404,23 @@ async def _custom_attributes_poller(self) -> None:
node = self._nodes[node_id]
if not node.available:
continue
for attribute_path in self._polled_attributes[node_id].copy():
try:
# try to read the attribute(s) - this will fire an event if the value changed
await self.read_attribute(
node_id, attribute_path, fabric_filtered=False
)
except (ChipStackError, NodeNotReady) as err:
LOGGER.warning(
"Polling custom attribute %s for node %s failed: %s",
attribute_path,
node_id,
str(err) or err.__class__.__name__,
# log full stack trace if verbose logging is enabled
exc_info=err
if LOGGER.isEnabledFor(VERBOSE_LOG_LEVEL)
else None,
)
# polling attributes is heavy on network traffic, so we throttle it a bit
await asyncio.sleep(2)
attribute_paths = list(self._polled_attributes[node_id])
try:
# try to read the attribute(s) - this will fire an event if the value changed
await self.read_attribute(
node_id, attribute_paths, fabric_filtered=False
)
except (ChipStackError, NodeNotReady) as err:
LOGGER.warning(
"Polling custom attribute(s) %s for node %s failed: %s",
",".join(attribute_paths),
node_id,
str(err) or err.__class__.__name__,
# log full stack trace if verbose logging is enabled
exc_info=err if LOGGER.isEnabledFor(VERBOSE_LOG_LEVEL) else None,
)
# polling attributes is heavy on network traffic, so we throttle it a bit
await asyncio.sleep(2)
# reschedule self to run at next interval
self._schedule_custom_attributes_poller()

Expand Down

0 comments on commit dd8f9bf

Please sign in to comment.