Support requesting multiple integration manifests in a single request (#12706)

* Support requesting multiple integration manifests in a single request

* only fetch if there are some to actually fetch

* handle empty

* not truthy, wrong language

* Do not copy params

Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
This commit is contained in:
J. Nick Koston 2022-05-18 14:09:09 -05:00 committed by GitHub
parent f4f51e1de5
commit 2796c3570a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 21 deletions

View File

@ -42,8 +42,18 @@ export const domainToName = (
manifest?: IntegrationManifest manifest?: IntegrationManifest
) => localize(`component.${domain}.title`) || manifest?.name || domain; ) => localize(`component.${domain}.title`) || manifest?.name || domain;
export const fetchIntegrationManifests = (hass: HomeAssistant) => export const fetchIntegrationManifests = (
hass.callWS<IntegrationManifest[]>({ type: "manifest/list" }); hass: HomeAssistant,
integrations?: string[]
) => {
const params: any = {
type: "manifest/list",
};
if (integrations) {
params.integrations = integrations;
}
return hass.callWS<IntegrationManifest[]>(params);
};
export const fetchIntegrationManifest = ( export const fetchIntegrationManifest = (
hass: HomeAssistant, hass: HomeAssistant,

View File

@ -46,7 +46,6 @@ import {
} from "../../../data/entity_registry"; } from "../../../data/entity_registry";
import { import {
domainToName, domainToName,
fetchIntegrationManifest,
fetchIntegrationManifests, fetchIntegrationManifests,
IntegrationManifest, IntegrationManifest,
} from "../../../data/integration"; } from "../../../data/integration";
@ -157,17 +156,19 @@ class HaConfigIntegrations extends SubscribeMixin(LitElement) {
}), }),
subscribeConfigFlowInProgress(this.hass, async (flowsInProgress) => { subscribeConfigFlowInProgress(this.hass, async (flowsInProgress) => {
const integrations: Set<string> = new Set(); const integrations: Set<string> = new Set();
const manifests: 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) {
integrations.add(flow.handler); integrations.add(flow.handler);
} }
this._fetchManifest(flow.handler); manifests.add(flow.handler);
}); });
await this.hass.loadBackendTranslation( await this.hass.loadBackendTranslation(
"config", "config",
Array.from(integrations) Array.from(integrations)
); );
this._fetchIntegrationManifests(manifests);
await nextRender(); await nextRender();
this._configEntriesInProgress = flowsInProgress.map((flow) => ({ this._configEntriesInProgress = flowsInProgress.map((flow) => ({
...flow, ...flow,
@ -566,8 +567,8 @@ class HaConfigIntegrations extends SubscribeMixin(LitElement) {
await scanUSBDevices(this.hass); await scanUSBDevices(this.hass);
} }
private async _fetchManifests() { private async _fetchManifests(integrations?: string[]) {
const fetched = await fetchIntegrationManifests(this.hass); const fetched = await fetchIntegrationManifests(this.hass, integrations);
// Make a copy so we can keep track of previously loaded manifests // Make a copy so we can keep track of previously loaded manifests
// for discovered flows (which are not part of these results) // for discovered flows (which are not part of these results)
const manifests = { ...this._manifests }; const manifests = { ...this._manifests };
@ -575,23 +576,25 @@ class HaConfigIntegrations extends SubscribeMixin(LitElement) {
this._manifests = manifests; this._manifests = manifests;
} }
private async _fetchManifest(domain: string) { private async _fetchIntegrationManifests(integrations: Set<string>) {
if (domain in this._manifests) { const manifestsToFetch: string[] = [];
return; for (const integration of integrations) {
} if (integration in this._manifests) {
if (this._extraFetchedManifests) { continue;
if (this._extraFetchedManifests.has(domain)) {
return;
} }
} else { if (this._extraFetchedManifests) {
this._extraFetchedManifests = new Set(); if (this._extraFetchedManifests.has(integration)) {
continue;
}
} else {
this._extraFetchedManifests = new Set();
}
this._extraFetchedManifests.add(integration);
manifestsToFetch.push(integration);
}
if (manifestsToFetch.length) {
await this._fetchManifests(manifestsToFetch);
} }
this._extraFetchedManifests.add(domain);
const manifest = await fetchIntegrationManifest(this.hass, domain);
this._manifests = {
...this._manifests,
[domain]: manifest,
};
} }
private _handleEntryRemoved(ev: HASSDomEvent<ConfigEntryRemovedEvent>) { private _handleEntryRemoved(ev: HASSDomEvent<ConfigEntryRemovedEvent>) {