From 768bb681d12d14fdefc3f9a93bf20495334ba4c1 Mon Sep 17 00:00:00 2001 From: karwosts Date: Fri, 25 Oct 2024 09:29:40 -0700 Subject: [PATCH] Add 'focus' option to geo_location_sources for map card --- src/panels/lovelace/cards/hui-map-card.ts | 35 +++++++++++++------ src/panels/lovelace/cards/types.ts | 7 +++- .../config-elements/hui-map-card-editor.ts | 2 +- 3 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/panels/lovelace/cards/hui-map-card.ts b/src/panels/lovelace/cards/hui-map-card.ts index ae3bd5279881..c50cb0c38eb8 100644 --- a/src/panels/lovelace/cards/hui-map-card.ts +++ b/src/panels/lovelace/cards/hui-map-card.ts @@ -50,6 +50,11 @@ interface MapEntityConfig extends EntityConfig { focus?: boolean; } +interface GeoEntity { + entity_id: string; + focus: boolean; +} + @customElement("hui-map-card") class HuiMapCard extends LitElement implements LovelaceCard { @property({ attribute: false }) public hass!: HomeAssistant; @@ -332,23 +337,32 @@ class HuiMapCard extends LitElement implements LovelaceCard { return color; } - private _getSourceEntities(states?: HassEntities): string[] { + private _getSourceEntities(states?: HassEntities): GeoEntity[] { if (!states || !this._config?.geo_location_sources) { return []; } - const geoEntities: string[] = []; + const sourceObjs = this._config.geo_location_sources.map((source) => + typeof source === "string" ? { source } : source + ); + + const geoEntities: GeoEntity[] = []; // Calculate visible geo location sources - const includesAll = this._config.geo_location_sources.includes("all"); + const allSource = sourceObjs.find((s) => s.source === "all"); for (const stateObj of Object.values(states)) { + const sourceObj = sourceObjs.find( + (s) => s.source === stateObj.attributes.source + ); if ( computeDomain(stateObj.entity_id) === "geo_location" && - (includesAll || - this._config.geo_location_sources.includes( - stateObj.attributes.source - )) + (allSource || sourceObj) ) { - geoEntities.push(stateObj.entity_id); + geoEntities.push({ + entity_id: stateObj.entity_id, + focus: sourceObj + ? (sourceObj.focus ?? true) + : (allSource?.focus ?? true), + }); } } return geoEntities; @@ -364,8 +378,9 @@ class HuiMapCard extends LitElement implements LovelaceCard { name: entityConf.name, })), ...this._getSourceEntities(this.hass?.states).map((entity) => ({ - entity_id: entity, - color: this._getColor(entity), + entity_id: entity.entity_id, + focus: entity.focus, + color: this._getColor(entity.entity_id), })), ]; } diff --git a/src/panels/lovelace/cards/types.ts b/src/panels/lovelace/cards/types.ts index c14f07cf790d..6ee537db30ba 100644 --- a/src/panels/lovelace/cards/types.ts +++ b/src/panels/lovelace/cards/types.ts @@ -298,6 +298,11 @@ export interface LogbookCardConfig extends LovelaceCardConfig { theme?: string; } +interface GeoLocationSourceConfig { + source: string; + focus?: boolean; +} + export interface MapCardConfig extends LovelaceCardConfig { type: "map"; title?: string; @@ -307,7 +312,7 @@ export interface MapCardConfig extends LovelaceCardConfig { default_zoom?: number; entities?: Array; hours_to_show?: number; - geo_location_sources?: string[]; + geo_location_sources?: Array; dark_mode?: boolean; theme_mode?: ThemeMode; } diff --git a/src/panels/lovelace/editor/config-elements/hui-map-card-editor.ts b/src/panels/lovelace/editor/config-elements/hui-map-card-editor.ts index 7aafbb59006e..8b17532bfda7 100644 --- a/src/panels/lovelace/editor/config-elements/hui-map-card-editor.ts +++ b/src/panels/lovelace/editor/config-elements/hui-map-card-editor.ts @@ -136,7 +136,7 @@ export class HuiMapCardEditor extends LitElement implements LovelaceCardEditor { } get _geo_location_sources(): string[] { - return this._config!.geo_location_sources || []; + return (this._config!.geo_location_sources as string[]) || []; } protected render() {