diff --git a/src/adapters/z2m.ts b/src/adapters/z2m.ts index 1e9d0b5..dfadc5f 100644 --- a/src/adapters/z2m.ts +++ b/src/adapters/z2m.ts @@ -2,6 +2,7 @@ import mqtt from "mqtt"; import { Z2MConfig, Thing, Channel, ChannelType, ItemType } from "@types"; import { snakecase } from "../utils/string"; import { Adapter } from "./abstract"; +import { enhancedStringConfig } from "../utils/config"; type Device = { definition?: Definition; @@ -140,7 +141,11 @@ export class Z2MAdapter extends Adapter { const { brokerURL, username, password, clientId, prefix } = this.config; return new Promise((resolve, reject) => { - const client = mqtt.connect(brokerURL, { username, password, clientId: clientId || "oh-importer" }); + const client = mqtt.connect(brokerURL, { + username: enhancedStringConfig(username), + password: enhancedStringConfig(password), + clientId: clientId || "oh-importer" + }); client.on("message", (_, message) => { client.end(); diff --git a/src/openhab/index.ts b/src/openhab/index.ts index 8a87128..beb5b31 100644 --- a/src/openhab/index.ts +++ b/src/openhab/index.ts @@ -1,5 +1,6 @@ import { OpenHABConfig, Thing } from "@types"; import axios, { AxiosInstance } from "axios"; +import { enhancedStringConfig } from "../utils/config"; export class OpenHAB { #api: AxiosInstance; @@ -12,7 +13,7 @@ export class OpenHAB { this.#api = axios.create({ baseURL: `${baseURL}/rest`, headers: { - Authorization: `Bearer ${token}` + Authorization: `Bearer ${enhancedStringConfig(token)}` } }); } diff --git a/src/utils/config.ts b/src/utils/config.ts new file mode 100644 index 0000000..18d823e --- /dev/null +++ b/src/utils/config.ts @@ -0,0 +1,17 @@ +import { readFileSync } from "fs"; + +const specialOptions: Record string> = { + $__file: (arg: string) => readFileSync(arg, 'utf8') +}; + +export const enhancedStringConfig = (value: string) => { + const trimmed = value.trim(); + + for(const opt of Object.keys(specialOptions)) { + if(trimmed.startsWith(`${opt}:`) && opt in specialOptions) { + return specialOptions[opt](trimmed.slice(opt.length + 1)); + } + } + + return trimmed; +}; \ No newline at end of file