Skip to content

Commit

Permalink
Restructure resource tools (#658)
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeeus authored Jun 10, 2023
1 parent 91878df commit 7d51a79
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 30 deletions.
8 changes: 4 additions & 4 deletions src/components/dialogs/hacs-download-dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import {
} from "../../data/repository";
import { getRepositories, repositoryBeta, websocketSubscription } from "../../data/websocket";
import { HacsStyles } from "../../styles/hacs-common-style";
import { generateLovelaceURL } from "../../tools/added-to-lovelace";
import { updateLovelaceResources } from "../../tools/update-lovelace-resources";
import { generateFrontendResourceURL } from "../../tools/frontend-resource";
import { updateFrontendResource } from "../../tools/frontend-resource";
import "./hacs-dialog";
import { HacsDialogBase } from "./hacs-dialog-base";

Expand Down Expand Up @@ -147,7 +147,7 @@ export class HacsDonwloadDialog extends HacsDialogBase {
? html`
<p>${this.hacs.localize(`dialog_download.lovelace_instruction`)}</p>
<pre>
url: ${generateLovelaceURL({ repository: this._repository, skipTag: true })}
url: ${generateFrontendResourceURL({ repository: this._repository, skipTag: true })}
type: module
</pre
>
Expand Down Expand Up @@ -227,7 +227,7 @@ export class HacsDonwloadDialog extends HacsDialogBase {
this.hacs.log.debug(this._repository.category, "_installRepository");
this.hacs.log.debug(this.hacs.info.lovelace_mode, "_installRepository");
if (this._repository.category === "plugin" && this.hacs.info.lovelace_mode === "storage") {
await updateLovelaceResources(this.hass, this._repository, selectedVersion);
await updateFrontendResource(this.hass, this._repository, selectedVersion);
}
this._installing = false;

Expand Down
20 changes: 0 additions & 20 deletions src/tools/added-to-lovelace.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,29 @@ import {
fetchResources,
updateResource,
} from "../../homeassistant-frontend/src/data/lovelace";
import { HomeAssistant } from "../../homeassistant-frontend/src/types";
import { RepositoryInfo } from "../data/repository";
import { generateLovelaceURL } from "./added-to-lovelace";

import type { HomeAssistant } from "../../homeassistant-frontend/src/types";
import type { RepositoryInfo } from "../data/repository";
import { HacsLogger } from "./hacs-logger";

export async function updateLovelaceResources(
const generateUniqueTag = (repository: RepositoryInfo, version?: string): string =>
String(
`${repository.id}${(
version ||
repository.installed_version ||
repository.selected_tag ||
repository.available_version
).replace(/\D+/g, "")}`
);

export async function updateFrontendResource(
hass: HomeAssistant,
repository: RepositoryInfo,
version?: string
): Promise<void> {
const logger = new HacsLogger("updateLovelaceResources");
const resources = await fetchResources(hass.connection);
const namespace = `/hacsfiles/${repository.full_name.split("/")[1]}`;
const url = generateLovelaceURL({ repository, version });
const url = generateFrontendResourceURL({ repository, version });
const exsisting = resources.find((resource) => resource.url.includes(namespace));

logger.debug({ namespace, url, exsisting });
Expand All @@ -36,3 +44,12 @@ export async function updateLovelaceResources(
});
}
}

export const generateFrontendResourceURL = (options: {
repository: RepositoryInfo;
version?: string;
skipTag?: boolean;
}): string =>
`/hacsfiles/${options.repository.full_name.split("/")[1]}/${options.repository.file_name}${
!options.skipTag ? `?hacstag=${generateUniqueTag(options.repository, options.version)}` : ""
}`;

0 comments on commit 7d51a79

Please sign in to comment.