Skip to content

Commit

Permalink
Preserve map entities object
Browse files Browse the repository at this point in the history
  • Loading branch information
amitfin committed Nov 20, 2023
1 parent 0099289 commit a4b1e1e
Showing 1 changed file with 42 additions and 38 deletions.
80 changes: 42 additions & 38 deletions src/panels/lovelace/cards/hui-map-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ class HuiMapCard extends LitElement implements LovelaceCard {

private _configEntities?: MapEntityConfig[];

private _mapEntities: HaMapEntity[] = [];

private _colorDict: Record<string, string> = {};

private _colorIndex = 0;
Expand Down Expand Up @@ -157,11 +159,7 @@ class HuiMapCard extends LitElement implements LovelaceCard {
<div id="root">
<ha-map
.hass=${this.hass}
.entities=${this._getEntities(
this.hass.states,
this._config,
this._configEntities
)}
.entities=${this._getMapEntities()}
.zoom=${this._config.default_zoom ?? DEFAULT_ZOOM}
.paths=${this._getHistoryPaths(this._config, this._stateHistory)}
.autoFit=${this._config.auto_fit || false}
Expand All @@ -183,20 +181,23 @@ class HuiMapCard extends LitElement implements LovelaceCard {
}

protected shouldUpdate(changedProps: PropertyValues) {
// Update if anything other than "hass" has changed.
if (!changedProps.has("hass") || changedProps.size > 1) {
return true;
}

const oldHass = changedProps.get("hass") as HomeAssistant | undefined;
if (!oldHass) {
if (!oldHass || !this._configEntities) {
return true;
}

if (oldHass.themes.darkMode !== this.hass.themes.darkMode) {
return true;
}

if (changedProps.has("_stateHistory")) {
return true;
}

if (this._config?.geo_location_sources) {
const oldSourceEntities = this._getSourceEntities(
oldHass.states,
Expand Down Expand Up @@ -320,47 +321,50 @@ class HuiMapCard extends LitElement implements LovelaceCard {
return color;
}

private _getSourceEntities = memoizeOne(
(states: HassEntities, config?: MapCardConfig): string[] => {
if (!states || !config || !config.geo_location_sources) {
return [];
}
private _getSourceEntities(
states: HassEntities,
config?: MapCardConfig
): string[] {
if (!states || !config || !config.geo_location_sources) {
return [];
}

const entities: string[] = [];
// Calculate visible geo location sources
const includesAll = config.geo_location_sources.includes("all");
for (const stateObj of Object.values(states)) {
if (
computeDomain(stateObj.entity_id) === "geo_location" &&
(includesAll ||
config.geo_location_sources.includes(stateObj.attributes.source))
) {
entities.push(stateObj.entity_id);
}
const geoEntities: string[] = [];
// Calculate visible geo location sources
const includesAll = config.geo_location_sources.includes("all");
for (const stateObj of Object.values(states)) {
if (
computeDomain(stateObj.entity_id) === "geo_location" &&
(includesAll ||
config.geo_location_sources.includes(stateObj.attributes.source))
) {
geoEntities.push(stateObj.entity_id);
}
return entities;
}
);
return geoEntities;
}

private _getEntities = memoizeOne(
(
states: HassEntities,
config?: MapCardConfig,
configEntities?: MapEntityConfig[]
): HaMapEntity[] => [
...(configEntities || []).map((entityConf) => ({
private _getMapEntities(): HaMapEntity[] {
const entities = [
...(this._configEntities || []).map((entityConf) => ({
entity_id: entityConf.entity,
color: this._getColor(entityConf.entity),
label_mode: entityConf.label_mode,
focus: entityConf.focus,
name: entityConf.name,
})),
...this._getSourceEntities(states, config).map((entity) => ({
entity_id: entity,
color: this._getColor(entity),
})),
]
);
...this._getSourceEntities(this.hass.states, this._config).map(
(entity) => ({
entity_id: entity,
color: this._getColor(entity),
})
),
];
if (JSON.stringify(entities) !== JSON.stringify(this._mapEntities)) {
this._mapEntities = entities;
}
return this._mapEntities;
}

private _getHistoryPaths = memoizeOne(
(
Expand Down

0 comments on commit a4b1e1e

Please sign in to comment.