Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'focus' option to geo_location_sources for map card #22535

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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
35 changes: 25 additions & 10 deletions src/panels/lovelace/cards/hui-map-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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),
})),
];
}
Expand Down
7 changes: 6 additions & 1 deletion src/panels/lovelace/cards/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -307,7 +312,7 @@ export interface MapCardConfig extends LovelaceCardConfig {
default_zoom?: number;
entities?: Array<EntityConfig | string>;
hours_to_show?: number;
geo_location_sources?: string[];
geo_location_sources?: Array<GeoLocationSourceConfig | string>;
dark_mode?: boolean;
theme_mode?: ThemeMode;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Loading