test_universal_remote.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. from unittest.mock import AsyncMock
  2. import pytest
  3. from bleak.backends.device import BLEDevice
  4. from switchbot.devices.universal_remote import SwitchbotUniversalRemote
  5. def create_device() -> SwitchbotUniversalRemote:
  6. """Create a Universal Remote device for command testing."""
  7. ble_device = BLEDevice(
  8. address="aa:bb:cc:dd:ee:ff", name="any", details={"rssi": -80}
  9. )
  10. device = SwitchbotUniversalRemote(ble_device)
  11. device._send_command = AsyncMock()
  12. return device
  13. @pytest.mark.asyncio
  14. @pytest.mark.parametrize(
  15. ("response", "expected"),
  16. [
  17. (
  18. b"\x01\x50\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  19. {"battery": 80, "charging": False},
  20. ),
  21. (
  22. b"\x01\x37\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01",
  23. {"battery": 55, "charging": True},
  24. ),
  25. ],
  26. )
  27. async def test_get_basic_info(response: bytes, expected: dict[str, int | bool]) -> None:
  28. """Test get_basic_info parses battery and charging state."""
  29. device = create_device()
  30. device._get_basic_info = AsyncMock(return_value=response)
  31. info = await device.get_basic_info()
  32. assert info == expected
  33. @pytest.mark.asyncio
  34. async def test_get_basic_info_returns_none_on_empty_response() -> None:
  35. """get_basic_info returns None when the device gives no data."""
  36. device = create_device()
  37. device._get_basic_info = AsyncMock(return_value=None)
  38. assert await device.get_basic_info() is None