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,
language: string,
category: TranslationCategory,
integration?: string,
integration?: string | string[],
config_flow?: boolean
): Promise<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._unsubEvents = subscribeConfigFlowInProgress(this.hass, (flows) => {
this._discovered = flows;
const integrations: Set<string> = new Set();
for (const flow of flows) {
// To render 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;
}),
subscribeConfigFlowInProgress(this.hass, async (flowsInProgress) => {
const translationsPromisses: Promise<LocalizeFunc>[] = [];
const integrations: Set<string> = new Set();
flowsInProgress.forEach((flow) => {
// To render title placeholders
if (flow.context.title_placeholders) {
translationsPromisses.push(
this.hass.loadBackendTranslation("config", flow.handler)
);
integrations.add(flow.handler);
}
this._fetchManifest(flow.handler);
});
await Promise.all(translationsPromisses);
await this.hass.loadBackendTranslation(
"config",
Array.from(integrations)
);
await nextRender();
this._configEntriesInProgress = flowsInProgress.map((flow) => ({
...flow,

View File

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