mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 20:27:08 +00:00
Cleanup nest auth registration, moving out of __init__.py (#63350)
This commit is contained in:
parent
cef0a18e23
commit
737ac7cb7c
@ -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))
|
||||
|
54
homeassistant/components/nest/auth.py
Normal file
54
homeassistant/components/nest/auth.py
Normal file
@ -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
|
@ -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."""
|
||||
|
||||
|
@ -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"
|
||||
|
Loading…
x
Reference in New Issue
Block a user