diff --git a/tests/components/conftest.py b/tests/components/conftest.py index 42944a48022..4294c0c2912 100644 --- a/tests/components/conftest.py +++ b/tests/components/conftest.py @@ -27,13 +27,14 @@ from homeassistant.config_entries import ( OptionsFlowManager, ) from homeassistant.const import STATE_OFF, STATE_ON -from homeassistant.core import HomeAssistant +from homeassistant.core import Context, HomeAssistant, ServiceRegistry, ServiceResponse from homeassistant.data_entry_flow import ( FlowContext, FlowHandler, FlowManager, FlowResultType, ) +from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import issue_registry as ir from homeassistant.helpers.translation import async_get_translations @@ -713,6 +714,23 @@ async def _check_create_issue_translations( ) +async def _check_exception_translation( + hass: HomeAssistant, + exception: HomeAssistantError, + translation_errors: dict[str, str], +) -> None: + if exception.translation_key is None: + return + await _validate_translation( + hass, + translation_errors, + "exceptions", + exception.translation_domain, + f"{exception.translation_key}.message", + exception.translation_placeholders, + ) + + @pytest.fixture(autouse=True) async def check_translations( ignore_translations: str | list[str], @@ -733,6 +751,7 @@ async def check_translations( # Keep reference to original functions _original_flow_manager_async_handle_step = FlowManager._async_handle_step _original_issue_registry_async_create_issue = ir.IssueRegistry.async_get_or_create + _original_service_registry_async_call = ServiceRegistry.async_call # Prepare override functions async def _flow_manager_async_handle_step( @@ -755,6 +774,33 @@ async def check_translations( ) return result + async def _service_registry_async_call( + self: ServiceRegistry, + domain: str, + service: str, + service_data: dict[str, Any] | None = None, + blocking: bool = False, + context: Context | None = None, + target: dict[str, Any] | None = None, + return_response: bool = False, + ) -> ServiceResponse: + try: + return await _original_service_registry_async_call( + self, + domain, + service, + service_data, + blocking, + context, + target, + return_response, + ) + except HomeAssistantError as err: + translation_coros.add( + _check_exception_translation(self._hass, err, translation_errors) + ) + raise + # Use override functions with ( patch( @@ -765,6 +811,10 @@ async def check_translations( "homeassistant.helpers.issue_registry.IssueRegistry.async_get_or_create", _issue_registry_async_create_issue, ), + patch( + "homeassistant.core.ServiceRegistry.async_call", + _service_registry_async_call, + ), ): yield diff --git a/tests/components/websocket_api/test_commands.py b/tests/components/websocket_api/test_commands.py index c1a043f915b..22e839d84e4 100644 --- a/tests/components/websocket_api/test_commands.py +++ b/tests/components/websocket_api/test_commands.py @@ -2390,6 +2390,9 @@ async def test_execute_script( ), ], ) +@pytest.mark.parametrize( + "ignore_translations", ["component.test.exceptions.test_error.message"] +) async def test_execute_script_err_localization( hass: HomeAssistant, websocket_client: MockHAClientWebSocket,