# Buggy code
def parse_msg(message):
    if message.member == "InterfacesAdded":
        msg_path = message.body[0]
        try:
            device_interface = message.body[1].get("org.bluez.Device1", {})
        except Exception as e:
            raise e
        devices[msg_path] = (
            {**devices[msg_path], **device_interface}
            if msg_path in devices
            else device_interface
        )
    elif message.member == "PropertiesChanged":
        iface, changed, invalidated = message.body
        if iface != defs.DEVICE_INTERFACE:
            return

        msg_path = message.path
        # the PropertiesChanged signal only sends changed properties, so we
        # need to get remaining properties from cached_devices. However, we
        # don't want to add all cached_devices to the devices dict since
        # they may not actually be nearby or powered on.
        if msg_path not in devices and msg_path in cached_devices:
            devices[msg_path] = cached_devices[msg_path]
        devices[msg_path] = (
            {**devices[msg_path], **changed} if msg_path in devices else changed
        )
    elif (
        message.member == "InterfacesRemoved"
        and message.body[1][0] == defs.BATTERY_INTERFACE
    ):
        logger.info(
            "{0}, {1} ({2}): {3}".format(
                message.member, message.interface, message.path, message.body
            )
        )
        return
    else:
        msg_path = message.path
        logger.info(
            "{0}, {1} ({2}): {3}".format(
                message.member, message.interface, message.path, message.body
            )
        )

    logger.info(
        "{0}, {1} ({2} dBm), Object Path: {3}".format(
            *_device_info(msg_path, devices.get(msg_path))
        )
    )


# Traceback (txdbus.error.RemoteError)
Traceback (most recent call last):
File "discover.py", line 29, in <module>
loop.run_until_complete(run())
File "/usr/lib64/python3.7/asyncio/base_events.py", line 584, in run_until_complete
return future.result()
File "discover.py", line 22, in run
devices = await discover(timeout=1)
File "/home/joe/code/proglove/python/bleak/bleak/backends/bluezdbus/discovery.py", line 125, in discover
bus = await client.connect(reactor, "system").asFuture(loop)
txdbus.error.RemoteError: org.freedesktop.DBus.Error.LimitsExceeded: The maximum number of active connections for UID 1000 has been reached

# Fixed code
def parse_msg(message):
    if message.member == "InterfacesAdded":
        msg_path = message.body[0]
        try:
            device_interface = message.body[1].get("org.bluez.Device1", {})
        except Exception as e:
            raise e
        devices[msg_path] = (
            {**devices[msg_path], **device_interface}
            if msg_path in devices
            else device_interface
        )
    elif message.member == "PropertiesChanged":
        iface, changed, invalidated = message.body
        if iface != defs.DEVICE_INTERFACE:
            return

        msg_path = message.path
        # the PropertiesChanged signal only sends changed properties, so we
        # need to get remaining properties from cached_devices. However, we
        # don't want to add all cached_devices to the devices dict since
        # they may not actually be nearby or powered on.
        if msg_path not in devices and msg_path in cached_devices:
            devices[msg_path] = cached_devices[msg_path]
        devices[msg_path] = (
            {**devices[msg_path], **changed} if msg_path in devices else changed
        )
    elif (
        message.member == "InterfacesRemoved"
        and message.body[1][0] == defs.BATTERY_INTERFACE
    ):
        logger.info(
            "{0}, {1} ({2}): {3}".format(
                message.member, message.interface, message.path, message.body
            )
        )
        return
    else:
        msg_path = message.path
        logger.info(
            "{0}, {1} ({2}): {3}".format(
                message.member, message.interface, message.path, message.body
            )
        )

    logger.info(
        "{0}, {1} ({2} dBm), Object Path: {3}".format(
            *_device_info(msg_path, devices.get(msg_path))
        )
    )