Skip to content

Commit fb3140c

Browse files
authored
Fix for labels showing power units
1 parent 604adb6 commit fb3140c

File tree

1 file changed

+40
-15
lines changed

1 file changed

+40
-15
lines changed

compact-power-card.js

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3658,22 +3658,31 @@ class CompactPowerCard extends (window.LitElement ||
36583658
const st = entity ? this.hass?.states?.[entity] : null;
36593659
const raw = attribute ? st?.attributes?.[attribute] : st?.state;
36603660
const isUnavailable = this._isUnavailableState(raw);
3661+
const isPowerEntity = this._isPowerDevice(entity);
36613662
const numeric = isUnavailable ? 0 : this._getNumericMaybe(entity, attribute);
36623663
const decimals = this._getDecimalPlaces(lbl);
36633664
const labelUnit =
36643665
unitOverride ||
36653666
this.hass?.states?.[entity]?.attributes?.unit_of_measurement ||
36663667
"";
36673668
const numericW = isUnavailable ? 0 : this._toWatts(numeric, labelUnit, true);
3668-
const hasNumeric = isUnavailable ? true : Number.isFinite(numericW);
3669+
const hasNumeric = isUnavailable
3670+
? true
3671+
: Number.isFinite(isPowerEntity ? numericW : numeric);
36693672
const val = isUnavailable
36703673
? "0"
36713674
: hasNumeric
3672-
? this._formatPowerWithOverride(numericW, decimals, "W", unitOverride ?? null)
3675+
? isPowerEntity
3676+
? this._formatPowerWithOverride(numericW, decimals, "W", unitOverride ?? null)
3677+
: this._formatEntity(entity, decimals, attribute, unitOverride)
36733678
: this._formatEntity(entity, decimals, attribute, unitOverride);
36743679
const threshold = this._toWatts(this._parseThreshold(lbl.threshold), "W", true);
3675-
const opacity = hasNumeric ? (numericW === 0 ? 1 : this._opacityFor(numericW, threshold)) : 1;
3676-
const hidden = hasNumeric ? this._isBelowThreshold(numericW, threshold) : false;
3680+
const opacity =
3681+
isPowerEntity && hasNumeric
3682+
? (numericW === 0 ? 1 : this._opacityFor(numericW, threshold))
3683+
: 1;
3684+
const hidden =
3685+
isPowerEntity && hasNumeric ? this._isBelowThreshold(numericW, threshold) : false;
36773686
const pos = gridLabelPositions[idx] || gridLabelPositions[gridLabelPositions.length - 1];
36783687
return {
36793688
entity,
@@ -3684,7 +3693,7 @@ class CompactPowerCard extends (window.LitElement ||
36843693
hidden,
36853694
xPct: pos.xPct,
36863695
yPx: pos.yPx,
3687-
numeric: hasNumeric ? numericW : 0,
3696+
numeric: hasNumeric ? (isPowerEntity ? numericW : numeric) : 0,
36883697
};
36893698
});
36903699

