    def update_metadevices(self):
        """
        Create or update iBeacon, Private_BLE and other meta-devices from
        the received advertisements.

        Note that at this point all the distances etc should be fresh for
        the source devices, so we can just copy values from them to the metadevice.
        However, the sources might not yet be using the metadevice's custom ref_power,
        so their *first* update might have the un-adjusted value after a mac change or
        other initialisation.
        """
        # First seed the metadevice skeletons and set their latest beacon_source entries
        # Private BLE Devices. It will only do anything if the self._do_private_device_init
        # flag is set.
        self.discover_private_ble_metadevices()

        # iBeacon devices should already have their metadevices created.
        # FIXME: irk and ibeacons will fight over their relative ref_power too.

        for metadev in self.metadevices.values():
            # We Expect the first beacon source to be the current one.
            # This is maintained by ibeacon or private_ble metadevice creation/update
            latest_source: str | None = None
            source_device: BermudaDevice | None = None
            if len(metadev.metadevice_sources) > 0:
                latest_source = metadev.metadevice_sources[0]
                if latest_source is not None:
                    source_device = self._get_device(latest_source)

            if latest_source is not None and source_device is not None:
                # Map the source device's scanner list into ours
                metadev.scanners = source_device.scanners

                # Set the source device's ref_power from our own. This will cause
                # the source device and all its scanner entries to update their
                # distance measurements. This won't affect Area wins though, because
                # they are "relative", not absolute.

                # FIXME: This has two potential bugs:
                # - if multiple metadevices share a source, they will
                #   "fight" over their preferred ref_power, if different.
                # - The non-meta device (if tracked) will receive distances
                #   based on the meta device's ref_power.
                # - The non-meta device if tracked will have its own ref_power ignored.
                #
                # None of these are terribly awful, but worth fixing.

                # Note we are setting the ref_power on the source_device, not the
                # individual scanner entries (it will propagate to them though)
                if source_device.ref_power != metadev.ref_power:
                    source_device.set_ref_power(metadev.ref_power)

                # anything that isn't already set to something interesting, overwrite
                # it with the new device's data.
                # Defaults:
                for attribute in [
                    # "create_sensor",  # don't copy this, maybe we're tracking the device alone
                    "name",
                    "name_by_user",
                    "name_devreg",
                    "name_bt_local_name",
                    "name_bt_serviceinfo",
                    "manufacturer",
                    # "options",
                ]:
                    if hasattr(metadev, attribute):
                        if getattr(metadev, attribute) in [None, False]:
                            setattr(metadev, attribute, getattr(source_device, attribute))
                            if attribute.startswith("name"):
                                # One of the name attributes have changed.
                                metadev.make_name()
                    else:
                        _LOGGER.error(
                            "Devices don't have a '%s' attribute, this is a bug.",
                            attribute,
                        )
                # Anything that's VERY interesting, overwrite it regardless of what's already there:
                # INTERESTING:
                for attribute in [
                    "area_distance",
                    "area_id",
                    "area_name",
                    "area_rssi",
                    "area_scanner",
                    "beacon_major",
                    "beacon_minor",
                    "beacon_power",
                    "beacon_unique_id",
                    "beacon_uuid",
                    "connectable",
                    "zone",
                ]:
                    if hasattr(metadev, attribute):
                        setattr(metadev, attribute, getattr(source_device, attribute))
                    else:
                        _LOGGER.error(
                            "Devices don't have a '%s' attribute, this is a bug.",
                            attribute,
                        )

                if source_device.last_seen > metadev.last_seen:
                    # Source is newer than the latest recorded, update last_seen
                    metadev.last_seen = source_device.last_seen

                elif source_device.last_seen == 0:
                    # _LOGGER.debug(
                    #     "New source %s for %s has no stamp yet. This is"
                    #     " expected if it's a fresh Private BLE source.",
                    #     source_device.address,
                    #     metadev.name
                    # )
                    pass
                elif source_device.last_seen < metadev.last_seen:
                    # We should not have a source device that is older than the
                    # current metadevice, so flag this if it occurs.
                    # This caught bug #138, not that I realised it at the time!
                    # (https://github.com/agittins/bermuda/issues/138)
                    _LOGGER.debug(
                        "Using freshest advert from %s for %s but it's still %s seconds too old!",
                        source_device.address,
                        metadev.name,
                        metadev.last_seen - source_device.last_seen,
                    )
Create or update iBeacon, Private_BLE and other meta-devices from
the received advertisements.

Note that at this point all the distances etc should be fresh for
the source devices, so we can just copy values from them to the metadevice.
However, the sources might not yet be using the metadevice's custom ref_power,
so their *first* update might have the un-adjusted value after a mac change or
other initialisation.
custom_components/bermuda/coordinator.py