Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,37 @@ Common keys for `pv`, `grid`, `home`, `battery`. The following settings are poss
| Name | Setting slug | What it does |
| ------------------- | ----------------------- | ------------------------------------------------------------------------------- |
| Entity | `entity` | Sensor/entity id used for values. |
| Color | `color` | Overrides line/icon/text color for that entity. |
| Color | `color` | Overrides line/icon/text color for that entity. Any CSS colour: hex (`"#ffea07"`), a theme colour name (`teal`, `primary`...), `var(--my-color)`, `rgba(...)`. In the visual editor, type or paste it in the field, or click the swatch to pick from the full colour palette (saved as hex). Empty the field to go back to the default. |
| Threshold | `threshold` | Values below this are zeroed (per `threshold_mode`) and dimmed. A threshold of `0` suppresses `0` and negative values. |
| Decimals | `decimal_places` | Number of decimals to display (defaults to card-level `decimal_places`, or 1). |
| Unit override | `unit` / `unit_of_measurement` | Converts `W` to `kW` if set to `kW` |
| Invert state values | `invert_state_values` | Flip positive/negative for the grid/battery readings |
| Tap Action | `tap_action` | Set to `more-info` or `navigate`. `navigate` requires a `navigation_path`. Default behaviour is `more-info` |
| Navigation Path | `navigation_path` | Used with `tap_action: navigate`. Set to a local path to go to a dashboard, e.g. `/my-dashboard/page` |

### Off-peak colour (Grid)

The grid can switch to a second colour while an off-peak tariff is active (icon, value, lines and home gradient). Available in the UI under **Grid > Off-peak Colour**.

| Name | Setting slug | What it does |
| ------------------- | ----------------------- | ------------------------------------------------------------------------------- |
| Off-peak Entity | `offpeak_entity` | Entity telling when off-peak is active: `binary_sensor`, `input_boolean`, `schedule`, or a tariff sensor. |
| Off-peak State | `offpeak_state` | State (or comma-separated states / list) meaning off-peak, case-insensitive. Default `on`. |
| Off-peak Colour | `offpeak_color` | Grid colour used while off-peak is active. Same formats as `color`. |

Example:

```yaml
type: custom:compact-power-card
entities:
grid:
entity: sensor.grid_power
color: "#488fc2"
offpeak_entity: sensor.linky_tariff
offpeak_state: "HEURE CREUSE"
offpeak_color: teal
```

### Separate Entities for Grid or Battery:

If you don't have a single entity for grid or battery power, you can instead use separate entities for both. This is not part of the configuration UI, it can only be added as YAML manually to the card.
Expand Down
214 changes: 197 additions & 17 deletions compact-power-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ if (!CompactPowerCardBase || !compactPowerCardHtml || !compactPowerCardCss) {
);
}

// Home Assistant theme colour names, accepted in YAML (`color: teal`).
const COMPACT_POWER_CARD_THEME_COLORS = new Set([
"primary", "accent", "red", "pink", "purple", "deep-purple", "indigo", "blue",
"light-blue", "cyan", "teal", "green", "light-green", "lime", "yellow", "amber",
"orange", "deep-orange", "brown", "light-grey", "grey", "dark-grey", "blue-grey",
"black", "white", "primary-text", "secondary-text", "disabled",
]);

