File: test_button_controller.py

package info (click to toggle)
python-aiohue 4.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 612 kB
  • sloc: python: 4,444; sh: 30; makefile: 5
file content (310 lines) | stat: -rw-r--r-- 9,667 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
"""Test button controller functions."""

import asyncio
from copy import deepcopy
from unittest.mock import Mock
from uuid import uuid4

from aiohue import HueBridgeV2
from aiohue.v2 import EventType
from aiohue.v2.controllers.sensors import ButtonController
from aiohue.v2.models.button import ButtonEvent
from aiohue.v2.models.resource import ResourceTypes


async def create_button(
    controller: ButtonController, callback: Mock, button_id: str, device_id: str
):
    """Create a button resource."""
    evt_data = {
        "id": button_id,
        "metadata": {"control_id": 1},
        "button": {
            "event_values": [
                "initial_press",
                "repeat",
                "short_release",
                "long_release",
                "long_press",
            ],
            "repeat_interval": 800,
        },
        "owner": {"rid": device_id, "rtype": ResourceTypes.DEVICE},
    }

    # Create a new resource
    # pylint: disable=protected-access
    await controller._handle_event(EventType.RESOURCE_ADDED, evt_data)

    callback.assert_called_once()
    callback.reset_mock()


async def create_device(
    bridge: HueBridgeV2, device_id: str, model_id: str, button_id: str
):
    """Create a device resource."""
    evt_data = {
        "id": device_id,
        "services": [{"rid": button_id, "rtype": "button"}],
        "product_data": {
            "model_id": model_id,
            "manufacturer_name": "Signify",
            "product_name": "Hue Light",
            "product_archetype": "classic_bulb",
            "certified": True,
            "software_version": "1.0.0",
        },
        "metadata": {"archetype": "classic_bulb", "name": "Kitchen"},
    }

    # pylint: disable=protected-access
    await bridge.devices._handle_event(EventType.RESOURCE_ADDED, evt_data)


def generate_button_event_data(button_id: str, event: str, device_id: str):
    """Generate button event data."""
    return {
        "id": button_id,
        "button": {
            "button_report": {
                "event": event,
                "updated": "2024-08-24T16:24:00.0Z",
            }
        },
        "owner": {"rid": device_id, "rtype": ResourceTypes.DEVICE},
    }


async def handle_button_event(
    controller: ButtonController, button_id: str, event: str, device_id: str
):
    """Handle button event."""
    # pylint: disable=protected-access
    await controller._handle_event(
        EventType.RESOURCE_UPDATED,
        generate_button_event_data(button_id, event, device_id),
    )


class CopyingMock(Mock):
    """Mock that deep copies its arguments."""

    def __call__(self, *args, **kwargs):
        args = deepcopy(args)
        kwargs = deepcopy(kwargs)
        return super().__call__(*args, **kwargs)


async def test_handle_events():
    """Test handling of events."""
    bridge = HueBridgeV2("127.0.0.1", "fake")
    controller = ButtonController(bridge)
    callback = CopyingMock(return_value=None)
    controller.subscribe(callback)

    button_id = str(uuid4())
    device_id = str(uuid4())

    await create_button(controller, callback, button_id, device_id)

    # Button event
    await handle_button_event(controller, button_id, "initial_press", device_id)

    callback.assert_called_once()
    assert (
        callback.call_args.args[1].button.button_report.event
        == ButtonEvent.INITIAL_PRESS
    )
    callback.reset_mock()

    evt_data = {
        "id": button_id,
        "owner": {"rid": device_id, "rtype": ResourceTypes.DEVICE},
    }

    # Absent button report is dropped
    # pylint: disable=protected-access
    await controller._handle_event(EventType.RESOURCE_UPDATED, evt_data)

    callback.assert_not_called()
    callback.reset_mock()


async def test_handle_events_button_workaround_short_release():
    """Test handling of events for device requiring button workaround."""
    bridge = HueBridgeV2("127.0.0.1", "fake")
    controller = ButtonController(bridge)
    callback = CopyingMock(return_value=None)
    controller.subscribe(callback)

    button_id = str(uuid4())
    device_id = str(uuid4())

    await create_device(bridge, device_id, "FOHSWITCH", button_id)
    await create_button(controller, callback, button_id, device_id)

    # Button event: initial_press
    await handle_button_event(controller, button_id, "initial_press", device_id)

    await asyncio.sleep(1.2)

    # Button event: short_release
    await handle_button_event(controller, button_id, "short_release", device_id)

    callback.assert_called()
    assert callback.call_count == 2
    assert (
        callback.call_args_list[0].args[1].button.button_report.event
        == ButtonEvent.INITIAL_PRESS
    )
    assert (
        callback.call_args_list[1].args[1].button.button_report.event
        == ButtonEvent.SHORT_RELEASE
    )


