Skip to content

Commit

Permalink
feat: add update_distance method (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Sep 23, 2022
1 parent 3473819 commit 56edbb5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/ibeacon_ble/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ class iBeaconAdvertisement:
source: str
distance: int | None

def update_rssi(self, rssi: int) -> None:
"""Update the RSSI and distance."""
self.rssi = rssi
distance = calculate_distance_meters(self.power, rssi)
self.distance = int(round(distance)) if distance is not None else None


def is_ibeacon_service_info(service_info: BluetoothServiceInfo) -> bool:
"""Return True if the service info is an iBeacon."""
Expand Down
4 changes: 4 additions & 0 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ def test_parse():
assert parsed.distance == 1
assert parsed.uuid == UUID("426c7565-4368-6172-6d42-6561636f6e73")

parsed.update_rssi(-70)
assert parsed.rssi == -70
assert parsed.distance == 3


def test_not_parse():
parsed = parse(NOT_IBEACON_SERVIE_INFO)
Expand Down

0 comments on commit 56edbb5

Please sign in to comment.