async def test_get_translations_while_loading_components(hass): """Test the get translations helper loads config flow translations.""" integration = Mock(file_path=pathlib.Path(__file__)) integration.name = "Component 1" hass.config.components.add("component1") load_count = 0 def mock_load_translation_files(files): """Mock load translation files.""" nonlocal load_count load_count += 1 # Mimic race condition by loading a component during setup return {"component1": {"title": "world"}} with patch( "homeassistant.helpers.translation.component_translation_path", return_value="bla.json", ), patch( "homeassistant.helpers.translation.load_translations_files", mock_load_translation_files, ), patch( "homeassistant.helpers.translation.async_get_integration", return_value=integration, ): tasks = [ translation.async_get_translations(hass, "en", "title") for _ in range(5) ] all_translations = await asyncio.gather(*tasks) assert all_translations[0] == { "component.component1.title": "world", } assert load_count == 1
def get(self, request, language): """Return translations.""" hass = request.app['hass'] resources = yield from async_get_translations(hass, language) return self.json({ 'resources': resources, })