Skip to content

Commit

Permalink
fix: handle data corruption (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Dec 10, 2022
1 parent f3e04c8 commit 77f2e9b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 9 deletions.
30 changes: 21 additions & 9 deletions src/bluetooth_adapters/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

from __future__ import annotations

import logging
import time
from dataclasses import dataclass
from typing import Any, Final, TypedDict

from bleak.backends.device import BLEDevice
from bleak.backends.scanner import AdvertisementData

_LOGGER = logging.getLogger(__name__)


@dataclass
class DiscoveredDeviceAdvertisementData:
Expand Down Expand Up @@ -107,16 +110,25 @@ def expire_stale_scanner_discovered_device_advertisement_data(

def discovered_device_advertisement_data_from_dict(
data: DiscoveredDeviceAdvertisementDataDict,
) -> DiscoveredDeviceAdvertisementData:
) -> DiscoveredDeviceAdvertisementData | None:
"""Build discovered_device_advertisement_data dict."""
return DiscoveredDeviceAdvertisementData(
data[CONNECTABLE],
data[EXPIRE_SECONDS],
_deserialize_discovered_device_advertisement_datas(
data[DISCOVERED_DEVICE_ADVERTISEMENT_DATAS]
),
_deserialize_discovered_device_timestamps(data[DISCOVERED_DEVICE_TIMESTAMPS]),
)
try:
return DiscoveredDeviceAdvertisementData(
data[CONNECTABLE],
data[EXPIRE_SECONDS],
_deserialize_discovered_device_advertisement_datas(
data[DISCOVERED_DEVICE_ADVERTISEMENT_DATAS]
),
_deserialize_discovered_device_timestamps(
data[DISCOVERED_DEVICE_TIMESTAMPS]
),
)
except Exception as err: # pylint: disable=broad-except
_LOGGER.exception(
"Error deserializing discovered_device_advertisement_data, adapter startup will be slow: %s",
err,
)
return None


def discovered_device_advertisement_data_to_dict(
Expand Down
33 changes: 33 additions & 0 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,7 @@ def test_discovered_device_advertisement_data_from_dict():
platform_data=("Test Device", ""),
rssi=-50,
)
assert result is not None
out_ble_device = result.discovered_device_advertisement_datas["AA:BB:CC:DD:EE:FF"][
0
]
Expand Down Expand Up @@ -1012,3 +1013,35 @@ def test_expire_stale_scanner_discovered_device_advertisement_data():
not in data["myscanner"]["discovered_device_advertisement_datas"]
)
assert "all_expired" not in data


def test_discovered_device_advertisement_data_from_dict_corrupt(caplog):
"""Test discovered_device_advertisement_data_from_dict with corrupt data."""
now = time.time()
result = discovered_device_advertisement_data_from_dict(
{
"connectable": True,
"discovered_device_advertisement_datas": {
"AA:BB:CC:DD:EE:FF": {
"advertisement_data": { # type: ignore[typeddict-item]
"local_name": "Test " "Device",
"manufacturer_data": {"76": "0215aabbccddeeff"},
"rssi": -50,
"service_data": {
"0000180d-0000-1000-8000-00805f9b34fb": "00000000"
},
"service_uuids": ["0000180d-0000-1000-8000-00805f9b34fb"],
},
"device": { # type: ignore[typeddict-item]
"address": "AA:BB:CC:DD:EE:FF",
"details": {"details": "test"},
"rssi": -50,
},
}
},
"discovered_device_timestamps": {"AA:BB:CC:DD:EE:FF": now},
"expire_seconds": 100,
}
)
assert result is None
assert "Error deserializing discovered_device_advertisement_data" in caplog.text

0 comments on commit 77f2e9b

Please sign in to comment.