diff --git a/homeassistant/helpers/entity_platform.py b/homeassistant/helpers/entity_platform.py index ec4eef1f6a7..2b9a5d436ed 100644 --- a/homeassistant/helpers/entity_platform.py +++ b/homeassistant/helpers/entity_platform.py @@ -843,7 +843,9 @@ class EntityPlatform: ) # Make sure it is valid in case an entity set the value themselves - if not valid_entity_id(entity.entity_id): + # Avoid calling valid_entity_id if we already know it is valid + # since it already made it in the registry + if not entity.registry_entry and not valid_entity_id(entity.entity_id): entity.add_to_platform_abort() raise HomeAssistantError(f"Invalid entity ID: {entity.entity_id}") diff --git a/tests/helpers/test_entity_platform.py b/tests/helpers/test_entity_platform.py index 59c4f7357f3..64f6d6bf9f5 100644 --- a/tests/helpers/test_entity_platform.py +++ b/tests/helpers/test_entity_platform.py @@ -1112,6 +1112,19 @@ async def test_entity_registry_updates_invalid_entity_id(hass: HomeAssistant) -> assert hass.states.get("diff_domain.world") is None +async def test_add_entity_with_invalid_id( + hass: HomeAssistant, caplog: pytest.LogCaptureFixture +) -> None: + """Test trying to add an entity with an invalid entity_id.""" + platform = MockEntityPlatform(hass) + entity = MockEntity(entity_id="i.n.v.a.l.i.d") + await platform.async_add_entities([entity]) + assert ( + "Error adding entity i.n.v.a.l.i.d for domain " + "test_domain with platform test_platform" in caplog.text + ) + + async def test_device_info_called( hass: HomeAssistant, device_registry: dr.DeviceRegistry ) -> None: