Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add power/energy sensor for Matter draft electrical measurement cluster #132920

Merged
merged 14 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions homeassistant/components/matter/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from chip.clusters.Types import Nullable, NullValue
from matter_server.client.models import device_types
from matter_server.common.custom_clusters import (
DraftElectricalMeasurementCluster,
EveCluster,
NeoCluster,
ThirdRealityMeteringCluster,
Expand Down Expand Up @@ -105,6 +106,35 @@
self._attr_native_value = value


class MatterDraftElectricalMeasurementSensor(MatterEntity, SensorEntity):
"""Representation of a Matter sensor for Matter 1.0 draft ElectricalMeasurement cluster."""

entity_description: MatterSensorEntityDescription

@callback
def _update_from_device(self) -> None:
"""Update from device."""
raw_value: Nullable | float | None
divisor: Nullable | float | None
multiplier: Nullable | float | None

raw_value, divisor, multiplier = (
self.get_matter_attribute_value(self._entity_info.attributes_to_watch[0]),
self.get_matter_attribute_value(self._entity_info.attributes_to_watch[1]),
self.get_matter_attribute_value(self._entity_info.attributes_to_watch[2]),
)

for value in (divisor, multiplier):
if value in (None, NullValue, 0):
self._attr_native_value = None
return

if raw_value in (None, NullValue):
self._attr_native_value = None

Check warning on line 133 in homeassistant/components/matter/sensor.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/matter/sensor.py#L133

Added line #L133 was not covered by tests
else:
self._attr_native_value = round(raw_value / divisor * multiplier, 2)


class MatterOperationalStateSensor(MatterSensor):
"""Representation of a sensor for Matter Operational State."""

Expand Down Expand Up @@ -641,6 +671,60 @@
clusters.ElectricalEnergyMeasurement.Attributes.CumulativeEnergyImported,
),
),
MatterDiscoverySchema(
platform=Platform.SENSOR,
entity_description=MatterSensorEntityDescription(
key="ElectricalMeasurementActivePower",
device_class=SensorDeviceClass.POWER,
entity_category=EntityCategory.DIAGNOSTIC,
native_unit_of_measurement=UnitOfPower.WATT,
suggested_display_precision=2,
state_class=SensorStateClass.MEASUREMENT,
),
entity_class=MatterDraftElectricalMeasurementSensor,
required_attributes=(
DraftElectricalMeasurementCluster.Attributes.ActivePower,
DraftElectricalMeasurementCluster.Attributes.AcPowerDivisor,
DraftElectricalMeasurementCluster.Attributes.AcPowerMultiplier,
),
absent_clusters=(clusters.ElectricalPowerMeasurement,),
),
MatterDiscoverySchema(
platform=Platform.SENSOR,
entity_description=MatterSensorEntityDescription(
key="ElectricalMeasurementRmsVoltage",
device_class=SensorDeviceClass.VOLTAGE,
entity_category=EntityCategory.DIAGNOSTIC,
native_unit_of_measurement=UnitOfElectricPotential.VOLT,
suggested_display_precision=0,
state_class=SensorStateClass.MEASUREMENT,
),
entity_class=MatterDraftElectricalMeasurementSensor,
required_attributes=(
DraftElectricalMeasurementCluster.Attributes.RmsVoltage,
DraftElectricalMeasurementCluster.Attributes.AcVoltageDivisor,
DraftElectricalMeasurementCluster.Attributes.AcVoltageMultiplier,
),
absent_clusters=(clusters.ElectricalPowerMeasurement,),
),
MatterDiscoverySchema(
platform=Platform.SENSOR,
entity_description=MatterSensorEntityDescription(
key="ElectricalMeasurementRmsCurrent",
device_class=SensorDeviceClass.CURRENT,
entity_category=EntityCategory.DIAGNOSTIC,
native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
suggested_display_precision=2,
state_class=SensorStateClass.MEASUREMENT,
),
entity_class=MatterDraftElectricalMeasurementSensor,
required_attributes=(
DraftElectricalMeasurementCluster.Attributes.RmsCurrent,
DraftElectricalMeasurementCluster.Attributes.AcCurrentDivisor,
DraftElectricalMeasurementCluster.Attributes.AcCurrentMultiplier,
),
absent_clusters=(clusters.ElectricalPowerMeasurement,),
),
MatterDiscoverySchema(
platform=Platform.SENSOR,
entity_description=MatterSensorEntityDescription(
Expand Down
1 change: 1 addition & 0 deletions tests/components/matter/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ async def integration_fixture(
"window_covering_pa_lift",
"window_covering_pa_tilt",
"window_covering_tilt",
"yandex_smart_socket",
]
)
async def matter_devices(
Expand Down
278 changes: 278 additions & 0 deletions tests/components/matter/fixtures/nodes/yandex_smart_socket.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,278 @@
{
"node_id": 4,
"date_commissioned": "2024-12-05T10:54:31.635203",
"last_interview": "2024-12-05T12:16:52.038776",
"interview_version": 6,
"available": true,
"is_bridge": false,
"attributes": {
"0/29/0": [
{
"0": 22,
"1": 1
}
],
"0/29/1": [29, 31, 40, 42, 48, 49, 51, 52, 54, 60, 62, 63],
"0/29/2": [41],
"0/29/3": [1],
"0/29/65532": 0,
"0/29/65533": 2,
"0/29/65528": [],
"0/29/65529": [],
"0/29/65531": [0, 1, 2, 3, 65528, 65529, 65531, 65532, 65533],
"0/31/0": [
{
"1": 5,
"2": 2,
"3": [112233],
"4": null,
"254": 3
}
],
"0/31/1": [],
"0/31/2": 4,
"0/31/3": 3,
"0/31/4": 4,
"0/31/65532": 0,
"0/31/65533": 1,
"0/31/65528": [],
"0/31/65529": [],
"0/31/65531": [0, 1, 2, 3, 4, 65528, 65529, 65531, 65532, 65533],
"0/40/0": 17,
"0/40/1": "Yandex",
"0/40/2": 5130,
"0/40/3": "YNDX-00540",
"0/40/4": 540,
"0/40/5": "",
"0/40/6": "XX",
"0/40/7": 0,
"0/40/8": "v0.4",
"0/40/9": 18,
"0/40/10": "8.0.r13402545-18",
"0/40/15": "HP000RM000V4RW",
"0/40/17": true,
"0/40/18": "E4480D32A5480B29",
"0/40/19": {
"0": 3,
"1": 3
},
"0/40/65532": 0,
"0/40/65533": 1,
"0/40/65528": [],
"0/40/65529": [],
"0/40/65531": [
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 17, 18, 19, 65528, 65529, 65531,
65532, 65533
],
"0/42/0": [],
"0/42/1": true,
"0/42/2": 1,
"0/42/3": null,
"0/42/65532": 0,
"0/42/65533": 1,
"0/42/65528": [],
"0/42/65529": [0],
"0/42/65531": [0, 1, 2, 3, 65528, 65529, 65531, 65532, 65533],
"0/48/0": 0,
"0/48/1": {
"0": 60,
"1": 900
},
"0/48/2": 0,
"0/48/3": 0,
"0/48/4": true,
"0/48/65532": 0,
"0/48/65533": 1,
"0/48/65528": [1, 3, 5],
"0/48/65529": [0, 2, 4],
"0/48/65531": [0, 1, 2, 3, 4, 65528, 65529, 65531, 65532, 65533],
"0/49/0": 1,
"0/49/1": [
{
"0": "**REDACTED**",
"1": true
}
],
"0/49/2": 10,
"0/49/3": 30,
"0/49/4": true,
"0/49/5": 0,
"0/49/6": "**REDACTED**",
"0/49/7": null,
"0/49/65532": 1,
"0/49/65533": 1,
"0/49/65528": [1, 5, 7],
"0/49/65529": [0, 2, 4, 6, 8],
"0/49/65531": [0, 1, 2, 3, 4, 5, 6, 7, 65528, 65529, 65531, 65532, 65533],
"0/51/0": [
{
"0": "WIFI_STA_DEF",
"1": true,
"2": null,
"3": null,
"4": "PAtP8Nse",
"5": ["wKgAEw=="],
"6": ["/oAAAAAAAAA+C0///vDbHg==", "/YrmoeskHZU+C0///vDbHg=="],
"7": 1
}
],
"0/51/1": 4,
"0/51/2": 124,
"0/51/3": 0,
"0/51/4": 1,
"0/51/5": [],
"0/51/6": [],
"0/51/7": [],
"0/51/8": false,
"0/51/65532": 0,
"0/51/65533": 1,
"0/51/65528": [],
"0/51/65529": [0],
"0/51/65531": [
0, 1, 2, 3, 4, 5, 6, 7, 8, 65528, 65529, 65531, 65532, 65533
],
"0/52/1": 79260,
"0/52/2": 171268,
"0/52/65532": 0,
"0/52/65533": 1,
"0/52/65528": [],
"0/52/65529": [],
"0/52/65531": [1, 2, 65528, 65529, 65531, 65532, 65533],
"0/54/0": "eJoYDvok",
"0/54/1": 4,
"0/54/2": 3,
"0/54/3": 11,
"0/54/4": -53,
"0/54/65532": 0,
"0/54/65533": 1,
"0/54/65528": [],
"0/54/65529": [],
"0/54/65531": [0, 1, 2, 3, 4, 65528, 65529, 65531, 65532, 65533],
"0/60/0": 0,
"0/60/1": null,
"0/60/2": null,
"0/60/65532": 0,
"0/60/65533": 1,
"0/60/65528": [],
"0/60/65529": [0, 1, 2],
"0/60/65531": [0, 1, 2, 65528, 65529, 65531, 65532, 65533],
"0/62/0": [
{
"1": "FTABAQEkAgE3AyQTAhgmBIAigScmBYAlTTo3BiQVASQRBBgkBwEkCAEwCUEEvOt9COzrjgf+b8q6FcKeKfbtqJybToVtEF0jiidbqg8FPmTIPTm1kU9hEiE6sd2N/GWSQHRoMi3YNl19h1PM3zcKNQEoARgkAgE2AwQCBAEYMAQUSu0+nQ/nOzrUNECyeBAqGPVu33YwBRS2PEiS/N109emRL3DTMaiWoWrEShgwC0DoGPCGt0HeGYnTS4TS2R7vbNhiFuuIrUQuxY5phP/UXBZosBDQTsnRTbMof18OkeO68MEcLXdIXjBJvBDaP/TsGA==",
"2": "FTABAQEkAgE3AyQUARgmBIAigScmBYAlTTo3BiQTAhgkBwEkCAEwCUEEz8nO5tz0gMFM5TW4YjYXGxkhr/UKHZg1rCa21StYqGd0wGaP7a5eMR+2BY20D1b11R7i6teWKnAaW+WqY0vQTjcKNQEpARgkAmAwBBS2PEiS/N109emRL3DTMaiWoWrESjAFFG//dFS5V0Y6/QdSQcC+z7idKKeJGDALQGFBsf7Ecq44e7NN8dCZIoJMUG16rmwD4ZtHtD4JPTxYabEreeblNF2ZDSgbo+A8sfz7Ci37WjznxbEj96vR8MgY",
"254": 3
}
],
"0/62/1": [
{
"1": "BLB0QnDldRPfV2xt6Nd/34ja8uaWwvsLYZsF3yCdIwyB/krYZ0u1uBS0FTo7E3iqvN0cDZ7fbhw0OUsKTVZ9Y10=",
"2": 65521,
"3": 1,
"4": 4,
"5": "",
"254": 3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EwCUEEMmvMdDf/h+u7fawdjIe6gXEeWuszCShR8ulsHMLnJYTMHVrkztOcj4cHw6haH/q909aVmL3xLlbEC2lZtmZClDcKNQEpARgkAmAwBBRoZjEcSXeh6IFBtW0A2OilJBdeYjAFFGhmMRxJd6HogUG1bQDY6KUkF15iGDALQJm5+/SkVrR4iBpGVqZZGOH+DpS+cQYqceN1+JSnDFwxJe+khYxFifMohSQ5NLlTiJQTZWYpqMKMZHT36pWWADUY"
],
"0/62/5": 3,
"0/62/65532": 0,
"0/62/65533": 1,
"0/62/65528": [1, 3, 5, 8],
"0/62/65529": [0, 2, 4, 6, 7, 9, 10, 11],
"0/62/65531": [0, 1, 2, 3, 4, 5, 65528, 65529, 65531, 65532, 65533],
"0/63/0": [],
"0/63/1": [],
"0/63/2": 4,
"0/63/3": 3,
"0/63/65532": 0,
"0/63/65533": 2,
"0/63/65528": [2, 5],
"0/63/65529": [0, 1, 3, 4],
"0/63/65531": [0, 1, 2, 3, 65528, 65529, 65531, 65532, 65533],
"1/3/0": 0,
"1/3/1": 2,
"1/3/65532": 0,
"1/3/65533": 4,
"1/3/65528": [],
"1/3/65529": [0, 64],
"1/3/65531": [0, 1, 65528, 65529, 65531, 65532, 65533],
"1/4/0": 128,
"1/4/65532": 1,
"1/4/65533": 4,
"1/4/65528": [0, 1, 2, 3],
"1/4/65529": [0, 1, 2, 3, 4, 5],
"1/4/65531": [0, 65528, 65529, 65531, 65532, 65533],
"1/6/0": true,
"1/6/16384": true,
"1/6/16385": 0,
"1/6/16386": 0,
"1/6/16387": 0,
"1/6/65532": 1,
"1/6/65533": 4,
"1/6/65528": [],
"1/6/65529": [0, 1, 2, 64, 65, 66],
"1/6/65531": [
0, 16384, 16385, 16386, 16387, 65528, 65529, 65531, 65532, 65533
],
"1/29/0": [
{
"0": 266,
"1": 2
}
],
"1/29/1": [3, 4, 6, 29, 2820, 336264194, 336264195],
"1/29/2": [],
"1/29/3": [],
"1/29/65532": 0,
"1/29/65533": 2,
"1/29/65528": [],
"1/29/65529": [],
"1/29/65531": [0, 1, 2, 3, 65528, 65529, 65531, 65532, 65533],
"1/2820/0": 9,
"1/2820/1285": 2170,
"1/2820/1288": 592,
"1/2820/1291": 560,
"1/2820/1536": 1,
"1/2820/1537": 10,
"1/2820/1538": 1,
"1/2820/1539": 1000,
"1/2820/1540": 1,
"1/2820/1541": 8,
"1/2820/2049": 2530,
"1/2820/2050": 16300,
"1/2820/65532": 0,
"1/2820/65533": 3,
"1/2820/65528": [],
"1/2820/65529": [],
"1/2820/65531": [
0, 1285, 1288, 1291, 1536, 1537, 1538, 1539, 1540, 1541, 2049, 2050,
65528, 65529, 65531, 65532, 65533
],
"1/336264194/336199680": 44,
"1/336264194/336199681": 0,
"1/336264194/336199682": 0,
"1/336264194/336199698": 70,
"1/336264194/65532": 0,
"1/336264194/65533": 1,
"1/336264194/65528": [],
"1/336264194/65529": [],
"1/336264194/65531": [
65528, 65529, 65531, 336199680, 336199681, 336199682, 336199698, 65532,
65533
],
"1/336264195/336199680": 0,
"1/336264195/65532": 0,
"1/336264195/65533": 1,
"1/336264195/65528": [],
"1/336264195/65529": [],
"1/336264195/65531": [65528, 65529, 65531, 336199680, 65532, 65533]
},
"attribute_subscriptions": []
}
Loading
Loading