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 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
|
"""Tests for Async IO Modern Forms Library."""
import json
from datetime import datetime, timedelta
from unittest.mock import patch
import pytest
import aiomodernforms
from aiomodernforms.const import (
ADAPTIVE_LEARNING_ON,
AWAY_MODE_ON,
FAN_SPEED_HIGH_VALUE,
FAN_SPEED_LOW_VALUE,
LIGHT_BRIGHTNESS_HIGH_VALUE,
LIGHT_BRIGHTNESS_LOW_VALUE,
STATE_ADAPTIVE_LEARNING,
STATE_AWAY_MODE,
STATE_FAN_DIRECTION,
STATE_FAN_POWER,
STATE_FAN_SLEEP_TIMER,
STATE_FAN_SPEED,
STATE_LIGHT_BRIGHTNESS,
STATE_LIGHT_POWER,
STATE_LIGHT_SLEEP_TIMER,
)
from aiomodernforms.exceptions import (
ModernFormsConnectionError,
ModernFormsConnectionTimeoutError,
ModernFormsNotInitializedError,
)
basic_response = {
"adaptiveLearning": False,
"awayModeEnabled": False,
"clientId": "MF_000000000000",
"decommission": False,
"factoryReset": False,
"fanDirection": "forward",
"fanOn": False,
"fanSleepTimer": 0,
"fanSpeed": 3,
"lightBrightness": 50,
"lightOn": False,
"lightSleepTimer": 0,
"resetRfPairList": False,
"rfPairModeActive": False,
"schedule": "",
}
basic_info = {
"clientId": "MF_000000000000",
"mac": "CC:CC:CC:CC:CC:CC",
"lightType": "F6IN-120V-R1-30",
"fanType": "1818-56",
"fanMotorType": "DC125X25",
"productionLotNumber": "",
"productSku": "",
"owner": "someone@somewhere.com",
"federatedIdentity": "us-east-1:f3da237b-c19c-4f61-b387-0e6dde2e470b",
"deviceName": "Fan",
"firmwareVersion": "01.03.0025",
"mainMcuFirmwareVersion": "01.03.3008",
"firmwareUrl": "",
}
@pytest.mark.asyncio
async def test_basic_status(aresponses):
"""Test JSON response is handled correctly."""
aresponses.add("fan.local", "/mf", "POST", response=basic_info)
aresponses.add(
"fan.local",
"/mf",
"POST",
response=basic_response,
repeat=2,
)
async with aiomodernforms.ModernFormsDevice("fan.local") as device:
await device.update()
response = await device.request(
commands={aiomodernforms.COMMAND_LIGHT_POWER: False}
)
assert response.fan_on == basic_response["fanOn"]
assert device.status.fan_direction == "forward"
assert device.info.fan_type == "1818-56"
@pytest.mark.asyncio
async def test_command(aresponses):
"""Test to make sure setting lights works."""
aresponses.add("fan.local", "/mf", "POST", response=basic_info)
aresponses.add("fan.local", "/mf", "POST", response=basic_response)
async def evaluate_request(request):
data = await request.json()
assert aiomodernforms.COMMAND_LIGHT_POWER in data
modified_response = basic_response.copy()
modified_response[STATE_LIGHT_POWER] = data[aiomodernforms.COMMAND_LIGHT_POWER]
return aresponses.Response(
status=200,
content_type="application/json",
text=json.dumps(modified_response),
)
aresponses.add("fan.local", "/mf", "POST", response=evaluate_request)
async with aiomodernforms.ModernFormsDevice("fan.local") as device:
await device.update()
response = await device.request(
commands={aiomodernforms.COMMAND_LIGHT_POWER: aiomodernforms.LIGHT_POWER_ON}
)
assert response.light_on == aiomodernforms.LIGHT_POWER_ON
@pytest.mark.asyncio
async def test_light(aresponses):
"""Test to make sure setting lights works."""
aresponses.add("fan.local", "/mf", "POST", response=basic_info)
aresponses.add("fan.local", "/mf", "POST", response=basic_response)
async def evaluate_request(request):
data = await request.json()
assert aiomodernforms.COMMAND_LIGHT_POWER in data
assert aiomodernforms.COMMAND_LIGHT_BRIGHTNESS in data
assert aiomodernforms.COMMAND_LIGHT_SLEEP_TIMER in data
modified_response = basic_response.copy()
modified_response[STATE_LIGHT_POWER] = data[aiomodernforms.COMMAND_LIGHT_POWER]
modified_response[STATE_LIGHT_BRIGHTNESS] = data[
aiomodernforms.COMMAND_LIGHT_BRIGHTNESS
]
modified_response[STATE_LIGHT_SLEEP_TIMER] = data[
aiomodernforms.COMMAND_LIGHT_SLEEP_TIMER
]
return aresponses.Response(
status=200,
content_type="application/json",
text=json.dumps(modified_response),
)
aresponses.add("fan.local", "/mf", "POST", response=evaluate_request)
async with aiomodernforms.ModernFormsDevice("fan.local") as device:
await device.update()
sleep_time = datetime.now() + timedelta(minutes=2)
await device.light(
on=aiomodernforms.LIGHT_POWER_ON,
brightness=aiomodernforms.LIGHT_BRIGHTNESS_HIGH_VALUE,
sleep=sleep_time,
)
assert device.status.light_on == aiomodernforms.LIGHT_POWER_ON
assert (
device.status.light_brightness == aiomodernforms.LIGHT_BRIGHTNESS_HIGH_VALUE
)
assert device.status.light_sleep_timer == int(sleep_time.timestamp())
@pytest.mark.asyncio
async def test_light_sleep_datetime(aresponses):
"""Test to make sure setting light sleep works."""
aresponses.add("fan.local", "/mf", "POST", response=basic_info)
aresponses.add("fan.local", "/mf", "POST", response=basic_response)
async def evaluate_request(request):
data = await request.json()
assert aiomodernforms.COMMAND_LIGHT_SLEEP_TIMER in data
modified_response = basic_response.copy()
modified_response[STATE_LIGHT_SLEEP_TIMER] = data[
aiomodernforms.COMMAND_LIGHT_SLEEP_TIMER
]
return aresponses.Response(
status=200,
content_type="application/json",
text=json.dumps(modified_response),
)
aresponses.add("fan.local", "/mf", "POST", response=evaluate_request)
async with aiomodernforms.ModernFormsDevice("fan.local") as device:
sleep_time = datetime.now() + timedelta(minutes=2)
await device.light(
sleep=sleep_time,
)
assert device.status.light_sleep_timer == int(sleep_time.timestamp())
@pytest.mark.asyncio
async def test_light_sleep_int(aresponses):
"""Test to make sure setting light sleep works."""
aresponses.add("fan.local", "/mf", "POST", response=basic_info)
aresponses.add("fan.local", "/mf", "POST", response=basic_response)
async def evaluate_request(request):
data = await request.json()
assert aiomodernforms.COMMAND_LIGHT_SLEEP_TIMER in data
modified_response = basic_response.copy()
modified_response[STATE_LIGHT_SLEEP_TIMER] = data[
aiomodernforms.COMMAND_LIGHT_SLEEP_TIMER
]
return aresponses.Response(
status=200,
content_type="application/json",
text=json.dumps(modified_response),
)
aresponses.add("fan.local", "/mf", "POST", response=evaluate_request)
async with aiomodernforms.ModernFormsDevice("fan.local") as device:
sleep_time = datetime.now() + timedelta(seconds=120)
await device.light(
sleep=120,
)
assert device.status.light_sleep_timer == int(sleep_time.timestamp())
@pytest.mark.asyncio
async def test_light_sleep_clear(aresponses):
"""Test to make sure setting light sleep works."""
aresponses.add("fan.local", "/mf", "POST", response=basic_info)
aresponses.add("fan.local", "/mf", "POST", response=basic_response)
async def evaluate_request(request):
data = await request.json()
assert aiomodernforms.COMMAND_LIGHT_SLEEP_TIMER in data
modified_response = basic_response.copy()
modified_response[STATE_LIGHT_SLEEP_TIMER] = data[
aiomodernforms.COMMAND_LIGHT_SLEEP_TIMER
]
return aresponses.Response(
status=200,
content_type="application/json",
text=json.dumps(modified_response),
)
aresponses.add("fan.local", "/mf", "POST", response=evaluate_request)
# check to clear timer
async with aiomodernforms.ModernFormsDevice("fan.local") as device:
await device.light(
sleep=0,
)
assert device.status.light_sleep_timer == 0
@pytest.mark.asyncio
async def test_fan(aresponses):
"""Test to make sure setting fan works."""
aresponses.add("fan.local", "/mf", "POST", response=basic_info)
aresponses.add("fan.local", "/mf", "POST", response=basic_response)
async def evaluate_request(request):
data = await request.json()
assert aiomodernforms.COMMAND_FAN_POWER in data
assert aiomodernforms.COMMAND_FAN_SPEED in data
assert aiomodernforms.COMMAND_FAN_DIRECTION in data
assert aiomodernforms.COMMAND_FAN_SLEEP_TIMER in data
modified_response = basic_response.copy()
modified_response[STATE_FAN_POWER] = data[aiomodernforms.COMMAND_FAN_POWER]
modified_response[STATE_FAN_SPEED] = data[aiomodernforms.COMMAND_FAN_SPEED]
modified_response[STATE_FAN_DIRECTION] = data[
aiomodernforms.COMMAND_FAN_DIRECTION
]
modified_response[STATE_FAN_SLEEP_TIMER] = data[
aiomodernforms.COMMAND_FAN_SLEEP_TIMER
]
return aresponses.Response(
status=200,
content_type="application/json",
text=json.dumps(modified_response),
)
aresponses.add("fan.local", "/mf", "POST", response=evaluate_request)
async with aiomodernforms.ModernFormsDevice("fan.local") as device:
await device.update()
sleep_time = datetime.now() + timedelta(minutes=2)
await device.fan(
on=aiomodernforms.FAN_POWER_ON,
speed=aiomodernforms.FAN_SPEED_HIGH_VALUE,
direction=aiomodernforms.FAN_DIRECTION_FORWARD,
sleep=sleep_time,
)
assert device.status.fan_on == aiomodernforms.FAN_POWER_ON
assert device.status.fan_speed == aiomodernforms.FAN_SPEED_HIGH_VALUE
assert device.status.fan_direction == aiomodernforms.FAN_DIRECTION_FORWARD
assert device.status.fan_sleep_timer == int(sleep_time.timestamp())
@pytest.mark.asyncio
async def test_fan_sleep_datetime(aresponses):
"""Test to make sure setting light sleep works."""
aresponses.add("fan.local", "/mf", "POST", response=basic_info)
aresponses.add("fan.local", "/mf", "POST", response=basic_response)
async def evaluate_request(request):
data = await request.json()
assert aiomodernforms.COMMAND_FAN_SLEEP_TIMER in data
modified_response = basic_response.copy()
modified_response[STATE_FAN_SLEEP_TIMER] = data[
aiomodernforms.COMMAND_FAN_SLEEP_TIMER
]
return aresponses.Response(
status=200,
content_type="application/json",
text=json.dumps(modified_response),
)
aresponses.add("fan.local", "/mf", "POST", response=evaluate_request)
async with aiomodernforms.ModernFormsDevice("fan.local") as device:
sleep_time = datetime.now() + timedelta(minutes=2)
await device.fan(
sleep=sleep_time,
)
assert device.status.fan_sleep_timer == int(sleep_time.timestamp())
@pytest.mark.asyncio
async def test_fan_sleep_int(aresponses):
"""Test to make sure setting light sleep works."""
aresponses.add("fan.local", "/mf", "POST", response=basic_info)
aresponses.add("fan.local", "/mf", "POST", response=basic_response)
async def evaluate_request(request):
data = await request.json()
assert aiomodernforms.COMMAND_FAN_SLEEP_TIMER in data
modified_response = basic_response.copy()
modified_response[STATE_FAN_SLEEP_TIMER] = data[
aiomodernforms.COMMAND_FAN_SLEEP_TIMER
]
return aresponses.Response(
status=200,
content_type="application/json",
text=json.dumps(modified_response),
)
aresponses.add("fan.local", "/mf", "POST", response=evaluate_request)
async with aiomodernforms.ModernFormsDevice("fan.local") as device:
sleep_time = datetime.now() + timedelta(seconds=120)
await device.fan(
sleep=120,
)
assert device.status.fan_sleep_timer == int(sleep_time.timestamp())
@pytest.mark.asyncio
async def test_fan_sleep_clear(aresponses):
"""Test to make sure setting light sleep works."""
aresponses.add("fan.local", "/mf", "POST", response=basic_info)
aresponses.add("fan.local", "/mf", "POST", response=basic_response)
async def evaluate_request(request):
data = await request.json()
assert aiomodernforms.COMMAND_FAN_SLEEP_TIMER in data
modified_response = basic_response.copy()
modified_response[STATE_FAN_SLEEP_TIMER] = data[
aiomodernforms.COMMAND_FAN_SLEEP_TIMER
]
return aresponses.Response(
status=200,
content_type="application/json",
text=json.dumps(modified_response),
)
aresponses.add("fan.local", "/mf", "POST", response=evaluate_request)
# check to clear timer
async with aiomodernforms.ModernFormsDevice("fan.local") as device:
await device.fan(
sleep=0,
)
assert device.status.fan_sleep_timer == 0
@pytest.mark.asyncio
async def test_away(aresponses):
"""Test to make sure setting away mode works."""
aresponses.add("fan.local", "/mf", "POST", response=basic_info)
aresponses.add("fan.local", "/mf", "POST", response=basic_response)
async def evaluate_request(request):
data = await request.json()
assert aiomodernforms.COMMAND_AWAY_MODE in data
modified_response = basic_response.copy()
modified_response[STATE_AWAY_MODE] = data[aiomodernforms.COMMAND_AWAY_MODE]
return aresponses.Response(
status=200,
content_type="application/json",
text=json.dumps(modified_response),
)
aresponses.add("fan.local", "/mf", "POST", response=evaluate_request)
async with aiomodernforms.ModernFormsDevice("fan.local") as device:
await device.update()
await device.away(AWAY_MODE_ON)
assert device.status.away_mode_enabled
@pytest.mark.asyncio
async def test_adaptive_learning(aresponses):
"""Test to make sure setting adaptive learning mode works."""
aresponses.add("fan.local", "/mf", "POST", response=basic_info)
aresponses.add("fan.local", "/mf", "POST", response=basic_response)
async def evaluate_request(request):
data = await request.json()
assert aiomodernforms.COMMAND_ADAPTIVE_LEARNING in data
modified_response = basic_response.copy()
modified_response[STATE_ADAPTIVE_LEARNING] = data[
aiomodernforms.COMMAND_ADAPTIVE_LEARNING
]
return aresponses.Response(
status=200,
content_type="application/json",
text=json.dumps(modified_response),
)
aresponses.add("fan.local", "/mf", "POST", response=evaluate_request)
async with aiomodernforms.ModernFormsDevice("fan.local") as device:
await device.update()
await device.adaptive_learning(ADAPTIVE_LEARNING_ON)
assert device.status.adaptive_learning_enabled
@pytest.mark.asyncio
async def test_invalid_setting(aresponses):
"""Test to make sure setting invalid settings are rejected."""
aresponses.add("fan.local", "/mf", "POST", response=basic_info)
aresponses.add("fan.local", "/mf", "POST", response=basic_response)
async with aiomodernforms.ModernFormsDevice("fan.local") as device:
await device.update()
# light on non boolean
with pytest.raises(aiomodernforms.ModernFormsInvalidSettingsError):
await device.light(on="foo")
# light brightness not integer
with pytest.raises(aiomodernforms.ModernFormsInvalidSettingsError):
await device.light(brightness="foo")
# light brightess out of range
with pytest.raises(aiomodernforms.ModernFormsInvalidSettingsError):
await device.light(brightness=LIGHT_BRIGHTNESS_HIGH_VALUE + 1)
with pytest.raises(aiomodernforms.ModernFormsInvalidSettingsError):
await device.light(brightness=LIGHT_BRIGHTNESS_LOW_VALUE - 1)
# light sleep non boolean
with pytest.raises(aiomodernforms.ModernFormsInvalidSettingsError):
await device.light(sleep="foo")
# light sleep out of range
with pytest.raises(aiomodernforms.ModernFormsInvalidSettingsError):
await device.light(sleep=datetime.now() + timedelta(hours=25))
with pytest.raises(aiomodernforms.ModernFormsInvalidSettingsError):
await device.light(sleep=datetime.now() - timedelta(minutes=1))
# fan on non boolean
with pytest.raises(aiomodernforms.ModernFormsInvalidSettingsError):
await device.fan(on="foo")
# fan speed not integer
with pytest.raises(aiomodernforms.ModernFormsInvalidSettingsError):
await device.fan(speed="foo")
# fan speed out of range
with pytest.raises(aiomodernforms.ModernFormsInvalidSettingsError):
await device.fan(speed=FAN_SPEED_HIGH_VALUE + 1)
with pytest.raises(aiomodernforms.ModernFormsInvalidSettingsError):
await device.fan(speed=FAN_SPEED_LOW_VALUE - 1)
# fan sleep non boolean
with pytest.raises(aiomodernforms.ModernFormsInvalidSettingsError):
await device.fan(sleep="foo")
# fan sleep out of range
with pytest.raises(aiomodernforms.ModernFormsInvalidSettingsError):
await device.fan(sleep=datetime.now() + timedelta(hours=25))
with pytest.raises(aiomodernforms.ModernFormsInvalidSettingsError):
await device.fan(sleep=datetime.now() - timedelta(minutes=1))
# fan direction non string
with pytest.raises(aiomodernforms.ModernFormsInvalidSettingsError):
await device.fan(direction=1)
# fan direction invlaid value
with pytest.raises(aiomodernforms.ModernFormsInvalidSettingsError):
await device.fan(direction="upwards")
@pytest.mark.asyncio
async def test_connection_error(aresponses):
"""Test to make validate proper connection error handling."""
aresponses.add("fan.local", "/mf", "POST", response=basic_info)
with pytest.raises(aiomodernforms.ModernFormsConnectionError):
async with aiomodernforms.ModernFormsDevice("fan.local") as device:
await device.update()
@pytest.mark.asyncio
async def test_server_error(aresponses):
"""Test to make validate proper server error handling."""
aresponses.add("fan.local", "/mf", "POST", response=basic_info)
aresponses.add(
"fan.local",
"/mf",
"POST",
response=aresponses.Response(text="error", status=500),
)
with pytest.raises(aiomodernforms.ModernFormsError):
async with aiomodernforms.ModernFormsDevice("fan.local") as device:
await device.update()
@pytest.mark.asyncio
async def test_reboot(aresponses):
"""Test how reboot is handled."""
aresponses.add("fan.local", "/mf", "POST", response=basic_info)
aresponses.add("fan.local", "/mf", "POST", response=basic_response)
async with aiomodernforms.ModernFormsDevice("fan.local") as device:
await device.update()
with patch(
"aiomodernforms.ModernFormsDevice.request",
side_effect=ModernFormsConnectionTimeoutError,
):
await device.reboot()
@pytest.mark.asyncio
async def test_status_not_initialized_response():
"""Test status when not initialized."""
with pytest.raises(ModernFormsNotInitializedError):
async with aiomodernforms.ModernFormsDevice("fan.local") as device:
device.status()
@pytest.mark.asyncio
async def test_info_not_initialized_response():
"""Test info when not initialized."""
with pytest.raises(ModernFormsNotInitializedError):
async with aiomodernforms.ModernFormsDevice("fan.local") as device:
device.info()
@pytest.mark.asyncio
async def test_empty_response(aresponses):
"""Test for an Empty Response."""
aresponses.add("fan.local", "/mf", "POST", response=basic_info)
async def send_empty_state(request):
await request.json()
return aresponses.Response(
status=200,
content_type="application/json",
text="{}",
)
aresponses.add("fan.local", "/mf", "POST", response=send_empty_state)
with pytest.raises(ModernFormsConnectionError):
async with aiomodernforms.ModernFormsDevice("fan.local") as device:
await device.update()
|