Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid starting config flow and show alert dialog early if single config entry only #19648

Merged
merged 14 commits into from
Mar 13, 2024
1 change: 1 addition & 0 deletions src/data/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export interface IntegrationManifest {
| "cloud_push"
| "local_polling"
| "local_push";
single_config_entry?: boolean;
}
export interface IntegrationSetup {
domain: string;
Expand Down
1 change: 1 addition & 0 deletions src/data/integrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface Integration {
iot_class?: string;
supported_by?: string;
is_built_in?: boolean;
single_config_entry?: boolean;
}

export interface Integrations {
Expand Down
24 changes: 24 additions & 0 deletions src/panels/config/integrations/dialog-add-integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import {
AddIntegrationDialogParams,
showYamlIntegrationDialog,
} from "./show-add-integration-dialog";
import { getConfigEntries } from "../../../data/config_entries";

export interface IntegrationListItem {
name: string;
Expand All @@ -67,6 +68,7 @@ export interface IntegrationListItem {
cloud?: boolean;
is_built_in?: boolean;
is_add?: boolean;
single_config_entry?: boolean;
}

@customElement("dialog-add-integration")
Expand Down Expand Up @@ -208,6 +210,7 @@ class AddIntegrationDialog extends LitElement {
supported_by: integration.supported_by,
is_built_in: supportedIntegration.is_built_in !== false,
cloud: supportedIntegration.iot_class?.startsWith("cloud_"),
single_config_entry: integration.single_config_entry,
});
} else if (
!("integration_type" in integration) &&
Expand Down Expand Up @@ -572,6 +575,27 @@ class AddIntegrationDialog extends LitElement {
return;
}

if (integration.single_config_entry) {
const configEntries = await getConfigEntries(this.hass, {
domain: integration.domain,
});
if (configEntries.length > 0) {
this.closeDialog();
showAlertDialog(this, {
title: this.hass.localize(
"ui.panel.config.integrations.config_flow.single_config_entry_title"
),
text: this.hass.localize(
"ui.panel.config.integrations.config_flow.single_config_entry",
{
integration_name: integration.name,
}
),
});
return;
}
}

if (integration.config_flow) {
this._createFlow(integration.domain);
return;
Expand Down
20 changes: 20 additions & 0 deletions src/panels/config/integrations/ha-config-integration-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1286,6 +1286,26 @@ class HaConfigIntegrationPage extends SubscribeMixin(LitElement) {
}

private async _addIntegration() {
if (this._manifest?.single_config_entry) {
const entries = this._domainConfigEntries(
this.domain,
this._extraConfigEntries || this.configEntries
);
if (entries.length > 0) {
await showAlertDialog(this, {
title: this.hass.localize(
"ui.panel.config.integrations.config_flow.single_config_entry_title"
),
text: this.hass.localize(
"ui.panel.config.integrations.config_flow.single_config_entry",
{
integration_name: this._manifest.name,
}
),
});
return;
}
}
showAddIntegrationDialog(this, {
domain: this.domain,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import "../../../components/ha-fab";
import "../../../components/ha-icon-button";
import "../../../components/ha-svg-icon";
import "../../../components/search-input";
import { ConfigEntry } from "../../../data/config_entries";
import { ConfigEntry, getConfigEntries } from "../../../data/config_entries";
import { getConfigFlowInProgressCollection } from "../../../data/config_flow";
import { fetchDiagnosticHandlers } from "../../../data/diagnostics";
import {
Expand Down Expand Up @@ -658,6 +658,24 @@ class HaConfigIntegrationsDashboard extends SubscribeMixin(LitElement) {
const integration = findIntegration(integrations, domain);

if (integration?.config_flow) {
if (integration.single_config_entry) {
const configEntries = await getConfigEntries(this.hass, { domain });
if (configEntries.length > 0) {
showAlertDialog(this, {
title: this.hass.localize(
"ui.panel.config.integrations.config_flow.single_config_entry_title"
),
text: this.hass.localize(
"ui.panel.config.integrations.config_flow.single_config_entry",
{
integration_name: integration.name,
}
),
});
return;
}
}

// Integration exists, so we can just create a flow
const localize = await this.hass.loadBackendTranslation(
"title",
Expand Down
4 changes: 3 additions & 1 deletion src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -3978,7 +3978,9 @@
"missing_matter": "To add a {brand} device, you first need the {integration} integration and {supported_hardware_link}. Do you want to proceed with the setup of {integration}?",
"matter_mobile_app": "You need to use the Home Assistant Companion app on your mobile phone to commission Matter devices.",
"supported_hardware": "supported hardware",
"proceed": "Proceed"
"proceed": "Proceed",
"single_config_entry_title": "This integration allows only one configuration",
"single_config_entry": "{integration_name} supports only one configuration. Adding additional ones is not needed."
}
},
"users": {
Expand Down
Loading