Fix recovering imap connection triggers re-auth (#90762)

pull/90779/head
Jan Bouwhuis 2023-04-04 12:59:57 +02:00 committed by GitHub
parent b4e12d34f6
commit 9b03d331ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 4 deletions

View File

@ -8,7 +8,7 @@ import email
import logging import logging
from typing import Any from typing import Any
from aioimaplib import AUTH, IMAP4_SSL, SELECTED, AioImapException from aioimaplib import AUTH, IMAP4_SSL, NONAUTH, SELECTED, AioImapException
import async_timeout import async_timeout
from homeassistant.config_entries import ConfigEntry, ConfigEntryState 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.""" """Connect to imap server and return client."""
client = IMAP4_SSL(data[CONF_SERVER], data[CONF_PORT]) client = IMAP4_SSL(data[CONF_SERVER], data[CONF_PORT])
await client.wait_hello_from_server() await client.wait_hello_from_server()
await client.login(data[CONF_USERNAME], data[CONF_PASSWORD]) if client.protocol.state == NONAUTH:
if client.protocol.state != AUTH: await client.login(data[CONF_USERNAME], data[CONF_PASSWORD])
if client.protocol.state not in {AUTH, SELECTED}:
raise InvalidAuth("Invalid username or password") 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: if client.protocol.state != SELECTED:
raise InvalidFolder(f"Folder {data[CONF_FOLDER]} is invalid") raise InvalidFolder(f"Folder {data[CONF_FOLDER]} is invalid")
return client return client