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 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
|
import logging
from unittest.mock import AsyncMock, Mock, patch
import pytest
from switchbot import SwitchbotModel
from switchbot.const.lock import LockStatus
from switchbot.devices import lock
from .test_adv_parser import generate_ble_device
def create_device_for_command_testing(model: str):
ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
return lock.SwitchbotLock(
ble_device, "ff", "ffffffffffffffffffffffffffffffff", model=model
)
@pytest.mark.parametrize(
"model",
[
SwitchbotModel.LOCK,
SwitchbotModel.LOCK_LITE,
SwitchbotModel.LOCK_PRO,
SwitchbotModel.LOCK_ULTRA,
],
)
def test_lock_init(model: str):
"""Test the initialization of the lock device."""
device = create_device_for_command_testing(model)
assert device._model == model
@pytest.mark.parametrize(
"model",
[
SwitchbotModel.AIR_PURIFIER,
],
)
def test_lock_init_with_invalid_model(model: str):
"""Test that initializing with an invalid model raises ValueError."""
with pytest.raises(
ValueError, match="initializing SwitchbotLock with a non-lock model"
):
create_device_for_command_testing(model)
@pytest.mark.asyncio
@pytest.mark.parametrize(
"model",
[
SwitchbotModel.LOCK,
SwitchbotModel.LOCK_LITE,
SwitchbotModel.LOCK_PRO,
SwitchbotModel.LOCK_ULTRA,
],
)
async def test_verify_encryption_key(model: str):
"""Test verify_encryption_key method."""
ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
with patch("switchbot.devices.lock.super") as mock_super:
mock_super().verify_encryption_key = AsyncMock(return_value=True)
result = await lock.SwitchbotLock.verify_encryption_key(
ble_device, "key_id", "encryption_key", model
)
assert result is True
@pytest.mark.asyncio
@pytest.mark.parametrize(
("model", "command"),
[
(SwitchbotModel.LOCK, b"W\x0fN\x01\x01\x10\x80"),
(SwitchbotModel.LOCK_LITE, b"W\x0fN\x01\x01\x10\x81"),
(SwitchbotModel.LOCK_PRO, b"W\x0fN\x01\x01\x10\x85"),
(SwitchbotModel.LOCK_ULTRA, b"W\x0fN\x01\x01\x10\x86"),
],
)
async def test_lock(model: str, command: bytes):
"""Test lock method."""
device = create_device_for_command_testing(model)
device._get_adv_value = Mock(return_value=LockStatus.UNLOCKED)
with (
patch.object(device, "_send_command", return_value=b"\x01\x00"),
patch.object(device, "_enable_notifications", return_value=True),
patch.object(device, "_get_basic_info", return_value=b"\x00\x64\x01"),
):
result = await device.lock()
assert result is True
@pytest.mark.asyncio
@pytest.mark.parametrize(
("model", "command"),
[
(SwitchbotModel.LOCK, b"W\x0fN\x01\x01\x10\x80"),
(SwitchbotModel.LOCK_LITE, b"W\x0fN\x01\x01\x10\x81"),
(SwitchbotModel.LOCK_PRO, b"W\x0fN\x01\x01\x10\x84"),
(SwitchbotModel.LOCK_ULTRA, b"W\x0fN\x01\x01\x10\x83"),
],
)
async def test_unlock(model: str, command: bytes):
"""Test unlock method."""
device = create_device_for_command_testing(model)
device._get_adv_value = Mock(return_value=LockStatus.LOCKED)
with (
patch.object(device, "_send_command", return_value=b"\x01\x00"),
patch.object(device, "_enable_notifications", return_value=True),
patch.object(device, "_get_basic_info", return_value=b"\x00\x64\x01"),
):
result = await device.unlock()
assert result is True
@pytest.mark.asyncio
@pytest.mark.parametrize(
"model",
[
SwitchbotModel.LOCK,
SwitchbotModel.LOCK_LITE,
SwitchbotModel.LOCK_PRO,
SwitchbotModel.LOCK_ULTRA,
],
)
async def test_unlock_without_unlatch(model: str):
"""Test unlock_without_unlatch method."""
device = create_device_for_command_testing(model)
device._get_adv_value = Mock(return_value=LockStatus.LOCKED)
with (
patch.object(device, "_send_command", return_value=b"\x01\x00"),
patch.object(device, "_enable_notifications", return_value=True),
patch.object(device, "_get_basic_info", return_value=b"\x00\x64\x01"),
):
result = await device.unlock_without_unlatch()
assert result is True
@pytest.mark.asyncio
@pytest.mark.parametrize(
"model",
[
SwitchbotModel.LOCK,
SwitchbotModel.LOCK_LITE,
SwitchbotModel.LOCK_PRO,
SwitchbotModel.LOCK_ULTRA,
],
)
async def test_get_basic_info(model: str):
"""Test get_basic_info method."""
device = create_device_for_command_testing(model)
lock_data = b"\x00\x80\x00\x00\x00\x00\x00\x00"
basic_data = b"\x00\x64\x01"
with (
patch.object(device, "_get_lock_info", return_value=lock_data),
patch.object(device, "_get_basic_info", return_value=basic_data),
):
result = await device.get_basic_info()
assert result is not None
assert "battery" in result
assert "firmware" in result
assert "calibration" in result
assert "status" in result
@pytest.mark.asyncio
@pytest.mark.parametrize(
"model",
[
SwitchbotModel.LOCK,
SwitchbotModel.LOCK_LITE,
SwitchbotModel.LOCK_PRO,
SwitchbotModel.LOCK_ULTRA,
],
)
async def test_get_basic_info_no_lock_data(model: str):
"""Test get_basic_info when no lock data is returned."""
device = create_device_for_command_testing(model)
with patch.object(device, "_get_lock_info", return_value=None):
result = await device.get_basic_info()
assert result is None
@pytest.mark.asyncio
@pytest.mark.parametrize(
"model",
[
SwitchbotModel.LOCK,
SwitchbotModel.LOCK_LITE,
SwitchbotModel.LOCK_PRO,
SwitchbotModel.LOCK_ULTRA,
],
)
async def test_get_basic_info_no_basic_data(model: str):
"""Test get_basic_info when no basic data is returned."""
device = create_device_for_command_testing(model)
lock_data = b"\x00\x80\x00\x00\x00\x00\x00\x00"
with (
patch.object(device, "_get_lock_info", return_value=lock_data),
patch.object(device, "_get_basic_info", return_value=None),
):
result = await device.get_basic_info()
assert result is None
def test_parse_basic_data():
"""Test _parse_basic_data method."""
device = create_device_for_command_testing(SwitchbotModel.LOCK)
basic_data = b"\x00\x64\x01"
result = device._parse_basic_data(basic_data)
assert result["battery"] == 100
assert result["firmware"] == 0.1
@pytest.mark.parametrize(
"model",
[
SwitchbotModel.LOCK,
SwitchbotModel.LOCK_LITE,
SwitchbotModel.LOCK_PRO,
SwitchbotModel.LOCK_ULTRA,
],
)
def test_is_calibrated(model: str):
"""Test is_calibrated method."""
device = create_device_for_command_testing(model)
device._get_adv_value = Mock(return_value=True)
assert device.is_calibrated() is True
@pytest.mark.parametrize(
"model",
[
SwitchbotModel.LOCK,
SwitchbotModel.LOCK_LITE,
SwitchbotModel.LOCK_PRO,
SwitchbotModel.LOCK_ULTRA,
],
)
def test_get_lock_status(model: str):
"""Test get_lock_status method."""
device = create_device_for_command_testing(model)
device._get_adv_value = Mock(return_value=LockStatus.LOCKED)
assert device.get_lock_status() == LockStatus.LOCKED
@pytest.mark.parametrize(
"model",
[
SwitchbotModel.LOCK,
SwitchbotModel.LOCK_PRO,
SwitchbotModel.LOCK_ULTRA,
],
)
def test_is_door_open(model: str):
"""Test is_door_open method."""
device = create_device_for_command_testing(model)
device._get_adv_value = Mock(return_value=True)
assert device.is_door_open() is True
@pytest.mark.parametrize(
"model",
[
SwitchbotModel.LOCK,
SwitchbotModel.LOCK_PRO,
SwitchbotModel.LOCK_ULTRA,
],
)
def test_is_unclosed_alarm_on(model: str):
"""Test is_unclosed_alarm_on method."""
device = create_device_for_command_testing(model)
device._get_adv_value = Mock(return_value=True)
assert device.is_unclosed_alarm_on() is True
@pytest.mark.parametrize(
"model",
[
SwitchbotModel.LOCK,
SwitchbotModel.LOCK_LITE,
SwitchbotModel.LOCK_PRO,
SwitchbotModel.LOCK_ULTRA,
],
)
def test_is_unlocked_alarm_on(model: str):
"""Test is_unlocked_alarm_on method."""
device = create_device_for_command_testing(model)
device._get_adv_value = Mock(return_value=True)
assert device.is_unlocked_alarm_on() is True
@pytest.mark.parametrize(
"model",
[
SwitchbotModel.LOCK,
],
)
def test_is_auto_lock_paused(model: str):
"""Test is_auto_lock_paused method."""
device = create_device_for_command_testing(model)
device._get_adv_value = Mock(return_value=True)
assert device.is_auto_lock_paused() is True
@pytest.mark.parametrize(
"model",
[
SwitchbotModel.LOCK,
SwitchbotModel.LOCK_LITE,
SwitchbotModel.LOCK_PRO,
SwitchbotModel.LOCK_ULTRA,
],
)
def test_is_night_latch_enabled(model: str):
"""Test is_night_latch_enabled method."""
device = create_device_for_command_testing(model)
device._get_adv_value = Mock(return_value=True)
assert device.is_night_latch_enabled() is True
@pytest.mark.asyncio
@pytest.mark.parametrize(
"model",
[
SwitchbotModel.LOCK,
SwitchbotModel.LOCK_LITE,
SwitchbotModel.LOCK_PRO,
SwitchbotModel.LOCK_ULTRA,
],
)
async def test_get_lock_info(model: str):
"""Test _get_lock_info method."""
device = create_device_for_command_testing(model)
expected_data = b"\x01\x00\x80\x00\x00\x00\x00\x00"
with patch.object(device, "_send_command", return_value=expected_data):
result = await device._get_lock_info()
assert result == expected_data
@pytest.mark.asyncio
@pytest.mark.parametrize(
"model",
[
SwitchbotModel.LOCK,
SwitchbotModel.LOCK_LITE,
SwitchbotModel.LOCK_PRO,
SwitchbotModel.LOCK_ULTRA,
],
)
async def test_get_lock_info_failure(model: str):
"""Test _get_lock_info method when command fails."""
device = create_device_for_command_testing(model)
with patch.object(device, "_send_command", return_value=b"\x00\x00"):
result = await device._get_lock_info()
assert result is None
@pytest.mark.asyncio
@pytest.mark.parametrize(
"model",
[
SwitchbotModel.LOCK,
SwitchbotModel.LOCK_LITE,
SwitchbotModel.LOCK_PRO,
SwitchbotModel.LOCK_ULTRA,
],
)
async def test_enable_notifications(model: str):
"""Test _enable_notifications method."""
device = create_device_for_command_testing(model)
with patch.object(device, "_send_command", return_value=b"\x01\x00"):
result = await device._enable_notifications()
assert result is True
@pytest.mark.asyncio
@pytest.mark.parametrize(
"model",
[
SwitchbotModel.LOCK,
SwitchbotModel.LOCK_LITE,
SwitchbotModel.LOCK_PRO,
SwitchbotModel.LOCK_ULTRA,
],
)
async def test_disable_notifications(model: str):
"""Test _disable_notifications method."""
device = create_device_for_command_testing(model)
device._notifications_enabled = True
with patch.object(device, "_send_command", return_value=b"\x01\x00"):
result = await device._disable_notifications()
assert result is True
assert device._notifications_enabled is False
@pytest.mark.asyncio
@pytest.mark.parametrize(
"model",
[
SwitchbotModel.LOCK,
SwitchbotModel.LOCK_LITE,
SwitchbotModel.LOCK_PRO,
SwitchbotModel.LOCK_ULTRA,
],
)
async def test_disable_notifications_already_disabled(model: str):
"""Test _disable_notifications when already disabled."""
device = create_device_for_command_testing(model)
device._notifications_enabled = False
with patch.object(device, "_send_command") as mock_send:
result = await device._disable_notifications()
assert result is True
mock_send.assert_not_called()
@pytest.mark.parametrize(
"model",
[
SwitchbotModel.LOCK,
SwitchbotModel.LOCK_LITE,
SwitchbotModel.LOCK_PRO,
SwitchbotModel.LOCK_ULTRA,
],
)
def test_notification_handler(model: str):
"""Test _notification_handler method."""
device = create_device_for_command_testing(model)
device._notifications_enabled = True
data = bytearray(b"\x0f\x00\x00\x00\x80\x00\x00\x00\x00\x00")
with patch.object(device, "_update_lock_status") as mock_update:
device._notification_handler(0, data)
mock_update.assert_called_once_with(data)
@pytest.mark.parametrize(
"model",
[
SwitchbotModel.LOCK,
SwitchbotModel.LOCK_LITE,
SwitchbotModel.LOCK_PRO,
SwitchbotModel.LOCK_ULTRA,
],
)
def test_notification_handler_not_enabled(model: str):
"""Test _notification_handler when notifications not enabled."""
device = create_device_for_command_testing(model)
device._notifications_enabled = False
data = bytearray(b"\x01\x00\x00\x00\x80\x00\x00\x00\x00\x00")
with (
patch.object(device, "_update_lock_status") as mock_update,
patch.object(
device.__class__.__bases__[0], "_notification_handler"
) as mock_super,
):
device._notification_handler(0, data)
mock_update.assert_not_called()
mock_super.assert_called_once()
@pytest.mark.parametrize(
"model",
[
SwitchbotModel.LOCK,
SwitchbotModel.LOCK_LITE,
SwitchbotModel.LOCK_PRO,
SwitchbotModel.LOCK_ULTRA,
],
)
def test_notification_handler_during_disconnect(
model: str, caplog: pytest.LogCaptureFixture
) -> None:
"""Test _notification_handler during expected disconnect."""
device = create_device_for_command_testing(model)
device._notifications_enabled = True
device._expected_disconnect = True
data = bytearray(b"\x0f\x00\x00\x00\x80\x00\x00\x00\x00\x00")
with (
patch.object(device, "_update_lock_status") as mock_update,
caplog.at_level(logging.DEBUG),
):
device._notification_handler(0, data)
# Should not update lock status during disconnect
mock_update.assert_not_called()
# Should log debug message
assert "Ignoring lock notification during expected disconnect" in caplog.text
@pytest.mark.parametrize(
"model",
[
SwitchbotModel.LOCK,
SwitchbotModel.LOCK_LITE,
SwitchbotModel.LOCK_PRO,
SwitchbotModel.LOCK_ULTRA,
],
)
def test_update_lock_status(model: str):
"""Test _update_lock_status method."""
device = create_device_for_command_testing(model)
data = bytearray(b"\x0f\x00\x00\x00\x80\x00\x00\x00\x00\x00")
with (
patch.object(device, "_decrypt", return_value=b"\x80\x00\x00\x00\x00\x00"),
patch.object(device, "_update_parsed_data", return_value=True),
patch.object(device, "_reset_disconnect_timer"),
patch.object(device, "_fire_callbacks"),
):
device._update_lock_status(data)
@pytest.mark.parametrize(
("model", "data", "expected"),
[
(
SwitchbotModel.LOCK,
b"\x80\x00\x00\x00\x00\x00",
{
"calibration": True,
"status": LockStatus.LOCKED, # (0x80 & 0b01110000) >> 4 = 0 = LOCKED
"door_open": False,
"unclosed_alarm": False,
"unlocked_alarm": False,
},
),
(
SwitchbotModel.LOCK_LITE,
b"\x80\x00\x00\x00\x00\x00",
{
"calibration": True,
"status": LockStatus.LOCKED, # (0x80 & 0b01110000) >> 4 = 0 = LOCKED
"unlocked_alarm": False,
},
),
(
SwitchbotModel.LOCK_PRO,
b"\x80\x00\x00\x00\x00\x00",
{
"calibration": True,
"status": LockStatus.LOCKED, # (0x80 & 0b01111000) >> 3 = 0 = LOCKED
"door_open": False,
"unclosed_alarm": False,
"unlocked_alarm": False,
},
),
(
SwitchbotModel.LOCK_ULTRA,
b"\x88\x10\x00\x00\x00\xc0",
{
"calibration": True,
"status": LockStatus.UNLOCKED, # (0x88 & 0b01111000) >> 3 = 0x08 >> 3 = 1 = UNLOCKED
"door_open": True,
"unclosed_alarm": True,
"unlocked_alarm": True,
},
),
],
)
def test_parse_lock_data(model: str, data: bytes, expected: dict):
"""Test _parse_lock_data static method."""
result = lock.SwitchbotLock._parse_lock_data(data, model)
assert result == expected
@pytest.mark.parametrize(
("model", "data", "expected"),
[
# Test LOCK with different status bits and flags
(
SwitchbotModel.LOCK,
b"\x94\x00\x00\x00\x00\x00", # Unlocked status (0x10 >> 4 = 1) with door open
{
"calibration": True,
"status": LockStatus.UNLOCKED,
"door_open": True,
"unclosed_alarm": False,
"unlocked_alarm": False,
},
),
# Test LOCK_LITE without door_open field
(
SwitchbotModel.LOCK_LITE,
b"\x90\x10\x00\x00\x00\x00", # Unlocked with unlocked alarm
{
"calibration": True,
"status": LockStatus.UNLOCKED,
"unlocked_alarm": True,
},
),
# Test LOCK_PRO with new bit positions
(
SwitchbotModel.LOCK_PRO,
b"\x90\x10\x00\x00\x00\xc0", # New format: status bits 3-6, door open bit 4 of byte 1
{
"calibration": True,
"status": LockStatus.LOCKING, # (0x90 & 0b01111000) >> 3 = 0x10 >> 3 = 2 (LOCKING)
"door_open": True, # bit 4 of byte 1 (0x10)
"unclosed_alarm": True, # bit 7 of byte 5 (0xc0)
"unlocked_alarm": True, # bit 6 of byte 5 (0xc0)
},
),
# Test LOCK_ULTRA with same format as PRO
(
SwitchbotModel.LOCK_ULTRA,
b"\x88\x00\x00\x00\x00\x40", # Unlocked with unlocked alarm only
{
"calibration": True,
"status": LockStatus.UNLOCKED, # (0x88 & 0b01111000) >> 3 = 0x08 >> 3 = 1
"door_open": False,
"unclosed_alarm": False,
"unlocked_alarm": True, # bit 6 of byte 5
},
),
],
)
def test_parse_lock_data_new_formats(model: str, data: bytes, expected: dict):
"""Test _parse_lock_data with new format changes."""
result = lock.SwitchbotLock._parse_lock_data(data, model)
assert result == expected
@pytest.mark.asyncio
@pytest.mark.parametrize(
"model",
[
SwitchbotModel.LOCK,
SwitchbotModel.LOCK_LITE,
SwitchbotModel.LOCK_PRO,
SwitchbotModel.LOCK_ULTRA,
],
)
async def test_lock_with_update(model: str):
"""Test lock method with status update."""
device = create_device_for_command_testing(model)
device._get_adv_value = Mock(side_effect=[None, LockStatus.UNLOCKED])
with (
patch.object(device, "update", new_callable=AsyncMock),
patch.object(device, "_send_command", return_value=b"\x01\x00"),
patch.object(device, "_enable_notifications", return_value=True),
patch.object(device, "_get_basic_info", return_value=b"\x00\x64\x01"),
):
result = await device.lock()
assert result is True
@pytest.mark.asyncio
@pytest.mark.parametrize(
("model", "status"),
[
(SwitchbotModel.LOCK, LockStatus.LOCKED),
(SwitchbotModel.LOCK_LITE, LockStatus.LOCKING),
(SwitchbotModel.LOCK_PRO, LockStatus.LOCKED),
(SwitchbotModel.LOCK_ULTRA, LockStatus.LOCKING),
],
)
async def test_lock_already_locked(model: str, status: LockStatus):
"""Test lock method when already locked."""
device = create_device_for_command_testing(model)
device._get_adv_value = Mock(return_value=status)
with patch.object(device, "_send_command") as mock_send:
result = await device.lock()
assert result is True
mock_send.assert_not_called()
@pytest.mark.asyncio
@pytest.mark.parametrize(
"model",
[
SwitchbotModel.LOCK,
SwitchbotModel.LOCK_LITE,
SwitchbotModel.LOCK_PRO,
SwitchbotModel.LOCK_ULTRA,
],
)
async def test_lock_with_invalid_basic_data(model: str):
"""Test lock method with invalid basic data."""
device = create_device_for_command_testing(model)
device._get_adv_value = Mock(return_value=LockStatus.UNLOCKED)
with (
patch.object(device, "_send_command", return_value=b"\x01\x00"),
patch.object(device, "_enable_notifications", return_value=True),
patch.object(device, "_get_basic_info", return_value=b"\x00"),
):
result = await device.lock()
assert result is True
|