Bump google-nest-sdm to 2.0.0
and cleanup nest auth implementation in config flow (#72770)
Cleanup nest auth implementaton in config flow
This commit is contained in:
parent
aab3fcad7b
commit
d31e43b980
5 changed files with 56 additions and 23 deletions
|
@ -72,6 +72,36 @@ class AsyncConfigEntryAuth(AbstractAuth):
|
||||||
return creds
|
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(
|
async def new_subscriber(
|
||||||
hass: HomeAssistant, entry: ConfigEntry
|
hass: HomeAssistant, entry: ConfigEntry
|
||||||
) -> GoogleNestSubscriber | None:
|
) -> GoogleNestSubscriber | None:
|
||||||
|
@ -89,22 +119,27 @@ async def new_subscriber(
|
||||||
):
|
):
|
||||||
_LOGGER.error("Configuration option 'subscriber_id' required")
|
_LOGGER.error("Configuration option 'subscriber_id' required")
|
||||||
return None
|
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(
|
auth = AsyncConfigEntryAuth(
|
||||||
aiohttp_client.async_get_clientsession(hass),
|
aiohttp_client.async_get_clientsession(hass),
|
||||||
session,
|
config_entry_oauth2_flow.OAuth2Session(hass, entry, implementation),
|
||||||
config[CONF_CLIENT_ID],
|
config[CONF_CLIENT_ID],
|
||||||
config[CONF_CLIENT_SECRET],
|
config[CONF_CLIENT_SECRET],
|
||||||
)
|
)
|
||||||
return GoogleNestSubscriber(auth, config[CONF_PROJECT_ID], subscriber_id)
|
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,
|
||||||
|
)
|
||||||
|
|
|
@ -43,7 +43,6 @@ from google_nest_sdm.exceptions import (
|
||||||
from google_nest_sdm.structure import InfoTrait, Structure
|
from google_nest_sdm.structure import InfoTrait, Structure
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import CONF_CLIENT_ID, CONF_CLIENT_SECRET
|
from homeassistant.const import CONF_CLIENT_ID, CONF_CLIENT_SECRET
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.data_entry_flow import FlowResult
|
from homeassistant.data_entry_flow import FlowResult
|
||||||
|
@ -319,12 +318,11 @@ class NestFlowHandler(
|
||||||
if not (subscriber_id := data.get(CONF_SUBSCRIBER_ID, "")):
|
if not (subscriber_id := data.get(CONF_SUBSCRIBER_ID, "")):
|
||||||
subscriber_id = _generate_subscription_id(cloud_project_id)
|
subscriber_id = _generate_subscription_id(cloud_project_id)
|
||||||
_LOGGER.debug("Creating subscriber id '%s'", subscriber_id)
|
_LOGGER.debug("Creating subscriber id '%s'", subscriber_id)
|
||||||
# Create a placeholder ConfigEntry to use since with the auth we've already created.
|
subscriber = api.new_subscriber_with_token(
|
||||||
entry = ConfigEntry(
|
self.hass,
|
||||||
version=1, domain=DOMAIN, title="", data=self._data, source=""
|
self._data["token"]["access_token"],
|
||||||
)
|
config[CONF_PROJECT_ID],
|
||||||
subscriber = await api.new_subscriber_with_impl(
|
subscriber_id,
|
||||||
self.hass, entry, subscriber_id, self.flow_impl
|
|
||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
await subscriber.create_subscription()
|
await subscriber.create_subscription()
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
"dependencies": ["ffmpeg", "http", "auth"],
|
"dependencies": ["ffmpeg", "http", "auth"],
|
||||||
"after_dependencies": ["media_source"],
|
"after_dependencies": ["media_source"],
|
||||||
"documentation": "https://www.home-assistant.io/integrations/nest",
|
"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"],
|
"codeowners": ["@allenporter"],
|
||||||
"quality_scale": "platinum",
|
"quality_scale": "platinum",
|
||||||
"dhcp": [
|
"dhcp": [
|
||||||
|
|
|
@ -741,7 +741,7 @@ google-cloud-pubsub==2.11.0
|
||||||
google-cloud-texttospeech==2.11.0
|
google-cloud-texttospeech==2.11.0
|
||||||
|
|
||||||
# homeassistant.components.nest
|
# homeassistant.components.nest
|
||||||
google-nest-sdm==1.8.0
|
google-nest-sdm==2.0.0
|
||||||
|
|
||||||
# homeassistant.components.google_travel_time
|
# homeassistant.components.google_travel_time
|
||||||
googlemaps==2.5.1
|
googlemaps==2.5.1
|
||||||
|
|
|
@ -535,7 +535,7 @@ goodwe==0.2.15
|
||||||
google-cloud-pubsub==2.11.0
|
google-cloud-pubsub==2.11.0
|
||||||
|
|
||||||
# homeassistant.components.nest
|
# homeassistant.components.nest
|
||||||
google-nest-sdm==1.8.0
|
google-nest-sdm==2.0.0
|
||||||
|
|
||||||
# homeassistant.components.google_travel_time
|
# homeassistant.components.google_travel_time
|
||||||
googlemaps==2.5.1
|
googlemaps==2.5.1
|
||||||
|
|
Loading…
Add table
Reference in a new issue