1
0

test_blind_tilt.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. from unittest.mock import AsyncMock
  2. import pytest
  3. from bleak.backends.device import BLEDevice
  4. from switchbot import SwitchBotAdvertisement, SwitchbotModel
  5. from switchbot.devices import blind_tilt
  6. from switchbot.devices.base_cover import COVER_EXT_SUM_KEY
  7. from .test_adv_parser import generate_ble_device
  8. def create_device_for_command_testing(
  9. position=50, calibration=True, reverse_mode=False
  10. ):
  11. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  12. curtain_device = blind_tilt.SwitchbotBlindTilt(
  13. ble_device, reverse_mode=reverse_mode
  14. )
  15. curtain_device.update_from_advertisement(
  16. make_advertisement_data(ble_device, True, position, calibration)
  17. )
  18. curtain_device._send_multiple_commands = AsyncMock()
  19. curtain_device.update = AsyncMock()
  20. return curtain_device
  21. def make_advertisement_data(
  22. ble_device: BLEDevice, in_motion: bool, position: int, calibration: bool = True
  23. ):
  24. """Set advertisement data with defaults."""
  25. return SwitchBotAdvertisement(
  26. address="aa:bb:cc:dd:ee:ff",
  27. data={
  28. "rawAdvData": b"c\xc0X\x00\x11\x04",
  29. "data": {
  30. "calibration": calibration,
  31. "battery": 88,
  32. "inMotion": in_motion,
  33. "tilt": position,
  34. "lightLevel": 1,
  35. "deviceChain": 1,
  36. },
  37. "isEncrypted": False,
  38. "model": "c",
  39. "modelFriendlyName": "Curtain",
  40. "modelName": SwitchbotModel.CURTAIN,
  41. },
  42. device=ble_device,
  43. rssi=-80,
  44. active=True,
  45. )
  46. @pytest.mark.asyncio
  47. async def test_open():
  48. blind_device = create_device_for_command_testing()
  49. await blind_device.open()
  50. blind_device._send_multiple_commands.assert_awaited_once_with(blind_tilt.OPEN_KEYS)
  51. @pytest.mark.asyncio
  52. @pytest.mark.parametrize(
  53. "position,keys", [(5, blind_tilt.CLOSE_DOWN_KEYS), (55, blind_tilt.CLOSE_UP_KEYS)]
  54. )
  55. async def test_close(position, keys):
  56. blind_device = create_device_for_command_testing(position=position)
  57. await blind_device.close()
  58. blind_device._send_multiple_commands.assert_awaited_once_with(keys)
  59. @pytest.mark.asyncio
  60. async def test_get_basic_info_returns_none_when_no_data():
  61. blind_device = create_device_for_command_testing()
  62. blind_device._get_basic_info = AsyncMock(return_value=None)
  63. assert await blind_device.get_basic_info() is None
  64. @pytest.mark.asyncio
  65. @pytest.mark.parametrize(
  66. "reverse_mode,data,result",
  67. [
  68. (
  69. False,
  70. bytes([0, 1, 10, 2, 255, 255, 50, 4]),
  71. [1, 1, 1, 1, 1, True, False, False, True, 50, 4],
  72. ),
  73. (
  74. False,
  75. bytes([0, 1, 10, 2, 0, 0, 50, 4]),
  76. [1, 1, 0, 0, 0, False, False, False, False, 50, 4],
  77. ),
  78. (
  79. False,
  80. bytes([0, 1, 10, 2, 0, 1, 50, 4]),
  81. [1, 1, 0, 0, 1, False, True, False, True, 50, 4],
  82. ),
  83. (
  84. True,
  85. bytes([0, 1, 10, 2, 255, 255, 50, 4]),
  86. [1, 1, 1, 1, 1, True, False, True, False, 50, 4],
  87. ),
  88. (
  89. True,
  90. bytes([0, 1, 10, 2, 0, 0, 50, 4]),
  91. [1, 1, 0, 0, 0, False, False, False, False, 50, 4],
  92. ),
  93. (
  94. True,
  95. bytes([0, 1, 10, 2, 0, 1, 50, 4]),
  96. [1, 1, 0, 0, 1, False, True, False, True, 50, 4],
  97. ),
  98. ],
  99. )
  100. async def test_get_basic_info(reverse_mode, data, result):
  101. blind_device = create_device_for_command_testing(reverse_mode=reverse_mode)
  102. blind_device._get_basic_info = AsyncMock(return_value=data)
  103. info = await blind_device.get_basic_info()
  104. assert info["battery"] == result[0]
  105. assert info["firmware"] == result[1]
  106. assert info["light"] == result[2]
  107. assert info["fault"] == result[2]
  108. assert info["solarPanel"] == result[3]
  109. assert info["calibration"] == result[3]
  110. assert info["calibrated"] == result[3]
  111. assert info["inMotion"] == result[4]
  112. assert info["motionDirection"]["opening"] == result[5]
  113. assert info["motionDirection"]["closing"] == result[6]
  114. assert info["motionDirection"]["up"] == result[7]
  115. assert info["motionDirection"]["down"] == result[8]
  116. assert info["tilt"] == result[9]
  117. assert info["timers"] == result[10]
  118. @pytest.mark.asyncio
  119. async def test_get_extended_info_summary_sends_command():
  120. blind_device = create_device_for_command_testing()
  121. blind_device._send_command = AsyncMock()
  122. await blind_device.get_extended_info_summary()
  123. blind_device._send_command.assert_awaited_once_with(key=COVER_EXT_SUM_KEY)
  124. @pytest.mark.asyncio
  125. @pytest.mark.parametrize("data_value", [(None), (b"\x07"), (b"\x00")])
  126. async def test_get_extended_info_summary_returns_none_when_bad_data(data_value):
  127. blind_device = create_device_for_command_testing()
  128. blind_device._send_command = AsyncMock(return_value=data_value)
  129. assert await blind_device.get_extended_info_summary() is None
  130. @pytest.mark.asyncio
  131. @pytest.mark.parametrize(
  132. "data,result", [(bytes([0, 0]), False), (bytes([0, 255]), True)]
  133. )
  134. async def test_get_extended_info_summary(data, result):
  135. blind_device = create_device_for_command_testing()
  136. blind_device._send_command = AsyncMock(return_value=data)
  137. ext_result = await blind_device.get_extended_info_summary()
  138. assert ext_result["device0"]["light"] == result
  139. @pytest.mark.parametrize("reverse_mode", [(True), (False)])
  140. def test_device_passive_opening(reverse_mode):
  141. """Test passive opening advertisement."""
  142. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  143. curtain_device = blind_tilt.SwitchbotBlindTilt(
  144. ble_device, reverse_mode=reverse_mode
  145. )
  146. curtain_device.update_from_advertisement(
  147. make_advertisement_data(ble_device, True, 0)
  148. )
  149. curtain_device.update_from_advertisement(
  150. make_advertisement_data(ble_device, True, 10)
  151. )
  152. assert curtain_device.is_opening() is True
  153. assert curtain_device.is_closing() is False
  154. @pytest.mark.parametrize("reverse_mode", [(True), (False)])
  155. def test_device_passive_closing(reverse_mode):
  156. """Test passive closing advertisement."""
  157. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  158. curtain_device = blind_tilt.SwitchbotBlindTilt(
  159. ble_device, reverse_mode=reverse_mode
  160. )
  161. curtain_device.update_from_advertisement(
  162. make_advertisement_data(ble_device, True, 100)
  163. )
  164. curtain_device.update_from_advertisement(
  165. make_advertisement_data(ble_device, True, 90)
  166. )
  167. assert curtain_device.is_opening() is False
  168. assert curtain_device.is_closing() is True
  169. @pytest.mark.parametrize("reverse_mode", [(True), (False)])
  170. def test_device_passive_opening_then_stop(reverse_mode):
  171. """Test passive stopped after opening advertisement."""
  172. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  173. curtain_device = blind_tilt.SwitchbotBlindTilt(
  174. ble_device, reverse_mode=reverse_mode
  175. )
  176. curtain_device.update_from_advertisement(
  177. make_advertisement_data(ble_device, True, 0)
  178. )
  179. curtain_device.update_from_advertisement(
  180. make_advertisement_data(ble_device, True, 10)
  181. )
  182. curtain_device.update_from_advertisement(
  183. make_advertisement_data(ble_device, False, 10)
  184. )
  185. assert curtain_device.is_opening() is False
  186. assert curtain_device.is_closing() is False
  187. @pytest.mark.parametrize("reverse_mode", [(True), (False)])
  188. def test_device_passive_closing_then_stop(reverse_mode):
  189. """Test passive stopped after closing advertisement."""
  190. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  191. curtain_device = blind_tilt.SwitchbotBlindTilt(
  192. ble_device, reverse_mode=reverse_mode
  193. )
  194. curtain_device.update_from_advertisement(
  195. make_advertisement_data(ble_device, True, 100)
  196. )
  197. curtain_device.update_from_advertisement(
  198. make_advertisement_data(ble_device, True, 90)
  199. )
  200. curtain_device.update_from_advertisement(
  201. make_advertisement_data(ble_device, False, 90)
  202. )
  203. assert curtain_device.is_opening() is False
  204. assert curtain_device.is_closing() is False