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

View File

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

View File

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

View File

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