From bd6addd4b9f46c8cf592a27569754a40a5596d28 Mon Sep 17 00:00:00 2001 From: Bram Kragten Date: Wed, 30 Mar 2022 12:23:24 +0200 Subject: [PATCH 1/3] Add support for my links to create a helper config entry --- .../config/helpers/ha-config-helpers.ts | 64 +++++++++++++++++++ .../integrations/ha-config-integrations.ts | 7 ++ 2 files changed, 71 insertions(+) diff --git a/src/panels/config/helpers/ha-config-helpers.ts b/src/panels/config/helpers/ha-config-helpers.ts index 4ce9d00118a6..e375f8d3ec5f 100644 --- a/src/panels/config/helpers/ha-config-helpers.ts +++ b/src/panels/config/helpers/ha-config-helpers.ts @@ -29,6 +29,14 @@ import { showEntityEditorDialog } from "../entities/show-dialog-entity-editor"; import { configSections } from "../ha-panel-config"; import { HELPER_DOMAINS } from "./const"; import { showHelperDetailDialog } from "./show-dialog-helper-detail"; +import { navigate } from "../../../common/navigate"; +import { extractSearchParam } from "../../../common/url/search-params"; +import { getConfigFlowHandlers } from "../../../data/config_flow"; +import { showConfigFlowDialog } from "../../../dialogs/config-flow/show-dialog-config-flow"; +import { + showAlertDialog, + showConfirmationDialog, +} from "../../../dialogs/generic/show-dialog-box"; // This groups items by a key but only returns last entry per key. const groupByOne = ( @@ -219,6 +227,62 @@ export class HaConfigHelpers extends SubscribeMixin(LitElement) { protected firstUpdated(changedProps: PropertyValues) { super.firstUpdated(changedProps); this._getConfigEntries(); + const localizePromise = this.hass.loadBackendTranslation( + "title", + undefined, + true + ); + if (this.route.path === "/add") { + this._handleAdd(localizePromise); + } + } + + private async _handleAdd(localizePromise: Promise) { + const domain = extractSearchParam("domain"); + navigate("/config/helpers", { replace: true }); + if (!domain) { + return; + } + const handlers = await getConfigFlowHandlers(this.hass, "helper"); + + if (!handlers.includes(domain)) { + const integrations = await getConfigFlowHandlers( + this.hass, + "integration" + ); + if (integrations.includes(domain)) { + navigate(`/config/integrations/add?domain=${domain}`, { + replace: true, + }); + return; + } + showAlertDialog(this, { + title: this.hass.localize( + "ui.panel.config.integrations.config_flow.error" + ), + text: this.hass.localize( + "ui.panel.config.integrations.config_flow.no_config_flow" + ), + }); + return; + } + const localize = await localizePromise; + if ( + !(await showConfirmationDialog(this, { + title: localize("ui.panel.config.integrations.confirm_new", { + integration: domainToName(localize, domain), + }), + })) + ) { + return; + } + showConfigFlowDialog(this, { + dialogClosedCallback: () => { + this._getConfigEntries(); + }, + startFlowHandler: domain, + showAdvanced: this.hass.userData?.showAdvanced, + }); } protected willUpdate(changedProps: PropertyValues) { diff --git a/src/panels/config/integrations/ha-config-integrations.ts b/src/panels/config/integrations/ha-config-integrations.ts index c860953efc6f..de945c3465f4 100644 --- a/src/panels/config/integrations/ha-config-integrations.ts +++ b/src/panels/config/integrations/ha-config-integrations.ts @@ -661,6 +661,13 @@ class HaConfigIntegrations extends SubscribeMixin(LitElement) { const handlers = await getConfigFlowHandlers(this.hass, "integration"); if (!handlers.includes(domain)) { + const helpers = await getConfigFlowHandlers(this.hass, "helper"); + if (helpers.includes(domain)) { + navigate(`/config/helpers/add?domain=${domain}`, { + replace: true, + }); + return; + } showAlertDialog(this, { title: this.hass.localize( "ui.panel.config.integrations.config_flow.error" From 65446b8d2b6e9b094aeafed7d8c3867dfbaa0d24 Mon Sep 17 00:00:00 2001 From: Bram Kragten Date: Wed, 30 Mar 2022 13:04:38 +0200 Subject: [PATCH 2/3] Add support for input helpers --- src/panels/config/helpers/dialog-helper-detail.ts | 2 +- src/panels/config/helpers/ha-config-helpers.ts | 6 ++++++ src/panels/config/helpers/show-dialog-helper-detail.ts | 3 ++- src/panels/config/integrations/ha-config-integrations.ts | 7 +++++++ 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/panels/config/helpers/dialog-helper-detail.ts b/src/panels/config/helpers/dialog-helper-detail.ts index c3adf6a82631..1a2833636ec8 100644 --- a/src/panels/config/helpers/dialog-helper-detail.ts +++ b/src/panels/config/helpers/dialog-helper-detail.ts @@ -66,7 +66,7 @@ export class DialogHelperDetail extends LitElement { public async showDialog(params: ShowDialogHelperDetailParams): Promise { this._params = params; - this._domain = undefined; + this._domain = params.domain; this._item = undefined; this._opened = true; await this.updateComplete; diff --git a/src/panels/config/helpers/ha-config-helpers.ts b/src/panels/config/helpers/ha-config-helpers.ts index e375f8d3ec5f..2535508bebc0 100644 --- a/src/panels/config/helpers/ha-config-helpers.ts +++ b/src/panels/config/helpers/ha-config-helpers.ts @@ -243,6 +243,12 @@ export class HaConfigHelpers extends SubscribeMixin(LitElement) { if (!domain) { return; } + if (HELPER_DOMAINS.includes(domain)) { + showHelperDetailDialog(this, { + domain, + }); + return; + } const handlers = await getConfigFlowHandlers(this.hass, "helper"); if (!handlers.includes(domain)) { diff --git a/src/panels/config/helpers/show-dialog-helper-detail.ts b/src/panels/config/helpers/show-dialog-helper-detail.ts index 83fbbce4ee77..bbee0bc6197d 100644 --- a/src/panels/config/helpers/show-dialog-helper-detail.ts +++ b/src/panels/config/helpers/show-dialog-helper-detail.ts @@ -4,8 +4,9 @@ import { DataEntryFlowDialogParams } from "../../../dialogs/config-flow/show-dia export const loadHelperDetailDialog = () => import("./dialog-helper-detail"); export interface ShowDialogHelperDetailParams { + domain?: string; // Only used for config entries - dialogClosedCallback: DataEntryFlowDialogParams["dialogClosedCallback"]; + dialogClosedCallback?: DataEntryFlowDialogParams["dialogClosedCallback"]; } export const showHelperDetailDialog = ( diff --git a/src/panels/config/integrations/ha-config-integrations.ts b/src/panels/config/integrations/ha-config-integrations.ts index de945c3465f4..a78eb0e94b25 100644 --- a/src/panels/config/integrations/ha-config-integrations.ts +++ b/src/panels/config/integrations/ha-config-integrations.ts @@ -67,6 +67,7 @@ import "./ha-ignored-config-entry-card"; import "./ha-integration-card"; import type { HaIntegrationCard } from "./ha-integration-card"; import { fetchDiagnosticHandlers } from "../../../data/diagnostics"; +import { HELPER_DOMAINS } from "../helpers/const"; export interface ConfigEntryUpdatedEvent { entry: ConfigEntry; @@ -661,6 +662,12 @@ class HaConfigIntegrations extends SubscribeMixin(LitElement) { const handlers = await getConfigFlowHandlers(this.hass, "integration"); if (!handlers.includes(domain)) { + if (HELPER_DOMAINS.includes(domain)) { + navigate(`/config/helpers/add?domain=${domain}`, { + replace: true, + }); + return; + } const helpers = await getConfigFlowHandlers(this.hass, "helper"); if (helpers.includes(domain)) { navigate(`/config/helpers/add?domain=${domain}`, { From 9138567507697916b99a04369164f9b3abaed383 Mon Sep 17 00:00:00 2001 From: Bram Kragten Date: Wed, 30 Mar 2022 13:07:20 +0200 Subject: [PATCH 3/3] simplify --- src/panels/config/helpers/ha-config-helpers.ts | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/panels/config/helpers/ha-config-helpers.ts b/src/panels/config/helpers/ha-config-helpers.ts index 2535508bebc0..4173fd69ef21 100644 --- a/src/panels/config/helpers/ha-config-helpers.ts +++ b/src/panels/config/helpers/ha-config-helpers.ts @@ -227,17 +227,12 @@ export class HaConfigHelpers extends SubscribeMixin(LitElement) { protected firstUpdated(changedProps: PropertyValues) { super.firstUpdated(changedProps); this._getConfigEntries(); - const localizePromise = this.hass.loadBackendTranslation( - "title", - undefined, - true - ); if (this.route.path === "/add") { - this._handleAdd(localizePromise); + this._handleAdd(); } } - private async _handleAdd(localizePromise: Promise) { + private async _handleAdd() { const domain = extractSearchParam("domain"); navigate("/config/helpers", { replace: true }); if (!domain) { @@ -272,10 +267,14 @@ export class HaConfigHelpers extends SubscribeMixin(LitElement) { }); return; } - const localize = await localizePromise; + const localize = await this.hass.loadBackendTranslation( + "title", + domain, + true + ); if ( !(await showConfirmationDialog(this, { - title: localize("ui.panel.config.integrations.confirm_new", { + title: this.hass.localize("ui.panel.config.integrations.confirm_new", { integration: domainToName(localize, domain), }), }))