Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/components/ha-selector/ha-selector-datetime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export class HaDateTimeSelector extends LitElement {

protected render() {
const values = this.value?.split(" ");

return html`
<ha-date-input
.label=${this.label}
Expand All @@ -37,7 +38,7 @@ export class HaDateTimeSelector extends LitElement {
</ha-date-input>
<ha-time-input
enable-second
.value=${values?.[1] || "00:00:00"}
.value=${values?.[1] || "0:00:00"}
.locale=${this.hass.locale}
.disabled=${this.disabled}
@value-changed=${this._valueChanged}
Expand Down
2 changes: 1 addition & 1 deletion src/components/ha-selector/ha-selector-number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class HaNumberSelector extends LitElement {
class=${classMap({ single: this.selector.number.mode === "box" })}
.min=${this.selector.number.min}
.max=${this.selector.number.max}
.value=${this.value || ""}
.value=${this.value ?? ""}
.step=${this.selector.number.step ?? 1}
.disabled=${this.disabled}
.required=${this.required}
Expand Down
13 changes: 13 additions & 0 deletions src/data/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,3 +465,16 @@ export const statisticsHaveType = (
stats: StatisticValue[],
type: StatisticType
) => 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,
});
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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,
Expand Down Expand Up @@ -111,6 +114,30 @@ class HaPanelDevStatistics extends SubscribeMixin(LitElement) {
: ""}`,
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>`,
},
})
);

Expand Down
166 changes: 166 additions & 0 deletions src/panels/developer-tools/statistics/dialog-statistics-adjust-sum.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
});
};