Ability to show map icons for geolocation sources (#23247)

This commit is contained in:
karwosts 2024-12-21 07:29:53 -08:00 committed by GitHub
parent 31e85836f0
commit 4de8b562bd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 33 additions and 18 deletions

View File

@ -1,9 +1,13 @@
import { LitElement, html, css } from "lit"; import { LitElement, html, css } from "lit";
import { property } from "lit/decorators"; import { property } from "lit/decorators";
import { styleMap } from "lit/directives/style-map"; import { styleMap } from "lit/directives/style-map";
import type { HomeAssistant } from "../../types";
import { fireEvent } from "../../common/dom/fire_event"; import { fireEvent } from "../../common/dom/fire_event";
import "../ha-state-icon";
class HaEntityMarker extends LitElement { class HaEntityMarker extends LitElement {
@property({ attribute: false }) public hass!: HomeAssistant;
@property({ attribute: "entity-id" }) public entityId?: string; @property({ attribute: "entity-id" }) public entityId?: string;
@property({ attribute: "entity-name" }) public entityName?: string; @property({ attribute: "entity-name" }) public entityName?: string;
@ -12,6 +16,8 @@ class HaEntityMarker extends LitElement {
@property({ attribute: "entity-color" }) public entityColor?: string; @property({ attribute: "entity-color" }) public entityColor?: string;
@property({ attribute: "show-icon", type: Boolean }) public showIcon = false;
protected render() { protected render() {
return html` return html`
<div <div
@ -26,6 +32,11 @@ class HaEntityMarker extends LitElement {
"background-image": `url(${this.entityPicture})`, "background-image": `url(${this.entityPicture})`,
})} })}
></div>` ></div>`
: this.showIcon && this.entityId
? html`<ha-state-icon
.hass=${this.hass}
.stateObj=${this.hass?.states[this.entityId]}
></ha-state-icon>`
: this.entityName} : this.entityName}
</div> </div>
`; `;

View File

@ -52,7 +52,7 @@ export interface HaMapPaths {
export interface HaMapEntity { export interface HaMapEntity {
entity_id: string; entity_id: string;
color: string; color: string;
label_mode?: "name" | "state"; label_mode?: "name" | "state" | "icon";
name?: string; name?: string;
focus?: boolean; focus?: boolean;
} }
@ -523,23 +523,24 @@ export class HaMap extends ReactiveElement {
.join("") .join("")
.substr(0, 3)); .substr(0, 3));
const entityMarker = document.createElement("ha-entity-marker");
entityMarker.hass = this.hass;
entityMarker.showIcon =
typeof entity !== "string" && entity.label_mode === "icon";
entityMarker.entityId = getEntityId(entity);
entityMarker.entityName = entityName;
entityMarker.entityPicture =
entityPicture && (typeof entity === "string" || !entity.label_mode)
? this.hass.hassUrl(entityPicture)
: "";
if (typeof entity !== "string") {
entityMarker.entityColor = entity.color;
}
// create marker with the icon // create marker with the icon
const marker = Leaflet.marker([latitude, longitude], { const marker = Leaflet.marker([latitude, longitude], {
icon: Leaflet.divIcon({ icon: Leaflet.divIcon({
html: ` html: entityMarker,
<ha-entity-marker
entity-id="${getEntityId(entity)}"
entity-name="${entityName}"
entity-picture="${
entityPicture ? this.hass.hassUrl(entityPicture) : ""
}"
${
typeof entity !== "string"
? `entity-color="${entity.color}"`
: ""
}
></ha-entity-marker>
`,
iconSize: [48, 48], iconSize: [48, 48],
className: "", className: "",
}), }),

View File

@ -44,6 +44,7 @@ interface MapEntityConfig extends EntityConfig {
interface GeoEntity { interface GeoEntity {
entity_id: string; entity_id: string;
label_mode?: "state" | "name" | "icon";
focus: boolean; focus: boolean;
} }
@ -351,6 +352,7 @@ class HuiMapCard extends LitElement implements LovelaceCard {
) { ) {
geoEntities.push({ geoEntities.push({
entity_id: stateObj.entity_id, entity_id: stateObj.entity_id,
label_mode: sourceObj?.label_mode ?? allSource?.label_mode,
focus: sourceObj focus: sourceObj
? (sourceObj.focus ?? true) ? (sourceObj.focus ?? true)
: (allSource?.focus ?? true), : (allSource?.focus ?? true),
@ -370,8 +372,7 @@ class HuiMapCard extends LitElement implements LovelaceCard {
name: entityConf.name, name: entityConf.name,
})), })),
...this._getSourceEntities(this.hass?.states).map((entity) => ({ ...this._getSourceEntities(this.hass?.states).map((entity) => ({
entity_id: entity.entity_id, ...entity,
focus: entity.focus,
color: this._getColor(entity.entity_id), color: this._getColor(entity.entity_id),
})), })),
]; ];

View File

@ -309,6 +309,7 @@ export interface LogbookCardConfig extends LovelaceCardConfig {
interface GeoLocationSourceConfig { interface GeoLocationSourceConfig {
source: string; source: string;
label_mode?: "name" | "state" | "icon";
focus?: boolean; focus?: boolean;
} }

View File

@ -48,6 +48,7 @@ export const mapEntitiesConfigStruct = union([
const geoSourcesConfigStruct = union([ const geoSourcesConfigStruct = union([
object({ object({
source: string(), source: string(),
label_mode: optional(string()),
focus: optional(boolean()), focus: optional(boolean()),
}), }),
string(), string(),