Simplify Lovelace mode (#2243)

This commit is contained in:
Paulus Schoutsen 2018-12-10 11:38:18 +01:00 committed by GitHub
parent 07e35ff81a
commit d9d92c8766
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 15 deletions

View File

@ -10,6 +10,7 @@ import { LitElement, html, PropertyValues } from "@polymer/lit-element";
import { hassLocalizeLitMixin } from "../../mixins/lit-localize-mixin";
import { TemplateResult } from "lit-html";
import { showSaveDialog } from "./editor/show-save-config-dialog";
import { generateLovelaceConfig } from "./common/generate-lovelace-config";
interface LovelacePanelConfig {
mode: "yaml" | "storage";
@ -121,12 +122,11 @@ class LovelacePanel extends hassLocalizeLitMixin(LitElement) {
}
private async _fetchConfig(force) {
let conf;
let gen: boolean;
let conf: LovelaceConfig;
let confMode: Lovelace["mode"] = this.panel!.config.mode;
try {
conf = await fetchConfig(this.hass!, force);
gen = false;
} catch (err) {
if (err.code !== "config_not_found") {
// tslint:disable-next-line
@ -135,21 +135,17 @@ class LovelacePanel extends hassLocalizeLitMixin(LitElement) {
this._errorMsg = err.message;
return;
}
const {
generateLovelaceConfig,
} = await import("./common/generate-lovelace-config");
conf = generateLovelaceConfig(this.hass!, this.localize);
gen = true;
confMode = "generated";
}
this._state = "loaded";
this.lovelace = {
config: conf,
autoGen: gen,
editMode: this.lovelace ? this.lovelace.editMode : false,
mode: this.panel!.config.mode,
mode: confMode,
setEditMode: (editMode: boolean) => {
if (!editMode || !this.lovelace!.autoGen) {
if (!editMode || this.lovelace!.mode !== "generated") {
this._updateLovelace({ editMode });
return;
}
@ -158,16 +154,16 @@ class LovelacePanel extends hassLocalizeLitMixin(LitElement) {
});
},
saveConfig: async (newConfig: LovelaceConfig): Promise<void> => {
const { config, autoGen } = this.lovelace!;
const { config, mode } = this.lovelace!;
try {
// Optimistic update
this._updateLovelace({ config: newConfig, autoGen: false });
this._updateLovelace({ config: newConfig, mode: "storage" });
await saveConfig(this.hass!, newConfig);
} catch (err) {
// tslint:disable-next-line
console.error(err);
// Rollback the optimistic update
this._updateLovelace({ config, autoGen });
this._updateLovelace({ config, mode });
throw err;
}
},

View File

@ -4,8 +4,7 @@ import { LovelaceCardConfig, LovelaceConfig } from "../../data/lovelace";
export interface Lovelace {
config: LovelaceConfig;
editMode: boolean;
autoGen: boolean;
mode: "yaml" | "storage";
mode: "generated" | "yaml" | "storage";
setEditMode: (editMode: boolean) => void;
saveConfig: (newConfig: LovelaceConfig) => Promise<void>;
}