From 94817f61e50b829e16a998be798ab156d641b320 Mon Sep 17 00:00:00 2001 From: Jan Bouwhuis Date: Wed, 5 Apr 2023 12:18:16 +0200 Subject: [PATCH] Suppress imap logging on reconnect and presume state (#90826) --- homeassistant/components/imap/coordinator.py | 15 ++++++--------- tests/components/imap/test_init.py | 4 ++-- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/homeassistant/components/imap/coordinator.py b/homeassistant/components/imap/coordinator.py index 80172436241..421cedad149 100644 --- a/homeassistant/components/imap/coordinator.py +++ b/homeassistant/components/imap/coordinator.py @@ -209,10 +209,9 @@ class ImapDataUpdateCoordinator(DataUpdateCoordinator[int | None]): await self.imap_client.stop_wait_server_push() await self.imap_client.close() await self.imap_client.logout() - except (AioImapException, asyncio.TimeoutError) as ex: + except (AioImapException, asyncio.TimeoutError): if log_error: - self.async_set_update_error(ex) - _LOGGER.warning("Error while cleaning up imap connection") + _LOGGER.debug("Error while cleaning up imap connection") self.imap_client = None async def shutdown(self, *_) -> None: @@ -276,30 +275,30 @@ class ImapPushDataUpdateCoordinator(ImapDataUpdateCoordinator): try: number_of_messages = await self._async_fetch_number_of_messages() except InvalidAuth as ex: + await self._cleanup() _LOGGER.warning( "Username or password incorrect, starting reauthentication" ) self.config_entry.async_start_reauth(self.hass) self.async_set_update_error(ex) - await self._cleanup() await asyncio.sleep(BACKOFF_TIME) except InvalidFolder as ex: _LOGGER.warning("Selected mailbox folder is invalid") + await self._cleanup() self.config_entry.async_set_state( self.hass, ConfigEntryState.SETUP_ERROR, "Selected mailbox folder is invalid.", ) self.async_set_update_error(ex) - await self._cleanup() await asyncio.sleep(BACKOFF_TIME) except ( UpdateFailed, AioImapException, asyncio.TimeoutError, ) as ex: - self.async_set_update_error(ex) await self._cleanup() + self.async_set_update_error(ex) await asyncio.sleep(BACKOFF_TIME) continue else: @@ -312,13 +311,11 @@ class ImapPushDataUpdateCoordinator(ImapDataUpdateCoordinator): await idle except (AioImapException, asyncio.TimeoutError): - _LOGGER.warning( + _LOGGER.debug( "Lost %s (will attempt to reconnect after %s s)", self.config_entry.data[CONF_SERVER], BACKOFF_TIME, ) - self.async_set_update_error(UpdateFailed("Lost connection")) - await self._cleanup() await asyncio.sleep(BACKOFF_TIME) async def shutdown(self, *_) -> None: diff --git a/tests/components/imap/test_init.py b/tests/components/imap/test_init.py index fdcf37b76ba..60efde71435 100644 --- a/tests/components/imap/test_init.py +++ b/tests/components/imap/test_init.py @@ -313,9 +313,9 @@ async def test_lost_connection_with_imap_push( assert "Lost imap.server.com (will attempt to reconnect after 10 s)" in caplog.text state = hass.states.get("sensor.imap_email_email_com") - # we should have an entity with an unavailable state + # Our entity should keep its current state as this assert state is not None - assert state.state == STATE_UNAVAILABLE + assert state.state == "0" @pytest.mark.parametrize("imap_has_capability", [True], ids=["push"])