Skip to content

Commit

Permalink
Filter energy grid sources to not allow duplicates (#17381)
Browse files Browse the repository at this point in the history
  • Loading branch information
karwosts authored Aug 9, 2023
1 parent a2b1be7 commit 416661f
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 14 deletions.
23 changes: 20 additions & 3 deletions src/components/entity/ha-statistic-picker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 [
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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(() => {
Expand All @@ -249,7 +265,8 @@ export class HaStatisticPicker extends LitElement {
this.includeStatisticsUnitOfMeasurement,
this.includeUnitClass,
this.includeDeviceClass,
this.entitiesOnly
this.entitiesOnly,
this.excludeStatistics
);
});
}
Expand Down
24 changes: 16 additions & 8 deletions src/panels/config/energy/components/ha-energy-grid-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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]
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ export class DialogEnergyGridFlowSettings

@state() private _error?: string;

private _excludeList?: string[];

public async showDialog(
params: EnergySettingsGridFlowDialogParams
): Promise<void> {
Expand All @@ -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 {
Expand Down Expand Up @@ -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>
Expand Down
4 changes: 4 additions & 0 deletions src/panels/config/energy/dialogs/show-dialogs-energy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
FlowFromGridSourceEnergyPreference,
FlowToGridSourceEnergyPreference,
GasSourceTypeEnergyPreference,
GridSourceTypeEnergyPreference,
SolarSourceTypeEnergyPreference,
WaterSourceTypeEnergyPreference,
} from "../../../../data/energy";
Expand All @@ -18,6 +19,7 @@ export interface EnergySettingsGridFlowDialogParams {
| FlowToGridSourceEnergyPreference;
metadata?: StatisticsMetaData;
direction: "from" | "to";
grid_source?: GridSourceTypeEnergyPreference;
saveCallback: (
source:
| FlowFromGridSourceEnergyPreference
Expand All @@ -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>;
}

Expand Down

0 comments on commit 416661f

Please sign in to comment.