From 840858b18c2ec05c8d0e5360eb94a38808ceeec9 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Tue, 22 Mar 2022 15:40:00 -0700 Subject: [PATCH] Add statistic adjust dialog (#12101) Co-authored-by: Zack Barett --- .../ha-selector/ha-selector-datetime.ts | 3 +- .../ha-selector/ha-selector-number.ts | 2 +- src/data/history.ts | 13 ++ .../statistics/developer-tools-statistics.ts | 27 +++ .../dialog-statistics-adjust-sum.ts | 166 ++++++++++++++++++ .../dialog-statistics-fix-units-changed.ts | 2 +- ...og-statistics-fix-unsupported-unit-meta.ts | 2 +- .../show-dialog-statistics-adjust-sum.ts | 20 +++ 8 files changed, 231 insertions(+), 4 deletions(-) create mode 100644 src/panels/developer-tools/statistics/dialog-statistics-adjust-sum.ts create mode 100644 src/panels/developer-tools/statistics/show-dialog-statistics-adjust-sum.ts diff --git a/src/components/ha-selector/ha-selector-datetime.ts b/src/components/ha-selector/ha-selector-datetime.ts index 80add84bd4..caee6d8457 100644 --- a/src/components/ha-selector/ha-selector-datetime.ts +++ b/src/components/ha-selector/ha-selector-datetime.ts @@ -26,6 +26,7 @@ export class HaDateTimeSelector extends LitElement { protected render() { const values = this.value?.split(" "); + return html` stats.some((stat) => stat[type] !== null); + +export const adjustStatisticsSum = ( + hass: HomeAssistant, + statistic_id: string, + start_time: string, + adjustment: number +): Promise => + hass.callWS({ + type: "recorder/adjust_sum_statistics", + statistic_id, + start_time, + adjustment, + }); diff --git a/src/panels/developer-tools/statistics/developer-tools-statistics.ts b/src/panels/developer-tools/statistics/developer-tools-statistics.ts index 4b48ebb116..3183133beb 100644 --- a/src/panels/developer-tools/statistics/developer-tools-statistics.ts +++ b/src/panels/developer-tools/statistics/developer-tools-statistics.ts @@ -1,10 +1,12 @@ import "@material/mwc-button/mwc-button"; +import { mdiSlopeUphill } from "@mdi/js"; import { HassEntity, UnsubscribeFunc } from "home-assistant-js-websocket"; import { css, CSSResultGroup, html, LitElement } from "lit"; import { customElement, property, state } from "lit/decorators"; import memoizeOne from "memoize-one"; import { fireEvent } from "../../../common/dom/fire_event"; import { computeStateName } from "../../../common/entity/compute_state_name"; +import "../../../components/ha-icon-overflow-menu"; import "../../../components/data-table/ha-data-table"; import type { DataTableColumnContainer } from "../../../components/data-table/ha-data-table"; import { subscribeEntityRegistry } from "../../../data/entity_registry"; @@ -24,6 +26,7 @@ import { haStyle } from "../../../resources/styles"; import { HomeAssistant } from "../../../types"; import { showFixStatisticsUnitsChangedDialog } from "./show-dialog-statistics-fix-units-changed"; import { showFixStatisticsUnsupportedUnitMetadataDialog } from "./show-dialog-statistics-fix-unsupported-unit-meta"; +import { showStatisticsAdjustSumDialog } from "./show-dialog-statistics-adjust-sum"; const FIX_ISSUES_ORDER = { no_state: 0, @@ -111,6 +114,30 @@ class HaPanelDevStatistics extends SubscribeMixin(LitElement) { : ""}`, width: "113px", }, + actions: { + title: "", + type: "overflow-menu", + template: ( + _info, + statistic: StatisticsMetaData + ) => html` + showStatisticsAdjustSumDialog(this, { + statistic: statistic, + }), + }, + ]} + style="color: var(--secondary-text-color)" + >`, + }, }) ); diff --git a/src/panels/developer-tools/statistics/dialog-statistics-adjust-sum.ts b/src/panels/developer-tools/statistics/dialog-statistics-adjust-sum.ts new file mode 100644 index 0000000000..9819177799 --- /dev/null +++ b/src/panels/developer-tools/statistics/dialog-statistics-adjust-sum.ts @@ -0,0 +1,166 @@ +import "@material/mwc-button/mwc-button"; +import { LitElement, TemplateResult, html, CSSResultGroup } from "lit"; +import { customElement, property, state } from "lit/decorators"; +import memoizeOne from "memoize-one"; +import "../../../components/ha-dialog"; +import { fireEvent } from "../../../common/dom/fire_event"; +import { haStyle, haStyleDialog } from "../../../resources/styles"; +import { HomeAssistant } from "../../../types"; +import "../../../components/ha-formfield"; +import "../../../components/ha-radio"; +import "../../../components/ha-form/ha-form"; +import type { DialogStatisticsAdjustSumParams } from "./show-dialog-statistics-adjust-sum"; +import type { + HaFormBaseSchema, + HaFormSchema, +} from "../../../components/ha-form/types"; +import { adjustStatisticsSum } from "../../../data/history"; +import { showAlertDialog } from "../../../dialogs/generic/show-dialog-box"; +import { showToast } from "../../../util/toast"; + +let lastMoment: string | undefined; + +@customElement("dialog-statistics-adjust-sum") +export class DialogStatisticsFixUnsupportedUnitMetadata extends LitElement { + @property({ attribute: false }) public hass!: HomeAssistant; + + @state() private _params?: DialogStatisticsAdjustSumParams; + + @state() private _data?: { + moment: string; + amount: number; + }; + + @state() private _busy = false; + + public showDialog(params: DialogStatisticsAdjustSumParams): void { + this._params = params; + this._busy = false; + const now = new Date(); + this._data = { + moment: + lastMoment || + `${now.getFullYear()}-${ + now.getMonth() + 1 + }-${now.getDate()} ${now.getHours()}:${now.getMinutes()}:${now.getSeconds()}`, + amount: 0, + }; + } + + public closeDialog(): void { + this._params = undefined; + fireEvent(this, "dialog-closed", { dialog: this.localName }); + } + + protected render(): TemplateResult | void { + if (!this._params) { + return html``; + } + + return html` + + + + + + + `; + } + + private _getSchema = memoizeOne((statistic): HaFormSchema[] => [ + { + type: "constant", + name: "name", + value: statistic.name || statistic.statistic_id, + }, + { + name: "moment", + required: true, + selector: { + datetime: {}, + }, + }, + { + name: "amount", + required: true, + default: 0, + selector: { + number: { + mode: "box", + step: 0.1, + unit_of_measurement: statistic.unit_of_measurement, + }, + }, + }, + ]); + + private _computeLabel(value: HaFormBaseSchema) { + switch (value.name) { + case "name": + return "Statistic"; + case "moment": + return "Moment to adjust"; + case "amount": + return "Amount"; + default: + return value.name; + } + } + + private _valueChanged(ev) { + this._data = ev.detail.value; + } + + private async _fixIssue(): Promise { + this._busy = true; + try { + await adjustStatisticsSum( + this.hass, + this._params!.statistic.statistic_id, + this._data!.moment, + this._data!.amount + ); + } catch (err: any) { + this._busy = false; + showAlertDialog(this, { + text: `Error adjusting sum: ${err.message || err}`, + }); + return; + } + showToast(this, { + message: "Statistic sum adjusted", + }); + lastMoment = this._data!.moment; + this.closeDialog(); + } + + static get styles(): CSSResultGroup { + return [haStyle, haStyleDialog]; + } +} + +declare global { + interface HTMLElementTagNameMap { + "dialog-statistics-adjust-sum": DialogStatisticsFixUnsupportedUnitMetadata; + } +} diff --git a/src/panels/developer-tools/statistics/dialog-statistics-fix-units-changed.ts b/src/panels/developer-tools/statistics/dialog-statistics-fix-units-changed.ts index 1ce0c607ce..3168bffc9f 100644 --- a/src/panels/developer-tools/statistics/dialog-statistics-fix-units-changed.ts +++ b/src/panels/developer-tools/statistics/dialog-statistics-fix-units-changed.ts @@ -11,7 +11,7 @@ import { } from "../../../data/history"; import "../../../components/ha-formfield"; import "../../../components/ha-radio"; -import { DialogStatisticsUnitsChangedParams } from "./show-dialog-statistics-fix-units-changed"; +import type { DialogStatisticsUnitsChangedParams } from "./show-dialog-statistics-fix-units-changed"; @customElement("dialog-statistics-fix-units-changed") export class DialogStatisticsFixUnitsChanged extends LitElement { diff --git a/src/panels/developer-tools/statistics/dialog-statistics-fix-unsupported-unit-meta.ts b/src/panels/developer-tools/statistics/dialog-statistics-fix-unsupported-unit-meta.ts index 93d1e320b1..4bfaebe489 100644 --- a/src/panels/developer-tools/statistics/dialog-statistics-fix-unsupported-unit-meta.ts +++ b/src/panels/developer-tools/statistics/dialog-statistics-fix-unsupported-unit-meta.ts @@ -8,7 +8,7 @@ import { HomeAssistant } from "../../../types"; import { updateStatisticsMetadata } from "../../../data/history"; import "../../../components/ha-formfield"; import "../../../components/ha-radio"; -import { DialogStatisticsUnsupportedUnitMetaParams } from "./show-dialog-statistics-fix-unsupported-unit-meta"; +import type { DialogStatisticsUnsupportedUnitMetaParams } from "./show-dialog-statistics-fix-unsupported-unit-meta"; @customElement("dialog-statistics-fix-unsupported-unit-meta") export class DialogStatisticsFixUnsupportedUnitMetadata extends LitElement { diff --git a/src/panels/developer-tools/statistics/show-dialog-statistics-adjust-sum.ts b/src/panels/developer-tools/statistics/show-dialog-statistics-adjust-sum.ts new file mode 100644 index 0000000000..1db2c76307 --- /dev/null +++ b/src/panels/developer-tools/statistics/show-dialog-statistics-adjust-sum.ts @@ -0,0 +1,20 @@ +import { fireEvent } from "../../../common/dom/fire_event"; +import { StatisticsMetaData } from "../../../data/history"; + +export const loadAdjustSumDialog = () => + import("./dialog-statistics-adjust-sum"); + +export interface DialogStatisticsAdjustSumParams { + statistic: StatisticsMetaData; +} + +export const showStatisticsAdjustSumDialog = ( + element: HTMLElement, + detailParams: DialogStatisticsAdjustSumParams +): void => { + fireEvent(element, "show-dialog", { + dialogTag: "dialog-statistics-adjust-sum", + dialogImport: loadAdjustSumDialog, + dialogParams: detailParams, + }); +};