Compare commits

...
4 changed files with 65 additions and 18 deletions
@@ -85,6 +85,14 @@ export const hasGasRateSource = (prefs: EnergyPreferences): boolean =>
(source) => source.type === "gas" && !!source.stat_rate
);
/** Whether the Now view has any live power or flow-rate content to show. */
export const hasNowViewContent = (prefs: EnergyPreferences): boolean =>
hasPowerSources(prefs) ||
hasPowerDevices(prefs) ||
hasWaterRateSource(prefs) ||
hasWaterRateDevices(prefs) ||
hasGasRateSource(prefs);
// --- Card catalog ----------------------------------------------------------
export interface EnergyCardCatalogEntry {
@@ -18,7 +18,7 @@ import {
hasDeviceConsumption,
hasEnergySource,
hasGasSource,
hasPowerDevices,
hasNowViewContent,
hasPowerSources,
hasWaterDevices,
hasWaterSource,
@@ -90,7 +90,8 @@ export class EnergyDashboardStrategy extends ReactiveElement {
if (
!prefs ||
(prefs.device_consumption.length === 0 &&
prefs.energy_sources.length === 0)
prefs.energy_sources.length === 0 &&
!hasWaterDevices(prefs))
) {
await import("../cards/energy-setup-wizard-card");
return {
@@ -100,8 +101,6 @@ export class EnergyDashboardStrategy extends ReactiveElement {
const hasEnergy = hasEnergySource(prefs);
const hasPowerSource = hasPowerSources(prefs);
const hasDevicePower = hasPowerDevices(prefs);
const hasPower = hasPowerSource || hasDevicePower;
const hasWater = hasWaterSource(prefs) || hasWaterDevices(prefs);
const hasGas = hasGasSource(prefs);
const hasDevices = hasDeviceConsumption(prefs);
@@ -118,7 +117,7 @@ export class EnergyDashboardStrategy extends ReactiveElement {
if (hasWater) {
candidateViews.push(WATER_VIEW);
}
if (hasPower) {
if (hasNowViewContent(prefs)) {
candidateViews.push(POWER_VIEW);
}
if (
@@ -9,9 +9,8 @@ import type { HomeAssistant } from "../../../types";
import type { EnergyViewStrategyConfig } from "./energy-cards";
import {
hasGasRateSource,
hasPowerDevices,
hasNowViewContent,
hasPowerSources,
hasWaterRateDevices,
hasWaterRateSource,
isEnergyCardVisible,
} from "./energy-cards";
@@ -54,20 +53,11 @@ export class PowerViewStrategy extends ReactiveElement {
};
const hasPowerSrc = !!prefs && hasPowerSources(prefs);
const hasPowerDev = !!prefs && hasPowerDevices(prefs);
const hasWaterDev = !!prefs && hasWaterRateDevices(prefs);
const hasWaterSrc = !!prefs && hasWaterRateSource(prefs);
const hasGasSrc = !!prefs && hasGasRateSource(prefs);
// No sources configured
if (
!prefs ||
(!hasPowerSrc &&
!hasPowerDev &&
!hasWaterDev &&
!hasWaterSrc &&
!hasGasSrc)
) {
// No live power or flow-rate sources configured
if (!prefs || !hasNowViewContent(prefs)) {
return view;
}
@@ -9,6 +9,7 @@ import {
energyCardKey,
hasEnergySource,
hasGasRateSource,
hasNowViewContent,
hasWaterRateSource,
isEnergyCardHidden,
isEnergyCardVisible,
@@ -166,6 +167,55 @@ describe("source predicates", () => {
true
);
});
it("hasNowViewContent is true for any live power or flow-rate content", () => {
expect(hasNowViewContent(makePrefs({ energy_sources: [WATER] }))).toBe(
false
);
expect(hasNowViewContent(makePrefs({ energy_sources: [WATER_RATE] }))).toBe(
true
);
expect(hasNowViewContent(makePrefs({ energy_sources: [GAS_RATE] }))).toBe(
true
);
expect(
hasNowViewContent(
makePrefs({
device_consumption_water: [
{
stat_consumption: "sensor.water_device",
stat_rate: "sensor.water_device_rate",
},
],
})
)
).toBe(true);
expect(
hasNowViewContent(
makePrefs({
device_consumption: [
{
stat_consumption: "sensor.device",
stat_rate: "sensor.device_rate",
},
],
})
)
).toBe(true);
expect(
hasNowViewContent(
makePrefs({
energy_sources: [
source({
type: "solar",
stat_energy_from: "sensor.solar",
stat_rate: "sensor.solar_power",
}),
],
})
)
).toBe(true);
});
});
describe("isEnergyCardVisible", () => {