From a01f18a3ac38933e3ffca9cb2f22a8bcfc97e74b Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 29 Sep 2022 08:37:31 -1000 Subject: [PATCH] Handle short local names from esphome proxies (#79321) --- .../components/esphome/bluetooth/scanner.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/esphome/bluetooth/scanner.py b/homeassistant/components/esphome/bluetooth/scanner.py index fbd5f185907..36138192f8f 100644 --- a/homeassistant/components/esphome/bluetooth/scanner.py +++ b/homeassistant/components/esphome/bluetooth/scanner.py @@ -83,15 +83,23 @@ class ESPHomeScanner(BaseHaScanner): """Call the registered callback.""" now = time.monotonic() address = ":".join(TWO_CHAR.findall("%012X" % adv.address)) # must be upper + name = adv.name + if prev_discovery := self._discovered_devices.get(address): + # If the last discovery had the full local name + # and this one doesn't, keep the old one as we + # always want the full local name over the short one + if len(prev_discovery.name) > len(adv.name): + name = prev_discovery.name + advertisement_data = AdvertisementData( # type: ignore[no-untyped-call] - local_name=None if adv.name == "" else adv.name, + local_name=None if name == "" else name, manufacturer_data=adv.manufacturer_data, service_data=adv.service_data, service_uuids=adv.service_uuids, ) device = BLEDevice( # type: ignore[no-untyped-call] address=address, - name=adv.name, + name=name, details=self._details, rssi=adv.rssi, )