diff --git a/homeassistant/components/imap/coordinator.py b/homeassistant/components/imap/coordinator.py index c3cd21e6b2d..b644c300979 100644 --- a/homeassistant/components/imap/coordinator.py +++ b/homeassistant/components/imap/coordinator.py @@ -298,7 +298,8 @@ class ImapDataUpdateCoordinator(DataUpdateCoordinator[int | None]): except (AioImapException, asyncio.TimeoutError): if log_error: _LOGGER.debug("Error while cleaning up imap connection") - self.imap_client = None + finally: + self.imap_client = None async def shutdown(self, *_: Any) -> None: """Close resources.""" @@ -370,7 +371,6 @@ class ImapPushDataUpdateCoordinator(ImapDataUpdateCoordinator): async def _async_wait_push_loop(self) -> None: """Wait for data push from server.""" - cleanup = False while True: try: number_of_messages = await self._async_fetch_number_of_messages() @@ -412,9 +412,6 @@ class ImapPushDataUpdateCoordinator(ImapDataUpdateCoordinator): await idle # From python 3.11 asyncio.TimeoutError is an alias of TimeoutError - except asyncio.CancelledError as ex: - cleanup = True - raise asyncio.CancelledError from ex except (AioImapException, asyncio.TimeoutError): _LOGGER.debug( "Lost %s (will attempt to reconnect after %s s)", @@ -423,9 +420,6 @@ class ImapPushDataUpdateCoordinator(ImapDataUpdateCoordinator): ) await self._cleanup() await asyncio.sleep(BACKOFF_TIME) - finally: - if cleanup: - await self._cleanup() async def shutdown(self, *_: Any) -> None: """Close resources."""