Suppress imap logging on reconnect and presume state (#90826)

This commit is contained in:
Jan Bouwhuis 2023-04-05 12:18:16 +02:00 committed by GitHub
parent 58ac8404ef
commit 94817f61e5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 11 deletions

View file

@ -209,10 +209,9 @@ class ImapDataUpdateCoordinator(DataUpdateCoordinator[int | None]):
await self.imap_client.stop_wait_server_push() await self.imap_client.stop_wait_server_push()
await self.imap_client.close() await self.imap_client.close()
await self.imap_client.logout() await self.imap_client.logout()
except (AioImapException, asyncio.TimeoutError) as ex: except (AioImapException, asyncio.TimeoutError):
if log_error: if log_error:
self.async_set_update_error(ex) _LOGGER.debug("Error while cleaning up imap connection")
_LOGGER.warning("Error while cleaning up imap connection")
self.imap_client = None self.imap_client = None
async def shutdown(self, *_) -> None: async def shutdown(self, *_) -> None:
@ -276,30 +275,30 @@ class ImapPushDataUpdateCoordinator(ImapDataUpdateCoordinator):
try: try:
number_of_messages = await self._async_fetch_number_of_messages() number_of_messages = await self._async_fetch_number_of_messages()
except InvalidAuth as ex: except InvalidAuth as ex:
await self._cleanup()
_LOGGER.warning( _LOGGER.warning(
"Username or password incorrect, starting reauthentication" "Username or password incorrect, starting reauthentication"
) )
self.config_entry.async_start_reauth(self.hass) self.config_entry.async_start_reauth(self.hass)
self.async_set_update_error(ex) self.async_set_update_error(ex)
await self._cleanup()
await asyncio.sleep(BACKOFF_TIME) await asyncio.sleep(BACKOFF_TIME)
except InvalidFolder as ex: except InvalidFolder as ex:
_LOGGER.warning("Selected mailbox folder is invalid") _LOGGER.warning("Selected mailbox folder is invalid")
await self._cleanup()
self.config_entry.async_set_state( self.config_entry.async_set_state(
self.hass, self.hass,
ConfigEntryState.SETUP_ERROR, ConfigEntryState.SETUP_ERROR,
"Selected mailbox folder is invalid.", "Selected mailbox folder is invalid.",
) )
self.async_set_update_error(ex) self.async_set_update_error(ex)
await self._cleanup()
await asyncio.sleep(BACKOFF_TIME) await asyncio.sleep(BACKOFF_TIME)
except ( except (
UpdateFailed, UpdateFailed,
AioImapException, AioImapException,
asyncio.TimeoutError, asyncio.TimeoutError,
) as ex: ) as ex:
self.async_set_update_error(ex)
await self._cleanup() await self._cleanup()
self.async_set_update_error(ex)
await asyncio.sleep(BACKOFF_TIME) await asyncio.sleep(BACKOFF_TIME)
continue continue
else: else:
@ -312,13 +311,11 @@ class ImapPushDataUpdateCoordinator(ImapDataUpdateCoordinator):
await idle await idle
except (AioImapException, asyncio.TimeoutError): except (AioImapException, asyncio.TimeoutError):
_LOGGER.warning( _LOGGER.debug(
"Lost %s (will attempt to reconnect after %s s)", "Lost %s (will attempt to reconnect after %s s)",
self.config_entry.data[CONF_SERVER], self.config_entry.data[CONF_SERVER],
BACKOFF_TIME, BACKOFF_TIME,
) )
self.async_set_update_error(UpdateFailed("Lost connection"))
await self._cleanup()
await asyncio.sleep(BACKOFF_TIME) await asyncio.sleep(BACKOFF_TIME)
async def shutdown(self, *_) -> None: async def shutdown(self, *_) -> None:

View file

@ -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 assert "Lost imap.server.com (will attempt to reconnect after 10 s)" in caplog.text
state = hass.states.get("sensor.imap_email_email_com") 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 is not None
assert state.state == STATE_UNAVAILABLE assert state.state == "0"
@pytest.mark.parametrize("imap_has_capability", [True], ids=["push"]) @pytest.mark.parametrize("imap_has_capability", [True], ids=["push"])