From 56c78ae1080fdd002c05ad2ee37a22baa30bc1c4 Mon Sep 17 00:00:00 2001 From: Bram Kragten Date: Wed, 21 Sep 2022 11:16:19 +0200 Subject: [PATCH] Fix price units for gas (#13824) --- src/components/ha-textfield.ts | 7 ++ src/data/energy.ts | 2 +- .../components/ha-energy-battery-settings.ts | 6 +- .../components/ha-energy-device-settings.ts | 4 +- .../components/ha-energy-gas-settings.ts | 12 ++-- .../components/ha-energy-grid-settings.ts | 6 +- .../components/ha-energy-solar-settings.ts | 4 +- .../components/ha-energy-validation-result.ts | 4 +- .../dialogs/dialog-energy-gas-settings.ts | 64 +++++++++++-------- .../energy/dialogs/show-dialogs-energy.ts | 4 +- src/panels/config/energy/ha-config-energy.ts | 20 +++--- 11 files changed, 79 insertions(+), 54 deletions(-) diff --git a/src/components/ha-textfield.ts b/src/components/ha-textfield.ts index 15f92ad470..5377e71e7f 100644 --- a/src/components/ha-textfield.ts +++ b/src/components/ha-textfield.ts @@ -82,6 +82,13 @@ export class HaTextField extends TextFieldBase { 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 { text-align: var(--text-field-text-align, start); } diff --git a/src/data/energy.ts b/src/data/energy.ts index 0d73a9994a..c8bb6cf55a 100644 --- a/src/data/energy.ts +++ b/src/data/energy.ts @@ -630,7 +630,7 @@ export const getEnergyGasUnitCategory = ( const statisticIdWithMeta = statisticsMetaData[source.stat_energy_from]; if (statisticIdWithMeta) { return ENERGY_GAS_VOLUME_UNITS.includes( - statisticIdWithMeta.display_unit_of_measurement + statisticIdWithMeta.statistics_unit_of_measurement ) ? "volume" : "energy"; diff --git a/src/panels/config/energy/components/ha-energy-battery-settings.ts b/src/panels/config/energy/components/ha-energy-battery-settings.ts index 9a7b3c6ad1..4d7ce0101a 100644 --- a/src/panels/config/energy/components/ha-energy-battery-settings.ts +++ b/src/panels/config/energy/components/ha-energy-battery-settings.ts @@ -36,7 +36,7 @@ export class EnergyBatterySettings extends LitElement { public preferences!: EnergyPreferences; @property({ attribute: false }) - public statsMetadata!: Record; + public statsMetadata?: Record; @property({ attribute: false }) public validationResult?: EnergyPreferencesValidation; @@ -104,14 +104,14 @@ export class EnergyBatterySettings extends LitElement { >${getStatisticLabel( this.hass, source.stat_energy_from, - this.statsMetadata[source.stat_energy_from] + this.statsMetadata?.[source.stat_energy_from] )} ${getStatisticLabel( this.hass, source.stat_energy_to, - this.statsMetadata[source.stat_energy_to] + this.statsMetadata?.[source.stat_energy_to] )} diff --git a/src/panels/config/energy/components/ha-energy-device-settings.ts b/src/panels/config/energy/components/ha-energy-device-settings.ts index 1d03ed1326..52012f4106 100644 --- a/src/panels/config/energy/components/ha-energy-device-settings.ts +++ b/src/panels/config/energy/components/ha-energy-device-settings.ts @@ -35,7 +35,7 @@ export class EnergyDeviceSettings extends LitElement { public preferences!: EnergyPreferences; @property({ attribute: false }) - public statsMetadata!: Record; + public statsMetadata?: Record; @property({ attribute: false }) public validationResult?: EnergyPreferencesValidation; @@ -90,7 +90,7 @@ export class EnergyDeviceSettings extends LitElement { >${getStatisticLabel( this.hass, device.stat_consumption, - this.statsMetadata[device.stat_consumption] + this.statsMetadata?.[device.stat_consumption] )} ; + public statsMetadata?: Record; @property({ attribute: false }) public validationResult?: EnergyPreferencesValidation; @@ -98,7 +98,7 @@ export class EnergyGasSettings extends LitElement { >${getStatisticLabel( this.hass, source.stat_energy_from, - this.statsMetadata[source.stat_energy_from] + this.statsMetadata?.[source.stat_energy_from] )} { delete source.unit_of_measurement; await this._savePreferences({ @@ -149,11 +152,12 @@ export class EnergyGasSettings extends LitElement { ev.currentTarget.closest(".row").source; showEnergySettingsGasDialog(this, { source: { ...origSource }, - unit: getEnergyGasUnitCategory( + allowedGasUnitCategory: getEnergyGasUnitCategory( this.preferences, this.statsMetadata, origSource.stat_energy_from ), + metadata: this.statsMetadata?.[origSource.stat_energy_from], saveCallback: async (newSource) => { await this._savePreferences({ ...this.preferences, diff --git a/src/panels/config/energy/components/ha-energy-grid-settings.ts b/src/panels/config/energy/components/ha-energy-grid-settings.ts index e4e85d79c4..c04993b0ec 100644 --- a/src/panels/config/energy/components/ha-energy-grid-settings.ts +++ b/src/panels/config/energy/components/ha-energy-grid-settings.ts @@ -55,7 +55,7 @@ export class EnergyGridSettings extends LitElement { public preferences!: EnergyPreferences; @property({ attribute: false }) - public statsMetadata!: Record; + public statsMetadata?: Record; @property({ attribute: false }) public validationResult?: EnergyPreferencesValidation; @@ -136,7 +136,7 @@ export class EnergyGridSettings extends LitElement { >${getStatisticLabel( this.hass, flow.stat_energy_from, - this.statsMetadata[flow.stat_energy_from] + this.statsMetadata?.[flow.stat_energy_from] )} ${getStatisticLabel( this.hass, flow.stat_energy_to, - this.statsMetadata[flow.stat_energy_to] + this.statsMetadata?.[flow.stat_energy_to] )} ; + public statsMetadata?: Record; @property({ attribute: false }) public validationResult?: EnergyPreferencesValidation; @@ -106,7 +106,7 @@ export class EnergySolarSettings extends LitElement { >${getStatisticLabel( this.hass, source.stat_energy_from, - this.statsMetadata[source.stat_energy_from] + this.statsMetadata?.[source.stat_energy_from] )} ${this.info diff --git a/src/panels/config/energy/components/ha-energy-validation-result.ts b/src/panels/config/energy/components/ha-energy-validation-result.ts index 88e26b42a3..4757e1f552 100644 --- a/src/panels/config/energy/components/ha-energy-validation-result.ts +++ b/src/panels/config/energy/components/ha-energy-validation-result.ts @@ -32,9 +32,7 @@ class EnergyValidationMessage extends LitElement { > ${this.hass.localize( `ui.panel.config.energy.validation.issues.${issueType}.description`, - issueType === "entity_unexpected_unit_price" - ? { currency: this.hass.config.currency } - : undefined + { currency: this.hass.config.currency } )} ${ issueType === "recorder_untracked" diff --git a/src/panels/config/energy/dialogs/dialog-energy-gas-settings.ts b/src/panels/config/energy/dialogs/dialog-energy-gas-settings.ts index 16ba76d3f8..2b382a40f4 100644 --- a/src/panels/config/energy/dialogs/dialog-energy-gas-settings.ts +++ b/src/panels/config/energy/dialogs/dialog-energy-gas-settings.ts @@ -21,6 +21,7 @@ import "../../../../components/ha-radio"; import "../../../../components/ha-formfield"; import "../../../../components/ha-textfield"; import type { HaRadio } from "../../../../components/ha-radio"; +import { getStatisticMetadata } from "../../../../data/recorder"; @customElement("dialog-energy-gas-settings") export class DialogEnergyGasSettings @@ -35,7 +36,9 @@ export class DialogEnergyGasSettings @state() private _costs?: "no-costs" | "number" | "entity" | "statistic"; - @state() private _unit?: string; + @state() private _pickableUnit?: string; + + @state() private _pickedDisplayUnit?: string; @state() private _error?: string; @@ -46,6 +49,7 @@ export class DialogEnergyGasSettings this._source = params.source ? { ...params.source } : emptyGasEnergyPreference(); + this._pickedDisplayUnit = params.metadata?.display_unit_of_measurement; this._costs = this._source.entity_energy_price ? "entity" : this._source.number_energy_price @@ -58,7 +62,8 @@ export class DialogEnergyGasSettings public closeDialog(): void { this._params = undefined; this._source = undefined; - this._unit = undefined; + this._pickableUnit = undefined; + this._pickedDisplayUnit = undefined; this._error = undefined; fireEvent(this, "dialog-closed", { dialog: this.localName }); } @@ -68,13 +73,16 @@ export class DialogEnergyGasSettings return html``; } - const unit = - this._unit || - (this._params.unit === undefined - ? "m³ or kWh" - : this._params.unit === "energy" - ? "kWh" - : "m³"); + const pickableUnit = + this._pickableUnit || + (this._params.allowedGasUnitCategory === undefined + ? "ft³, m³, Wh, kWh or MWh" + : this._params.allowedGasUnitCategory === "energy" + ? "Wh, kWh or MWh" + : "ft³ or m³"); + + const externalSource = + this._source.stat_cost && this._source.stat_cost.includes(":"); return html` @@ -160,6 +168,7 @@ export class DialogEnergyGasSettings value="entity" name="costs" .checked=${this._costs === "entity"} + .disabled=${externalSource} @change=${this._handleCostChanged} > @@ -171,7 +180,7 @@ export class DialogEnergyGasSettings .value=${this._source.entity_energy_price} .label=${this.hass.localize( `ui.panel.config.energy.gas.dialog.cost_entity_input`, - { unit } + { unit: this._pickedDisplayUnit || pickableUnit } )} @value-changed=${this._priceEntityChanged} >` @@ -192,14 +201,16 @@ export class DialogEnergyGasSettings ? html` ` : ""} @@ -250,18 +261,21 @@ export class DialogEnergyGasSettings }; } - private _statisticChanged(ev: CustomEvent<{ value: string }>) { + private async _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; + this._pickedDisplayUnit = entity.attributes.unit_of_measurement; + } else { + this._pickedDisplayUnit = ( + await getStatisticMetadata(this.hass, [ev.detail.value]) + )[0]?.display_unit_of_measurement; } } else { - this._unit = undefined; + this._pickedDisplayUnit = undefined; + } + if (ev.detail.value.includes(":") && this._costs !== "statistic") { + this._costs = "no-costs"; } this._source = { ...this._source!, diff --git a/src/panels/config/energy/dialogs/show-dialogs-energy.ts b/src/panels/config/energy/dialogs/show-dialogs-energy.ts index 6feb7e7078..ac27b8206b 100644 --- a/src/panels/config/energy/dialogs/show-dialogs-energy.ts +++ b/src/panels/config/energy/dialogs/show-dialogs-energy.ts @@ -9,6 +9,7 @@ import { GasSourceTypeEnergyPreference, SolarSourceTypeEnergyPreference, } from "../../../../data/energy"; +import { StatisticsMetaData } from "../../../../data/recorder"; export interface EnergySettingsGridFlowDialogParams { source?: @@ -45,7 +46,8 @@ export interface EnergySettingsBatteryDialogParams { export interface EnergySettingsGasDialogParams { source?: GasSourceTypeEnergyPreference; - unit?: EnergyGasUnit; + allowedGasUnitCategory?: EnergyGasUnit; + metadata?: StatisticsMetaData; saveCallback: (source: GasSourceTypeEnergyPreference) => Promise; } diff --git a/src/panels/config/energy/ha-config-energy.ts b/src/panels/config/energy/ha-config-energy.ts index d779876137..22006e511a 100644 --- a/src/panels/config/energy/ha-config-energy.ts +++ b/src/panels/config/energy/ha-config-energy.ts @@ -90,37 +90,37 @@ class HaConfigEnergy extends LitElement {