Fix duplicate power sensors for Matter 1.3 powerplugs (#126269)

* Prevent duplicate power sensors in Matter sensor platform

* adjust test as well
This commit is contained in:
Marcel van der Veldt 2024-09-20 12:05:19 +02:00 committed by GitHub
parent 3ad6589f25
commit ef94fcf873
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 61 additions and 46 deletions

View File

@ -100,13 +100,20 @@ def async_discover_entities(
): ):
continue 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( if schema.absent_attributes is not None and any(
endpoint.has_attribute(None, val_schema) endpoint.has_attribute(None, val_schema)
for val_schema in schema.absent_attributes for val_schema in schema.absent_attributes
): ):
continue 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 # all checks passed, this value belongs to an entity
attributes_to_watch = list(schema.required_attributes) attributes_to_watch = list(schema.required_attributes)

View File

@ -6,7 +6,7 @@ from dataclasses import dataclass
from typing import TypedDict from typing import TypedDict
from chip.clusters import Objects as clusters 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.device_types import DeviceType
from matter_server.client.models.node import MatterEndpoint 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 # [optional] the attribute's endpoint_id must match ANY of these values
endpoint_id: tuple[int, ...] | None = None endpoint_id: tuple[int, ...] | None = None
# [optional] additional attributes that MAY NOT be present # [optional] attributes that MAY NOT be present
# on the node for this scheme to pass # (on the same endpoint) for this scheme to pass
absent_attributes: tuple[type[ClusterAttributeDescriptor], ...] | None = None 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 # these attributes are copied over to attributes_to_watch and
# are not discovered by other entities # are not discovered by other entities
optional_attributes: tuple[type[ClusterAttributeDescriptor], ...] | None = None optional_attributes: tuple[type[ClusterAttributeDescriptor], ...] | None = None

View File

