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 a geo_location selector to map editor #22538

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
58 changes: 52 additions & 6 deletions src/panels/lovelace/editor/config-elements/hui-map-card-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe this component (hui-input-list-editor) is now completely unused.

import { EntityConfig } from "../../entity-rows/types";
import { LovelaceCardEditor } from "../../types";
import { processEditorEntities } from "../process-editor-entities";
Expand Down Expand Up @@ -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) =>
[
Expand Down Expand Up @@ -166,17 +170,40 @@ export class HuiMapCardEditor extends LitElement implements LovelaceCardEditor {
)}
</h3>
<hui-input-list-editor
.inputLabel=${this.hass.localize(
"ui.panel.lovelace.editor.card.map.source"
)}
<ha-selector-select
.label=${this.hass.localize("ui.panel.lovelace.editor.card.map.source")}
.required=${false}
.hass=${this.hass}
.value=${this._geo_location_sources}
@value-changed=${this._geoSourcesChanged}
></hui-input-list-editor>
.selector=${this._selectSchema(
this._possibleGeoSources,
this.hass.localize
)}
></ha-selector-select>
`;
}

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 };
Expand Down Expand Up @@ -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<string, string> = {};
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<ReturnType<typeof this._schema>>
) => {
Expand Down
1 change: 1 addition & 0 deletions src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading