diff --git a/homeassistant/helpers/entity_platform.py b/homeassistant/helpers/entity_platform.py index d101abe2b1a..fb542f660d1 100644 --- a/homeassistant/helpers/entity_platform.py +++ b/homeassistant/helpers/entity_platform.py @@ -325,11 +325,13 @@ class EntityPlatform: entity.platform = None return + requested_entity_id = None suggested_object_id = None # Get entity_id from unique ID registration if entity.unique_id is not None: if entity.entity_id is not None: + requested_entity_id = entity.entity_id suggested_object_id = split_entity_id(entity.entity_id)[1] else: suggested_object_id = entity.name @@ -435,9 +437,14 @@ class EntityPlatform: already_exists = True if already_exists: - msg = f"Entity id already exists - ignoring: {entity.entity_id}" if entity.unique_id is not None: - msg += f". Platform {self.platform_name} does not generate unique IDs" + msg = f"Platform {self.platform_name} does not generate unique IDs. " + if requested_entity_id: + msg += f"ID {entity.unique_id} is already used by {entity.entity_id} - ignoring {requested_entity_id}" + else: + msg += f"ID {entity.unique_id} already exists - ignoring {entity.entity_id}" + else: + msg = f"Entity id already exists - ignoring: {entity.entity_id}" self.logger.error(msg) entity.hass = None entity.platform = None diff --git a/tests/helpers/test_entity_platform.py b/tests/helpers/test_entity_platform.py index ddecd1988ed..527a89843dd 100644 --- a/tests/helpers/test_entity_platform.py +++ b/tests/helpers/test_entity_platform.py @@ -375,8 +375,9 @@ async def test_async_remove_with_platform(hass): assert len(hass.states.async_entity_ids()) == 0 -async def test_not_adding_duplicate_entities_with_unique_id(hass): +async def test_not_adding_duplicate_entities_with_unique_id(hass, caplog): """Test for not adding duplicate entities.""" + caplog.set_level(logging.ERROR) component = EntityComponent(_LOGGER, DOMAIN, hass) await component.async_add_entities( @@ -384,9 +385,20 @@ async def test_not_adding_duplicate_entities_with_unique_id(hass): ) assert len(hass.states.async_entity_ids()) == 1 + assert not caplog.text ent2 = MockEntity(name="test2", unique_id="not_very_unique") await component.async_add_entities([ent2]) + assert "test1" in caplog.text + assert DOMAIN in caplog.text + + ent3 = MockEntity( + name="test2", entity_id="test_domain.test3", unique_id="not_very_unique" + ) + await component.async_add_entities([ent3]) + assert "test1" in caplog.text + assert "test3" in caplog.text + assert DOMAIN in caplog.text assert ent2.hass is None assert ent2.platform is None