# Buggy code
async def get_services(self) -> BleakGATTServiceCollection:
    """Get all services registered for this GATT server.

    Returns:
       A :py:class:`bleak.backends.service.BleakGATTServiceCollection` with this device's services tree.

    """
    if self._services_resolved:
        return self.services

    while True:
        properties = await self._get_device_properties()
        services_resolved = properties.get("ServicesResolved", False)
        if services_resolved:
            break
        await asyncio.sleep(0.02, loop=self.loop)

    logger.debug("Get Services...")
    objs = await get_managed_objects(
        self._bus, self.loop, self._device_path + "/service"
    )

    # There is no guarantee that services are listed before characteristics
    # Managed Objects dict.
    # Need multiple iterations to construct the Service Collection

    _chars, _descs = [], []

    for object_path, interfaces in objs.items():
        logger.debug(utils.format_GATT_object(object_path, interfaces))
        if defs.GATT_SERVICE_INTERFACE in interfaces:
            service = interfaces.get(defs.GATT_SERVICE_INTERFACE)
            self.services.add_service(BleakGATTServiceBlueZDBus(service, object_path))
        elif defs.GATT_CHARACTERISTIC_INTERFACE in interfaces:
            char = interfaces.get(defs.GATT_CHARACTERISTIC_INTERFACE)
            _chars.append([char, object_path])
        elif defs.GATT_DESCRIPTOR_INTERFACE in interfaces:
            desc = interfaces.get(defs.GATT_DESCRIPTOR_INTERFACE)
            _descs.append([desc, object_path])

    for char, object_path in _chars:
        _service = list(filter(lambda x: x.path == char["Service"], self.services))
        self.services.add_characteristic(
            BleakGATTCharacteristicBlueZDBus(char, object_path, _service[0].uuid)
        )
        self._char_path_to_uuid[object_path] = char.get("UUID")

    for desc, object_path in _descs:
        _characteristic = list(
            filter(
                lambda x: x.path == desc["Characteristic"],
                self.services.characteristics.values(),
            )
        )
        self.services.add_descriptor(
            BleakGATTDescriptorBlueZDBus(desc, object_path, _characteristic[0].uuid)
        )

    self._services_resolved = True
    return self.services


# Traceback (bleak.exc.BleakError)
/Users/zaytsev/PycharmProjects/bluetooth/venv/bin/python /Users/zaytsev/PycharmProjects/bluetooth/bleak_demo.py
DEBUG:bleak.backends.corebluetooth.CentralManagerDelegate:Bluetooth powered on
DEBUG:bleak.backends.corebluetooth.CentralManagerDelegate:Discovered device A0E49DB2-B7F1-4A65-AB2E-D75121192329: Unknown @ RSSI: -79
A0E49DB2-B7F1-4A65-AB2E-D75121192329: HUAWEI Band 2-b6e
DEBUG:bleak.backends.corebluetooth.client:Connecting to BLE device @ A0E49DB2-B7F1-4A65-AB2E-D75121192329
DEBUG:bleak.backends.corebluetooth.CentralManagerDelegate:Successfully connected to device uuid A0E49DB2-B7F1-4A65-AB2E-D75121192329
WARNING:bleak.backends.corebluetooth.PeripheralDelegate:PeripheralDelegate is not compliant
DEBUG:bleak.backends.corebluetooth.client:Retrieving services...
DEBUG:bleak.backends.corebluetooth.PeripheralDelegate:Services discovered
DEBUG:bleak.backends.corebluetooth.client:Retrieving characteristics for service 180F
DEBUG:bleak.backends.corebluetooth.PeripheralDelegate:Characteristics discovered
DEBUG:bleak.backends.corebluetooth.client:Retrieving descriptors for characteristic 2A19
DEBUG:bleak.backends.corebluetooth.PeripheralDelegate:Descriptor discovered 2A19
DEBUG:bleak.backends.corebluetooth.client:Retrieving characteristics for service 180A
DEBUG:bleak.backends.corebluetooth.PeripheralDelegate:Characteristics discovered
DEBUG:bleak.backends.corebluetooth.client:Retrieving descriptors for characteristic 2A29
DEBUG:bleak.backends.corebluetooth.PeripheralDelegate:Descriptor discovered 2A29
DEBUG:bleak.backends.corebluetooth.client:Retrieving descriptors for characteristic 2A24
DEBUG:bleak.backends.corebluetooth.PeripheralDelegate:Descriptor discovered 2A24
DEBUG:bleak.backends.corebluetooth.client:Retrieving descriptors for characteristic 2A26
DEBUG:bleak.backends.corebluetooth.PeripheralDelegate:Descriptor discovered 2A26
DEBUG:bleak.backends.corebluetooth.client:Retrieving descriptors for characteristic 2A28
DEBUG:bleak.backends.corebluetooth.PeripheralDelegate:Descriptor discovered 2A28
DEBUG:bleak.backends.corebluetooth.client:Retrieving descriptors for characteristic 2A50
DEBUG:bleak.backends.corebluetooth.PeripheralDelegate:Descriptor discovered 2A50
DEBUG:bleak.backends.corebluetooth.client:Retrieving characteristics for service 180D
DEBUG:bleak.backends.corebluetooth.PeripheralDelegate:Characteristics discovered
DEBUG:bleak.backends.corebluetooth.client:Retrieving descriptors for characteristic 2A37
DEBUG:bleak.backends.corebluetooth.PeripheralDelegate:Descriptor discovered 2A37
DEBUG:bleak.backends.corebluetooth.client:Retrieving descriptors for characteristic 2A38
DEBUG:bleak.backends.corebluetooth.PeripheralDelegate:Descriptor discovered 2A38
DEBUG:bleak.backends.corebluetooth.client:Retrieving characteristics for service FE86
DEBUG:bleak.backends.corebluetooth.PeripheralDelegate:Characteristics discovered
DEBUG:bleak.backends.corebluetooth.client:Retrieving descriptors for characteristic FE01
DEBUG:bleak.backends.corebluetooth.PeripheralDelegate:Descriptor discovered FE01
DEBUG:bleak.backends.corebluetooth.client:Retrieving descriptors for characteristic FE02
DEBUG:bleak.backends.corebluetooth.PeripheralDelegate:Descriptor discovered FE02
DEBUG:bleak.backends.corebluetooth.client:Retrieving descriptors for characteristic FE03
DEBUG:bleak.backends.corebluetooth.PeripheralDelegate:Descriptor discovered FE03
DEBUG:bleak.backends.corebluetooth.client:Retrieving descriptors for characteristic FE04
DEBUG:bleak.backends.corebluetooth.PeripheralDelegate:Descriptor discovered FE04
DEBUG:bleak.backends.corebluetooth.client:Retrieving services...
DEBUG:bleak.backends.corebluetooth.client:Retrieving characteristics for service 180F
DEBUG:bleak.backends.corebluetooth.CentralManagerDelegate:Peripheral Device disconnected!
Traceback (most recent call last):
File "/Users/zaytsev/PycharmProjects/bluetooth/bleak_demo.py", line 25, in <module>
loop.run_until_complete(ble_get_services(address, loop))
File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/base_events.py", line 579, in run_until_complete
return future.result()
File "/Users/zaytsev/PycharmProjects/bluetooth/bleak_demo.py", line 18, in ble_get_services
svcs = await client.get_services()
File "/Users/zaytsev/PycharmProjects/bluetooth/venv/lib/python3.7/site-packages/bleak/backends/corebluetooth/client.py", line 118, in get_services
self.services.add_service(BleakGATTServiceCoreBluetooth(service))
File "/Users/zaytsev/PycharmProjects/bluetooth/venv/lib/python3.7/site-packages/bleak/backends/service.py", line 101, in add_service
"This service is already present in this BleakGATTServiceCollection!"
bleak.exc.BleakError: This service is already present in this BleakGATTServiceCollection!

