diff --git a/tests/common.py b/tests/common.py index 8bd45e4d7f8..3ec3f6d844c 100644 --- a/tests/common.py +++ b/tests/common.py @@ -1815,3 +1815,20 @@ async def snapshot_platform( state = hass.states.get(entity_entry.entity_id) assert state, f"State not found for {entity_entry.entity_id}" assert state == snapshot(name=f"{entity_entry.entity_id}-state") + + +def reset_translation_cache(hass: HomeAssistant, components: list[str]) -> None: + """Reset translation cache for specified components. + + Use this if you are mocking a core component (for example via + mock_integration), to ensure that the mocked translations are not + persisted in the shared session cache. + """ + translations_cache = translation._async_get_translations_cache(hass) + for loaded_components in translations_cache.cache_data.loaded.values(): + for component_to_unload in components: + loaded_components.discard(component_to_unload) + for loaded_categories in translations_cache.cache_data.cache.values(): + for loaded_components in loaded_categories.values(): + for component_to_unload in components: + loaded_components.pop(component_to_unload, None) diff --git a/tests/components/conftest.py b/tests/components/conftest.py index 4294c0c2912..5628a2b1aaf 100644 --- a/tests/components/conftest.py +++ b/tests/components/conftest.py @@ -827,9 +827,6 @@ async def check_translations( f"Unused ignore translations: {', '.join(unused_ignore)}. " "Please remove them from the ignore_translations fixture." ) - for key, description in translation_errors.items(): - if key.startswith("component.cloud.issues."): - # cloud tests are flaky - continue + for description in translation_errors.values(): if description not in {"used", "unused"}: pytest.fail(description) diff --git a/tests/components/tts/test_init.py b/tests/components/tts/test_init.py index 80dff87eb9b..9d8dbf3ef94 100644 --- a/tests/components/tts/test_init.py +++ b/tests/components/tts/test_init.py @@ -45,6 +45,7 @@ from tests.common import ( mock_integration, mock_platform, mock_restore_cache, + reset_translation_cache, ) from tests.typing import ClientSessionGenerator, WebSocketGenerator @@ -1988,3 +1989,6 @@ async def test_default_engine_prefer_cloud_entity( provider_engine = tts.async_resolve_engine(hass, "test") assert provider_engine == "test" assert tts.async_default_engine(hass) == "tts.cloud_tts_entity" + + # Reset the `cloud` translations cache + reset_translation_cache(hass, ["cloud"]) diff --git a/tests/conftest.py b/tests/conftest.py index b858073a5e4..c46ed0407e5 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1192,7 +1192,12 @@ def mock_get_source_ip() -> Generator[_patch]: @pytest.fixture(autouse=True, scope="session") def translations_once() -> Generator[_patch]: - """Only load translations once per session.""" + """Only load translations once per session. + + Warning: having this as a session fixture can cause issues with tests that + create mock integrations, overriding the real integration translations + with empty ones. Translations should be reset after such tests (see #131628) + """ cache = _TranslationsCacheData({}, {}) patcher = patch( "homeassistant.helpers.translation._TranslationsCacheData",