test_adv_parser.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. from switchbot import SwitchbotModel
  6. def test_parse_advertisement_data_curtain():
  7. """Test parse_advertisement_data for curtain."""
  8. ble_device = BLEDevice("aa:bb:cc:dd:ee:ff", "any")
  9. adv_data = AdvertisementData(
  10. manufacturer_data={2409: b"\xe7\xabF\xac\x8f\x92|\x0f\x00\x11\x04"},
  11. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"c\xc0X\x00\x11\x04"},
  12. )
  13. result = parse_advertisement_data(ble_device, adv_data)
  14. assert result == SwitchBotAdvertisement(
  15. address="aa:bb:cc:dd:ee:ff",
  16. data={
  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. "paired": True,
  26. },
  27. "isEncrypted": False,
  28. "model": "c",
  29. "modelFriendlyName": "Curtain",
  30. "modelName": SwitchbotModel.CURTAIN,
  31. },
  32. device=ble_device,
  33. )
  34. def test_parse_advertisement_data_paired_curtain():
  35. """Test parse_advertisement_data for a paired curtain."""
  36. ble_device = BLEDevice("aa:bb:cc:dd:ee:ff", "any")
  37. adv_data = AdvertisementData(
  38. manufacturer_data={89: b'\xd1K3\x1f\n\xfd'}, service_data={'00000d00-0000-1000-8000-00805f9b34fb': b'c@X\x00!\x04'}
  39. )
  40. result = parse_advertisement_data(ble_device, adv_data)
  41. assert result == SwitchBotAdvertisement(
  42. address="aa:bb:cc:dd:ee:ff",
  43. data={
  44. "rawAdvData": b'c@X\x00!\x04',
  45. "data": {
  46. "calibration": True,
  47. "battery": 88,
  48. "inMotion": False,
  49. "position": 100,
  50. "lightLevel": 2,
  51. "deviceChain": 1,
  52. "paired": True,
  53. },
  54. "isEncrypted": False,
  55. "model": "c",
  56. "modelFriendlyName": "Curtain",
  57. "modelName": SwitchbotModel.CURTAIN,
  58. },
  59. device=ble_device,
  60. )
  61. # Paired Curtain
  62. # AdvertisementData(local_name='WoCurtain', manufacturer_data={89: b'\xd1K3\x1f\n\xfd'}, service_data={'00000d00-0000-1000-8000-00805f9b34fb': b'c@X\x00!\x04'}, service_uuids=['cba20d00-224d-11e6-9fb8-0002a5d5c51b']) connectable: True match: {'switchbot'} rssi: -40
  63. def test_parse_advertisement_data_unpaired_curtain():
  64. """Test parse_advertisement_data for an unpaired curtain."""
  65. ble_device = BLEDevice("aa:bb:cc:dd:ee:ff", "any")
  66. adv_data = AdvertisementData(
  67. manufacturer_data={89: b'\xd1K3\x1f\n\xfd'}, service_data={'00000d00-0000-1000-8000-00805f9b34fb': b'c\xd0X\x00!\x04'}
  68. )
  69. result = parse_advertisement_data(ble_device, adv_data)
  70. assert result == SwitchBotAdvertisement(
  71. address="aa:bb:cc:dd:ee:ff",
  72. data={
  73. "rawAdvData": b'c\xd0X\x00!\x04',
  74. "data": {
  75. "calibration": True,
  76. "battery": 88,
  77. "inMotion": False,
  78. "position": 100,
  79. "lightLevel": 2,
  80. "deviceChain": 1,
  81. "paired": False,
  82. },
  83. "isEncrypted": False,
  84. "model": "c",
  85. "modelFriendlyName": "Curtain",
  86. "modelName": SwitchbotModel.CURTAIN,
  87. },
  88. device=ble_device,
  89. )
  90. # Unpaired Curtain
  91. # AdvertisementData(local_name='WoCurtain', manufacturer_data={89: b'\xd1K3\x1f\n\xfd'}, service_data={'00000d00-0000-1000-8000-00805f9b34fb': b'c\xd0X\x00!\x04'}, service_uuids=['cba20d00-224d-11e6-9fb8-0002a5d5c51b']) connectable: True match: {'switchbot'} rssi: -40
  92. def test_parse_advertisement_data_empty():
  93. """Test parse_advertisement_data with empty data does not blow up."""
  94. ble_device = BLEDevice("aa:bb:cc:dd:ee:ff", "any")
  95. adv_data = AdvertisementData(
  96. manufacturer_data={2409: b"\xe7\xabF\xac\x8f\x92|\x0f\x00\x11\x04"},
  97. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b""},
  98. )
  99. result = parse_advertisement_data(ble_device, adv_data)
  100. assert result is None
  101. def test_new_bot_firmware():
  102. """Test parsing adv data from new bot firmware."""
  103. ble_device = BLEDevice("aa:bb:cc:dd:ee:ff", "any")
  104. adv_data = AdvertisementData(
  105. manufacturer_data={89: b"\xd8.\xad\xcd\r\x85"},
  106. service_data={"00000d00-0000-1000-8000-00805f9b34fb": b"H\x10\xe1"},
  107. service_uuids=["CBA20D00-224D-11E6-9FB8-0002A5D5C51B"],
  108. )
  109. result = parse_advertisement_data(ble_device, adv_data)
  110. assert result == SwitchBotAdvertisement(
  111. address="aa:bb:cc:dd:ee:ff",
  112. data={
  113. "rawAdvData": b"H\x10\xe1",
  114. "data": {"switchMode": False, "isOn": False, "battery": 97},
  115. "model": "H",
  116. "isEncrypted": False,
  117. "modelFriendlyName": "Bot",
  118. "modelName": SwitchbotModel.BOT,
  119. },
  120. device=ble_device,
  121. )