1 """API for Google Mail bound to Home Assistant OAuth."""
3 from functools
import partial
5 from aiohttp.client_exceptions
import ClientError, ClientResponseError
6 from google.auth.exceptions
import RefreshError
7 from google.oauth2.credentials
import Credentials
8 from googleapiclient.discovery
import Resource, build
14 ConfigEntryAuthFailed,
22 """Provide Google Mail authentication tied to an OAuth2 based config entry."""
27 oauth2_session: config_entry_oauth2_flow.OAuth2Session,
29 """Initialize Google Mail Auth."""
35 """Return the access token."""
36 return self.
oauth_sessionoauth_session.token[CONF_ACCESS_TOKEN]
39 """Check the token."""
41 await self.
oauth_sessionoauth_session.async_ensure_token_valid()
42 except (RefreshError, ClientResponseError, ClientError)
as ex:
45 is ConfigEntryState.SETUP_IN_PROGRESS
47 if isinstance(ex, ClientResponseError)
and 400 <= ex.status < 500:
49 "OAuth session is not valid, reauth required"
51 raise ConfigEntryNotReady
from ex
53 isinstance(ex, RefreshError)
54 or hasattr(ex,
"status")
57 self.
oauth_sessionoauth_session.config_entry.async_start_reauth(
64 """Get current resource."""
66 return await self.
_hass_hass.async_add_executor_job(
67 partial(build,
"gmail",
"v1", credentials=credentials)
Resource get_resource(self)
None __init__(self, HomeAssistant hass, config_entry_oauth2_flow.OAuth2Session oauth2_session)
str check_and_refresh_token(self)