File: test_client.py

package info (click to toggle)
pybalboa 1.1.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 296 kB
  • sloc: python: 1,783; makefile: 4
file content (320 lines) | stat: -rw-r--r-- 11,289 bytes parent folder | download | duplicates (2)
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
"""Tests module."""

from __future__ import annotations

from datetime import time, timedelta
from unittest.mock import patch

import pytest

from pybalboa import SpaClient
from pybalboa.enums import (
    HeatMode,
    LowHighRange,
    MessageType,
    OffLowHighState,
    OffOnState,
    SettingsCode,
    TemperatureUnit,
)

from .conftest import SpaServer

HOST = "localhost"


@pytest.mark.asyncio
async def test_bfbp20s(bfbp20s: SpaServer) -> None:
    """Test the spa client."""
    async with SpaClient(HOST, bfbp20s.port) as spa:
        assert spa.connected
        assert await spa.async_configuration_loaded()
        assert spa.configuration_loaded
        assert spa.model == "BFBP20S"
        assert spa.mac_address == "00:15:27:71:f1:9a"
        assert spa.software_version == "M100_220 V36.0"
        assert spa.pump_count == 1

        control = spa.pumps[0]
        assert control.name == "Pump 1"
        assert isinstance(control.state, OffLowHighState)
        assert control.state == OffLowHighState.OFF
        assert control.options == list(OffLowHighState)

        control = spa.lights[0]
        assert control.name == "Light 1"
        assert isinstance(control.state, OffOnState)
        assert control.state == OffOnState.ON
        assert control.options == list(OffOnState)
        await control.set_state(OffOnState.OFF)
        assert bfbp20s.received_messages[-1]

        control = spa.lights[1]
        assert control.name == "Light 2"
        assert isinstance(control.state, OffOnState)
        assert control.state == OffOnState.OFF
        assert control.options == list(OffOnState)

        assert spa.circulation_pump
        control = spa.circulation_pump
        assert control.name == "Circulation pump"
        assert isinstance(control.state, OffOnState)
        assert control.state == OffOnState.ON
        assert control.options == list(OffOnState)

        control = spa.temperature_range
        assert control.name == "Temperature range"
        assert isinstance(control.state, LowHighRange)
        assert control.state == LowHighRange.HIGH
        assert control.options == list(LowHighRange)

        control = spa.heat_mode
        assert control.name == "Heat mode"
        assert isinstance(control.state, HeatMode)
        assert control.state == HeatMode.READY
        assert control.options == list(HeatMode)[:2]

        with patch("pybalboa.client.SpaClient.send_message") as send_message:
            await spa.configure_filter_cycle(
                1, start=time(1, 30), duration=timedelta(hours=3, minutes=15)
            )
            # validate a configure filter cycle message was awaited
            expected_message = [MessageType.FILTER_CYCLE, 1, 30, 3, 15, 135, 0, 1, 5]
            send_message.assert_any_await(*expected_message)
            # validate a request filter cycle message was awaited
            send_message.assert_any_await(
                MessageType.REQUEST, SettingsCode.FILTER_CYCLE, 0x00, 0x00
            )

            send_message.reset_mock()
            await spa.configure_filter_cycle(
                2,
                start=time(13, 15),
                duration=timedelta(hours=3, minutes=45),
                enabled=False,
            )
            # validate a configure filter cycle message was awaited
            expected_message = [MessageType.FILTER_CYCLE, 19, 0, 2, 0, 13, 15, 3, 45]
            send_message.assert_any_await(*expected_message)


@pytest.mark.asyncio
async def test_lpi501st(lpi501st: SpaServer) -> None:
    """Test the spa client."""
    async with SpaClient(HOST, lpi501st.port) as spa:
        assert spa.connected
        assert await spa.async_configuration_loaded()
        assert spa.configuration_loaded
        assert spa.model == "LPI501ST"
        assert spa.mac_address == "00:15:27:73:d1:47"
        assert spa.software_version == "M100_201 V36.0"
        assert spa.pump_count == 2

        control = spa.pumps[0]
        assert control.name == "Pump 1"
        assert isinstance(control.state, OffLowHighState)
        assert control.state == OffLowHighState.OFF
        assert control.options == list(OffLowHighState)

        control = spa.pumps[1]
        assert control.name == "Pump 2"
        assert isinstance(control.state, OffOnState)
        assert control.state == OffOnState.OFF
        assert control.options == list(OffOnState)


@pytest.mark.asyncio
async def test_mxbp20(mxbp20: SpaServer) -> None:
    """Test the spa client."""
    async with SpaClient(HOST, mxbp20.port) as spa:
        assert spa.connected
        assert await spa.async_configuration_loaded()
        assert spa.configuration_loaded
        assert spa.model == "MXBP20"
        assert spa.pump_count == 2

        control = spa.pumps[0]
        assert control.name == "Pump 1"
        assert isinstance(control.state, OffLowHighState)
        assert control.state == OffLowHighState.OFF
        assert control.options == list(OffLowHighState)

        control = spa.pumps[1]
        assert control.name == "Pump 2"
        assert isinstance(control.state, OffLowHighState)
        assert control.state == OffLowHighState.OFF
        assert control.options == list(OffLowHighState)


