Skip to content

Commit 7e5fd50

Browse files
committed
Made HA events backward compatible. Also send more information as arguments of the zha_event: Added "description" containing the ButtonAction name.
Got rid of deprecation warnings in test_sinope.py
1 parent 5ee1785 commit 7e5fd50

File tree

3 files changed

+75
-43
lines changed

3 files changed

+75
-43
lines changed

tests/test_sinope.py

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,10 @@ def _get_packet_data(
101101
@pytest.mark.parametrize(
102102
"press_type,button,exp_event",
103103
(
104-
(ButtonAction.Single_off, TURN_OFF, COMMAND_M_INITIAL_PRESS),
105-
(ButtonAction.Single_on, TURN_ON, COMMAND_M_INITIAL_PRESS),
106-
(ButtonAction.Single_release_off, TURN_OFF, COMMAND_M_SHORT_RELEASE),
107-
(ButtonAction.Single_release_on, TURN_ON, COMMAND_M_SHORT_RELEASE),
104+
(ButtonAction.Pressed_off, TURN_OFF, COMMAND_M_INITIAL_PRESS),
105+
(ButtonAction.Pressed_on, TURN_ON, COMMAND_M_INITIAL_PRESS),
106+
(ButtonAction.Released_off, TURN_OFF, COMMAND_M_SHORT_RELEASE),
107+
(ButtonAction.Released_on, TURN_ON, COMMAND_M_SHORT_RELEASE),
108108
(ButtonAction.Double_on, TURN_ON, COMMAND_M_MULTI_PRESS_COMPLETE),
109109
(ButtonAction.Double_off, TURN_OFF, COMMAND_M_MULTI_PRESS_COMPLETE),
110110
(ButtonAction.Long_on, TURN_ON, COMMAND_M_LONG_RELEASE),
@@ -135,7 +135,16 @@ class Listener:
135135
),
136136
)
137137
data = _get_packet_data(foundation.GeneralCommand.Report_Attributes, attr)
138-
device.handle_message(260, cluster_id, endpoint_id, endpoint_id, data)
138+
139+
device.packet_received(
140+
t.ZigbeePacket(
141+
profile_id=260,
142+
cluster_id=cluster_id,
143+
src_ep=endpoint_id,
144+
dst_ep=endpoint_id,
145+
data=t.SerializableBytes(data),
146+
)
147+
)
139148

140149
if exp_event is None:
141150
assert cluster_listener.zha_send_event.call_count == 0
@@ -147,6 +156,7 @@ class Listener:
147156
"attribute_id": 84,
148157
"attribute_name": "action_report",
149158
"button": button,
159+
"description": press_type.name,
150160
"value": press_type.value,
151161
},
152162
)
@@ -172,7 +182,15 @@ class Listener:
172182

173183
# read attributes general command
174184
data = _get_packet_data(foundation.GeneralCommand.Read_Attributes)
175-
device.handle_message(260, cluster_id, endpoint_id, endpoint_id, data)
185+
device.packet_received(
186+
t.ZigbeePacket(
187+
profile_id=260,
188+
cluster_id=cluster_id,
189+
src_ep=endpoint_id,
190+
dst_ep=endpoint_id,
191+
data=t.SerializableBytes(data),
192+
)
193+
)
176194
# no ZHA events emitted because we only handle Report_Attributes
177195
assert cluster_listener.zha_send_event.call_count == 0
178196

@@ -184,7 +202,15 @@ class Listener:
184202
), # 0x29 = t.int16s
185203
)
186204
data = _get_packet_data(foundation.GeneralCommand.Report_Attributes, attr)
187-
device.handle_message(260, cluster_id, endpoint_id, endpoint_id, data)
205+
device.packet_received(
206+
t.ZigbeePacket(
207+
profile_id=260,
208+
cluster_id=cluster_id,
209+
src_ep=endpoint_id,
210+
dst_ep=endpoint_id,
211+
data=t.SerializableBytes(data),
212+
)
213+
)
188214
# ZHA event emitted because we pass non "action_report"
189215
# reports to the base class handler.
190216
assert cluster_listener.zha_send_event.call_count == 1

