mirror of
https://github.com/home-assistant/frontend.git
synced 2025-04-26 14:27:20 +00:00
Add energy distribution card to auto lovelace if energy configured (#9675)
* Add energy distribution card to auto lovelace if energy configured * Add import Co-authored-by: Bram Kragten <mail@bramkragten.nl>
This commit is contained in:
parent
481a79e311
commit
847fa2e700
@ -11,6 +11,7 @@ import { css, html, LitElement, svg } from "lit";
|
|||||||
import { customElement, property, state } from "lit/decorators";
|
import { customElement, property, state } from "lit/decorators";
|
||||||
import { classMap } from "lit/directives/class-map";
|
import { classMap } from "lit/directives/class-map";
|
||||||
import { ifDefined } from "lit/directives/if-defined";
|
import { ifDefined } from "lit/directives/if-defined";
|
||||||
|
import "@material/mwc-button";
|
||||||
import { formatNumber } from "../../../../common/string/format_number";
|
import { formatNumber } from "../../../../common/string/format_number";
|
||||||
import "../../../../components/ha-card";
|
import "../../../../components/ha-card";
|
||||||
import "../../../../components/ha-svg-icon";
|
import "../../../../components/ha-svg-icon";
|
||||||
@ -401,6 +402,15 @@ class HuiEnergyDistrubutionCard
|
|||||||
</svg>
|
</svg>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
${this._config.linkDashboard
|
||||||
|
? html`
|
||||||
|
<div class="card-actions">
|
||||||
|
<a href="/energy"
|
||||||
|
><mwc-button> Go to the energy dashboard </mwc-button></a
|
||||||
|
>
|
||||||
|
</div>
|
||||||
|
`
|
||||||
|
: ""}
|
||||||
</ha-card>
|
</ha-card>
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
@ -556,6 +566,9 @@ class HuiEnergyDistrubutionCard
|
|||||||
stroke-dasharray: 238.76104;
|
stroke-dasharray: 238.76104;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
.card-actions a {
|
||||||
|
text-decoration: none;
|
||||||
|
}
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -98,6 +98,7 @@ export interface EnergySummaryCardConfig extends LovelaceCardConfig {
|
|||||||
export interface EnergyDistributionCardConfig extends LovelaceCardConfig {
|
export interface EnergyDistributionCardConfig extends LovelaceCardConfig {
|
||||||
type: "energy-distribution";
|
type: "energy-distribution";
|
||||||
title?: string;
|
title?: string;
|
||||||
|
linkDashboard?: boolean;
|
||||||
collection_key?: string;
|
collection_key?: string;
|
||||||
}
|
}
|
||||||
export interface EnergyUsageGraphCardConfig extends LovelaceCardConfig {
|
export interface EnergyUsageGraphCardConfig extends LovelaceCardConfig {
|
||||||
|
@ -7,6 +7,10 @@ import { compare } from "../../../common/string/compare";
|
|||||||
import { LocalizeFunc } from "../../../common/translations/localize";
|
import { LocalizeFunc } from "../../../common/translations/localize";
|
||||||
import type { AreaRegistryEntry } from "../../../data/area_registry";
|
import type { AreaRegistryEntry } from "../../../data/area_registry";
|
||||||
import type { DeviceRegistryEntry } from "../../../data/device_registry";
|
import type { DeviceRegistryEntry } from "../../../data/device_registry";
|
||||||
|
import {
|
||||||
|
EnergyPreferences,
|
||||||
|
GridSourceTypeEnergyPreference,
|
||||||
|
} from "../../../data/energy";
|
||||||
import type { EntityRegistryEntry } from "../../../data/entity_registry";
|
import type { EntityRegistryEntry } from "../../../data/entity_registry";
|
||||||
import { domainToName } from "../../../data/integration";
|
import { domainToName } from "../../../data/integration";
|
||||||
import { LovelaceCardConfig, LovelaceViewConfig } from "../../../data/lovelace";
|
import { LovelaceCardConfig, LovelaceViewConfig } from "../../../data/lovelace";
|
||||||
@ -292,7 +296,8 @@ export const generateDefaultViewConfig = (
|
|||||||
deviceEntries: DeviceRegistryEntry[],
|
deviceEntries: DeviceRegistryEntry[],
|
||||||
entityEntries: EntityRegistryEntry[],
|
entityEntries: EntityRegistryEntry[],
|
||||||
entities: HassEntities,
|
entities: HassEntities,
|
||||||
localize: LocalizeFunc
|
localize: LocalizeFunc,
|
||||||
|
energyPrefs?: EnergyPreferences
|
||||||
): LovelaceViewConfig => {
|
): LovelaceViewConfig => {
|
||||||
const states = computeDefaultViewStates(entities, entityEntries);
|
const states = computeDefaultViewStates(entities, entityEntries);
|
||||||
const path = "default_view";
|
const path = "default_view";
|
||||||
@ -337,6 +342,21 @@ export const generateDefaultViewConfig = (
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (energyPrefs) {
|
||||||
|
// Distribution card requires the grid to be configured
|
||||||
|
const grid = energyPrefs.energy_sources.find(
|
||||||
|
(source) => source.type === "grid"
|
||||||
|
) as GridSourceTypeEnergyPreference | undefined;
|
||||||
|
|
||||||
|
if (grid && grid.flow_from.length > 0) {
|
||||||
|
areaCards.push({
|
||||||
|
title: "Energy distribution",
|
||||||
|
type: "energy-distribution",
|
||||||
|
linkDashboard: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
config.cards!.unshift(...areaCards);
|
config.cards!.unshift(...areaCards);
|
||||||
|
|
||||||
return config;
|
return config;
|
||||||
|
@ -2,6 +2,7 @@ import { STATE_NOT_RUNNING } from "home-assistant-js-websocket";
|
|||||||
import { subscribeOne } from "../../../common/util/subscribe-one";
|
import { subscribeOne } from "../../../common/util/subscribe-one";
|
||||||
import { subscribeAreaRegistry } from "../../../data/area_registry";
|
import { subscribeAreaRegistry } from "../../../data/area_registry";
|
||||||
import { subscribeDeviceRegistry } from "../../../data/device_registry";
|
import { subscribeDeviceRegistry } from "../../../data/device_registry";
|
||||||
|
import { getEnergyPreferences } from "../../../data/energy";
|
||||||
import { subscribeEntityRegistry } from "../../../data/entity_registry";
|
import { subscribeEntityRegistry } from "../../../data/entity_registry";
|
||||||
import { generateDefaultViewConfig } from "../common/generate-lovelace-config";
|
import { generateDefaultViewConfig } from "../common/generate-lovelace-config";
|
||||||
import {
|
import {
|
||||||
@ -37,12 +38,14 @@ export class OriginalStatesStrategy {
|
|||||||
subscribeEntityRegistry(hass.connection, () => undefined);
|
subscribeEntityRegistry(hass.connection, () => undefined);
|
||||||
}
|
}
|
||||||
|
|
||||||
const [areaEntries, deviceEntries, entityEntries, localize] =
|
const [areaEntries, deviceEntries, entityEntries, localize, energyPrefs] =
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
subscribeOne(hass.connection, subscribeAreaRegistry),
|
subscribeOne(hass.connection, subscribeAreaRegistry),
|
||||||
subscribeOne(hass.connection, subscribeDeviceRegistry),
|
subscribeOne(hass.connection, subscribeDeviceRegistry),
|
||||||
subscribeOne(hass.connection, subscribeEntityRegistry),
|
subscribeOne(hass.connection, subscribeEntityRegistry),
|
||||||
hass.loadBackendTranslation("title"),
|
hass.loadBackendTranslation("title"),
|
||||||
|
// It raises if not configured, just swallow that.
|
||||||
|
getEnergyPreferences(hass).catch(() => undefined),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// User can override default view. If they didn't, we will add one
|
// User can override default view. If they didn't, we will add one
|
||||||
@ -52,7 +55,8 @@ export class OriginalStatesStrategy {
|
|||||||
deviceEntries,
|
deviceEntries,
|
||||||
entityEntries,
|
entityEntries,
|
||||||
hass.states,
|
hass.states,
|
||||||
localize
|
localize,
|
||||||
|
energyPrefs
|
||||||
);
|
);
|
||||||
|
|
||||||
// Add map of geo locations to default view if loaded
|
// Add map of geo locations to default view if loaded
|
||||||
|
Loading…
x
Reference in New Issue
Block a user