Allow specifying collection keys so energy dashboard has own (#9674)

This commit is contained in:
Paulus Schoutsen 2021-08-02 05:23:15 -07:00 committed by GitHub
parent f19f2ff321
commit 481a79e311
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 62 additions and 24 deletions

View File

@ -240,15 +240,23 @@ export interface EnergyCollection extends Collection<EnergyData> {
export const getEnergyDataCollection = (
hass: HomeAssistant,
prefs?: EnergyPreferences
options: { prefs?: EnergyPreferences; key?: string } = {}
): EnergyCollection => {
if ((hass.connection as any)._energy) {
return (hass.connection as any)._energy;
let key = "_energy";
if (options.key) {
if (!options.key.startsWith("energy_")) {
throw new Error("Key need to start with energy_");
}
key = `_${options.key}`;
}
if ((hass.connection as any)[key]) {
return (hass.connection as any)[key];
}
const collection = getCollection<EnergyData>(
hass.connection,
"_energy",
key,
async () => {
if (!collection.prefs) {
// This will raise if not found.
@ -304,7 +312,7 @@ export const getEnergyDataCollection = (
};
collection._active = 0;
collection.prefs = prefs;
collection.prefs = options.prefs;
const now = new Date();
// Set start to start of today if we have data for today, otherwise yesterday
collection.start = now.getHours() > 0 ? startOfToday() : startOfYesterday();

View File

@ -52,10 +52,11 @@ export class EnergyStrategy {
(source) => source.type === "solar"
);
getEnergyDataCollection(hass, prefs);
getEnergyDataCollection(hass, { prefs, key: "energy_dashboard" });
view.cards!.push({
type: "energy-date-selection",
collection_key: "energy_dashboard",
});
// Only include if we have a grid source.
@ -63,6 +64,7 @@ export class EnergyStrategy {
view.cards!.push({
title: "Energy usage",
type: "energy-usage-graph",
collection_key: "energy_dashboard",
});
}
@ -71,6 +73,7 @@ export class EnergyStrategy {
view.cards!.push({
title: "Solar production",
type: "energy-solar-graph",
collection_key: "energy_dashboard",
});
}
@ -80,6 +83,7 @@ export class EnergyStrategy {
title: "Energy distribution",
type: "energy-distribution",
view_layout: { position: "sidebar" },
collection_key: "energy_dashboard",
});
}
@ -87,6 +91,7 @@ export class EnergyStrategy {
view.cards!.push({
title: "Sources",
type: "energy-sources-table",
collection_key: "energy_dashboard",
});
}
@ -95,6 +100,7 @@ export class EnergyStrategy {
view.cards!.push({
type: "energy-grid-neutrality-gauge",
view_layout: { position: "sidebar" },
collection_key: "energy_dashboard",
});
}
@ -103,6 +109,7 @@ export class EnergyStrategy {
view.cards!.push({
type: "energy-solar-consumed-gauge",
view_layout: { position: "sidebar" },
collection_key: "energy_dashboard",
});
}
@ -111,6 +118,7 @@ export class EnergyStrategy {
view.cards!.push({
type: "energy-carbon-consumed-gauge",
view_layout: { position: "sidebar" },
collection_key: "energy_dashboard",
});
}
@ -119,6 +127,7 @@ export class EnergyStrategy {
view.cards!.push({
title: "Monitor individual devices",
type: "energy-devices-graph",
collection_key: "energy_dashboard",
});
}

View File

@ -45,7 +45,9 @@ class HuiEnergyCarbonGaugeCard
public hassSubscribe(): UnsubscribeFunc[] {
return [
getEnergyDataCollection(this.hass).subscribe((data) => {
getEnergyDataCollection(this.hass, {
key: this._config?.collection_key,
}).subscribe((data) => {
this._data = data;
}),
];

View File

@ -37,9 +37,9 @@ export class HuiEnergyDateSelectionCard
public hassSubscribe(): UnsubscribeFunc[] {
return [
getEnergyDataCollection(this.hass).subscribe((data) =>
this._updateDates(data)
),
getEnergyDataCollection(this.hass, {
key: this._config?.collection_key,
}).subscribe((data) => this._updateDates(data)),
];
}
@ -103,7 +103,9 @@ export class HuiEnergyDateSelectionCard
) {
return;
}
const energyCollection = getEnergyDataCollection(this.hass);
const energyCollection = getEnergyDataCollection(this.hass, {
key: this._config?.collection_key,
});
energyCollection.setPeriod(ev.detail.startDate, ev.detail.endDate);
energyCollection.refresh();
}

View File

@ -45,9 +45,9 @@ export class HuiEnergyDevicesGraphCard
public hassSubscribe(): UnsubscribeFunc[] {
return [
getEnergyDataCollection(this.hass).subscribe((data) =>
this._getStatistics(data)
),
getEnergyDataCollection(this.hass, {
key: this._config?.collection_key,
}).subscribe((data) => this._getStatistics(data)),
];
}

View File

@ -47,7 +47,9 @@ class HuiEnergyDistrubutionCard
public hassSubscribe(): UnsubscribeFunc[] {
return [
getEnergyDataCollection(this.hass).subscribe((data) => {
getEnergyDataCollection(this.hass, {
key: this._config?.collection_key,
}).subscribe((data) => {
this._data = data;
}),
];

View File

@ -38,7 +38,9 @@ class HuiEnergyGridGaugeCard
public hassSubscribe(): UnsubscribeFunc[] {
return [
getEnergyDataCollection(this.hass!).subscribe((data) => {
getEnergyDataCollection(this.hass!, {
key: this._config?.collection_key,
}).subscribe((data) => {
this._data = data;
}),
];

View File

@ -32,7 +32,9 @@ class HuiEnergySolarGaugeCard
public hassSubscribe(): UnsubscribeFunc[] {
return [
getEnergyDataCollection(this.hass!).subscribe((data) => {
getEnergyDataCollection(this.hass!, {
key: this._config?.collection_key,
}).subscribe((data) => {
this._data = data;
}),
];

View File

@ -58,9 +58,9 @@ export class HuiEnergySolarGraphCard
public hassSubscribe(): UnsubscribeFunc[] {
return [
getEnergyDataCollection(this.hass).subscribe((data) =>
this._getStatistics(data)
),
getEnergyDataCollection(this.hass, {
key: this._config?.collection_key,
}).subscribe((data) => this._getStatistics(data)),
];
}

View File

@ -46,7 +46,9 @@ export class HuiEnergySourcesTableCard
public hassSubscribe(): UnsubscribeFunc[] {
return [
getEnergyDataCollection(this.hass).subscribe((data) => {
getEnergyDataCollection(this.hass, {
key: this._config?.collection_key,
}).subscribe((data) => {
this._data = data;
}),
];

View File

@ -46,9 +46,9 @@ export class HuiEnergyUsageGraphCard
public hassSubscribe(): UnsubscribeFunc[] {
return [
getEnergyDataCollection(this.hass).subscribe((data) =>
this._getStatistics(data)
),
getEnergyDataCollection(this.hass, {
key: this._config?.collection_key,
}).subscribe((data) => this._getStatistics(data)),
];
}

View File

@ -92,45 +92,54 @@ export interface ButtonCardConfig extends LovelaceCardConfig {
export interface EnergySummaryCardConfig extends LovelaceCardConfig {
type: "energy-summary";
title?: string;
collection_key?: string;
}
export interface EnergyDistributionCardConfig extends LovelaceCardConfig {
type: "energy-distribution";
title?: string;
collection_key?: string;
}
export interface EnergyUsageGraphCardConfig extends LovelaceCardConfig {
type: "energy-summary-graph";
title?: string;
collection_key?: string;
}
export interface EnergySolarGraphCardConfig extends LovelaceCardConfig {
type: "energy-solar-graph";
title?: string;
collection_key?: string;
}
export interface EnergyDevicesGraphCardConfig extends LovelaceCardConfig {
type: "energy-devices-graph";
title?: string;
collection_key?: string;
}
export interface EnergySourcesTableCardConfig extends LovelaceCardConfig {
type: "energy-sources-table";
title?: string;
collection_key?: string;
}
export interface EnergySolarGaugeCardConfig extends LovelaceCardConfig {
type: "energy-solar-consumed-gauge";
title?: string;
collection_key?: string;
}
export interface EnergyGridGaugeCardConfig extends LovelaceCardConfig {
type: "energy-grid-result-gauge";
title?: string;
collection_key?: string;
}
export interface EnergyCarbonGaugeCardConfig extends LovelaceCardConfig {
type: "energy-carbon-consumed-gauge";
title?: string;
collection_key?: string;
}
export interface EntityFilterCardConfig extends LovelaceCardConfig {