From b82223e4816086f7c5ea39fb349167899ed6d3d9 Mon Sep 17 00:00:00 2001 From: Markus Zehnder Date: Wed, 15 Nov 2023 09:54:06 +0100 Subject: [PATCH] fix: discovery error handling during reconnect If anything goes wrong during SSDP discovery and an exception is raised, the reconnect task would be dead and the integration would no longer connect to a device. --- intg-denonavr/avr.py | 1 + intg-denonavr/discover.py | 19 ++++++++++++------- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/intg-denonavr/avr.py b/intg-denonavr/avr.py index ca6286f..98c8ebd 100644 --- a/intg-denonavr/avr.py +++ b/intg-denonavr/avr.py @@ -461,6 +461,7 @@ async def _handle_connection_failure(self, connect_duration: float, ex): _LOG.info("IP address of '%s' changed: %s", self._name, item["host"]) self._receiver._host = item["host"] # pylint: disable=W0212 # seems to be the only way self.events.emit(Events.IP_ADDRESS_CHANGED, self.id, self._receiver.host) + break else: await asyncio.sleep(backoff) diff --git a/intg-denonavr/discover.py b/intg-denonavr/discover.py index c5b8c96..63c6427 100644 --- a/intg-denonavr/discover.py +++ b/intg-denonavr/discover.py @@ -19,11 +19,16 @@ async def denon_avrs() -> list[dict]: """ _LOG.debug("Starting discovery") - avrs = await denonavr.async_discover() - if not avrs: - _LOG.info("No AVRs discovered") + # extra safety, if anything goes wrong here the reconnection logic is dead + try: + avrs = await denonavr.async_discover() + if not avrs: + _LOG.info("No AVRs discovered") + return [] + + _LOG.info("Found AVR(s): %s", avrs) + + return avrs + except Exception as ex: # pylint: disable=broad-exception-caught + _LOG.error("Failed to start discovery: %s", ex) return [] - - _LOG.info("Found AVR(s): %s", avrs) - - return avrs