Skip to content

Narrowing down errors with nested try/exceptions #9

@pippim

Description

@pippim

Firstly, great application. I was able to get it up and running on Ubuntu 16.04 LTS Plus in just a few hours. In the connect() function, I had to make these changes to narrow down my problems:

def connect(MAC, hci_device="hci0", reset_on_start=True):
    """
    Create and start a new backend adapter and connect it to a device.

    When connecting to multiple devices at the same time make sure to set reset_on_start
    to False after the first connection is made, otherwise all connections made before are
    invalidated.

    :param string MAC: MAC address of the device to connect to.
    :param string hci_device: Added 2025-01-10 was defaulting to "hci0".
    :param bool reset_on_start: Perhaps due to a bug in gatttool or pygatt,
        but if the bluez backend isn't restarted, it can sometimes lock up
        the computer when trying to make a connection to HCI device.
    """
    adapter = pygatt.GATTToolBackend(hci_device=hci_device)  # Create instance
    try:
        adapter.start(reset_on_start=reset_on_start)
        try:
            device = adapter.connect(MAC)
        except pygatt.exceptions.NotConnectedError:
            raise pygatt.exceptions.NotConnectedError(
                "Device MAC: '" + MAC + "' not connected!")
    except pygatt.exceptions.NotConnectedError:
        raise pygatt.exceptions.NotConnectedError(
            "Adapter on device: '" + hci_device + "' cannot start!")

    ''' ORIGINAL CODE: 
    try:
        adapter = pygatt.GATTToolBackend()
        adapter.start(reset_on_start=reset_on_start)
        device = adapter.connect(MAC)
    except pygatt.exceptions.NotConnectedError:
        raise pygatt.exceptions.NotConnectedError("Device nor connected!")
    '''

    log.info("Device connected")
    return device

Before narrowing down the exact error I was only seeing "Device nor connected".

Digging into gatttool.py I had to make these changes to fix the REAL error:

    @at_most_one_device
    def char_write_handle(self, handle, value, wait_for_response=True,
                          timeout=30):
        """
        Writes a value to a given characteristic handle.

        :param handle:
        :param string value:
        :param wait_for_response: If true, performs an attribute write. If
            false, sends a command and expects no acknowledgement from the
            device.
        :param timeout:
        """

        # 2025-01-07 debug error:
        #     ''.join("{0:02x}".format(byte) for byte in value),
        # ValueError: Unknown format code 'x' for object of type 'str'

        try:  # 2025-01-11 - Python 2.7.12 support by default
            byte_str = ''.join("{0:02x}".format(ord(byte)) for byte in value)
        except TypeError:
            byte_str = ''.join("{0:02x}".format(byte) for byte in value)

        cmd = 'char-write-{0} 0x{1:02x} {2}'.format(
            'req' if wait_for_response else 'cmd',
            handle,
            #''.join("{0:02x}".format(byte) for byte in value),  # 2025-01-07
            #''.join("{0:02x}".format(ord(byte)) for byte in value),
            byte_str,
        )

(... snip ...)

I was pleasantly surprised how easy it was to turn on the "Happy Lighting" LED light strip automatically from Linux. Note it's Python 2.7.12.

https://github.com/pippim/HomA/issues/1

HomA.Breathing.stats.mp4

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions