mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-23 17:26:42 +00:00
Add statistic adjust dialog (#12101)
Co-authored-by: Zack Barett <zackbarett@hey.com>
This commit is contained in:
parent
afd2e71f6c
commit
840858b18c
@ -26,6 +26,7 @@ export class HaDateTimeSelector extends LitElement {
|
|||||||
|
|
||||||
protected render() {
|
protected render() {
|
||||||
const values = this.value?.split(" ");
|
const values = this.value?.split(" ");
|
||||||
|
|
||||||
return html`
|
return html`
|
||||||
<ha-date-input
|
<ha-date-input
|
||||||
.label=${this.label}
|
.label=${this.label}
|
||||||
@ -37,7 +38,7 @@ export class HaDateTimeSelector extends LitElement {
|
|||||||
</ha-date-input>
|
</ha-date-input>
|
||||||
<ha-time-input
|
<ha-time-input
|
||||||
enable-second
|
enable-second
|
||||||
.value=${values?.[1] || "00:00:00"}
|
.value=${values?.[1] || "0:00:00"}
|
||||||
.locale=${this.hass.locale}
|
.locale=${this.hass.locale}
|
||||||
.disabled=${this.disabled}
|
.disabled=${this.disabled}
|
||||||
@value-changed=${this._valueChanged}
|
@value-changed=${this._valueChanged}
|
||||||
|
@ -46,7 +46,7 @@ export class HaNumberSelector extends LitElement {
|
|||||||
class=${classMap({ single: this.selector.number.mode === "box" })}
|
class=${classMap({ single: this.selector.number.mode === "box" })}
|
||||||
.min=${this.selector.number.min}
|
.min=${this.selector.number.min}
|
||||||
.max=${this.selector.number.max}
|
.max=${this.selector.number.max}
|
||||||
.value=${this.value || ""}
|
.value=${this.value ?? ""}
|
||||||
.step=${this.selector.number.step ?? 1}
|
.step=${this.selector.number.step ?? 1}
|
||||||
.disabled=${this.disabled}
|
.disabled=${this.disabled}
|
||||||
.required=${this.required}
|
.required=${this.required}
|
||||||
|
@ -465,3 +465,16 @@ export const statisticsHaveType = (
|
|||||||
stats: StatisticValue[],
|
stats: StatisticValue[],
|
||||||
type: StatisticType
|
type: StatisticType
|
||||||
) => stats.some((stat) => stat[type] !== null);
|
) => stats.some((stat) => stat[type] !== null);
|
||||||
|
|
||||||
|
export const adjustStatisticsSum = (
|
||||||
|
hass: HomeAssistant,
|
||||||
|
statistic_id: string,
|
||||||
|
start_time: string,
|
||||||
|
adjustment: number
|
||||||
|
): Promise<void> =>
|
||||||
|
hass.callWS({
|
||||||
|
type: "recorder/adjust_sum_statistics",
|
||||||
|
statistic_id,
|
||||||
|
start_time,
|
||||||
|
adjustment,
|
||||||
|
});
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
import "@material/mwc-button/mwc-button";
|
import "@material/mwc-button/mwc-button";
|
||||||
|
import { mdiSlopeUphill } from "@mdi/js";
|
||||||
import { HassEntity, UnsubscribeFunc } from "home-assistant-js-websocket";
|
import { HassEntity, UnsubscribeFunc } from "home-assistant-js-websocket";
|
||||||
import { css, CSSResultGroup, html, LitElement } from "lit";
|
import { css, CSSResultGroup, html, LitElement } from "lit";
|
||||||
import { customElement, property, state } from "lit/decorators";
|
import { customElement, property, state } from "lit/decorators";
|
||||||
import memoizeOne from "memoize-one";
|
import memoizeOne from "memoize-one";
|
||||||
import { fireEvent } from "../../../common/dom/fire_event";
|
import { fireEvent } from "../../../common/dom/fire_event";
|
||||||
import { computeStateName } from "../../../common/entity/compute_state_name";
|
import { computeStateName } from "../../../common/entity/compute_state_name";
|
||||||
|
import "../../../components/ha-icon-overflow-menu";
|
||||||
import "../../../components/data-table/ha-data-table";
|
import "../../../components/data-table/ha-data-table";
|
||||||
import type { DataTableColumnContainer } from "../../../components/data-table/ha-data-table";
|
import type { DataTableColumnContainer } from "../../../components/data-table/ha-data-table";
|
||||||
import { subscribeEntityRegistry } from "../../../data/entity_registry";
|
import { subscribeEntityRegistry } from "../../../data/entity_registry";
|
||||||
@ -24,6 +26,7 @@ import { haStyle } from "../../../resources/styles";
|
|||||||
import { HomeAssistant } from "../../../types";
|
import { HomeAssistant } from "../../../types";
|
||||||
import { showFixStatisticsUnitsChangedDialog } from "./show-dialog-statistics-fix-units-changed";
|
import { showFixStatisticsUnitsChangedDialog } from "./show-dialog-statistics-fix-units-changed";
|
||||||
import { showFixStatisticsUnsupportedUnitMetadataDialog } from "./show-dialog-statistics-fix-unsupported-unit-meta";
|
import { showFixStatisticsUnsupportedUnitMetadataDialog } from "./show-dialog-statistics-fix-unsupported-unit-meta";
|
||||||
|
import { showStatisticsAdjustSumDialog } from "./show-dialog-statistics-adjust-sum";
|
||||||
|
|
||||||
const FIX_ISSUES_ORDER = {
|
const FIX_ISSUES_ORDER = {
|
||||||
no_state: 0,
|
no_state: 0,
|
||||||
@ -111,6 +114,30 @@ class HaPanelDevStatistics extends SubscribeMixin(LitElement) {
|
|||||||
: ""}`,
|
: ""}`,
|
||||||
width: "113px",
|
width: "113px",
|
||||||
},
|
},
|
||||||
|
actions: {
|
||||||
|
title: "",
|
||||||
|
type: "overflow-menu",
|
||||||
|
template: (
|
||||||
|
_info,
|
||||||
|
statistic: StatisticsMetaData
|
||||||
|
) => html`<ha-icon-overflow-menu
|
||||||
|
.hass=${this.hass}
|
||||||
|
.narrow=${this.narrow}
|
||||||
|
.items=${[
|
||||||
|
{
|
||||||
|
path: mdiSlopeUphill,
|
||||||
|
label: localize(
|
||||||
|
"ui.panel.developer-tools.tabs.statistics.adjust_sum"
|
||||||
|
),
|
||||||
|
action: () =>
|
||||||
|
showStatisticsAdjustSumDialog(this, {
|
||||||
|
statistic: statistic,
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
]}
|
||||||
|
style="color: var(--secondary-text-color)"
|
||||||
|
></ha-icon-overflow-menu>`,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -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`
|
||||||
|
<ha-dialog
|
||||||
|
open
|
||||||
|
@closed=${this.closeDialog}
|
||||||
|
heading="Adjust sum for a specific time."
|
||||||
|
>
|
||||||
|
<ha-form
|
||||||
|
.hass=${this.hass}
|
||||||
|
.schema=${this._getSchema(this._params.statistic)}
|
||||||
|
.data=${this._data}
|
||||||
|
.computeLabel=${this._computeLabel}
|
||||||
|
.disabled=${this._busy}
|
||||||
|
@value-changed=${this._valueChanged}
|
||||||
|
></ha-form>
|
||||||
|
|
||||||
|
<mwc-button
|
||||||
|
slot="primaryAction"
|
||||||
|
@click=${this._fixIssue}
|
||||||
|
dialogInitialFocus
|
||||||
|
label="Adjust"
|
||||||
|
></mwc-button>
|
||||||
|
<mwc-button
|
||||||
|
slot="secondaryAction"
|
||||||
|
dialogAction="cancel"
|
||||||
|
.label=${this.hass.localize("ui.common.close")}
|
||||||
|
></mwc-button>
|
||||||
|
</ha-dialog>
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
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<void> {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
@ -11,7 +11,7 @@ import {
|
|||||||
} from "../../../data/history";
|
} from "../../../data/history";
|
||||||
import "../../../components/ha-formfield";
|
import "../../../components/ha-formfield";
|
||||||
import "../../../components/ha-radio";
|
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")
|
@customElement("dialog-statistics-fix-units-changed")
|
||||||
export class DialogStatisticsFixUnitsChanged extends LitElement {
|
export class DialogStatisticsFixUnitsChanged extends LitElement {
|
||||||
|
@ -8,7 +8,7 @@ import { HomeAssistant } from "../../../types";
|
|||||||
import { updateStatisticsMetadata } from "../../../data/history";
|
import { updateStatisticsMetadata } from "../../../data/history";
|
||||||
import "../../../components/ha-formfield";
|
import "../../../components/ha-formfield";
|
||||||
import "../../../components/ha-radio";
|
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")
|
@customElement("dialog-statistics-fix-unsupported-unit-meta")
|
||||||
export class DialogStatisticsFixUnsupportedUnitMetadata extends LitElement {
|
export class DialogStatisticsFixUnsupportedUnitMetadata extends LitElement {
|
||||||
|
@ -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,
|
||||||
|
});
|
||||||
|
};
|
Loading…
x
Reference in New Issue
Block a user