1
0

test_relay_switch.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 relay_switch
  6. from .test_adv_parser import generate_ble_device
  7. def create_device_for_command_testing(calibration=True, reverse_mode=False):
  8. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  9. relay_switch_device = relay_switch.SwitchbotRelaySwitch(
  10. ble_device, "ff", "ffffffffffffffffffffffffffffffff"
  11. )
  12. relay_switch_device.update_from_advertisement(make_advertisement_data(ble_device))
  13. return relay_switch_device
  14. def make_advertisement_data(ble_device: BLEDevice):
  15. """Set advertisement data with defaults."""
  16. return SwitchBotAdvertisement(
  17. address="aa:bb:cc:dd:ee:ff",
  18. data={
  19. "rawAdvData": b"$X|\x0866G\x81\x00\x00\x001\x00\x00\x00\x00",
  20. "data": {
  21. "switchMode": True,
  22. "sequence_number": 71,
  23. "isOn": True,
  24. "power": 4.9,
  25. "voltage": 0,
  26. "current": 0,
  27. },
  28. "isEncrypted": False,
  29. "model": "<",
  30. "modelFriendlyName": "Relay Switch 1PM",
  31. "modelName": SwitchbotModel.RELAY_SWITCH_1PM,
  32. },
  33. device=ble_device,
  34. rssi=-80,
  35. active=True,
  36. )
  37. @pytest.mark.asyncio
  38. async def test_turn_on():
  39. relay_switch_device = create_device_for_command_testing()
  40. relay_switch_device._send_command = AsyncMock(return_value=b"\x01")
  41. await relay_switch_device.turn_on()
  42. assert relay_switch_device.is_on() is True
  43. @pytest.mark.asyncio
  44. async def test_trun_off():
  45. relay_switch_device = create_device_for_command_testing()
  46. relay_switch_device._send_command = AsyncMock(return_value=b"\x01")
  47. await relay_switch_device.turn_off()
  48. assert relay_switch_device.is_on() is False