From 5d5210b47d174979e3729420aaa7ab64b58b1485 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 29 May 2024 12:55:53 -1000 Subject: [PATCH] Fix google_mail doing blocking i/o in the event loop (take 2) (#118441) --- homeassistant/components/google_mail/__init__.py | 2 +- homeassistant/components/google_mail/api.py | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/google_mail/__init__.py b/homeassistant/components/google_mail/__init__.py index 1ac963b430a..441ecd3841f 100644 --- a/homeassistant/components/google_mail/__init__.py +++ b/homeassistant/components/google_mail/__init__.py @@ -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 diff --git a/homeassistant/components/google_mail/api.py b/homeassistant/components/google_mail/api.py index e824e4b3ddd..485d640a04d 100644 --- a/homeassistant/components/google_mail/api.py +++ b/homeassistant/components/google_mail/api.py @@ -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) + )