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