Skip to content

Commit 6b75c27

Browse files
authored
Fix switch events for Aqara T2 relay (#2939)
1 parent e40f71a commit 6b75c27

File tree

2 files changed

+42
-31
lines changed

2 files changed

+42
-31
lines changed

tests/test_xiaomi.py

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232

3333
import zhaquirks
3434
from zhaquirks.const import (
35+
BUTTON_1,
36+
BUTTON_2,
3537
DEVICE_TYPE,
3638
ENDPOINTS,
3739
INPUT_CLUSTERS,
@@ -1609,31 +1611,6 @@ async def test_xiaomi_e1_roller_commands_1(zigpy_device_from_quirk, command, val
16091611
assert multistate_cluster._write_attributes.call_args[0][0][0].value.value == value
16101612

16111613

1612-
@pytest.mark.parametrize(
1613-
"quirk",
1614-
(zhaquirks.xiaomi.aqara.switch_acn047.AqaraT2Relay,),
1615-
)
1616-
async def test_aqara_t2_relay(zigpy_device_from_quirk, quirk):
1617-
"""Test Aqara T2 relay."""
1618-
1619-
device = zigpy_device_from_quirk(quirk)
1620-
mi_cluster = device.endpoints[1].multistate_input
1621-
mi_listener = ClusterListener(mi_cluster)
1622-
1623-
mi_cluster.update_attribute(MultistateInput.AttributeDefs.present_value.id, 1)
1624-
assert len(mi_listener.attribute_updates) == 1
1625-
assert mi_listener.attribute_updates[0][0] == 0
1626-
assert mi_listener.attribute_updates[0][1] == "single"
1627-
1628-
mi_cluster.update_attribute(MultistateInput.AttributeDefs.state_text.id, "foo")
1629-
assert len(mi_listener.attribute_updates) == 2
1630-
assert (
1631-
mi_listener.attribute_updates[1][0]
1632-
== MultistateInput.AttributeDefs.state_text.id
1633-
)
1634-
assert mi_listener.attribute_updates[1][1] == "foo"
1635-
1636-
16371614
@pytest.mark.parametrize(
16381615
"command, value",
16391616
[
@@ -1664,6 +1641,30 @@ async def test_xiaomi_e1_roller_commands_2(zigpy_device_from_quirk, command, val
16641641
)
16651642

16661643

1644+
@pytest.mark.parametrize("endpoint", [(1), (2)])
1645+
async def test_aqara_t2_relay(zigpy_device_from_quirk, endpoint):
1646+
"""Test Aqara T2 relay."""
1647+
1648+
device = zigpy_device_from_quirk(zhaquirks.xiaomi.aqara.switch_acn047.AqaraT2Relay)
1649+
mi_cluster = device.endpoints[endpoint].multistate_input
1650+
mi_listener = ClusterListener(mi_cluster)
1651+
1652+
buttons = {1: BUTTON_1, 2: BUTTON_2}
1653+
1654+
mi_cluster.update_attribute(MultistateInput.AttributeDefs.present_value.id, 1)
1655+
assert len(mi_listener.attribute_updates) == 1
1656+
assert mi_listener.attribute_updates[0][0] == 0
1657+
assert mi_listener.attribute_updates[0][1] == buttons[endpoint]
1658+
1659+
mi_cluster.update_attribute(MultistateInput.AttributeDefs.state_text.id, "foo")
1660+
assert len(mi_listener.attribute_updates) == 2
1661+
assert (
1662+
mi_listener.attribute_updates[1][0]
1663+
== MultistateInput.AttributeDefs.state_text.id
1664+
)
1665+
assert mi_listener.attribute_updates[1][1] == "foo"
1666+
1667+
16671668
def test_aqara_acn003_signature_match(assert_signature_matches_quirk):
16681669
signature = {
16691670
"node_descriptor": "NodeDescriptor(logical_type=<LogicalType.Router: 1>, complex_descriptor_available=0, user_descriptor_available=0, reserved=0, aps_flags=0, frequency_band=<FrequencyBand.Freq2400MHz: 8>, mac_capability_flags=<MACCapabilityFlags.FullFunctionDevice|MainsPowered|RxOnWhenIdle|AllocateAddress: 142>, manufacturer_code=4447, maximum_buffer_size=82, maximum_incoming_transfer_size=82, server_mask=11264, maximum_outgoing_transfer_size=82, descriptor_capability_field=<DescriptorCapability.NONE: 0>, *allocate_address=True, *is_alternate_pan_coordinator=False, *is_coordinator=False, *is_end_device=False, *is_full_function_device=True, *is_mains_powered=True, *is_receiver_on_when_idle=True, *is_router=True, *is_security_capable=False)",

zhaquirks/xiaomi/aqara/switch_acn047.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@
1919

2020
from zhaquirks.const import (
2121
ATTR_ID,
22+
BUTTON_1,
23+
BUTTON_2,
2224
COMMAND,
2325
DEVICE_TYPE,
26+
ENDPOINT_ID,
2427
ENDPOINTS,
2528
INPUT_CLUSTERS,
2629
MODELS_INFO,
@@ -40,8 +43,7 @@
4043
XiaomiCustomDevice,
4144
)
4245

43-
PRESS_TYPES = {1: "single"}
44-
SINGLE = "single"
46+
PRESS_TYPES = {1: BUTTON_1, 2: BUTTON_2}
4547
STATUS_TYPE_ATTR = 0x0055 # decimal = 85
4648

4749

@@ -54,14 +56,15 @@ def __init__(self, *args, **kwargs):
5456
super().__init__(*args, **kwargs)
5557

5658
def _update_attribute(self, attrid, value):
57-
if attrid == STATUS_TYPE_ATTR:
58-
self._current_state = PRESS_TYPES.get(value)
59+
if attrid == STATUS_TYPE_ATTR and value == 1:
60+
self._current_state = PRESS_TYPES.get(self.endpoint.endpoint_id)
5961
event_args = {
6062
PRESS_TYPE: self._current_state,
6163
ATTR_ID: attrid,
6264
VALUE: value,
65+
ENDPOINT_ID: self.endpoint.endpoint_id,
6366
}
64-
self.listener_event(ZHA_SEND_EVENT, self, self._current_state, event_args)
67+
self.listener_event(ZHA_SEND_EVENT, self._current_state, event_args)
6568
super()._update_attribute(0, self._current_state)
6669
else:
6770
super()._update_attribute(attrid, value)
@@ -206,5 +209,12 @@ class AqaraT2Relay(XiaomiCustomDevice):
206209
}
207210

208211
device_automation_triggers = {
209-
(SHORT_PRESS, SHORT_PRESS): {COMMAND: SINGLE},
212+
(SHORT_PRESS, PRESS_TYPES[1]): {
213+
ENDPOINT_ID: 1,
214+
COMMAND: PRESS_TYPES[1],
215+
},
216+
(SHORT_PRESS, PRESS_TYPES[2]): {
217+
ENDPOINT_ID: 2,
218+
COMMAND: PRESS_TYPES[2],
219+
},
210220
}

0 commit comments

Comments
 (0)