From 0f534423a41bdc051f44e3dc77e09cc1caa72a7f Mon Sep 17 00:00:00 2001 From: Erik Date: Wed, 21 Sep 2022 14:16:27 +0200 Subject: [PATCH] Address review comments --- src/components/chart/statistics-chart.ts | 31 ++++++++++++++----- src/data/recorder.ts | 9 ++---- .../hui-statistics-graph-card-editor.ts | 3 +- 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/src/components/chart/statistics-chart.ts b/src/components/chart/statistics-chart.ts index d9373cf576..58696e4cb8 100644 --- a/src/components/chart/statistics-chart.ts +++ b/src/components/chart/statistics-chart.ts @@ -30,6 +30,15 @@ import { import type { HomeAssistant } from "../../types"; import "./ha-chart-base"; +export type ExtendedStatisticType = StatisticType | "state" | "sum_rel"; + +export const statTypeMap: Record = { + mean: "mean", + min: "min", + max: "max", + sum: "sum", + sum_rel: "sum", +}; @customElement("statistics-chart") class StatisticsChart extends LitElement { @property({ attribute: false }) public hass!: HomeAssistant; @@ -42,7 +51,7 @@ class StatisticsChart extends LitElement { @property({ attribute: false }) public endTime?: Date; - @property({ type: Array }) public statTypes: Array = [ + @property({ type: Array }) public statTypes: Array = [ "sum", "min", "mean", @@ -307,7 +316,7 @@ class StatisticsChart extends LitElement { : this.statTypes; sortedTypes.forEach((type) => { - if (statisticsHaveType(stats, type)) { + if (statisticsHaveType(stats, statTypeMap[type])) { const band = drawBands && (type === "min" || type === "max"); statTypes.push(type); statDataSets.push({ @@ -335,8 +344,14 @@ class StatisticsChart extends LitElement { let prevDate: Date | null = null; // Process chart data. - let initVal: number | null = null; - let prevSum: number | null = null; + const initVal: Record<"sum" | "sum_rel", number | null> = { + sum: null, + sum_rel: null, + }; + const prevSum: Record<"sum" | "sum_rel", number | null> = { + sum: null, + sum_rel: null, + }; stats.forEach((stat) => { const date = new Date(stat.start); if (prevDate === date) { @@ -347,11 +362,11 @@ class StatisticsChart extends LitElement { statTypes.forEach((type) => { let val: number | null; if (type === "sum" || type === "sum_rel") { - if (initVal === null) { - initVal = val = type === "sum_rel" ? 0 : stat.sum || 0; - prevSum = stat.sum; + if (initVal[type] === null) { + initVal[type] = val = type === "sum_rel" ? 0 : stat.sum || 0; + prevSum[type] = stat.sum; } else { - val = initVal + ((stat.sum || 0) - prevSum!); + val = initVal[type]! + ((stat.sum || 0) - prevSum[type]!); } } else { val = stat[type]; diff --git a/src/data/recorder.ts b/src/data/recorder.ts index cdef963024..f98c5b75a8 100644 --- a/src/data/recorder.ts +++ b/src/data/recorder.ts @@ -1,7 +1,7 @@ import { computeStateName } from "../common/entity/compute_state_name"; import { HomeAssistant } from "../types"; -export type StatisticType = "sum" | "sum_rel" | "min" | "max" | "mean"; +export type StatisticType = "sum" | "min" | "max" | "mean"; export interface Statistics { [statisticId: string]: StatisticValue[]; @@ -201,13 +201,10 @@ export const calculateStatisticsSumGrowth = ( export const statisticsHaveType = ( stats: StatisticValue[], type: StatisticType -) => { - type = type === "sum_rel" ? "sum" : type; - return stats.some((stat) => stat[type] !== null); -}; +) => stats.some((stat) => stat[type] !== null); const mean_stat_types: readonly StatisticType[] = ["mean", "min", "max"]; -const sum_stat_types: readonly StatisticType[] = ["sum", "sum_rel"]; +const sum_stat_types: readonly StatisticType[] = ["sum"]; export const statisticsMetaHasType = ( metadata: StatisticsMetaData, diff --git a/src/panels/lovelace/editor/config-elements/hui-statistics-graph-card-editor.ts b/src/panels/lovelace/editor/config-elements/hui-statistics-graph-card-editor.ts index 24bcb1635d..c5df20a6ed 100644 --- a/src/panels/lovelace/editor/config-elements/hui-statistics-graph-card-editor.ts +++ b/src/panels/lovelace/editor/config-elements/hui-statistics-graph-card-editor.ts @@ -36,6 +36,7 @@ import { statisticsMetaHasType, } from "../../../../data/recorder"; import { deepEqual } from "../../../../common/util/deep-equal"; +import { statTypeMap } from "../../../../components/chart/statistics-chart"; const statTypeStruct = union([ literal("sum"), @@ -157,7 +158,7 @@ export class HuiStatisticsGraphCardEditor disabled: !metaDatas || !metaDatas?.every((metaData) => - statisticsMetaHasType(metaData, stat_type) + statisticsMetaHasType(metaData, statTypeMap[stat_type]) ), })), },