Add Google Mail integration (#82637)
* Add Google Mail integration * oops * prettier * Add email service * adjustments * update * move email to notify * break out services * tweaks * Add CC and BCC support * drop scope check, breakout tests * use abstract auth * tweak * bump dependency * dependency bump * remove oauth2client
This commit is contained in:
parent
d2537dacc6
commit
ad65fc27bc
30 changed files with 1303 additions and 0 deletions
41
homeassistant/components/google_mail/api.py
Normal file
41
homeassistant/components/google_mail/api.py
Normal file
|
@ -0,0 +1,41 @@
|
|||
"""API for Google Mail bound to Home Assistant OAuth."""
|
||||
from aiohttp import ClientSession
|
||||
from google.auth.exceptions import RefreshError
|
||||
from google.oauth2.credentials import Credentials
|
||||
from google.oauth2.utils import OAuthClientAuthHandler
|
||||
from googleapiclient.discovery import Resource, build
|
||||
|
||||
from homeassistant.const import CONF_ACCESS_TOKEN
|
||||
from homeassistant.helpers import config_entry_oauth2_flow
|
||||
|
||||
|
||||
class AsyncConfigEntryAuth(OAuthClientAuthHandler):
|
||||
"""Provide Google Mail authentication tied to an OAuth2 based config entry."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
websession: ClientSession,
|
||||
oauth2Session: config_entry_oauth2_flow.OAuth2Session,
|
||||
) -> None:
|
||||
"""Initialize Google Mail Auth."""
|
||||
self.oauth_session = oauth2Session
|
||||
super().__init__(websession)
|
||||
|
||||
@property
|
||||
def access_token(self) -> str:
|
||||
"""Return the access token."""
|
||||
return self.oauth_session.token[CONF_ACCESS_TOKEN]
|
||||
|
||||
async def check_and_refresh_token(self) -> str:
|
||||
"""Check the token."""
|
||||
await self.oauth_session.async_ensure_token_valid()
|
||||
return self.access_token
|
||||
|
||||
async def get_resource(self) -> Resource:
|
||||
"""Get current resource."""
|
||||
try:
|
||||
credentials = Credentials(await self.check_and_refresh_token())
|
||||
except RefreshError as ex:
|
||||
self.oauth_session.config_entry.async_start_reauth(self.oauth_session.hass)
|
||||
raise ex
|
||||
return build("gmail", "v1", credentials=credentials)
|
Loading…
Add table
Add a link
Reference in a new issue