Change stat_types to supported ones for statistics
This commit is contained in:
parent
95bbf6e0d2
commit
72ad0f0f61
|
@ -1,7 +1,7 @@
|
|||
import { computeStateName } from "../common/entity/compute_state_name";
|
||||
import { HomeAssistant } from "../types";
|
||||
|
||||
export type StatisticType = "sum" | "min" | "max" | "mean";
|
||||
export type StatisticType = "state" | "sum" | "min" | "max" | "mean";
|
||||
|
||||
export interface Statistics {
|
||||
[statisticId: string]: StatisticValue[];
|
||||
|
|
|
@ -90,7 +90,10 @@ export class HuiStatisticsGraphCard extends LitElement implements LovelaceCard {
|
|||
if (typeof config.stat_types === "string") {
|
||||
this._config = { ...config, stat_types: [config.stat_types] };
|
||||
} else if (!config.stat_types) {
|
||||
this._config = { ...config, stat_types: ["sum", "min", "max", "mean"] };
|
||||
this._config = {
|
||||
...config,
|
||||
stat_types: ["state", "sum", "min", "max", "mean"],
|
||||
};
|
||||
} else {
|
||||
this._config = config;
|
||||
}
|
||||
|
|
|
@ -37,6 +37,7 @@ import {
|
|||
} from "../../../../data/recorder";
|
||||
import { deepEqual } from "../../../../common/util/deep-equal";
|
||||
import { statTypeMap } from "../../../../components/chart/statistics-chart";
|
||||
import { ensureArray } from "../../../../common/ensure-array";
|
||||
|
||||
const statTypeStruct = union([
|
||||
literal("state"),
|
||||
|
@ -192,7 +193,11 @@ export class HuiStatisticsGraphCardEditor
|
|||
? Array.isArray(this._config!.stat_types)
|
||||
? this._config!.stat_types
|
||||
: [this._config!.stat_types]
|
||||
: stat_types;
|
||||
: stat_types.filter((stat_type) =>
|
||||
this._metaDatas?.every((metaData) =>
|
||||
statisticsMetaHasType(metaData, statTypeMap[stat_type])
|
||||
)
|
||||
);
|
||||
const data = {
|
||||
chart_type: "line",
|
||||
period: "hour",
|
||||
|
@ -232,7 +237,7 @@ export class HuiStatisticsGraphCardEditor
|
|||
fireEvent(this, "config-changed", { config: ev.detail.value });
|
||||
}
|
||||
|
||||
private _entitiesChanged(ev: CustomEvent): void {
|
||||
private async _entitiesChanged(ev: CustomEvent): Promise<void> {
|
||||
const config = { ...this._config!, entities: ev.detail.value };
|
||||
if (
|
||||
config.entities?.some((statistic_id) => statistic_id.includes(":")) &&
|
||||
|
@ -240,6 +245,17 @@ export class HuiStatisticsGraphCardEditor
|
|||
) {
|
||||
delete config.period;
|
||||
}
|
||||
if (config.stat_types && config.entities.length) {
|
||||
const metadata = await getStatisticMetadata(this.hass!, config.entities);
|
||||
config.stat_types = ensureArray(config.stat_types).filter((stat_type) =>
|
||||
metadata.every((metaData) =>
|
||||
statisticsMetaHasType(metaData, statTypeMap[stat_type])
|
||||
)
|
||||
);
|
||||
if (!config.stat_types.length) {
|
||||
delete config.stat_types;
|
||||
}
|
||||
}
|
||||
fireEvent(this, "config-changed", {
|
||||
config,
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue