Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regenerate LL when attached #3102

Merged
merged 2 commits into from
Apr 19, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions src/panels/lovelace/ha-panel-lovelace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,19 @@ class LovelacePanel extends LitElement {
this.lovelace.language !== this.hass.language
) {
// language has been changed, rebuild UI
this._fetchConfig(false);
this._setLovelaceConfig(this.lovelace.config, this.lovelace.mode);
} else if (this.lovelace && this.lovelace.mode === "generated") {
// When lovelace is generated, we re-generate each time a user goes
// to the states panel to make sure new entities are shown.
this._regenerateConfig();
}
}

private async _regenerateConfig() {
const conf = await generateLovelaceConfig(this.hass!, this.hass!.localize);
this._setLovelaceConfig(conf, "generated");
}

private _closeEditor() {
this._state = "loaded";
}
Expand All @@ -148,12 +157,12 @@ class LovelacePanel extends LitElement {
this._fetchConfig(true);
}

private async _fetchConfig(force) {
private async _fetchConfig(forceDiskRefresh) {
let conf: LovelaceConfig;
let confMode: Lovelace["mode"] = this.panel!.config.mode;

try {
conf = await fetchConfig(this.hass!, force);
conf = await fetchConfig(this.hass!, forceDiskRefresh);
} catch (err) {
if (err.code !== "config_not_found") {
// tslint:disable-next-line
Expand All @@ -167,10 +176,14 @@ class LovelacePanel extends LitElement {
}

this._state = "loaded";
this._setLovelaceConfig(conf, confMode);
}

private _setLovelaceConfig(config: LovelaceConfig, mode: Lovelace["mode"]) {
this.lovelace = {
config: conf,
config,
mode,
editMode: this.lovelace ? this.lovelace.editMode : false,
mode: confMode,
language: this.hass!.language,
enableFullEditMode: () => {
if (!editorLoaded) {
Expand All @@ -189,7 +202,7 @@ class LovelacePanel extends LitElement {
});
},
saveConfig: async (newConfig: LovelaceConfig): Promise<void> => {
const { config, mode } = this.lovelace!;
const { config: previousConfig, mode: previousMode } = this.lovelace!;
try {
// Optimistic update
this._updateLovelace({
Expand All @@ -202,8 +215,8 @@ class LovelacePanel extends LitElement {
console.error(err);
// Rollback the optimistic update
this._updateLovelace({
config,
mode,
config: previousConfig,
mode: previousMode,
});
throw err;
}
Expand Down