Skip to content

Commit

Permalink
Fix google_mail doing blocking i/o in the event loop (take 2) (#118441)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored and frenck committed May 29, 2024
1 parent 27cc97b commit 5d5210b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
2 changes: 1 addition & 1 deletion homeassistant/components/google_mail/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 8 additions & 1 deletion homeassistant/components/google_mail/api.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
"""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
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,
Expand All @@ -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
Expand Down Expand Up @@ -58,4 +63,6 @@ async def check_and_refresh_token(self) -> str:
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)
)

0 comments on commit 5d5210b

Please sign in to comment.