Skip to content

Ability to show map icons for geolocation sources - #23247

Merged
bramkragten merged 5 commits into
home-assistant:devfrom
karwosts:map-geolocation-icons
Dec 21, 2024
Merged

Ability to show map icons for geolocation sources#23247
bramkragten merged 5 commits into
home-assistant:devfrom
karwosts:map-geolocation-icons

Conversation

@karwosts

Copy link
Copy Markdown
Member

Proposed change

Add a mode to show geolocation entities on the map as icons instead of their current name. The current behavior seems to just produce an unintelligible letter-salad, so I think seeing the icons would be quite a bit cleaner. Perhaps it should even be made the default mode, but for now it's just backward-compatible optional mode. Most geolocation sources I've seen tend to include a nice icon as part of the entity (typically for natural disasters).

Current:
image

Proposed:
image

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New feature (thank you!)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Example configuration

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue or discussion:
  • Link to documentation pull request:

Checklist

  • The code change is tested and works locally.
  • There is no commented out code in this PR.
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

@MindFreeze MindFreeze left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be part of label_mode and label_mode should just override the entity picture

@karwosts

Copy link
Copy Markdown
Member Author

Unified the option with label_mode to make it constant, I think everything works but icon mode is only really good for geo_locations as the icon is pulled from the entity attributes instead of the registry.

I didn't add entity-registry based icon support for non-geolocation entities at this time as I'm not sure if it's really a useful usecase, I think it's more practical for autogenerated entities than the standard ones.

@MindFreeze

MindFreeze commented Dec 18, 2024

Copy link
Copy Markdown
Member

I didn't add entity-registry based icon support for non-geolocation entities at this time as I'm not sure if it's really a useful usecase, I think it's more practical for autogenerated entities than the standard ones.

I think this should support the entity registry. Users expect consistency and can assign icons to entities like cars or people that are displayed on maps. Documentation would also be more complicated if this isn't supported

Comment thread src/components/map/ha-entity-marker.ts Outdated
></div>`
: this.entityName}
: this.entityIcon
? html`<ha-icon .icon=${this.entityIcon}></ha-icon>`

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cant we just use ha-state-icon? That will handle it all.

@karwosts karwosts Dec 20, 2024

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kinda tried this once but didn't get it to work.

ha-state-icon needs hass, and I think the way the marker is constructed in ha-map and the html template is passed to map.addLayer() makes that not work for some reason (currently everything to ha-entity-marker is passed as an attribute instead of a property?), when I tried to add a hass property to the marker it always ended up being undefined when that item rendered.

Probably need some advanced JS I don't understand to make that work correctly?

If you think that should work without anything special I can try again and see if I missed something obvious...

@bramkragten bramkragten Dec 20, 2024

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should kinda work, it will not update hass when it changes though (except if the icon is recreated when hass changes ofc), but that could also be added if needed:

      const entityMarker = document.createElement("ha-entity-marker");
      entityMarker.hass = this.hass;
      entityMarker.entityId = getEntityId(entity);
      entityMarker.entityName = entityName;
      entityMarker.entityPicture = entityPicture
        ? this.hass.hassUrl(entityPicture)
        : "";
      if (typeof entity !== "string") {
        entityMarker.entityColor = entity.color;
      }

      // create marker with the icon
      const marker = Leaflet.marker([latitude, longitude], {
        icon: Leaflet.divIcon({
          html: entityMarker,
          iconSize: [48, 48],
          className: "",
        }),
        title: title,
      });

Another option would be to extend Leaflet.divIcon with lit-html support, so we can write lit-html templates in the html option.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this suggestion seems to work well.

Now have working icons for all entities 👍 (even updates correctly when the state changes)

@bramkragten
bramkragten merged commit 4de8b56 into home-assistant:dev Dec 21, 2024
@karwosts
karwosts deleted the map-geolocation-icons branch December 21, 2024 15:46
@github-actions github-actions Bot locked and limited conversation to collaborators Dec 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants