Allow theme colors for individual sets in energy-usage-graph-card (#17527)

Co-authored-by: Bram Kragten <mail@bramkragten.nl>
This commit is contained in:
karwosts 2023-08-30 04:38:38 -07:00 committed by GitHub
parent 099e317d17
commit e002c5d96c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -44,6 +44,11 @@ import { HomeAssistant } from "../../../../types";
import { LovelaceCard } from "../../types"; import { LovelaceCard } from "../../types";
import { EnergyUsageGraphCardConfig } from "../types"; import { EnergyUsageGraphCardConfig } from "../types";
interface ColorSet {
base: string;
overrides?: Record<string, string>;
}
@customElement("hui-energy-usage-graph-card") @customElement("hui-energy-usage-graph-card")
export class HuiEnergyUsageGraphCard export class HuiEnergyUsageGraphCard
extends SubscribeMixin(LitElement) extends SubscribeMixin(LitElement)
@ -354,26 +359,68 @@ export class HuiEnergyUsageGraphCard
} }
const computedStyles = getComputedStyle(this); const computedStyles = getComputedStyle(this);
const colors = {
to_grid: computedStyles const colorPropertyMap = {
.getPropertyValue("--energy-grid-return-color") to_grid: "--energy-grid-return-color",
.trim(), to_battery: "--energy-battery-in-color",
to_battery: computedStyles from_grid: "--energy-grid-consumption-color",
.getPropertyValue("--energy-battery-in-color") used_grid: "--energy-grid-consumption-color",
.trim(), used_solar: "--energy-solar-color",
from_grid: computedStyles used_battery: "--energy-battery-out-color",
.getPropertyValue("--energy-grid-consumption-color")
.trim(),
used_grid: computedStyles
.getPropertyValue("--energy-grid-consumption-color")
.trim(),
used_solar: computedStyles
.getPropertyValue("--energy-solar-color")
.trim(),
used_battery: computedStyles
.getPropertyValue("--energy-battery-out-color")
.trim(),
}; };
const colors = {
to_grid: {
base: computedStyles.getPropertyValue(colorPropertyMap.to_grid).trim(),
},
to_battery: {
base: computedStyles
.getPropertyValue(colorPropertyMap.to_battery)
.trim(),
},
from_grid: {
base: computedStyles
.getPropertyValue(colorPropertyMap.from_grid)
.trim(),
},
used_grid: {
base: computedStyles
.getPropertyValue(colorPropertyMap.used_grid)
.trim(),
},
used_solar: {
base: computedStyles
.getPropertyValue(colorPropertyMap.used_solar)
.trim(),
},
used_battery: {
base: computedStyles
.getPropertyValue(colorPropertyMap.used_battery)
.trim(),
},
};
Object.entries(colorPropertyMap).forEach(([key, colorProp]) => {
if (
key === "used_grid" ||
key === "used_solar" ||
key === "used_battery"
) {
return;
}
colors[key].overrides = [];
if (statIds[key]) {
Object.values(statIds[key]).forEach((id, idx) => {
const override = computedStyles
.getPropertyValue(colorProp + "-" + idx)
.trim();
if (override.length > 0) {
colors[key].overrides[id] = override;
}
});
}
});
const labels = { const labels = {
used_grid: this.hass.localize( used_grid: this.hass.localize(
"ui.panel.lovelace.cards.energy.energy_usage_graph.combined_from_grid" "ui.panel.lovelace.cards.energy.energy_usage_graph.combined_from_grid"
@ -443,12 +490,12 @@ export class HuiEnergyUsageGraphCard
from_battery?: string[] | undefined; from_battery?: string[] | undefined;
}, },
colors: { colors: {
to_grid: string; to_grid: ColorSet;
to_battery: string; to_battery: ColorSet;
from_grid: string; from_grid: ColorSet;
used_grid: string; used_grid: ColorSet;
used_solar: string; used_solar: ColorSet;
used_battery: string; used_battery: ColorSet;
}, },
labels: { labels: {
used_grid: string; used_grid: string;
@ -598,15 +645,18 @@ export class HuiEnergyUsageGraphCard
Object.entries(combinedData).forEach(([type, sources]) => { Object.entries(combinedData).forEach(([type, sources]) => {
Object.entries(sources).forEach(([statId, source], idx) => { Object.entries(sources).forEach(([statId, source], idx) => {
let borderColor = colors[type].overrides?.[statId];
if (!borderColor) {
const modifiedColor = const modifiedColor =
idx > 0 idx > 0
? this.hass.themes.darkMode ? this.hass.themes.darkMode
? labBrighten(rgb2lab(hex2rgb(colors[type])), idx) ? labBrighten(rgb2lab(hex2rgb(colors[type].base)), idx)
: labDarken(rgb2lab(hex2rgb(colors[type])), idx) : labDarken(rgb2lab(hex2rgb(colors[type].base)), idx)
: undefined; : undefined;
const borderColor = modifiedColor borderColor = modifiedColor
? rgb2hex(lab2rgb(modifiedColor)) ? rgb2hex(lab2rgb(modifiedColor))
: colors[type]; : colors[type].base;
}
const points: ScatterDataPoint[] = []; const points: ScatterDataPoint[] = [];
// Process chart data. // Process chart data.