From 114e254aa650e5eb8f2ad0db419db8bd1840513a Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Mon, 2 Sep 2024 14:20:50 +0200 Subject: [PATCH] Don't raise when registering entity service with invalid schema (#125057) * Don't raise when registering entity service with invalid schema * Update homeassistant/helpers/service.py Co-authored-by: Robert Resch --------- Co-authored-by: Robert Resch --- homeassistant/helpers/service.py | 11 ++++++++++- tests/helpers/test_entity_component.py | 22 ++++++++++++---------- tests/helpers/test_entity_platform.py | 22 ++++++++++++---------- 3 files changed, 34 insertions(+), 21 deletions(-) diff --git a/homeassistant/helpers/service.py b/homeassistant/helpers/service.py index 573073f3809..bb9490b9edd 100644 --- a/homeassistant/helpers/service.py +++ b/homeassistant/helpers/service.py @@ -1268,7 +1268,16 @@ def async_register_entity_service( # the check could be extended to require All/Any to have sub schema(s) # with all entity service fields elif not cv.is_entity_service_schema(schema): - raise HomeAssistantError("The schema is not an entity service schema") + # pylint: disable-next=import-outside-toplevel + from .frame import report + + report( + ( + "registers an entity service with a non entity service schema " + "which will stop working in HA Core 2025.9" + ), + error_if_core=False, + ) service_func: str | HassJob[..., Any] service_func = func if isinstance(func, str) else HassJob(func) diff --git a/tests/helpers/test_entity_component.py b/tests/helpers/test_entity_component.py index 8f4ece09a17..9723b91eb9a 100644 --- a/tests/helpers/test_entity_component.py +++ b/tests/helpers/test_entity_component.py @@ -557,21 +557,22 @@ async def test_register_entity_service( async def test_register_entity_service_non_entity_service_schema( - hass: HomeAssistant, + hass: HomeAssistant, caplog: pytest.LogCaptureFixture ) -> None: """Test attempting to register a service with a non entity service schema.""" component = EntityComponent(_LOGGER, DOMAIN, hass) + expected_message = "registers an entity service with a non entity service schema" - for schema in ( - vol.Schema({"some": str}), - vol.All(vol.Schema({"some": str})), - vol.Any(vol.Schema({"some": str})), + for idx, schema in enumerate( + ( + vol.Schema({"some": str}), + vol.All(vol.Schema({"some": str})), + vol.Any(vol.Schema({"some": str})), + ) ): - with pytest.raises( - HomeAssistantError, - match=("The schema is not an entity service schema"), - ): - component.async_register_entity_service("hello", schema, Mock()) + component.async_register_entity_service(f"hello_{idx}", schema, Mock()) + assert expected_message in caplog.text + caplog.clear() for idx, schema in enumerate( ( @@ -581,6 +582,7 @@ async def test_register_entity_service_non_entity_service_schema( ) ): component.async_register_entity_service(f"test_service_{idx}", schema, Mock()) + assert expected_message not in caplog.text async def test_register_entity_service_response_data(hass: HomeAssistant) -> None: diff --git a/tests/helpers/test_entity_platform.py b/tests/helpers/test_entity_platform.py index 2b0598cfe9d..db83819085b 100644 --- a/tests/helpers/test_entity_platform.py +++ b/tests/helpers/test_entity_platform.py @@ -1811,23 +1811,24 @@ async def test_register_entity_service_none_schema( async def test_register_entity_service_non_entity_service_schema( - hass: HomeAssistant, + hass: HomeAssistant, caplog: pytest.LogCaptureFixture ) -> None: """Test attempting to register a service with a non entity service schema.""" entity_platform = MockEntityPlatform( hass, domain="mock_integration", platform_name="mock_platform", platform=None ) + expected_message = "registers an entity service with a non entity service schema" - for schema in ( - vol.Schema({"some": str}), - vol.All(vol.Schema({"some": str})), - vol.Any(vol.Schema({"some": str})), + for idx, schema in enumerate( + ( + vol.Schema({"some": str}), + vol.All(vol.Schema({"some": str})), + vol.Any(vol.Schema({"some": str})), + ) ): - with pytest.raises( - HomeAssistantError, - match="The schema is not an entity service schema", - ): - entity_platform.async_register_entity_service("hello", schema, Mock()) + entity_platform.async_register_entity_service(f"hello_{idx}", schema, Mock()) + assert expected_message in caplog.text + caplog.clear() for idx, schema in enumerate( ( @@ -1839,6 +1840,7 @@ async def test_register_entity_service_non_entity_service_schema( entity_platform.async_register_entity_service( f"test_service_{idx}", schema, Mock() ) + assert expected_message not in caplog.text @pytest.mark.parametrize("update_before_add", [True, False])