diff --git a/src/panels/lovelace/cards/hui-map-card.ts b/src/panels/lovelace/cards/hui-map-card.ts index ed4feb2cc4..c3076985f4 100644 --- a/src/panels/lovelace/cards/hui-map-card.ts +++ b/src/panels/lovelace/cards/hui-map-card.ts @@ -20,6 +20,7 @@ import { formatTimeWeekday, } from "../../../common/datetime/format_time"; import { computeDomain } from "../../../common/entity/compute_domain"; +import { deepEqual } from "../../../common/util/deep-equal"; import parseAspectRatio from "../../../common/util/parse-aspect-ratio"; import "../../../components/ha-card"; import "../../../components/ha-alert"; @@ -27,6 +28,7 @@ import "../../../components/ha-icon-button"; import "../../../components/map/ha-map"; import type { HaMap, + HaMapEntity, HaMapPathPoint, HaMapPaths, } from "../../../components/map/ha-map"; @@ -70,6 +72,8 @@ class HuiMapCard extends LitElement implements LovelaceCard { private _configEntities?: MapEntityConfig[]; + @state() private _mapEntities: HaMapEntity[] = []; + private _colorDict: Record = {}; private _colorIndex = 0; @@ -102,6 +106,7 @@ class HuiMapCard extends LitElement implements LovelaceCard { this._configEntities = config.entities ? processConfigEntities(config.entities) : []; + this._mapEntities = this._getMapEntities(); } public getCardSize(): number { @@ -156,14 +161,10 @@ class HuiMapCard extends LitElement implements LovelaceCard {
{ - if (!states || !config) { - return undefined; - } - - const geoEntities: string[] = []; - if (config.geo_location_sources) { - // 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 [ - ...(configEntities || []).map((entityConf) => ({ - entity_id: entityConf.entity, - color: this._getColor(entityConf.entity), - label_mode: entityConf.label_mode, - focus: entityConf.focus, - name: entityConf.name, - })), - ...geoEntities.map((entity) => ({ - entity_id: entity, - color: this._getColor(entity), - })), - ]; + private _getSourceEntities(states?: HassEntities): string[] { + if (!states || !this._config?.geo_location_sources) { + return []; } - ); + + const geoEntities: string[] = []; + // Calculate visible geo location sources + const includesAll = this._config.geo_location_sources.includes("all"); + for (const stateObj of Object.values(states)) { + if ( + computeDomain(stateObj.entity_id) === "geo_location" && + (includesAll || + this._config.geo_location_sources.includes( + stateObj.attributes.source + )) + ) { + geoEntities.push(stateObj.entity_id); + } + } + return geoEntities; + } + + private _getMapEntities(): HaMapEntity[] { + return [ + ...(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(this.hass?.states).map((entity) => ({ + entity_id: entity, + color: this._getColor(entity), + })), + ]; + } private _getHistoryPaths = memoizeOne( (