Skip to content

Commit

Permalink
Use WS functions from core for lovelace resource actions (#651)
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeeus authored Jun 4, 2023
1 parent f2ade3e commit 96787da
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 31 deletions.
27 changes: 0 additions & 27 deletions src/data/websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ import { HomeAssistant } from "../../homeassistant-frontend/src/types";
import { Hacs, HacsInfo } from "./hacs";
import { HacsDispatchEvent } from "./common";
import { RepositoryBase } from "./repository";
import {
LovelaceResource,
LovelaceResourcesMutableParams,
} from "../../homeassistant-frontend/src/data/lovelace";

export const fetchHacsInfo = async (hass: HomeAssistant) =>
hass.connection.sendMessagePromise<HacsInfo>({
Expand Down Expand Up @@ -55,29 +51,6 @@ export const repositoriesClearNew = async (hass: HomeAssistant, hacs: Hacs) =>
categories: hacs.info.categories,
});

export const fetchResources = (hass: HomeAssistant): Promise<LovelaceResource[]> =>
hass.connection.sendMessagePromise({
type: "lovelace/resources",
});

export const createResource = (hass: HomeAssistant, values: LovelaceResourcesMutableParams) =>
hass.callWS<LovelaceResource>({
type: "lovelace/resources/create",
...values,
});

export const updateResource = (hass: HomeAssistant, values: LovelaceResourcesMutableParams) =>
hass.callWS<LovelaceResource>({
type: "lovelace/resources/update",
...values,
});

export const deleteResource = (hass: HomeAssistant, id: string) =>
hass.callWS({
type: "lovelace/resources/delete",
resource_id: id,
});

export const websocketSubscription = (
hass: HomeAssistant,
onChange: (result: Record<any, any> | null) => void,
Expand Down
11 changes: 7 additions & 4 deletions src/tools/update-lovelace-resources.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import {
createResource,
fetchResources,
updateResource,
} from "../../homeassistant-frontend/src/data/lovelace";
import { HomeAssistant } from "../../homeassistant-frontend/src/types";
import { RepositoryInfo } from "../data/repository";
import { createResource, fetchResources, updateResource } from "../data/websocket";
import { generateLovelaceURL } from "./added-to-lovelace";

import { HacsLogger } from "./hacs-logger";
Expand All @@ -11,7 +15,7 @@ export async function updateLovelaceResources(
version?: string
): Promise<void> {
const logger = new HacsLogger("updateLovelaceResources");
const resources = await fetchResources(hass);
const resources = await fetchResources(hass.connection);
const namespace = `/hacsfiles/${repository.full_name.split("/")[1]}`;
const url = generateLovelaceURL({ repository, version });
const exsisting = resources.find((resource) => resource.url.includes(namespace));
Expand All @@ -20,9 +24,8 @@ export async function updateLovelaceResources(

if (exsisting && exsisting.url !== url) {
logger.debug(`Updating exsusting resource for ${namespace}`);
await updateResource(hass, {
await updateResource(hass, exsisting.id, {
url,
resource_id: exsisting.id,
res_type: exsisting.type,
});
} else if (!resources.map((resource) => resource.url).includes(url)) {
Expand Down

0 comments on commit 96787da

Please sign in to comment.