2020-10-21 08:17:49 +00:00
|
|
|
"""API for Google Nest Device Access bound to Home Assistant OAuth."""
|
|
|
|
|
2021-11-30 06:41:29 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-01-02 00:51:01 +00:00
|
|
|
import datetime
|
2021-11-30 06:41:29 +00:00
|
|
|
import logging
|
2021-07-28 07:12:32 +00:00
|
|
|
from typing import cast
|
2021-01-02 00:51:01 +00:00
|
|
|
|
2020-10-21 08:17:49 +00:00
|
|
|
from aiohttp import ClientSession
|
|
|
|
from google.oauth2.credentials import Credentials
|
2024-10-29 11:58:36 +00:00
|
|
|
from google_nest_sdm.admin_client import PUBSUB_API_HOST, AdminClient
|
2020-10-21 08:17:49 +00:00
|
|
|
from google_nest_sdm.auth import AbstractAuth
|
2021-11-15 00:08:22 +00:00
|
|
|
from google_nest_sdm.google_nest_subscriber import GoogleNestSubscriber
|
2020-10-21 08:17:49 +00:00
|
|
|
|
2021-11-15 00:08:22 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers import aiohttp_client, config_entry_oauth2_flow
|
2020-10-21 08:17:49 +00:00
|
|
|
|
2021-11-15 00:08:22 +00:00
|
|
|
from .const import (
|
|
|
|
API_URL,
|
|
|
|
CONF_PROJECT_ID,
|
|
|
|
CONF_SUBSCRIBER_ID,
|
2024-10-29 11:58:36 +00:00
|
|
|
CONF_SUBSCRIPTION_NAME,
|
2021-11-15 00:08:22 +00:00
|
|
|
OAUTH2_TOKEN,
|
|
|
|
SDM_SCOPES,
|
|
|
|
)
|
2024-12-26 17:27:20 +00:00
|
|
|
from .types import NestConfigEntry
|
2021-01-02 00:51:01 +00:00
|
|
|
|
2021-11-30 06:41:29 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2020-10-21 08:17:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AsyncConfigEntryAuth(AbstractAuth):
|
|
|
|
"""Provide Google Nest Device Access authentication tied to an OAuth2 based config entry."""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
websession: ClientSession,
|
|
|
|
oauth_session: config_entry_oauth2_flow.OAuth2Session,
|
2021-01-02 00:51:01 +00:00
|
|
|
client_id: str,
|
|
|
|
client_secret: str,
|
2021-05-20 12:06:44 +00:00
|
|
|
) -> None:
|
2020-10-21 08:17:49 +00:00
|
|
|
"""Initialize Google Nest Device Access auth."""
|
2021-01-02 00:51:01 +00:00
|
|
|
super().__init__(websession, API_URL)
|
2020-10-21 08:17:49 +00:00
|
|
|
self._oauth_session = oauth_session
|
2021-01-02 00:51:01 +00:00
|
|
|
self._client_id = client_id
|
|
|
|
self._client_secret = client_secret
|
2020-10-21 08:17:49 +00:00
|
|
|
|
2021-07-26 23:43:52 +00:00
|
|
|
async def async_get_access_token(self) -> str:
|
2021-01-02 00:51:01 +00:00
|
|
|
"""Return a valid access token for SDM API."""
|
2024-10-16 09:42:44 +00:00
|
|
|
await self._oauth_session.async_ensure_token_valid()
|
2021-07-28 07:12:32 +00:00
|
|
|
return cast(str, self._oauth_session.token["access_token"])
|
2020-10-21 08:17:49 +00:00
|
|
|
|
2021-07-26 23:43:52 +00:00
|
|
|
async def async_get_creds(self) -> Credentials:
|
2025-02-11 10:51:30 +00:00
|
|
|
"""Return an OAuth credential for Pub/Sub Subscriber.
|
|
|
|
|
|
|
|
The subscriber will call this when connecting to the stream to refresh
|
|
|
|
the token. We construct a credentials object using the underlying
|
|
|
|
OAuth2Session since the subscriber may expect the expiry fields to
|
|
|
|
be present.
|
|
|
|
"""
|
|
|
|
await self.async_get_access_token()
|
2021-01-02 00:51:01 +00:00
|
|
|
token = self._oauth_session.token
|
2024-06-07 09:13:33 +00:00
|
|
|
creds = Credentials( # type: ignore[no-untyped-call]
|
2021-01-02 00:51:01 +00:00
|
|
|
token=token["access_token"],
|
|
|
|
refresh_token=token["refresh_token"],
|
|
|
|
token_uri=OAUTH2_TOKEN,
|
|
|
|
client_id=self._client_id,
|
|
|
|
client_secret=self._client_secret,
|
|
|
|
scopes=SDM_SCOPES,
|
|
|
|
)
|
|
|
|
creds.expiry = datetime.datetime.fromtimestamp(token["expires_at"])
|
|
|
|
return creds
|
2021-11-15 00:08:22 +00:00
|
|
|
|
|
|
|
|
2022-05-31 15:53:36 +00:00
|
|
|
class AccessTokenAuthImpl(AbstractAuth):
|
|
|
|
"""Authentication implementation used during config flow, without refresh.
|
|
|
|
|
|
|
|
This exists to allow the config flow to use the API before it has fully
|
|
|
|
created a config entry required by OAuth2Session. This does not support
|
|
|
|
refreshing tokens, which is fine since it should have been just created.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
websession: ClientSession,
|
|
|
|
access_token: str,
|
2024-10-29 11:58:36 +00:00
|
|
|
host: str,
|
2022-05-31 15:53:36 +00:00
|
|
|
) -> None:
|
|
|
|
"""Init the Nest client library auth implementation."""
|
2024-10-29 11:58:36 +00:00
|
|
|
super().__init__(websession, host)
|
2022-05-31 15:53:36 +00:00
|
|
|
self._access_token = access_token
|
|
|
|
|
|
|
|
async def async_get_access_token(self) -> str:
|
|
|
|
"""Return the access token."""
|
|
|
|
return self._access_token
|
|
|
|
|
|
|
|
async def async_get_creds(self) -> Credentials:
|
|
|
|
"""Return an OAuth credential for Pub/Sub Subscriber."""
|
2024-06-07 09:13:33 +00:00
|
|
|
return Credentials( # type: ignore[no-untyped-call]
|
2022-05-31 15:53:36 +00:00
|
|
|
token=self._access_token,
|
|
|
|
token_uri=OAUTH2_TOKEN,
|
|
|
|
scopes=SDM_SCOPES,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2025-02-09 17:31:18 +00:00
|
|
|
async def new_auth(hass: HomeAssistant, entry: NestConfigEntry) -> AbstractAuth:
|
2021-11-15 00:08:22 +00:00
|
|
|
"""Create a GoogleNestSubscriber."""
|
|
|
|
implementation = (
|
|
|
|
await config_entry_oauth2_flow.async_get_config_entry_implementation(
|
|
|
|
hass, entry
|
|
|
|
)
|
|
|
|
)
|
2022-06-15 14:15:53 +00:00
|
|
|
if not isinstance(
|
|
|
|
implementation, config_entry_oauth2_flow.LocalOAuth2Implementation
|
2021-11-30 06:41:29 +00:00
|
|
|
):
|
2023-01-30 13:06:52 +00:00
|
|
|
raise TypeError(f"Unexpected auth implementation {implementation}")
|
2025-02-09 17:31:18 +00:00
|
|
|
return AsyncConfigEntryAuth(
|
2021-11-15 00:08:22 +00:00
|
|
|
aiohttp_client.async_get_clientsession(hass),
|
2022-05-31 15:53:36 +00:00
|
|
|
config_entry_oauth2_flow.OAuth2Session(hass, entry, implementation),
|
2022-06-15 14:15:53 +00:00
|
|
|
implementation.client_id,
|
|
|
|
implementation.client_secret,
|
2021-11-15 00:08:22 +00:00
|
|
|
)
|
2025-02-09 17:31:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def new_subscriber(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
entry: NestConfigEntry,
|
|
|
|
auth: AbstractAuth,
|
|
|
|
) -> GoogleNestSubscriber:
|
|
|
|
"""Create a GoogleNestSubscriber."""
|
|
|
|
if (subscription_name := entry.data.get(CONF_SUBSCRIPTION_NAME)) is None:
|
|
|
|
subscription_name = entry.data[CONF_SUBSCRIBER_ID]
|
2024-10-29 11:58:36 +00:00
|
|
|
return GoogleNestSubscriber(auth, entry.data[CONF_PROJECT_ID], subscription_name)
|
2022-05-31 15:53:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
def new_subscriber_with_token(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
access_token: str,
|
|
|
|
project_id: str,
|
2024-10-29 11:58:36 +00:00
|
|
|
subscription_name: str,
|
2022-05-31 15:53:36 +00:00
|
|
|
) -> GoogleNestSubscriber:
|
|
|
|
"""Create a GoogleNestSubscriber with an access token."""
|
|
|
|
return GoogleNestSubscriber(
|
|
|
|
AccessTokenAuthImpl(
|
|
|
|
aiohttp_client.async_get_clientsession(hass),
|
|
|
|
access_token,
|
2024-10-29 11:58:36 +00:00
|
|
|
API_URL,
|
2022-05-31 15:53:36 +00:00
|
|
|
),
|
|
|
|
project_id,
|
2024-10-29 11:58:36 +00:00
|
|
|
subscription_name,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def new_pubsub_admin_client(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
access_token: str,
|
|
|
|
cloud_project_id: str,
|
|
|
|
) -> AdminClient:
|
|
|
|
"""Create a Nest AdminClient with an access token."""
|
|
|
|
return AdminClient(
|
|
|
|
auth=AccessTokenAuthImpl(
|
|
|
|
aiohttp_client.async_get_clientsession(hass),
|
|
|
|
access_token,
|
|
|
|
PUBSUB_API_HOST,
|
|
|
|
),
|
|
|
|
cloud_project_id=cloud_project_id,
|
2022-05-31 15:53:36 +00:00
|
|
|
)
|