From e55df73a91105cca549eb2c103d1bce705905707 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20S=C3=B8rensen?= Date: Sat, 11 Sep 2021 20:38:35 +0200 Subject: [PATCH] Remove usage of discovery info (#10015) --- src/auth/ha-authorize.ts | 24 +++++---------------- src/data/discovery.ts | 16 -------------- src/data/onboarding.ts | 21 ++++++++++++++++++ src/onboarding/ha-onboarding.ts | 16 ++++++-------- src/onboarding/onboarding-restore-backup.ts | 19 +++++----------- src/translations/en.json | 1 - 6 files changed, 38 insertions(+), 59 deletions(-) delete mode 100644 src/data/discovery.ts diff --git a/src/auth/ha-authorize.ts b/src/auth/ha-authorize.ts index 9d51431102..cade5741f0 100644 --- a/src/auth/ha-authorize.ts +++ b/src/auth/ha-authorize.ts @@ -8,10 +8,6 @@ import { AuthUrlSearchParams, fetchAuthProviders, } from "../data/auth"; -import { - DiscoveryInformation, - fetchDiscoveryInformation, -} from "../data/discovery"; import { litLocalizeLiteMixin } from "../mixins/lit-localize-lite-mixin"; import { registerServiceWorker } from "../util/register-service-worker"; import "./ha-auth-flow"; @@ -29,8 +25,6 @@ class HaAuthorize extends litLocalizeLiteMixin(LitElement) { @state() private _authProviders?: AuthProvider[]; - @state() private _discovery?: DiscoveryInformation; - constructor() { super(); this.translationFragment = "page-authorize"; @@ -58,17 +52,14 @@ class HaAuthorize extends litLocalizeLiteMixin(LitElement) { // the name with a bold tag. const loggingInWith = document.createElement("div"); loggingInWith.innerText = this.localize( - this._discovery?.location_name - ? "ui.panel.page-authorize.logging_in_to_with" - : "ui.panel.page-authorize.logging_in_with", - "locationName", - "LOCATION", + "ui.panel.page-authorize.logging_in_with", "authProviderName", "NAME" ); - loggingInWith.innerHTML = loggingInWith.innerHTML - .replace("**LOCATION**", `${this._discovery?.location_name}`) - .replace("**NAME**", `${this._authProvider!.name}`); + loggingInWith.innerHTML = loggingInWith.innerHTML.replace( + "**NAME**", + `${this._authProvider!.name}` + ); const inactiveProviders = this._authProviders.filter( (prv) => prv !== this._authProvider @@ -108,7 +99,6 @@ class HaAuthorize extends litLocalizeLiteMixin(LitElement) { protected firstUpdated(changedProps: PropertyValues) { super.firstUpdated(changedProps); this._fetchAuthProviders(); - this._fetchDiscoveryInfo(); if (matchMedia("(prefers-color-scheme: dark)").matches) { applyThemesOnElement( @@ -144,10 +134,6 @@ class HaAuthorize extends litLocalizeLiteMixin(LitElement) { } } - private async _fetchDiscoveryInfo() { - this._discovery = await fetchDiscoveryInformation(); - } - private async _fetchAuthProviders() { // Fetch auth providers try { diff --git a/src/data/discovery.ts b/src/data/discovery.ts deleted file mode 100644 index 40f558365b..0000000000 --- a/src/data/discovery.ts +++ /dev/null @@ -1,16 +0,0 @@ -export interface DiscoveryInformation { - uuid: string; - base_url: string | null; - external_url: string | null; - internal_url: string | null; - location_name: string; - installation_type: string; - requires_api_password: boolean; - version: string; -} - -export const fetchDiscoveryInformation = - async (): Promise => { - const response = await fetch("/api/discovery_info", { method: "GET" }); - return response.json(); - }; diff --git a/src/data/onboarding.ts b/src/data/onboarding.ts index 3d9e24ee9b..9e5c751ada 100644 --- a/src/data/onboarding.ts +++ b/src/data/onboarding.ts @@ -1,6 +1,15 @@ import { HomeAssistant } from "../types"; import { handleFetchPromise } from "../util/hass-call-api"; +export interface InstallationType { + installation_type: + | "Home Assistant Operating System" + | "Home Assistant Container" + | "Home Assistant Supervised" + | "Home Assistant Core" + | "Unknown"; +} + // eslint-disable-next-line @typescript-eslint/no-empty-interface export interface OnboardingCoreConfigStepResponse {} @@ -65,3 +74,15 @@ export const onboardIntegrationStep = ( "onboarding/integration", params ); + +export const fetchInstallationType = async (): Promise => { + const response = await fetch("/api/onboarding/installation_type", { + method: "GET", + }); + + if (response.status === 401) { + throw Error("unauthorized"); + } + + return response.json(); +}; diff --git a/src/onboarding/ha-onboarding.ts b/src/onboarding/ha-onboarding.ts index e06665b300..1a0c4f2dd7 100644 --- a/src/onboarding/ha-onboarding.ts +++ b/src/onboarding/ha-onboarding.ts @@ -13,14 +13,12 @@ import { extractSearchParamsObject } from "../common/url/search-params"; import { subscribeOne } from "../common/util/subscribe-one"; import { AuthUrlSearchParams, hassUrl } from "../data/auth"; import { - DiscoveryInformation, - fetchDiscoveryInformation, -} from "../data/discovery"; -import { + InstallationType, fetchOnboardingOverview, OnboardingResponses, OnboardingStep, onboardIntegrationStep, + fetchInstallationType, } from "../data/onboarding"; import { subscribeUser } from "../data/ws-user"; import { litLocalizeLiteMixin } from "../mixins/lit-localize-lite-mixin"; @@ -71,7 +69,7 @@ class HaOnboarding extends litLocalizeLiteMixin(HassElement) { @state() private _steps?: OnboardingStep[]; - @state() private _discoveryInformation?: DiscoveryInformation; + @state() private _installation_type?: InstallationType; protected render(): TemplateResult { const step = this._curStep()!; @@ -92,7 +90,7 @@ class HaOnboarding extends litLocalizeLiteMixin(HassElement) { ? html` ` @@ -130,7 +128,7 @@ class HaOnboarding extends litLocalizeLiteMixin(HassElement) { protected firstUpdated(changedProps: PropertyValues) { super.firstUpdated(changedProps); this._fetchOnboardingSteps(); - this._fetchDiscoveryInformation(); + this._fetchInstallationType(); import("./onboarding-integrations"); import("./onboarding-core-config"); registerServiceWorker(this, false); @@ -174,9 +172,9 @@ class HaOnboarding extends litLocalizeLiteMixin(HassElement) { this._restoring = true; } - private async _fetchDiscoveryInformation(): Promise { + private async _fetchInstallationType(): Promise { try { - const response = await fetchDiscoveryInformation(); + const response = await fetchInstallationType(); this._supervisor = [ "Home Assistant OS", "Home Assistant Supervised", diff --git a/src/onboarding/onboarding-restore-backup.ts b/src/onboarding/onboarding-restore-backup.ts index bc3c2d4265..2f3e1aca52 100644 --- a/src/onboarding/onboarding-restore-backup.ts +++ b/src/onboarding/onboarding-restore-backup.ts @@ -6,14 +6,11 @@ import { showHassioBackupDialog } from "../../hassio/src/dialogs/backup/show-dia import { showBackupUploadDialog } from "../../hassio/src/dialogs/backup/show-dialog-backup-upload"; import type { LocalizeFunc } from "../common/translations/localize"; import "../components/ha-card"; -import { - DiscoveryInformation, - fetchDiscoveryInformation, -} from "../data/discovery"; import { makeDialogManager } from "../dialogs/make-dialog-manager"; import { ProvideHassLitMixin } from "../mixins/provide-hass-lit-mixin"; import { haStyle } from "../resources/styles"; import "./onboarding-loading"; +import { fetchInstallationType, InstallationType } from "../data/onboarding"; declare global { interface HASSDomEvents { @@ -30,7 +27,7 @@ class OnboardingRestoreBackup extends ProvideHassLitMixin(LitElement) { @property({ type: Boolean }) public restoring = false; @property({ attribute: false }) - public discoveryInformation?: DiscoveryInformation; + public installationType?: InstallationType; protected render(): TemplateResult { return this.restoring @@ -64,17 +61,11 @@ class OnboardingRestoreBackup extends ProvideHassLitMixin(LitElement) { private async _checkRestoreStatus(): Promise { if (this.restoring) { try { - const response = await fetchDiscoveryInformation(); - - if ( - !this.discoveryInformation || - this.discoveryInformation.uuid !== response.uuid - ) { - // When the UUID changes, the restore is complete + await fetchInstallationType(); + } catch (err) { + if ((err as Error).message === "unauthorized") { window.location.replace("/"); } - } catch (err) { - // We fully expected issues with fetching info untill restore is complete. } } } diff --git a/src/translations/en.json b/src/translations/en.json index dae6405e5b..92529a281c 100755 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -3517,7 +3517,6 @@ "page-authorize": { "initializing": "Initializing", "authorizing_client": "You're about to give {clientId} access to your Home Assistant instance.", - "logging_in_to_with": "Logging in to **{locationName}** with **{authProviderName}**.", "logging_in_with": "Logging in with **{authProviderName}**.", "pick_auth_provider": "Or log in with", "abort_intro": "Login aborted",