Skip to content

Commit

Permalink
fix: ensure lazy advertisement uses None when name is not present (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored May 3, 2024
1 parent aeb5f94 commit c300f73
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/habluetooth/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ def scanner_adv_received(self, service_info: BluetoothServiceInfoBleak) -> None:
service_info.service_uuids,
source,
service_info.device,
service_info.advertisement,
service_info._advertisement,
True,
service_info.time,
service_info.tx_power,
Expand Down
18 changes: 17 additions & 1 deletion src/habluetooth/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,28 @@ def __init__(
self.time = time
self.tx_power = tx_power

def __repr__(self) -> str:
"""Return the representation of the object."""
return (
f"<{self.__class__.__name__} "
f"name={self.name} "
f"address={self.address} "
f"rssi={self.rssi} "
f"manufacturer_data={self.manufacturer_data} "
f"service_data={self.service_data} "
f"service_uuids={self.service_uuids} "
f"source={self.source} "
f"connectable={self.connectable} "
f"time={self.time} "
f"tx_power={self.tx_power}>"
)

@property
def advertisement(self) -> AdvertisementData:
"""Get the advertisement data."""
if self._advertisement is None:
self._advertisement = AdvertisementData(
None if self.name == "" else self.name,
None if self.name == "" or self.name == self.address else self.name,
self.manufacturer_data,
self.service_data,
self.service_uuids,
Expand Down
6 changes: 6 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ def test_from_device_and_advertisement_data():
"time": now_monotonic,
"tx_power": -127,
}
assert str(service_info) == (
"<BluetoothServiceInfoBleak name=wohand address=44:44:33:11:23:45 rssi=-127 "
"manufacturer_data={} service_data={} "
"service_uuids=['cba20d00-224d-11e6-9fb8-0002a5d5c51b'] source=local "
f"connectable=True time={now_monotonic} tx_power=-127>"
)


def test_pyobjc_compat():
Expand Down

0 comments on commit c300f73

Please sign in to comment.