mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Improve onboarding provider call (#107864)
This commit is contained in:
parent
7023ac7366
commit
e36141a4bc
@ -3,17 +3,20 @@ from __future__ import annotations
|
|||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
from http import HTTPStatus
|
from http import HTTPStatus
|
||||||
|
from typing import cast
|
||||||
|
|
||||||
|
from aiohttp import web
|
||||||
from aiohttp.web_exceptions import HTTPUnauthorized
|
from aiohttp.web_exceptions import HTTPUnauthorized
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.auth.const import GROUP_ID_ADMIN
|
from homeassistant.auth.const import GROUP_ID_ADMIN
|
||||||
|
from homeassistant.auth.providers.homeassistant import HassAuthProvider
|
||||||
from homeassistant.components import person
|
from homeassistant.components import person
|
||||||
from homeassistant.components.auth import indieauth
|
from homeassistant.components.auth import indieauth
|
||||||
from homeassistant.components.http import KEY_HASS_REFRESH_TOKEN_ID
|
from homeassistant.components.http import KEY_HASS_REFRESH_TOKEN_ID
|
||||||
from homeassistant.components.http.data_validator import RequestDataValidator
|
from homeassistant.components.http.data_validator import RequestDataValidator
|
||||||
from homeassistant.components.http.view import HomeAssistantView
|
from homeassistant.components.http.view import HomeAssistantView
|
||||||
from homeassistant.core import callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers import area_registry as ar
|
from homeassistant.helpers import area_registry as ar
|
||||||
from homeassistant.helpers.system_info import async_get_system_info
|
from homeassistant.helpers.system_info import async_get_system_info
|
||||||
from homeassistant.helpers.translation import async_get_translations
|
from homeassistant.helpers.translation import async_get_translations
|
||||||
@ -123,9 +126,9 @@ class UserOnboardingView(_BaseOnboardingView):
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
async def post(self, request, data):
|
async def post(self, request: web.Request, data: dict[str, str]) -> web.Response:
|
||||||
"""Handle user creation, area creation."""
|
"""Handle user creation, area creation."""
|
||||||
hass = request.app["hass"]
|
hass: HomeAssistant = request.app["hass"]
|
||||||
|
|
||||||
async with self._lock:
|
async with self._lock:
|
||||||
if self._async_is_done():
|
if self._async_is_done():
|
||||||
@ -137,13 +140,10 @@ class UserOnboardingView(_BaseOnboardingView):
|
|||||||
user = await hass.auth.async_create_user(
|
user = await hass.auth.async_create_user(
|
||||||
data["name"], group_ids=[GROUP_ID_ADMIN]
|
data["name"], group_ids=[GROUP_ID_ADMIN]
|
||||||
)
|
)
|
||||||
await hass.async_add_executor_job(
|
await provider.async_add_auth(data["username"], data["password"])
|
||||||
provider.data.add_auth, data["username"], data["password"]
|
|
||||||
)
|
|
||||||
credentials = await provider.async_get_or_create_credentials(
|
credentials = await provider.async_get_or_create_credentials(
|
||||||
{"username": data["username"]}
|
{"username": data["username"]}
|
||||||
)
|
)
|
||||||
await provider.data.async_save()
|
|
||||||
await hass.auth.async_link_user(user, credentials)
|
await hass.auth.async_link_user(user, credentials)
|
||||||
if "person" in hass.config.components:
|
if "person" in hass.config.components:
|
||||||
await person.async_create_person(hass, data["name"], user_id=user.id)
|
await person.async_create_person(hass, data["name"], user_id=user.id)
|
||||||
@ -292,10 +292,10 @@ class AnalyticsOnboardingView(_BaseOnboardingView):
|
|||||||
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _async_get_hass_provider(hass):
|
def _async_get_hass_provider(hass: HomeAssistant) -> HassAuthProvider:
|
||||||
"""Get the Home Assistant auth provider."""
|
"""Get the Home Assistant auth provider."""
|
||||||
for prv in hass.auth.auth_providers:
|
for prv in hass.auth.auth_providers:
|
||||||
if prv.type == "homeassistant":
|
if prv.type == "homeassistant":
|
||||||
return prv
|
return cast(HassAuthProvider, prv)
|
||||||
|
|
||||||
raise RuntimeError("No Home Assistant provider found")
|
raise RuntimeError("No Home Assistant provider found")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user