From be1e1ff9fc044e4116775896bdafee485dbe6e8a Mon Sep 17 00:00:00 2001 From: Bram Kragten Date: Wed, 26 Mar 2025 13:35:38 +0100 Subject: [PATCH] Statistics: Add support for mean type (#24758) * Statistics: Add support for mean type * update * update enum * Update en.json --- src/data/recorder.ts | 26 +++- .../statistics/developer-tools-statistics.ts | 5 +- .../statistics/dialog-statistics-fix.ts | 120 ++++++++++-------- .../hui-statistic-card-editor.ts | 7 +- src/translations/en.json | 12 ++ 5 files changed, 113 insertions(+), 57 deletions(-) diff --git a/src/data/recorder.ts b/src/data/recorder.ts index 632923e8fa..260f02f32b 100644 --- a/src/data/recorder.ts +++ b/src/data/recorder.ts @@ -36,13 +36,19 @@ export interface Statistic { change: number | null; } +export enum StatisticMeanType { + NONE = 0, + ARIMETHIC = 1, + CIRCULAR = 2, +} + export interface StatisticsMetaData { statistics_unit_of_measurement: string | null; statistic_id: string; source: string; name?: string | null; has_sum: boolean; - has_mean: boolean; + mean_type: StatisticMeanType; unit_class: string | null; } @@ -51,6 +57,7 @@ export const STATISTIC_TYPES: StatisticsValidationResult["type"][] = [ "entity_no_longer_recorded", "state_class_removed", "units_changed", + "mean_type_changed", "no_state", ]; @@ -59,7 +66,8 @@ export type StatisticsValidationResult = | StatisticsValidationResultEntityNotRecorded | StatisticsValidationResultEntityNoLongerRecorded | StatisticsValidationResultStateClassRemoved - | StatisticsValidationResultUnitsChanged; + | StatisticsValidationResultUnitsChanged + | StatisticsValidationResultMeanTypeChanged; export interface StatisticsValidationResultNoState { type: "no_state"; @@ -91,6 +99,15 @@ export interface StatisticsValidationResultUnitsChanged { }; } +export interface StatisticsValidationResultMeanTypeChanged { + type: "mean_type_changed"; + data: { + statistic_id: string; + state_mean_type: StatisticMeanType; + metadata_mean_type: StatisticMeanType; + }; +} + export interface StatisticsUnitConfiguration { energy?: "Wh" | "kWh" | "MWh" | "GJ"; power?: "W" | "kW"; @@ -278,7 +295,10 @@ export const statisticsMetaHasType = ( metadata: StatisticsMetaData, type: StatisticType ) => { - if (mean_stat_types.includes(type) && metadata.has_mean) { + if ( + mean_stat_types.includes(type) && + metadata.mean_type !== StatisticMeanType.NONE + ) { return true; } if (sum_stat_types.includes(type) && metadata.has_sum) { diff --git a/src/panels/developer-tools/statistics/developer-tools-statistics.ts b/src/panels/developer-tools/statistics/developer-tools-statistics.ts index ac06d1fc9b..6bf45ae2cf 100644 --- a/src/panels/developer-tools/statistics/developer-tools-statistics.ts +++ b/src/panels/developer-tools/statistics/developer-tools-statistics.ts @@ -41,6 +41,7 @@ import type { import { clearStatistics, getStatisticIds, + StatisticMeanType, updateStatisticsIssues, validateStatistics, } from "../../../data/recorder"; @@ -57,6 +58,7 @@ const FIX_ISSUES_ORDER: Record = { entity_not_recorded: 1, state_class_removed: 2, units_changed: 3, + mean_type_changed: 4, }; const FIXABLE_ISSUES: StatisticsValidationResult["type"][] = [ @@ -64,6 +66,7 @@ const FIXABLE_ISSUES: StatisticsValidationResult["type"][] = [ "entity_no_longer_recorded", "state_class_removed", "units_changed", + "mean_type_changed", ]; type StatisticData = StatisticsMetaData & { @@ -641,7 +644,7 @@ class HaPanelDevStatistics extends KeyboardShortcutMixin(LitElement) { source: "", state: this.hass.states[statisticId], issues: issues[statisticId], - has_mean: false, + mean_type: StatisticMeanType.NONE, has_sum: false, unit_class: null, }); diff --git a/src/panels/developer-tools/statistics/dialog-statistics-fix.ts b/src/panels/developer-tools/statistics/dialog-statistics-fix.ts index 02264cd97e..5a1a4e5e94 100644 --- a/src/panels/developer-tools/statistics/dialog-statistics-fix.ts +++ b/src/panels/developer-tools/statistics/dialog-statistics-fix.ts @@ -57,32 +57,35 @@ export class DialogStatisticsFix extends LitElement { { name: getStatisticLabel( this.hass, - this._params.issue.data.statistic_id, + issue.data.statistic_id, undefined ), - statistic_id: this._params.issue.data.statistic_id, + statistic_id: issue.data.statistic_id, + ...(issue.type === "mean_type_changed" + ? { + metadata_mean_type: this.hass.localize( + `ui.panel.developer-tools.tabs.statistics.mean_type.${issue.data.metadata_mean_type}` + ), + state_mean_type: this.hass.localize( + `ui.panel.developer-tools.tabs.statistics.mean_type.${issue.data.state_mean_type}` + ), + } + : {}), } )}

${this.hass.localize( `ui.panel.developer-tools.tabs.statistics.fix_issue.${issue.type}.info_text_2`, { statistic_id: issue.data.statistic_id } )} - ${issue.type === "entity_not_recorded" + ${issue.type === "mean_type_changed" ? html`

- - ${this.hass.localize( - "ui.panel.developer-tools.tabs.statistics.fix_issue.entity_not_recorded.info_text_3_link" - )}` - : issue.type === "entity_no_longer_recorded" - ? html`
+
${this.hass.localize( - "ui.panel.developer-tools.tabs.statistics.fix_issue.entity_no_longer_recorded.info_text_3_link" + "ui.panel.developer-tools.tabs.statistics.fix_issue.entity_not_recorded.info_text_3_link" )}

- ${this.hass.localize( - "ui.panel.developer-tools.tabs.statistics.fix_issue.entity_no_longer_recorded.info_text_4" - )}` - : issue.type === "state_class_removed" - ? html` + >` + : issue.type === "entity_no_longer_recorded" + ? html` + ${this.hass.localize( + "ui.panel.developer-tools.tabs.statistics.fix_issue.entity_no_longer_recorded.info_text_3_link" + )}

${this.hass.localize( - "ui.panel.developer-tools.tabs.statistics.fix_issue.state_class_removed.info_text_6", - { statistic_id: issue.data.statistic_id } + "ui.panel.developer-tools.tabs.statistics.fix_issue.entity_no_longer_recorded.info_text_4" )}` - : nothing} + : issue.type === "state_class_removed" + ? html` + ${this.hass.localize( + "ui.panel.developer-tools.tabs.statistics.fix_issue.state_class_removed.info_text_6", + { statistic_id: issue.data.statistic_id } + )}` + : nothing}

${issue.type !== "entity_not_recorded" diff --git a/src/panels/lovelace/editor/config-elements/hui-statistic-card-editor.ts b/src/panels/lovelace/editor/config-elements/hui-statistic-card-editor.ts index b44bb8cca1..d00448e852 100644 --- a/src/panels/lovelace/editor/config-elements/hui-statistic-card-editor.ts +++ b/src/panels/lovelace/editor/config-elements/hui-statistic-card-editor.ts @@ -13,6 +13,7 @@ import type { } from "../../../../data/recorder"; import { getStatisticMetadata, + StatisticMeanType, statisticsMetaHasType, } from "../../../../data/recorder"; import type { HomeAssistant } from "../../../../types"; @@ -220,7 +221,11 @@ export class HuiStatisticCardEditor if (metadata && !metadata.has_sum && config.stat_type === "change") { config.stat_type = "mean"; } - if (metadata && !metadata.has_mean && config.stat_type !== "change") { + if ( + metadata && + metadata.mean_type === StatisticMeanType.NONE && + config.stat_type !== "change" + ) { config.stat_type = "change"; } } diff --git a/src/translations/en.json b/src/translations/en.json index f170d81c68..086a3cf2e4 100644 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -8073,6 +8073,7 @@ "no_issue": "No issue", "issues": { "units_changed": "The unit of this entity changed from ''{metadata_unit}'' to ''{state_unit}''.", + "mean_type_changed": "The mean type of this entity changed from ''{metadata_mean_type}'' to ''{state_mean_type}''.", "state_class_removed": "This entity no longer has a state class", "entity_not_recorded": "This entity is excluded from being recorded.", "entity_no_longer_recorded": "This entity is no longer being recorded.", @@ -8083,6 +8084,11 @@ "title": "Delete selected statistics", "info_text": "Do you want to permanently delete the long term statistics {statistic_count, plural,\n one {of this entity}\n other {of {statistic_count} entities}\n} from your database?" }, + "mean_type": { + "0": "None", + "1": "Arimethic", + "2": "Circular" + }, "fix_issue": { "fix": "Fix issue", "clearing_failed": "Clearing the statistics failed", @@ -8131,6 +8137,12 @@ "info_text_2": "If the historic statistic values have a wrong unit, you can update the units of the old values. The values will not be updated.", "info_text_3": "Otherwise you can choose to delete all historic statistic values, and start over." }, + "mean_type_changed": { + "title": "The mean type has changed", + "info_text_1": "The mean type of ''{name}'' ({statistic_id}) changed from ''{metadata_mean_type}'' to ''{state_mean_type}''.", + "info_text_2": "Statistics cannot be generated until the old statistics are deleted, or the mean type matches the old statistics data again.", + "info_text_3": "Do you want to permanently delete the long term statistics of {statistic_id} from your database?" + }, "adjust_sum": { "title": "Adjust a statistic", "no_statistics_found": "No statistics found for this period.",