From d5031b9c1aee5adb0b151cb8f9900d2495dc274f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Pluta?= Date: Fri, 15 Nov 2024 20:19:25 +0100 Subject: [PATCH] Add support for pushing things to OpenHAB --- src/importer/index.ts | 9 +++++---- src/openhab/index.ts | 4 ++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/importer/index.ts b/src/importer/index.ts index 97725bf..5874c2b 100644 --- a/src/importer/index.ts +++ b/src/importer/index.ts @@ -21,10 +21,6 @@ export const importThings = async (config: Config, options: ImportOptions) => { return new constructor(source, cfg).loadThings(); }))).flat(); - if (options.force) { - await openhab.deleteThings(...things.map(t => t.UID)); - } - const existingThings = await openhab.getThings(); const existingThingsUIDs = existingThings.map(t => t.UID); @@ -36,6 +32,11 @@ export const importThings = async (config: Config, options: ImportOptions) => { .filter(t => !existingThingsUIDs.includes(t.UID)) .filter(filter); + if (options.force) { + await openhab.deleteThings(...thingsToImport.map(t => t.UID)); + } + + await openhab.createThings(...thingsToImport); return thingsToImport; } \ No newline at end of file diff --git a/src/openhab/index.ts b/src/openhab/index.ts index beb5b31..93a16a9 100644 --- a/src/openhab/index.ts +++ b/src/openhab/index.ts @@ -28,8 +28,8 @@ export class OpenHAB { } async createThings(...things: Thing[]): Promise { - const response = await this.#api.post('/things'); - return response.data; + const responses = await Promise.all(things.map(thing => this.#api.post('/things', thing))); + return responses.map(r => r.data); } }