Skip to content

Commit a1c0b1e

Browse files
authored
Remove incorrect total power attribute forwarding from Xiaomi (#4095)
1 parent b5f2f93 commit a1c0b1e

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

tests/test_xiaomi.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -601,19 +601,14 @@ async def test_xiaomi_plug_power(zigpy_device_from_quirk, quirk):
601601
assert em_listener.attribute_updates[1][0] == zcl_em_current_power
602602
assert em_listener.attribute_updates[1][1] == 150 # multiplied by 10
603603

604-
# Test total power consumption on ElectricalMeasurement cluster and SmartEnergy cluster
605-
zcl_em_total_power = ElectricalMeasurement.AttributeDefs.total_active_power.id
604+
# Test total power consumption on SmartEnergy cluster
606605
zcl_se_total_power = Metering.AttributeDefs.current_summ_delivered.id
607606
se_cluster = device.endpoints[1].smartenergy_metering
608607
se_listener = ClusterListener(se_cluster)
609608

610609
basic_cluster.update_attribute(
611610
XIAOMI_AQARA_ATTRIBUTE, create_aqara_attr_report({149: 0.001})
612611
)
613-
# electrical measurement cluster
614-
assert len(em_listener.attribute_updates) == 3
615-
assert em_listener.attribute_updates[2][0] == zcl_em_total_power
616-
assert em_listener.attribute_updates[2][1] == 1 # multiplied by 1000
617612

618613
# smart energy cluster
619614
assert len(se_listener.attribute_updates) == 1
@@ -630,8 +625,20 @@ async def test_xiaomi_plug_power(zigpy_device_from_quirk, quirk):
630625
assert analog_input_listener.attribute_updates[0][0] == zcl_analog_input_value
631626
assert analog_input_listener.attribute_updates[0][1] == 40
632627

633-
assert em_listener.attribute_updates[3][0] == zcl_em_current_power
634-
assert em_listener.attribute_updates[3][1] == 400 # multiplied by 10
628+
assert em_listener.attribute_updates[2][0] == zcl_em_current_power
629+
assert em_listener.attribute_updates[2][1] == 400 # multiplied by 10
630+
631+
632+
async def test_xiaomi_total_active_power_clear(zigpy_device_from_quirk):
633+
"""Tests that the total_active_power attribute is cleared during init."""
634+
635+
with mock.patch(
636+
"zhaquirks.xiaomi.ElectricalMeasurementCluster._update_attribute"
637+
) as update_attribute_mock:
638+
zigpy_device_from_quirk(zhaquirks.xiaomi.aqara.plug_eu.PlugMAEU01)
639+
update_attribute_mock.assert_called_with(
640+
ElectricalMeasurement.AttributeDefs.total_active_power.id, None
641+
)
635642

636643

637644
@pytest.mark.parametrize(

zhaquirks/xiaomi/__init__.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -304,10 +304,6 @@ def _update_attribute(self, attrid, value):
304304

305305
if CONSUMPTION in attributes:
306306
zcl_consumption = round(attributes[CONSUMPTION] * 1000)
307-
self.endpoint.electrical_measurement.update_attribute(
308-
ElectricalMeasurement.AttributeDefs.total_active_power.id,
309-
zcl_consumption,
310-
)
311307
self.endpoint.smartenergy_metering.update_attribute(
312308
Metering.AttributeDefs.current_summ_delivered.id, zcl_consumption
313309
)
@@ -661,6 +657,14 @@ def __init__(self, *args, **kwargs):
661657
if self.CONSUMPTION_ID not in self._attr_cache:
662658
self._update_attribute(self.CONSUMPTION_ID, 0)
663659

660+
# Previously, this cluster was wrongly setting the total_active_power attribute,
661+
# which was not added to HA.
662+
# Since it is now added to HA and the incorrect value could be set, we need to
663+
# reset it.
664+
self._update_attribute(
665+
ElectricalMeasurement.AttributeDefs.total_active_power.id, None
666+
)
667+
664668

665669
class MeteringCluster(LocalDataCluster, Metering):
666670
"""Metering cluster to receive reports that are sent to the basic cluster."""

0 commit comments

Comments
 (0)