mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 04:07:08 +00:00
Add base class for onboarding views (#141970)
This commit is contained in:
parent
aaafdee56f
commit
78338f161f
@ -51,7 +51,7 @@ async def async_setup(
|
|||||||
hass: HomeAssistant, data: OnboardingStoreData, store: OnboardingStorage
|
hass: HomeAssistant, data: OnboardingStoreData, store: OnboardingStorage
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the onboarding view."""
|
"""Set up the onboarding view."""
|
||||||
hass.http.register_view(OnboardingView(data, store))
|
hass.http.register_view(OnboardingStatusView(data, store))
|
||||||
hass.http.register_view(InstallationTypeOnboardingView(data))
|
hass.http.register_view(InstallationTypeOnboardingView(data))
|
||||||
hass.http.register_view(UserOnboardingView(data, store))
|
hass.http.register_view(UserOnboardingView(data, store))
|
||||||
hass.http.register_view(CoreConfigOnboardingView(data, store))
|
hass.http.register_view(CoreConfigOnboardingView(data, store))
|
||||||
@ -63,17 +63,30 @@ async def async_setup(
|
|||||||
setup_cloud_views(hass, data)
|
setup_cloud_views(hass, data)
|
||||||
|
|
||||||
|
|
||||||
class OnboardingView(HomeAssistantView):
|
class _BaseOnboardingView(HomeAssistantView):
|
||||||
"""Return the onboarding status."""
|
"""Base class for onboarding views."""
|
||||||
|
|
||||||
|
def __init__(self, data: OnboardingStoreData) -> None:
|
||||||
|
"""Initialize the onboarding view."""
|
||||||
|
self._data = data
|
||||||
|
|
||||||
|
|
||||||
|
class _NoAuthBaseOnboardingView(_BaseOnboardingView):
|
||||||
|
"""Base class for unauthenticated onboarding views."""
|
||||||
|
|
||||||
requires_auth = False
|
requires_auth = False
|
||||||
|
|
||||||
|
|
||||||
|
class OnboardingStatusView(_NoAuthBaseOnboardingView):
|
||||||
|
"""Return the onboarding status."""
|
||||||
|
|
||||||
url = "/api/onboarding"
|
url = "/api/onboarding"
|
||||||
name = "api:onboarding"
|
name = "api:onboarding"
|
||||||
|
|
||||||
def __init__(self, data: OnboardingStoreData, store: OnboardingStorage) -> None:
|
def __init__(self, data: OnboardingStoreData, store: OnboardingStorage) -> None:
|
||||||
"""Initialize the onboarding view."""
|
"""Initialize the onboarding view."""
|
||||||
|
super().__init__(data)
|
||||||
self._store = store
|
self._store = store
|
||||||
self._data = data
|
|
||||||
|
|
||||||
async def get(self, request: web.Request) -> web.Response:
|
async def get(self, request: web.Request) -> web.Response:
|
||||||
"""Return the onboarding status."""
|
"""Return the onboarding status."""
|
||||||
@ -82,17 +95,12 @@ class OnboardingView(HomeAssistantView):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class InstallationTypeOnboardingView(HomeAssistantView):
|
class InstallationTypeOnboardingView(_NoAuthBaseOnboardingView):
|
||||||
"""Return the installation type during onboarding."""
|
"""Return the installation type during onboarding."""
|
||||||
|
|
||||||
requires_auth = False
|
|
||||||
url = "/api/onboarding/installation_type"
|
url = "/api/onboarding/installation_type"
|
||||||
name = "api:onboarding:installation_type"
|
name = "api:onboarding:installation_type"
|
||||||
|
|
||||||
def __init__(self, data: OnboardingStoreData) -> None:
|
|
||||||
"""Initialize the onboarding installation type view."""
|
|
||||||
self._data = data
|
|
||||||
|
|
||||||
async def get(self, request: web.Request) -> web.Response:
|
async def get(self, request: web.Request) -> web.Response:
|
||||||
"""Return the onboarding status."""
|
"""Return the onboarding status."""
|
||||||
if self._data["done"]:
|
if self._data["done"]:
|
||||||
@ -103,15 +111,15 @@ class InstallationTypeOnboardingView(HomeAssistantView):
|
|||||||
return self.json({"installation_type": info["installation_type"]})
|
return self.json({"installation_type": info["installation_type"]})
|
||||||
|
|
||||||
|
|
||||||
class _BaseOnboardingView(HomeAssistantView):
|
class _BaseOnboardingStepView(_BaseOnboardingView):
|
||||||
"""Base class for onboarding."""
|
"""Base class for an onboarding step."""
|
||||||
|
|
||||||
step: str
|
step: str
|
||||||
|
|
||||||
def __init__(self, data: OnboardingStoreData, store: OnboardingStorage) -> None:
|
def __init__(self, data: OnboardingStoreData, store: OnboardingStorage) -> None:
|
||||||
"""Initialize the onboarding view."""
|
"""Initialize the onboarding view."""
|
||||||
|
super().__init__(data)
|
||||||
self._store = store
|
self._store = store
|
||||||
self._data = data
|
|
||||||
self._lock = asyncio.Lock()
|
self._lock = asyncio.Lock()
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
@ -131,7 +139,7 @@ class _BaseOnboardingView(HomeAssistantView):
|
|||||||
listener()
|
listener()
|
||||||
|
|
||||||
|
|
||||||
class UserOnboardingView(_BaseOnboardingView):
|
class UserOnboardingView(_BaseOnboardingStepView):
|
||||||
"""View to handle create user onboarding step."""
|
"""View to handle create user onboarding step."""
|
||||||
|
|
||||||
url = "/api/onboarding/users"
|
url = "/api/onboarding/users"
|
||||||
@ -197,7 +205,7 @@ class UserOnboardingView(_BaseOnboardingView):
|
|||||||
return self.json({"auth_code": auth_code})
|
return self.json({"auth_code": auth_code})
|
||||||
|
|
||||||
|
|
||||||
class CoreConfigOnboardingView(_BaseOnboardingView):
|
class CoreConfigOnboardingView(_BaseOnboardingStepView):
|
||||||
"""View to finish core config onboarding step."""
|
"""View to finish core config onboarding step."""
|
||||||
|
|
||||||
url = "/api/onboarding/core_config"
|
url = "/api/onboarding/core_config"
|
||||||
@ -243,7 +251,7 @@ class CoreConfigOnboardingView(_BaseOnboardingView):
|
|||||||
return self.json({})
|
return self.json({})
|
||||||
|
|
||||||
|
|
||||||
class IntegrationOnboardingView(_BaseOnboardingView):
|
class IntegrationOnboardingView(_BaseOnboardingStepView):
|
||||||
"""View to finish integration onboarding step."""
|
"""View to finish integration onboarding step."""
|
||||||
|
|
||||||
url = "/api/onboarding/integration"
|
url = "/api/onboarding/integration"
|
||||||
@ -290,7 +298,7 @@ class IntegrationOnboardingView(_BaseOnboardingView):
|
|||||||
return self.json({"auth_code": auth_code})
|
return self.json({"auth_code": auth_code})
|
||||||
|
|
||||||
|
|
||||||
class AnalyticsOnboardingView(_BaseOnboardingView):
|
class AnalyticsOnboardingView(_BaseOnboardingStepView):
|
||||||
"""View to finish analytics onboarding step."""
|
"""View to finish analytics onboarding step."""
|
||||||
|
|
||||||
url = "/api/onboarding/analytics"
|
url = "/api/onboarding/analytics"
|
||||||
@ -312,17 +320,7 @@ class AnalyticsOnboardingView(_BaseOnboardingView):
|
|||||||
return self.json({})
|
return self.json({})
|
||||||
|
|
||||||
|
|
||||||
class BackupOnboardingView(HomeAssistantView):
|
def with_backup_manager[_ViewT: _BaseOnboardingView, **_P](
|
||||||
"""Backup onboarding view."""
|
|
||||||
|
|
||||||
requires_auth = False
|
|
||||||
|
|
||||||
def __init__(self, data: OnboardingStoreData) -> None:
|
|
||||||
"""Initialize the view."""
|
|
||||||
self._data = data
|
|
||||||
|
|
||||||
|
|
||||||
def with_backup_manager[_ViewT: BackupOnboardingView, **_P](
|
|
||||||
func: Callable[
|
func: Callable[
|
||||||
Concatenate[_ViewT, BackupManager, web.Request, _P],
|
Concatenate[_ViewT, BackupManager, web.Request, _P],
|
||||||
Coroutine[Any, Any, web.Response],
|
Coroutine[Any, Any, web.Response],
|
||||||
@ -354,7 +352,7 @@ def with_backup_manager[_ViewT: BackupOnboardingView, **_P](
|
|||||||
return with_backup
|
return with_backup
|
||||||
|
|
||||||
|
|
||||||
class BackupInfoView(BackupOnboardingView):
|
class BackupInfoView(_NoAuthBaseOnboardingView):
|
||||||
"""Get backup info view."""
|
"""Get backup info view."""
|
||||||
|
|
||||||
url = "/api/onboarding/backup/info"
|
url = "/api/onboarding/backup/info"
|
||||||
@ -373,7 +371,7 @@ class BackupInfoView(BackupOnboardingView):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class RestoreBackupView(BackupOnboardingView):
|
class RestoreBackupView(_NoAuthBaseOnboardingView):
|
||||||
"""Restore backup view."""
|
"""Restore backup view."""
|
||||||
|
|
||||||
url = "/api/onboarding/backup/restore"
|
url = "/api/onboarding/backup/restore"
|
||||||
@ -418,7 +416,7 @@ class RestoreBackupView(BackupOnboardingView):
|
|||||||
return web.Response(status=HTTPStatus.OK)
|
return web.Response(status=HTTPStatus.OK)
|
||||||
|
|
||||||
|
|
||||||
class UploadBackupView(BackupOnboardingView, backup_http.UploadBackupView):
|
class UploadBackupView(_NoAuthBaseOnboardingView, backup_http.UploadBackupView):
|
||||||
"""Upload backup view."""
|
"""Upload backup view."""
|
||||||
|
|
||||||
url = "/api/onboarding/backup/upload"
|
url = "/api/onboarding/backup/upload"
|
||||||
@ -442,16 +440,7 @@ def setup_cloud_views(hass: HomeAssistant, data: OnboardingStoreData) -> None:
|
|||||||
# pylint: disable-next=import-outside-toplevel,hass-component-root-import
|
# pylint: disable-next=import-outside-toplevel,hass-component-root-import
|
||||||
from homeassistant.components.cloud.const import DATA_CLOUD
|
from homeassistant.components.cloud.const import DATA_CLOUD
|
||||||
|
|
||||||
class CloudOnboardingView(HomeAssistantView):
|
def with_cloud[_ViewT: _BaseOnboardingView, **_P](
|
||||||
"""Cloud onboarding view."""
|
|
||||||
|
|
||||||
requires_auth = False
|
|
||||||
|
|
||||||
def __init__(self, data: OnboardingStoreData) -> None:
|
|
||||||
"""Initialize the view."""
|
|
||||||
self._data = data
|
|
||||||
|
|
||||||
def with_cloud[_ViewT: CloudOnboardingView, **_P](
|
|
||||||
func: Callable[
|
func: Callable[
|
||||||
Concatenate[_ViewT, web.Request, _P],
|
Concatenate[_ViewT, web.Request, _P],
|
||||||
Coroutine[Any, Any, web.Response],
|
Coroutine[Any, Any, web.Response],
|
||||||
@ -486,7 +475,7 @@ def setup_cloud_views(hass: HomeAssistant, data: OnboardingStoreData) -> None:
|
|||||||
return _with_cloud
|
return _with_cloud
|
||||||
|
|
||||||
class CloudForgotPasswordView(
|
class CloudForgotPasswordView(
|
||||||
CloudOnboardingView, cloud_http.CloudForgotPasswordView
|
_NoAuthBaseOnboardingView, cloud_http.CloudForgotPasswordView
|
||||||
):
|
):
|
||||||
"""View to start Forgot Password flow."""
|
"""View to start Forgot Password flow."""
|
||||||
|
|
||||||
@ -498,7 +487,7 @@ def setup_cloud_views(hass: HomeAssistant, data: OnboardingStoreData) -> None:
|
|||||||
"""Handle forgot password request."""
|
"""Handle forgot password request."""
|
||||||
return await super()._post(request)
|
return await super()._post(request)
|
||||||
|
|
||||||
class CloudLoginView(CloudOnboardingView, cloud_http.CloudLoginView):
|
class CloudLoginView(_NoAuthBaseOnboardingView, cloud_http.CloudLoginView):
|
||||||
"""Login to Home Assistant Cloud."""
|
"""Login to Home Assistant Cloud."""
|
||||||
|
|
||||||
url = "/api/onboarding/cloud/login"
|
url = "/api/onboarding/cloud/login"
|
||||||
@ -509,7 +498,7 @@ def setup_cloud_views(hass: HomeAssistant, data: OnboardingStoreData) -> None:
|
|||||||
"""Handle login request."""
|
"""Handle login request."""
|
||||||
return await super()._post(request)
|
return await super()._post(request)
|
||||||
|
|
||||||
class CloudLogoutView(CloudOnboardingView, cloud_http.CloudLogoutView):
|
class CloudLogoutView(_NoAuthBaseOnboardingView, cloud_http.CloudLogoutView):
|
||||||
"""Log out of the Home Assistant cloud."""
|
"""Log out of the Home Assistant cloud."""
|
||||||
|
|
||||||
url = "/api/onboarding/cloud/logout"
|
url = "/api/onboarding/cloud/logout"
|
||||||
@ -520,7 +509,7 @@ def setup_cloud_views(hass: HomeAssistant, data: OnboardingStoreData) -> None:
|
|||||||
"""Handle logout request."""
|
"""Handle logout request."""
|
||||||
return await super()._post(request)
|
return await super()._post(request)
|
||||||
|
|
||||||
class CloudStatusView(CloudOnboardingView):
|
class CloudStatusView(_NoAuthBaseOnboardingView):
|
||||||
"""Get cloud status view."""
|
"""Get cloud status view."""
|
||||||
|
|
||||||
url = "/api/onboarding/cloud/status"
|
url = "/api/onboarding/cloud/status"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user