class CompactPowerCard extends CompactPowerCardBase {

static get properties() {
Expand Down Expand Up @@ -119,7 +127,7 @@ class CompactPowerCard extends CompactPowerCardBase {
selector: { number: { step: 1, } },
},
{ name: "decimal_places", title: "Decimal Places", selector: { number: {} } },
{ name: "color", selector: {text: {} } },
{ name: "color", selector: { cpc_color: {} } },
{
name: "unit",
selector: {
Expand All @@ -131,8 +139,23 @@ class CompactPowerCard extends CompactPowerCardBase {
},
]
},
{ name: "invert_state_values", selector: { boolean: {} } },
{ name: "tap_action",
{ name: "invert_state_values", selector: { boolean: {} } },
{ name: "",
type: "expandable",
title: "Off-peak Colour",
flatten: true,
schema: [
{ name: "offpeak_entity", selector: { entity: {} } },
{ name: "",
type: "grid",
schema: [
{ name: "offpeak_state", selector: { text: {} } },
{ name: "offpeak_color", selector: { cpc_color: {} } },
]
},
]
},
{ name: "tap_action",
selector: {
select: {
mode: "dropdown",
Expand Down Expand Up @@ -174,7 +197,7 @@ class CompactPowerCard extends CompactPowerCardBase {
},
color: {
label: "Colour",
selector: { text: {} },
selector: { cpc_color: {} },
},
threshold: {
label: "Threshold",
Expand All @@ -200,7 +223,7 @@ class CompactPowerCard extends CompactPowerCardBase {
selector: { number: { step: 1, } },
},
{ name: "decimal_places", selector: { number: {} } },
{ name: "color", selector: {text: {} } },
{ name: "color", selector: { cpc_color: {} } },
{ name: "icon", selector: { icon: {} } },
{
name: "unit",
Expand Down Expand Up @@ -260,7 +283,7 @@ class CompactPowerCard extends CompactPowerCardBase {
},
color: {
label: "Colour",
selector: { text: {} },
selector: { cpc_color: {} },
},
threshold: {
label: "Threshold",
Expand Down Expand Up @@ -313,7 +336,7 @@ class CompactPowerCard extends CompactPowerCardBase {
},
color: {
label: "Colour",
selector: { text: {} },
selector: { cpc_color: {} },
},
tap_action: {
label: "Tap Action",
Expand Down Expand Up @@ -364,7 +387,7 @@ class CompactPowerCard extends CompactPowerCardBase {
},
color: {
label: "Colour",
selector: { text: {} },
selector: { cpc_color: {} },
},
threshold: {
label: "Threshold",
Expand All @@ -387,7 +410,7 @@ class CompactPowerCard extends CompactPowerCardBase {
name: "threshold", id: "power-threshold", selector: { number: { step: 1, } }
},
{ name: "decimal_places", selector: { number: {} } },
{ name: "color", selector: {text: {} } },
{ name: "color", selector: { cpc_color: {} } },
{
name: "unit",
selector: {
Expand Down Expand Up @@ -458,7 +481,7 @@ class CompactPowerCard extends CompactPowerCardBase {
},
color: {
label: "Colour",
selector: { text: {} },
selector: { cpc_color: {} },
},
threshold: {
label: "Power Threshold (in watts)",
Expand Down Expand Up @@ -489,12 +512,19 @@ class CompactPowerCard extends CompactPowerCardBase {
if (schema.id === "device-entity") return "Device Power Entity";
if (schema.id === "pv-entity") return "PV Power Entity";
if (schema.id === "home-entity") return "Home Power Entity";
if (schema.name === "offpeak_entity") return "Off-peak Entity";
if (schema.name === "offpeak_state") return "Off-peak State";
if (schema.name === "offpeak_color") return "Off-peak Colour";
return undefined;
},
computeHelper: (schema) => {
switch (schema.name) {
case "help_text":
return "some helpful text";
return "some helpful text";
case "offpeak_entity":
return "Entity that tells when the off-peak tariff is active (binary_sensor, input_boolean, schedule or a tariff sensor)";
case "offpeak_state":
return "State meaning off-peak (default: on). Separate several states with commas";
}
return undefined;
},
Expand Down Expand Up @@ -1653,6 +1683,7 @@ class CompactPowerCard extends CompactPowerCardBase {
addEntityConfig(ents.battery);
add(this._extractEntityRef(ents.grid?.import_entity || ents.grid?.importEntity));
add(this._extractEntityRef(ents.grid?.export_entity || ents.grid?.exportEntity));
add(this._extractEntityRef(ents.grid?.offpeak_entity));

const pvLabels = this._normalizeLabels(ents.pv?.labels, null);
const gridLabels = this._normalizeLabels(ents.grid?.labels, null);
Expand Down Expand Up @@ -1788,14 +1819,47 @@ class CompactPowerCard extends CompactPowerCardBase {
battery: "var(--energy-battery-in-color)",
};

return (
if (kind === "grid") {
const offpeak = this._getGridOffpeakColor(entityCfg);
if (offpeak) return offpeak;
}

return this._resolveColor(
entityCfg?.color ||
colors[kind] ||
cfg[`${kind}_color`] ||
defaults[kind]
);
}

// Theme colour names ("red", "primary"...) become
// var(--red-color); [r, g, b] arrays become rgb(); any other string is plain CSS.
_resolveColor(color) {
if (Array.isArray(color) && color.length >= 3) {
return `rgb(${color.slice(0, 3).map((c) => Number(c) || 0).join(", ")})`;
}
if (typeof color !== "string") return color || undefined;
const value = color.trim();
if (!value || value === "none") return undefined;
if (COMPACT_POWER_CARD_THEME_COLORS.has(value)) return `var(--${value}-color)`;
return value;
}

_getGridOffpeakColor(gridCfg) {
const entityId = this._extractEntityRef(gridCfg?.offpeak_entity);
const color = gridCfg?.offpeak_color;
if (!entityId || !color) return undefined;
const state = this.hass?.states?.[entityId]?.state;
if (state === undefined || state === null) return undefined;
const raw = gridCfg.offpeak_state;
const wanted = (Array.isArray(raw) ? raw : String(raw ?? "").split(","))
.map((s) => String(s).trim().toLowerCase())
.filter(Boolean);
if (!wanted.length) wanted.push("on");
if (!wanted.includes(String(state).trim().toLowerCase())) return undefined;
return this._resolveColor(color);
}

_getHeightFactor() {
return 1;
}
Expand Down Expand Up @@ -3552,7 +3616,7 @@ class CompactPowerCard extends CompactPowerCardBase {
let val = hasPowerNumeric
? this._formatPowerWithOverride(numericW, decimals, "W", unitOverride ?? null)
: this._formatEntity(entity, decimals, attribute, unitOverride);
const color = src.color || homeColor;
const color = this._resolveColor(src.color) || homeColor;
const threshold = isPowerDevice
? this._toWatts(this._parseThreshold(src.threshold), "W", true)
: null;
Expand Down Expand Up @@ -3729,7 +3793,7 @@ class CompactPowerCard extends CompactPowerCardBase {
const attribute = lbl.attribute || null;
const unitOverride = this._getUnitOverride(lbl);
const icon = lbl.icon || this._getLabelIcon(entity, attribute, "mdi:tag-text-outline");
const color = lbl.color || gridColor;
const color = this._resolveColor(lbl.color) || gridColor;
const st = entity ? this.hass?.states?.[entity] : null;
const raw = attribute ? st?.attributes?.[attribute] : st?.state;
const isUnavailable = this._isUnavailableState(raw);
Expand Down Expand Up @@ -3788,7 +3852,7 @@ class CompactPowerCard extends CompactPowerCardBase {
const attribute = lbl.attribute || null;
const unitOverride = this._getUnitOverride(lbl);
const icon = lbl.icon || this._getLabelIcon(entity, attribute, "mdi:tag-text-outline");
const color = lbl.color || batteryLabelDefaultColor;
const color = this._resolveColor(lbl.color) || batteryLabelDefaultColor;
const st = entity ? this.hass?.states?.[entity] : null;
const raw = attribute ? st?.attributes?.[attribute] : st?.state;
const isUnavailable = this._isUnavailableState(raw);
Expand Down Expand Up @@ -3869,7 +3933,7 @@ class CompactPowerCard extends CompactPowerCardBase {
const name = lbl.name || null;
const unitOverride = this._getUnitOverride(lbl);
const icon = lbl.icon || this._getLabelIcon(entity, attribute, "mdi:tag-text-outline");
const color = lbl.color || pvColor;
const color = this._resolveColor(lbl.color) || pvColor;
const st = entity ? this.hass?.states?.[entity] : null;
const raw = attribute ? st?.attributes?.[attribute] : st?.state;
const isUnavailable = this._isUnavailableState(raw);
Expand Down Expand Up @@ -3930,7 +3994,7 @@ class CompactPowerCard extends CompactPowerCardBase {
const icon = Number.isFinite(soc)
? this._getBatteryIcon(soc)
: cfg.icon || this._getLabelIcon(entity, null, "mdi:battery");
const color = cfg.color || batteryColor;
const color = this._resolveColor(cfg.color) || batteryColor;
const numericW = item.raw; // raw value (watts) for direction/opacity
const stateUnit =
item.unit ||
Expand Down Expand Up @@ -4305,6 +4369,122 @@ class CompactPowerCard extends CompactPowerCardBase {
}
}

// Colour field for the visual editor (`selector: { cpc_color: {} }`): ha-selector
// renders `ha-selector-<type>` for any type, so the card can ship its own.
// A text field (paste #FFEA07, a theme name or var(--x)) next to a swatch that
// opens the full native colour picker. The value stays the string as written.
const compactPowerCardHexOf = (cssColor) => {
const m = /^rgba?\(\s*(\d+)[,\s]+(\d+)[,\s]+(\d+)(?:[,\s/]+([\d.]+%?))?\s*\)$/i.exec(cssColor || "");
if (!m || (m[4] !== undefined && parseFloat(m[4]) === 0)) return undefined;
return "#" + [m[1], m[2], m[3]].map((c) => Number(c).toString(16).padStart(2, "0")).join("");
};

class CompactPowerCardColorSelector extends CompactPowerCardBase {
static get properties() {
return {
hass: { attribute: false },
selector: { attribute: false },
value: {},
label: {},
helper: {},
disabled: { type: Boolean },
required: { type: Boolean },
};
}

static get styles() {
return compactPowerCardCss`
:host { display: block; }
.row { display: flex; align-items: center; gap: 8px; }
ha-selector { flex: 1; min-width: 0; }
.swatch {
position: relative;
flex: none;
width: 40px;
height: 40px;
border-radius: 50%;
border: 1px solid var(--divider-color);
overflow: hidden;
cursor: pointer;
background: repeating-conic-gradient(var(--divider-color) 0 25%, transparent 0 50%) 0 0 / 12px 12px;
}
.fill { position: absolute; inset: 0; background: var(--cpc-swatch, transparent); }
input[type="color"] {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
opacity: 0;
border: 0;
padding: 0;
cursor: pointer;
}
`;
}

get _text() {
if (typeof this.value === "string") return this.value;
if (Array.isArray(this.value)) return `rgb(${this.value.slice(0, 3).join(", ")})`;
return "";
}

render() {
const text = this._text.trim();
const css = COMPACT_POWER_CARD_THEME_COLORS.has(text) ? `var(--${text}-color)` : text;
return compactPowerCardHtml`
<div class="row">
<ha-selector
.hass=${this.hass}
.selector=${{ text: {} }}
.value=${this._text}
.label=${this.label}
.helper=${this.helper}
.disabled=${this.disabled}
.required=${this.required}
@value-changed=${this._textChanged}
></ha-selector>
<label class="swatch" style=${`--cpc-swatch: ${css || "transparent"}`}>
<span class="fill"></span>
<input
type="color"
aria-label=${this.label || "Colour"}
.disabled=${this.disabled}
@click=${this._syncPicker}
@input=${this._picked}
/>
</label>
</div>
`;
}

_syncPicker(ev) {
// The swatch shows any CSS colour (theme names, var()); read back what the
// browser resolved so the picker opens on the current colour.
const fill = this.shadowRoot?.querySelector(".fill");
if (fill) ev.target.value = compactPowerCardHexOf(getComputedStyle(fill).backgroundColor) || "#000000";
}

_textChanged(ev) {
ev.stopPropagation();
this._fire((ev.detail.value || "").trim() || undefined);
}

_picked(ev) {
this._fire(ev.target.value);
}

_fire(value) {
if (value === this.value) return;
this.dispatchEvent(
new CustomEvent("value-changed", { detail: { value }, bubbles: true, composed: true })
);
}
}

if (!customElements.get("ha-selector-cpc_color")) {
customElements.define("ha-selector-cpc_color", CompactPowerCardColorSelector);
}

customElements.define("compact-power-card", CompactPowerCard);

// Register card metadata for the Lovelace card picker
Expand Down