From 737ac7cb7ceef498db1e4ca9d3bedc054d36f41a Mon Sep 17 00:00:00 2001 From: Allen Porter Date: Mon, 17 Jan 2022 22:17:23 -0800 Subject: [PATCH] Cleanup nest auth registration, moving out of __init__.py (#63350) --- homeassistant/components/nest/__init__.py | 70 +------------------- homeassistant/components/nest/auth.py | 54 +++++++++++++++ homeassistant/components/nest/config_flow.py | 29 +++++++- homeassistant/components/nest/const.py | 3 + 4 files changed, 87 insertions(+), 69 deletions(-) create mode 100644 homeassistant/components/nest/auth.py diff --git a/homeassistant/components/nest/__init__.py b/homeassistant/components/nest/__init__.py index ec7a732e0b0..85513378ed7 100644 --- a/homeassistant/components/nest/__init__.py +++ b/homeassistant/components/nest/__init__.py @@ -42,7 +42,7 @@ from homeassistant.exceptions import ( HomeAssistantError, Unauthorized, ) -from homeassistant.helpers import config_entry_oauth2_flow, config_validation as cv +from homeassistant.helpers import config_validation as cv from homeassistant.helpers.entity_registry import async_entries_for_device from homeassistant.helpers.typing import ConfigType @@ -54,9 +54,6 @@ from .const import ( DATA_SDM, DATA_SUBSCRIBER, DOMAIN, - OAUTH2_AUTHORIZE, - OAUTH2_TOKEN, - OOB_REDIRECT_URI, ) from .events import EVENT_NAME_MAP, NEST_EVENT from .legacy import async_setup_legacy, async_setup_legacy_entry @@ -97,8 +94,6 @@ CONFIG_SCHEMA = vol.Schema( # Platforms for SDM API PLATFORMS = [Platform.SENSOR, Platform.CAMERA, Platform.CLIMATE] -WEB_AUTH_DOMAIN = DOMAIN -INSTALLED_AUTH_DOMAIN = f"{DOMAIN}.installed" # Fetch media events with a disk backed cache, with a limit for each camera # device. The largest media items are mp4 clips at ~120kb each, and we target @@ -109,49 +104,6 @@ EVENT_MEDIA_CACHE_SIZE = 1024 # number of events THUMBNAIL_SIZE_PX = 175 -class WebAuth(config_entry_oauth2_flow.LocalOAuth2Implementation): - """OAuth implementation using OAuth for web applications.""" - - name = "OAuth for Web" - - def __init__( - self, hass: HomeAssistant, client_id: str, client_secret: str, project_id: str - ) -> None: - """Initialize WebAuth.""" - super().__init__( - hass, - WEB_AUTH_DOMAIN, - client_id, - client_secret, - OAUTH2_AUTHORIZE.format(project_id=project_id), - OAUTH2_TOKEN, - ) - - -class InstalledAppAuth(config_entry_oauth2_flow.LocalOAuth2Implementation): - """OAuth implementation using OAuth for installed applications.""" - - name = "OAuth for Apps" - - def __init__( - self, hass: HomeAssistant, client_id: str, client_secret: str, project_id: str - ) -> None: - """Initialize InstalledAppAuth.""" - super().__init__( - hass, - INSTALLED_AUTH_DOMAIN, - client_id, - client_secret, - OAUTH2_AUTHORIZE.format(project_id=project_id), - OAUTH2_TOKEN, - ) - - @property - def redirect_uri(self) -> str: - """Return the redirect uri.""" - return OOB_REDIRECT_URI - - async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Set up Nest components with dispatch between old/new flows.""" hass.data[DOMAIN] = {} @@ -164,25 +116,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: if config_mode == config_flow.ConfigMode.LEGACY: return await async_setup_legacy(hass, config) - project_id = config[DOMAIN][CONF_PROJECT_ID] - config_flow.NestFlowHandler.async_register_implementation( - hass, - InstalledAppAuth( - hass, - config[DOMAIN][CONF_CLIENT_ID], - config[DOMAIN][CONF_CLIENT_SECRET], - project_id, - ), - ) - config_flow.NestFlowHandler.async_register_implementation( - hass, - WebAuth( - hass, - config[DOMAIN][CONF_CLIENT_ID], - config[DOMAIN][CONF_CLIENT_SECRET], - project_id, - ), - ) + config_flow.register_flow_implementation_from_config(hass, config) hass.http.register_view(NestEventMediaView(hass)) hass.http.register_view(NestEventMediaThumbnailView(hass)) diff --git a/homeassistant/components/nest/auth.py b/homeassistant/components/nest/auth.py new file mode 100644 index 00000000000..648623b64c7 --- /dev/null +++ b/homeassistant/components/nest/auth.py @@ -0,0 +1,54 @@ +"""OAuth implementations.""" +from homeassistant.core import HomeAssistant +from homeassistant.helpers import config_entry_oauth2_flow + +from .const import ( + INSTALLED_AUTH_DOMAIN, + OAUTH2_AUTHORIZE, + OAUTH2_TOKEN, + OOB_REDIRECT_URI, + WEB_AUTH_DOMAIN, +) + + +class WebAuth(config_entry_oauth2_flow.LocalOAuth2Implementation): + """OAuth implementation using OAuth for web applications.""" + + name = "OAuth for Web" + + def __init__( + self, hass: HomeAssistant, client_id: str, client_secret: str, project_id: str + ) -> None: + """Initialize WebAuth.""" + super().__init__( + hass, + WEB_AUTH_DOMAIN, + client_id, + client_secret, + OAUTH2_AUTHORIZE.format(project_id=project_id), + OAUTH2_TOKEN, + ) + + +class InstalledAppAuth(config_entry_oauth2_flow.LocalOAuth2Implementation): + """OAuth implementation using OAuth for installed applications.""" + + name = "OAuth for Apps" + + def __init__( + self, hass: HomeAssistant, client_id: str, client_secret: str, project_id: str + ) -> None: + """Initialize InstalledAppAuth.""" + super().__init__( + hass, + INSTALLED_AUTH_DOMAIN, + client_id, + client_secret, + OAUTH2_AUTHORIZE.format(project_id=project_id), + OAUTH2_TOKEN, + ) + + @property + def redirect_uri(self) -> str: + """Return the redirect uri.""" + return OOB_REDIRECT_URI diff --git a/homeassistant/components/nest/config_flow.py b/homeassistant/components/nest/config_flow.py index b8912ad3269..2d5ad567781 100644 --- a/homeassistant/components/nest/config_flow.py +++ b/homeassistant/components/nest/config_flow.py @@ -44,14 +44,16 @@ 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 from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import config_entry_oauth2_flow +from homeassistant.helpers.typing import ConfigType from homeassistant.util import get_random_string from homeassistant.util.json import load_json -from . import api +from . import api, auth from .const import ( CONF_CLOUD_PROJECT_ID, CONF_PROJECT_ID, @@ -119,6 +121,31 @@ def register_flow_implementation( } +def register_flow_implementation_from_config( + hass: HomeAssistant, + config: ConfigType, +) -> None: + """Register auth implementations for SDM API from configuration yaml.""" + NestFlowHandler.async_register_implementation( + hass, + auth.InstalledAppAuth( + hass, + config[DOMAIN][CONF_CLIENT_ID], + config[DOMAIN][CONF_CLIENT_SECRET], + config[DOMAIN][CONF_PROJECT_ID], + ), + ) + NestFlowHandler.async_register_implementation( + hass, + auth.WebAuth( + hass, + config[DOMAIN][CONF_CLIENT_ID], + config[DOMAIN][CONF_CLIENT_SECRET], + config[DOMAIN][CONF_PROJECT_ID], + ), + ) + + class NestAuthError(HomeAssistantError): """Base class for Nest auth errors.""" diff --git a/homeassistant/components/nest/const.py b/homeassistant/components/nest/const.py index a92a48bfd6c..7aabcfe8d77 100644 --- a/homeassistant/components/nest/const.py +++ b/homeassistant/components/nest/const.py @@ -5,6 +5,9 @@ DATA_SDM = "sdm" DATA_SUBSCRIBER = "subscriber" DATA_NEST_CONFIG = "nest_config" +WEB_AUTH_DOMAIN = DOMAIN +INSTALLED_AUTH_DOMAIN = f"{DOMAIN}.installed" + CONF_PROJECT_ID = "project_id" CONF_SUBSCRIBER_ID = "subscriber_id" CONF_CLOUD_PROJECT_ID = "cloud_project_id"