Make history split by device class option in history card (#18871)

This commit is contained in:
Bram Kragten 2023-12-04 12:06:26 +01:00 committed by GitHub
parent 7e74502ba3
commit 96fbbc55e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 13 deletions

View File

@ -420,7 +420,8 @@ export const computeHistory = (
hass: HomeAssistant, hass: HomeAssistant,
stateHistory: HistoryStates, stateHistory: HistoryStates,
localize: LocalizeFunc, localize: LocalizeFunc,
sensorNumericalDeviceClasses: string[] sensorNumericalDeviceClasses: string[],
splitDeviceClasses = false
): HistoryResult => { ): HistoryResult => {
const lineChartDevices: { [unit: string]: HistoryStates } = {}; const lineChartDevices: { [unit: string]: HistoryStates } = {};
const timelineDevices: TimelineEntity[] = []; const timelineDevices: TimelineEntity[] = [];
@ -473,7 +474,7 @@ export const computeHistory = (
currentState?.attributes || numericStateFromHistory?.a currentState?.attributes || numericStateFromHistory?.a
)?.device_class; )?.device_class;
const key = computeGroupKey(unit, deviceClass); const key = computeGroupKey(unit, deviceClass, splitDeviceClasses);
if (!unit) { if (!unit) {
timelineDevices.push( timelineDevices.push(
@ -487,9 +488,13 @@ export const computeHistory = (
currentState currentState
) )
); );
} else if (key in lineChartDevices && entityId in lineChartDevices[key]) { } else if (
key &&
key in lineChartDevices &&
entityId in lineChartDevices[key]
) {
lineChartDevices[key][entityId].push(...stateInfo); lineChartDevices[key][entityId].push(...stateInfo);
} else { } else if (key) {
if (!(key in lineChartDevices)) { if (!(key in lineChartDevices)) {
lineChartDevices[key] = {}; lineChartDevices[key] = {};
} }
@ -514,5 +519,6 @@ export const computeHistory = (
export const computeGroupKey = ( export const computeGroupKey = (
unit: string | undefined, unit: string | undefined,
device_class: string | undefined device_class: string | undefined,
) => `${unit}_${device_class || ""}`; splitDeviceClasses: boolean
) => (splitDeviceClasses ? `${unit}_${device_class || ""}` : unit);

View File

@ -224,17 +224,19 @@ class HaPanelHistory extends SubscribeMixin(LitElement) {
const keys = new Set( const keys = new Set(
historyResult.line historyResult.line
.map((i) => computeGroupKey(i.unit, i.device_class)) .map((i) => computeGroupKey(i.unit, i.device_class, true))
.concat( .concat(
ltsResult.line.map((i) => computeGroupKey(i.unit, i.device_class)) ltsResult.line.map((i) =>
computeGroupKey(i.unit, i.device_class, true)
)
) )
); );
keys.forEach((key) => { keys.forEach((key) => {
const historyItem = historyResult.line.find( const historyItem = historyResult.line.find(
(i) => computeGroupKey(i.unit, i.device_class) === key (i) => computeGroupKey(i.unit, i.device_class, true) === key
); );
const ltsItem = ltsResult.line.find( const ltsItem = ltsResult.line.find(
(i) => computeGroupKey(i.unit, i.device_class) === key (i) => computeGroupKey(i.unit, i.device_class, true) === key
); );
if (historyItem && ltsItem) { if (historyItem && ltsItem) {
const newLineItem: LineChartUnit = { ...historyItem, data: [] }; const newLineItem: LineChartUnit = { ...historyItem, data: [] };
@ -410,7 +412,8 @@ class HaPanelHistory extends SubscribeMixin(LitElement) {
this.hass, this.hass,
statsHistoryStates, statsHistoryStates,
this.hass.localize, this.hass.localize,
sensorNumericDeviceClasses sensorNumericDeviceClasses,
true
); );
// remap states array to statistics array // remap states array to statistics array
(this._statisticsHistory?.line || []).forEach((item) => { (this._statisticsHistory?.line || []).forEach((item) => {
@ -460,7 +463,8 @@ class HaPanelHistory extends SubscribeMixin(LitElement) {
this.hass, this.hass,
history, history,
this.hass.localize, this.hass.localize,
sensorNumericDeviceClasses sensorNumericDeviceClasses,
true
); );
}, },
this._startDate, this._startDate,

View File

@ -118,7 +118,8 @@ export class HuiHistoryGraphCard extends LitElement implements LovelaceCard {
this.hass!, this.hass!,
combinedHistory, combinedHistory,
this.hass!.localize, this.hass!.localize,
sensorNumericDeviceClasses sensorNumericDeviceClasses,
this._config?.split_device_classes
); );
}, },
this._hoursToShow, this._hoursToShow,

View File

@ -323,6 +323,7 @@ export interface HistoryGraphCardConfig extends LovelaceCardConfig {
title?: string; title?: string;
show_names?: boolean; show_names?: boolean;
logarithmic_scale?: boolean; logarithmic_scale?: boolean;
split_device_classes?: boolean;
} }
export interface StatisticsGraphCardConfig extends LovelaceCardConfig { export interface StatisticsGraphCardConfig extends LovelaceCardConfig {