Skip to content

Commit 4de8b56

Browse files
authored
Ability to show map icons for geolocation sources (#23247)
1 parent 31e8583 commit 4de8b56

File tree

5 files changed

+33
-18
lines changed

5 files changed

+33
-18
lines changed

src/components/map/ha-entity-marker.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import { LitElement, html, css } from "lit";
22
import { property } from "lit/decorators";
33
import { styleMap } from "lit/directives/style-map";
4+
import type { HomeAssistant } from "../../types";
45
import { fireEvent } from "../../common/dom/fire_event";
6+
import "../ha-state-icon";
57

68
class HaEntityMarker extends LitElement {
9+
@property({ attribute: false }) public hass!: HomeAssistant;
10+
711
@property({ attribute: "entity-id" }) public entityId?: string;
812

913
@property({ attribute: "entity-name" }) public entityName?: string;
@@ -12,6 +16,8 @@ class HaEntityMarker extends LitElement {
1216

1317
@property({ attribute: "entity-color" }) public entityColor?: string;
1418

19+
@property({ attribute: "show-icon", type: Boolean }) public showIcon = false;
20+
1521
protected render() {
1622
return html`
1723
<div
@@ -26,7 +32,12 @@ class HaEntityMarker extends LitElement {
2632
"background-image": `url(${this.entityPicture})`,
2733
})}
2834
></div>`
29-
: this.entityName}
35+
: this.showIcon && this.entityId
36+
? html`<ha-state-icon
37+
.hass=${this.hass}
38+
.stateObj=${this.hass?.states[this.entityId]}
39+
></ha-state-icon>`
40+
: this.entityName}
3041
</div>
3142
`;
3243
}

src/components/map/ha-map.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export interface HaMapPaths {
5252
export interface HaMapEntity {
5353
entity_id: string;
5454
color: string;
55-
label_mode?: "name" | "state";
55+
label_mode?: "name" | "state" | "icon";
5656
name?: string;
5757
focus?: boolean;
5858
}
@@ -523,23 +523,24 @@ export class HaMap extends ReactiveElement {
523523
.join("")
524524
.substr(0, 3));
525525

526+
const entityMarker = document.createElement("ha-entity-marker");
527+
entityMarker.hass = this.hass;
528+
entityMarker.showIcon =
529+
typeof entity !== "string" && entity.label_mode === "icon";
530+
entityMarker.entityId = getEntityId(entity);
531+
entityMarker.entityName = entityName;
532+
entityMarker.entityPicture =
533+
entityPicture && (typeof entity === "string" || !entity.label_mode)
534+
? this.hass.hassUrl(entityPicture)
535+
: "";
536+
if (typeof entity !== "string") {
537+
entityMarker.entityColor = entity.color;
538+
}
539+
526540
// create marker with the icon
527541
const marker = Leaflet.marker([latitude, longitude], {
528542
icon: Leaflet.divIcon({
529-
html: `
530-
<ha-entity-marker
531-
entity-id="${getEntityId(entity)}"
532-
entity-name="${entityName}"
533-
entity-picture="${
534-
entityPicture ? this.hass.hassUrl(entityPicture) : ""
535-
}"
536-
${
537-
typeof entity !== "string"
538-
? `entity-color="${entity.color}"`
539-
: ""
540-
}
541-
></ha-entity-marker>
542-
`,
543+
html: entityMarker,
543544
iconSize: [48, 48],
544545
className: "",
545546
}),

src/panels/lovelace/cards/hui-map-card.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ interface MapEntityConfig extends EntityConfig {
4444

4545
interface GeoEntity {
4646
entity_id: string;
47+
label_mode?: "state" | "name" | "icon";
4748
focus: boolean;
4849
}
4950

@@ -351,6 +352,7 @@ class HuiMapCard extends LitElement implements LovelaceCard {
351352
) {
352353
geoEntities.push({
353354
entity_id: stateObj.entity_id,
355+
label_mode: sourceObj?.label_mode ?? allSource?.label_mode,
354356
focus: sourceObj
355357
? (sourceObj.focus ?? true)
356358
: (allSource?.focus ?? true),
@@ -370,8 +372,7 @@ class HuiMapCard extends LitElement implements LovelaceCard {
370372
name: entityConf.name,
371373
})),
372374
...this._getSourceEntities(this.hass?.states).map((entity) => ({
373-
entity_id: entity.entity_id,
374-
focus: entity.focus,
375+
...entity,
375376
color: this._getColor(entity.entity_id),
376377
})),
377378
];

src/panels/lovelace/cards/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ export interface LogbookCardConfig extends LovelaceCardConfig {
309309

310310
interface GeoLocationSourceConfig {
311311
source: string;
312+
label_mode?: "name" | "state" | "icon";
312313
focus?: boolean;
313314
}
314315

src/panels/lovelace/editor/config-elements/hui-map-card-editor.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export const mapEntitiesConfigStruct = union([
4848
const geoSourcesConfigStruct = union([
4949
object({
5050
source: string(),
51+
label_mode: optional(string()),
5152
focus: optional(boolean()),
5253
}),
5354
string(),

0 commit comments

Comments
 (0)