diff --git a/homeassistant/helpers/entity.py b/homeassistant/helpers/entity.py index f33f26679a4d..673862c85814 100644 --- a/homeassistant/helpers/entity.py +++ b/homeassistant/helpers/entity.py @@ -1466,7 +1466,7 @@ class Entity( except BaseException as ex: self.__remove_future.set_exception(ex) raise - finally: + else: self.__remove_future.set_result(None) @final diff --git a/tests/helpers/test_entity.py b/tests/helpers/test_entity.py index a0f1d020942c..193e6ace90e4 100644 --- a/tests/helpers/test_entity.py +++ b/tests/helpers/test_entity.py @@ -614,6 +614,25 @@ async def test_async_remove_runs_callbacks(hass: HomeAssistant) -> None: assert ent._platform_state == entity.EntityPlatformState.REMOVED +async def test_async_remove_reports_the_original_error(hass: HomeAssistant) -> None: + """Test a failing removal surfaces its own exception.""" + + class MockEntityFailingRemoval(entity.Entity): + """Entity that cannot be removed cleanly.""" + + async def async_will_remove_from_hass(self) -> None: + """Fail while being removed.""" + raise ValueError("Boom") + + platform = MockEntityPlatform(hass, domain="test") + ent = MockEntityFailingRemoval() + ent.entity_id = "test.test" + await platform.async_add_entities([ent]) + + with pytest.raises(ValueError, match="Boom"): + await ent.async_remove() + + async def test_async_remove_ignores_in_flight_polling(hass: HomeAssistant) -> None: """Test in flight polling is ignored after removing.""" result = []