From ba583cc669f28ece133f12f33286e40695a655b2 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Tue, 11 Feb 2025 01:21:43 +0100 Subject: [PATCH] Add test for trying to add an entity to an unknown config subentry (#138211) --- tests/helpers/test_entity_platform.py | 38 +++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/tests/helpers/test_entity_platform.py b/tests/helpers/test_entity_platform.py index ee9f9f09110..41b7271150a 100644 --- a/tests/helpers/test_entity_platform.py +++ b/tests/helpers/test_entity_platform.py @@ -2402,3 +2402,41 @@ async def test_device_type_error_checking( assert len(device_registry.devices) == 0 assert len(entity_registry.entities) == number_of_entities assert len(hass.states.async_all()) == number_of_entities + + +async def test_add_entity_unknown_subentry( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, + caplog: pytest.LogCaptureFixture, +) -> None: + """Test adding an entity to an unknown subentry.""" + + async def async_setup_entry( + hass: HomeAssistant, + config_entry: ConfigEntry, + async_add_entities: AddConfigEntryEntitiesCallback, + ) -> None: + """Mock setup entry method.""" + async_add_entities( + [MockEntity(name="test", unique_id="unique")], + config_subentry_id="unknown-subentry", + ) + + platform = MockPlatform(async_setup_entry=async_setup_entry) + config_entry = MockConfigEntry(entry_id="super-mock-id") + config_entry.add_to_hass(hass) + entity_platform = MockEntityPlatform( + hass, platform_name=config_entry.domain, platform=platform + ) + + assert not await entity_platform.async_setup_entry(config_entry) + await hass.async_block_till_done() + full_name = f"{config_entry.domain}.{entity_platform.domain}" + assert full_name not in hass.config.components + assert len(hass.states.async_entity_ids()) == 0 + assert len(entity_registry.entities) == 0 + + assert ( + "Can't add entities to unknown subentry unknown-subentry " + "of config entry super-mock-id" + ) in caplog.text