-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
dialog-energy-battery-settings.ts
154 lines (135 loc) · 4.59 KB
/
dialog-energy-battery-settings.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { mdiBatteryHigh } from "@mdi/js";
import { css, CSSResultGroup, html, LitElement, TemplateResult } from "lit";
import { customElement, property, state } from "lit/decorators";
import { fireEvent } from "../../../../common/dom/fire_event";
import "../../../../components/ha-dialog";
import {
BatterySourceTypeEnergyPreference,
emptyBatteryEnergyPreference,
} from "../../../../data/energy";
import { HassDialog } from "../../../../dialogs/make-dialog-manager";
import { haStyle, haStyleDialog } from "../../../../resources/styles";
import { HomeAssistant } from "../../../../types";
import { EnergySettingsBatteryDialogParams } from "./show-dialogs-energy";
import "@material/mwc-button/mwc-button";
import "../../../../components/entity/ha-statistic-picker";
import { getSensorDeviceClassConvertibleUnits } from "../../../../data/sensor";
const energyUnitClasses = ["energy"];
@customElement("dialog-energy-battery-settings")
export class DialogEnergyBatterySettings
extends LitElement
implements HassDialog<EnergySettingsBatteryDialogParams>
{
@property({ attribute: false }) public hass!: HomeAssistant;
@state() private _params?: EnergySettingsBatteryDialogParams;
@state() private _source?: BatterySourceTypeEnergyPreference;
@state() private _energy_units?: string[];
@state() private _error?: string;
public async showDialog(
params: EnergySettingsBatteryDialogParams
): Promise<void> {
this._params = params;
this._source = params.source
? { ...params.source }
: emptyBatteryEnergyPreference();
this._energy_units = (
await getSensorDeviceClassConvertibleUnits(this.hass, "energy")
).units;
}
public closeDialog(): void {
this._params = undefined;
this._source = undefined;
this._error = undefined;
fireEvent(this, "dialog-closed", { dialog: this.localName });
}
protected render(): TemplateResult {
if (!this._params || !this._source) {
return html``;
}
const pickableUnit = this._energy_units?.join(", ") || "";
return html`
<ha-dialog
open
.heading=${html`<ha-svg-icon
.path=${mdiBatteryHigh}
style="--mdc-icon-size: 32px;"
></ha-svg-icon>
${this.hass.localize(
"ui.panel.config.energy.battery.dialog.header"
)}`}
@closed=${this.closeDialog}
>
${this._error ? html`<p class="error">${this._error}</p>` : ""}
<div>
${this.hass.localize(
"ui.panel.config.energy.battery.dialog.entity_para",
{ unit: pickableUnit }
)}
</div>
<ha-statistic-picker
.hass=${this.hass}
.includeUnitClass=${energyUnitClasses}
.value=${this._source.stat_energy_to}
.label=${this.hass.localize(
"ui.panel.config.energy.battery.dialog.energy_into_battery"
)}
@value-changed=${this._statisticToChanged}
dialogInitialFocus
></ha-statistic-picker>
<ha-statistic-picker
.hass=${this.hass}
.includeUnitClass=${energyUnitClasses}
.value=${this._source.stat_energy_from}
.label=${this.hass.localize(
"ui.panel.config.energy.battery.dialog.energy_out_of_battery"
)}
@value-changed=${this._statisticFromChanged}
></ha-statistic-picker>
<mwc-button @click=${this.closeDialog} slot="secondaryAction">
${this.hass.localize("ui.common.cancel")}
</mwc-button>
<mwc-button
@click=${this._save}
.disabled=${!this._source.stat_energy_from ||
!this._source.stat_energy_to}
slot="primaryAction"
>
${this.hass.localize("ui.common.save")}
</mwc-button>
</ha-dialog>
`;
}
private _statisticToChanged(ev: CustomEvent<{ value: string }>) {
this._source = { ...this._source!, stat_energy_to: ev.detail.value };
}
private _statisticFromChanged(ev: CustomEvent<{ value: string }>) {
this._source = { ...this._source!, stat_energy_from: ev.detail.value };
}
private async _save() {
try {
await this._params!.saveCallback(this._source!);
this.closeDialog();
} catch (err: any) {
this._error = err.message;
}
}
static get styles(): CSSResultGroup {
return [
haStyle,
haStyleDialog,
css`
ha-dialog {
--mdc-dialog-max-width: 430px;
}
ha-statistic-picker {
width: 100%;
}
`,
];
}
}
declare global {
interface HTMLElementTagNameMap {
"dialog-energy-battery-settings": DialogEnergyBatterySettings;
}
}