zhaquirks/sinope/__init__.py

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Module for Sinope quirks implementations."""
22

33
from zigpy.quirks import CustomCluster
4+
import zigpy.types as t
45
from zigpy.zcl.clusters.general import DeviceTemperature
56

67
from zhaquirks.const import (
@@ -16,7 +17,7 @@
1617
COMMAND_M_SHORT_RELEASE,
1718
DOUBLE_PRESS,
1819
ENDPOINT_ID,
19-
LONG_RELEASE,
20+
LONG_PRESS,
2021
SHORT_PRESS,
2122
SHORT_RELEASE,
2223
TURN_OFF,
@@ -28,6 +29,20 @@
2829
SINOPE_MANUFACTURER_CLUSTER_ID = 0xFF01
2930
ATTRIBUTE_ACTION = "action_report"
3031

32+
33+
class ButtonAction(t.enum8):
34+
"""Action_report values."""
35+
36+
Pressed_on = 0x01
37+
Released_on = 0x02
38+
Long_on = 0x03
39+
Double_on = 0x04
40+
Pressed_off = 0x11
41+
Released_off = 0x12
42+
Long_off = 0x13
43+
Double_off = 0x14
44+
45+
3146
LIGHT_DEVICE_TRIGGERS = {
3247
(SHORT_PRESS, TURN_ON): {
3348
ENDPOINT_ID: 1,
@@ -37,7 +52,7 @@
3752
ATTRIBUTE_ID: 84,
3853
ATTRIBUTE_NAME: ATTRIBUTE_ACTION,
3954
BUTTON: TURN_ON,
40-
VALUE: 1,
55+
VALUE: ButtonAction.Pressed_on,
4156
},
4257
},
4358
(SHORT_PRESS, TURN_OFF): {
@@ -48,7 +63,7 @@
4863
ATTRIBUTE_ID: 84,
4964
ATTRIBUTE_NAME: ATTRIBUTE_ACTION,
5065
BUTTON: TURN_OFF,
51-
VALUE: 17,
66+
VALUE: ButtonAction.Pressed_off,
5267
},
5368
},
5469
(SHORT_RELEASE, TURN_ON): {
@@ -59,7 +74,7 @@
5974
ATTRIBUTE_ID: 84,
6075
ATTRIBUTE_NAME: ATTRIBUTE_ACTION,
6176
BUTTON: TURN_ON,
62-
VALUE: 2,
77+
VALUE: ButtonAction.Released_on,
6378
},
6479
},
6580
(SHORT_RELEASE, TURN_OFF): {
@@ -70,7 +85,7 @@
7085
ATTRIBUTE_ID: 84,
7186
ATTRIBUTE_NAME: ATTRIBUTE_ACTION,
7287
BUTTON: TURN_OFF,
73-
VALUE: 18,
88+
VALUE: ButtonAction.Released_off,
7489
},
7590
},
7691
(DOUBLE_PRESS, TURN_ON): {
@@ -81,7 +96,7 @@
8196
ATTRIBUTE_ID: 84,
8297
ATTRIBUTE_NAME: ATTRIBUTE_ACTION,
8398
BUTTON: TURN_ON,
84-
VALUE: 4,
99+
VALUE: ButtonAction.Double_on,
85100
},
86101
},
87102
(DOUBLE_PRESS, TURN_OFF): {
@@ -92,29 +107,29 @@
92107
ATTRIBUTE_ID: 84,
93108
ATTRIBUTE_NAME: ATTRIBUTE_ACTION,
94109
BUTTON: TURN_OFF,
95-
VALUE: 20,
110+
VALUE: ButtonAction.Double_off,
96111
},
97112
},
98-
(LONG_RELEASE, TURN_ON): {
113+
(LONG_PRESS, TURN_ON): {
99114
ENDPOINT_ID: 1,
100115
CLUSTER_ID: 65281,
101116
COMMAND: COMMAND_M_LONG_RELEASE,
102117
ARGS: {
103118
ATTRIBUTE_ID: 84,
104119
ATTRIBUTE_NAME: ATTRIBUTE_ACTION,
105120
BUTTON: TURN_ON,
106-
VALUE: 3,
121+
VALUE: ButtonAction.Long_on,
107122
},
108123
},
109-
(LONG_RELEASE, TURN_OFF): {
124+
(LONG_PRESS, TURN_OFF): {
110125
ENDPOINT_ID: 1,
111126
CLUSTER_ID: 65281,
112127
COMMAND: COMMAND_M_LONG_RELEASE,
113128
ARGS: {
114129
ATTRIBUTE_ID: 84,
115130
ATTRIBUTE_NAME: ATTRIBUTE_ACTION,
116131
BUTTON: TURN_OFF,
117-
VALUE: 19,
132+
VALUE: ButtonAction.Long_off,
118133
},
119134
},
120135
}

zhaquirks/sinope/light.py

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@
3030
ATTRIBUTE_ID,
3131
ATTRIBUTE_NAME,
3232
BUTTON,
33+
COMMAND,
3334
COMMAND_M_INITIAL_PRESS,
3435
COMMAND_M_LONG_RELEASE,
3536
COMMAND_M_MULTI_PRESS_COMPLETE,
3637
COMMAND_M_SHORT_RELEASE,
38+
DESCRIPTION,
3739
DEVICE_TYPE,
3840
ENDPOINTS,
3941
INPUT_CLUSTERS,
@@ -51,6 +53,7 @@
5153
LIGHT_DEVICE_TRIGGERS,
5254
SINOPE,
5355
SINOPE_MANUFACTURER_CLUSTER_ID,
56+
ButtonAction,
5457
CustomDeviceTemperatureCluster,
5558
)
5659

@@ -79,19 +82,6 @@ class DoubleFull(t.enum8):
7982
On = 0x01
8083

8184

82-
class ButtonAction(t.enum8):
83-
"""Action_report values."""
84-
85-
Single_on = 0x01
86-
Single_release_on = 0x02
87-
Long_on = 0x03
88-
Double_on = 0x04
89-
Single_off = 0x11
90-
Single_release_off = 0x12
91-
Long_off = 0x13
92-
Double_off = 0x14
93-
94-
9585
class SinopeTechnologiesManufacturerCluster(CustomCluster):
9686
"""SinopeTechnologiesManufacturerCluster manufacturer cluster."""
9787

@@ -163,7 +153,7 @@ class AttributeDefs(foundation.BaseAttributeDefs):
163153
server_commands = {
164154
0x54: foundation.ZCLCommandDef(
165155
"button_press",
166-
{"command": t.uint8_t},
156+
{COMMAND: t.uint8_t},
167157
direction=foundation.Direction.Server_to_Client,
168158
is_manufacturer_specific=True,
169159
)
@@ -197,40 +187,41 @@ def handle_cluster_general_request(
197187
hdr, args, dst_addressing=dst_addressing
198188
)
199189

200-
value = attr.value.value
190+
action = self.Action(attr.value.value)
201191

202-
action, button = self._get_command_from_action(self.Action(value))
203-
if not action or not button:
192+
command, button = self._get_command_from_action(action)
193+
if not command or not button:
204194
return
205195

206196
event_args = {
207197
ATTRIBUTE_ID: 84,
208198
ATTRIBUTE_NAME: ATTRIBUTE_ACTION,
209199
BUTTON: button,
210-
VALUE: value.value,
200+
DESCRIPTION: action.name,
201+
VALUE: action.value,
211202
}
212203

213204
self.debug(
214-
"SINOPE ZHA_SEND_EVENT action: '%s' event_args: %s",
215-
self.Action(value),
205+
"SINOPE ZHA_SEND_EVENT command: '%s' event_args: %s",
206+
command,
216207
event_args,
217208
)
218209

219-
self.listener_event(ZHA_SEND_EVENT, action, event_args)
210+
self.listener_event(ZHA_SEND_EVENT, command, event_args)
220211

221212
def _get_command_from_action(
222213
self, action: ButtonAction
223214
) -> tuple[str | None, str | None]:
224215
# const lookup = {1: 'up_single', 2: 'up_single_released', 3: 'up_hold', 4: 'up_double',
225216
# 17: 'down_single, 18: 'down_single_released', 19: 'down_hold', 20: 'down_double'};
226217
match action:
227-
case self.Action.Single_off:
218+
case self.Action.Pressed_off:
228219
return COMMAND_M_INITIAL_PRESS, TURN_OFF
229-
case self.Action.Single_on:
220+
case self.Action.Pressed_on:
230221
return COMMAND_M_INITIAL_PRESS, TURN_ON
231-
case self.Action.Single_release_off:
222+
case self.Action.Released_off:
232223
return COMMAND_M_SHORT_RELEASE, TURN_OFF
233-
case self.Action.Single_release_on:
224+
case self.Action.Released_on:
234225
return COMMAND_M_SHORT_RELEASE, TURN_ON
235226
case self.Action.Double_off:
236227
return COMMAND_M_MULTI_PRESS_COMPLETE, TURN_OFF

0 commit comments

Comments
 (0)