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

View File

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

View File

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