Fix price units for gas (#13824)

This commit is contained in:
Bram Kragten 2022-09-21 11:16:19 +02:00 committed by GitHub
parent 82a641a200
commit 56c78ae108
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 79 additions and 54 deletions

View File

@ -82,6 +82,13 @@ export class HaTextField extends TextFieldBase {
direction: var(--direction); direction: var(--direction);
} }
.mdc-floating-label:not(.mdc-floating-label--float-above) {
text-overflow: ellipsis;
width: inherit;
padding-right: 30px;
box-sizing: border-box;
}
input { input {
text-align: var(--text-field-text-align, start); text-align: var(--text-field-text-align, start);
} }

View File

@ -630,7 +630,7 @@ export const getEnergyGasUnitCategory = (
const statisticIdWithMeta = statisticsMetaData[source.stat_energy_from]; const statisticIdWithMeta = statisticsMetaData[source.stat_energy_from];
if (statisticIdWithMeta) { if (statisticIdWithMeta) {
return ENERGY_GAS_VOLUME_UNITS.includes( return ENERGY_GAS_VOLUME_UNITS.includes(
statisticIdWithMeta.display_unit_of_measurement statisticIdWithMeta.statistics_unit_of_measurement
) )
? "volume" ? "volume"
: "energy"; : "energy";

View File

@ -36,7 +36,7 @@ export class EnergyBatterySettings extends LitElement {
public preferences!: EnergyPreferences; public preferences!: EnergyPreferences;
@property({ attribute: false }) @property({ attribute: false })
public statsMetadata!: Record<string, StatisticsMetaData>; public statsMetadata?: Record<string, StatisticsMetaData>;
@property({ attribute: false }) @property({ attribute: false })
public validationResult?: EnergyPreferencesValidation; public validationResult?: EnergyPreferencesValidation;
@ -104,14 +104,14 @@ export class EnergyBatterySettings extends LitElement {
>${getStatisticLabel( >${getStatisticLabel(
this.hass, this.hass,
source.stat_energy_from, source.stat_energy_from,
this.statsMetadata[source.stat_energy_from] this.statsMetadata?.[source.stat_energy_from]
)}</span )}</span
> >
<span <span
>${getStatisticLabel( >${getStatisticLabel(
this.hass, this.hass,
source.stat_energy_to, source.stat_energy_to,
this.statsMetadata[source.stat_energy_to] this.statsMetadata?.[source.stat_energy_to]
)}</span )}</span
> >
</div> </div>

View File

@ -35,7 +35,7 @@ export class EnergyDeviceSettings extends LitElement {
public preferences!: EnergyPreferences; public preferences!: EnergyPreferences;
@property({ attribute: false }) @property({ attribute: false })
public statsMetadata!: Record<string, StatisticsMetaData>; public statsMetadata?: Record<string, StatisticsMetaData>;
@property({ attribute: false }) @property({ attribute: false })
public validationResult?: EnergyPreferencesValidation; public validationResult?: EnergyPreferencesValidation;
@ -90,7 +90,7 @@ export class EnergyDeviceSettings extends LitElement {
>${getStatisticLabel( >${getStatisticLabel(
this.hass, this.hass,
device.stat_consumption, device.stat_consumption,
this.statsMetadata[device.stat_consumption] this.statsMetadata?.[device.stat_consumption]
)}</span )}</span
> >
<ha-icon-button <ha-icon-button

View File

@ -36,7 +36,7 @@ export class EnergyGasSettings extends LitElement {
public preferences!: EnergyPreferences; public preferences!: EnergyPreferences;
@property({ attribute: false }) @property({ attribute: false })
public statsMetadata!: Record<string, StatisticsMetaData>; public statsMetadata?: Record<string, StatisticsMetaData>;
@property({ attribute: false }) @property({ attribute: false })
public validationResult?: EnergyPreferencesValidation; public validationResult?: EnergyPreferencesValidation;
@ -98,7 +98,7 @@ export class EnergyGasSettings extends LitElement {
>${getStatisticLabel( >${getStatisticLabel(
this.hass, this.hass,
source.stat_energy_from, source.stat_energy_from,
this.statsMetadata[source.stat_energy_from] this.statsMetadata?.[source.stat_energy_from]
)}</span )}</span
> >
<ha-icon-button <ha-icon-button
@ -133,7 +133,10 @@ export class EnergyGasSettings extends LitElement {
private _addSource() { private _addSource() {
showEnergySettingsGasDialog(this, { showEnergySettingsGasDialog(this, {
unit: getEnergyGasUnitCategory(this.preferences, this.statsMetadata), allowedGasUnitCategory: getEnergyGasUnitCategory(
this.preferences,
this.statsMetadata
),
saveCallback: async (source) => { saveCallback: async (source) => {
delete source.unit_of_measurement; delete source.unit_of_measurement;
await this._savePreferences({ await this._savePreferences({
@ -149,11 +152,12 @@ export class EnergyGasSettings extends LitElement {
ev.currentTarget.closest(".row").source; ev.currentTarget.closest(".row").source;
showEnergySettingsGasDialog(this, { showEnergySettingsGasDialog(this, {
source: { ...origSource }, source: { ...origSource },
unit: getEnergyGasUnitCategory( allowedGasUnitCategory: getEnergyGasUnitCategory(
this.preferences, this.preferences,
this.statsMetadata, this.statsMetadata,
origSource.stat_energy_from origSource.stat_energy_from
), ),
metadata: this.statsMetadata?.[origSource.stat_energy_from],
saveCallback: async (newSource) => { saveCallback: async (newSource) => {
await this._savePreferences({ await this._savePreferences({
...this.preferences, ...this.preferences,

View File

@ -55,7 +55,7 @@ export class EnergyGridSettings extends LitElement {
public preferences!: EnergyPreferences; public preferences!: EnergyPreferences;
@property({ attribute: false }) @property({ attribute: false })
public statsMetadata!: Record<string, StatisticsMetaData>; public statsMetadata?: Record<string, StatisticsMetaData>;
@property({ attribute: false }) @property({ attribute: false })
public validationResult?: EnergyPreferencesValidation; public validationResult?: EnergyPreferencesValidation;
@ -136,7 +136,7 @@ export class EnergyGridSettings extends LitElement {
>${getStatisticLabel( >${getStatisticLabel(
this.hass, this.hass,
flow.stat_energy_from, flow.stat_energy_from,
this.statsMetadata[flow.stat_energy_from] this.statsMetadata?.[flow.stat_energy_from]
)}</span )}</span
> >
<ha-icon-button <ha-icon-button
@ -183,7 +183,7 @@ export class EnergyGridSettings extends LitElement {
>${getStatisticLabel( >${getStatisticLabel(
this.hass, this.hass,
flow.stat_energy_to, flow.stat_energy_to,
this.statsMetadata[flow.stat_energy_to] this.statsMetadata?.[flow.stat_energy_to]
)}</span )}</span
> >
<ha-icon-button <ha-icon-button

View File

@ -36,7 +36,7 @@ export class EnergySolarSettings extends LitElement {
public preferences!: EnergyPreferences; public preferences!: EnergyPreferences;
@property({ attribute: false }) @property({ attribute: false })
public statsMetadata!: Record<string, StatisticsMetaData>; public statsMetadata?: Record<string, StatisticsMetaData>;
@property({ attribute: false }) @property({ attribute: false })
public validationResult?: EnergyPreferencesValidation; public validationResult?: EnergyPreferencesValidation;
@ -106,7 +106,7 @@ export class EnergySolarSettings extends LitElement {
>${getStatisticLabel( >${getStatisticLabel(
this.hass, this.hass,
source.stat_energy_from, source.stat_energy_from,
this.statsMetadata[source.stat_energy_from] this.statsMetadata?.[source.stat_energy_from]
)}</span )}</span
> >
${this.info ${this.info

View File

@ -32,9 +32,7 @@ class EnergyValidationMessage extends LitElement {
> >
${this.hass.localize( ${this.hass.localize(
`ui.panel.config.energy.validation.issues.${issueType}.description`, `ui.panel.config.energy.validation.issues.${issueType}.description`,
issueType === "entity_unexpected_unit_price" { currency: this.hass.config.currency }
? { currency: this.hass.config.currency }
: undefined
)} )}
${ ${
issueType === "recorder_untracked" issueType === "recorder_untracked"

View File

@ -21,6 +21,7 @@ import "../../../../components/ha-radio";
import "../../../../components/ha-formfield"; import "../../../../components/ha-formfield";
import "../../../../components/ha-textfield"; import "../../../../components/ha-textfield";
import type { HaRadio } from "../../../../components/ha-radio"; import type { HaRadio } from "../../../../components/ha-radio";
import { getStatisticMetadata } from "../../../../data/recorder";
@customElement("dialog-energy-gas-settings") @customElement("dialog-energy-gas-settings")
export class DialogEnergyGasSettings export class DialogEnergyGasSettings
@ -35,7 +36,9 @@ export class DialogEnergyGasSettings
@state() private _costs?: "no-costs" | "number" | "entity" | "statistic"; @state() private _costs?: "no-costs" | "number" | "entity" | "statistic";
@state() private _unit?: string; @state() private _pickableUnit?: string;
@state() private _pickedDisplayUnit?: string;
@state() private _error?: string; @state() private _error?: string;
@ -46,6 +49,7 @@ export class DialogEnergyGasSettings
this._source = params.source this._source = params.source
? { ...params.source } ? { ...params.source }
: emptyGasEnergyPreference(); : emptyGasEnergyPreference();
this._pickedDisplayUnit = params.metadata?.display_unit_of_measurement;
this._costs = this._source.entity_energy_price this._costs = this._source.entity_energy_price
? "entity" ? "entity"
: this._source.number_energy_price : this._source.number_energy_price
@ -58,7 +62,8 @@ export class DialogEnergyGasSettings
public closeDialog(): void { public closeDialog(): void {
this._params = undefined; this._params = undefined;
this._source = undefined; this._source = undefined;
this._unit = undefined; this._pickableUnit = undefined;
this._pickedDisplayUnit = undefined;
this._error = undefined; this._error = undefined;
fireEvent(this, "dialog-closed", { dialog: this.localName }); fireEvent(this, "dialog-closed", { dialog: this.localName });
} }
@ -68,13 +73,16 @@ export class DialogEnergyGasSettings
return html``; return html``;
} }
const unit = const pickableUnit =
this._unit || this._pickableUnit ||
(this._params.unit === undefined (this._params.allowedGasUnitCategory === undefined
? "m³ or kWh" ? "ft³, m³, Wh, kWh or MWh"
: this._params.unit === "energy" : this._params.allowedGasUnitCategory === "energy"
? "kWh" ? "Wh, kWh or MWh"
: "m³"); : "ft³ or m³");
const externalSource =
this._source.stat_cost && this._source.stat_cost.includes(":");
return html` return html`
<ha-dialog <ha-dialog
@ -90,22 +98,21 @@ export class DialogEnergyGasSettings
<ha-statistic-picker <ha-statistic-picker
.hass=${this.hass} .hass=${this.hass}
.includeStatisticsUnitOfMeasurement=${this._params.unit === undefined .includeStatisticsUnitOfMeasurement=${this._params
.allowedGasUnitCategory === undefined
? ENERGY_GAS_UNITS ? ENERGY_GAS_UNITS
: this._params.unit === "energy" : this._params.allowedGasUnitCategory === "energy"
? ENERGY_GAS_ENERGY_UNITS ? ENERGY_GAS_ENERGY_UNITS
: ENERGY_GAS_VOLUME_UNITS} : ENERGY_GAS_VOLUME_UNITS}
.value=${this._source.stat_energy_from} .value=${this._source.stat_energy_from}
.label=${`${this.hass.localize( .label=${`${this.hass.localize(
"ui.panel.config.energy.gas.dialog.gas_usage" "ui.panel.config.energy.gas.dialog.gas_usage"
)} (${ )} (${
this._params.unit === undefined this._params.allowedGasUnitCategory === undefined
? this.hass.localize( ? this.hass.localize(
"ui.panel.config.energy.gas.dialog.m3_or_kWh" "ui.panel.config.energy.gas.dialog.m3_or_kWh"
) )
: this._params.unit === "energy" : pickableUnit
? "kWh"
: "m³"
})`} })`}
@value-changed=${this._statisticChanged} @value-changed=${this._statisticChanged}
dialogInitialFocus dialogInitialFocus
@ -136,6 +143,7 @@ export class DialogEnergyGasSettings
value="statistic" value="statistic"
name="costs" name="costs"
.checked=${this._costs === "statistic"} .checked=${this._costs === "statistic"}
.disabled=${externalSource}
@change=${this._handleCostChanged} @change=${this._handleCostChanged}
></ha-radio> ></ha-radio>
</ha-formfield> </ha-formfield>
@ -160,6 +168,7 @@ export class DialogEnergyGasSettings
value="entity" value="entity"
name="costs" name="costs"
.checked=${this._costs === "entity"} .checked=${this._costs === "entity"}
.disabled=${externalSource}
@change=${this._handleCostChanged} @change=${this._handleCostChanged}
></ha-radio> ></ha-radio>
</ha-formfield> </ha-formfield>
@ -171,7 +180,7 @@ export class DialogEnergyGasSettings
.value=${this._source.entity_energy_price} .value=${this._source.entity_energy_price}
.label=${this.hass.localize( .label=${this.hass.localize(
`ui.panel.config.energy.gas.dialog.cost_entity_input`, `ui.panel.config.energy.gas.dialog.cost_entity_input`,
{ unit } { unit: this._pickedDisplayUnit || pickableUnit }
)} )}
@value-changed=${this._priceEntityChanged} @value-changed=${this._priceEntityChanged}
></ha-entity-picker>` ></ha-entity-picker>`
@ -192,14 +201,16 @@ export class DialogEnergyGasSettings
? html`<ha-textfield ? html`<ha-textfield
.label=${this.hass.localize( .label=${this.hass.localize(
`ui.panel.config.energy.gas.dialog.cost_number_input`, `ui.panel.config.energy.gas.dialog.cost_number_input`,
{ unit } { unit: this._pickedDisplayUnit || pickableUnit }
)} )}
class="price-options" class="price-options"
step=".01" step=".01"
type="number" type="number"
.value=${this._source.number_energy_price} .value=${this._source.number_energy_price}
@change=${this._numberPriceChanged} @change=${this._numberPriceChanged}
.suffix=${`${this.hass.config.currency}/${unit}`} .suffix=${`${this.hass.config.currency}/${
this._pickedDisplayUnit || pickableUnit
}`}
> >
</ha-textfield>` </ha-textfield>`
: ""} : ""}
@ -250,18 +261,21 @@ export class DialogEnergyGasSettings
}; };
} }
private _statisticChanged(ev: CustomEvent<{ value: string }>) { private async _statisticChanged(ev: CustomEvent<{ value: string }>) {
if (ev.detail.value) { if (ev.detail.value) {
const entity = this.hass.states[ev.detail.value]; const entity = this.hass.states[ev.detail.value];
if (entity?.attributes.unit_of_measurement) { if (entity?.attributes.unit_of_measurement) {
// Wh is normalized to kWh by stats generation this._pickedDisplayUnit = entity.attributes.unit_of_measurement;
this._unit = } else {
entity.attributes.unit_of_measurement === "Wh" this._pickedDisplayUnit = (
? "kWh" await getStatisticMetadata(this.hass, [ev.detail.value])
: entity.attributes.unit_of_measurement; )[0]?.display_unit_of_measurement;
} }
} else { } else {
this._unit = undefined; this._pickedDisplayUnit = undefined;
}
if (ev.detail.value.includes(":") && this._costs !== "statistic") {
this._costs = "no-costs";
} }
this._source = { this._source = {
...this._source!, ...this._source!,

View File

@ -9,6 +9,7 @@ import {
GasSourceTypeEnergyPreference, GasSourceTypeEnergyPreference,
SolarSourceTypeEnergyPreference, SolarSourceTypeEnergyPreference,
} from "../../../../data/energy"; } from "../../../../data/energy";
import { StatisticsMetaData } from "../../../../data/recorder";
export interface EnergySettingsGridFlowDialogParams { export interface EnergySettingsGridFlowDialogParams {
source?: source?:
@ -45,7 +46,8 @@ export interface EnergySettingsBatteryDialogParams {
export interface EnergySettingsGasDialogParams { export interface EnergySettingsGasDialogParams {
source?: GasSourceTypeEnergyPreference; source?: GasSourceTypeEnergyPreference;
unit?: EnergyGasUnit; allowedGasUnitCategory?: EnergyGasUnit;
metadata?: StatisticsMetaData;
saveCallback: (source: GasSourceTypeEnergyPreference) => Promise<void>; saveCallback: (source: GasSourceTypeEnergyPreference) => Promise<void>;
} }

View File

@ -90,37 +90,37 @@ class HaConfigEnergy extends LitElement {
<ha-energy-grid-settings <ha-energy-grid-settings
.hass=${this.hass} .hass=${this.hass}
.preferences=${this._preferences!} .preferences=${this._preferences!}
.statsMetadata=${this._statsMetadata!} .statsMetadata=${this._statsMetadata}
.validationResult=${this._validationResult!} .validationResult=${this._validationResult}
@value-changed=${this._prefsChanged} @value-changed=${this._prefsChanged}
></ha-energy-grid-settings> ></ha-energy-grid-settings>
<ha-energy-solar-settings <ha-energy-solar-settings
.hass=${this.hass} .hass=${this.hass}
.preferences=${this._preferences!} .preferences=${this._preferences!}
.statsMetadata=${this._statsMetadata!} .statsMetadata=${this._statsMetadata}
.validationResult=${this._validationResult!} .validationResult=${this._validationResult}
.info=${this._info} .info=${this._info}
@value-changed=${this._prefsChanged} @value-changed=${this._prefsChanged}
></ha-energy-solar-settings> ></ha-energy-solar-settings>
<ha-energy-battery-settings <ha-energy-battery-settings
.hass=${this.hass} .hass=${this.hass}
.preferences=${this._preferences!} .preferences=${this._preferences!}
.statsMetadata=${this._statsMetadata!} .statsMetadata=${this._statsMetadata}
.validationResult=${this._validationResult!} .validationResult=${this._validationResult}
@value-changed=${this._prefsChanged} @value-changed=${this._prefsChanged}
></ha-energy-battery-settings> ></ha-energy-battery-settings>
<ha-energy-gas-settings <ha-energy-gas-settings
.hass=${this.hass} .hass=${this.hass}
.preferences=${this._preferences!} .preferences=${this._preferences!}
.statsMetadata=${this._statsMetadata!} .statsMetadata=${this._statsMetadata}
.validationResult=${this._validationResult!} .validationResult=${this._validationResult}
@value-changed=${this._prefsChanged} @value-changed=${this._prefsChanged}
></ha-energy-gas-settings> ></ha-energy-gas-settings>
<ha-energy-device-settings <ha-energy-device-settings
.hass=${this.hass} .hass=${this.hass}
.preferences=${this._preferences!} .preferences=${this._preferences!}
.statsMetadata=${this._statsMetadata!} .statsMetadata=${this._statsMetadata}
.validationResult=${this._validationResult!} .validationResult=${this._validationResult}
@value-changed=${this._prefsChanged} @value-changed=${this._prefsChanged}
></ha-energy-device-settings> ></ha-energy-device-settings>
</div> </div>