File: binary_sensor_test.py

package info (click to toggle)
python-xknx 3.10.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,044 kB
  • sloc: python: 40,087; javascript: 8,556; makefile: 32; sh: 12
file content (386 lines) | stat: -rw-r--r-- 13,682 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
"""Unit test for BinarySensor objects."""

from unittest.mock import Mock, patch

from xknx import XKNX
from xknx.devices import BinarySensor
from xknx.dpt import DPTArray, DPTBinary
from xknx.telegram import GroupAddress, Telegram
from xknx.telegram.apci import GroupValueResponse, GroupValueWrite

from ..conftest import EventLoopClockAdvancer


class TestBinarySensor:
    """Test class for BinarySensor objects."""

    #
    # TEST PROCESS
    #
    async def test_process(self) -> None:
        """Test process / reading telegrams from telegram queue."""
        xknx = XKNX()
        binaryinput = BinarySensor(xknx, "TestInput", "1/2/3")

        assert binaryinput.state is None

        telegram_on = Telegram(
            destination_address=GroupAddress("1/2/3"),
            payload=GroupValueWrite(DPTBinary(1)),
        )
        binaryinput.process(telegram_on)
        assert binaryinput.state is True

        telegram_off = Telegram(
            destination_address=GroupAddress("1/2/3"),
            payload=GroupValueWrite(DPTBinary(0)),
        )
        binaryinput.process(telegram_off)
        assert binaryinput.state is False

        binaryinput2 = BinarySensor(xknx, "TestInput", "1/2/4")
        assert binaryinput2.state is None

        telegram_off2 = Telegram(
            destination_address=GroupAddress("1/2/4"),
            payload=GroupValueWrite(DPTBinary(0)),
        )
        binaryinput2.process(telegram_off2)
        assert binaryinput2.last_telegram == telegram_off2
        assert binaryinput2.state is False

    async def test_process_invert(self) -> None:
        """Test process / reading telegrams from telegram queue."""
        xknx = XKNX()
        bs_invert = BinarySensor(xknx, "TestInput", "1/2/3", invert=True)

        assert bs_invert.state is None

        telegram_on = Telegram(
            destination_address=GroupAddress("1/2/3"),
            payload=GroupValueWrite(DPTBinary(0)),
        )
        bs_invert.process(telegram_on)

        assert bs_invert.state is True

        telegram_off = Telegram(
            destination_address=GroupAddress("1/2/3"),
            payload=GroupValueWrite(DPTBinary(1)),
        )
        bs_invert.process(telegram_off)
        assert bs_invert.state is False

    async def test_process_reset_after(
        self, time_travel: EventLoopClockAdvancer
    ) -> None:
        """Test process / reading telegrams from telegram queue."""
        xknx = XKNX()
        reset_after_sec = 1
        after_update_callback = Mock()
        binaryinput = BinarySensor(
            xknx,
            "TestInput",
            "1/2/3",
            reset_after=reset_after_sec,
            device_updated_cb=after_update_callback,
        )
        telegram_on = Telegram(
            destination_address=GroupAddress("1/2/3"),
            payload=GroupValueWrite(DPTBinary(1)),
        )

        binaryinput.process(telegram_on)
        assert binaryinput.state

        await time_travel(reset_after_sec)
        assert not binaryinput.state
        # once for 'on' and once for 'off'
        assert after_update_callback.call_count == 2

        after_update_callback.reset_mock()
        # multiple telegrams during reset_after time period shall reset timer
        binaryinput.process(telegram_on)
        after_update_callback.assert_called_once()
        binaryinput.process(telegram_on)
        binaryinput.process(telegram_on)
        # second and third telegram resets timer but doesn't run callback
        after_update_callback.assert_called_once()
        assert binaryinput.state

        await time_travel(reset_after_sec)
        assert not binaryinput.state
        # once for 'on' and once for 'off'
        assert after_update_callback.call_count == 2

    async def test_process_wrong_payload(self) -> None:
        """Test process wrong telegram (wrong payload type)."""
        xknx = XKNX()
        binary_sensor = BinarySensor(xknx, "Warning", group_address_state="1/2/3")
        telegram = Telegram(
            destination_address=GroupAddress("1/2/3"),
            payload=GroupValueWrite(DPTArray((0x1, 0x2, 0x3))),
        )
        with patch("logging.Logger.warning") as log_mock:
            binary_sensor.process(telegram)
            log_mock.assert_called_once()
            assert binary_sensor.state is None

    #
    # TEST SWITCHED ON
    #
    def test_is_on(self) -> None:
        """Test is_on() and is_off() of a BinarySensor with state 'on'."""
        xknx = XKNX()
        binaryinput = BinarySensor(xknx, "TestInput", "1/2/3")
        assert not binaryinput.is_on()
        assert binaryinput.is_off()
        binaryinput._set_internal_state(True)

        assert binaryinput.is_on()
        assert not binaryinput.is_off()

    #
    # TEST SWITCHED OFF
    #
    def test_is_off(self) -> None:
        """Test is_on() and is_off() of a BinarySensor with state 'off'."""
        xknx = XKNX()
        binaryinput = BinarySensor(xknx, "TestInput", "1/2/3")
        binaryinput._set_internal_state(False)

        assert not binaryinput.is_on()
        assert binaryinput.is_off()

    #
    # TEST PROCESS CALLBACK
    #
    async def test_process_callback(self) -> None:
        """Test after_update_callback after state of binary sensor was changed."""
        xknx = XKNX()
        switch = BinarySensor(
            xknx, "TestInput", group_address_state="1/2/3", ignore_internal_state=False
        )
        after_update_callback = Mock()

        switch.register_device_updated_cb(after_update_callback)

        telegram = Telegram(
            destination_address=GroupAddress("1/2/3"),
            payload=GroupValueWrite(DPTBinary(1)),
        )
        switch.process(telegram)
        # no _context_task started because ignore_internal_state is False
        assert switch._context_task is None
        after_update_callback.assert_called_once_with(switch)

        after_update_callback.reset_mock()
        # send same telegram again
        switch.process(telegram)
        after_update_callback.assert_not_called()

    async def test_process_callback_context_timeout(
        self, time_travel: EventLoopClockAdvancer
    ) -> None:
        """Test after_update_callback after context_timeout."""
        _timeout = 3
        xknx = XKNX()
        switch = BinarySensor(
            xknx,
            "TestInput",
            group_address_state="1/2/3",
            ignore_internal_state=True,
            context_timeout=_timeout,
        )
        after_update_callback = Mock()

        switch.register_device_updated_cb(after_update_callback)

        telegram = Telegram(
            destination_address=GroupAddress("1/2/3"),
            payload=GroupValueWrite(DPTBinary(1)),
        )
        assert switch.counter == 0

        switch.process(telegram)
        after_update_callback.assert_not_called()
        assert switch.counter == 1
        await time_travel(_timeout)
        after_update_callback.assert_called_with(switch)
        # once with counter 1 and once with counter 0
        assert after_update_callback.call_count == 2

        after_update_callback.reset_mock()
        # send same telegram again
        switch.process(telegram)
        assert switch.counter == 1
        await time_travel(_timeout / 2)  # not yet timed out
        switch.process(telegram)
        assert switch.counter == 2
        # incoming telegram resets timer (not sure if this is what we actually want)
        await time_travel(_timeout / 2)  # not yet timed out
        after_update_callback.assert_not_called()

        await time_travel(_timeout / 2)
        after_update_callback.assert_called_with(switch)
        # once with counter 2 and once with counter 0
        assert after_update_callback.call_count == 2
        assert switch.counter == 0

    async def test_process_callback_ignore_internal_state_no_counter(self) -> None:
        """Test after_update_callback after state of switch was changed."""
        xknx = XKNX()
        switch = BinarySensor(
            xknx,
            "TestInput",
            group_address_state="1/2/3",
            ignore_internal_state=True,
            context_timeout=0,
        )
        after_update_callback = Mock()

        switch.register_device_updated_cb(after_update_callback)

        telegram = Telegram(
            destination_address=GroupAddress("1/2/3"),
            payload=GroupValueWrite(DPTBinary(1)),
        )
        switch.process(telegram)
        # no _context_task started because context_timeout is False
        assert switch._context_task is None
        after_update_callback.assert_called_once_with(switch)

        after_update_callback.reset_mock()
        # send same telegram again
        switch.process(telegram)
        after_update_callback.assert_called_once_with(switch)

    async def test_process_group_value_response(self) -> None:
        """Test process of GroupValueResponse telegrams."""
        xknx = XKNX()
        switch = BinarySensor(
            xknx,
            "TestInput",
            group_address_state="1/2/3",
            ignore_internal_state=True,
        )
        after_update_callback = Mock()

        switch.register_device_updated_cb(after_update_callback)

        write_telegram = Telegram(
            destination_address=GroupAddress("1/2/3"),
            payload=GroupValueWrite(DPTBinary(1)),
        )
        response_telegram = Telegram(
            destination_address=GroupAddress("1/2/3"),
            payload=GroupValueResponse(
                DPTBinary(1),
            ),
        )
        assert switch.state is None
        # initial GroupValueResponse changes state and runs callback
        switch.process(response_telegram)
        assert switch.state
        after_update_callback.assert_called_once_with(switch)
        # GroupValueWrite with same payload runs callback because of `ignore_internal_state`
        after_update_callback.reset_mock()
        switch.process(write_telegram)
        assert switch.state
        after_update_callback.assert_called_once_with(switch)
        # GroupValueResponse should not run callback when state has not changed
        after_update_callback.reset_mock()
        switch.process(response_telegram)
        after_update_callback.assert_not_called()

    async def test_process_always_callback(self) -> None:
        """Test process of GroupValueResponse telegrams."""
        xknx = XKNX()
        switch = BinarySensor(
            xknx,
            "TestInput",
            group_address_state="1/2/3",
            always_callback=True,
        )
        after_update_callback = Mock()

        switch.register_device_updated_cb(after_update_callback)

        write_telegram = Telegram(
            destination_address=GroupAddress("1/2/3"),
            payload=GroupValueWrite(DPTBinary(1)),
        )
        response_telegram = Telegram(
            destination_address=GroupAddress("1/2/3"),
            payload=GroupValueResponse(
                DPTBinary(1),
            ),
        )
        assert switch.state is None
        # initial GroupValueResponse changes state and runs callback
        switch.process(response_telegram)
        assert switch.state
        after_update_callback.assert_called_once_with(switch)
        # GroupValueWrite with same payload runs callback because of `always_callback`
        after_update_callback.reset_mock()
        switch.process(write_telegram)
        assert switch.state
        after_update_callback.assert_called_once_with(switch)
        # GroupValueResponse should run callback because of `always_callback`
        after_update_callback.reset_mock()
        switch.process(response_telegram)
        after_update_callback.assert_called_once_with(switch)

    #
    # TEST COUNTER
    #
    def test_counter(self) -> None:
        """Test counter functionality."""
        xknx = XKNX()
        switch = BinarySensor(
            xknx, "TestInput", group_address_state="1/2/3", context_timeout=1
        )
        with patch("time.time") as mock_time:
            mock_time.return_value = 1517000000.0
            assert switch.bump_and_get_counter(True) == 1

            mock_time.return_value = 1517000000.1
            assert switch.bump_and_get_counter(True) == 2

            mock_time.return_value = 1517000000.2
            assert switch.bump_and_get_counter(False) == 1

            mock_time.return_value = 1517000000.3
            assert switch.bump_and_get_counter(True) == 3

            mock_time.return_value = 1517000000.4
            assert switch.bump_and_get_counter(False) == 2

            mock_time.return_value = 1517000002.0  # TIME OUT ...
            assert switch.bump_and_get_counter(True) == 1

            mock_time.return_value = 1517000004.1  # TIME OUT ...
            assert switch.bump_and_get_counter(False) == 1

    async def test_remove_tasks(self, xknx_no_interface: XKNX) -> None:
        """Test remove tasks."""
        xknx = xknx_no_interface
        switch = BinarySensor(
            xknx,
            "TestInput",
            group_address_state="1/2/3",
            context_timeout=1,
            reset_after=10,
        )
        xknx.devices.async_add(switch)
        async with xknx:
            write_telegram = Telegram(
                destination_address=GroupAddress("1/2/3"),
                payload=GroupValueWrite(DPTBinary(1)),
            )
            switch.process(write_telegram)
            assert switch._context_task
            assert switch._reset_task
            xknx.devices.async_remove(switch)
            assert switch._context_task is None
            assert switch._reset_task is None