From ef94fcf87361ab13aa6daeab047a6bfc41105d49 Mon Sep 17 00:00:00 2001 From: Marcel van der Veldt Date: Fri, 20 Sep 2024 12:05:19 +0200 Subject: [PATCH] Fix duplicate power sensors for Matter 1.3 powerplugs (#126269) * Prevent duplicate power sensors in Matter sensor platform * adjust test as well --- homeassistant/components/matter/discovery.py | 9 ++- homeassistant/components/matter/models.py | 12 ++-- homeassistant/components/matter/sensor.py | 30 ++++------ .../nodes/eve-energy-plug-patched.json | 56 ++++++++++++------- 4 files changed, 61 insertions(+), 46 deletions(-) diff --git a/homeassistant/components/matter/discovery.py b/homeassistant/components/matter/discovery.py index 33c8bb47e6a..c3e347e9808 100644 --- a/homeassistant/components/matter/discovery.py +++ b/homeassistant/components/matter/discovery.py @@ -100,13 +100,20 @@ def async_discover_entities( ): continue - # check for values that may not be present + # check for endpoint-attributes that may not be present if schema.absent_attributes is not None and any( endpoint.has_attribute(None, val_schema) for val_schema in schema.absent_attributes ): continue + # check for clusters that may not be present + if schema.absent_clusters is not None and any( + endpoint.node.has_cluster(val_schema) + for val_schema in schema.absent_clusters + ): + continue + # all checks passed, this value belongs to an entity attributes_to_watch = list(schema.required_attributes) diff --git a/homeassistant/components/matter/models.py b/homeassistant/components/matter/models.py index bb79d3571cf..c9488437a06 100644 --- a/homeassistant/components/matter/models.py +++ b/homeassistant/components/matter/models.py @@ -6,7 +6,7 @@ from dataclasses import dataclass from typing import TypedDict from chip.clusters import Objects as clusters -from chip.clusters.Objects import ClusterAttributeDescriptor +from chip.clusters.Objects import Cluster, ClusterAttributeDescriptor from matter_server.client.models.device_types import DeviceType from matter_server.client.models.node import MatterEndpoint @@ -95,11 +95,15 @@ class MatterDiscoverySchema: # [optional] the attribute's endpoint_id must match ANY of these values endpoint_id: tuple[int, ...] | None = None - # [optional] additional attributes that MAY NOT be present - # on the node for this scheme to pass + # [optional] attributes that MAY NOT be present + # (on the same endpoint) for this scheme to pass absent_attributes: tuple[type[ClusterAttributeDescriptor], ...] | None = None - # [optional] additional attributes that may be present + # [optional] cluster(s) that MAY NOT be present + # (on ANY endpoint) for this scheme to pass + absent_clusters: tuple[type[Cluster], ...] | None = None + + # [optional] additional attributes that may be present (on the same endpoint) # these attributes are copied over to attributes_to_watch and # are not discovered by other entities optional_attributes: tuple[type[ClusterAttributeDescriptor], ...] | None = None diff --git a/homeassistant/components/matter/sensor.py b/homeassistant/components/matter/sensor.py index da627734be6..94102151e17 100644 --- a/homeassistant/components/matter/sensor.py +++ b/homeassistant/components/matter/sensor.py @@ -188,7 +188,7 @@ DISCOVERY_SCHEMAS = [ ), entity_class=MatterSensor, required_attributes=(EveCluster.Attributes.Watt,), - absent_attributes=(clusters.ElectricalPowerMeasurement.Attributes.ActivePower,), + absent_clusters=(clusters.ElectricalPowerMeasurement,), ), MatterDiscoverySchema( platform=Platform.SENSOR, @@ -202,7 +202,7 @@ DISCOVERY_SCHEMAS = [ ), entity_class=MatterSensor, required_attributes=(EveCluster.Attributes.Voltage,), - absent_attributes=(clusters.ElectricalPowerMeasurement.Attributes.Voltage,), + absent_clusters=(clusters.ElectricalPowerMeasurement,), ), MatterDiscoverySchema( platform=Platform.SENSOR, @@ -216,9 +216,7 @@ DISCOVERY_SCHEMAS = [ ), entity_class=MatterSensor, required_attributes=(EveCluster.Attributes.WattAccumulated,), - absent_attributes=( - clusters.ElectricalEnergyMeasurement.Attributes.CumulativeEnergyImported, - ), + absent_clusters=(clusters.ElectricalEnergyMeasurement,), ), MatterDiscoverySchema( platform=Platform.SENSOR, @@ -232,9 +230,7 @@ DISCOVERY_SCHEMAS = [ ), entity_class=MatterSensor, required_attributes=(EveCluster.Attributes.Current,), - absent_attributes=( - clusters.ElectricalPowerMeasurement.Attributes.ActiveCurrent, - ), + absent_clusters=(clusters.ElectricalPowerMeasurement,), ), MatterDiscoverySchema( platform=Platform.SENSOR, @@ -398,7 +394,7 @@ DISCOVERY_SCHEMAS = [ required_attributes=( ThirdRealityMeteringCluster.Attributes.InstantaneousDemand, ), - absent_attributes=(clusters.ElectricalPowerMeasurement.Attributes.ActivePower,), + absent_clusters=(clusters.ElectricalPowerMeasurement,), ), MatterDiscoverySchema( platform=Platform.SENSOR, @@ -415,9 +411,7 @@ DISCOVERY_SCHEMAS = [ required_attributes=( ThirdRealityMeteringCluster.Attributes.CurrentSummationDelivered, ), - absent_attributes=( - clusters.ElectricalEnergyMeasurement.Attributes.CumulativeEnergyImported, - ), + absent_clusters=(clusters.ElectricalEnergyMeasurement,), ), MatterDiscoverySchema( platform=Platform.SENSOR, @@ -432,7 +426,7 @@ DISCOVERY_SCHEMAS = [ ), entity_class=MatterSensor, required_attributes=(NeoCluster.Attributes.Watt,), - absent_attributes=(clusters.ElectricalPowerMeasurement.Attributes.ActivePower,), + absent_clusters=(clusters.ElectricalPowerMeasurement,), ), MatterDiscoverySchema( platform=Platform.SENSOR, @@ -446,9 +440,7 @@ DISCOVERY_SCHEMAS = [ ), entity_class=MatterSensor, required_attributes=(NeoCluster.Attributes.WattAccumulated,), - absent_attributes=( - clusters.ElectricalEnergyMeasurement.Attributes.CumulativeEnergyImported, - ), + absent_clusters=(clusters.ElectricalEnergyMeasurement,), ), MatterDiscoverySchema( platform=Platform.SENSOR, @@ -463,7 +455,7 @@ DISCOVERY_SCHEMAS = [ ), entity_class=MatterSensor, required_attributes=(NeoCluster.Attributes.Voltage,), - absent_attributes=(clusters.ElectricalPowerMeasurement.Attributes.Voltage,), + absent_clusters=(clusters.ElectricalPowerMeasurement,), ), MatterDiscoverySchema( platform=Platform.SENSOR, @@ -477,9 +469,7 @@ DISCOVERY_SCHEMAS = [ ), entity_class=MatterSensor, required_attributes=(NeoCluster.Attributes.Current,), - absent_attributes=( - clusters.ElectricalPowerMeasurement.Attributes.ActiveCurrent, - ), + absent_clusters=(clusters.ElectricalPowerMeasurement,), ), MatterDiscoverySchema( platform=Platform.SENSOR, diff --git a/tests/components/matter/fixtures/nodes/eve-energy-plug-patched.json b/tests/components/matter/fixtures/nodes/eve-energy-plug-patched.json index 6b449643e8e..18c4a8c68ef 100644 --- a/tests/components/matter/fixtures/nodes/eve-energy-plug-patched.json +++ b/tests/components/matter/fixtures/nodes/eve-energy-plug-patched.json @@ -305,9 +305,23 @@ 319422468, 319422469, 319422471, 319422472, 319422473, 319422474, 319422475, 319422476, 319422478, 319422481, 319422482, 65533 ], - "1/144/0": 2, - "1/144/1": 3, - "1/144/2": [ + "2/29/0": [ + { + "0": 1296, + "1": 1 + } + ], + "2/29/1": [3, 29, 144, 145, 156], + "2/29/2": [], + "2/29/3": [], + "2/29/65532": 0, + "2/29/65533": 2, + "2/29/65528": [], + "2/29/65529": [], + "2/29/65531": [0, 1, 2, 3, 65528, 65529, 65531, 65532, 65533], + "2/144/0": 2, + "2/144/1": 3, + "2/144/2": [ { "0": 1, "1": true, @@ -345,16 +359,16 @@ ] } ], - "1/144/4": 220000, - "1/144/5": 2000, - "1/144/8": 550000, - "1/144/65533": 1, - "1/144/65532": 2, - "1/144/65531": [0, 1, 2, 4, 5, 8, 65528, 65529, 65530, 65531, 65532, 65533], - "1/144/65530": [], - "1/144/65529": [], - "1/144/65528": [], - "1/145/0": { + "2/144/4": 220000, + "2/144/5": 2000, + "2/144/8": 550000, + "2/144/65533": 1, + "2/144/65532": 2, + "2/144/65531": [0, 1, 2, 4, 5, 8, 65528, 65529, 65530, 65531, 65532, 65533], + "2/144/65530": [], + "2/144/65529": [], + "2/144/65528": [], + "2/145/0": { "0": 14, "1": true, "2": 0, @@ -366,16 +380,16 @@ } ] }, - "1/145/65533": 1, - "1/145/65532": 7, - "1/145/65531": [0, 1, 2, 65528, 65529, 65530, 65531, 65532, 65533], - "1/145/65530": [0], - "1/145/65529": [], - "1/145/65528": [], - "1/145/1": { + "2/145/65533": 1, + "2/145/65532": 7, + "2/145/65531": [0, 1, 2, 65528, 65529, 65530, 65531, 65532, 65533], + "2/145/65530": [0], + "2/145/65529": [], + "2/145/65528": [], + "2/145/1": { "0": 2500 }, - "1/145/2": null + "2/145/2": null }, "attribute_subscriptions": [], "last_subscription_attempt": 0