diff --git a/src/bluetooth_data_tools/__init__.py b/src/bluetooth_data_tools/__init__.py index d28b59e..ef4dc09 100644 --- a/src/bluetooth_data_tools/__init__.py +++ b/src/bluetooth_data_tools/__init__.py @@ -8,6 +8,14 @@ L_PACK = Struct(">L") +__all__ = [ + "address_to_bytes", + "manufacturer_data_to_raw", + "newest_manufacturer_data", + "human_readable_name", + "short_address", +] + def short_address(address: str) -> str: """Convert a Bluetooth address to a short address.""" @@ -15,6 +23,11 @@ def short_address(address: str) -> str: return f"{results[-2].upper()}{results[-1].upper()}"[-4:] +def human_readable_name(name: str | None, local_name: str, address: str) -> str: + """Return a human readable name for the given name, local_name, and address.""" + return f"{name or local_name} ({short_address(address)})" + + def newest_manufacturer_data(manufacturer_data: dict[int, bytes]) -> bytes | None: """Return the raw data from manufacturer data.""" if manufacturer_data and (last_id := list(manufacturer_data)[-1]): diff --git a/tests/test_init.py b/tests/test_init.py index 05be7c9..0df1e8f 100644 --- a/tests/test_init.py +++ b/tests/test_init.py @@ -1,7 +1,9 @@ from bluetooth_data_tools import ( address_to_bytes, + human_readable_name, manufacturer_data_to_raw, newest_manufacturer_data, + short_address, ) @@ -21,3 +23,14 @@ def test_manufacturer_data_to_raw(): manufacturer_data_to_raw(1, b"\x01\x02\x03\x04") == b"\x00\x00\x01\x00\x01\x02\x03\x04" ) + + +def test_short_address(): + assert short_address("AA:BB:CC:DD:EE:FF") == "EEFF" + + +def test_human_readable_name(): + assert ( + human_readable_name("My Device", "Your Device", "AA:BB:CC:DD:EE:FF") + == "My Device (EEFF)" + )