From 57216781457f4fba010a3da4cddda3ec3d2fa689 Mon Sep 17 00:00:00 2001
From: Rob Pilling <robpilling@gmail.com>
Date: Mon, 2 Dec 2024 08:38:34 +0000
Subject: [PATCH] Make non-top-level connection errors `debug` level (#361)

This avoids spamming the home-assistant logs, which can occur when
home-assistant has been running but the roomba hasn't been turned on, so
no initial connection has been established.
This allows users to read the logs/investigate other home-assistant
issues, without having to disable the roomba integration.

For example instead of this message being repeated 3x:

```
2024-MM-DD HH:MM:SS.MMM ERROR (roombapy) [roombapy.remote_client] Can't connect to 192.168.0.123
Traceback (most recent call last):
  File .../lib/python3.12/site-packages/roombapy/remote_client.py", line 93, in connect
    self._open_mqtt_connection()
  File .../lib/python3.12/site-packages/roombapy/remote_client.py", line 117, in _open_mqtt_connection
    self.mqtt_client.connect(self.address, self.port)
  File .../lib/python3.12/site-packages/paho/mqtt/client.py", line 914, in connect
    return self.reconnect()
           ^^^^^^^^^^^^^^^^
  File .../lib/python3.12/site-packages/paho/mqtt/client.py", line 1044, in reconnect
    sock = self._create_socket_connection()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File .../lib/python3.12/site-packages/paho/mqtt/client.py", line 3685, in _create_socket_connection
    return socket.create_connection(addr, timeout=self._connect_timeout, source_address=source)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File .../lib/python3.12/socket.py", line 865, in create_connection
    raise exceptions[0]
  File .../lib/python3.12/socket.py", line 850, in create_connection
    sock.connect(sa)
OSError: [Errno 113] No route to host
```

and these then following:

```
2024-MM-DD HH:MM:SS.MMM ERROR (roombapy) [roombapy.remote_client] Unable to connect to 192.168.0.123
2024-MM-DD HH:MM:SS.MMM WARNING (roombapy) [roombapy.roomba] Unexpectedly disconnected from Roomba 192.168.0.123, code The connection was lost
2024-MM-DD HH:MM:SS.MMM WARNING (roombapy) [roombapy.roomba] Periodic connection lost due to Unable to connect to Roomba at 192.168.0.123
```

we now receive:
```
2024-MM-DD HH:MM:SS.MMM WARNING (roombapy) [roombapy.roomba] Unexpectedly disconnected from Roomba 192.168.0.123, code The connection was lost
```

With the ability to get back more detailed connection logs by enabling debug logging in home-assistant.

Signed-off-by: Philipp Schmitt <pschmitt@users.noreply.github.com>
Co-authored-by: Philipp Schmitt <pschmitt@users.noreply.github.com>
---
 roombapy/remote_client.py | 2 +-
 roombapy/roomba.py        | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/roombapy/remote_client.py b/roombapy/remote_client.py
index f81f34e..3d738cc 100644
--- a/roombapy/remote_client.py
+++ b/roombapy/remote_client.py
@@ -97,7 +97,7 @@ def connect(self) -> bool:
                 return True
             attempt += 1
 
-        self.log.error("Unable to connect to %s", self.address)
+        self.log.debug("Unable to connect to %s", self.address)
         return False
 
     def disconnect(self) -> None:
diff --git a/roombapy/roomba.py b/roombapy/roomba.py
index 4a18edf..3b94002 100644
--- a/roombapy/roomba.py
+++ b/roombapy/roomba.py
@@ -167,7 +167,7 @@ def periodic_connection(self) -> None:
             except RoombaConnectionError as error:
                 self.periodic_connection_running = False
                 self.on_disconnect(MQTT_ERROR_MESSAGES[7])
-                self.log.warning("Periodic connection lost due to %s", error)
+                self.log.debug("Periodic connection lost due to %s", error)
                 return
             time.sleep(self.delay)