Show correct units for prices in energy gas settings (#10164)

This commit is contained in:
Bram Kragten 2021-10-06 17:38:32 +02:00 committed by GitHub
parent 365cf1f7ef
commit 8e010618bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 11 deletions

View File

@ -34,6 +34,8 @@ export class DialogEnergyGasSettings
@state() private _costs?: "no-costs" | "number" | "entity" | "statistic";
@state() private _unit?: string;
@state() private _error?: string;
public async showDialog(
@ -55,6 +57,7 @@ export class DialogEnergyGasSettings
public closeDialog(): void {
this._params = undefined;
this._source = undefined;
this._unit = undefined;
this._error = undefined;
fireEvent(this, "dialog-closed", { dialog: this.localName });
}
@ -64,6 +67,14 @@ export class DialogEnergyGasSettings
return html``;
}
const unit =
this._unit ||
(this._params.unit === undefined
? "m³ or kWh"
: this._params.unit === "energy"
? "kWh"
: "m³");
return html`
<ha-dialog
open
@ -154,7 +165,8 @@ export class DialogEnergyGasSettings
include-domains='["sensor", "input_number"]'
.value=${this._source.entity_energy_price}
.label=${this.hass.localize(
`ui.panel.config.energy.gas.dialog.cost_entity_input`
`ui.panel.config.energy.gas.dialog.cost_entity_input`,
{ unit }
)}
@value-changed=${this._priceEntityChanged}
></ha-entity-picker>`
@ -174,7 +186,8 @@ export class DialogEnergyGasSettings
${this._costs === "number"
? html`<paper-input
.label=${this.hass.localize(
`ui.panel.config.energy.gas.dialog.cost_number_input`
`ui.panel.config.energy.gas.dialog.cost_number_input`,
{ unit }
)}
no-label-float
class="price-options"
@ -183,12 +196,7 @@ export class DialogEnergyGasSettings
.value=${this._source.number_energy_price}
@value-changed=${this._numberPriceChanged}
>
<span slot="suffix"
>${this.hass.localize(
`ui.panel.config.energy.gas.dialog.cost_number_suffix`,
{ currency: this.hass.config.currency }
)}</span
>
<span slot="suffix">${this.hass.config.currency}/${unit}</span>
</paper-input>`
: ""}
@ -239,6 +247,18 @@ export class DialogEnergyGasSettings
}
private _statisticChanged(ev: CustomEvent<{ value: string }>) {
if (ev.detail.value) {
const entity = this.hass.states[ev.detail.value];
if (entity?.attributes.unit_of_measurement) {
// Wh is normalized to kWh by stats generation
this._unit =
entity.attributes.unit_of_measurement === "Wh"
? "kWh"
: entity.attributes.unit_of_measurement;
}
} else {
this._unit = undefined;
}
this._source = {
...this._source!,
stat_energy_from: ev.detail.value,

View File

@ -1045,10 +1045,9 @@
"cost_stat": "Use an entity tracking the total costs",
"cost_stat_input": "Total Costs Entity",
"cost_entity": "Use an entity with current price",
"cost_entity_input": "Entity with the current price",
"cost_entity_input": "Entity with the current price per {unit}",
"cost_number": "Use a static price",
"cost_number_input": "Price per m³",
"cost_number_suffix": "{currency}/m³"
"cost_number_input": "Price per {unit}"
}
},
"device_consumption": {