| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487 |
- import logging
- from typing import Any
- from unittest.mock import AsyncMock, Mock, 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)]
- 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)
- @pytest.mark.parametrize(
- "model",
- [
- SwitchbotModel.LOCK,
- SwitchbotModel.LOCK_LITE,
- SwitchbotModel.LOCK_PRO,
- SwitchbotModel.LOCK_ULTRA,
- SwitchbotModel.LOCK_ULTRA_MAX,
- SwitchbotModel.UWB_LOCK,
- 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_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_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_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_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_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_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_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)
|