Skip to content

Commit

Permalink
Add discovery schemas for Matter 1.3 power/energy sensors (#125403)
Browse files Browse the repository at this point in the history
* Add missing discovery schemas for (Matter 1.3) Power/Energy measurements

* Prevent discovery of custom cluster if 1.3 cluster present

* add tests

* Use f-strings

---------

Co-authored-by: Martin Hjelmare <[email protected]>
  • Loading branch information
marcelveldt and MartinHjelmare authored Sep 7, 2024
1 parent b8c3a44 commit cbd884d
Show file tree
Hide file tree
Showing 3 changed files with 540 additions and 6 deletions.
83 changes: 83 additions & 0 deletions homeassistant/components/matter/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ def _update_from_device(self) -> None:
),
entity_class=MatterSensor,
required_attributes=(EveCluster.Attributes.Watt,),
absent_attributes=(clusters.ElectricalPowerMeasurement.Attributes.ActivePower,),
),
MatterDiscoverySchema(
platform=Platform.SENSOR,
Expand All @@ -188,6 +189,7 @@ def _update_from_device(self) -> None:
),
entity_class=MatterSensor,
required_attributes=(EveCluster.Attributes.Voltage,),
absent_attributes=(clusters.ElectricalPowerMeasurement.Attributes.Voltage,),
),
MatterDiscoverySchema(
platform=Platform.SENSOR,
Expand All @@ -201,6 +203,9 @@ def _update_from_device(self) -> None:
),
entity_class=MatterSensor,
required_attributes=(EveCluster.Attributes.WattAccumulated,),
absent_attributes=(
clusters.ElectricalEnergyMeasurement.Attributes.CumulativeEnergyImported,
),
),
MatterDiscoverySchema(
platform=Platform.SENSOR,
Expand All @@ -214,6 +219,9 @@ def _update_from_device(self) -> None:
),
entity_class=MatterSensor,
required_attributes=(EveCluster.Attributes.Current,),
absent_attributes=(
clusters.ElectricalPowerMeasurement.Attributes.ActiveCurrent,
),
),
MatterDiscoverySchema(
platform=Platform.SENSOR,
Expand Down Expand Up @@ -377,6 +385,7 @@ def _update_from_device(self) -> None:
required_attributes=(
ThirdRealityMeteringCluster.Attributes.InstantaneousDemand,
),
absent_attributes=(clusters.ElectricalPowerMeasurement.Attributes.ActivePower,),
),
MatterDiscoverySchema(
platform=Platform.SENSOR,
Expand All @@ -393,6 +402,9 @@ def _update_from_device(self) -> None:
required_attributes=(
ThirdRealityMeteringCluster.Attributes.CurrentSummationDelivered,
),
absent_attributes=(
clusters.ElectricalEnergyMeasurement.Attributes.CumulativeEnergyImported,
),
),
MatterDiscoverySchema(
platform=Platform.SENSOR,
Expand All @@ -407,6 +419,7 @@ def _update_from_device(self) -> None:
),
entity_class=MatterSensor,
required_attributes=(NeoCluster.Attributes.Watt,),
absent_attributes=(clusters.ElectricalPowerMeasurement.Attributes.ActivePower,),
),
MatterDiscoverySchema(
platform=Platform.SENSOR,
Expand All @@ -420,6 +433,9 @@ def _update_from_device(self) -> None:
),
entity_class=MatterSensor,
required_attributes=(NeoCluster.Attributes.WattAccumulated,),
absent_attributes=(
clusters.ElectricalEnergyMeasurement.Attributes.CumulativeEnergyImported,
),
),
MatterDiscoverySchema(
platform=Platform.SENSOR,
Expand All @@ -434,6 +450,7 @@ def _update_from_device(self) -> None:
),
entity_class=MatterSensor,
required_attributes=(NeoCluster.Attributes.Voltage,),
absent_attributes=(clusters.ElectricalPowerMeasurement.Attributes.Voltage,),
),
MatterDiscoverySchema(
platform=Platform.SENSOR,
Expand All @@ -447,6 +464,9 @@ def _update_from_device(self) -> None:
),
entity_class=MatterSensor,
required_attributes=(NeoCluster.Attributes.Current,),
absent_attributes=(
clusters.ElectricalPowerMeasurement.Attributes.ActiveCurrent,
),
),
MatterDiscoverySchema(
platform=Platform.SENSOR,
Expand All @@ -462,4 +482,67 @@ def _update_from_device(self) -> None:
required_attributes=(clusters.Switch.Attributes.CurrentPosition,),
allow_multi=True, # also used for event entity
),
MatterDiscoverySchema(
platform=Platform.SENSOR,
entity_description=MatterSensorEntityDescription(
key="ElectricalPowerMeasurementWatt",
device_class=SensorDeviceClass.POWER,
entity_category=EntityCategory.DIAGNOSTIC,
native_unit_of_measurement=UnitOfPower.WATT,
suggested_display_precision=2,
state_class=SensorStateClass.MEASUREMENT,
measurement_to_ha=lambda x: x / 1000,
),
entity_class=MatterSensor,
required_attributes=(
clusters.ElectricalPowerMeasurement.Attributes.ActivePower,
),
),
MatterDiscoverySchema(
platform=Platform.SENSOR,
entity_description=MatterSensorEntityDescription(
key="ElectricalPowerMeasurementVoltage",
device_class=SensorDeviceClass.VOLTAGE,
entity_category=EntityCategory.DIAGNOSTIC,
native_unit_of_measurement=UnitOfElectricPotential.VOLT,
suggested_display_precision=0,
state_class=SensorStateClass.MEASUREMENT,
measurement_to_ha=lambda x: x / 1000,
),
entity_class=MatterSensor,
required_attributes=(clusters.ElectricalPowerMeasurement.Attributes.Voltage,),
),
MatterDiscoverySchema(
platform=Platform.SENSOR,
entity_description=MatterSensorEntityDescription(
key="ElectricalPowerMeasurementActiveCurrent",
device_class=SensorDeviceClass.CURRENT,
entity_category=EntityCategory.DIAGNOSTIC,
native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
suggested_display_precision=2,
state_class=SensorStateClass.MEASUREMENT,
measurement_to_ha=lambda x: x / 1000,
),
entity_class=MatterSensor,
required_attributes=(
clusters.ElectricalPowerMeasurement.Attributes.ActiveCurrent,
),
),
MatterDiscoverySchema(
platform=Platform.SENSOR,
entity_description=MatterSensorEntityDescription(
key="ElectricalEnergyMeasurementCumulativeEnergyImported",
device_class=SensorDeviceClass.ENERGY,
entity_category=EntityCategory.DIAGNOSTIC,
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
suggested_display_precision=3,
state_class=SensorStateClass.TOTAL_INCREASING,
# id 0 of the EnergyMeasurementStruct is the cumulative energy (in mWh)
measurement_to_ha=lambda x: x.energy / 1000000,
),
entity_class=MatterSensor,
required_attributes=(
clusters.ElectricalEnergyMeasurement.Attributes.CumulativeEnergyImported,
),
),
]
Loading

0 comments on commit cbd884d

Please sign in to comment.