From cc1c5e45b2b94983990fc370e00eb2569b43fbe2 Mon Sep 17 00:00:00 2001 From: Bram Kragten Date: Tue, 1 Jun 2021 21:03:00 +0200 Subject: [PATCH] Display error when enabling/disabling config entries (#9325) --- src/data/config_entries.ts | 8 ++-- .../config/devices/ha-config-device-page.ts | 43 +++++++++++++++++-- .../integrations/ha-integration-card.ts | 27 +++++++++++- src/translations/en.json | 2 + 4 files changed, 71 insertions(+), 9 deletions(-) diff --git a/src/data/config_entries.ts b/src/data/config_entries.ts index 8e4de41a9e..6bc9b8242d 100644 --- a/src/data/config_entries.ts +++ b/src/data/config_entries.ts @@ -52,13 +52,15 @@ export const reloadConfigEntry = (hass: HomeAssistant, configEntryId: string) => require_restart: boolean; }>("POST", `config/config_entries/entry/${configEntryId}/reload`); +export interface DisableConfigEntryResult { + require_restart: boolean; +} + export const disableConfigEntry = ( hass: HomeAssistant, configEntryId: string ) => - hass.callWS<{ - require_restart: boolean; - }>({ + hass.callWS({ type: "config_entries/disable", entry_id: configEntryId, disabled_by: "user", diff --git a/src/panels/config/devices/ha-config-device-page.ts b/src/panels/config/devices/ha-config-device-page.ts index 3e2cdcfd4e..5ace01bdcb 100644 --- a/src/panels/config/devices/ha-config-device-page.ts +++ b/src/panels/config/devices/ha-config-device-page.ts @@ -11,7 +11,11 @@ import { slugify } from "../../../common/string/slugify"; import "../../../components/entity/ha-battery-icon"; import "../../../components/ha-icon-next"; import { AreaRegistryEntry } from "../../../data/area_registry"; -import { ConfigEntry, disableConfigEntry } from "../../../data/config_entries"; +import { + ConfigEntry, + disableConfigEntry, + DisableConfigEntryResult, +} from "../../../data/config_entries"; import { computeDeviceName, DeviceRegistryEntry, @@ -25,7 +29,10 @@ import { } from "../../../data/entity_registry"; import { SceneEntities, showSceneEditor } from "../../../data/scene"; import { findRelated, RelatedResult } from "../../../data/search"; -import { showConfirmationDialog } from "../../../dialogs/generic/show-dialog-box"; +import { + showAlertDialog, + showConfirmationDialog, +} from "../../../dialogs/generic/show-dialog-box"; import "../../../layouts/hass-error-screen"; import "../../../layouts/hass-tabs-subpage"; import { haStyle } from "../../../resources/styles"; @@ -671,13 +678,41 @@ export class HaConfigDevicePage extends LitElement { dismissText: this.hass.localize("ui.common.no"), })) ) { - disableConfigEntry(this.hass, cnfg_entry); + let result: DisableConfigEntryResult; + try { + // eslint-disable-next-line no-await-in-loop + result = await disableConfigEntry(this.hass, cnfg_entry); + } catch (err) { + showAlertDialog(this, { + title: this.hass.localize( + "ui.panel.config.integrations.config_entry.disable_error" + ), + text: err.message, + }); + return; + } + if (result.require_restart) { + showAlertDialog(this, { + text: this.hass.localize( + "ui.panel.config.integrations.config_entry.disable_restart_confirm" + ), + }); + } delete updates.disabled_by; } } } } - await updateDeviceRegistryEntry(this.hass, this.deviceId, updates); + try { + await updateDeviceRegistryEntry(this.hass, this.deviceId, updates); + } catch (err) { + showAlertDialog(this, { + title: this.hass.localize( + "ui.panel.config.devices.update_device_error" + ), + text: err.message, + }); + } if ( !oldDeviceName || diff --git a/src/panels/config/integrations/ha-integration-card.ts b/src/panels/config/integrations/ha-integration-card.ts index 4e2a103c48..bfd3421d08 100644 --- a/src/panels/config/integrations/ha-integration-card.ts +++ b/src/panels/config/integrations/ha-integration-card.ts @@ -18,6 +18,7 @@ import { ConfigEntry, deleteConfigEntry, disableConfigEntry, + DisableConfigEntryResult, enableConfigEntry, reloadConfigEntry, updateConfigEntry, @@ -487,7 +488,18 @@ export class HaIntegrationCard extends LitElement { if (!confirmed) { return; } - const result = await disableConfigEntry(this.hass, entryId); + let result: DisableConfigEntryResult; + try { + result = await disableConfigEntry(this.hass, entryId); + } catch (err) { + showAlertDialog(this, { + title: this.hass.localize( + "ui.panel.config.integrations.config_entry.disable_error" + ), + text: err.message, + }); + return; + } if (result.require_restart) { showAlertDialog(this, { text: this.hass.localize( @@ -503,7 +515,18 @@ export class HaIntegrationCard extends LitElement { private async _enableIntegration(configEntry: ConfigEntry) { const entryId = configEntry.entry_id; - const result = await enableConfigEntry(this.hass, entryId); + let result: DisableConfigEntryResult; + try { + result = await enableConfigEntry(this.hass, entryId); + } catch (err) { + showAlertDialog(this, { + title: this.hass.localize( + "ui.panel.config.integrations.config_entry.disable_error" + ), + text: err.message, + }); + return; + } if (result.require_restart) { showAlertDialog(this, { diff --git a/src/translations/en.json b/src/translations/en.json index dae343b3c2..458e140bc3 100755 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -1975,6 +1975,7 @@ "confirm_rename_entity_ids": "Do you also want to rename the entity IDs of your entities?", "confirm_rename_entity_ids_warning": "This will not change any configuration (like automations, scripts, scenes, dashboards) that is currently using these entities! You will have to update them yourself to use the new entity IDs!", "confirm_disable_config_entry": "There are no more devices for the config entry {entry_name}, do you want to instead disable the config entry?", + "update_device_error": "Updating the device failed", "disabled": "Disabled", "data_table": { "device": "Device", @@ -2163,6 +2164,7 @@ "reload_restart_confirm": "Restart Home Assistant to finish reloading this integration", "disable_restart_confirm": "Restart Home Assistant to finish disabling this integration", "enable_restart_confirm": "Restart Home Assistant to finish enabling this integration", + "disable_error": "Enabling or disabling of the integration failed", "manuf": "by {manufacturer}", "hub": "Connected via", "firmware": "Firmware: {version}",