From 6bd6adc4f54b013d0ce544ac78e4c326c2d39daf Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 10 Apr 2024 15:18:47 -1000 Subject: [PATCH] Avoid calling valid_entity_id when adding entities if they are already registered (#115388) --- homeassistant/helpers/entity_platform.py | 4 +++- tests/helpers/test_entity_platform.py | 13 +++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/homeassistant/helpers/entity_platform.py b/homeassistant/helpers/entity_platform.py index ec4eef1f6a79..2b9a5d436edf 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 59c4f7357f3d..64f6d6bf9f5f 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: