Skip to content

Commit 7e1e8da

Browse files
committed
Fix Lit helper resolution and threshold thresholding
1 parent c52579e commit 7e1e8da

2 files changed

Lines changed: 47 additions & 17 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ Common keys for `pv`, `grid`, `home`, `battery`. The following settings are poss
101101
| ------------------- | ----------------------- | ------------------------------------------------------------------------------- |
102102
| Entity | `entity` | Sensor/entity id used for values. |
103103
| Color | `color` | Overrides line/icon/text color for that entity. |
104-
| Threshold | `threshold` | Values below this are zeroed (per `threshold_mode`) and dimmed. |
104+
| Threshold | `threshold` | Values below this are zeroed (per `threshold_mode`) and dimmed. A threshold of `0` suppresses `0` and negative values. |
105105
| Decimals | `decimal_places` | Number of decimals to display (defaults to card-level `decimal_places`, or 1). |
106106
| Unit override | `unit` / `unit_of_measurement` | Converts `W` to `kW` if set to `kW` |
107107
| Invert state values | `invert_state_values` | Flip positive/negative for the grid/battery readings |
@@ -214,7 +214,7 @@ Devices are power feeds within your home that you want to show in the card. By d
214214
| Name Label | `name` | Show a label for each device. Can use either a string or entity ID. Only works with card set to 4 or more rows in height in the sections UI. [See here](https://github.com/pacemaker82/Compact-Power-Card#managing-the-size-of-the-card) for more |
215215
| Icon | `icon` | Optional icon for the device badge. |
216216
| Color | `color` | Optional color override for that device. |
217-
| Threshold | `threshold` | Dims/zeros device below threshold (in watts) (per `threshold_mode`). |
217+
| Threshold | `threshold` | Dims/zeros device below threshold (in watts) (per `threshold_mode`). A threshold of `0` suppresses `0` and negative values. |
218218
| Subtract from home | `subtract_from_home` | `true` or `false` - Overrides card-level `subtract_devices_from_home` for this device. |
219219
| Force hide under threshold | `force_hide_under_threshold` | If true, hides the device entirely when under threshold. Will hide at 0 W if no threshold set. |
220220
| Decimals | `decimal_places` | Decimal places for that device (defaults to card-level `decimal_places`). |

compact-power-card.js

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,31 @@
1-
class CompactPowerCard extends (window.LitElement ||
2-
Object.getPrototypeOf(customElements.get("ha-panel-lovelace"))) {
1+
const compactPowerCardPanel =
2+
customElements.get("ha-panel-lovelace") || customElements.get("hui-masonry-view");
3+
4+
const CompactPowerCardBase =
5+
(compactPowerCardPanel && Object.getPrototypeOf(compactPowerCardPanel)) ||
6+
window.LitElement;
7+
8+
const compactPowerCardHtml =
9+
(typeof window.html === "function" && window.html) ||
10+
(typeof CompactPowerCardBase?.prototype?.html === "function" &&
11+
CompactPowerCardBase.prototype.html) ||
12+
(typeof window.LitElement?.prototype?.html === "function" &&
13+
window.LitElement.prototype.html);
14+
15+
const compactPowerCardCss =
16+
(typeof window.css === "function" && window.css) ||
17+
(typeof CompactPowerCardBase?.prototype?.css === "function" &&
18+
CompactPowerCardBase.prototype.css) ||
19+
(typeof window.LitElement?.prototype?.css === "function" &&
20+
window.LitElement.prototype.css);
21+
22+
if (!CompactPowerCardBase || !compactPowerCardHtml || !compactPowerCardCss) {
23+
throw new Error(
24+
"compact-power-card: Failed to resolve Lit html/css helpers from the Home Assistant frontend."
25+
);
26+
}
27+
28+
class CompactPowerCard extends CompactPowerCardBase {
329

430
static get properties() {
531
return {
@@ -9,8 +35,7 @@ class CompactPowerCard extends (window.LitElement ||
935
}
1036

1137
get html() {
12-
return (window.LitElement ||
13-
Object.getPrototypeOf(customElements.get("ha-panel-lovelace"))).prototype.html;
38+
return compactPowerCardHtml;
1439
}
1540

1641
static getConfigForm() {
@@ -561,10 +586,7 @@ class CompactPowerCard extends (window.LitElement ||
561586
}
562587

563588
static get styles() {
564-
const css =
565-
(window.LitElement ||
566-
Object.getPrototypeOf(customElements.get("ha-panel-lovelace"))).prototype.css;
567-
return css`
589+
return compactPowerCardCss`
568590
:host {
569591
--cpc-scale: 1;
570592
--cpc-text-scale: 1;
@@ -2107,15 +2129,19 @@ class CompactPowerCard extends (window.LitElement ||
21072129
if (!Number.isFinite(value)) return 1;
21082130
if (value === 0) return 0.4;
21092131
if (threshold == null) return 1;
2110-
return Math.abs(value) < threshold ? 0.4 : 1;
2132+
return this._isThresholdSuppressed(value, threshold) ? 0.4 : 1;
21112133
}
21122134

2113-
_isBelowThreshold(value, threshold) {
2114-
if (!Number.isFinite(value)) return false;
2115-
if (!Number.isFinite(threshold) || threshold <= 0) return false;
2135+
_isThresholdSuppressed(value, threshold) {
2136+
if (!Number.isFinite(value) || !Number.isFinite(threshold)) return false;
2137+
if (threshold <= 0) return value <= threshold;
21162138
return Math.abs(value) < threshold;
21172139
}
21182140

2141+
_isBelowThreshold(value, threshold) {
2142+
return this._isThresholdSuppressed(value, threshold);
2143+
}
2144+
21192145
_getNumeric(entityId, attribute = null) {
21202146
if (!this.hass || !entityId) return 0;
21212147
const st = this.hass.states[entityId];
@@ -2410,7 +2436,7 @@ class CompactPowerCard extends (window.LitElement ||
24102436

24112437
const applyThreshold = (value, threshold) => {
24122438
if (threshold == null) return value;
2413-
return Math.abs(value) < threshold ? 0 : value;
2439+
return this._isThresholdSuppressed(value, threshold) ? 0 : value;
24142440
};
24152441

24162442
const pvThreshold = this._toWatts(this._parseThreshold(pvCfg.threshold), "W", true);
@@ -3137,7 +3163,11 @@ class CompactPowerCard extends (window.LitElement ||
31373163
batteryNode,
31383164
homeNode,
31393165
} = layout;
3140-
const deviceYOffset = this._shouldUseExternalHeight() && rowCount < 3 ? 20 : 0;
3166+
const deviceYOffset =
3167+
(this._shouldUseExternalHeight() && rowCount < 3) ||
3168+
(!this._shouldUseExternalHeight() && (!hasPv || !hasBattery))
3169+
? 20
3170+
: 0;
31413171
const labelBonus = !hasPv || !hasBattery ? (rowCount >= 4 ? 2 : 1) : 0;
31423172
const gridBatteryBonus =
31433173
hasPv && hasBattery && pvLabels.length <= 4
@@ -3222,7 +3252,7 @@ class CompactPowerCard extends (window.LitElement ||
32223252
const useThresholdForCalc = thresholdMode === "calculations";
32233253
const applyThreshold = (value, threshold) => {
32243254
if (threshold == null) return value;
3225-
return Math.abs(value) < threshold ? 0 : value;
3255+
return this._isThresholdSuppressed(value, threshold) ? 0 : value;
32263256
};
32273257

32283258
const pvDisplayUnit = "W";

0 commit comments

Comments
 (0)