Fix recovering imap connection triggers re-auth (#90762)
parent
b4e12d34f6
commit
9b03d331ca
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue