Add integration step to onboarding (#23732)
* Return an extra oauth2 auth code during onboarding * Areas in const * Add integration step * Lint * Fix tests * Fix test * Verify integration added to done * Verify step is marked as done
This commit is contained in:
1 parent
07ee3b2eb9
commit
f4016b4aad
9 files changed
+205
-25
No files matched your search
@@ -97,8 +97,7 @@ class AuthProvidersView(HomeAssistantView):
|
||||
async def get(self, request):
|
||||
"""Get available auth providers."""
|
||||
hass = request.app['hass']
|
||||
|
||||
if not hass.components.onboarding.async_is_onboarded():
|
||||
if not hass.components.onboarding.async_is_user_onboarded():
|
||||
return self.json_message(
|
||||
message='Onboarding not finished',
|
||||
status_code=400,
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"area": {
|
||||
"bedroom": "Bedroom",
|
||||
"kitchen": "Kitchen",
|
||||
"living_room": "Living Room"
|
||||
}
|
||||
}
|
||||
@@ -1,24 +1,42 @@
|
||||
"""Support to help onboard new users."""
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.loader import bind_hass
|
||||
from homeassistant.helpers.storage import Store
|
||||
|
||||
from .const import DOMAIN, STEP_USER, STEPS
|
||||
from .const import DOMAIN, STEP_USER, STEPS, STEP_INTEGRATION
|
||||
|
||||
STORAGE_KEY = DOMAIN
|
||||
STORAGE_VERSION = 1
|
||||
STORAGE_VERSION = 2
|
||||
|
||||
|
||||
class OnboadingStorage(Store):
|
||||
"""Store onboarding data."""
|
||||
|
||||
async def _async_migrate_func(self, old_version, old_data):
|
||||
"""Migrate to the new version."""
|
||||
# From version 1 -> 2, we automatically mark the integration step done
|
||||
old_data['done'].append(STEP_INTEGRATION)
|
||||
return old_data
|
||||
|
||||
|
||||
@bind_hass
|
||||
@callback
|
||||
def async_is_onboarded(hass):
|
||||
"""Return if Home Assistant has been onboarded."""
|
||||
return hass.data.get(DOMAIN, True)
|
||||
data = hass.data.get(DOMAIN)
|
||||
return data is None or data is True
|
||||
|
||||
|
||||
@bind_hass
|
||||
@callback
|
||||
def async_is_user_onboarded(hass):
|
||||
"""Return if a user has been created as part of onboarding."""
|
||||
return async_is_onboarded(hass) or STEP_USER in hass.data[DOMAIN]['done']
|
||||
|
||||
|
||||
async def async_setup(hass, config):
|
||||
"""Set up the onboarding component."""
|
||||
store = hass.helpers.storage.Store(
|
||||
STORAGE_VERSION, STORAGE_KEY, private=True)
|
||||
store = OnboadingStorage(hass, STORAGE_VERSION, STORAGE_KEY, private=True)
|
||||
data = await store.async_load()
|
||||
|
||||
if data is None:
|
||||
@@ -43,7 +61,7 @@ async def async_setup(hass, config):
|
||||
if set(data['done']) == set(STEPS):
|
||||
return True
|
||||
|
||||
hass.data[DOMAIN] = False
|
||||
hass.data[DOMAIN] = data
|
||||
|
||||
from . import views
|
||||
|
||||
|
||||
@@ -1,7 +1,15 @@
|
||||
"""Constants for the onboarding component."""
|
||||
DOMAIN = 'onboarding'
|
||||
STEP_USER = 'user'
|
||||
STEP_INTEGRATION = 'integration'
|
||||
|
||||
STEPS = [
|
||||
STEP_USER
|
||||
STEP_USER,
|
||||
STEP_INTEGRATION,
|
||||
]
|
||||
|
||||
DEFAULT_AREAS = (
|
||||
'living_room',
|
||||
'kitchen',
|
||||
'bedroom',
|
||||
)
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"area": {
|
||||
"living_room": "Living Room",
|
||||
"bedroom": "Bedroom",
|
||||
"kitchen": "Kitchen"
|
||||
}
|
||||
}
|
||||
@@ -7,13 +7,14 @@ from homeassistant.components.http.data_validator import RequestDataValidator
|
||||
from homeassistant.components.http.view import HomeAssistantView
|
||||
from homeassistant.core import callback
|
||||
|
||||
from .const import DOMAIN, STEP_USER, STEPS
|
||||
from .const import DOMAIN, STEP_USER, STEPS, DEFAULT_AREAS, STEP_INTEGRATION
|
||||
|
||||
|
||||
async def async_setup(hass, data, store):
|
||||
"""Set up the onboarding view."""
|
||||
hass.http.register_view(OnboardingView(data, store))
|
||||
hass.http.register_view(UserOnboardingView(data, store))
|
||||
hass.http.register_view(IntegrationOnboardingView(data, store))
|
||||
|
||||
|
||||
class OnboardingView(HomeAssistantView):
|
||||
@@ -41,7 +42,6 @@ class OnboardingView(HomeAssistantView):
|
||||
class _BaseOnboardingView(HomeAssistantView):
|
||||
"""Base class for onboarding."""
|
||||
|
||||
requires_auth = False
|
||||
step = None
|
||||
|
||||
def __init__(self, data, store):
|
||||
@@ -60,14 +60,16 @@ class _BaseOnboardingView(HomeAssistantView):
|
||||
self._data['done'].append(self.step)
|
||||
await self._store.async_save(self._data)
|
||||
|
||||
hass.data[DOMAIN] = len(self._data) == len(STEPS)
|
||||
if set(self._data['done']) == set(STEPS):
|
||||
hass.data[DOMAIN] = True
|
||||
|
||||
|
||||
class UserOnboardingView(_BaseOnboardingView):
|
||||
"""View to handle onboarding."""
|
||||
"""View to handle create user onboarding step."""
|
||||
|
||||
url = '/api/onboarding/users'
|
||||
name = 'api:onboarding:users'
|
||||
requires_auth = False
|
||||
step = STEP_USER
|
||||
|
||||
@RequestDataValidator(vol.Schema({
|
||||
@@ -75,9 +77,10 @@ class UserOnboardingView(_BaseOnboardingView):
|
||||
vol.Required('username'): str,
|
||||
vol.Required('password'): str,
|
||||
vol.Required('client_id'): str,
|
||||
vol.Required('language'): str,
|
||||
}))
|
||||
async def post(self, request, data):
|
||||
"""Return the manifest.json."""
|
||||
"""Handle user creation, area creation."""
|
||||
hass = request.app['hass']
|
||||
|
||||
async with self._lock:
|
||||
@@ -100,14 +103,58 @@ class UserOnboardingView(_BaseOnboardingView):
|
||||
data['name'], user_id=user.id
|
||||
)
|
||||
|
||||
# Create default areas using the users supplied language.
|
||||
translations = \
|
||||
await hass.helpers.translation.async_get_translations(
|
||||
data['language'])
|
||||
|
||||
area_registry = \
|
||||
await hass.helpers.area_registry.async_get_registry()
|
||||
|
||||
for area in DEFAULT_AREAS:
|
||||
area_registry.async_create(
|
||||
translations['component.onboarding.area.{}'.format(area)]
|
||||
)
|
||||
|
||||
await self._async_mark_done(hass)
|
||||
|
||||
# Return an authorization code to allow fetching tokens.
|
||||
# Return authorization code for fetching tokens and connect
|
||||
# during onboarding.
|
||||
auth_code = hass.components.auth.create_auth_code(
|
||||
data['client_id'], user
|
||||
)
|
||||
return self.json({
|
||||
'auth_code': auth_code
|
||||
'auth_code': auth_code,
|
||||
})
|
||||
|
||||
|
||||
class IntegrationOnboardingView(_BaseOnboardingView):
|
||||
"""View to finish integration onboarding step."""
|
||||
|
||||
url = '/api/onboarding/integration'
|
||||
name = 'api:onboarding:integration'
|
||||
step = STEP_INTEGRATION
|
||||
|
||||
@RequestDataValidator(vol.Schema({
|
||||
vol.Required('client_id'): str,
|
||||
}))
|
||||
async def post(self, request, data):
|
||||
"""Handle user creation, area creation."""
|
||||
hass = request.app['hass']
|
||||
user = request['hass_user']
|
||||
|
||||
async with self._lock:
|
||||
if self._async_is_done():
|
||||
return self.json_message('Integration step already done', 403)
|
||||
|
||||
await self._async_mark_done(hass)
|
||||
|
||||
# Return authorization code so we can redirect user and log them in
|
||||
auth_code = hass.components.auth.create_auth_code(
|
||||
data['client_id'], user
|
||||
)
|
||||
return self.json({
|
||||
'auth_code': auth_code,
|
||||
})
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user