Filter energy grid sources to not allow duplicates (#17381)

This commit is contained in:
karwosts 2023-08-09 07:18:57 -07:00 committed by GitHub
parent a2b1be754f
commit 416661f3d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 59 additions and 14 deletions

View File

@ -79,6 +79,14 @@ export class HaStatisticPicker extends LitElement {
@property({ type: Boolean, attribute: "entities-only" })
public entitiesOnly = false;
/**
* List of statistics to be excluded.
* @type {Array}
* @attr exclude-statistics
*/
@property({ type: Array, attribute: "exclude-statistics" })
public excludeStatistics?: string[];
@state() private _opened?: boolean;
@query("ha-combo-box", true) public comboBox!: HaComboBox;
@ -118,7 +126,8 @@ export class HaStatisticPicker extends LitElement {
includeStatisticsUnitOfMeasurement?: string | string[],
includeUnitClass?: string | string[],
includeDeviceClass?: string | string[],
entitiesOnly?: boolean
entitiesOnly?: boolean,
excludeStatistics?: string[]
): StatisticItem[] => {
if (!statisticIds.length) {
return [
@ -163,6 +172,12 @@ export class HaStatisticPicker extends LitElement {
const output: StatisticItem[] = [];
statisticIds.forEach((meta) => {
if (
excludeStatistics &&
excludeStatistics.includes(meta.statistic_id)
) {
return;
}
const entityState = this.hass.states[meta.statistic_id];
if (!entityState) {
if (!entitiesOnly) {
@ -240,7 +255,8 @@ export class HaStatisticPicker extends LitElement {
this.includeStatisticsUnitOfMeasurement,
this.includeUnitClass,
this.includeDeviceClass,
this.entitiesOnly
this.entitiesOnly,
this.excludeStatistics
);
} else {
this.updateComplete.then(() => {
@ -249,7 +265,8 @@ export class HaStatisticPicker extends LitElement {
this.includeStatisticsUnitOfMeasurement,
this.includeUnitClass,
this.includeDeviceClass,
this.entitiesOnly
this.entitiesOnly,
this.excludeStatistics
);
});
}

View File

@ -294,13 +294,13 @@ export class EnergyGridSettings extends LitElement {
}
private _addFromSource() {
const gridSource = this.preferences.energy_sources.find(
(src) => src.type === "grid"
) as GridSourceTypeEnergyPreference | undefined;
showEnergySettingsGridFlowFromDialog(this, {
grid_source: gridSource,
saveCallback: async (flow) => {
let preferences: EnergyPreferences;
const gridSource = this.preferences.energy_sources.find(
(src) => src.type === "grid"
) as GridSourceTypeEnergyPreference | undefined;
if (!gridSource) {
preferences = {
...this.preferences,
@ -328,13 +328,13 @@ export class EnergyGridSettings extends LitElement {
}
private _addToSource() {
const gridSource = this.preferences.energy_sources.find(
(src) => src.type === "grid"
) as GridSourceTypeEnergyPreference | undefined;
showEnergySettingsGridFlowToDialog(this, {
grid_source: gridSource,
saveCallback: async (flow) => {
let preferences: EnergyPreferences;
const gridSource = this.preferences.energy_sources.find(
(src) => src.type === "grid"
) as GridSourceTypeEnergyPreference | undefined;
if (!gridSource) {
preferences = {
...this.preferences,
@ -364,8 +364,12 @@ export class EnergyGridSettings extends LitElement {
private _editFromSource(ev) {
const origSource: FlowFromGridSourceEnergyPreference =
ev.currentTarget.closest(".row").source;
const gridSource = this.preferences.energy_sources.find(
(src) => src.type === "grid"
) as GridSourceTypeEnergyPreference | undefined;
showEnergySettingsGridFlowFromDialog(this, {
source: { ...origSource },
grid_source: gridSource,
metadata: this.statsMetadata?.[origSource.stat_energy_from],
saveCallback: async (source) => {
const flowFrom = energySourcesByType(this.preferences).grid![0]
@ -392,8 +396,12 @@ export class EnergyGridSettings extends LitElement {
private _editToSource(ev) {
const origSource: FlowToGridSourceEnergyPreference =
ev.currentTarget.closest(".row").source;
const gridSource = this.preferences.energy_sources.find(
(src) => src.type === "grid"
) as GridSourceTypeEnergyPreference | undefined;
showEnergySettingsGridFlowToDialog(this, {
source: { ...origSource },
grid_source: gridSource,
metadata: this.statsMetadata?.[origSource.stat_energy_to],
saveCallback: async (source) => {
const flowTo = energySourcesByType(this.preferences).grid![0].flow_to;

View File

@ -49,6 +49,8 @@ export class DialogEnergyGridFlowSettings
@state() private _error?: string;
private _excludeList?: string[];
public async showDialog(
params: EnergySettingsGridFlowDialogParams
): Promise<void> {
@ -67,18 +69,31 @@ export class DialogEnergyGridFlowSettings
]
? "statistic"
: "no-costs";
this._pickedDisplayUnit = getDisplayUnit(
this.hass,
const initialSourceId =
this._source[
this._params.direction === "from"
? "stat_energy_from"
: "stat_energy_to"
],
];
this._pickedDisplayUnit = getDisplayUnit(
this.hass,
initialSourceId,
params.metadata
);
this._energy_units = (
await getSensorDeviceClassConvertibleUnits(this.hass, "energy")
).units;
this._excludeList = [
...(this._params.grid_source?.flow_from?.map(
(entry) => entry.stat_energy_from
) || []),
...(this._params.grid_source?.flow_to?.map(
(entry) => entry.stat_energy_to
) || []),
].filter((id) => id !== initialSourceId);
}
public closeDialog(): void {
@ -154,6 +169,7 @@ export class DialogEnergyGridFlowSettings
.label=${this.hass.localize(
`ui.panel.config.energy.grid.flow_dialog.${this._params.direction}.energy_stat`
)}
.excludeStatistics=${this._excludeList}
@value-changed=${this._statisticChanged}
dialogInitialFocus
></ha-statistic-picker>

View File

@ -7,6 +7,7 @@ import {
FlowFromGridSourceEnergyPreference,
FlowToGridSourceEnergyPreference,
GasSourceTypeEnergyPreference,
GridSourceTypeEnergyPreference,
SolarSourceTypeEnergyPreference,
WaterSourceTypeEnergyPreference,
} from "../../../../data/energy";
@ -18,6 +19,7 @@ export interface EnergySettingsGridFlowDialogParams {
| FlowToGridSourceEnergyPreference;
metadata?: StatisticsMetaData;
direction: "from" | "to";
grid_source?: GridSourceTypeEnergyPreference;
saveCallback: (
source:
| FlowFromGridSourceEnergyPreference
@ -28,12 +30,14 @@ export interface EnergySettingsGridFlowDialogParams {
export interface EnergySettingsGridFlowFromDialogParams {
source?: FlowFromGridSourceEnergyPreference;
metadata?: StatisticsMetaData;
grid_source?: GridSourceTypeEnergyPreference;
saveCallback: (source: FlowFromGridSourceEnergyPreference) => Promise<void>;
}
export interface EnergySettingsGridFlowToDialogParams {
source?: FlowToGridSourceEnergyPreference;
metadata?: StatisticsMetaData;
grid_source?: GridSourceTypeEnergyPreference;
saveCallback: (source: FlowToGridSourceEnergyPreference) => Promise<void>;
}