format MQTT code with ruff

This commit is contained in:
Dan 2025-09-01 15:54:48 -04:00
parent a45915517f
commit 3952035579

View File

@ -45,7 +45,7 @@ class MqttClient(Communicator):
self._stop_reconnect = True
if self._reconnect_thread and self._reconnect_thread.is_alive():
self._reconnect_thread.join(timeout=5)
if hasattr(self, 'client'):
if hasattr(self, "client"):
self.client.disconnect()
def _set_initial_topics(self) -> None:
@ -203,9 +203,15 @@ class MqttClient(Communicator):
"""Mqtt disconnection callback."""
self.connected = False
# Debug reason code thoroughly
reason_name = reason_code.getName() if hasattr(reason_code, 'getName') else str(reason_code)
reason_value = getattr(reason_code, 'value', reason_code)
logger.error(f"MQTT disconnected - reason: '{reason_name}', code: {reason_value}, type: {type(reason_code)}")
reason_name = (
reason_code.getName()
if hasattr(reason_code, "getName")
else str(reason_code)
)
reason_value = getattr(reason_code, "value", reason_code)
logger.error(
f"MQTT disconnected - reason: '{reason_name}', code: {reason_value}, type: {type(reason_code)}"
)
# Don't attempt reconnection if we're stopping or if it was a clean disconnect
if self._stop_reconnect:
@ -221,9 +227,7 @@ class MqttClient(Communicator):
# Start reconnection in a separate thread to avoid blocking
if not self._reconnect_thread or not self._reconnect_thread.is_alive():
self._reconnect_thread = threading.Thread(
target=self._reconnect_loop,
name="mqtt-reconnect",
daemon=True
target=self._reconnect_loop, name="mqtt-reconnect", daemon=True
)
self._reconnect_thread.start()
@ -322,7 +326,9 @@ class MqttClient(Communicator):
while not self._stop_reconnect and not self.connected:
attempt += 1
logger.error(f"Will attempt MQTT reconnection in {self._reconnect_delay} seconds (attempt {attempt})")
logger.error(
f"Will attempt MQTT reconnection in {self._reconnect_delay} seconds (attempt {attempt})"
)
# Wait with ability to exit early if stopping
for _ in range(self._reconnect_delay):
@ -336,10 +342,12 @@ class MqttClient(Communicator):
break
try:
logger.error(f"Creating fresh MQTT client for reconnection attempt {attempt}...")
logger.error(
f"Creating fresh MQTT client for reconnection attempt {attempt}..."
)
# Clean up old client if it exists
if hasattr(self, 'client'):
if hasattr(self, "client"):
try:
self.client.disconnect()
self.client.loop_stop()
@ -352,11 +360,15 @@ class MqttClient(Communicator):
# Give the connection attempt some time to complete
for _ in range(5): # Wait up to 5 seconds for connection
if self.connected:
logger.error(f"MQTT fresh connection successful on attempt {attempt}!")
logger.error(
f"MQTT fresh connection successful on attempt {attempt}!"
)
return
time.sleep(1)
logger.error(f"MQTT fresh connection attempt {attempt} timed out, will retry")
logger.error(
f"MQTT fresh connection attempt {attempt} timed out, will retry"
)
# Continue the loop to retry
except Exception as e:
logger.error(f"MQTT fresh connection attempt {attempt} failed: {e}")