From 624cb48f7887b871203fcf7f9962be9db77627e5 Mon Sep 17 00:00:00 2001 From: Bram Kragten Date: Wed, 30 Mar 2022 13:53:28 +0200 Subject: [PATCH] Add support for my links to create a helper config entry (#12155) --- .../config/helpers/dialog-helper-detail.ts | 2 +- .../config/helpers/ha-config-helpers.ts | 69 +++++++++++++++++++ .../helpers/show-dialog-helper-detail.ts | 3 +- .../integrations/ha-config-integrations.ts | 14 ++++ 4 files changed, 86 insertions(+), 2 deletions(-) diff --git a/src/panels/config/helpers/dialog-helper-detail.ts b/src/panels/config/helpers/dialog-helper-detail.ts index c3adf6a826..1a2833636e 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 4ce9d00118..4173fd69ef 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,67 @@ export class HaConfigHelpers extends SubscribeMixin(LitElement) { protected firstUpdated(changedProps: PropertyValues) { super.firstUpdated(changedProps); this._getConfigEntries(); + if (this.route.path === "/add") { + this._handleAdd(); + } + } + + private async _handleAdd() { + const domain = extractSearchParam("domain"); + navigate("/config/helpers", { replace: true }); + if (!domain) { + return; + } + if (HELPER_DOMAINS.includes(domain)) { + showHelperDetailDialog(this, { + 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 this.hass.loadBackendTranslation( + "title", + domain, + true + ); + if ( + !(await showConfirmationDialog(this, { + title: this.hass.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/helpers/show-dialog-helper-detail.ts b/src/panels/config/helpers/show-dialog-helper-detail.ts index 83fbbce4ee..bbee0bc619 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 c860953efc..a78eb0e94b 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,19 @@ 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}`, { + replace: true, + }); + return; + } showAlertDialog(this, { title: this.hass.localize( "ui.panel.config.integrations.config_flow.error"