mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-27 03:06:41 +00:00
Add ability to remove Lovelace config (#4430)
* Add ability to remove Lovelace config * Update hc-lovelace.ts
This commit is contained in:
parent
a33cf97e2c
commit
fbd5185ce2
@ -39,6 +39,7 @@ class HcLovelace extends LitElement {
|
|||||||
mode: "storage",
|
mode: "storage",
|
||||||
language: "en",
|
language: "en",
|
||||||
saveConfig: async () => undefined,
|
saveConfig: async () => undefined,
|
||||||
|
deleteConfig: async () => undefined,
|
||||||
setEditMode: () => undefined,
|
setEditMode: () => undefined,
|
||||||
};
|
};
|
||||||
return this.lovelaceConfig.views[index].panel
|
return this.lovelaceConfig.views[index].panel
|
||||||
|
@ -113,6 +113,11 @@ export const saveConfig = (
|
|||||||
config,
|
config,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export const deleteConfig = (hass: HomeAssistant): Promise<void> =>
|
||||||
|
hass.callWS({
|
||||||
|
type: "lovelace/config/delete",
|
||||||
|
});
|
||||||
|
|
||||||
export const subscribeLovelaceUpdates = (
|
export const subscribeLovelaceUpdates = (
|
||||||
conn: Connection,
|
conn: Connection,
|
||||||
onChange: () => void
|
onChange: () => void
|
||||||
|
@ -6,6 +6,7 @@ import {
|
|||||||
saveConfig,
|
saveConfig,
|
||||||
subscribeLovelaceUpdates,
|
subscribeLovelaceUpdates,
|
||||||
WindowWithLovelaceProm,
|
WindowWithLovelaceProm,
|
||||||
|
deleteConfig,
|
||||||
} from "../../data/lovelace";
|
} from "../../data/lovelace";
|
||||||
import "../../layouts/hass-loading-screen";
|
import "../../layouts/hass-loading-screen";
|
||||||
import "../../layouts/hass-error-screen";
|
import "../../layouts/hass-error-screen";
|
||||||
@ -316,6 +317,28 @@ class LovelacePanel extends LitElement {
|
|||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
deleteConfig: async (): Promise<void> => {
|
||||||
|
const { config: previousConfig, mode: previousMode } = this.lovelace!;
|
||||||
|
try {
|
||||||
|
// Optimistic update
|
||||||
|
this._updateLovelace({
|
||||||
|
config: await generateLovelaceConfigFromHass(this.hass!),
|
||||||
|
mode: "generated",
|
||||||
|
editMode: false,
|
||||||
|
});
|
||||||
|
this._ignoreNextUpdateEvent = true;
|
||||||
|
await deleteConfig(this.hass!);
|
||||||
|
} catch (err) {
|
||||||
|
// tslint:disable-next-line
|
||||||
|
console.error(err);
|
||||||
|
// Rollback the optimistic update
|
||||||
|
this._updateLovelace({
|
||||||
|
config: previousConfig,
|
||||||
|
mode: previousMode,
|
||||||
|
});
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,6 +28,8 @@ import "../../components/ha-code-editor";
|
|||||||
import { HaCodeEditor } from "../../components/ha-code-editor";
|
import { HaCodeEditor } from "../../components/ha-code-editor";
|
||||||
import { HomeAssistant } from "../../types";
|
import { HomeAssistant } from "../../types";
|
||||||
import { computeRTL } from "../../common/util/compute_rtl";
|
import { computeRTL } from "../../common/util/compute_rtl";
|
||||||
|
import { LovelaceConfig } from "../../data/lovelace";
|
||||||
|
import { showConfirmationDialog } from "../../dialogs/confirmation/show-dialog-confirmation";
|
||||||
|
|
||||||
const lovelaceStruct = struct.interface({
|
const lovelaceStruct = struct.interface({
|
||||||
title: "string?",
|
title: "string?",
|
||||||
@ -180,9 +182,44 @@ class LovelaceFullConfigEditor extends LitElement {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async _removeConfig() {
|
||||||
|
try {
|
||||||
|
await this.lovelace!.deleteConfig();
|
||||||
|
} catch (err) {
|
||||||
|
alert(
|
||||||
|
this.hass.localize(
|
||||||
|
"ui.panel.lovelace.editor.raw_editor.error_remove",
|
||||||
|
"error",
|
||||||
|
err
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
window.onbeforeunload = null;
|
||||||
|
if (this.closeEditor) {
|
||||||
|
this.closeEditor();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private async _handleSave() {
|
private async _handleSave() {
|
||||||
this._saving = true;
|
this._saving = true;
|
||||||
|
|
||||||
|
const value = this.yamlEditor.value;
|
||||||
|
|
||||||
|
if (!value) {
|
||||||
|
showConfirmationDialog(this, {
|
||||||
|
title: this.hass.localize(
|
||||||
|
"ui.panel.lovelace.editor.raw_editor.confirm_remove_config_title"
|
||||||
|
),
|
||||||
|
text: this.hass.localize(
|
||||||
|
"ui.panel.lovelace.editor.raw_editor.confirm_remove_config_text"
|
||||||
|
),
|
||||||
|
confirmBtnText: this.hass.localize("ui.common.yes"),
|
||||||
|
cancelBtnText: this.hass.localize("ui.common.no"),
|
||||||
|
confirm: () => this._removeConfig(),
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (this.yamlEditor.hasComments) {
|
if (this.yamlEditor.hasComments) {
|
||||||
if (
|
if (
|
||||||
!confirm(
|
!confirm(
|
||||||
@ -195,9 +232,9 @@ class LovelaceFullConfigEditor extends LitElement {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let value;
|
let config: LovelaceConfig;
|
||||||
try {
|
try {
|
||||||
value = safeLoad(this.yamlEditor.value);
|
config = safeLoad(value);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
alert(
|
alert(
|
||||||
this.hass.localize(
|
this.hass.localize(
|
||||||
@ -210,7 +247,7 @@ class LovelaceFullConfigEditor extends LitElement {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
value = lovelaceStruct(value);
|
config = lovelaceStruct(config);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
alert(
|
alert(
|
||||||
this.hass.localize(
|
this.hass.localize(
|
||||||
@ -222,7 +259,7 @@ class LovelaceFullConfigEditor extends LitElement {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
await this.lovelace!.saveConfig(value);
|
await this.lovelace!.saveConfig(config);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
alert(
|
alert(
|
||||||
this.hass.localize(
|
this.hass.localize(
|
||||||
|
@ -21,6 +21,7 @@ export interface Lovelace {
|
|||||||
enableFullEditMode: () => void;
|
enableFullEditMode: () => void;
|
||||||
setEditMode: (editMode: boolean) => void;
|
setEditMode: (editMode: boolean) => void;
|
||||||
saveConfig: (newConfig: LovelaceConfig) => Promise<void>;
|
saveConfig: (newConfig: LovelaceConfig) => Promise<void>;
|
||||||
|
deleteConfig: () => Promise<void>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface LovelaceBadge extends HTMLElement {
|
export interface LovelaceBadge extends HTMLElement {
|
||||||
|
@ -1615,11 +1615,14 @@
|
|||||||
"save": "Save",
|
"save": "Save",
|
||||||
"unsaved_changes": "Unsaved changes",
|
"unsaved_changes": "Unsaved changes",
|
||||||
"saved": "Saved",
|
"saved": "Saved",
|
||||||
|
"confirm_remove_config_title": "Are you sure you want to remove your Lovelace configuration? We will automatically generate your Lovelace views with your areas and devices.",
|
||||||
|
"confirm_remove_config_text": "We will automatically generate your Lovelace views with your areas and devices if you remove your Lovelace configuration.",
|
||||||
"confirm_unsaved_changes": "You have unsaved changes, are you sure you want to exit?",
|
"confirm_unsaved_changes": "You have unsaved changes, are you sure you want to exit?",
|
||||||
"confirm_unsaved_comments": "Your config contains comment(s), these will not be saved. Do you want to continue?",
|
"confirm_unsaved_comments": "Your config contains comment(s), these will not be saved. Do you want to continue?",
|
||||||
"error_parse_yaml": "Unable to parse YAML: {error}",
|
"error_parse_yaml": "Unable to parse YAML: {error}",
|
||||||
"error_invalid_config": "Your config is not valid: {error}",
|
"error_invalid_config": "Your config is not valid: {error}",
|
||||||
"error_save_yaml": "Unable to save YAML: {error}"
|
"error_save_yaml": "Unable to save YAML: {error}",
|
||||||
|
"error_remove": "Unable to remove config: {error}"
|
||||||
},
|
},
|
||||||
"edit_lovelace": {
|
"edit_lovelace": {
|
||||||
"header": "Title of your Lovelace UI",
|
"header": "Title of your Lovelace UI",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user