async def test_handle_events_button_workaround_repeats():
    """Test handling of events for device requiring button workaround."""
    bridge = HueBridgeV2("127.0.0.1", "fake")
    controller = ButtonController(bridge)
    callback = CopyingMock(return_value=None)
    controller.subscribe(callback)

    button_id = str(uuid4())
    device_id = str(uuid4())

    await create_device(bridge, device_id, "FOHSWITCH", button_id)
    await create_button(controller, callback, button_id, device_id)

    # Button event: initial_press
    await handle_button_event(controller, button_id, "initial_press", device_id)

    await asyncio.sleep(2.2)

    # Button event: short_release
    await handle_button_event(controller, button_id, "short_release", device_id)

    callback.assert_called()
    assert callback.call_count == 4
    assert (
        callback.call_args_list[0].args[1].button.button_report.event
        == ButtonEvent.INITIAL_PRESS
    )
    assert (
        callback.call_args_list[1].args[1].button.button_report.event
        == ButtonEvent.REPEAT
    )
    assert (
        callback.call_args_list[2].args[1].button.button_report.event
        == ButtonEvent.REPEAT
    )
    assert (
        callback.call_args_list[3].args[1].button.button_report.event
        == ButtonEvent.SHORT_RELEASE
    )


async def test_handle_events_button_workaround_long_release():
    """Test handling of events for device requiring button workaround."""
    bridge = HueBridgeV2("127.0.0.1", "fake")
    controller = ButtonController(bridge)
    callback = CopyingMock(return_value=None)
    controller.subscribe(callback)

    button_id = str(uuid4())
    device_id = str(uuid4())

    await create_device(bridge, device_id, "FOHSWITCH", button_id)
    await create_button(controller, callback, button_id, device_id)

    # Button event: initial_press
    await handle_button_event(controller, button_id, "initial_press", device_id)

    await asyncio.sleep(12.7)

    callback.assert_called()
    assert callback.call_count == 23
    assert (
        callback.call_args_list[22].args[1].button.button_report.event
        == ButtonEvent.LONG_RELEASE
    )


async def test_handle_events_button_workaround_interrupt():
    """Test handling of events for device requiring button workaround."""
    bridge = HueBridgeV2("127.0.0.1", "fake")
    controller = ButtonController(bridge)
    callback = CopyingMock(return_value=None)
    controller.subscribe(callback)

    button1_id = str(uuid4())
    button2_id = str(uuid4())
    device1_id = str(uuid4())
    device2_id = str(uuid4())

    await create_device(bridge, device1_id, "FOHSWITCH", button1_id)
    await create_device(bridge, device2_id, "ZGPSWITCH", button2_id)
    await create_button(controller, callback, button1_id, device1_id)
    await create_button(controller, callback, button2_id, device2_id)

    # Button event: initial_press
    await handle_button_event(controller, button1_id, "initial_press", device1_id)

    await asyncio.sleep(2.2)

    # Button event: initial_press
    await handle_button_event(controller, button2_id, "initial_press", device2_id)

    callback.assert_called()
    assert callback.call_count == 4
    assert (
        callback.call_args_list[0].args[1].button.button_report.event
        == ButtonEvent.INITIAL_PRESS
    )
    assert (
        callback.call_args_list[1].args[1].button.button_report.event
        == ButtonEvent.REPEAT
    )
    assert (
        callback.call_args_list[2].args[1].button.button_report.event
        == ButtonEvent.REPEAT
    )
    assert callback.call_args_list[3].args[1].id == button2_id
    assert (
        callback.call_args_list[3].args[1].button.button_report.event
        == ButtonEvent.INITIAL_PRESS
    )


async def test_handle_events_button_non_workaround_device():
    """Test handling of events for device requiring button workaround."""
    bridge = HueBridgeV2("127.0.0.1", "fake")
    controller = ButtonController(bridge)
    callback = CopyingMock(return_value=None)
    controller.subscribe(callback)

    button_id = str(uuid4())
    device_id = str(uuid4())

    await create_device(bridge, device_id, "ZGPSWITCH", button_id)
    await create_button(controller, callback, button_id, device_id)

    # Button event: initial_press
    await handle_button_event(controller, button_id, "initial_press", device_id)

    await asyncio.sleep(2.2)

    # Button event: short_release
    await handle_button_event(controller, button_id, "short_release", device_id)

    callback.assert_called()
    assert callback.call_count == 2
    assert (
        callback.call_args_list[0].args[1].button.button_report.event
        == ButtonEvent.INITIAL_PRESS
    )
    assert (
        callback.call_args_list[1].args[1].button.button_report.event
        == ButtonEvent.SHORT_RELEASE
    )