test_art_frame_2.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. from unittest.mock import AsyncMock
  2. import pytest
  3. from switchbot import SwitchbotArtFrame2, SwitchbotModel
  4. from switchbot.adv_parser import parse_advertisement_data
  5. from switchbot.adv_parsers.image_frame import process_image_frame
  6. from switchbot.devices.device import SwitchbotOperationError
  7. from .test_adv_parser import generate_advertisement_data, generate_ble_device
  8. @pytest.fixture(params=[SwitchbotArtFrame2])
  9. def device(request):
  10. frame = request.param(
  11. generate_ble_device("AA:BB:CC:DD:EE:FF", "Frame"), "ff", "ff" * 16
  12. )
  13. frame._send_command = AsyncMock(return_value=b"\x01")
  14. frame.update = AsyncMock()
  15. return frame
  16. @pytest.mark.parametrize(
  17. ("model", "suffix"),
  18. [
  19. (SwitchbotModel.ART_FRAME_2, "11f1c0"),
  20. ],
  21. )
  22. @pytest.mark.parametrize("pairing", [0, 1])
  23. def test_discovery(model, suffix, pairing):
  24. adv = parse_advertisement_data(
  25. generate_ble_device("AA:BB:CC:DD:EE:FF", "Frame"),
  26. generate_advertisement_data(
  27. service_data={
  28. "0000fd3d-0000-1000-8000-00805f9b34fb": bytes([0, 0, 54, pairing])
  29. + bytes.fromhex(suffix)
  30. },
  31. manufacturer_data={2409: bytes.fromhex("aabbccddeeff01b634125e")},
  32. ),
  33. )
  34. assert adv.data["modelName"] == model
  35. assert adv.data["data"] == {
  36. "sequence_number": 1,
  37. "battery_charging": True,
  38. "battery": 54,
  39. "image_index": 0x1234,
  40. "display_size": 5,
  41. "display_mode": 1,
  42. "last_network_status": 1,
  43. "orientation": 1,
  44. }
  45. @pytest.mark.parametrize("data", [None] + [bytes(n) for n in range(11)])
  46. def test_short_advertisements(data):
  47. assert process_image_frame(None, data) == {}
  48. @pytest.mark.asyncio
  49. async def test_basic_info(device):
  50. device._get_basic_info = AsyncMock(
  51. return_value=bytes.fromhex("01b607015c34f1010101")
  52. )
  53. assert await device.get_basic_info() == {
  54. "battery_charging": True,
  55. "battery": 54,
  56. "firmware": 0.7,
  57. "hardware": 1,
  58. "display_size": 5,
  59. "display_mode": 1,
  60. "last_network_status": 1,
  61. "current_image_index": 0xF134,
  62. "total_num_of_images": 257,
  63. "orientation": 1,
  64. }
  65. device._send_command.assert_not_awaited()
  66. @pytest.mark.asyncio
  67. @pytest.mark.parametrize("data", [None, b"", bytes(9), bytes(10)])
  68. async def test_invalid_basic_info(device, data):
  69. device._get_basic_info = AsyncMock(return_value=data)
  70. assert await device.get_basic_info() is None
  71. @pytest.mark.asyncio
  72. @pytest.mark.parametrize(
  73. ("method", "command"), [("next_image", "570F7A0701"), ("prev_image", "570F7A0700")]
  74. )
  75. @pytest.mark.parametrize(("status", "expected"), [(b"\x01", True), (b"\x09", False)])
  76. async def test_navigation(device, method, command, status, expected):
  77. device._send_command.return_value = status
  78. assert await getattr(device, method)() is expected
  79. device._send_command.assert_awaited_once_with(command)
  80. @pytest.mark.asyncio
  81. async def test_paginated_bitmap(device):
  82. device._send_command.side_effect = [b"\x01\x02\x00\x81\x01", b"\x01\x02\x01\x02"]
  83. assert await device._get_image_indexes() == [1, 8, 9, 18]
  84. assert [call.args[0] for call in device._send_command.await_args_list] == [
  85. "570F7B0700",
  86. "570F7B0701",
  87. ]
  88. @pytest.mark.asyncio
  89. @pytest.mark.parametrize(
  90. "responses",
  91. [
  92. [None],
  93. [b"\x01"],
  94. [b"\x09\x01\x00\x01"],
  95. [b"\x01\x00\x00\x01"],
  96. [b"\x01\x01\x01\x01"],
  97. [b"\x01\x02\x00\x01", b"\x01\x03\x01\x01"],
  98. [b"\x01\x02\x00\x01", b"\x01\x02\x00\x01"],
  99. [b"\x01\x01\x00" + bytes(2561)],
  100. ],
  101. )
  102. async def test_invalid_bitmap(device, responses):
  103. device._send_command.side_effect = responses
  104. with pytest.raises(SwitchbotOperationError, match="Invalid image bitmap"):
  105. await device._get_image_indexes()
  106. @pytest.mark.asyncio
  107. async def test_random_sparse_image(device):
  108. device.get_basic_info = AsyncMock(return_value={"current_image_index": 1})
  109. device._send_command.side_effect = [
  110. b"\x01\x01\x00\x01" + bytes(31) + b"\x01",
  111. b"\x01",
  112. ]
  113. assert await device.random_image()
  114. device._send_command.assert_awaited_with("570F7A020101")
  115. @pytest.mark.asyncio
  116. @pytest.mark.parametrize("bitmap", [b"\x00", b"\x01"])
  117. async def test_random_without_alternative(device, bitmap):
  118. device.get_basic_info = AsyncMock(return_value={"current_image_index": 1})
  119. device._send_command.return_value = b"\x01\x01\x00" + bitmap
  120. with pytest.raises(RuntimeError, match="No images"):
  121. await device.random_image()
  122. @pytest.mark.asyncio
  123. async def test_random_without_info(device):
  124. device.get_basic_info = AsyncMock(return_value=None)
  125. with pytest.raises(SwitchbotOperationError, match="Failed to retrieve"):
  126. await device.random_image()
  127. device._send_command.assert_not_awaited()
  128. @pytest.mark.asyncio
  129. async def test_set_image(device):
  130. device._send_command.side_effect = [
  131. b"\x01\x01\x00" + bytes(2559) + b"\x80",
  132. b"\x01",
  133. ]
  134. assert await device.set_image(0)
  135. device._send_command.assert_awaited_with("570F7A020050")
  136. @pytest.mark.asyncio
  137. @pytest.mark.parametrize("index", [-1, 1])
  138. async def test_set_out_of_range(device, index):
  139. device._send_command.return_value = b"\x01\x01\x00\x01"
  140. with pytest.raises(ValueError, match="out of range"):
  141. await device.set_image(index)