Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remember hidden detailed energy device statistics #21406

Merged
merged 2 commits into from
Jul 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { LovelaceCard } from "../../types";
import { EnergyDevicesDetailGraphCardConfig } from "../types";
import { hasConfigChanged } from "../../common/has-changed";
import { getCommonOptions } from "./common/energy-chart-options";
import { storage } from "../../../../common/decorators/storage";

const UNIT = "kWh";

Expand All @@ -64,7 +65,12 @@ export class HuiEnergyDevicesDetailGraphCard

@state() private _compareEnd?: Date;

@state() private _hiddenStats = new Set<string>();
@storage({
key: "energy-devices-hidden-stats",
state: true,
subscribe: false,
})
private _hiddenStats: string[] = [];

protected hassSubscribeRequiredHostProps = ["_config"];

Expand Down Expand Up @@ -143,19 +149,18 @@ export class HuiEnergyDevicesDetailGraphCard
}

private _datasetHidden(ev) {
ev.stopPropagation();
this._hiddenStats.add(
this._data!.prefs.device_consumption[ev.detail.index].stat_consumption
);
this.requestUpdate("_hiddenStats");
this._hiddenStats = [
...this._hiddenStats,
this._data!.prefs.device_consumption[ev.detail.index].stat_consumption,
];
Comment on lines +152 to +155
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure no duplicates in _hiddenStats.

The method _datasetHidden currently adds statistics directly to _hiddenStats. Consider checking for duplicates before adding to prevent the same stat from being hidden multiple times.

- this._hiddenStats = [...this._hiddenStats, this._data!.prefs.device_consumption[ev.detail.index].stat_consumption];
+ if (!this._hiddenStats.includes(this._data!.prefs.device_consumption[ev.detail.index].stat_consumption)) {
+   this._hiddenStats = [...this._hiddenStats, this._data!.prefs.device_consumption[ev.detail.index].stat_consumption];
+ }
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
this._hiddenStats = [
...this._hiddenStats,
this._data!.prefs.device_consumption[ev.detail.index].stat_consumption,
];
if (!this._hiddenStats.includes(this._data!.prefs.device_consumption[ev.detail.index].stat_consumption)) {
this._hiddenStats = [
...this._hiddenStats,
this._data!.prefs.device_consumption[ev.detail.index].stat_consumption,
];
}

}

private _datasetUnhidden(ev) {
ev.stopPropagation();
this._hiddenStats.delete(
this._data!.prefs.device_consumption[ev.detail.index].stat_consumption
this._hiddenStats = this._hiddenStats.filter(
(stat) =>
stat !==
this._data!.prefs.device_consumption[ev.detail.index].stat_consumption
);
this.requestUpdate("_hiddenStats");
}

private _createOptions = memoizeOne(
Expand Down Expand Up @@ -341,7 +346,7 @@ export class HuiEnergyDevicesDetailGraphCard
statisticsMetaData[source.stat_consumption]
),
hidden:
this._hiddenStats.has(source.stat_consumption) || itemExceedsMax,
this._hiddenStats.includes(source.stat_consumption) || itemExceedsMax,
borderColor: compare ? color + "7F" : color,
backgroundColor: compare ? color + "32" : color + "7F",
data: consumptionData,
Expand Down
Loading