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
This commit is contained in:
parent
6f81d21a35
commit
6e59568ba3
3 changed files with 18 additions and 5 deletions
|
@ -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]
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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",
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue