Use tracked entity friendly name for proximity sensors (#109744)

user tracked entity friendly name
pull/109757/head
Michael 2024-02-05 20:18:59 +01:00 committed by GitHub
parent 189f3dacfb
commit 45f44e9216
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 6 deletions

View File

@ -69,6 +69,7 @@ class TrackedEntityDescriptor(NamedTuple):
entity_id: str
identifier: str
name: str
def _device_info(coordinator: ProximityDataUpdateCoordinator) -> DeviceInfo:
@ -95,13 +96,24 @@ async def async_setup_entry(
entity_reg = er.async_get(hass)
for tracked_entity_id in coordinator.tracked_entities:
tracked_entity_object_id = tracked_entity_id.split(".")[-1]
if (entity_entry := entity_reg.async_get(tracked_entity_id)) is not None:
tracked_entity_descriptors.append(
TrackedEntityDescriptor(tracked_entity_id, entity_entry.id)
TrackedEntityDescriptor(
tracked_entity_id,
entity_entry.id,
entity_entry.name
or entity_entry.original_name
or tracked_entity_object_id,
)
)
else:
tracked_entity_descriptors.append(
TrackedEntityDescriptor(tracked_entity_id, tracked_entity_id)
TrackedEntityDescriptor(
tracked_entity_id,
tracked_entity_id,
tracked_entity_object_id,
)
)
entities += [
@ -165,7 +177,7 @@ class ProximityTrackedEntitySensor(
self._attr_unique_id = f"{coordinator.config_entry.entry_id}_{tracked_entity_descriptor.identifier}_{description.key}"
self._attr_device_info = _device_info(coordinator)
self._attr_translation_placeholders = {
"tracked_entity": self.tracked_entity_id.split(".")[-1]
"tracked_entity": tracked_entity_descriptor.name
}
async def async_added_to_hass(self) -> None:

View File

@ -11,7 +11,12 @@ from homeassistant.components.proximity.const import (
DOMAIN,
)
from homeassistant.components.script import scripts_with_entity
from homeassistant.const import CONF_ZONE, STATE_UNAVAILABLE, STATE_UNKNOWN
from homeassistant.const import (
ATTR_FRIENDLY_NAME,
CONF_ZONE,
STATE_UNAVAILABLE,
STATE_UNKNOWN,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
import homeassistant.helpers.issue_registry as ir
@ -1205,7 +1210,7 @@ async def test_sensor_unique_ids(
) -> None:
"""Test that when tracked entity is renamed."""
t1 = entity_registry.async_get_or_create(
"device_tracker", "device_tracker", "test1"
"device_tracker", "device_tracker", "test1", original_name="Test tracker 1"
)
hass.states.async_set(t1.entity_id, "not_home")
@ -1227,10 +1232,12 @@ async def test_sensor_unique_ids(
assert await hass.config_entries.async_setup(mock_config.entry_id)
await hass.async_block_till_done()
sensor_t1 = f"sensor.home_{t1.entity_id.split('.')[-1]}_distance"
sensor_t1 = "sensor.home_test_tracker_1_distance"
entity = entity_registry.async_get(sensor_t1)
assert entity
assert entity.unique_id == f"{mock_config.entry_id}_{t1.id}_dist_to_zone"
state = hass.states.get(sensor_t1)
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "home Test tracker 1 Distance"
entity = entity_registry.async_get("sensor.home_test2_distance")
assert entity