From 6e59568ba36ac4f432e4c27765de30e24afad977 Mon Sep 17 00:00:00 2001 From: Cody C <50791984+codyc1515@users.noreply.github.com> Date: Fri, 26 Jan 2024 01:05:07 +1300 Subject: [PATCH] Use feed name as entity name in GeoJSON (#108753) * Add support for entity name in GeoJSON Previously GeoJSON names were just the config entry ID. This is not very user friendly. Particularly so when there are many config entries and many, many entities from those same many config entries. * Update GeoJSON tests to support entity names --- .../components/geo_json_events/geo_location.py | 5 ++++- tests/components/geo_json_events/__init__.py | 3 +++ .../geo_json_events/test_geo_location.py | 15 +++++++++++---- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/geo_json_events/geo_location.py b/homeassistant/components/geo_json_events/geo_location.py index 8cb30535e66..8915962c4ff 100644 --- a/homeassistant/components/geo_json_events/geo_location.py +++ b/homeassistant/components/geo_json_events/geo_location.py @@ -104,7 +104,10 @@ class GeoJsonLocationEvent(GeolocationEvent): def _update_from_feed(self, feed_entry: GenericFeedEntry) -> None: """Update the internal state from the provided feed entry.""" - self._attr_name = feed_entry.title + if feed_entry.properties and "name" in feed_entry.properties: + self._attr_name = feed_entry.properties.get("name") + else: + self._attr_name = feed_entry.title self._attr_distance = feed_entry.distance_to_home self._attr_latitude = feed_entry.coordinates[0] self._attr_longitude = feed_entry.coordinates[1] diff --git a/tests/components/geo_json_events/__init__.py b/tests/components/geo_json_events/__init__.py index f95ee747bf3..7d7148b3c20 100644 --- a/tests/components/geo_json_events/__init__.py +++ b/tests/components/geo_json_events/__init__.py @@ -1,4 +1,5 @@ """Tests for the geo_json_events component.""" +from typing import Any from unittest.mock import MagicMock @@ -7,6 +8,7 @@ def _generate_mock_feed_entry( title: str, distance_to_home: float, coordinates: tuple[float, float], + properties: dict[str, Any] | None = None, ) -> MagicMock: """Construct a mock feed entry for testing purposes.""" feed_entry = MagicMock() @@ -14,4 +16,5 @@ def _generate_mock_feed_entry( feed_entry.title = title feed_entry.distance_to_home = distance_to_home feed_entry.coordinates = coordinates + feed_entry.properties = properties return feed_entry diff --git a/tests/components/geo_json_events/test_geo_location.py b/tests/components/geo_json_events/test_geo_location.py index 3875a525e73..3176a37ab74 100644 --- a/tests/components/geo_json_events/test_geo_location.py +++ b/tests/components/geo_json_events/test_geo_location.py @@ -16,6 +16,7 @@ from homeassistant.const import ( ATTR_FRIENDLY_NAME, ATTR_LATITUDE, ATTR_LONGITUDE, + ATTR_NAME, ATTR_UNIT_OF_MEASUREMENT, CONF_RADIUS, CONF_SCAN_INTERVAL, @@ -50,7 +51,13 @@ async def test_entity_lifecycle( """Test entity lifecycle..""" config_entry.add_to_hass(hass) # Set up a mock feed entries for this test. - mock_entry_1 = _generate_mock_feed_entry("1234", "Title 1", 15.5, (-31.0, 150.0)) + mock_entry_1 = _generate_mock_feed_entry( + "1234", + "Title 1", + 15.5, + (-31.0, 150.0), + {ATTR_NAME: "Properties 1"}, + ) mock_entry_2 = _generate_mock_feed_entry("2345", "Title 2", 20.5, (-31.1, 150.1)) mock_entry_3 = _generate_mock_feed_entry("3456", "Title 3", 25.5, (-31.2, 150.2)) mock_entry_4 = _generate_mock_feed_entry("4567", "Title 4", 12.5, (-31.3, 150.3)) @@ -69,14 +76,14 @@ async def test_entity_lifecycle( assert len(hass.states.async_entity_ids(GEO_LOCATION_DOMAIN)) == 3 assert len(entity_registry.entities) == 3 - state = hass.states.get(f"{GEO_LOCATION_DOMAIN}.title_1") + state = hass.states.get(f"{GEO_LOCATION_DOMAIN}.properties_1") assert state is not None - assert state.name == "Title 1" + assert state.name == "Properties 1" assert state.attributes == { ATTR_EXTERNAL_ID: "1234", ATTR_LATITUDE: -31.0, ATTR_LONGITUDE: 150.0, - ATTR_FRIENDLY_NAME: "Title 1", + ATTR_FRIENDLY_NAME: "Properties 1", ATTR_UNIT_OF_MEASUREMENT: UnitOfLength.KILOMETERS, ATTR_SOURCE: "geo_json_events", }