@pytest.mark.asyncio
async def test_bp501g1(bp501g1: SpaServer) -> None:
    """Test the spa client."""
    async with SpaClient(HOST, bp501g1.port) as spa:
        assert spa.connected
        assert await spa.async_configuration_loaded()
        assert spa.configuration_loaded
        assert spa.model == "BP501G1"
        assert spa.mac_address == "00:15:27:73:5b:e2"
        assert spa.software_version == "M100_201 V20.0"
        assert spa.pump_count == 2

        assert len(spa.aux) == 0
        assert len(spa.blowers) == 0
        assert len(spa.lights) == 1
        assert len(spa.pumps) == 2

        assert spa.circulation_pump is None

        control = spa.pumps[0]
        assert control.name == "Pump 1"
        assert isinstance(control.state, OffLowHighState)
        assert control.state == OffLowHighState.LOW
        assert control.options == list(OffLowHighState)

        control = spa.pumps[1]
        assert control.name == "Pump 2"
        assert isinstance(control.state, OffOnState)
        assert control.state == OffOnState.ON
        assert control.options == list(OffOnState)

        control = spa.lights[0]
        assert control.name == "Light 1"
        assert isinstance(control.state, OffOnState)
        assert control.state == OffOnState.OFF
        assert control.options == list(OffOnState)
        await control.set_state(OffOnState.ON)
        assert bp501g1.received_messages[-1]

        control = spa.temperature_range
        assert control.name == "Temperature range"
        assert isinstance(control.state, LowHighRange)
        assert control.state == LowHighRange.HIGH
        assert control.options == list(LowHighRange)

        control = spa.heat_mode
        assert control.name == "Heat mode"
        assert isinstance(control.state, HeatMode)
        assert control.state == HeatMode.READY
        assert control.options == list(HeatMode)[:2]


@pytest.mark.asyncio
async def test_bp6013g1(bp6013g1: SpaServer) -> None:
    """Test the spa client."""
    async with SpaClient(HOST, bp6013g1.port) as spa:
        assert spa.connected
        assert await spa.async_configuration_loaded()
        assert spa.configuration_loaded
        assert spa.model == "BP6013G1"
        assert spa.mac_address == "00:15:27:e4:00:9d"
        assert spa.software_version == "M100_226 V43.0"
        assert spa.pump_count == 1

        assert len(spa.aux) == 0
        assert len(spa.blowers) == 1
        assert len(spa.lights) == 1
        assert len(spa.pumps) == 1

        assert spa.temperature_unit == TemperatureUnit.CELSIUS

        assert spa.circulation_pump

        control = spa.pumps[0]
        assert control.name == "Pump 1"
        assert isinstance(control.state, OffOnState)
        assert control.state == OffOnState.OFF
        assert control.options == list(OffOnState)

        control = spa.lights[0]
        assert control.name == "Light 1"
        assert isinstance(control.state, OffOnState)
        assert control.state == OffOnState.ON
        assert control.options == list(OffOnState)
        await control.set_state(OffOnState.OFF)
        assert bp6013g1.received_messages[-1]

        control = spa.blowers[0]
        assert control.name == "Blower 1"
        assert isinstance(control.state, OffOnState)
        assert control.state == OffOnState.OFF
        assert control.options == list(OffOnState)
        await control.set_state(OffOnState.ON)
        assert bp6013g1.received_messages[-1]

        control = spa.temperature_range
        assert control.name == "Temperature range"
        assert isinstance(control.state, LowHighRange)
        assert control.state == LowHighRange.HIGH
        assert control.options == list(LowHighRange)

        control = spa.heat_mode
        assert control.name == "Heat mode"
        assert isinstance(control.state, HeatMode)
        assert control.state == HeatMode.READY
        assert control.options == list(HeatMode)[:2]


@pytest.mark.asyncio
@pytest.mark.parametrize(
    ("error", "error_message", "method", "params"),
    [
        (
            ValueError,
            "Invalid filter cycle",
            "configure_filter_cycle",
            {"filter_cycle": None},
        ),
        (
            ValueError,
            "Invalid filter cycle",
            "configure_filter_cycle",
            {"filter_cycle": 3},
        ),
        (ValueError, "At least one of", "configure_filter_cycle", {"filter_cycle": 1}),
        (
            ValueError,
            "Only one of",
            "configure_filter_cycle",
            {"filter_cycle": 1, "end": time(), "duration": timedelta(hours=1)},
        ),
        (
            ValueError,
            "Filter cycle 1 cannot be disabled",
            "configure_filter_cycle",
            {"filter_cycle": 1, "start": time(), "enabled": False},
        ),
        (
            ValueError,
            "Filter cycle 1 requires",
            "configure_filter_cycle",
            {"filter_cycle": 1, "enabled": True},
        ),
        (
            ValueError,
            "Invalid duration",
            "configure_filter_cycle",
            {"filter_cycle": 1, "duration": timedelta()},
        ),
        (
            ValueError,
            "Invalid duration",
            "configure_filter_cycle",
            {"filter_cycle": 1, "duration": timedelta(minutes=5)},
        ),
        (ValueError, "Invalid fault log entry", "request_fault_log", {"entry": -1}),
        (ValueError, "Invalid fault log entry", "request_fault_log", {"entry": 25}),
        (ValueError, "Invalid temperature", "set_temperature", {"temperature": 0}),
        (ValueError, "Invalid time", "set_time", {"hour": 45, "minute": 0}),
    ],
)
async def test_client_errors(
    bfbp20s: SpaServer,
    error: Exception,
    error_message: str,
    method: str,
    params: dict | None,
) -> None:
    """Test the spa client."""
    async with SpaClient(HOST, bfbp20s.port) as spa:
        with pytest.raises(error, match=error_message):
            await getattr(spa, method)(**(params or {}))