Statistics: Add support for mean type (#24758)

* Statistics: Add support for mean type

* update

* update enum

* Update en.json
This commit is contained in:
Bram Kragten 2025-03-26 13:35:38 +01:00 committed by GitHub
parent 2717e1e6cb
commit be1e1ff9fc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 113 additions and 57 deletions

View File

@ -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) {

View File

@ -41,6 +41,7 @@ import type {
import {
clearStatistics,
getStatisticIds,
StatisticMeanType,
updateStatisticsIssues,
validateStatistics,
} from "../../../data/recorder";
@ -57,6 +58,7 @@ const FIX_ISSUES_ORDER: Record<StatisticsValidationResult["type"], number> = {
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,
});

View File

@ -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}`
),
}
: {}),
}
)}<br /><br />
${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`<br /><br />
<a
href=${documentationUrl(
this.hass,
"/integrations/recorder/#configure-filter"
)}
target="_blank"
rel="noreferrer noopener"
>
${this.hass.localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.entity_not_recorded.info_text_3_link"
)}</a
>`
: issue.type === "entity_no_longer_recorded"
? html`<a
${this.hass.localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.mean_type_changed.info_text_3",
{ statistic_id: issue.data.statistic_id }
)}`
: issue.type === "entity_not_recorded"
? html`<br /><br />
<a
href=${documentationUrl(
this.hass,
"/integrations/recorder/#configure-filter"
@ -91,44 +94,57 @@ export class DialogStatisticsFix extends LitElement {
rel="noreferrer noopener"
>
${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"
)}</a
><br /><br />
${this.hass.localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.entity_no_longer_recorded.info_text_4"
)}`
: issue.type === "state_class_removed"
? html`<ul>
<li>
${this.hass.localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.state_class_removed.info_text_3"
)}
</li>
<li>
${this.hass.localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.state_class_removed.info_text_4"
)}
<a
href="https://developers.home-assistant.io/docs/core/entity/sensor/#long-term-statistics"
target="_blank"
rel="noreferrer noopener"
>
${this.hass.localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.state_class_removed.info_text_4_link"
)}</a
>
</li>
<li>
${this.hass.localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.state_class_removed.info_text_5"
)}
</li>
</ul>
>`
: issue.type === "entity_no_longer_recorded"
? html`<a
href=${documentationUrl(
this.hass,
"/integrations/recorder/#configure-filter"
)}
target="_blank"
rel="noreferrer noopener"
>
${this.hass.localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.entity_no_longer_recorded.info_text_3_link"
)}</a
><br /><br />
${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`<ul>
<li>
${this.hass.localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.state_class_removed.info_text_3"
)}
</li>
<li>
${this.hass.localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.state_class_removed.info_text_4"
)}
<a
href="https://developers.home-assistant.io/docs/core/entity/sensor/#long-term-statistics"
target="_blank"
rel="noreferrer noopener"
>
${this.hass.localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.state_class_removed.info_text_4_link"
)}</a
>
</li>
<li>
${this.hass.localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.state_class_removed.info_text_5"
)}
</li>
</ul>
${this.hass.localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.state_class_removed.info_text_6",
{ statistic_id: issue.data.statistic_id }
)}`
: nothing}
</p>
${issue.type !== "entity_not_recorded"

View File

@ -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";
}
}

View File

@ -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.",