| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569 |
- import logging
- from typing import Any
- from unittest.mock import AsyncMock, Mock, call, patch
- import pytest
- from switchbot import SwitchbotLockUltra, SwitchbotModel
- from switchbot.const.lock import LockStatus, QuickKeyFunction
- from switchbot.devices import lock
- from switchbot.devices.device import SwitchbotOperationError
- from switchbot.models import SwitchBotAdvertisement
- from .test_adv_parser import generate_ble_device
- @pytest.mark.asyncio
- @pytest.mark.parametrize(
- ("replies", "valid"),
- [
- (
- [bytes.fromhex("01 88 00 00 00 00 00"), bytes.fromhex("01 85 28 21 14")],
- True,
- ),
- ([None], False),
- ([bytes.fromhex("01 88 00 00 00 00 00"), None], False),
- ],
- )
- async def test_uwb_lock_key_verification(replies, valid):
- device = generate_ble_device("aa:bb:cc:dd:ee:ff", "UWB Lock")
- with patch.object(SwitchbotLockUltra, "_send_command", side_effect=replies) as send:
- assert (
- await SwitchbotLockUltra.verify_encryption_key(
- device, "ff", "ff" * 16, SwitchbotModel.UWB_LOCK
- )
- is valid
- )
- assert [args.kwargs["key"] for args in send.await_args_list] == [
- "570f4f8107",
- "5702",
- ][: len(replies)]
- @pytest.mark.asyncio
- async def test_nano_update_reads_lock_and_basic_info():
- device = create_device_for_command_testing(SwitchbotModel.LOCK_NANO)
- device.update_from_advertisement(
- SwitchBotAdvertisement(
- device._device.address, {"data": {"battery": 95}}, device._device, -60
- )
- )
- replies = [bytes.fromhex("01 90 08 5f 00 00 40"), bytes.fromhex("01 4b 21 14")]
- with patch.object(device, "_send_command", side_effect=replies) as send:
- await device.update()
- assert send.await_args_list == [
- call(key="570f4f810c", retry=device._retry_count),
- call(key="5702", retry=device._retry_count),
- ]
- assert device.get_lock_status() is LockStatus.LOCKING
- assert device.is_calibrated()
- assert device.is_auto_lock_paused()
- assert device.is_unlocked_alarm_on()
- assert device.get_battery_percent() == 75
- assert device.parsed_data["firmware"] == 3.3
- @pytest.mark.asyncio
- async def test_nano_key_verification_without_response():
- device = generate_ble_device("aa:bb:cc:dd:ee:ff", "Lock Nano")
- with patch.object(lock.SwitchbotLock, "_send_command", return_value=None):
- assert not await lock.SwitchbotLock.verify_encryption_key(
- device, "ff", "ff" * 16, SwitchbotModel.LOCK_NANO
- )
- @pytest.mark.asyncio
- async def test_nano_lock_info_command():
- device = create_device_for_command_testing(SwitchbotModel.LOCK_NANO)
- with patch.object(device, "_send_command", return_value=b"\x01\x00") as send:
- assert await device._get_lock_info() == b"\x01\x00"
- send.assert_awaited_once_with(key="570f4f810c", retry=device._retry_count)
- @pytest.mark.asyncio
- async def test_nano_notification_command():
- device = create_device_for_command_testing(SwitchbotModel.LOCK_NANO)
- with patch.object(device, "_send_command", return_value=b"\x01\x00") as send:
- assert await device._enable_notifications()
- send.assert_awaited_once_with("570e01001e0000810c")
- def create_device_for_command_testing(model: str, cls=lock.SwitchbotLock):
- ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
- return cls(ble_device, "ff", "ffffffffffffffffffffffffffffffff", model=model)
- def test_nano_status_notification_updates_device():
- device = create_device_for_command_testing(SwitchbotModel.LOCK_NANO)
- advertisement = SwitchBotAdvertisement(
- device._device.address,
- {"data": {"status": LockStatus.UNLOCKED}},
- device._device,
- -60,
- )
- device.update_from_advertisement(advertisement)
- received = []
- device.subscribe(lambda: received.append(device.get_lock_status()))
- payload = bytes.fromhex("90 00 00 00 00 00")
- with (
- patch.object(device, "_decrypt", return_value=payload) as decrypt,
- patch.object(device, "_reset_disconnect_timer"),
- ):
- frame = bytearray.fromhex("0f 00 00 00 12 34 56 78 9a bc")
- device._notification_handler(0, frame)
- decrypt.assert_called_once_with(frame[4:])
- assert received == [LockStatus.LOCKING]
- assert device.get_lock_status() is LockStatus.LOCKING
- @pytest.mark.parametrize(
- "model",
- [
- SwitchbotModel.LOCK,
- SwitchbotModel.LOCK_LITE,
- SwitchbotModel.LOCK_PRO,
- SwitchbotModel.LOCK_ULTRA,
- SwitchbotModel.LOCK_ULTRA_MAX,
- SwitchbotModel.UWB_LOCK,
- SwitchbotModel.LOCK_NANO,
- SwitchbotModel.LOCK_VISION,
- SwitchbotModel.LOCK_VISION_PRO,
- SwitchbotModel.LOCK_PRO_WIFI,
- ],
- )
- 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_JP,
- ],
- )
- 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)
- def test_ultra_direct_construction():
- device = generate_ble_device("aa:bb:cc:dd:ee:ff", "lock")
- assert (
- SwitchbotLockUltra(device, "ff", "ff" * 16)._model is SwitchbotModel.LOCK_ULTRA
- )
- def test_default_model_classvar():
- """The classvar default is SwitchbotModel.LOCK."""
- ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
- device = lock.SwitchbotLock(ble_device, "ff", "ffffffffffffffffffffffffffffffff")
- assert device._model == SwitchbotModel.LOCK
- @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"),
- (SwitchbotModel.LOCK_ULTRA_MAX, b"W\x0fN\x01\x01\x00\x00"),
- (SwitchbotModel.UWB_LOCK, b"W\x0fN\x01\x01\x00\x00"),
- (SwitchbotModel.LOCK_NANO, b"W\x0fN\x01\x01\x00\x00"),
- (SwitchbotModel.LOCK_VISION, b"W\x0fN\x01\x01\x00\x80"),
- (SwitchbotModel.LOCK_VISION_PRO, b"W\x0fN\x01\x01\x00\x80"),
- (SwitchbotModel.LOCK_PRO_WIFI, b"W\x0fN\x01\x01\x10\x82"),
- ],
- )
- 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"
- ) as mock_send_command,
- 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
- mock_send_command.assert_any_call(lock.COMMAND_LOCK[model])
- @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"),
- (SwitchbotModel.LOCK_ULTRA_MAX, b"W\x0fN\x01\x01\x00\x80"),
- (SwitchbotModel.UWB_LOCK, b"W\x0fN\x01\x01\x00\x80"),
- (SwitchbotModel.LOCK_NANO, b"W\x0fN\x01\x01\x00\x80"),
- (SwitchbotModel.LOCK_VISION, b"W\x0fN\x01\x01\x00\x80"),
- (SwitchbotModel.LOCK_VISION_PRO, b"W\x0fN\x01\x01\x00\x80"),
- (SwitchbotModel.LOCK_PRO_WIFI, b"W\x0fN\x01\x01\x10\x81"),
- ],
- )
- 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"
- ) as mock_send_command,
- 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
- mock_send_command.assert_any_call(lock.COMMAND_UNLOCK[model])
- @pytest.mark.asyncio
- @pytest.mark.parametrize(
- "model",
- [
- SwitchbotModel.LOCK,
- SwitchbotModel.LOCK_LITE,
- SwitchbotModel.LOCK_PRO,
- SwitchbotModel.LOCK_ULTRA,
- SwitchbotModel.LOCK_ULTRA_MAX,
- SwitchbotModel.UWB_LOCK,
- SwitchbotModel.LOCK_NANO,
- SwitchbotModel.LOCK_VISION,
- SwitchbotModel.LOCK_VISION_PRO,
- SwitchbotModel.LOCK_PRO_WIFI,
- ],
- )
- 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_NANO,
- SwitchbotModel.LOCK_PRO,
- SwitchbotModel.LOCK_ULTRA,
- SwitchbotModel.LOCK_ULTRA_MAX,
- SwitchbotModel.LOCK_VISION,
- SwitchbotModel.LOCK_VISION_PRO,
- SwitchbotModel.LOCK_PRO_WIFI,
- ],
- )
- 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 = bytes.fromhex("01 64 01 21 14")
- 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_NANO,
- SwitchbotModel.LOCK_PRO,
- SwitchbotModel.LOCK_ULTRA,
- SwitchbotModel.LOCK_VISION,
- SwitchbotModel.LOCK_VISION_PRO,
- SwitchbotModel.LOCK_PRO_WIFI,
- ],
- )
- 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_NANO,
- SwitchbotModel.LOCK_PRO,
- SwitchbotModel.LOCK_ULTRA,
- SwitchbotModel.LOCK_VISION,
- SwitchbotModel.LOCK_VISION_PRO,
- SwitchbotModel.LOCK_PRO_WIFI,
- ],
- )
- 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
- @pytest.mark.parametrize("battery", [100, 255])
- def test_parse_basic_data(battery):
- """Test _parse_basic_data method."""
- device = create_device_for_command_testing(SwitchbotModel.LOCK)
- basic_data = bytes([0, battery, 1])
- result = device._parse_basic_data(basic_data)
- assert result["battery"] == battery
- assert result["firmware"] == 0.1
- @pytest.mark.asyncio
- @pytest.mark.parametrize(
- "model",
- [SwitchbotModel.LOCK_ULTRA, SwitchbotModel.LOCK_ULTRA_MAX, SwitchbotModel.UWB_LOCK],
- )
- @pytest.mark.parametrize(
- ("basic_info", "battery", "firmware"),
- [
- ("01 4b 28 21 14", 75, 3.3), # Main battery in use.
- ("01 85 28 21 14", 40, 3.3), # Backup battery in use.
- ("01 f3 28 21 14", 40, 3.3), # Byte 1 must never appear as 243%.
- ("01 80 ff 21 14", 255, 3.3), # Report the backup battery byte as received.
- ("01 4b 28", 60, 2.1), # Truncated reply: keep the last readings.
- ],
- )
- async def test_lock2_basic_info(model, basic_info, battery, firmware):
- device = create_device_for_command_testing(model, SwitchbotLockUltra)
- device.update_from_advertisement(
- SwitchBotAdvertisement(
- device._device.address,
- {"data": {"battery": 60, "firmware": 2.1}},
- device._device,
- -60,
- )
- )
- with (
- patch.object(
- device, "_get_lock_info", return_value=bytes.fromhex("01 80 00 00 00 00 00")
- ),
- patch.object(device, "_get_basic_info", return_value=bytes.fromhex(basic_info)),
- ):
- await device.update()
- assert device.get_battery_percent() == battery
- assert device.parsed_data["firmware"] == firmware
- assert device.get_lock_status() is LockStatus.LOCKED
- @pytest.mark.asyncio
- @pytest.mark.parametrize(
- "model",
- [SwitchbotModel.LOCK_ULTRA, SwitchbotModel.LOCK_ULTRA_MAX, SwitchbotModel.UWB_LOCK],
- )
- async def test_lock2_battery_after_lock(model):
- device = create_device_for_command_testing(model, SwitchbotLockUltra)
- device.update_from_advertisement(
- SwitchBotAdvertisement(
- device._device.address,
- {"data": {"status": LockStatus.UNLOCKED}},
- device._device,
- -60,
- )
- )
- with (
- patch.object(device, "_enable_notifications", return_value=True),
- patch.object(device, "_send_command", return_value=bytes.fromhex("01")),
- patch.object(
- device, "_get_basic_info", return_value=bytes.fromhex("01 f3 28 21 14")
- ),
- ):
- assert await device.lock()
- assert device.get_battery_percent() == 40
- assert device.parsed_data["firmware"] == 3.3
- @pytest.mark.asyncio
- async def test_lock_ultra_max_verifies_encryption_key():
- """Lock Ultra Max verifies a valid encryption key."""
- lock_data = b"\x00\x80\x00\x00\x00\x00\x00\x00"
- basic_data = bytes.fromhex("01 64 01 21 14")
- ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
- with (
- patch.object(lock.SwitchbotLock, "_get_lock_info", return_value=lock_data),
- patch.object(lock.SwitchbotLock, "_get_basic_info", return_value=basic_data),
- ):
- assert await SwitchbotLockUltra.verify_encryption_key(
- ble_device,
- "ff",
- "ffffffffffffffffffffffffffffffff",
- SwitchbotModel.LOCK_ULTRA_MAX,
- )
- @pytest.mark.parametrize(
- "model",
- [
- SwitchbotModel.LOCK,
- SwitchbotModel.LOCK_LITE,
- SwitchbotModel.LOCK_PRO,
- SwitchbotModel.LOCK_ULTRA,
- SwitchbotModel.LOCK_VISION,
- SwitchbotModel.LOCK_VISION_PRO,
- SwitchbotModel.LOCK_PRO_WIFI,
- ],
- )
- 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,
- SwitchbotModel.LOCK_VISION,
- SwitchbotModel.LOCK_VISION_PRO,
- SwitchbotModel.LOCK_PRO_WIFI,
- ],
- )
- 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,
- SwitchbotModel.LOCK_VISION_PRO,
- SwitchbotModel.LOCK_PRO_WIFI,
- ],
- )
- 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,
- SwitchbotModel.LOCK_VISION_PRO,
- SwitchbotModel.LOCK_PRO_WIFI,
- ],
- )
- 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,
- SwitchbotModel.LOCK_VISION,
- SwitchbotModel.LOCK_VISION_PRO,
- SwitchbotModel.LOCK_PRO_WIFI,
- ],
- )
- 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,
- SwitchbotModel.LOCK_VISION,
- SwitchbotModel.LOCK_VISION_PRO,
- SwitchbotModel.LOCK_PRO_WIFI,
- ],
- )
- 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,
- SwitchbotModel.LOCK_VISION,
- SwitchbotModel.LOCK_VISION_PRO,
- SwitchbotModel.LOCK_PRO_WIFI,
- ],
- )
- 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,
- SwitchbotModel.LOCK_VISION,
- SwitchbotModel.LOCK_VISION_PRO,
- SwitchbotModel.LOCK_PRO_WIFI,
- ],
- )
- 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,
- SwitchbotModel.LOCK_VISION,
- SwitchbotModel.LOCK_VISION_PRO,
- SwitchbotModel.LOCK_PRO_WIFI,
- ],
- )
- 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,
- SwitchbotModel.LOCK_VISION,
- SwitchbotModel.LOCK_VISION_PRO,
- SwitchbotModel.LOCK_PRO_WIFI,
- ],
- )
- 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,
- SwitchbotModel.LOCK_VISION,
- SwitchbotModel.LOCK_VISION_PRO,
- SwitchbotModel.LOCK_PRO_WIFI,
- ],
- )
- 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,
- SwitchbotModel.LOCK_VISION,
- SwitchbotModel.LOCK_VISION_PRO,
- SwitchbotModel.LOCK_PRO_WIFI,
- ],
- )
- 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,
- SwitchbotModel.LOCK_VISION,
- SwitchbotModel.LOCK_VISION_PRO,
- SwitchbotModel.LOCK_PRO_WIFI,
- ],
- )
- 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(
- lock.SwitchbotSequenceDevice, "_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,
- SwitchbotModel.LOCK_VISION,
- SwitchbotModel.LOCK_VISION_PRO,
- SwitchbotModel.LOCK_PRO_WIFI,
- ],
- )
- 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,
- SwitchbotModel.LOCK_VISION,
- SwitchbotModel.LOCK_VISION_PRO,
- SwitchbotModel.LOCK_PRO_WIFI,
- ],
- )
- 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,
- "half_lock_calibration": False,
- },
- ),
- (
- SwitchbotModel.LOCK_VISION,
- b"\x80\x00\x00\x00\x00\x00",
- {
- "calibration": True,
- "status": LockStatus.LOCKED, # (0x80 & 0b01110000) >> 4 = 0 = LOCKED
- "unlocked_alarm": False,
- },
- ),
- (
- SwitchbotModel.LOCK_VISION_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_PRO_WIFI,
- 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,
- },
- ),
- ],
- )
- 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
- "half_lock_calibration": False,
- },
- ),
- (
- SwitchbotModel.LOCK_PRO_WIFI,
- 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)
- },
- ),
- ],
- )
- 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,
- SwitchbotModel.LOCK_VISION,
- SwitchbotModel.LOCK_VISION_PRO,
- SwitchbotModel.LOCK_PRO_WIFI,
- ],
- )
- 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
- def test_is_half_lock_calibrated():
- """Test is_half_lock_calibrated method."""
- device = create_device_for_command_testing(SwitchbotModel.LOCK_ULTRA)
- device._get_adv_value = Mock(return_value=True)
- assert device.is_half_lock_calibrated() is True
- device._get_adv_value = Mock(return_value=False)
- assert device.is_half_lock_calibrated() is False
- @pytest.mark.asyncio
- async def test_half_lock_calibrated():
- """Test half_lock succeeds when calibrated."""
- device = create_device_for_command_testing(SwitchbotModel.LOCK_ULTRA)
- device._get_adv_value = Mock(side_effect=[True, 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"\x01\x64\x01"),
- ):
- result = await device.half_lock()
- assert result is True
- @pytest.mark.asyncio
- async def test_half_lock_not_calibrated():
- """Test half_lock raises SwitchbotOperationError when not calibrated."""
- device = create_device_for_command_testing(SwitchbotModel.LOCK_ULTRA)
- device._get_adv_value = Mock(return_value=False)
- with pytest.raises(SwitchbotOperationError, match="not calibrated"):
- await device.half_lock()
- @pytest.mark.asyncio
- @pytest.mark.parametrize(
- "model",
- [
- SwitchbotModel.LOCK,
- SwitchbotModel.LOCK_LITE,
- SwitchbotModel.LOCK_PRO,
- SwitchbotModel.LOCK_ULTRA_MAX,
- SwitchbotModel.UWB_LOCK,
- SwitchbotModel.LOCK_NANO,
- SwitchbotModel.LOCK_VISION,
- SwitchbotModel.LOCK_VISION_PRO,
- SwitchbotModel.LOCK_PRO_WIFI,
- ],
- )
- async def test_half_lock_unsupported_model(model: str):
- """Test half_lock raises SwitchbotOperationError on unsupported models."""
- device = create_device_for_command_testing(model)
- with pytest.raises(SwitchbotOperationError, match="not supported"):
- await device.half_lock()
- @pytest.mark.asyncio
- async def test_half_lock():
- """Test half_lock method."""
- device = create_device_for_command_testing(SwitchbotModel.LOCK_ULTRA)
- device._get_adv_value = Mock(side_effect=[True, LockStatus.LOCKED])
- with (
- patch.object(device, "_send_command", return_value=b"\x01\x00") as mock_send,
- patch.object(device, "_enable_notifications", return_value=True),
- patch.object(device, "_get_basic_info", return_value=b"\x01\x64\x01"),
- ):
- result = await device.half_lock()
- assert result is True
- mock_send.assert_awaited_once_with(
- lock.COMMAND_HALF_LOCK[SwitchbotModel.LOCK_ULTRA]
- )
- @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),
- (SwitchbotModel.LOCK_VISION, LockStatus.LOCKED),
- (SwitchbotModel.LOCK_VISION_PRO, LockStatus.LOCKED),
- (SwitchbotModel.LOCK_PRO_WIFI, LockStatus.LOCKED),
- ],
- )
- 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,
- SwitchbotModel.LOCK_VISION,
- SwitchbotModel.LOCK_VISION_PRO,
- SwitchbotModel.LOCK_PRO_WIFI,
- ],
- )
- 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
- @pytest.mark.asyncio
- async def test_get_quick_key():
- """Reading the Quick Key parses the config byte (Lock Ultra)."""
- device = create_device_for_command_testing(SwitchbotModel.LOCK_ULTRA)
- # 0xca = enabled, single press, Lock & Unlock
- with patch.object(
- device, "_send_command", return_value=b"\x01\xca\x00\x00\x00\x80"
- ) as mock_send_command:
- result = await device.get_quick_key()
- mock_send_command.assert_any_call(
- key="570f4f0401",
- retry=device._retry_count,
- )
- assert result == {
- "raw": 0xCA,
- "light": True,
- "sound": True,
- "manual_unlock_linkage": False,
- "enabled": True,
- "double_press": False,
- "function": QuickKeyFunction.LOCK_AND_UNLOCK,
- "motor_speed": 0,
- }
- @pytest.mark.asyncio
- async def test_get_quick_key_failure():
- """A non-success status returns None."""
- device = create_device_for_command_testing(SwitchbotModel.LOCK_ULTRA)
- with patch.object(device, "_send_command", return_value=b"\x00"):
- assert await device.get_quick_key() is None
- @pytest.mark.asyncio
- async def test_get_quick_key_zero_properties():
- """A zero property byte means disabled, single press, and lock only."""
- device = create_device_for_command_testing(SwitchbotModel.LOCK_ULTRA)
- with patch.object(device, "_send_command", return_value=b"\x01\x00"):
- assert await device.get_quick_key() == {
- "raw": 0,
- "light": False,
- "sound": False,
- "manual_unlock_linkage": False,
- "enabled": False,
- "double_press": False,
- "function": QuickKeyFunction.LOCK_ONLY,
- "motor_speed": None,
- }
- @pytest.mark.asyncio
- async def test_set_quick_key_function_update_flag():
- """Bit 1 selects the entire two-bit function field for writing."""
- device = create_device_for_command_testing(SwitchbotModel.LOCK_ULTRA)
- with patch.object(
- device,
- "_send_command",
- return_value=b"\x01\xc9", # echo 0xc9 = Unlock Only
- ) as mock_send_command:
- result = await device.set_quick_key(function=QuickKeyFunction.UNLOCK_ONLY)
- mock_send_command.assert_awaited_once_with(
- key="570f4e0401000201ff", retry=device._retry_count
- )
- assert result is True
- @pytest.mark.asyncio
- async def test_set_quick_key_multiple_fields():
- """Update all Quick Key fields while keeping the default target and speed."""
- device = create_device_for_command_testing(SwitchbotModel.LOCK_ULTRA)
- with patch.object(
- device, "_send_command", return_value=b"\x01\xce"
- ) as mock_send_command:
- result = await device.set_quick_key(
- enabled=True, double_press=True, function=QuickKeyFunction.LOCK_AND_UNLOCK
- )
- mock_send_command.assert_awaited_once_with(
- key="570f4e0401000e0eff", retry=device._retry_count
- )
- assert result is True
- @pytest.mark.asyncio
- @pytest.mark.parametrize(
- ("kwargs", "flags_and_value"),
- [
- ({"enabled": False}, "0800"),
- ({"double_press": False}, "0400"),
- ({"function": QuickKeyFunction.LOCK_ONLY}, "0200"),
- (
- {
- "enabled": False,
- "double_press": False,
- "function": QuickKeyFunction.LOCK_ONLY,
- },
- "0e00",
- ),
- ],
- )
- async def test_set_quick_key_false_and_zero_values(
- kwargs: dict[str, Any], flags_and_value: str
- ):
- """Explicit false and zero values still select their fields for writing."""
- device = create_device_for_command_testing(SwitchbotModel.LOCK_ULTRA)
- with patch.object(
- device, "_send_command", return_value=b"\x01\xc0"
- ) as mock_send_command:
- result = await device.set_quick_key(**kwargs)
- mock_send_command.assert_awaited_once_with(
- key=f"570f4e040100{flags_and_value}ff", retry=device._retry_count
- )
- assert result is True
- @pytest.mark.asyncio
- async def test_set_quick_key_requires_a_field():
- """Calling with nothing to change raises ValueError."""
- device = create_device_for_command_testing(SwitchbotModel.LOCK_ULTRA)
- with pytest.raises(ValueError, match="at least one setting"):
- await device.set_quick_key()
- @pytest.mark.asyncio
- async def test_quick_key_unsupported_model():
- """Quick Key is Lock Ultra only; other models raise."""
- device = create_device_for_command_testing(SwitchbotModel.LOCK)
- with pytest.raises(SwitchbotOperationError):
- await device.get_quick_key()
- with pytest.raises(SwitchbotOperationError):
- await device.set_quick_key(enabled=True)
- @pytest.mark.asyncio
- async def test_get_quick_key_short_response():
- """A success status but a truncated payload returns None."""
- device = create_device_for_command_testing(SwitchbotModel.LOCK_ULTRA)
- with patch.object(device, "_send_command", return_value=b"\x01"):
- assert await device.get_quick_key() is None
- @pytest.mark.asyncio
- async def test_get_quick_key_unknown_function():
- """An undefined 2-bit function value keeps the bits that are unambiguous."""
- device = create_device_for_command_testing(SwitchbotModel.LOCK_ULTRA)
- # 0xcb = enabled, single press, function bits 0b11 (undefined)
- with patch.object(device, "_send_command", return_value=b"\x01\xcb"):
- assert await device.get_quick_key() == {
- "raw": 0xCB,
- "light": True,
- "sound": True,
- "manual_unlock_linkage": False,
- "enabled": True,
- "double_press": False,
- "function": None,
- "motor_speed": None,
- }
- @pytest.mark.asyncio
- @pytest.mark.parametrize(
- ("properties", "light", "sound"),
- [
- (0x0A, False, False),
- (0x4A, False, True),
- (0x8A, True, False),
- (0xCA, True, True),
- ],
- )
- async def test_get_quick_key_light_and_sound_settings(
- properties: int, light: bool, sound: bool
- ):
- """Read light and sound independently of the original Quick Key fields."""
- device = create_device_for_command_testing(SwitchbotModel.LOCK_ULTRA)
- with patch.object(
- device, "_send_command", return_value=bytes([1, properties, 0, 0, 0, 0])
- ):
- assert await device.get_quick_key() == {
- "raw": properties,
- "light": light,
- "sound": sound,
- "manual_unlock_linkage": False,
- "enabled": True,
- "double_press": False,
- "function": QuickKeyFunction.LOCK_AND_UNLOCK,
- "motor_speed": 0,
- }
- @pytest.mark.asyncio
- @pytest.mark.parametrize(
- ("response", "expected"),
- [
- (b"\x01", False),
- (b"\x06", False),
- (b"\x01\x08", True),
- (b"\x06\x08", True),
- (b"\x01\x00", False),
- (b"\x06\x00", False),
- (b"\x00", False),
- (b"\x02", False),
- (b"\x03", False),
- (b"\x05", False),
- ],
- )
- async def test_set_quick_key_command_status(response: bytes, expected: bool):
- """Require a successful status and matching config without another read."""
- device = create_device_for_command_testing(SwitchbotModel.LOCK_ULTRA)
- with patch.object(
- device, "_send_command", return_value=response
- ) as mock_send_command:
- assert await device.set_quick_key(enabled=True) is expected
- mock_send_command.assert_awaited_once_with(
- key="570f4e0401000808ff", retry=device._retry_count
- )
- @pytest.mark.asyncio
- async def test_get_quick_key_manual_unlock_linkage_and_motor_speed():
- """Read primary properties and speed without using the secondary values."""
- device = create_device_for_command_testing(SwitchbotModel.LOCK_ULTRA)
- with patch.object(
- device, "_send_command", return_value=bytes([1, 0xD9, 0xFF, 2, 0, 0])
- ):
- assert await device.get_quick_key() == {
- "raw": 0xD9,
- "light": True,
- "sound": True,
- "manual_unlock_linkage": True,
- "enabled": True,
- "double_press": False,
- "function": QuickKeyFunction.UNLOCK_ONLY,
- "motor_speed": 2,
- }
- @pytest.mark.asyncio
- @pytest.mark.parametrize(
- ("kwargs", "command", "config"),
- [
- ({"light": True}, "570f4e0401008080ff", 0xCA),
- ({"light": False}, "570f4e0401008000ff", 0x4A),
- ({"sound": True}, "570f4e0401004040ff", 0xCA),
- ({"sound": False}, "570f4e0401004000ff", 0x8A),
- ({"manual_unlock_linkage": True}, "570f4e0401001010ff", 0xDA),
- ({"manual_unlock_linkage": False}, "570f4e0401001000ff", 0xCA),
- ],
- )
- async def test_set_quick_key_independent_properties(
- kwargs: dict[str, bool], command: str, config: int
- ):
- """Each basic property can be changed while retaining the Quick Key fields."""
- device = create_device_for_command_testing(SwitchbotModel.LOCK_ULTRA)
- with patch.object(
- device, "_send_command", return_value=bytes([1, config])
- ) as mock_send_command:
- assert await device.set_quick_key(**kwargs) is True
- mock_send_command.assert_awaited_once_with(key=command, retry=device._retry_count)
- @pytest.mark.asyncio
- @pytest.mark.parametrize(
- ("kwargs", "command", "config"),
- [
- (
- {
- "light": True,
- "sound": False,
- "manual_unlock_linkage": True,
- "enabled": False,
- "double_press": True,
- "function": QuickKeyFunction.UNLOCK_ONLY,
- },
- "570f4e040100de95ff",
- 0x95,
- ),
- (
- {
- "light": False,
- "sound": False,
- "manual_unlock_linkage": False,
- "enabled": False,
- "double_press": False,
- "function": QuickKeyFunction.LOCK_ONLY,
- },
- "570f4e040100de00ff",
- 0x20,
- ),
- ],
- )
- async def test_set_quick_key_combines_all_properties(
- kwargs: dict[str, Any], command: str, config: int
- ):
- """Combine all writable config fields without selecting reserved bits."""
- device = create_device_for_command_testing(SwitchbotModel.LOCK_ULTRA)
- with patch.object(
- device, "_send_command", return_value=bytes([1, config])
- ) as mock_send_command:
- assert await device.set_quick_key(**kwargs) is True
- mock_send_command.assert_awaited_once_with(key=command, retry=device._retry_count)
- @pytest.mark.asyncio
- @pytest.mark.parametrize("motor_speed", [0, 1, 2])
- async def test_set_quick_key_motor_speed(motor_speed: int):
- """Change speed alone with a single write command."""
- device = create_device_for_command_testing(SwitchbotModel.LOCK_ULTRA)
- with patch.object(
- device,
- "_send_command",
- return_value=b"\x01\xca",
- ) as mock_send_command:
- assert await device.set_quick_key(motor_speed=motor_speed) is True
- mock_send_command.assert_awaited_once_with(
- key=f"570f4e0401000000{motor_speed:02x}", retry=device._retry_count
- )
- @pytest.mark.asyncio
- async def test_set_quick_key_motor_speed_with_other_properties():
- """Write speed and properties together without an additional read."""
- device = create_device_for_command_testing(SwitchbotModel.LOCK_ULTRA)
- with patch.object(
- device,
- "_send_command",
- return_value=b"\x01\x5d",
- ) as mock_send_command:
- assert (
- await device.set_quick_key(
- light=False,
- sound=True,
- manual_unlock_linkage=True,
- enabled=True,
- double_press=True,
- function=QuickKeyFunction.UNLOCK_ONLY,
- motor_speed=2,
- )
- is True
- )
- mock_send_command.assert_awaited_once_with(
- key="570f4e040100de5d02", retry=device._retry_count
- )
- @pytest.mark.asyncio
- @pytest.mark.parametrize("motor_speed", [-1, 3, 0x100])
- async def test_set_quick_key_invalid_motor_speed(motor_speed: int):
- """Invalid speeds fail before Bluetooth commands are sent."""
- device = create_device_for_command_testing(SwitchbotModel.LOCK_ULTRA)
- with (
- patch.object(device, "_send_command") as mock_send_command,
- pytest.raises(ValueError, match="motor_speed must be"),
- ):
- await device.set_quick_key(motor_speed=motor_speed)
- mock_send_command.assert_not_called()
- @pytest.mark.asyncio
- async def test_set_quick_key_motor_speed_keep():
- """Explicit 0xFF preserves speed without an additional read."""
- device = create_device_for_command_testing(SwitchbotModel.LOCK_ULTRA)
- with patch.object(
- device, "_send_command", return_value=b"\x01\xca"
- ) as mock_send_command:
- assert await device.set_quick_key(light=True, motor_speed=0xFF) is True
- mock_send_command.assert_awaited_once_with(
- key="570f4e0401008080ff", retry=device._retry_count
- )
- @pytest.mark.asyncio
- @pytest.mark.parametrize(
- ("kwargs", "config", "reported"),
- [
- ({"light": True}, 0x00, False),
- ({"light": False}, 0x80, True),
- ({"sound": True}, 0x00, False),
- ({"manual_unlock_linkage": True}, 0x00, False),
- ({"enabled": False}, 0x08, True),
- ({"double_press": True}, 0x00, False),
- ({"function": QuickKeyFunction.LOCK_ONLY}, 0x01, QuickKeyFunction.UNLOCK_ONLY),
- ({"function": QuickKeyFunction.UNLOCK_ONLY}, 0x00, QuickKeyFunction.LOCK_ONLY),
- ({"function": QuickKeyFunction.LOCK_AND_UNLOCK}, 0x03, None),
- ],
- )
- async def test_set_quick_key_config_mismatch(
- kwargs: dict[str, Any],
- config: int,
- reported: bool | QuickKeyFunction | None,
- caplog: pytest.LogCaptureFixture,
- ):
- """Identify the rejected field, requested value, and reported value."""
- device = create_device_for_command_testing(SwitchbotModel.LOCK_ULTRA)
- with patch.object(
- device, "_send_command", return_value=bytes([1, config])
- ) as mock_send_command:
- assert await device.set_quick_key(**kwargs) is False
- mock_send_command.assert_awaited_once()
- setting, expected = next(iter(kwargs.items()))
- assert (
- f"Quick Key {setting} was not applied "
- f"(requested={expected}, reported={reported}, config={config:#04x})"
- ) in caplog.text
- @pytest.mark.asyncio
- async def test_set_quick_key_multiple_config_mismatches(
- caplog: pytest.LogCaptureFixture,
- ):
- """Report every mismatching field in a combined write."""
- device = create_device_for_command_testing(SwitchbotModel.LOCK_ULTRA)
- with patch.object(device, "_send_command", return_value=b"\x01\x00"):
- assert (
- await device.set_quick_key(
- light=True,
- sound=True,
- manual_unlock_linkage=True,
- enabled=True,
- double_press=True,
- function=QuickKeyFunction.UNLOCK_ONLY,
- )
- is False
- )
- messages = [
- record.message
- for record in caplog.records
- if "was not applied" in record.message
- ]
- assert len(messages) == 6
- for setting in (
- "light",
- "sound",
- "manual_unlock_linkage",
- "enabled",
- "double_press",
- "function",
- ):
- assert any(
- f"Quick Key {setting} was not applied" in message for message in messages
- )
- @pytest.mark.asyncio
- @pytest.mark.parametrize("status", [0x00, 0x02, 0x03, 0x04, 0x05, 0x09, 0x0E])
- async def test_set_quick_key_failed_status_logs_code(
- status: int, caplog: pytest.LogCaptureFixture
- ):
- """Keep the device status code in diagnostics for rejected commands."""
- device = create_device_for_command_testing(SwitchbotModel.LOCK_ULTRA)
- with patch.object(device, "_send_command", return_value=bytes([status])):
- assert await device.set_quick_key(enabled=True) is False
- assert (
- f"Quick Key command failed (status={status:#04x}, response={status:02x})"
- in caplog.text
- )
- @pytest.mark.asyncio
- @pytest.mark.parametrize("status", [0x01, 0x06])
- async def test_set_quick_key_missing_config_logs_status(
- status: int, caplog: pytest.LogCaptureFixture
- ):
- """Distinguish a missing config from a rejected command."""
- device = create_device_for_command_testing(SwitchbotModel.LOCK_ULTRA)
- with patch.object(device, "_send_command", return_value=bytes([status])):
- assert await device.set_quick_key(enabled=True) is False
- assert (
- f"Quick Key write response is missing config (status={status:#04x}, response={status:02x})"
- ) in caplog.text
- @pytest.mark.asyncio
- @pytest.mark.parametrize("response", [None, b""])
- async def test_set_quick_key_absent_response_raises(response: bytes | None):
- """Preserve the existing operation error for an absent response."""
- device = create_device_for_command_testing(SwitchbotModel.LOCK_ULTRA)
- with (
- patch.object(device, "_send_command", return_value=response),
- pytest.raises(SwitchbotOperationError, match="Sending command failed"),
- ):
- await device.set_quick_key(enabled=True)
|