@ -188,7 +188,7 @@ DISCOVERY_SCHEMAS = [
), ),
entity_class=MatterSensor, entity_class=MatterSensor,
required_attributes=(EveCluster.Attributes.Watt,), required_attributes=(EveCluster.Attributes.Watt,),
absent_attributes=(clusters.ElectricalPowerMeasurement.Attributes.ActivePower,), absent_clusters=(clusters.ElectricalPowerMeasurement,),
), ),
MatterDiscoverySchema( MatterDiscoverySchema(
platform=Platform.SENSOR, platform=Platform.SENSOR,
@ -202,7 +202,7 @@ DISCOVERY_SCHEMAS = [
), ),
entity_class=MatterSensor, entity_class=MatterSensor,
required_attributes=(EveCluster.Attributes.Voltage,), required_attributes=(EveCluster.Attributes.Voltage,),
absent_attributes=(clusters.ElectricalPowerMeasurement.Attributes.Voltage,), absent_clusters=(clusters.ElectricalPowerMeasurement,),
), ),
MatterDiscoverySchema( MatterDiscoverySchema(
platform=Platform.SENSOR, platform=Platform.SENSOR,
@ -216,9 +216,7 @@ DISCOVERY_SCHEMAS = [
), ),
entity_class=MatterSensor, entity_class=MatterSensor,
required_attributes=(EveCluster.Attributes.WattAccumulated,), required_attributes=(EveCluster.Attributes.WattAccumulated,),
absent_attributes=( absent_clusters=(clusters.ElectricalEnergyMeasurement,),
clusters.ElectricalEnergyMeasurement.Attributes.CumulativeEnergyImported,
),
), ),
MatterDiscoverySchema( MatterDiscoverySchema(
platform=Platform.SENSOR, platform=Platform.SENSOR,
@ -232,9 +230,7 @@ DISCOVERY_SCHEMAS = [
), ),
entity_class=MatterSensor, entity_class=MatterSensor,
required_attributes=(EveCluster.Attributes.Current,), required_attributes=(EveCluster.Attributes.Current,),
absent_attributes=( absent_clusters=(clusters.ElectricalPowerMeasurement,),
clusters.ElectricalPowerMeasurement.Attributes.ActiveCurrent,
),
), ),
MatterDiscoverySchema( MatterDiscoverySchema(
platform=Platform.SENSOR, platform=Platform.SENSOR,
@ -398,7 +394,7 @@ DISCOVERY_SCHEMAS = [
required_attributes=( required_attributes=(
ThirdRealityMeteringCluster.Attributes.InstantaneousDemand, ThirdRealityMeteringCluster.Attributes.InstantaneousDemand,
), ),
absent_attributes=(clusters.ElectricalPowerMeasurement.Attributes.ActivePower,), absent_clusters=(clusters.ElectricalPowerMeasurement,),
), ),
MatterDiscoverySchema( MatterDiscoverySchema(
platform=Platform.SENSOR, platform=Platform.SENSOR,
@ -415,9 +411,7 @@ DISCOVERY_SCHEMAS = [
required_attributes=( required_attributes=(
ThirdRealityMeteringCluster.Attributes.CurrentSummationDelivered, ThirdRealityMeteringCluster.Attributes.CurrentSummationDelivered,
), ),
absent_attributes=( absent_clusters=(clusters.ElectricalEnergyMeasurement,),
clusters.ElectricalEnergyMeasurement.Attributes.CumulativeEnergyImported,
),
), ),
MatterDiscoverySchema( MatterDiscoverySchema(
platform=Platform.SENSOR, platform=Platform.SENSOR,
@ -432,7 +426,7 @@ DISCOVERY_SCHEMAS = [
), ),
entity_class=MatterSensor, entity_class=MatterSensor,
required_attributes=(NeoCluster.Attributes.Watt,), required_attributes=(NeoCluster.Attributes.Watt,),
absent_attributes=(clusters.ElectricalPowerMeasurement.Attributes.ActivePower,), absent_clusters=(clusters.ElectricalPowerMeasurement,),
), ),
MatterDiscoverySchema( MatterDiscoverySchema(
platform=Platform.SENSOR, platform=Platform.SENSOR,
@ -446,9 +440,7 @@ DISCOVERY_SCHEMAS = [
), ),
entity_class=MatterSensor, entity_class=MatterSensor,
required_attributes=(NeoCluster.Attributes.WattAccumulated,), required_attributes=(NeoCluster.Attributes.WattAccumulated,),
absent_attributes=( absent_clusters=(clusters.ElectricalEnergyMeasurement,),
clusters.ElectricalEnergyMeasurement.Attributes.CumulativeEnergyImported,
),
), ),
MatterDiscoverySchema( MatterDiscoverySchema(
platform=Platform.SENSOR, platform=Platform.SENSOR,
@ -463,7 +455,7 @@ DISCOVERY_SCHEMAS = [
), ),
entity_class=MatterSensor, entity_class=MatterSensor,
required_attributes=(NeoCluster.Attributes.Voltage,), required_attributes=(NeoCluster.Attributes.Voltage,),
absent_attributes=(clusters.ElectricalPowerMeasurement.Attributes.Voltage,), absent_clusters=(clusters.ElectricalPowerMeasurement,),
), ),
MatterDiscoverySchema( MatterDiscoverySchema(
platform=Platform.SENSOR, platform=Platform.SENSOR,
@ -477,9 +469,7 @@ DISCOVERY_SCHEMAS = [
), ),
entity_class=MatterSensor, entity_class=MatterSensor,
required_attributes=(NeoCluster.Attributes.Current,), required_attributes=(NeoCluster.Attributes.Current,),
absent_attributes=( absent_clusters=(clusters.ElectricalPowerMeasurement,),
clusters.ElectricalPowerMeasurement.Attributes.ActiveCurrent,
),
), ),
MatterDiscoverySchema( MatterDiscoverySchema(
platform=Platform.SENSOR, platform=Platform.SENSOR,

View File

@ -305,9 +305,23 @@
319422468, 319422469, 319422471, 319422472, 319422473, 319422474, 319422468, 319422469, 319422471, 319422472, 319422473, 319422474,
319422475, 319422476, 319422478, 319422481, 319422482, 65533 319422475, 319422476, 319422478, 319422481, 319422482, 65533
], ],
"1/144/0": 2, "2/29/0": [
"1/144/1": 3, {
"1/144/2": [ "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, "0": 1,
"1": true, "1": true,
@ -345,16 +359,16 @@
] ]
} }
], ],
"1/144/4": 220000, "2/144/4": 220000,
"1/144/5": 2000, "2/144/5": 2000,
"1/144/8": 550000, "2/144/8": 550000,
"1/144/65533": 1, "2/144/65533": 1,
"1/144/65532": 2, "2/144/65532": 2,
"1/144/65531": [0, 1, 2, 4, 5, 8, 65528, 65529, 65530, 65531, 65532, 65533], "2/144/65531": [0, 1, 2, 4, 5, 8, 65528, 65529, 65530, 65531, 65532, 65533],
"1/144/65530": [], "2/144/65530": [],
"1/144/65529": [], "2/144/65529": [],
"1/144/65528": [], "2/144/65528": [],
"1/145/0": { "2/145/0": {
"0": 14, "0": 14,
"1": true, "1": true,
"2": 0, "2": 0,
@ -366,16 +380,16 @@
} }
] ]
}, },
"1/145/65533": 1, "2/145/65533": 1,
"1/145/65532": 7, "2/145/65532": 7,
"1/145/65531": [0, 1, 2, 65528, 65529, 65530, 65531, 65532, 65533], "2/145/65531": [0, 1, 2, 65528, 65529, 65530, 65531, 65532, 65533],
"1/145/65530": [0], "2/145/65530": [0],
"1/145/65529": [], "2/145/65529": [],
"1/145/65528": [], "2/145/65528": [],
"1/145/1": { "2/145/1": {
"0": 2500 "0": 2500
}, },
"1/145/2": null "2/145/2": null
}, },
"attribute_subscriptions": [], "attribute_subscriptions": [],
"last_subscription_attempt": 0 "last_subscription_attempt": 0