From 1b46190a0ce0445670c7a2dfedf41e49a14dc627 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20S=C3=B8rensen?= Date: Sat, 11 Sep 2021 15:38:38 +0200 Subject: [PATCH] Add view to get installation type during onboarding (#56095) --- homeassistant/components/onboarding/views.py | 24 +++++++++++++ tests/components/onboarding/test_views.py | 37 ++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/homeassistant/components/onboarding/views.py b/homeassistant/components/onboarding/views.py index dec80642845..cedce0d1d51 100644 --- a/homeassistant/components/onboarding/views.py +++ b/homeassistant/components/onboarding/views.py @@ -1,6 +1,7 @@ """Onboarding views.""" import asyncio +from aiohttp.web_exceptions import HTTPUnauthorized import voluptuous as vol from homeassistant.auth.const import GROUP_ID_ADMIN @@ -10,6 +11,7 @@ from homeassistant.components.http.data_validator import RequestDataValidator from homeassistant.components.http.view import HomeAssistantView from homeassistant.const import HTTP_BAD_REQUEST, HTTP_FORBIDDEN from homeassistant.core import callback +from homeassistant.helpers.system_info import async_get_system_info from .const import ( DEFAULT_AREAS, @@ -25,6 +27,7 @@ from .const import ( async def async_setup(hass, data, store): """Set up the onboarding view.""" hass.http.register_view(OnboardingView(data, store)) + hass.http.register_view(InstallationTypeOnboardingView(data)) hass.http.register_view(UserOnboardingView(data, store)) hass.http.register_view(CoreConfigOnboardingView(data, store)) hass.http.register_view(IntegrationOnboardingView(data, store)) @@ -50,6 +53,27 @@ class OnboardingView(HomeAssistantView): ) +class InstallationTypeOnboardingView(HomeAssistantView): + """Return the installation type during onboarding.""" + + requires_auth = False + url = "/api/onboarding/installation_type" + name = "api:onboarding:installation_type" + + def __init__(self, data): + """Initialize the onboarding installation type view.""" + self._data = data + + async def get(self, request): + """Return the onboarding status.""" + if self._data["done"]: + raise HTTPUnauthorized() + + hass = request.app["hass"] + info = await async_get_system_info(hass) + return self.json({"installation_type": info["installation_type"]}) + + class _BaseOnboardingView(HomeAssistantView): """Base class for onboarding.""" diff --git a/tests/components/onboarding/test_views.py b/tests/components/onboarding/test_views.py index 66f68ad8b33..77666f18fad 100644 --- a/tests/components/onboarding/test_views.py +++ b/tests/components/onboarding/test_views.py @@ -442,3 +442,40 @@ async def test_onboarding_analytics(hass, hass_storage, hass_client, hass_admin_ resp = await client.post("/api/onboarding/analytics") assert resp.status == 403 + + +async def test_onboarding_installation_type(hass, hass_storage, hass_client): + """Test returning installation type during onboarding.""" + mock_storage(hass_storage, {"done": []}) + await async_setup_component(hass, "persistent_notification", {}) + + assert await async_setup_component(hass, "onboarding", {}) + await hass.async_block_till_done() + + client = await hass_client() + + with patch( + "homeassistant.components.onboarding.views.async_get_system_info", + return_value={"installation_type": "Home Assistant Core"}, + ): + resp = await client.get("/api/onboarding/installation_type") + + assert resp.status == 200 + + resp_content = await resp.json() + assert resp_content["installation_type"] == "Home Assistant Core" + + +async def test_onboarding_installation_type_after_done(hass, hass_storage, hass_client): + """Test raising for installation type after onboarding.""" + mock_storage(hass_storage, {"done": [const.STEP_USER]}) + await async_setup_component(hass, "persistent_notification", {}) + + assert await async_setup_component(hass, "onboarding", {}) + await hass.async_block_till_done() + + client = await hass_client() + + resp = await client.get("/api/onboarding/installation_type") + + assert resp.status == 401