Fix badges not saved in view editor (#19971)

This commit is contained in:
Paul Bottein 2024-03-04 15:28:58 +01:00
parent 56c681bcf8
commit ac66079d41
No known key found for this signature in database

View File

@ -23,7 +23,6 @@ import "../../../../components/ha-dialog";
import "../../../../components/ha-dialog-header"; import "../../../../components/ha-dialog-header";
import "../../../../components/ha-yaml-editor"; import "../../../../components/ha-yaml-editor";
import type { HaYamlEditor } from "../../../../components/ha-yaml-editor"; import type { HaYamlEditor } from "../../../../components/ha-yaml-editor";
import { LovelaceBadgeConfig } from "../../../../data/lovelace/config/badge";
import { import {
LovelaceViewConfig, LovelaceViewConfig,
isStrategyView, isStrategyView,
@ -61,8 +60,6 @@ export class HuiDialogEditView extends LitElement {
@state() private _config?: LovelaceViewConfig; @state() private _config?: LovelaceViewConfig;
@state() private _badges?: LovelaceBadgeConfig[];
@state() private _saving = false; @state() private _saving = false;
@state() private _curTab?: string; @state() private _curTab?: string;
@ -88,7 +85,6 @@ export class HuiDialogEditView extends LitElement {
if (this._yamlMode && changedProperties.has("_yamlMode")) { if (this._yamlMode && changedProperties.has("_yamlMode")) {
const viewConfig = { const viewConfig = {
...this._config, ...this._config,
badges: this._badges,
}; };
this._editor?.setValue(viewConfig); this._editor?.setValue(viewConfig);
} }
@ -99,7 +95,6 @@ export class HuiDialogEditView extends LitElement {
if (this._params.viewIndex === undefined) { if (this._params.viewIndex === undefined) {
this._config = {}; this._config = {};
this._badges = [];
this._dirty = false; this._dirty = false;
return; return;
} }
@ -108,19 +103,15 @@ export class HuiDialogEditView extends LitElement {
if (isStrategyView(view)) { if (isStrategyView(view)) {
const { strategy, ...viewConfig } = view; const { strategy, ...viewConfig } = view;
this._config = viewConfig; this._config = viewConfig;
this._badges = [];
return; return;
} }
const { badges, ...viewConfig } = view; this._config = view;
this._config = viewConfig;
this._badges = badges ? processEditorEntities(badges) : [];
} }
public closeDialog(): void { public closeDialog(): void {
this._curTabIndex = 0; this._curTabIndex = 0;
this._params = undefined; this._params = undefined;
this._config = {}; this._config = {};
this._badges = [];
this._yamlMode = false; this._yamlMode = false;
this._dirty = false; this._dirty = false;
fireEvent(this, "dialog-closed", { dialog: this.localName }); fireEvent(this, "dialog-closed", { dialog: this.localName });
@ -166,7 +157,7 @@ export class HuiDialogEditView extends LitElement {
break; break;
case "tab-badges": case "tab-badges":
content = html` content = html`
${this._badges?.length ${this._config?.badges?.length
? html` ? html`
${VIEWS_NO_BADGE_SUPPORT.includes(this._type) ${VIEWS_NO_BADGE_SUPPORT.includes(this._type)
? html` ? html`
@ -178,7 +169,7 @@ export class HuiDialogEditView extends LitElement {
` `
: nothing} : nothing}
<div class="preview-badges"> <div class="preview-badges">
${this._badges.map( ${this._config.badges.map(
(badgeConfig) => html` (badgeConfig) => html`
<hui-badge-preview <hui-badge-preview
.hass=${this.hass} .hass=${this.hass}
@ -191,7 +182,7 @@ export class HuiDialogEditView extends LitElement {
: nothing} : nothing}
<hui-entity-editor <hui-entity-editor
.hass=${this.hass} .hass=${this.hass}
.entities=${this._badges} .entities=${this._config?.badges || []}
@entities-changed=${this._badgesChanged} @entities-changed=${this._badgesChanged}
></hui-entity-editor> ></hui-entity-editor>
`; `;
@ -420,9 +411,9 @@ export class HuiDialogEditView extends LitElement {
} }
this._saving = true; this._saving = true;
const viewConf: LovelaceViewConfig = {
const viewConf = {
...this._config, ...this._config,
badges: this._badges,
}; };
if (viewConf.type === SECTION_VIEW_LAYOUT && !viewConf.sections?.length) { if (viewConf.type === SECTION_VIEW_LAYOUT && !viewConf.sections?.length) {
@ -487,10 +478,13 @@ export class HuiDialogEditView extends LitElement {
} }
private _badgesChanged(ev: EntitiesEditorEvent): void { private _badgesChanged(ev: EntitiesEditorEvent): void {
if (!this._badges || !this.hass || !ev.detail || !ev.detail.entities) { if (!this.hass || !ev.detail || !ev.detail.entities) {
return; return;
} }
this._badges = processEditorEntities(ev.detail.entities); this._config = {
...this._config,
badges: processEditorEntities(ev.detail.entities),
};
this._dirty = true; this._dirty = true;
} }
@ -501,7 +495,6 @@ export class HuiDialogEditView extends LitElement {
} }
const { badges, ...config } = ev.detail.value; const { badges, ...config } = ev.detail.value;
this._config = config; this._config = config;
this._badges = badges;
this._dirty = true; this._dirty = true;
} }