From e10c8faa47e426a412876f3d8f1a53a007e6fc15 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 25 Aug 2020 18:00:50 -0500 Subject: [PATCH] Add UI control to reload a config entry (integration) (#6656) * Add UI control to reload an integration * Refactor to move reload above delete and check supports_unload * Avoid index switch * Update src/panels/config/integrations/ha-integration-card.ts --- src/data/config_entries.ts | 6 ++ .../integrations/ha-integration-card.ts | 69 +++++++++++++++---- src/translations/en.json | 3 + 3 files changed, 64 insertions(+), 14 deletions(-) diff --git a/src/data/config_entries.ts b/src/data/config_entries.ts index 527838ff3b..925d9cc9b1 100644 --- a/src/data/config_entries.ts +++ b/src/data/config_entries.ts @@ -8,6 +8,7 @@ export interface ConfigEntry { state: string; connection_class: string; supports_options: boolean; + supports_unload: boolean; } export interface ConfigEntryMutableParams { @@ -37,6 +38,11 @@ export const deleteConfigEntry = (hass: HomeAssistant, configEntryId: string) => require_restart: boolean; }>("DELETE", `config/config_entries/entry/${configEntryId}`); +export const reloadConfigEntry = (hass: HomeAssistant, configEntryId: string) => + hass.callApi<{ + require_restart: boolean; + }>("POST", `config/config_entries/entry/${configEntryId}/reload`); + export const getConfigEntrySystemOptions = ( hass: HomeAssistant, configEntryId: string diff --git a/src/panels/config/integrations/ha-integration-card.ts b/src/panels/config/integrations/ha-integration-card.ts index fc03a16ddd..dddeaf6754 100644 --- a/src/panels/config/integrations/ha-integration-card.ts +++ b/src/panels/config/integrations/ha-integration-card.ts @@ -14,6 +14,7 @@ import { ConfigEntry, updateConfigEntry, deleteConfigEntry, + reloadConfigEntry, } from "../../../data/config_entries"; import { EntityRegistryEntry } from "../../../data/entity_registry"; import { DeviceRegistryEntry } from "../../../data/device_registry"; @@ -28,7 +29,8 @@ import { haStyle } from "../../../resources/styles"; import "../../../components/ha-icon-next"; import { fireEvent } from "../../../common/dom/fire_event"; import { mdiDotsVertical, mdiOpenInNew } from "@mdi/js"; -import { ActionDetail } from "@material/mwc-list/mwc-list-foundation"; +import type { RequestSelectedDetail } from "@material/mwc-list/mwc-list-item"; +import { shouldHandleRequestSelectedEvent } from "../../../common/mwc/handle-request-selected-event"; export interface ConfigEntryUpdatedEvent { entry: ConfigEntry; @@ -228,7 +230,7 @@ export class HaIntegrationCard extends LitElement { ` : ""} - + - + ${this.hass.localize( "ui.panel.config.integrations.config_entry.system_options" )} @@ -259,7 +261,17 @@ export class HaIntegrationCard extends LitElement { `} - + ${item.state === "loaded" && item.supports_unload + ? html` + ${this.hass.localize( + "ui.panel.config.integrations.config_entry.reload" + )} + ` + : ""} + ${this.hass.localize( "ui.panel.config.integrations.config_entry.delete" )} @@ -309,17 +321,31 @@ export class HaIntegrationCard extends LitElement { showOptionsFlowDialog(this, ev.target.closest("ha-card").configEntry); } - private _handleAction(ev: CustomEvent) { - const configEntry = ((ev.target as HTMLElement).closest("ha-card") as any) - .configEntry; - switch (ev.detail.index) { - case 0: - this._showSystemOptions(configEntry); - break; - case 1: - this._removeIntegration(configEntry); - break; + private _handleReload(ev: CustomEvent): void { + if (!shouldHandleRequestSelectedEvent(ev)) { + return; } + this._reloadIntegration( + ((ev.target as HTMLElement).closest("ha-card") as any).configEntry + ); + } + + private _handleDelete(ev: CustomEvent): void { + if (!shouldHandleRequestSelectedEvent(ev)) { + return; + } + this._removeIntegration( + ((ev.target as HTMLElement).closest("ha-card") as any).configEntry + ); + } + + private _handleSystemOptions(ev: CustomEvent): void { + if (!shouldHandleRequestSelectedEvent(ev)) { + return; + } + this._showSystemOptions( + ((ev.target as HTMLElement).closest("ha-card") as any).configEntry + ); } private _showSystemOptions(configEntry: ConfigEntry) { @@ -353,6 +379,21 @@ export class HaIntegrationCard extends LitElement { }); } + private async _reloadIntegration(configEntry: ConfigEntry) { + const entryId = configEntry.entry_id; + + reloadConfigEntry(this.hass, entryId).then((result) => { + const locale_key = result.require_restart + ? "reload_restart_confirm" + : "reload_confirm"; + showAlertDialog(this, { + text: this.hass.localize( + `ui.panel.config.integrations.config_entry.${locale_key}` + ), + }); + }); + } + private async _editEntryName(ev) { const configEntry = ev.target.closest("ha-card").configEntry; const newName = await showPromptDialog(this, { diff --git a/src/translations/en.json b/src/translations/en.json index 2544e4f57b..e5c4952fb1 100755 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -1606,7 +1606,10 @@ "documentation": "Documentation", "delete": "Delete", "delete_confirm": "Are you sure you want to delete this integration?", + "reload": "Reload", "restart_confirm": "Restart Home Assistant to finish removing this integration", + "reload_confirm": "The integration was reloaded", + "reload_restart_confirm": "Restart Home Assistant to finish reloading this integration", "manuf": "by {manufacturer}", "hub": "Connected via", "firmware": "Firmware: {version}",