    def _refresh_area_by_min_distance(self, device: BermudaDevice):
        """Very basic Area setting by finding closest beacon to a given device."""
        closest_scanner: BermudaDeviceScanner | None = None
        _max_radius = self.options.get(CONF_MAX_RADIUS, DEFAULT_MAX_RADIUS)

        if device.area_scanner is not None:
            closest_scanner = device.area_scanner  # The one to beat.

        for scanner in device.scanners.values():
            # Check each scanner and keep note of the closest one based on rssi_distance.
            # Note that rssi_distance is smoothed/filtered, and might be None if the last
            # reading was old enough that our algo decides it's "away".
            if (
                scanner.rssi_distance is not None  # We have a valid distance
                and scanner.rssi_distance < _max_radius  # It's within the max radius
            ):
                # We're a chance, let's see if we win...
                if (
                    closest_scanner is None  # No existing candidate, we are in the lead.
                    or closest_scanner.rssi_distance is None  # existing winner has no distance
                    or scanner.rssi_distance < closest_scanner.rssi_distance  # we are closer.
                ):
                    # We are the closest, valid scanner.
                    # TODO: At this point, it might be worth looking at the velocity and other history,
                    #       to see if we should *not* take over the area, as maybe we're in a fringe
                    #       area and some hysterisis is in order. but don't sacrifice responsiveness!
                    #
                    closest_scanner = scanner

        # Apply the newly-found closest scanner (or apply None if we didn't find one)
        device.apply_scanner_selection(closest_scanner)
Very basic Area setting by finding closest beacon to a given device.
custom_components/bermuda/coordinator.py