This commit is contained in:
Bram Kragten 2019-01-12 23:44:34 +01:00 committed by Paulus Schoutsen
parent 963bdcc53c
commit 668d4e82ba

View File

@ -56,6 +56,7 @@ class HuiGaugeCard extends LitElement implements LovelaceCard {
public hass?: HomeAssistant; public hass?: HomeAssistant;
private _config?: Config; private _config?: Config;
private _updated?: boolean;
static get properties(): PropertyDeclarations { static get properties(): PropertyDeclarations {
return { return {
@ -78,6 +79,11 @@ class HuiGaugeCard extends LitElement implements LovelaceCard {
this._config = { min: 0, max: 100, theme: "default", ...config }; this._config = { min: 0, max: 100, theme: "default", ...config };
} }
public connectedCallback(): void {
super.connectedCallback();
this._setBaseUnit();
}
protected render(): TemplateResult { protected render(): TemplateResult {
if (!this._config || !this.hass) { if (!this._config || !this.hass) {
return html``; return html``;
@ -148,12 +154,8 @@ class HuiGaugeCard extends LitElement implements LovelaceCard {
} }
protected firstUpdated(): void { protected firstUpdated(): void {
(this.shadowRoot!.querySelector( this._updated = true;
"ha-card" this._setBaseUnit();
)! as HTMLElement).style.setProperty(
"--base-unit",
this._computeBaseUnit()
);
} }
protected updated(changedProps: PropertyValues): void { protected updated(changedProps: PropertyValues): void {
@ -169,6 +171,19 @@ class HuiGaugeCard extends LitElement implements LovelaceCard {
} }
} }
private _setBaseUnit(): void {
if (!this.isConnected || !this._updated) {
return;
}
const baseUnit = this._computeBaseUnit();
if (baseUnit === "0px") {
return;
}
(this.shadowRoot!.querySelector(
"ha-card"
)! as HTMLElement).style.setProperty("--base-unit", baseUnit);
}
private _computeSeverity(numberValue: number): string { private _computeSeverity(numberValue: number): string {
const sections = this._config!.severity; const sections = this._config!.severity;