Compare commits

...
5 changed files with 227 additions and 11 deletions

No files matched your search

+6
View File
@@ -124,6 +124,12 @@ export const refreshMarketplaceRepository = (
repository: repositoryId,
});
// Fetches the information of every installed repository from GitHub again
export const refreshInstalledMarketplaceRepositories = (hass: CallWS) =>
hass.callWS<{ failed: string[] }>({
type: "marketplace/repositories/refresh",
});
export const addMarketplaceRepository = (
hass: CallWS,
repository: string,
@@ -4,11 +4,15 @@ import type { HassEntities } from "home-assistant-js-websocket";
import {
mdiCheckAll,
mdiCheckCircleOutline,
mdiCloudDownloadOutline,
mdiCompassOutline,
mdiDotsVertical,
mdiDownloadMultiple,
mdiHelpCircleOutline,
mdiLinkPlus,
mdiOpenInNew,
mdiRefresh,
mdiSourceRepositoryMultiple,
mdiStar,
mdiStore,
mdiViewGridOutline,
@@ -90,6 +94,7 @@ import type {
import { marketplaceErrorMessage } from "../../../data/marketplace/websocket";
import {
dismissNewMarketplaceRepositories,
refreshInstalledMarketplaceRepositories,
uninstallMarketplaceRepository,
} from "../../../data/marketplace/repository";
import { haStyle } from "../../../resources/styles";
@@ -115,6 +120,8 @@ import type {
RepositoryFilters,
} from "./dashboard-repositories";
import { documentationUrl } from "../../../util/documentation-url";
import { showToast } from "../../../util/toast";
import { handleGitHubRateLimited } from "../tools/connect-github";
import { renderRepositoryIcon } from "../tools/repository-icon";
import type { IntegrationCategoryIcons } from "../../../data/icons";
import { getIntegrationCategoryIcons } from "../../../data/icons";
@@ -172,6 +179,9 @@ const repositoriesOfTab = (
? repositories.filter((repository) => repository.installed)
: repositories;
// Each tab has its own dashboard, so a refresh started on one is shared here
let refreshingInstalled = false;
@customElement("ha-marketplace-dashboard")
export class HaMarketplaceDashboard extends LitElement {
@property({ attribute: false }) public marketplace!: MarketplaceData;
@@ -530,6 +540,9 @@ export class HaMarketplaceDashboard extends LitElement {
}
private _renderToolbar() {
const repositoriesContainsInstalled = this.marketplace.repositories.some(
(repository) => repository.installed
);
// Dismissing clears all of them, not only the ones the filters show
const repositoriesContainsNew = this.marketplace.repositories.some(
(repository) => repository.new
@@ -565,22 +578,52 @@ export class HaMarketplaceDashboard extends LitElement {
.label=${this._i18n.localize("ui.common.menu")}
.path=${mdiDotsVertical}
></ha-icon-button>
<ha-dropdown-item value="documentation">
${this._i18n.localize("ui.panel.marketplace.menu.documentation")}
</ha-dropdown-item>
<ha-dropdown-item value="custom_repositories">
${this._i18n.localize("ui.panel.marketplace.menu.custom_repositories")}
</ha-dropdown-item>
<ha-dropdown-item value="download_source">
${this._i18n.localize("ui.panel.marketplace.menu.download_source")}
</ha-dropdown-item>
${
repositoriesContainsInstalled
? html`<ha-dropdown-item
value="refresh_installed"
.disabled=${refreshingInstalled}
>
<ha-svg-icon slot="icon" .path=${mdiRefresh}></ha-svg-icon>
${this._i18n.localize(
"ui.panel.marketplace.menu.refresh_installed"
)}
</ha-dropdown-item>`
: nothing
}
${
repositoriesContainsNew
? html`<ha-dropdown-item value="dismiss_new">
<ha-svg-icon slot="icon" .path=${mdiCheckAll}></ha-svg-icon>
${this._i18n.localize("ui.panel.marketplace.menu.dismiss")}
</ha-dropdown-item>`
: nothing
}
${
repositoriesContainsInstalled || repositoriesContainsNew
? html`<wa-divider></wa-divider>`
: nothing
}
<ha-dropdown-item value="custom_repositories">
<ha-svg-icon
slot="icon"
.path=${mdiSourceRepositoryMultiple}
></ha-svg-icon>
${this._i18n.localize("ui.panel.marketplace.menu.custom_repositories")}
</ha-dropdown-item>
<ha-dropdown-item value="download_source">
<ha-svg-icon
slot="icon"
.path=${mdiCloudDownloadOutline}
></ha-svg-icon>
${this._i18n.localize("ui.panel.marketplace.menu.download_source")}
</ha-dropdown-item>
<wa-divider></wa-divider>
<ha-dropdown-item value="documentation">
<ha-svg-icon slot="icon" .path=${mdiHelpCircleOutline}></ha-svg-icon>
${this._i18n.localize("ui.panel.marketplace.menu.documentation")}
<ha-svg-icon slot="details" .path=${mdiOpenInNew}></ha-svg-icon>
</ha-dropdown-item>
</ha-dropdown>
</div>`;
}
@@ -968,12 +1011,70 @@ export class HaMarketplaceDashboard extends LitElement {
case "download_source":
this._openDownloadSource();
break;
case "refresh_installed":
this._refreshInstalled();
break;
case "dismiss_new":
this._dismissNew();
break;
}
};
private async _refreshInstalled() {
const localize = this._i18n.localize;
showToast(this, {
message: localize("ui.panel.marketplace.refresh_installed.started"),
});
let failed: string[] | undefined;
let error: unknown;
refreshingInstalled = true;
this.requestUpdate();
try {
({ failed } = await refreshInstalledMarketplaceRepositories(this._api));
} catch (err: unknown) {
error = err;
} finally {
refreshingInstalled = false;
this.requestUpdate();
}
// Switching tabs swaps this dashboard out while the refresh runs, the app
// itself can still show how it went.
const host = this.isConnected
? this
: (document.querySelector("home-assistant") as HTMLElement | null);
if (!host) {
return;
}
if (failed === undefined) {
if (!handleGitHubRateLimited(host, this._api, localize, error)) {
showAlertDialog(host, {
title: localize("ui.panel.marketplace.dialog.error.title"),
text: marketplaceErrorMessage(error, localize),
});
}
return;
}
if (failed.length) {
// The log has why, the dialog names which ones
showAlertDialog(host, {
title: localize("ui.panel.marketplace.refresh_installed.failed_title"),
text: localize("ui.panel.marketplace.refresh_installed.failed", {
count: failed.length,
repositories: failed.join(", "),
}),
});
return;
}
showToast(host, {
message: localize("ui.panel.marketplace.refresh_installed.done"),
});
}
private _handleOverflowAction = (ev: HaDropdownSelectEvent) => {
if (!this._overflowMenuRepository) {
return;
+8 -1
View File
@@ -13764,7 +13764,14 @@
"custom_repositories": "Custom repositories",
"dismiss": "Dismiss new repositories",
"documentation": "Documentation",
"download_source": "Download source"
"download_source": "Download source",
"refresh_installed": "Refresh installed repositories"
},
"refresh_installed": {
"started": "Refreshing the information of installed repositories…",
"done": "The information of installed repositories is up to date.",
"failed_title": "Some repositories were not refreshed",
"failed": "{count, plural,\n one {Could not refresh {repositories}. Check the Home Assistant logs for details.}\n other {Could not refresh {count} repositories: {repositories}. Check the Home Assistant logs for details.}\n}"
},
"dialog": {
"error": {
+93 -1
View File
@@ -8,7 +8,10 @@ import {
deleteConfigEntry,
getConfigEntries,
} from "../../../src/data/config_entries";
import { uninstallMarketplaceRepository } from "../../../src/data/marketplace/repository";
import {
refreshInstalledMarketplaceRepositories,
uninstallMarketplaceRepository,
} from "../../../src/data/marketplace/repository";
import "../../../src/panels/marketplace/dashboards/ha-marketplace-dashboard";
const stubElement = vi.hoisted(() => (tag: string) => {
@@ -64,6 +67,7 @@ vi.mock("../../../src/data/config_entries", async (importOriginal) => ({
}));
vi.mock("../../../src/data/marketplace/repository", async (importOriginal) => ({
...(await importOriginal<object>()),
refreshInstalledMarketplaceRepositories: vi.fn(),
uninstallMarketplaceRepository: vi.fn(),
}));
@@ -728,3 +732,91 @@ it("uninstalls nothing when the setups could not be checked", async () => {
expect(vi.mocked(deleteConfigEntry)).not.toHaveBeenCalled();
expect(vi.mocked(uninstallMarketplaceRepository)).not.toHaveBeenCalled();
});
const installedRepository = {
id: "1",
name: "Installed",
full_name: "owner/installed",
category: "integration",
status: "installed",
installed: true,
};
const refreshInstalled = (
dashboard: Awaited<ReturnType<typeof openDashboard>>
) =>
dashboard
.shadowRoot!.querySelector(".toolbar-actions ha-dropdown")!
.dispatchEvent(
new CustomEvent("wa-select", {
detail: { item: { value: "refresh_installed" } },
})
);
it("refreshes installed repositories and says when it is done", async () => {
vi.mocked(refreshInstalledMarketplaceRepositories).mockResolvedValue({
failed: [],
});
const dashboard = await openDashboard([installedRepository]);
const toasts: string[] = [];
dashboard.addEventListener("hass-notification", (ev) =>
toasts.push((ev as CustomEvent).detail.message)
);
refreshInstalled(dashboard);
await vi.waitFor(() =>
expect(toasts).toEqual([
"ui.panel.marketplace.refresh_installed.started",
"ui.panel.marketplace.refresh_installed.done",
])
);
expect(
vi.mocked(refreshInstalledMarketplaceRepositories)
).toHaveBeenCalledTimes(1);
});
it("names the repositories that could not be refreshed", async () => {
vi.mocked(refreshInstalledMarketplaceRepositories).mockResolvedValue({
failed: ["owner/installed"],
});
const dashboard = await openDashboard([installedRepository]);
const dialog = new Promise<{ title: string }>((resolve) => {
dashboard.addEventListener("show-dialog", (ev) =>
resolve(
((ev as CustomEvent).detail as { dialogParams: { title: string } })
.dialogParams
)
);
});
refreshInstalled(dashboard);
expect((await dialog).title).toBe(
"ui.panel.marketplace.refresh_installed.failed_title"
);
});
it("says the refresh is done after switching tabs", async () => {
let finish!: (result: { failed: string[] }) => void;
vi.mocked(refreshInstalledMarketplaceRepositories).mockReturnValue(
new Promise((resolve) => {
finish = resolve;
})
);
const app = document.createElement("home-assistant");
document.body.append(app);
const toasts: string[] = [];
app.addEventListener("hass-notification", (ev) =>
toasts.push((ev as CustomEvent).detail.message)
);
const dashboard = await openDashboard([installedRepository]);
refreshInstalled(dashboard);
dashboard.remove();
finish({ failed: [] });
await vi.waitFor(() =>
expect(toasts).toEqual(["ui.panel.marketplace.refresh_installed.done"])
);
});
@@ -50,6 +50,16 @@ describe("Marketplace strings", () => {
{ number: 1, count: "1" },
"1 open issue or pull request on GitHub",
],
[
"refresh_installed.failed",
{ count: 1, repositories: "owner/card" },
"Could not refresh owner/card. Check the Home Assistant logs for details.",
],
[
"refresh_installed.failed",
{ count: 2, repositories: "owner/card, owner/integration" },
"Could not refresh 2 repositories: owner/card, owner/integration. Check the Home Assistant logs for details.",
],
])("formats %s with %o", (key, values, expected) => {
expect(format(key, values)).toBe(expected);
});