123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- from switchbot.adv_parser import parse_advertisement_data
- from bleak.backends.scanner import AdvertisementData
- from bleak.backends.device import BLEDevice
- from switchbot.models import SwitchBotAdvertisement
- from switchbot import SwitchbotModel
- def test_parse_advertisement_data_curtain():
- """Test parse_advertisement_data for curtain."""
- ble_device = BLEDevice("aa:bb:cc:dd:ee:ff", "any")
- adv_data = AdvertisementData(
- manufacturer_data={2409: b"\xe7\xabF\xac\x8f\x92|\x0f\x00\x11\x04"},
- service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"c\xc0X\x00\x11\x04"},
- )
- result = parse_advertisement_data(ble_device, adv_data)
- assert result == SwitchBotAdvertisement(
- address="aa:bb:cc:dd:ee:ff",
- data={
- "rawAdvData": b"c\xc0X\x00\x11\x04",
- "data": {
- "calibration": True,
- "battery": 88,
- "inMotion": False,
- "position": 100,
- "lightLevel": 1,
- "deviceChain": 1,
- "paired": True,
- },
- "isEncrypted": False,
- "model": "c",
- "modelFriendlyName": "Curtain",
- "modelName": SwitchbotModel.CURTAIN,
- },
- device=ble_device,
- )
- def test_parse_advertisement_data_paired_curtain():
- """Test parse_advertisement_data for a paired curtain."""
- ble_device = BLEDevice("aa:bb:cc:dd:ee:ff", "any")
- adv_data = AdvertisementData(
- manufacturer_data={89: b'\xd1K3\x1f\n\xfd'}, service_data={'00000d00-0000-1000-8000-00805f9b34fb': b'c@X\x00!\x04'}
- )
- result = parse_advertisement_data(ble_device, adv_data)
- assert result == SwitchBotAdvertisement(
- address="aa:bb:cc:dd:ee:ff",
- data={
- "rawAdvData": b'c@X\x00!\x04',
- "data": {
- "calibration": True,
- "battery": 88,
- "inMotion": False,
- "position": 100,
- "lightLevel": 2,
- "deviceChain": 1,
- "paired": True,
- },
- "isEncrypted": False,
- "model": "c",
- "modelFriendlyName": "Curtain",
- "modelName": SwitchbotModel.CURTAIN,
- },
- device=ble_device,
- )
- # Paired Curtain
- # 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
- def test_parse_advertisement_data_unpaired_curtain():
- """Test parse_advertisement_data for an unpaired curtain."""
- ble_device = BLEDevice("aa:bb:cc:dd:ee:ff", "any")
- adv_data = AdvertisementData(
- manufacturer_data={89: b'\xd1K3\x1f\n\xfd'}, service_data={'00000d00-0000-1000-8000-00805f9b34fb': b'c\xd0X\x00!\x04'}
- )
- result = parse_advertisement_data(ble_device, adv_data)
- assert result == SwitchBotAdvertisement(
- address="aa:bb:cc:dd:ee:ff",
- data={
- "rawAdvData": b'c\xd0X\x00!\x04',
- "data": {
- "calibration": True,
- "battery": 88,
- "inMotion": False,
- "position": 100,
- "lightLevel": 2,
- "deviceChain": 1,
- "paired": False,
- },
- "isEncrypted": False,
- "model": "c",
- "modelFriendlyName": "Curtain",
- "modelName": SwitchbotModel.CURTAIN,
- },
- device=ble_device,
- )
- # Unpaired Curtain
- # 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
- def test_parse_advertisement_data_empty():
- """Test parse_advertisement_data with empty data does not blow up."""
- ble_device = BLEDevice("aa:bb:cc:dd:ee:ff", "any")
- adv_data = AdvertisementData(
- manufacturer_data={2409: b"\xe7\xabF\xac\x8f\x92|\x0f\x00\x11\x04"},
- service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b""},
- )
- result = parse_advertisement_data(ble_device, adv_data)
- assert result is None
- def test_new_bot_firmware():
- """Test parsing adv data from new bot firmware."""
- ble_device = BLEDevice("aa:bb:cc:dd:ee:ff", "any")
- adv_data = AdvertisementData(
- manufacturer_data={89: b"\xd8.\xad\xcd\r\x85"},
- service_data={"00000d00-0000-1000-8000-00805f9b34fb": b"H\x10\xe1"},
- service_uuids=["CBA20D00-224D-11E6-9FB8-0002A5D5C51B"],
- )
- result = parse_advertisement_data(ble_device, adv_data)
- assert result == SwitchBotAdvertisement(
- address="aa:bb:cc:dd:ee:ff",
- data={
- "rawAdvData": b"H\x10\xe1",
- "data": {"switchMode": False, "isOn": False, "battery": 97},
- "model": "H",
- "isEncrypted": False,
- "modelFriendlyName": "Bot",
- "modelName": SwitchbotModel.BOT,
- },
- device=ble_device,
- )
|