/
remote_value_updown_test.py
118 lines (105 loc) · 4.34 KB
/
remote_value_updown_test.py
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
"""Unit test for RemoteValueUpDown objects."""
import asyncio
import unittest
from xknx import XKNX
from xknx.dpt import DPTArray, DPTBinary
from xknx.exceptions import ConversionError, CouldNotParseTelegram
from xknx.remote_value import RemoteValueUpDown
from xknx.telegram import GroupAddress, Telegram
class TestRemoteValueUpDown(unittest.TestCase):
"""Test class for RemoteValueUpDown objects."""
def setUp(self):
"""Set up test class."""
self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(self.loop)
def tearDown(self):
"""Tear down test class."""
self.loop.close()
def test_to_knx(self):
"""Test to_knx function with normal operation."""
xknx = XKNX()
remote_value = RemoteValueUpDown(xknx)
self.assertEqual(
remote_value.to_knx(RemoteValueUpDown.Direction.UP), DPTBinary(0)
)
self.assertEqual(
remote_value.to_knx(RemoteValueUpDown.Direction.DOWN), DPTBinary(1)
)
def test_from_knx(self):
"""Test from_knx function with normal operation."""
xknx = XKNX()
remote_value = RemoteValueUpDown(xknx)
self.assertEqual(
remote_value.from_knx(DPTBinary(0)), RemoteValueUpDown.Direction.UP
)
self.assertEqual(
remote_value.from_knx(DPTBinary(1)), RemoteValueUpDown.Direction.DOWN
)
def test_to_knx_invert(self):
"""Test to_knx function with normal operation."""
xknx = XKNX()
remote_value = RemoteValueUpDown(xknx, invert=True)
self.assertEqual(
remote_value.to_knx(RemoteValueUpDown.Direction.UP), DPTBinary(1)
)
self.assertEqual(
remote_value.to_knx(RemoteValueUpDown.Direction.DOWN), DPTBinary(0)
)
def test_from_knx_invert(self):
"""Test from_knx function with normal operation."""
xknx = XKNX()
remote_value = RemoteValueUpDown(xknx, invert=True)
self.assertEqual(
remote_value.from_knx(DPTBinary(0)), RemoteValueUpDown.Direction.DOWN
)
self.assertEqual(
remote_value.from_knx(DPTBinary(1)), RemoteValueUpDown.Direction.UP
)
def test_to_knx_error(self):
"""Test to_knx function with wrong parametern."""
xknx = XKNX()
remote_value = RemoteValueUpDown(xknx)
with self.assertRaises(ConversionError):
remote_value.to_knx(1)
def test_set(self):
"""Test setting value."""
xknx = XKNX()
remote_value = RemoteValueUpDown(xknx, group_address=GroupAddress("1/2/3"))
self.loop.run_until_complete(remote_value.down())
self.assertEqual(xknx.telegrams.qsize(), 1)
telegram = xknx.telegrams.get_nowait()
self.assertEqual(
telegram, Telegram(GroupAddress("1/2/3"), payload=DPTBinary(1))
)
self.loop.run_until_complete(remote_value.up())
self.assertEqual(xknx.telegrams.qsize(), 1)
telegram = xknx.telegrams.get_nowait()
self.assertEqual(
telegram, Telegram(GroupAddress("1/2/3"), payload=DPTBinary(0))
)
def test_process(self):
"""Test process telegram."""
xknx = XKNX()
remote_value = RemoteValueUpDown(xknx, group_address=GroupAddress("1/2/3"))
telegram = Telegram(
destination_address=GroupAddress("1/2/3"), payload=DPTBinary(1)
)
self.assertEqual(remote_value.value, None)
self.loop.run_until_complete(remote_value.process(telegram))
self.assertEqual(remote_value.value, RemoteValueUpDown.Direction.DOWN)
def test_to_process_error(self):
"""Test process errornous telegram."""
xknx = XKNX()
remote_value = RemoteValueUpDown(xknx, group_address=GroupAddress("1/2/3"))
with self.assertRaises(CouldNotParseTelegram):
telegram = Telegram(
destination_address=GroupAddress("1/2/3"), payload=DPTArray(0x01)
)
self.loop.run_until_complete(remote_value.process(telegram))
with self.assertRaises(CouldNotParseTelegram):
telegram = Telegram(
destination_address=GroupAddress("1/2/3"), payload=DPTBinary(3)
)
self.loop.run_until_complete(remote_value.process(telegram))
# pylint: disable=pointless-statement
remote_value.value