Clear all energy collection prefs (#9684)

This commit is contained in:
Bram Kragten 2021-08-02 18:39:01 +02:00 committed by GitHub
parent bcc0052fe0
commit 4d01199986
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 13 deletions

View File

@ -12,6 +12,8 @@ import { ConfigEntry, getConfigEntries } from "./config_entries";
import { subscribeEntityRegistry } from "./entity_registry";
import { fetchStatistics, Statistics } from "./history";
const energyCollectionKeys: string[] = [];
export const emptyFlowFromGridSourceEnergyPreference =
(): FlowFromGridSourceEnergyPreference => ({
stat_energy_from: "",
@ -123,11 +125,7 @@ export const saveEnergyPreferences = async (
type: "energy/save_prefs",
...prefs,
});
const energyCollection = getEnergyDataCollection(hass);
energyCollection.clearPrefs();
if (energyCollection._active) {
energyCollection.refresh();
}
clearEnergyCollectionPreferences(hass);
return newPrefs;
};
@ -252,6 +250,16 @@ export interface EnergyCollection extends Collection<EnergyData> {
_active: number;
}
const clearEnergyCollectionPreferences = (hass: HomeAssistant) => {
energyCollectionKeys.forEach((key) => {
const energyCollection = getEnergyDataCollection(hass, { key });
energyCollection.clearPrefs();
if (energyCollection._active) {
energyCollection.refresh();
}
});
};
export const getEnergyDataCollection = (
hass: HomeAssistant,
options: { prefs?: EnergyPreferences; key?: string } = {}
@ -268,6 +276,8 @@ export const getEnergyDataCollection = (
return (hass.connection as any)[key];
}
energyCollectionKeys.push(options.key || "energy");
const collection = getCollection<EnergyData>(
hass.connection,
key,

View File

@ -1,6 +1,5 @@
import {
EnergyPreferences,
getEnergyDataCollection,
getEnergyPreferences,
GridSourceTypeEnergyPreference,
} from "../../../data/energy";
@ -52,8 +51,6 @@ export class EnergyStrategy {
(source) => source.type === "solar"
);
getEnergyDataCollection(hass, { prefs, key: "energy_dashboard" });
if (info.narrow) {
view.cards!.push({
type: "energy-date-selection",

View File

@ -7,6 +7,8 @@ import { formatDate } from "../../../common/datetime/format_date";
import { EnergyData, getEnergyDataCollection } from "../../../data/energy";
import { SubscribeMixin } from "../../../mixins/subscribe-mixin";
import { HomeAssistant } from "../../../types";
import "@material/mwc-icon-button/mwc-icon-button";
import "../../../components/ha-svg-icon";
@customElement("hui-energy-period-selector")
export class HuiEnergyPeriodSelector extends SubscribeMixin(LitElement) {
@ -47,11 +49,7 @@ export class HuiEnergyPeriodSelector extends SubscribeMixin(LitElement) {
<ha-svg-icon .path=${mdiChevronLeft}></ha-svg-icon>
</mwc-icon-button>
<div class="label">${label}</div>
<mwc-icon-button
.disabled=${isStartToday}
label="Next Day"
@click=${this._pickNextDay}
>
<mwc-icon-button label="Next Day" @click=${this._pickNextDay}>
<ha-svg-icon .path=${mdiChevronRight}></ha-svg-icon>
</mwc-icon-button>
</div>