-
+
${this._config.name || computeStateName(stateObj)}
@@ -130,13 +136,6 @@ class HuiGaugeCard extends LitElement implements LovelaceCard {
return hasConfigOrEntityChanged(this, changedProps);
}
- protected firstUpdated(changedProps: PropertyValues): void {
- super.firstUpdated(changedProps);
- if (!this._gauge) {
- this._initGauge();
- }
- }
-
protected updated(changedProps: PropertyValues): void {
super.updated(changedProps);
if (!this._config || !this.hass) {
@@ -156,66 +155,38 @@ class HuiGaugeCard extends LitElement implements LovelaceCard {
) {
applyThemesOnElement(this, this.hass.themes, this._config.theme);
}
- const oldState = oldHass?.states[this._config.entity];
- const stateObj = this.hass.states[this._config.entity];
- if (oldState?.state !== stateObj.state) {
- this._gauge.setValueAnimated(stateObj.state, 1);
- }
}
- private _initGauge() {
- if (!this._gaugeElement || !this._config || !this.hass) {
- return;
+ private _computeSeverity(numberValue: number): string {
+ const sections = this._config!.severity;
+
+ if (!sections) {
+ return severityMap.normal;
}
- if (this._gauge) {
- this._gaugeElement.removeChild(this._gaugeElement.lastChild!);
- this._gauge = undefined;
- }
- this._gauge = Gauge(this._gaugeElement, {
- min: this._config.min,
- max: this._config.max,
- dialStartAngle: 180,
- dialEndAngle: 0,
- viewBox: "0 0 100 55",
- label: (value) => `${Math.round(value)}
- ${
- this._config!.unit ||
- this.hass?.states[this._config!.entity].attributes
- .unit_of_measurement ||
- ""
- }`,
- color: (value) => {
- const sections = this._config!.severity;
- if (!sections) {
- return severityMap.normal;
- }
+ const sectionsArray = Object.keys(sections);
+ const sortable = sectionsArray.map((severity) => [
+ severity,
+ sections[severity],
+ ]);
- const sectionsArray = Object.keys(sections);
- const sortable = sectionsArray.map((severity) => [
- severity,
- sections[severity],
- ]);
-
- for (const severity of sortable) {
- if (severityMap[severity[0]] == null || isNaN(severity[1])) {
- return severityMap.normal;
- }
- }
- sortable.sort((a, b) => a[1] - b[1]);
-
- if (value >= sortable[0][1] && value < sortable[1][1]) {
- return severityMap[sortable[0][0]];
- }
- if (value >= sortable[1][1] && value < sortable[2][1]) {
- return severityMap[sortable[1][0]];
- }
- if (value >= sortable[2][1]) {
- return severityMap[sortable[2][0]];
- }
+ for (const severity of sortable) {
+ if (severityMap[severity[0]] == null || isNaN(severity[1])) {
return severityMap.normal;
- },
- });
+ }
+ }
+ sortable.sort((a, b) => a[1] - b[1]);
+
+ if (numberValue >= sortable[0][1] && numberValue < sortable[1][1]) {
+ return severityMap[sortable[0][0]];
+ }
+ if (numberValue >= sortable[1][1] && numberValue < sortable[2][1]) {
+ return severityMap[sortable[1][0]];
+ }
+ if (numberValue >= sortable[2][1]) {
+ return severityMap[sortable[2][0]];
+ }
+ return severityMap.normal;
}
private _handleClick(): void {
@@ -244,29 +215,20 @@ class HuiGaugeCard extends LitElement implements LovelaceCard {
outline: none;
background: var(--divider-color);
}
- #gauge {
+
+ ha-gauge {
+ --gauge-color: var(--label-badge-blue);
width: 100%;
max-width: 300px;
}
- .dial {
- stroke: #ccc;
- stroke-width: 15;
- }
- .value {
- stroke-width: 15;
- }
- .value-text {
- fill: var(--primary-text-color);
- font-size: var(--gauge-value-font-size, 1.1em);
- transform: translate(0, -5px);
- font-family: inherit;
- }
+
.name {
text-align: center;
line-height: initial;
color: var(--primary-text-color);
width: 100%;
font-size: 15px;
+ margin-top: 8px;
}
`;
}
diff --git a/src/panels/lovelace/cards/hui-thermostat-card.ts b/src/panels/lovelace/cards/hui-thermostat-card.ts
index ea254bd21f..04a7a8a1ac 100644
--- a/src/panels/lovelace/cards/hui-thermostat-card.ts
+++ b/src/panels/lovelace/cards/hui-thermostat-card.ts
@@ -12,6 +12,7 @@ import {
PropertyValues,
svg,
TemplateResult,
+ query,
} from "lit-element";
import { classMap } from "lit-html/directives/class-map";
import { UNIT_F } from "../../../common/const";
@@ -33,6 +34,7 @@ import { hasConfigOrEntityChanged } from "../common/has-changed";
import { createEntityNotFoundWarning } from "../components/hui-warning";
import { LovelaceCard, LovelaceCardEditor } from "../types";
import { ThermostatCardConfig } from "./types";
+import type { HaCard } from "../../../components/ha-card";
const modeIcons: { [mode in HvacMode]: string } = {
auto: "hass:calendar-sync",
@@ -77,6 +79,8 @@ export class HuiThermostatCard extends LitElement implements LovelaceCard {
@internalProperty() private _setTemp?: number | number[];
+ @query("ha-card") private _card?: HaCard;
+
public getCardSize(): number {
return 5;
}
@@ -290,18 +294,17 @@ export class HuiThermostatCard extends LitElement implements LovelaceCard {
// That way it will auto-scale correctly
// This is not done to the SVG containing the current temperature, because
// it should not be centered on the text, but only on the value
- if (this.shadowRoot && this.shadowRoot.querySelector("ha-card")) {
- (this.shadowRoot.querySelector(
- "ha-card"
- ) as LitElement).updateComplete.then(() => {
- const svgRoot = this.shadowRoot!.querySelector("#set-values");
- const box = svgRoot!.querySelector("g")!.getBBox();
- svgRoot!.setAttribute(
+ const card = this._card;
+ if (card) {
+ card.updateComplete.then(() => {
+ const svgRoot = this.shadowRoot!.querySelector("#set-values")!;
+ const box = svgRoot.querySelector("g")!.getBBox()!;
+ svgRoot.setAttribute(
"viewBox",
- `${box!.x} ${box!.y} ${box!.width} ${box!.height}`
+ `${box.x} ${box!.y} ${box.width} ${box.height}`
);
- svgRoot!.setAttribute("width", `${box!.width}`);
- svgRoot!.setAttribute("height", `${box!.height}`);
+ svgRoot.setAttribute("width", `${box.width}`);
+ svgRoot.setAttribute("height", `${box.height}`);
});
}
}
diff --git a/yarn.lock b/yarn.lock
index 50621e3fee..9701db610f 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -11305,11 +11305,6 @@ sver-compat@^1.5.0:
es6-iterator "^2.0.1"
es6-symbol "^3.1.1"
-svg-gauge@^1.0.6:
- version "1.0.6"
- resolved "https://registry.yarnpkg.com/svg-gauge/-/svg-gauge-1.0.6.tgz#1e84a366b1cce5b95dab3e33f41fdde867692d28"
- integrity sha512-gRkznVhtS18eOM/GMPDXAvrLZOpqzNVDg4bFAPAEjiDKd1tZHFIe8Bwt3G6TFg/H+pFboetPPI+zoV+bOL26QQ==
-
symbol-observable@^1.1.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/symbol-observable/-/symbol-observable-1.2.0.tgz#c22688aed4eab3cdc2dfeacbb561660560a00804"