Fix hui-energy-compare-card (#24711)

* Fix hui-energy-compare-card

* rename var
This commit is contained in:
karwosts 2025-03-24 07:14:18 -07:00 committed by GitHub
parent e0fbd3cd1f
commit 4c952c191a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 16 additions and 1 deletions

View File

@ -12,6 +12,7 @@ import type { LovelaceCard } from "../../types";
import type { EnergyCardBaseConfig } from "../types";
import { hasConfigChanged } from "../../common/has-changed";
import "../../../../components/ha-alert";
import { fireEvent } from "../../../../common/dom/fire_event";
@customElement("hui-energy-compare-card")
export class HuiEnergyCompareCard
@ -33,6 +34,11 @@ export class HuiEnergyCompareCard
// eslint-disable-next-line lit/no-native-attributes
@property({ type: Boolean, reflect: true }) hidden = true;
// Energy compare card cannot tolerate being removed from the DOM by hui-card,
// as it calculates its own visibility and needs an active collection
// subscription to do so.
connectedWhileHidden = true;
public getCardSize(): Promise<number> | number {
return 1;
}
@ -106,7 +112,11 @@ export class HuiEnergyCompareCard
this._end = data.end;
this._startCompare = data.startCompare;
this._endCompare = data.endCompare;
const oldHidden = this.hidden;
this.hidden = !this._startCompare;
if (oldHidden !== this.hidden) {
fireEvent(this, "card-visibility-changed");
}
}
private _stopCompare(): void {

View File

@ -270,7 +270,11 @@ export class HuiCard extends ReactiveElement {
fireEvent(this, "card-visibility-changed", { value: visible });
}
if (!visible && this._element.parentElement) {
if (this._element.connectedWhileHidden === true) {
if (!this._element.parentElement) {
this.appendChild(this._element);
}
} else if (!visible && this._element.parentElement) {
this.removeChild(this._element);
} else if (visible && !this._element.parentElement) {
this.appendChild(this._element);

View File

@ -65,6 +65,7 @@ export interface LovelaceCard extends HTMLElement {
hass?: HomeAssistant;
preview?: boolean;
layout?: string;
connectedWhileHidden?: boolean;
getCardSize(): number | Promise<number>;
/** @deprecated Use `getGridOptions` instead */
getLayoutOptions?(): LovelaceLayoutOptions;