|
1 | 1 | import binascii
|
| 2 | +from unittest.mock import AsyncMock |
2 | 3 |
|
3 |
| -import asynctest |
4 |
| -from asynctest import CoroutineMock |
| 4 | +import pytest |
5 | 5 | from websockets.exceptions import InvalidMessage
|
6 | 6 |
|
7 | 7 | from vallox_websocket_api.client import Client
|
|
10 | 10 | ValloxWebsocketException,
|
11 | 11 | )
|
12 | 12 |
|
13 |
| -from tests.decorators import with_client |
14 | 13 |
|
| 14 | +async def test_fetch_metric(client: Client, ws): |
| 15 | + ws.recv.return_value = binascii.unhexlify( |
| 16 | + "0024000000000000000000000000000001000800030000000000000061df98b100030003203fb9500331000000000000000000560000000000000000000000000000000000000000001b00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002b000f732a6ca969a171d1730800010000022700000028000000000000000001a6029e000100000028ffffffffffffffffffffffffffffffffffffffffffffffff000000000000000057c503e8ffffffffffff000000190000000000010000000000000000000300001b98012000a50000000000000000001e00010000000100000000000000000007001b000f001700010012000200070044000000010000000000000007003200320001000000000000001e0000c0a80501ffffff0000000000000000000000000000000000000000000000000000000000c0a8050c86076097f78844b7ac4db61e502fe4f2004c004c000100c00101001c001e000a00320000003703840000708f00320032000a0000000000010000000a721f0000000000010000000f728300000000000000000064715700000000000000000000000000000000000000010037001e000000000000000068bf71bb000083910000002600b4000000010001000000010001001e000f00080001001200000003000000000000000000000017000003e90000000000000001000100010000000a003200010000000000000000000000000000000000000000001000000000000000000000000000540048000000000000000000000000000000c8000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a90000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" |
| 17 | + ) |
15 | 18 |
|
16 |
| -class TestClient(asynctest.TestCase): |
17 |
| - @with_client |
18 |
| - async def testFetchMetric(self, client: Client, ws): |
19 |
| - ws.recv.return_value = binascii.unhexlify( |
20 |
| - "0024000000000000000000000000000001000800030000000000000061df98b100030003203fb9500331000000000000000000560000000000000000000000000000000000000000001b00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002b000f732a6ca969a171d1730800010000022700000028000000000000000001a6029e000100000028ffffffffffffffffffffffffffffffffffffffffffffffff000000000000000057c503e8ffffffffffff000000190000000000010000000000000000000300001b98012000a50000000000000000001e00010000000100000000000000000007001b000f001700010012000200070044000000010000000000000007003200320001000000000000001e0000c0a80501ffffff0000000000000000000000000000000000000000000000000000000000c0a8050c86076097f78844b7ac4db61e502fe4f2004c004c000100c00101001c001e000a00320000003703840000708f00320032000a0000000000010000000a721f0000000000010000000f728300000000000000000064715700000000000000000000000000000000000000010037001e000000000000000068bf71bb000083910000002600b4000000010001000000010001001e000f00080001001200000003000000000000000000000017000003e90000000000000001000100010000000a003200010000000000000000000000000000000000000000001000000000000000000000000000540048000000000000000000000000000000c8000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a9000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" |
21 |
| - ) |
| 19 | + result = await client.fetch_metric("A_CYC_ENABLED") |
22 | 20 |
|
23 |
| - result = await client.fetch_metric("A_CYC_ENABLED") |
| 21 | + assert result == 1 |
| 22 | + ws.send.assert_called_once_with(binascii.unhexlify("0300f6000000f900")) |
24 | 23 |
|
25 |
| - self.assertEqual(1, result) |
26 | 24 |
|
27 |
| - ws.send.assert_called_once_with(binascii.unhexlify("0300f6000000f900")) |
| 25 | +async def test_set_temp_value(client: Client, ws): |
| 26 | + await client.set_values({"A_CYC_BOOST_AIR_TEMP_TARGET": "19"}) |
28 | 27 |
|
29 |
| - @with_client |
30 |
| - async def testSetTempValue(self, client: Client, ws): |
31 |
| - ws.recv.return_value = binascii.unhexlify("0200f500f700") |
| 28 | + ws.send.assert_called_once_with(binascii.unhexlify("0400f90022501f723ec3")) |
32 | 29 |
|
33 |
| - await client.set_values({"A_CYC_BOOST_AIR_TEMP_TARGET": "19"}) |
34 | 30 |
|
35 |
| - ws.send.assert_called_once_with(binascii.unhexlify("0400f90022501f723ec3")) |
| 31 | +async def test_set_temp_value_fraction(client: Client, ws): |
| 32 | + await client.set_values({"A_CYC_BOOST_AIR_TEMP_TARGET": "19.1"}) |
36 | 33 |
|
37 |
| - @with_client |
38 |
| - async def testSetTempValueFraction(self, client: Client, ws): |
39 |
| - ws.recv.return_value = binascii.unhexlify("0200f500f700") |
| 34 | + ws.send.assert_called_once_with(binascii.unhexlify("0400f9002250297248c3")) |
40 | 35 |
|
41 |
| - await client.set_values({"A_CYC_BOOST_AIR_TEMP_TARGET": "19.1"}) |
42 | 36 |
|
43 |
| - ws.send.assert_called_once_with(binascii.unhexlify("0400f9002250297248c3")) |
| 37 | +async def test_set_temp_value_fraction_rounding1(client: Client, ws): |
| 38 | + await client.set_values({"A_CYC_BOOST_AIR_TEMP_TARGET": "19.145"}) |
44 | 39 |
|
45 |
| - @with_client |
46 |
| - async def testSetTempValueFractionRounding1(self, client: Client, ws): |
47 |
| - ws.recv.return_value = binascii.unhexlify("0200f500f700") |
| 40 | + ws.send.assert_called_once_with(binascii.unhexlify("0400f9002250297248c3")) |
48 | 41 |
|
49 |
| - await client.set_values({"A_CYC_BOOST_AIR_TEMP_TARGET": "19.145"}) |
50 | 42 |
|
51 |
| - ws.send.assert_called_once_with(binascii.unhexlify("0400f9002250297248c3")) |
| 43 | +async def test_set_temp_value_fraction_rounding2(client: Client, ws): |
| 44 | + await client.set_values({"A_CYC_BOOST_AIR_TEMP_TARGET": "18.991"}) |
52 | 45 |
|
53 |
| - @with_client |
54 |
| - async def testSetTempValueFractionRounding2(self, client: Client, ws): |
55 |
| - ws.recv.return_value = binascii.unhexlify("0200f500f700") |
| 46 | + ws.send.assert_called_once_with(binascii.unhexlify("0400f90022501f723ec3")) |
56 | 47 |
|
57 |
| - await client.set_values({"A_CYC_BOOST_AIR_TEMP_TARGET": "18.991"}) |
58 | 48 |
|
59 |
| - ws.send.assert_called_once_with(binascii.unhexlify("0400f90022501f723ec3")) |
| 49 | +async def test_set_value(client: Client, ws): |
| 50 | + await client.set_values( |
| 51 | + { |
| 52 | + "A_CYC_STATE": 0, |
| 53 | + "A_CYC_BOOST_TIMER": 0, |
| 54 | + "A_CYC_FIREPLACE_TIMER": 0, |
| 55 | + "A_CYC_EXTRA_TIMER": 0, |
| 56 | + } |
| 57 | + ) |
60 | 58 |
|
61 |
| - @with_client |
62 |
| - async def testSetValue(self, client: Client, ws): |
63 |
| - ws.recv.return_value = binascii.unhexlify("0200f500f700") |
| 59 | + ws.send.assert_called_once_with( |
| 60 | + binascii.unhexlify("0a00f900011200000412000005120000061200001349") |
| 61 | + ) |
64 | 62 |
|
65 |
| - await client.set_values( |
66 |
| - { |
67 |
| - "A_CYC_STATE": 0, |
68 |
| - "A_CYC_BOOST_TIMER": 0, |
69 |
| - "A_CYC_FIREPLACE_TIMER": 0, |
70 |
| - "A_CYC_EXTRA_TIMER": 0, |
71 |
| - } |
72 |
| - ) |
73 | 63 |
|
74 |
| - ws.send.assert_called_once_with( |
75 |
| - binascii.unhexlify("0a00f900011200000412000005120000061200001349") |
76 |
| - ) |
| 64 | +async def test_set_assertion(client: Client, ws): |
| 65 | + with pytest.raises(ValloxInvalidInputException): |
| 66 | + await client.set_values({"A_CYC_BOOST_TIMER": "11.2"}) |
77 | 67 |
|
78 |
| - @with_client |
79 |
| - async def testSetAssertion(self, client: Client, ws): |
80 |
| - ws.recv.return_value = binascii.unhexlify("0200f500f700") |
| 68 | + with pytest.raises(ValloxInvalidInputException): |
| 69 | + await client.set_values({"A_CYC_BOOST_AIR_TEMP_TARGET": "11.a"}) |
81 | 70 |
|
82 |
| - with self.assertRaises(ValloxInvalidInputException): |
83 |
| - await client.set_values({"A_CYC_BOOST_TIMER": "11.2"}) |
| 71 | + with pytest.raises(ValloxInvalidInputException): |
| 72 | + await client.set_values({"A_CYC_BOOST_SPEED_SETTING": "11.2"}) |
84 | 73 |
|
85 |
| - with self.assertRaises(ValloxInvalidInputException): |
86 |
| - await client.set_values({"A_CYC_BOOST_AIR_TEMP_TARGET": "11.a"}) |
| 74 | + with pytest.raises(ValloxInvalidInputException): |
| 75 | + await client.set_values({"A_CYC_FIREPLACE_SUPP_FAN": "11.2"}) |
87 | 76 |
|
88 |
| - with self.assertRaises(ValloxInvalidInputException): |
89 |
| - await client.set_values({"A_CYC_BOOST_SPEED_SETTING": "11.2"}) |
| 77 | + ws.send.assert_not_called() |
90 | 78 |
|
91 |
| - with self.assertRaises(ValloxInvalidInputException): |
92 |
| - await client.set_values({"A_CYC_FIREPLACE_SUPP_FAN": "11.2"}) |
93 | 79 |
|
94 |
| - @with_client |
95 |
| - async def testSetMissing(self, client: Client, ws): |
96 |
| - ws.recv.return_value = binascii.unhexlify("0200f500f700") |
| 80 | +async def test_set_missing(client: Client, ws): |
| 81 | + with pytest.raises(ValloxInvalidInputException): |
| 82 | + await client.set_values({"A_CYC_BOOSTER": 10}) |
97 | 83 |
|
98 |
| - with self.assertRaises(ValloxInvalidInputException): |
99 |
| - await client.set_values({"A_CYC_BOOSTER": 10}) |
| 84 | + ws.send.assert_not_called() |
100 | 85 |
|
101 |
| - @with_client |
102 |
| - async def testSetUnsettable(self, client: Client, ws): |
103 |
| - ws.recv.return_value = binascii.unhexlify("0200f500f700") |
104 | 86 |
|
105 |
| - with self.assertRaises(ValloxInvalidInputException): |
106 |
| - await client.set_values({"A_CYC_RH_VALUE": 22}) |
| 87 | +async def test_set_unsettable(client: Client, ws): |
| 88 | + with pytest.raises(ValloxInvalidInputException): |
| 89 | + await client.set_values({"A_CYC_RH_VALUE": 22}) |
107 | 90 |
|
108 |
| - @with_client |
109 |
| - async def testSetNewSettableAddressByName(self, client: Client, ws): |
110 |
| - ws.recv.return_value = binascii.unhexlify("0200f500f700") |
| 91 | + ws.send.assert_not_called() |
111 | 92 |
|
112 |
| - client.set_settable_address("A_CYC_RH_VALUE", int) |
113 | 93 |
|
114 |
| - await client.set_values({"A_CYC_RH_VALUE": 22}) |
| 94 | +async def test_set_new_settable_address_by_name(client: Client, ws): |
| 95 | + client.set_settable_address("A_CYC_RH_VALUE", int) |
115 | 96 |
|
116 |
| - @with_client |
117 |
| - async def testSetNewSettableAddressByAddress(self, client: Client, ws): |
118 |
| - ws.recv.return_value = binascii.unhexlify("0200f500f700") |
| 97 | + await client.set_values({"A_CYC_RH_VALUE": 22}) |
119 | 98 |
|
120 |
| - client.set_settable_address(4363, int) |
121 | 99 |
|
122 |
| - await client.set_values({"A_CYC_RH_VALUE": 22}) |
| 100 | +async def test_set_new_settable_address_by_address(client: Client, ws): |
| 101 | + client.set_settable_address(4363, int) |
123 | 102 |
|
124 |
| - @with_client |
125 |
| - async def testSetNewSettableAddressByAddressException(self, client: Client, ws): |
126 |
| - ws.recv.side_effect = CoroutineMock(side_effect=InvalidMessage()) |
| 103 | + await client.set_values({"A_CYC_RH_VALUE": 22}) |
127 | 104 |
|
128 |
| - client.set_settable_address(4363, int) |
129 | 105 |
|
130 |
| - await self.assertAsyncRaisesRegex( |
131 |
| - ValloxWebsocketException, |
132 |
| - "Websocket handshake failed", |
133 |
| - client.set_values({"A_CYC_RH_VALUE": 22}), |
134 |
| - ) |
| 106 | +async def test_set_new_settable_address_by_address_exception(client: Client, ws): |
| 107 | + ws.recv.side_effect = AsyncMock(side_effect=InvalidMessage()) |
| 108 | + |
| 109 | + client.set_settable_address(4363, int) |
| 110 | + |
| 111 | + with pytest.raises(ValloxWebsocketException): |
| 112 | + await client.set_values({"A_CYC_RH_VALUE": 22}) |
0 commit comments