From 4e9b887580445e7a829a719fd3a05ac5d3d5db40 Mon Sep 17 00:00:00 2001 From: Ashley Gittins Date: Tue, 10 Oct 2023 13:00:34 +0000 Subject: [PATCH] Improve device names, capture tx_power - try harder to find a suitable name to use for each device - add tx_power to DeviceScanner vars --- custom_components/bermuda/__init__.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/custom_components/bermuda/__init__.py b/custom_components/bermuda/__init__.py index b100b78..ffb0675 100644 --- a/custom_components/bermuda/__init__.py +++ b/custom_components/bermuda/__init__.py @@ -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() @@ -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(