Fix update config mecanism in hui-card (#21218)

This commit is contained in:
Paul Bottein 2024-06-28 20:17:57 +02:00 committed by GitHub
parent d33cf4f199
commit 85865af0c3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -31,6 +31,8 @@ export class HuiCard extends ReactiveElement {
@property({ attribute: false }) public hass?: HomeAssistant; @property({ attribute: false }) public hass?: HomeAssistant;
private _elementConfig?: LovelaceCardConfig;
public load() { public load() {
if (!this.config) { if (!this.config) {
throw new Error("Cannot build card without config"); throw new Error("Cannot build card without config");
@ -84,8 +86,18 @@ export class HuiCard extends ReactiveElement {
return this._element?.getLayoutOptions?.() ?? {}; return this._element?.getLayoutOptions?.() ?? {};
} }
private _updateElement(config: LovelaceCardConfig) {
if (!this._element) {
return;
}
this._element.setConfig(config);
this._elementConfig = config;
fireEvent(this, "card-updated");
}
private _loadElement(config: LovelaceCardConfig) { private _loadElement(config: LovelaceCardConfig) {
this._element = createCardElement(config); this._element = createCardElement(config);
this._elementConfig = config;
if (this.hass) { if (this.hass) {
this._element.hass = this.hass; this._element.hass = this.hass;
} }
@ -135,15 +147,14 @@ export class HuiCard extends ReactiveElement {
super.update(changedProps); super.update(changedProps);
if (this._element) { if (this._element) {
if (changedProps.has("config") && this.hasUpdated) { if (changedProps.has("config")) {
const oldConfig = changedProps.get("config"); const elementConfig = this._elementConfig;
if (this.config !== oldConfig && this.config) { if (this.config !== elementConfig && this.config) {
const typeChanged = this.config?.type !== oldConfig?.type; const typeChanged = this.config?.type !== elementConfig?.type;
if (typeChanged) { if (typeChanged) {
this._loadElement(this.config); this._loadElement(this.config);
} else { } else {
this._element?.setConfig(this.config); this._updateElement(this.config);
fireEvent(this, "card-updated");
} }
} }
} }