Support requesting translations for multiple integrations in one request (#12704)

* Support requesting translations for multiple integrations in one request

- Requires https://github.com/home-assistant/core/pull/71979

* onboarding as well

* integrations -> integration

* fix cache

* short return if they are all loaded

* reduce

* reduce

* reduce
This commit is contained in:
J. Nick Koston 2022-05-18 13:37:47 -05:00 committed by GitHub
parent 7d1c77a38f
commit af6b0d3266
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 13 deletions

View File

@ -52,7 +52,7 @@ export const getHassTranslations = async (
hass: HomeAssistant, hass: HomeAssistant,
language: string, language: string,
category: TranslationCategory, category: TranslationCategory,
integration?: string, integration?: string | string[],
config_flow?: boolean config_flow?: boolean
): Promise<Record<string, unknown>> => { ): Promise<Record<string, unknown>> => {
const result = await hass.callWS<{ resources: Record<string, unknown> }>({ const result = await hass.callWS<{ resources: Record<string, unknown> }>({

View File

@ -49,12 +49,14 @@ class OnboardingIntegrations extends LitElement {
this.hass.loadBackendTranslation("title", undefined, true); this.hass.loadBackendTranslation("title", undefined, true);
this._unsubEvents = subscribeConfigFlowInProgress(this.hass, (flows) => { this._unsubEvents = subscribeConfigFlowInProgress(this.hass, (flows) => {
this._discovered = flows; this._discovered = flows;
const integrations: Set<string> = new Set();
for (const flow of flows) { for (const flow of flows) {
// To render title placeholders // To render title placeholders
if (flow.context.title_placeholders) { if (flow.context.title_placeholders) {
this.hass.loadBackendTranslation("config", flow.handler); integrations.add(flow.handler);
} }
} }
this.hass.loadBackendTranslation("config", Array.from(integrations));
}); });
} }

View File

@ -156,17 +156,18 @@ class HaConfigIntegrations extends SubscribeMixin(LitElement) {
this._deviceRegistryEntries = entries; this._deviceRegistryEntries = entries;
}), }),
subscribeConfigFlowInProgress(this.hass, async (flowsInProgress) => { subscribeConfigFlowInProgress(this.hass, async (flowsInProgress) => {
const translationsPromisses: Promise<LocalizeFunc>[] = []; const integrations: Set<string> = new Set();
flowsInProgress.forEach((flow) => { flowsInProgress.forEach((flow) => {
// To render title placeholders // To render title placeholders
if (flow.context.title_placeholders) { if (flow.context.title_placeholders) {
translationsPromisses.push( integrations.add(flow.handler);
this.hass.loadBackendTranslation("config", flow.handler)
);
} }
this._fetchManifest(flow.handler); this._fetchManifest(flow.handler);
}); });
await Promise.all(translationsPromisses); await this.hass.loadBackendTranslation(
"config",
Array.from(integrations)
);
await nextRender(); await nextRender();
this._configEntriesInProgress = flowsInProgress.map((flow) => ({ this._configEntriesInProgress = flowsInProgress.map((flow) => ({
...flow, ...flow,

View File

@ -242,12 +242,22 @@ export default <T extends Constructor<HassBaseEl>>(superClass: T) =>
}; };
} }
let integrationsToLoad: string[] = [];
// Check if already loaded // Check if already loaded
if (!force) { if (!force) {
if (integration) { if (integration && Array.isArray(integration)) {
integrationsToLoad = integration.filter(
(i) => !alreadyLoaded.integrations.includes(i)
);
if (!integrationsToLoad.length) {
return this.hass!.localize;
}
} else if (integration) {
if (alreadyLoaded.integrations.includes(integration)) { if (alreadyLoaded.integrations.includes(integration)) {
return this.hass!.localize; return this.hass!.localize;
} }
integrationsToLoad = [integration];
} else if ( } else if (
configFlow ? alreadyLoaded.configFlow : alreadyLoaded.setup configFlow ? alreadyLoaded.configFlow : alreadyLoaded.setup
) { ) {
@ -256,10 +266,8 @@ export default <T extends Constructor<HassBaseEl>>(superClass: T) =>
} }
// Add to cache // Add to cache
if (integration) { if (integrationsToLoad.length) {
if (!alreadyLoaded.integrations.includes(integration)) { alreadyLoaded.integrations.push(...integrationsToLoad);
alreadyLoaded.integrations.push(integration);
}
} else { } else {
alreadyLoaded.setup = true; alreadyLoaded.setup = true;
if (configFlow) { if (configFlow) {
@ -271,7 +279,7 @@ export default <T extends Constructor<HassBaseEl>>(superClass: T) =>
this.hass!, this.hass!,
language, language,
category, category,
integration, integrationsToLoad.length ? integrationsToLoad : undefined,
configFlow configFlow
); );