Bump google-nest-sdm to `2.0.0` and cleanup nest auth implementation in config flow (#72770)

Cleanup nest auth implementaton in config flow
pull/72776/head
Allen Porter 2022-05-31 08:53:36 -07:00 committed by GitHub
parent aab3fcad7b
commit d31e43b980
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 56 additions and 23 deletions

View File

@ -72,6 +72,36 @@ class AsyncConfigEntryAuth(AbstractAuth):
return creds
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,
) -> None:
"""Init the Nest client library auth implementation."""
super().__init__(websession, API_URL)
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."""
return Credentials(
token=self._access_token,
token_uri=OAUTH2_TOKEN,
scopes=SDM_SCOPES,
)
async def new_subscriber(
hass: HomeAssistant, entry: ConfigEntry
) -> GoogleNestSubscriber | None:
@ -89,22 +119,27 @@ async def new_subscriber(
):
_LOGGER.error("Configuration option 'subscriber_id' required")
return None
return await new_subscriber_with_impl(hass, entry, subscriber_id, implementation)
async def new_subscriber_with_impl(
hass: HomeAssistant,
entry: ConfigEntry,
subscriber_id: str,
implementation: config_entry_oauth2_flow.AbstractOAuth2Implementation,
) -> GoogleNestSubscriber:
"""Create a GoogleNestSubscriber, used during ConfigFlow."""
config = hass.data[DOMAIN][DATA_NEST_CONFIG]
session = config_entry_oauth2_flow.OAuth2Session(hass, entry, implementation)
auth = AsyncConfigEntryAuth(
aiohttp_client.async_get_clientsession(hass),
session,
config_entry_oauth2_flow.OAuth2Session(hass, entry, implementation),
config[CONF_CLIENT_ID],
config[CONF_CLIENT_SECRET],
)
return GoogleNestSubscriber(auth, config[CONF_PROJECT_ID], subscriber_id)
def new_subscriber_with_token(
hass: HomeAssistant,
access_token: str,
project_id: str,
subscriber_id: str,
) -> GoogleNestSubscriber:
"""Create a GoogleNestSubscriber with an access token."""
return GoogleNestSubscriber(
AccessTokenAuthImpl(
aiohttp_client.async_get_clientsession(hass),
access_token,
),
project_id,
subscriber_id,
)

View File

@ -43,7 +43,6 @@ from google_nest_sdm.exceptions import (
from google_nest_sdm.structure import InfoTrait, Structure
import voluptuous as vol
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_CLIENT_ID, CONF_CLIENT_SECRET
from homeassistant.core import HomeAssistant, callback
from homeassistant.data_entry_flow import FlowResult
@ -319,12 +318,11 @@ class NestFlowHandler(
if not (subscriber_id := data.get(CONF_SUBSCRIBER_ID, "")):
subscriber_id = _generate_subscription_id(cloud_project_id)
_LOGGER.debug("Creating subscriber id '%s'", subscriber_id)
# Create a placeholder ConfigEntry to use since with the auth we've already created.
entry = ConfigEntry(
version=1, domain=DOMAIN, title="", data=self._data, source=""
)
subscriber = await api.new_subscriber_with_impl(
self.hass, entry, subscriber_id, self.flow_impl
subscriber = api.new_subscriber_with_token(
self.hass,
self._data["token"]["access_token"],
config[CONF_PROJECT_ID],
subscriber_id,
)
try:
await subscriber.create_subscription()

View File

@ -5,7 +5,7 @@
"dependencies": ["ffmpeg", "http", "auth"],
"after_dependencies": ["media_source"],
"documentation": "https://www.home-assistant.io/integrations/nest",
"requirements": ["python-nest==4.2.0", "google-nest-sdm==1.8.0"],
"requirements": ["python-nest==4.2.0", "google-nest-sdm==2.0.0"],
"codeowners": ["@allenporter"],
"quality_scale": "platinum",
"dhcp": [

View File

@ -741,7 +741,7 @@ google-cloud-pubsub==2.11.0
google-cloud-texttospeech==2.11.0
# homeassistant.components.nest
google-nest-sdm==1.8.0
google-nest-sdm==2.0.0
# homeassistant.components.google_travel_time
googlemaps==2.5.1

View File

@ -535,7 +535,7 @@ goodwe==0.2.15
google-cloud-pubsub==2.11.0
# homeassistant.components.nest
google-nest-sdm==1.8.0
google-nest-sdm==2.0.0
# homeassistant.components.google_travel_time
googlemaps==2.5.1