From 2c9da198f5066c902b52b8b4da37aec611c4ee54 Mon Sep 17 00:00:00 2001 From: Zach Date: Mon, 26 Feb 2024 21:07:36 +0100 Subject: [PATCH] Fix applying mqtt.reconnect_count by reordering except clauses (#331) * Fix applying reconnect_count reconnect_count is not applied on server disconnect because exception caught before except clause to handle reconnect. * ignore pylint warning "Missing library stubs or py.typed marker"for asyncio_mqtt.error --------- Co-authored-by: Benji <46675043+BenjiU@users.noreply.github.com> --- mqtt_io/server.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/mqtt_io/server.py b/mqtt_io/server.py index 204fcc7c..2b87a12b 100644 --- a/mqtt_io/server.py +++ b/mqtt_io/server.py @@ -18,6 +18,7 @@ from hashlib import sha1 from importlib import import_module from typing import Any, Dict, List, Optional, Tuple, Type, Union, overload +from asyncio_mqtt.error import MqttCodeError # type: ignore import backoff # type: ignore from typing_extensions import Literal @@ -1394,21 +1395,16 @@ async def _main_loop(self) -> None: if self.config["mqtt"].get("ha_discovery", {}).get("enabled"): self._ha_discovery_announce() - try: - await asyncio.gather(*self.critical_tasks) - except asyncio.CancelledError: - break - except Exception: # pylint: disable=broad-except - #_LOG.exception("Exception in critical task:") - _LOG.error("Exception in critical task") + await asyncio.gather(*self.critical_tasks) except asyncio.CancelledError: break - except MQTTException: + except (MQTTException,MqttCodeError): if reconnects_remaining is not None: reconnect = reconnects_remaining > 0 reconnects_remaining -= 1 - #_LOG.exception("Connection to MQTT broker failed") - _LOG.error("Connection to MQTT broker failed") + _LOG.exception("Connection to MQTT broker failed") + except Exception: # pylint: disable=broad-except + _LOG.exception("Exception in critical task:") finally: _LOG.debug("Clearing events and cancelling 'critical_tasks'")