Another connection issue thread with lots of research and will to help and test!

Hello,

Hello

There is one issue I’ve read about here on the forum. I applied the solution from this post to the Python library.

Basically this one is not an error. It’s just letting you know that the connection has been closed which is absolutely fine. I debugged this one. But Homeassisant doesn’t like Errors.

2020-12-04 01:05:49 ERROR (SyncWorker_7) [homeassistant.components.yeelight] Unable to update device 192.168.178.35, [Bedroom] Bedside Lamp Kristina: Bulb closed the connection.

So what I implemeneted was this:

def send_command(self, method, params=None):
    """
    Send a command to the bulb.

    :param str method:  The name of the method to send.
    :param list params: The list of parameters for the method.

    :raises BulbException: When the bulb indicates an error condition.
    :returns: The response from the bulb.
    """
    command = {"id": self._cmd_id, "method": method, "params": params}

    _LOGGER.debug("%s > %s", self, command)

    try:
        self._socket.send((json.dumps(command) + "\r\n").encode("utf8"))
        **time.sleep(0.05)**
        **self._socket.close();**
    except socket.error as ex:
        # Some error occurred, remove this socket in hopes that we can later
        # create a new one.
        self.__socket.close()
        self.__socket = None
        raise_from(BulbException("A socket error occurred when sending the command."), ex)

As you can see I implemented a little wait after a command is sent and then close it, which also increases reaction times dramatically. (it can be any command, get_prop, set_ etc.)

        **time.sleep(0.05)**
        **self._socket.close();**

Same here without timeout.

    while response is None:
        try:
            data = self._socket.recv(16 * 1024)
            **self._socket.close()**
        except socket.error:
            # An error occured, let's close and abort...
            self.__socket.close()
            self.__socket = None
            response = {"error": "Bulb closed the connection."}
            break

So for those error you are getting I can confirm that it is a problem on the library. But it is really a non-problem. The library is just not specifically made to work with Homeassistant.
So if I implement my own Python solution there are no issues with it.

best regards,
Thomas Barbut