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_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",

View File

@ -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<ConfigEntry[]>("GET", "config/config_entries/entry");
@ -34,9 +33,9 @@ export const getConfigEntries = (hass: HomeAssistant) =>
export const updateConfigEntry = (
hass: HomeAssistant,
configEntryId: string,
updatedValues: Partial<ConfigEntryMutableParams>
updatedValues: ConfigEntryMutableParams
) =>
hass.callWS<ConfigEntry>({
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<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-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<void> {
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<void> {
this._submitting = true;
const data: Parameters<typeof updateConfigEntrySystemOptions>[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 {

View File

@ -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 {

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([
mdiSyncOff,
this.hass.localize(