From 4f2027051b3e4fcbee8a978583575e13a96475c6 Mon Sep 17 00:00:00 2001 From: karwosts Date: Fri, 25 Oct 2024 14:23:35 -0700 Subject: [PATCH] Add a geo_location selector to map editor --- .../config-elements/hui-map-card-editor.ts | 58 +++++++++++++++++-- src/translations/en.json | 1 + 2 files changed, 53 insertions(+), 6 deletions(-) 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..8b8186d0f8c3 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 @@ -15,15 +15,17 @@ import { import memoizeOne from "memoize-one"; import { fireEvent } from "../../../../common/dom/fire_event"; import { hasLocation } from "../../../../common/entity/has_location"; +import { computeDomain } from "../../../../common/entity/compute_domain"; import "../../../../components/ha-form/ha-form"; import { SchemaUnion } from "../../../../components/ha-form/types"; +import type { SelectSelector } from "../../../../data/selector"; import "../../../../components/ha-formfield"; import "../../../../components/ha-switch"; +import "../../../../components/ha-selector/ha-selector-select"; import { HomeAssistant, ValueChangedEvent } from "../../../../types"; import { DEFAULT_HOURS_TO_SHOW, DEFAULT_ZOOM } from "../../cards/hui-map-card"; import { MapCardConfig } from "../../cards/types"; import "../../components/hui-entity-editor"; -import "../../components/hui-input-list-editor"; import { EntityConfig } from "../../entity-rows/types"; import { LovelaceCardEditor } from "../../types"; import { processEditorEntities } from "../process-editor-entities"; @@ -67,6 +69,8 @@ export class HuiMapCardEditor extends LitElement implements LovelaceCardEditor { @state() private _configEntities?: EntityConfig[]; + @state() private _possibleGeoSources?: { value: string; label?: string }[]; + private _schema = memoizeOne( (localize: LocalizeFunc) => [ @@ -166,17 +170,40 @@ export class HuiMapCardEditor extends LitElement implements LovelaceCardEditor { )} - + .selector=${this._selectSchema( + this._possibleGeoSources, + this.hass.localize + )} + > `; } + private _selectSchema = memoizeOne( + (options, localize: LocalizeFunc): SelectSelector => ({ + select: { + sort: true, + multiple: true, + custom_value: true, + options: options.length + ? options + : [ + { + value: "", + label: localize( + "ui.panel.lovelace.editor.card.map.no_geo_location_sources" + ), + }, + ], + }, + }) + ); + private _entitiesValueChanged(ev: EntitiesEditorEvent): void { if (ev.detail && ev.detail.entities) { this._config = { ...this._config!, entities: ev.detail.entities }; @@ -213,6 +240,25 @@ export class HuiMapCardEditor extends LitElement implements LovelaceCardEditor { fireEvent(this, "config-changed", { config: ev.detail.value }); } + protected willUpdate() { + if (this.hass && !this._possibleGeoSources) { + const sources: Record = {}; + Object.entries(this.hass.states).forEach(([entity_id, stateObj]) => { + const domain = computeDomain(entity_id); + if (domain === "geo_location" && stateObj.attributes.source) { + sources[stateObj.attributes.source] = stateObj.attributes.attribution; + } + }); + + this._possibleGeoSources = Object.entries(sources).map( + ([source, attribution]) => ({ + value: source, + label: attribution || source, + }) + ); + } + } + private _computeLabelCallback = ( schema: SchemaUnion> ) => { diff --git a/src/translations/en.json b/src/translations/en.json index 44dd8e733f3d..2c92372dceeb 100644 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -6067,6 +6067,7 @@ "map": { "name": "Map", "geo_location_sources": "Geolocation sources", + "no_geo_location_sources": "No geolocation sources available", "dark_mode": "Dark mode?", "appearance": "Appearance", "theme_mode": "Theme Mode",