Skip to content

Commit

Permalink
refactor it a small bit
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelveldt committed Sep 19, 2024
1 parent 7b6af0c commit 87ceab4
Showing 1 changed file with 16 additions and 25 deletions.
41 changes: 16 additions & 25 deletions matter_server/common/custom_clusters.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ def __init_subclass__(cls: ClusterAttributeDescriptor, *args, **kwargs) -> None:
ALL_CUSTOM_ATTRIBUTES[cls.cluster_id][cls.attribute_id] = cls


def should_poll_eve_energy(node_data: MatterNodeData) -> bool:
"""Check if the (Eve Energy) custom attribute should be polled for state changes."""
if node_data.attributes.get("0/40/2") != 4874:
# Some implementation (such as MatterBridge) use the
# Eve cluster to send the power measurements. Filter that out.
return False
# if the ElectricalPowerMeasurement cluster is NOT present,
# we should poll the custom Eve cluster attribute(s).
return node_data.attributes.get("2/144/65531") is None


@dataclass
class EveCluster(Cluster, CustomClusterMixin):
"""Custom (vendor-specific) cluster for Eve - Vendor ID 4874 (0x130a)."""
Expand Down Expand Up @@ -145,11 +156,7 @@ def attribute_type(cls) -> ClusterObjectFieldDescriptor:
class Watt(ClusterAttributeDescriptor, CustomClusterAttributeMixin):
"""Watt Attribute within the Eve Cluster."""

@staticmethod
def should_poll(node_data: MatterNodeData) -> bool:
"""Check if the custom attribute should be polled for state changes."""
# if fw version < 9000 (=3.5.0), polling is needed
return node_data.attributes.get("0/40/9", 0) < 9000
should_poll = should_poll_eve_energy

@ChipUtility.classproperty
def cluster_id(cls) -> int:
Expand All @@ -172,11 +179,7 @@ def attribute_type(cls) -> ClusterObjectFieldDescriptor:
class WattAccumulated(ClusterAttributeDescriptor, CustomClusterAttributeMixin):
"""WattAccumulated Attribute within the Eve Cluster."""

@staticmethod
def should_poll(node_data: MatterNodeData) -> bool:
"""Check if the custom attribute should be polled for state changes."""
# if fw version < 9000 (=3.5.0), polling is needed
return node_data.attributes.get("0/40/9", 0) < 9000
should_poll = should_poll_eve_energy

@ChipUtility.classproperty
def cluster_id(cls) -> int:
Expand All @@ -201,11 +204,7 @@ class WattAccumulatedControlPoint(
):
"""wattAccumulatedControlPoint Attribute within the Eve Cluster."""

@staticmethod
def should_poll(node_data: MatterNodeData) -> bool:
"""Check if the custom attribute should be polled for state changes."""
# if fw version < 9000 (=3.5.0), polling is needed
return node_data.attributes.get("0/40/9", 0) < 9000
should_poll = should_poll_eve_energy

@ChipUtility.classproperty
def cluster_id(cls) -> int:
Expand All @@ -228,11 +227,7 @@ def attribute_type(cls) -> ClusterObjectFieldDescriptor:
class Voltage(ClusterAttributeDescriptor, CustomClusterAttributeMixin):
"""Voltage Attribute within the Eve Cluster."""

@staticmethod
def should_poll(node_data: MatterNodeData) -> bool:
"""Check if the custom attribute should be polled for state changes."""
# if fw version < 9000 (=3.5.0), polling is needed
return node_data.attributes.get("0/40/9", 0) < 9000
should_poll = should_poll_eve_energy

@ChipUtility.classproperty
def cluster_id(cls) -> int:
Expand All @@ -255,11 +250,7 @@ def attribute_type(cls) -> ClusterObjectFieldDescriptor:
class Current(ClusterAttributeDescriptor, CustomClusterAttributeMixin):
"""Current Attribute within the Eve Cluster."""

@staticmethod
def should_poll(node_data: MatterNodeData) -> bool:
"""Check if the custom attribute should be polled for state changes."""
# if fw version < 9000 (=3.5.0), polling is needed
return node_data.attributes.get("0/40/9", 0) < 9000
should_poll = should_poll_eve_energy

@ChipUtility.classproperty
def cluster_id(cls) -> int:
Expand Down

0 comments on commit 87ceab4

Please sign in to comment.