test_adv_parser.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. from switchbot.adv_parser import parse_advertisement_data
  2. from bleak.backends.scanner import AdvertisementData
  3. from bleak.backends.device import BLEDevice
  4. from switchbot.models import SwitchBotAdvertisement
  5. def test_parse_advertisement_data_curtain():
  6. """Test parse_advertisement_data for curtain."""
  7. ble_device = BLEDevice("aa:bb:cc:dd:ee:ff", "any")
  8. adv_data = AdvertisementData(
  9. manufacturer_data={2409: b"\xe7\xabF\xac\x8f\x92|\x0f\x00\x11\x04"},
  10. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"c\xc0X\x00\x11\x04"},
  11. )
  12. result = parse_advertisement_data(ble_device, adv_data)
  13. assert result == SwitchBotAdvertisement(
  14. address="aa:bb:cc:dd:ee:ff",
  15. data={
  16. "address": "aa:bb:cc:dd:ee:ff",
  17. "rawAdvData": b"c\xc0X\x00\x11\x04",
  18. "data": {
  19. "calibration": True,
  20. "battery": 88,
  21. "inMotion": False,
  22. "position": 100,
  23. "lightLevel": 1,
  24. "deviceChain": 1,
  25. "rssi": 0,
  26. },
  27. "isEncrypted": False,
  28. "model": "c",
  29. "modelFriendlyName": "Curtain",
  30. "modelName": "WoCurtain",
  31. },
  32. device=ble_device,
  33. )
  34. def test_parse_advertisement_data_empty():
  35. """Test parse_advertisement_data with empty data does not blow up."""
  36. ble_device = BLEDevice("aa:bb:cc:dd:ee:ff", "any")
  37. adv_data = AdvertisementData(
  38. manufacturer_data={2409: b"\xe7\xabF\xac\x8f\x92|\x0f\x00\x11\x04"},
  39. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b""},
  40. )
  41. result = parse_advertisement_data(ble_device, adv_data)
  42. assert result is None