Skip to content

Commit

Permalink
Improve device names, capture tx_power
Browse files Browse the repository at this point in the history
- try harder to find a suitable name to use for each device
- add tx_power to DeviceScanner vars
  • Loading branch information
agittins committed Oct 10, 2023
1 parent dbbe007 commit 4e9b887
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions custom_components/bermuda/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ def __init__(
self.adapter: str = scandata.scanner.adapter
self.source: str = scandata.scanner.source
self.rssi: float = scandata.advertisement.rssi
self.tx_power: float = scandata.advertisement.tx_power
self.rssi_distance: float = rssi_to_metres(self.rssi)
self.adverts: dict[str, bytes] = scandata.advertisement.service_data.items()

Expand Down Expand Up @@ -329,21 +330,22 @@ async def _async_update_data(self):
# We probably don't need to do all of this every time, but we
# want to catch any changes, eg when the system learns the local
# name etc.
device.name = service_info.device.name
device.local_name = service_info.advertisement.local_name
device.manufacturer = service_info.manufacturer
device.name = device.name or service_info.device.name
device.local_name = (
device.local_name or service_info.advertisement.local_name
)
device.manufacturer = device.manufacturer or service_info.manufacturer
device.connectable = service_info.connectable

# Try to make a nice name for prefname.
# TODO: Add support for user-defined name, especially since the
# device_tracker entry can only be renamed using the editor.
if service_info.advertisement.local_name is not None:
device.prefname = service_info.advertisement.local_name
elif service_info.device.name is not None:
device.prefname = service_info.device.name
else:
# we tried. Fall back to boring...
device.prefname = "bermuda_" + slugify(service_info.address)
if device.prefname is None or device.prefname.startswith(DOMAIN + "_"):
device.prefname = (
device.name
or device.local_name
or DOMAIN + "_" + slugify(device.address)
)

# Work through the scanner entries...
matched_scanners = bluetooth.async_scanner_devices_by_address(
Expand Down

0 comments on commit 4e9b887

Please sign in to comment.