Skip to content

Commit 604adb6

Browse files
authored
changes to support unavailable entities set to 0
1 parent 332b3bd commit 604adb6

1 file changed

Lines changed: 45 additions & 18 deletions

File tree

‎compact-power-card.js‎

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2085,6 +2085,11 @@ class CompactPowerCard extends (window.LitElement ||
20852085
return Number.isFinite(n) ? n : null;
20862086
}
20872087

2088+
_isUnavailableState(raw) {
2089+
const s = String(raw ?? "").toLowerCase();
2090+
return s === "unknown" || s === "unavailable";
2091+
}
2092+
20882093
_parseDecimalPlaces(val) {
20892094
const n = typeof val === "string" ? parseInt(val, 10) : val;
20902095
if (!Number.isFinite(n) || n < 0) return null;
@@ -3468,13 +3473,14 @@ class CompactPowerCard extends (window.LitElement ||
34683473
const displayName = nameEntity ? this._formatEntityStateWithUnit(nameEntity) : name;
34693474
const icon = src.icon || this._getEntityIcon(entity, "mdi:power-plug");
34703475
const isPowerDevice = this._isPowerDevice(entity);
3471-
const numeric = this._getNumericMaybe(entity, attribute);
3472-
const unit =
3473-
this.hass?.states?.[entity]?.attributes?.unit_of_measurement ||
3474-
"";
3476+
const st = entity ? this.hass?.states?.[entity] : null;
3477+
const raw = attribute ? st?.attributes?.[attribute] : st?.state;
3478+
const isUnavailable = this._isUnavailableState(raw);
3479+
const numeric = isUnavailable ? 0 : this._getNumericMaybe(entity, attribute);
3480+
const unit = st?.attributes?.unit_of_measurement || "";
34753481
const decimals = this._getDecimalPlaces(src);
3476-
const numericW = this._toWatts(numeric, unit, true);
3477-
const hasNumeric = Number.isFinite(numericW);
3482+
const numericW = isUnavailable ? 0 : this._toWatts(numeric, unit, true);
3483+
const hasNumeric = isUnavailable ? true : Number.isFinite(numericW);
34783484
const unitOverride = this._getUnitOverride(src);
34793485
let val = hasNumeric
34803486
? this._formatPowerWithOverride(numericW, decimals, "W", unitOverride ?? null)
@@ -3649,15 +3655,22 @@ class CompactPowerCard extends (window.LitElement ||
36493655
const unitOverride = this._getUnitOverride(lbl);
36503656
const icon = lbl.icon || this._getLabelIcon(entity, attribute, "mdi:tag-text-outline");
36513657
const color = lbl.color || gridColor;
3652-
const numeric = this._getNumericMaybe(entity, attribute);
3658+
const st = entity ? this.hass?.states?.[entity] : null;
3659+
const raw = attribute ? st?.attributes?.[attribute] : st?.state;
3660+
const isUnavailable = this._isUnavailableState(raw);
3661+
const numeric = isUnavailable ? 0 : this._getNumericMaybe(entity, attribute);
36533662
const decimals = this._getDecimalPlaces(lbl);
3654-
const val = this._formatEntity(entity, decimals, attribute, unitOverride);
36553663
const labelUnit =
36563664
unitOverride ||
36573665
this.hass?.states?.[entity]?.attributes?.unit_of_measurement ||
36583666
"";
3659-
const numericW = this._toWatts(numeric, labelUnit, true);
3660-
const hasNumeric = Number.isFinite(numericW);
3667+
const numericW = isUnavailable ? 0 : this._toWatts(numeric, labelUnit, true);
3668+
const hasNumeric = isUnavailable ? true : Number.isFinite(numericW);
3669+
const val = isUnavailable
3670+
? "0"
3671+
: hasNumeric
3672+
? this._formatPowerWithOverride(numericW, decimals, "W", unitOverride ?? null)
3673+
: this._formatEntity(entity, decimals, attribute, unitOverride);
36613674
const threshold = this._toWatts(this._parseThreshold(lbl.threshold), "W", true);
36623675
const opacity = hasNumeric ? (numericW === 0 ? 1 : this._opacityFor(numericW, threshold)) : 1;
36633676
const hidden = hasNumeric ? this._isBelowThreshold(numericW, threshold) : false;
@@ -3692,15 +3705,22 @@ class CompactPowerCard extends (window.LitElement ||
36923705
const unitOverride = this._getUnitOverride(lbl);
36933706
const icon = lbl.icon || this._getLabelIcon(entity, attribute, "mdi:tag-text-outline");
36943707
const color = lbl.color || batteryLabelDefaultColor;
3695-
const numeric = this._getNumericMaybe(entity, attribute);
3708+
const st = entity ? this.hass?.states?.[entity] : null;
3709+
const raw = attribute ? st?.attributes?.[attribute] : st?.state;
3710+
const isUnavailable = this._isUnavailableState(raw);
3711+
const numeric = isUnavailable ? 0 : this._getNumericMaybe(entity, attribute);
36963712
const decimals = this._getDecimalPlaces(lbl);
3697-
const val = this._formatEntity(entity, decimals, attribute, unitOverride);
36983713
const labelUnit =
36993714
unitOverride ||
37003715
this.hass?.states?.[entity]?.attributes?.unit_of_measurement ||
37013716
"";
3702-
const numericW = this._toWatts(numeric, labelUnit, true);
3703-
const hasNumeric = Number.isFinite(numericW);
3717+
const numericW = isUnavailable ? 0 : this._toWatts(numeric, labelUnit, true);
3718+
const hasNumeric = isUnavailable ? true : Number.isFinite(numericW);
3719+
const val = isUnavailable
3720+
? "0"
3721+
: hasNumeric
3722+
? this._formatPowerWithOverride(numericW, decimals, "W", unitOverride ?? null)
3723+
: this._formatEntity(entity, decimals, attribute, unitOverride);
37043724
const threshold = this._toWatts(this._parseThreshold(lbl.threshold), "W", true);
37053725
const opacity = hasNumeric ? this._opacityFor(numericW, threshold) : 1;
37063726
const hidden = hasNumeric ? this._isBelowThreshold(numericW, threshold) : false;
@@ -3758,15 +3778,22 @@ class CompactPowerCard extends (window.LitElement ||
37583778
const unitOverride = this._getUnitOverride(lbl);
37593779
const icon = lbl.icon || this._getLabelIcon(entity, attribute, "mdi:tag-text-outline");
37603780
const color = lbl.color || pvColor;
3761-
const numeric = this._getNumericMaybe(entity, attribute);
3781+
const st = entity ? this.hass?.states?.[entity] : null;
3782+
const raw = attribute ? st?.attributes?.[attribute] : st?.state;
3783+
const isUnavailable = this._isUnavailableState(raw);
3784+
const numeric = isUnavailable ? 0 : this._getNumericMaybe(entity, attribute);
37623785
const decimals = this._getDecimalPlaces(lbl);
3763-
const val = this._formatEntity(entity, decimals, attribute, unitOverride);
37643786
const labelUnit =
37653787
unitOverride ||
37663788
this.hass?.states?.[entity]?.attributes?.unit_of_measurement ||
37673789
"";
3768-
const numericW = this._toWatts(numeric, labelUnit, true);
3769-
const hasNumeric = Number.isFinite(numericW);
3790+
const numericW = isUnavailable ? 0 : this._toWatts(numeric, labelUnit, true);
3791+
const hasNumeric = isUnavailable ? true : Number.isFinite(numericW);
3792+
const val = isUnavailable
3793+
? "0"
3794+
: hasNumeric
3795+
? this._formatPowerWithOverride(numericW, decimals, "W", unitOverride ?? null)
3796+
: this._formatEntity(entity, decimals, attribute, unitOverride);
37703797
const threshold = this._toWatts(this._parseThreshold(lbl.threshold), "W", true);
37713798
const opacity = hasNumeric ? (numericW === 0 ? 1 : this._opacityFor(numericW, threshold)) : 1;
37723799
const hidden = hasNumeric ? this._isBelowThreshold(numericW, threshold) : false;

0 commit comments

Comments
 (0)