Fix google_mail doing blocking i/o in the event loop (take 2) (#118441)

pull/118845/head
J. Nick Koston 2024-05-29 12:55:53 -10:00 committed by Franck Nijhof
parent 27cc97bbeb
commit 5d5210b47d
No known key found for this signature in database
GPG Key ID: D62583BA8AB11CA3
2 changed files with 9 additions and 2 deletions

View File

@ -32,7 +32,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Google Mail from a config entry."""
implementation = await async_get_config_entry_implementation(hass, entry)
session = OAuth2Session(hass, entry, implementation)
auth = AsyncConfigEntryAuth(session)
auth = AsyncConfigEntryAuth(hass, session)
await auth.check_and_refresh_token()
hass.data[DOMAIN][entry.entry_id] = auth

View File

@ -1,5 +1,7 @@
"""API for Google Mail bound to Home Assistant OAuth."""
from functools import partial
from aiohttp.client_exceptions import ClientError, ClientResponseError
from google.auth.exceptions import RefreshError
from google.oauth2.credentials import Credentials
@ -7,6 +9,7 @@ from googleapiclient.discovery import Resource, build
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import CONF_ACCESS_TOKEN
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import (
ConfigEntryAuthFailed,
ConfigEntryNotReady,
@ -20,9 +23,11 @@ class AsyncConfigEntryAuth:
def __init__(
self,
hass: HomeAssistant,
oauth2_session: config_entry_oauth2_flow.OAuth2Session,
) -> None:
"""Initialize Google Mail Auth."""
self._hass = hass
self.oauth_session = oauth2_session
@property
@ -58,4 +63,6 @@ class AsyncConfigEntryAuth:
async def get_resource(self) -> Resource:
"""Get current resource."""
credentials = Credentials(await self.check_and_refresh_token())
return build("gmail", "v1", credentials=credentials)
return await self._hass.async_add_executor_job(
partial(build, "gmail", "v1", credentials=credentials)
)