Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/panels/energy/strategies/energy-overview-view-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { DEFAULT_ENERGY_COLLECTION_KEY } from "../constants";

@customElement("energy-overview-view-strategy")
export class EnergyOverviewViewStrategy extends ReactiveElement {
static registryDependencies = [];

static async generate(
_config: LovelaceStrategyConfig,
hass: HomeAssistant
Expand Down
2 changes: 2 additions & 0 deletions src/panels/energy/strategies/energy-view-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {

@customElement("energy-view-strategy")
export class EnergyViewStrategy extends ReactiveElement {
static registryDependencies = [];

static async generate(
_config: LovelaceStrategyConfig,
hass: HomeAssistant
Expand Down
2 changes: 2 additions & 0 deletions src/panels/energy/strategies/gas-view-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import type { LovelaceSectionConfig } from "../../../data/lovelace/config/sectio

@customElement("gas-view-strategy")
export class GasViewStrategy extends ReactiveElement {
static registryDependencies = [];

static async generate(
_config: LovelaceStrategyConfig,
hass: HomeAssistant
Expand Down
2 changes: 2 additions & 0 deletions src/panels/energy/strategies/power-view-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import type { LovelaceBadgeConfig } from "../../../data/lovelace/config/badge";

@customElement("power-view-strategy")
export class PowerViewStrategy extends ReactiveElement {
static registryDependencies = [];

static async generate(
_config: LovelaceStrategyConfig,
hass: HomeAssistant
Expand Down
2 changes: 2 additions & 0 deletions src/panels/energy/strategies/water-view-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { shouldShowFloorsAndAreas } from "./show-floors-and-areas";

@customElement("water-view-strategy")
export class WaterViewStrategy extends ReactiveElement {
static registryDependencies = [];

static async generate(
_config: LovelaceStrategyConfig,
hass: HomeAssistant
Expand Down
59 changes: 26 additions & 33 deletions src/panels/home/ha-panel-home.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { styleMap } from "lit/directives/style-map";
import { atLeastVersion } from "../../common/config/version";
import { navigate } from "../../common/navigate";
import { debounce } from "../../common/util/debounce";
import { deepEqual } from "../../common/util/deep-equal";
import "../../components/ha-button";
import "../../components/ha-svg-icon";
import { updateAreaRegistryEntry } from "../../data/area/area_registry";
Expand All @@ -26,7 +25,10 @@ import { showDeviceRegistryDetailDialog } from "../config/devices/device-registr
import { showAddIntegrationDialog } from "../config/integrations/show-add-integration-dialog";
import "../lovelace/hui-root";
import type { ExtraActionItem } from "../lovelace/hui-root";
import { expandLovelaceConfigStrategies } from "../lovelace/strategies/get-strategy";
import {
checkStrategyShouldRegenerate,
generateLovelaceDashboardStrategy,
} from "../lovelace/strategies/get-strategy";
import type { Lovelace } from "../lovelace/types";
import { showEditHomeDialog } from "./dialogs/show-dialog-edit-home";
import { showNewOverviewDialog } from "./dialogs/show-dialog-new-overview";
Expand Down Expand Up @@ -95,26 +97,19 @@ class PanelHome extends LitElement {
return;
}

if (oldHass && this.hass) {
// If the entity registry changed, ask the user if they want to refresh the config
if (
oldHass.entities !== this.hass.entities ||
oldHass.devices !== this.hass.devices ||
oldHass.areas !== this.hass.areas ||
oldHass.floors !== this.hass.floors ||
oldHass.panels !== this.hass.panels
) {
if (this.hass.config.state === "RUNNING") {
this._debounceRegistriesChanged();
return;
}
}
// If ha started, refresh the config
if (
this.hass.config.state === "RUNNING" &&
oldHass.config.state !== "RUNNING"
) {
if (oldHass && this.hass && this.hass.config.state === "RUNNING") {
if (oldHass.config.state !== "RUNNING") {
this._setup();
return;
}
const shouldRegenerate = checkStrategyShouldRegenerate(
"dashboard",
this._strategyConfig.strategy,
oldHass,
this.hass
);
if (shouldRegenerate) {
this._debounceRegenerateStrategy();
}
}
}
Expand All @@ -135,12 +130,12 @@ class PanelHome extends LitElement {
this._setLovelace();
}

private _debounceRegistriesChanged = debounce(
() => this._registriesChanged(),
private _debounceRegenerateStrategy = debounce(
() => this._regenerateStrategyConfig(),
200
);

private _registriesChanged = async () => {
private _regenerateStrategyConfig() {
// If on an area view that no longer exists, redirect to overview
const path = this.route?.path?.split("/")[1];
if (path?.startsWith("areas-")) {
Expand All @@ -151,7 +146,7 @@ class PanelHome extends LitElement {
}
}
this._setLovelace();
};
}

private _updateExtraActionItems() {
const path = this.route?.path?.split("/")[1];
Expand Down Expand Up @@ -312,24 +307,22 @@ class PanelHome extends LitElement {
});
}

private async _setLovelace() {
const strategyConfig: LovelaceDashboardStrategyConfig = {
private get _strategyConfig(): LovelaceDashboardStrategyConfig {
return {
strategy: {
type: "home",
favorite_entities: this._config.favorite_entities,
home_panel: true,
},
};
}

const config = await expandLovelaceConfigStrategies(
strategyConfig,
private async _setLovelace() {
const config = await generateLovelaceDashboardStrategy(
this._strategyConfig,
this.hass
);

if (deepEqual(config, this._lovelace?.config)) {
return;
}

this._lovelace = {
config: config,
rawConfig: config,
Expand Down
68 changes: 14 additions & 54 deletions src/panels/lovelace/ha-panel-lovelace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
removeSearchParam,
} from "../../common/url/search-params";
import { debounce } from "../../common/util/debounce";
import { deepEqual } from "../../common/util/deep-equal";
import "../../components/ha-button";
import { domainToName } from "../../data/integration";
import { subscribeLovelaceUpdates } from "../../data/lovelace";
Expand All @@ -36,7 +35,10 @@ import { checkLovelaceConfig } from "./common/check-lovelace-config";
import { loadLovelaceResources } from "./common/load-resources";
import { showSaveDialog } from "./editor/show-save-config-dialog";
import "./hui-root";
import { generateLovelaceDashboardStrategy } from "./strategies/get-strategy";
import {
checkStrategyShouldRegenerate,
generateLovelaceDashboardStrategy,
} from "./strategies/get-strategy";
import type { Lovelace } from "./types";
import { generateDefaultView } from "./views/default-view";
import { fetchDashboards } from "../../data/lovelace/dashboard";
Expand All @@ -50,12 +52,6 @@ interface LovelacePanelConfig {
let editorLoaded = false;
let resourcesLoaded = false;

declare global {
interface HASSDomEvents {
"strategy-config-changed": undefined;
}
}

@customElement("ha-panel-lovelace")
export class LovelacePanel extends LitElement {
@property({ attribute: false }) public panel?: PanelInfo<
Expand Down Expand Up @@ -129,7 +125,6 @@ export class LovelacePanel extends LitElement {
.route=${this.route}
.narrow=${this.narrow}
@config-refresh=${this._forceFetchConfig}
@strategy-config-changed=${this._strategyConfigChanged}
></hui-root>
`;
}
Expand Down Expand Up @@ -195,61 +190,26 @@ export class LovelacePanel extends LitElement {
this.lovelace &&
isStrategyDashboard(this.lovelace.rawConfig)
) {
// If the entity registry changed, ask the user if they want to refresh the config
if (
oldHass.entities !== this.hass.entities ||
oldHass.devices !== this.hass.devices ||
oldHass.areas !== this.hass.areas ||
oldHass.floors !== this.hass.floors
) {
if (this.hass.config.state === "RUNNING") {
this._debounceRegistriesChanged();
}
}
// If ha started, refresh the config
if (
this.hass.config.state === "RUNNING" &&
oldHass.config.state !== "RUNNING"
(oldHass.config.state !== "RUNNING" ||
checkStrategyShouldRegenerate(
"dashboard",
this.lovelace.rawConfig.strategy,
oldHass,
this.hass
))
) {
this._regenerateStrategyConfig();
this._debounceRegenerateStrategy();
}
}
}

private _debounceRegistriesChanged = debounce(
() => this._registriesChanged(),
private _debounceRegenerateStrategy = debounce(
() => this._regenerateStrategyConfig(),
200
);

private _registriesChanged = async () => {
if (!this.hass || !this.lovelace) {
return;
}
const rawConfig = this.lovelace.rawConfig;

if (!isStrategyDashboard(rawConfig)) {
return;
}

const oldConfig = this.lovelace.config;
const generatedConfig = await generateLovelaceDashboardStrategy(
rawConfig,
this.hass!
);

const newConfig = checkLovelaceConfig(generatedConfig) as LovelaceConfig;

// Regenerate if the config changed
if (!deepEqual(newConfig, oldConfig)) {
this._regenerateStrategyConfig();
}
};

private _strategyConfigChanged = (ev: CustomEvent) => {
ev.stopPropagation();
this._regenerateStrategyConfig();
};

private async _regenerateStrategyConfig() {
if (!this.hass || !this.lovelace) {
return;
Expand Down
33 changes: 32 additions & 1 deletion src/panels/lovelace/sections/hui-section.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { customElement, property, state } from "lit/decorators";
import { storage } from "../../../common/decorators/storage";
import { deepEqual } from "../../../common/util/deep-equal";
import { fireEvent } from "../../../common/dom/fire_event";
import { debounce } from "../../../common/util/debounce";
import "../../../components/ha-svg-icon";
import type { LovelaceSectionElement } from "../../../data/lovelace";
import type { LovelaceCardConfig } from "../../../data/lovelace/config/card";
Expand All @@ -24,7 +25,10 @@ import { showEditCardDialog } from "../editor/card-editor/show-edit-card-dialog"
import { addCard, replaceCard } from "../editor/config-util";
import { performDeleteCard } from "../editor/delete-card";
import { parseLovelaceCardPath } from "../editor/lovelace-path";
import { generateLovelaceSectionStrategy } from "../strategies/get-strategy";
import {
checkStrategyShouldRegenerate,
generateLovelaceSectionStrategy,
} from "../strategies/get-strategy";
import type { Lovelace } from "../types";
import { DEFAULT_SECTION_LAYOUT } from "./const";

Expand Down Expand Up @@ -105,9 +109,36 @@ export class HuiSection extends ConditionalListenerMixin<LovelaceSectionConfig>(
(!oldConfig || this.config !== oldConfig)
) {
this._initializeConfig();
return;
}

if (!changedProperties.has("hass")) {
return;
}

const oldHass = changedProperties.get("hass") as HomeAssistant | undefined;
if (
oldHass &&
this.hass &&
isStrategySection(this.config) &&
this.hass.config.state === "RUNNING" &&
(oldHass.config.state !== "RUNNING" ||
checkStrategyShouldRegenerate(
"section",
this.config.strategy,
oldHass,
this.hass
))
) {
this._debounceRefreshConfig();
}
}

private _debounceRefreshConfig = debounce(
() => this._initializeConfig(),
200
);

public disconnectedCallback() {
super.disconnectedCallback();
}
Expand Down
2 changes: 2 additions & 0 deletions src/panels/lovelace/strategies/areas/area-view-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ const computeHeadingCard = (

@customElement("area-view-strategy")
export class AreaViewStrategy extends ReactiveElement {
static registryDependencies = ["entities", "devices", "areas"];

static async generate(
config: AreaViewStrategyConfig,
hass: HomeAssistant
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export interface AreasDashboardStrategyConfig {

@customElement("areas-dashboard-strategy")
export class AreasDashboardStrategy extends ReactiveElement {
static registryDependencies = ["areas"];

static async generate(
config: AreasDashboardStrategyConfig,
hass: HomeAssistant
Expand Down
Loading
Loading