From 9b03d331cab41643ad6a058b7e9cb2d1205f9389 Mon Sep 17 00:00:00 2001 From: Jan Bouwhuis Date: Tue, 4 Apr 2023 12:59:57 +0200 Subject: [PATCH] Fix recovering imap connection triggers re-auth (#90762) --- homeassistant/components/imap/coordinator.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/imap/coordinator.py b/homeassistant/components/imap/coordinator.py index 97432b91054..80172436241 100644 --- a/homeassistant/components/imap/coordinator.py +++ b/homeassistant/components/imap/coordinator.py @@ -8,7 +8,7 @@ import email import logging from typing import Any -from aioimaplib import AUTH, IMAP4_SSL, SELECTED, AioImapException +from aioimaplib import AUTH, IMAP4_SSL, NONAUTH, SELECTED, AioImapException import async_timeout from homeassistant.config_entries import ConfigEntry, ConfigEntryState @@ -36,10 +36,12 @@ async def connect_to_server(data: Mapping[str, Any]) -> IMAP4_SSL: """Connect to imap server and return client.""" client = IMAP4_SSL(data[CONF_SERVER], data[CONF_PORT]) await client.wait_hello_from_server() - await client.login(data[CONF_USERNAME], data[CONF_PASSWORD]) - if client.protocol.state != AUTH: + if client.protocol.state == NONAUTH: + await client.login(data[CONF_USERNAME], data[CONF_PASSWORD]) + if client.protocol.state not in {AUTH, SELECTED}: raise InvalidAuth("Invalid username or password") - await client.select(data[CONF_FOLDER]) + if client.protocol.state == AUTH: + await client.select(data[CONF_FOLDER]) if client.protocol.state != SELECTED: raise InvalidFolder(f"Folder {data[CONF_FOLDER]} is invalid") return client