Add translation checks for service exceptions (#131266)

* Add translation checks for service exceptions

* Adjust

* Remove invalid comment
This commit is contained in:
epenet 2024-11-27 08:46:45 +01:00 committed by GitHub
parent 00c4fa4146
commit 2b939ce6ec
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 54 additions and 1 deletions

View File

@ -27,13 +27,14 @@ from homeassistant.config_entries import (
OptionsFlowManager, OptionsFlowManager,
) )
from homeassistant.const import STATE_OFF, STATE_ON 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 ( from homeassistant.data_entry_flow import (
FlowContext, FlowContext,
FlowHandler, FlowHandler,
FlowManager, FlowManager,
FlowResultType, FlowResultType,
) )
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import issue_registry as ir from homeassistant.helpers import issue_registry as ir
from homeassistant.helpers.translation import async_get_translations 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) @pytest.fixture(autouse=True)
async def check_translations( async def check_translations(
ignore_translations: str | list[str], ignore_translations: str | list[str],
@ -733,6 +751,7 @@ async def check_translations(
# Keep reference to original functions # Keep reference to original functions
_original_flow_manager_async_handle_step = FlowManager._async_handle_step _original_flow_manager_async_handle_step = FlowManager._async_handle_step
_original_issue_registry_async_create_issue = ir.IssueRegistry.async_get_or_create _original_issue_registry_async_create_issue = ir.IssueRegistry.async_get_or_create
_original_service_registry_async_call = ServiceRegistry.async_call
# Prepare override functions # Prepare override functions
async def _flow_manager_async_handle_step( async def _flow_manager_async_handle_step(
@ -755,6 +774,33 @@ async def check_translations(
) )
return result 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 # Use override functions
with ( with (
patch( patch(
@ -765,6 +811,10 @@ async def check_translations(
"homeassistant.helpers.issue_registry.IssueRegistry.async_get_or_create", "homeassistant.helpers.issue_registry.IssueRegistry.async_get_or_create",
_issue_registry_async_create_issue, _issue_registry_async_create_issue,
), ),
patch(
"homeassistant.core.ServiceRegistry.async_call",
_service_registry_async_call,
),
): ):
yield yield

View File

@ -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( async def test_execute_script_err_localization(
hass: HomeAssistant, hass: HomeAssistant,
websocket_client: MockHAClientWebSocket, websocket_client: MockHAClientWebSocket,