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 <mail@bramkragten.nl>

* Update dialog-config-entry-system-options.ts

Co-authored-by: Bram Kragten <mail@bramkragten.nl>
This commit is contained in:
Paulus Schoutsen 2021-06-01 12:22:25 -07:00 committed by GitHub
parent acd5e1c081
commit 9d0b20adce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 52 deletions

View File

@ -31,10 +31,8 @@ const createConfigEntry = (
supports_options: false, supports_options: false,
supports_unload: true, supports_unload: true,
disabled_by: null, disabled_by: null,
system_options: { pref_disable_new_entities: false,
disable_new_entities: false, pref_disable_polling: false,
disable_polling: false,
},
reason: null, reason: null,
...override, ...override,
}); });
@ -68,10 +66,7 @@ const optionsFlowEntry = createConfigEntry("Options Flow", {
supports_options: true, supports_options: true,
}); });
const disabledPollingEntry = createConfigEntry("Disabled Polling", { const disabledPollingEntry = createConfigEntry("Disabled Polling", {
system_options: { pref_disable_polling: true,
disable_new_entities: false,
disable_polling: true,
},
}); });
const setupErrorEntry = createConfigEntry("Setup Error", { const setupErrorEntry = createConfigEntry("Setup Error", {
state: "setup_error", state: "setup_error",

View File

@ -14,19 +14,18 @@ export interface ConfigEntry {
| "failed_unload"; | "failed_unload";
supports_options: boolean; supports_options: boolean;
supports_unload: boolean; supports_unload: boolean;
system_options: ConfigEntrySystemOptions; pref_disable_new_entities: boolean;
pref_disable_polling: boolean;
disabled_by: "user" | null; disabled_by: "user" | null;
reason: string | null; reason: string | null;
} }
export interface ConfigEntryMutableParams { export type ConfigEntryMutableParams = Partial<
title: string; Pick<
} ConfigEntry,
"title" | "pref_disable_new_entities" | "pref_disable_polling"
export interface ConfigEntrySystemOptions { >
disable_new_entities: boolean; >;
disable_polling: boolean;
}
export const getConfigEntries = (hass: HomeAssistant) => export const getConfigEntries = (hass: HomeAssistant) =>
hass.callApi<ConfigEntry[]>("GET", "config/config_entries/entry"); hass.callApi<ConfigEntry[]>("GET", "config/config_entries/entry");
@ -34,9 +33,9 @@ export const getConfigEntries = (hass: HomeAssistant) =>
export const updateConfigEntry = ( export const updateConfigEntry = (
hass: HomeAssistant, hass: HomeAssistant,
configEntryId: string, configEntryId: string,
updatedValues: Partial<ConfigEntryMutableParams> updatedValues: ConfigEntryMutableParams
) => ) =>
hass.callWS<ConfigEntry>({ hass.callWS<{ require_restart: boolean; config_entry: ConfigEntry }>({
type: "config_entries/update", type: "config_entries/update",
entry_id: configEntryId, entry_id: configEntryId,
...updatedValues, ...updatedValues,
@ -74,17 +73,3 @@ export const enableConfigEntry = (hass: HomeAssistant, configEntryId: string) =>
entry_id: configEntryId, entry_id: configEntryId,
disabled_by: null, disabled_by: null,
}); });
export const updateConfigEntrySystemOptions = (
hass: HomeAssistant,
configEntryId: string,
params: Partial<ConfigEntrySystemOptions>
) =>
hass.callWS<{
require_restart: boolean;
system_options: ConfigEntrySystemOptions;
}>({
type: "config_entries/system_options/update",
entry_id: configEntryId,
...params,
});

View File

@ -7,7 +7,10 @@ import "../../components/ha-dialog";
import "../../components/ha-formfield"; import "../../components/ha-formfield";
import "../../components/ha-switch"; import "../../components/ha-switch";
import type { HaSwitch } from "../../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 { haStyleDialog } from "../../resources/styles";
import type { HomeAssistant } from "../../types"; import type { HomeAssistant } from "../../types";
import { showAlertDialog } from "../generic/show-dialog-box"; import { showAlertDialog } from "../generic/show-dialog-box";
@ -32,8 +35,8 @@ class DialogConfigEntrySystemOptions extends LitElement {
): Promise<void> { ): Promise<void> {
this._params = params; this._params = params;
this._error = undefined; this._error = undefined;
this._disableNewEntities = params.entry.system_options.disable_new_entities; this._disableNewEntities = params.entry.pref_disable_new_entities;
this._disablePolling = params.entry.system_options.disable_polling; this._disablePolling = params.entry.pref_disable_polling;
} }
public closeDialog(): void { public closeDialog(): void {
@ -147,14 +150,14 @@ class DialogConfigEntrySystemOptions extends LitElement {
private async _updateEntry(): Promise<void> { private async _updateEntry(): Promise<void> {
this._submitting = true; this._submitting = true;
const data: Parameters<typeof updateConfigEntrySystemOptions>[2] = { const data: ConfigEntryMutableParams = {
disable_new_entities: this._disableNewEntities, pref_disable_new_entities: this._disableNewEntities,
}; };
if (this._allowUpdatePolling()) { if (this._allowUpdatePolling()) {
data.disable_polling = this._disablePolling; data.pref_disable_polling = this._disablePolling;
} }
try { try {
const result = await updateConfigEntrySystemOptions( const result = await updateConfigEntry(
this.hass, this.hass,
this._params!.entry.entry_id, this._params!.entry.entry_id,
data data
@ -166,15 +169,8 @@ class DialogConfigEntrySystemOptions extends LitElement {
), ),
}); });
} }
const curEntry = this._params!.entry; this._params!.entryUpdated(result.config_entry);
this._params!.entryUpdated({ this.closeDialog();
...curEntry,
system_options: {
...curEntry.system_options,
...data,
},
});
this._params = undefined;
} catch (err) { } catch (err) {
this._error = err.message || "Unknown error"; this._error = err.message || "Unknown error";
} finally { } finally {

View File

@ -590,10 +590,10 @@ export class HaIntegrationCard extends LitElement {
if (newName === null) { if (newName === null) {
return; return;
} }
const newEntry = await updateConfigEntry(this.hass, configEntry.entry_id, { const result = await updateConfigEntry(this.hass, configEntry.entry_id, {
title: newName, title: newName,
}); });
fireEvent(this, "entry-updated", { entry: newEntry }); fireEvent(this, "entry-updated", { entry: result.config_entry });
} }
static get styles(): CSSResultGroup { static get styles(): CSSResultGroup {

View File

@ -63,7 +63,7 @@ export class HaIntegrationHeader extends LitElement {
]); ]);
} }
if (this.configEntry?.system_options.disable_polling) { if (this.configEntry?.pref_disable_polling) {
icons.push([ icons.push([
mdiSyncOff, mdiSyncOff,
this.hass.localize( this.hass.localize(