From d31e43b98049921950fed9cce6c5b45fd1d0c59c Mon Sep 17 00:00:00 2001 From: Allen Porter Date: Tue, 31 May 2022 08:53:36 -0700 Subject: [PATCH] Bump google-nest-sdm to `2.0.0` and cleanup nest auth implementation in config flow (#72770) Cleanup nest auth implementaton in config flow --- homeassistant/components/nest/api.py | 61 +++++++++++++++----- homeassistant/components/nest/config_flow.py | 12 ++-- homeassistant/components/nest/manifest.json | 2 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- 5 files changed, 56 insertions(+), 23 deletions(-) diff --git a/homeassistant/components/nest/api.py b/homeassistant/components/nest/api.py index 3934b0b3cf1..830db926d9a 100644 --- a/homeassistant/components/nest/api.py +++ b/homeassistant/components/nest/api.py @@ -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, + ) diff --git a/homeassistant/components/nest/config_flow.py b/homeassistant/components/nest/config_flow.py index aeebd48abb4..61a61f6c8e0 100644 --- a/homeassistant/components/nest/config_flow.py +++ b/homeassistant/components/nest/config_flow.py @@ -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() diff --git a/homeassistant/components/nest/manifest.json b/homeassistant/components/nest/manifest.json index ce0b68c782a..4f768e08843 100644 --- a/homeassistant/components/nest/manifest.json +++ b/homeassistant/components/nest/manifest.json @@ -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": [ diff --git a/requirements_all.txt b/requirements_all.txt index 871980bffae..e7a00e1709c 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -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 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index ba8a7cefd9c..c6154c71985 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -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