# Fixed code
async def get_services(self) -> BleakGATTServiceCollection:
    """Get all services registered for this GATT server.

    Returns:
       A :py:class:`bleak.backends.service.BleakGATTServiceCollection` with this device's services tree.

    """
    if self._services_resolved:
        return self.services

    sleep_loop_sec = 0.02
    total_slept_sec = 0

    while total_slept_sec < 5.0:
        properties = await self._get_device_properties()
        services_resolved = properties.get("ServicesResolved", False)
        if services_resolved:
            break
        await asyncio.sleep(sleep_loop_sec, loop=self.loop)
        total_slept_sec += sleep_loop_sec

    if not services_resolved:
        raise BleakError("Services discovery error")

    logger.debug("Get Services...")
    objs = await get_managed_objects(
        self._bus, self.loop, self._device_path + "/service"
    )

    # There is no guarantee that services are listed before characteristics
    # Managed Objects dict.
    # Need multiple iterations to construct the Service Collection

    _chars, _descs = [], []

    for object_path, interfaces in objs.items():
        logger.debug(utils.format_GATT_object(object_path, interfaces))
        if defs.GATT_SERVICE_INTERFACE in interfaces:
            service = interfaces.get(defs.GATT_SERVICE_INTERFACE)
            self.services.add_service(BleakGATTServiceBlueZDBus(service, object_path))
        elif defs.GATT_CHARACTERISTIC_INTERFACE in interfaces:
            char = interfaces.get(defs.GATT_CHARACTERISTIC_INTERFACE)
            _chars.append([char, object_path])
        elif defs.GATT_DESCRIPTOR_INTERFACE in interfaces:
            desc = interfaces.get(defs.GATT_DESCRIPTOR_INTERFACE)
            _descs.append([desc, object_path])

    for char, object_path in _chars:
        _service = list(filter(lambda x: x.path == char["Service"], self.services))
        self.services.add_characteristic(
            BleakGATTCharacteristicBlueZDBus(char, object_path, _service[0].uuid)
        )
        self._char_path_to_uuid[object_path] = char.get("UUID")

    for desc, object_path in _descs:
        _characteristic = list(
            filter(
                lambda x: x.path == desc["Characteristic"],
                self.services.characteristics.values(),
            )
        )
        self.services.add_descriptor(
            BleakGATTDescriptorBlueZDBus(desc, object_path, _characteristic[0].uuid)
        )

    self._services_resolved = True
    return self.services