@@ -3708,22 +3717,29 @@ class CompactPowerCard extends (window.LitElement ||
37083717
const st = entity ? this.hass?.states?.[entity] : null;
37093718
const raw = attribute ? st?.attributes?.[attribute] : st?.state;
37103719
const isUnavailable = this._isUnavailableState(raw);
3720+
const isPowerEntity = this._isPowerDevice(entity);
37113721
const numeric = isUnavailable ? 0 : this._getNumericMaybe(entity, attribute);
37123722
const decimals = this._getDecimalPlaces(lbl);
37133723
const labelUnit =
37143724
unitOverride ||
37153725
this.hass?.states?.[entity]?.attributes?.unit_of_measurement ||
37163726
"";
37173727
const numericW = isUnavailable ? 0 : this._toWatts(numeric, labelUnit, true);
3718-
const hasNumeric = isUnavailable ? true : Number.isFinite(numericW);
3728+
const hasNumeric = isUnavailable
3729+
? true
3730+
: Number.isFinite(isPowerEntity ? numericW : numeric);
37193731
const val = isUnavailable
37203732
? "0"
37213733
: hasNumeric
3722-
? this._formatPowerWithOverride(numericW, decimals, "W", unitOverride ?? null)
3734+
? isPowerEntity
3735+
? this._formatPowerWithOverride(numericW, decimals, "W", unitOverride ?? null)
3736+
: this._formatEntity(entity, decimals, attribute, unitOverride)
37233737
: this._formatEntity(entity, decimals, attribute, unitOverride);
37243738
const threshold = this._toWatts(this._parseThreshold(lbl.threshold), "W", true);
3725-
const opacity = hasNumeric ? this._opacityFor(numericW, threshold) : 1;
3726-
const hidden = hasNumeric ? this._isBelowThreshold(numericW, threshold) : false;
3739+
const opacity =
3740+
isPowerEntity && hasNumeric ? this._opacityFor(numericW, threshold) : 1;
3741+
const hidden =
3742+
isPowerEntity && hasNumeric ? this._isBelowThreshold(numericW, threshold) : false;
37273743
const pos = batteryLabelPositions[idx] || batteryLabelPositions[batteryLabelPositions.length - 1];
37283744
return {
37293745
entity,
@@ -3734,7 +3750,7 @@ class CompactPowerCard extends (window.LitElement ||
37343750
hidden,
37353751
xPct: pos.xPct,
37363752
yPx: pos.yPx,
3737-
numeric: hasNumeric ? numericW : 0,
3753+
numeric: hasNumeric ? (isPowerEntity ? numericW : numeric) : 0,
37383754
};
37393755
});
37403756

@@ -3781,22 +3797,31 @@ class CompactPowerCard extends (window.LitElement ||
37813797
const st = entity ? this.hass?.states?.[entity] : null;
37823798
const raw = attribute ? st?.attributes?.[attribute] : st?.state;
37833799
const isUnavailable = this._isUnavailableState(raw);
3800+
const isPowerEntity = this._isPowerDevice(entity);
37843801
const numeric = isUnavailable ? 0 : this._getNumericMaybe(entity, attribute);
37853802
const decimals = this._getDecimalPlaces(lbl);
37863803
const labelUnit =
37873804
unitOverride ||
37883805
this.hass?.states?.[entity]?.attributes?.unit_of_measurement ||
37893806
"";
37903807
const numericW = isUnavailable ? 0 : this._toWatts(numeric, labelUnit, true);
3791-
const hasNumeric = isUnavailable ? true : Number.isFinite(numericW);
3808+
const hasNumeric = isUnavailable
3809+
? true
3810+
: Number.isFinite(isPowerEntity ? numericW : numeric);
37923811
const val = isUnavailable
37933812
? "0"
37943813
: hasNumeric
3795-
? this._formatPowerWithOverride(numericW, decimals, "W", unitOverride ?? null)
3814+
? isPowerEntity
3815+
? this._formatPowerWithOverride(numericW, decimals, "W", unitOverride ?? null)
3816+
: this._formatEntity(entity, decimals, attribute, unitOverride)
37963817
: this._formatEntity(entity, decimals, attribute, unitOverride);
37973818
const threshold = this._toWatts(this._parseThreshold(lbl.threshold), "W", true);
3798-
const opacity = hasNumeric ? (numericW === 0 ? 1 : this._opacityFor(numericW, threshold)) : 1;
3799-
const hidden = hasNumeric ? this._isBelowThreshold(numericW, threshold) : false;
3819+
const opacity =
3820+
isPowerEntity && hasNumeric
3821+
? (numericW === 0 ? 1 : this._opacityFor(numericW, threshold))
3822+
: 1;
3823+
const hidden =
3824+
isPowerEntity && hasNumeric ? this._isBelowThreshold(numericW, threshold) : false;
38003825
const posMeta = pvLabelPositions[idx] || pvLabelPositions[pvLabelPositions.length - 1];
38013826
const leftPct = (posMeta.x / baseWidth) * 100;
38023827
const yPct = pctBaseY(pvLabelY); // PV stays fixed in Y
@@ -3810,7 +3835,7 @@ class CompactPowerCard extends (window.LitElement ||
38103835
hidden,
38113836
leftPct,
38123837
topPct: yPct,
3813-
numeric: hasNumeric ? numericW : 0,
3838+
numeric: hasNumeric ? (isPowerEntity ? numericW : numeric) : 0,
38143839
};
38153840
});
38163841

0 commit comments

Comments
 (0)