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 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
|
"""Test the RabbitAir client."""
import asyncio
import json
from typing import Any, Callable, Coroutine, Dict, Optional
from unittest.mock import Mock, patch
import pytest
from rabbitair import (
Error,
FilterType,
Gas,
Lights,
Mode,
Model,
Moodlight,
ProtocolError,
Quality,
Sensitivity,
Speed,
TimerMode,
UdpClient,
)
TEST_IP = "192.0.2.1"
TEST_TOKEN = "0123456789ABCDEF0123456789ABCDEF"
TEST_STATE_RESPONSE_A2 = """{
"id": 0,
"data": {
"model": 1,
"firmware": [3],
"power": true,
"mode": 0,
"speed": 1,
"quality": 4,
"sensitivity": 0,
"ionizer": false,
"moodlight": 0,
"sleep": false,
"filter_replacement": false,
"filter_life": 525600,
"light_sensor": true,
"particulate_sensor": 0,
"filter_timer": 525580,
"all_light_off": 2,
"error": 0,
"timer_mode": 0,
"timer": 1,
"schedule": "AAAAAAAAAAAAAAAAAAAAAAAA",
"rssi": -61,
"v": "2.3.17"
}
}"""
TEST_STATE_RESPONSE_A3 = """{
"id": 0,
"data": {
"model": 3,
"firmware": [1, 0, 0, 4],
"power": true,
"mode": 2,
"speed": 4,
"quality": 3,
"sensitivity": 2,
"ionizer": true,
"idle": 0,
"moodlight": 1,
"filter_cleaning": false,
"filter_replacement": false,
"filter_life": 525600,
"light_sensor": true,
"filter_timer": 525580,
"all_light_off": 1,
"error": 0,
"tag_state": 0,
"tag_uid": [0, 0, 0, 0, 0, 0, 0],
"filter_type": 0,
"pm_sensor": [19, 29, 31],
"color": [31, 0, 20, 0, 22, 40, 22, 30, 6],
"lsens_ctl": false,
"filter_ctl": false,
"buzzer": false,
"gas": 0,
"lock": false,
"open": false,
"timer_mode": 0,
"timer": 0,
"schedule": "AAAAAAAAAAAAAAAAAAAAAAAA",
"rssi": -52,
"v": "2.3.17"
}
}"""
TEST_TS_RESPONSE = """{
"id": 0,
"data": {
"v": 1,
"ts": 103431
}
}"""
TEST_INFO_RESPONSE_A2 = """{
"id": 0,
"data": {
"name": "1234567890_123456789012345678",
"mcu": "2.3.17",
"build": "Nov 29 2021 21:41:45",
"wifi": "v3.3.2",
"mac": "01:23:45:67:89:AB",
"time": "22:35:29",
"heap": 69304,
"hmin": 57004,
"stack": 1372,
"clk": 80000000,
"uptime": 314070,
"mup": 65174,
"wup": 306293,
"iup": 294213,
"cup": 297758,
"flc": 1,
"ftc": 0,
"errb": 8,
"errw": 1263,
"rssi": {
"cur": -68,
"min": -78,
"max": -58,
"avg": -66
}
}
}"""
TEST_INFO_RESPONSE_A3 = """{
"id": 0,
"data": {
"name": "1234567890_123456789012345678",
"mcu": "2.3.17",
"build": "Nov 29 2021 21:41:45",
"wifi": "v3.3.2",
"mac": "01:23:45:67:89:AB",
"time": "22:35:29",
"heap": 69304,
"hmin": 57004,
"stack": 1372,
"clk": 80000000,
"uptime": 314070,
"mup": 65174,
"wup": 306293,
"iup": 294213,
"cup": 297758,
"flc": 1,
"ftc": 0,
"errb": 8,
"errw": 1263,
"fv":"1.0.0.4",
"rssi": {
"cur": -68,
"min": -78,
"max": -58,
"avg": -66
}
}
}"""
def mock_command(
model: Optional[Model],
) -> Callable[[Any, Dict[str, Any]], Coroutine[Any, Any, Dict[str, Any]]]:
"""Mock command."""
if model is Model.MinusA2:
state_response = TEST_STATE_RESPONSE_A2
info_response = TEST_INFO_RESPONSE_A2
elif model is Model.A3:
state_response = TEST_STATE_RESPONSE_A3
info_response = TEST_INFO_RESPONSE_A3
else:
state_response = "{}"
info_response = "{}"
async def command(self: Any, request: Dict[str, Any]) -> Dict[str, Any]:
if request["cmd"] == 4:
response = state_response
elif request["cmd"] == 9:
response = TEST_TS_RESPONSE
elif request["cmd"] == 255:
response = info_response
else:
assert False
result: Dict[str, Any] = json.loads(response)
result["id"] = request["id"]
return result
return command
@pytest.mark.parametrize("token", [TEST_TOKEN.lower(), TEST_TOKEN.upper(), "", None])
def test_create(token: str) -> None:
"""Instance creation test."""
UdpClient(TEST_IP, token)
@pytest.mark.parametrize(
"token", [TEST_TOKEN[:30], TEST_TOKEN + TEST_TOKEN, TEST_TOKEN.replace("1", "x")]
)
def test_create_fail(token: str) -> None:
"""Test cases where instance creation fails."""
with pytest.raises(ValueError):
UdpClient(TEST_IP, token)
async def test_zeroconf() -> None:
"""Test mDNS resolver."""
info = Mock()
info.parsed_addresses.return_value = [TEST_IP]
async def async_get_service_info(type: str, name: str) -> Mock:
return info
zc = Mock()
zc.async_get_service_info = async_get_service_info
with patch(
"rabbitair.Client._command", new_callable=mock_command, model=Model.MinusA2
):
with UdpClient("test.local", TEST_TOKEN, zeroconf=zc) as client:
await client.get_state()
assert len(info.parsed_addresses.mock_calls) == 1
async def test_state_a2() -> None:
"""Test state response for MinusA2."""
with patch(
"rabbitair.Client._command", new_callable=mock_command, model=Model.MinusA2
):
with UdpClient(TEST_IP, TEST_TOKEN) as client:
state = await client.get_state()
assert state.model is Model.MinusA2
assert state.main_firmware == "3"
assert state.power is True
assert state.mode is Mode.Auto
assert state.speed is Speed.Silent
assert state.quality is Quality.Highest
assert state.sensitivity is Sensitivity.High
assert state.ionizer is False
assert state.idle is None
assert state.moodlight is Moodlight.Off
assert state.sleep is False
assert state.filter_cleaning is None
assert state.filter_replacement is False
assert state.filter_life == 525600
assert state.light_sensor is True
assert state.particulate_sensor == 0
assert state.filter_timer == 525580
assert state.lights is Lights.Auto
assert state.error is Error.NoError
assert state.tag_state is None
assert state.tag_uid is None
assert state.filter_type is None
assert state.pm_sensor is None
assert state.color is None
assert state.light_sensor_ctl is None
assert state.filter_ctl is None
assert state.buzzer is None
assert state.gas is None
assert state.child_lock is None
assert state.open is None
assert state.timer_mode is TimerMode.Off
assert state.timer == 1
assert state.schedule == "AAAAAAAAAAAAAAAAAAAAAAAA"
assert state.rssi == -61
assert state.wifi_firmware == "2.3.17"
async def test_state_a3() -> None:
"""Test state response for A3."""
with patch("rabbitair.Client._command", new_callable=mock_command, model=Model.A3):
with UdpClient(TEST_IP, TEST_TOKEN) as client:
state = await client.get_state()
assert state.model is Model.A3
assert state.main_firmware == "1.0.0.4"
assert state.power is True
assert state.mode is Mode.Manual
assert state.speed is Speed.High
assert state.quality is Quality.High
assert state.sensitivity is Sensitivity.Low
assert state.ionizer is True
assert state.idle is False
assert state.moodlight is Moodlight.On
assert state.sleep is None
assert state.filter_cleaning is False
assert state.filter_replacement is False
assert state.filter_life == 525600
assert state.light_sensor is True
assert state.particulate_sensor is None
assert state.filter_timer == 525580
assert state.lights is Lights.On
assert state.error is Error.NoError
assert state.tag_state is False
assert state.tag_uid == [0, 0, 0, 0, 0, 0, 0]
assert state.filter_type is FilterType.Unknown
assert state.pm_sensor == [19, 29, 31]
assert state.color == [31, 0, 20, 0, 22, 40, 22, 30, 6]
assert state.light_sensor_ctl is False
assert state.filter_ctl is False
assert state.buzzer is False
assert state.gas is Gas.Preheat
assert state.child_lock is False
assert state.open is False
assert state.timer_mode is TimerMode.Off
assert state.timer == 0
assert state.schedule == "AAAAAAAAAAAAAAAAAAAAAAAA"
assert state.rssi == -52
assert state.wifi_firmware == "2.3.17"
@pytest.mark.parametrize("model,fv", [(Model.MinusA2, None), (Model.A3, "1.0.0.4")])
async def test_info(model: Model, fv: str) -> None:
"""Test info response."""
with patch("rabbitair.Client._command", new_callable=mock_command, model=model):
with UdpClient(TEST_IP, TEST_TOKEN) as client:
info = await client.get_info()
assert info.name == "1234567890_123456789012345678"
assert info.wifi_firmware == "2.3.17"
assert info.build == "Nov 29 2021 21:41:45"
assert info.mac == "01:23:45:67:89:AB"
assert info.time == "22:35:29"
assert info.uptime == 314070
assert info.motor_uptime == 65174
assert info.wifi_uptime == 306293
assert info.internet_uptime == 294213
assert info.cloud_uptime == 297758
assert info.main_firmware == fv
assert info.rssi is not None
assert info.rssi.current == -68
assert info.rssi.min == -78
assert info.rssi.max == -58
assert info.rssi.average == -66
async def test_no_response() -> None:
"""Test no response."""
with patch("rabbitair.Client._command", side_effect=asyncio.TimeoutError):
with UdpClient(TEST_IP, TEST_TOKEN) as client:
with pytest.raises(asyncio.TimeoutError):
await client.get_state()
async def test_protocol_error() -> None:
"""Test protocol error response."""
with patch("rabbitair.Client._command", side_effect=ProtocolError):
with UdpClient(TEST_IP, TEST_TOKEN) as client:
with pytest.raises(ProtocolError):
await client.get_state()
async def test_sequential_requests() -> None:
"""Test sequential requests."""
with patch(
"rabbitair.Client._command", new_callable=mock_command, model=Model.MinusA2
):
with UdpClient(TEST_IP, TEST_TOKEN) as client:
await client.get_state()
await client.get_info()
async def test_set_state() -> None:
"""Test set state."""
with patch("rabbitair.Client._command", new_callable=mock_command, model=Model.A3):
with UdpClient(TEST_IP, TEST_TOKEN) as client:
await client.set_state(
power=True,
mode=Mode.Manual,
speed=Speed.Medium,
sensitivity=Sensitivity.Medium,
ionizer=True,
moodlight=Moodlight.On,
filter_cleaning=False,
filter_replacement=False,
filter_life=525600,
filter_timer=0,
lights=Lights.Off,
color=[31, 0, 20, 0, 22, 40, 22, 30, 6],
light_sensor_ctl=True,
filter_ctl=True,
buzzer=True,
child_lock=False,
timer_mode=TimerMode.Schedule,
timer=60,
schedule="A012345A012345A012345A01",
)
with pytest.raises(ValueError):
await client.set_state(filter_life=-1)
with pytest.raises(ValueError):
await client.set_state(filter_life=525601)
with pytest.raises(ValueError):
await client.set_state(filter_timer=-1)
with pytest.raises(ValueError):
await client.set_state(filter_timer=525601)
with pytest.raises(ValueError):
await client.set_state(color=[])
with pytest.raises(ValueError):
await client.set_state(color=[41, -1, 20, 0, 22, 40, 22, 30, 6])
with pytest.raises(ValueError):
await client.set_state(timer=-1)
with pytest.raises(ValueError):
await client.set_state(timer=1441)
with pytest.raises(ValueError):
await client.set_state(schedule="A12")
with pytest.raises(ValueError):
await client.set_state(schedule="ABC6789AAAAAAAAAAAAAAAAA")
|