From 9d0b20adce19cab8c64e8f02740a37e327f133f4 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Tue, 1 Jun 2021 12:22:25 -0700 Subject: [PATCH] Add support for system options v2 (#9332) * Add support for system options v2 * Update src/dialogs/config-entry-system-options/dialog-config-entry-system-options.ts Co-authored-by: Bram Kragten * Update dialog-config-entry-system-options.ts Co-authored-by: Bram Kragten --- gallery/src/demos/demo-integration-card.ts | 11 ++---- src/data/config_entries.ts | 35 ++++++------------- .../dialog-config-entry-system-options.ts | 28 +++++++-------- .../integrations/ha-integration-card.ts | 4 +-- .../integrations/ha-integration-header.ts | 2 +- 5 files changed, 28 insertions(+), 52 deletions(-) diff --git a/gallery/src/demos/demo-integration-card.ts b/gallery/src/demos/demo-integration-card.ts index 97e581f2cc..4f5162c176 100644 --- a/gallery/src/demos/demo-integration-card.ts +++ b/gallery/src/demos/demo-integration-card.ts @@ -31,10 +31,8 @@ const createConfigEntry = ( supports_options: false, supports_unload: true, disabled_by: null, - system_options: { - disable_new_entities: false, - disable_polling: false, - }, + pref_disable_new_entities: false, + pref_disable_polling: false, reason: null, ...override, }); @@ -68,10 +66,7 @@ const optionsFlowEntry = createConfigEntry("Options Flow", { supports_options: true, }); const disabledPollingEntry = createConfigEntry("Disabled Polling", { - system_options: { - disable_new_entities: false, - disable_polling: true, - }, + pref_disable_polling: true, }); const setupErrorEntry = createConfigEntry("Setup Error", { state: "setup_error", diff --git a/src/data/config_entries.ts b/src/data/config_entries.ts index 6bc9b8242d..e83c0498d3 100644 --- a/src/data/config_entries.ts +++ b/src/data/config_entries.ts @@ -14,19 +14,18 @@ export interface ConfigEntry { | "failed_unload"; supports_options: boolean; supports_unload: boolean; - system_options: ConfigEntrySystemOptions; + pref_disable_new_entities: boolean; + pref_disable_polling: boolean; disabled_by: "user" | null; reason: string | null; } -export interface ConfigEntryMutableParams { - title: string; -} - -export interface ConfigEntrySystemOptions { - disable_new_entities: boolean; - disable_polling: boolean; -} +export type ConfigEntryMutableParams = Partial< + Pick< + ConfigEntry, + "title" | "pref_disable_new_entities" | "pref_disable_polling" + > +>; export const getConfigEntries = (hass: HomeAssistant) => hass.callApi("GET", "config/config_entries/entry"); @@ -34,9 +33,9 @@ export const getConfigEntries = (hass: HomeAssistant) => export const updateConfigEntry = ( hass: HomeAssistant, configEntryId: string, - updatedValues: Partial + updatedValues: ConfigEntryMutableParams ) => - hass.callWS({ + hass.callWS<{ require_restart: boolean; config_entry: ConfigEntry }>({ type: "config_entries/update", entry_id: configEntryId, ...updatedValues, @@ -74,17 +73,3 @@ export const enableConfigEntry = (hass: HomeAssistant, configEntryId: string) => entry_id: configEntryId, disabled_by: null, }); - -export const updateConfigEntrySystemOptions = ( - hass: HomeAssistant, - configEntryId: string, - params: Partial -) => - hass.callWS<{ - require_restart: boolean; - system_options: ConfigEntrySystemOptions; - }>({ - type: "config_entries/system_options/update", - entry_id: configEntryId, - ...params, - }); diff --git a/src/dialogs/config-entry-system-options/dialog-config-entry-system-options.ts b/src/dialogs/config-entry-system-options/dialog-config-entry-system-options.ts index 92cac06fef..37d49e0a8a 100644 --- a/src/dialogs/config-entry-system-options/dialog-config-entry-system-options.ts +++ b/src/dialogs/config-entry-system-options/dialog-config-entry-system-options.ts @@ -7,7 +7,10 @@ import "../../components/ha-dialog"; import "../../components/ha-formfield"; import "../../components/ha-switch"; import type { HaSwitch } from "../../components/ha-switch"; -import { updateConfigEntrySystemOptions } from "../../data/config_entries"; +import { + ConfigEntryMutableParams, + updateConfigEntry, +} from "../../data/config_entries"; import { haStyleDialog } from "../../resources/styles"; import type { HomeAssistant } from "../../types"; import { showAlertDialog } from "../generic/show-dialog-box"; @@ -32,8 +35,8 @@ class DialogConfigEntrySystemOptions extends LitElement { ): Promise { this._params = params; this._error = undefined; - this._disableNewEntities = params.entry.system_options.disable_new_entities; - this._disablePolling = params.entry.system_options.disable_polling; + this._disableNewEntities = params.entry.pref_disable_new_entities; + this._disablePolling = params.entry.pref_disable_polling; } public closeDialog(): void { @@ -147,14 +150,14 @@ class DialogConfigEntrySystemOptions extends LitElement { private async _updateEntry(): Promise { this._submitting = true; - const data: Parameters[2] = { - disable_new_entities: this._disableNewEntities, + const data: ConfigEntryMutableParams = { + pref_disable_new_entities: this._disableNewEntities, }; if (this._allowUpdatePolling()) { - data.disable_polling = this._disablePolling; + data.pref_disable_polling = this._disablePolling; } try { - const result = await updateConfigEntrySystemOptions( + const result = await updateConfigEntry( this.hass, this._params!.entry.entry_id, data @@ -166,15 +169,8 @@ class DialogConfigEntrySystemOptions extends LitElement { ), }); } - const curEntry = this._params!.entry; - this._params!.entryUpdated({ - ...curEntry, - system_options: { - ...curEntry.system_options, - ...data, - }, - }); - this._params = undefined; + this._params!.entryUpdated(result.config_entry); + this.closeDialog(); } catch (err) { this._error = err.message || "Unknown error"; } finally { diff --git a/src/panels/config/integrations/ha-integration-card.ts b/src/panels/config/integrations/ha-integration-card.ts index bfd3421d08..85663de87d 100644 --- a/src/panels/config/integrations/ha-integration-card.ts +++ b/src/panels/config/integrations/ha-integration-card.ts @@ -590,10 +590,10 @@ export class HaIntegrationCard extends LitElement { if (newName === null) { return; } - const newEntry = await updateConfigEntry(this.hass, configEntry.entry_id, { + const result = await updateConfigEntry(this.hass, configEntry.entry_id, { title: newName, }); - fireEvent(this, "entry-updated", { entry: newEntry }); + fireEvent(this, "entry-updated", { entry: result.config_entry }); } static get styles(): CSSResultGroup { diff --git a/src/panels/config/integrations/ha-integration-header.ts b/src/panels/config/integrations/ha-integration-header.ts index d18ba01670..de6932d1a5 100644 --- a/src/panels/config/integrations/ha-integration-header.ts +++ b/src/panels/config/integrations/ha-integration-header.ts @@ -63,7 +63,7 @@ export class HaIntegrationHeader extends LitElement { ]); } - if (this.configEntry?.system_options.disable_polling) { + if (this.configEntry?.pref_disable_polling) { icons.push([ mdiSyncOff, this.hass.localize(