From e8525f76ac6a3397dd8546e9237719353a324a57 Mon Sep 17 00:00:00 2001 From: Kuba Orlik Date: Mon, 5 Feb 2024 21:38:15 +0100 Subject: [PATCH] Add support for specifying custom config using env variable --- env.js | 3 ++- get-config.js | 7 +++++++ index.js | 7 ++++--- latest_color.js | 4 +++- light-loop.js | 3 ++- 5 files changed, 18 insertions(+), 6 deletions(-) create mode 100644 get-config.js diff --git a/env.js b/env.js index 1550184..4deed25 100644 --- a/env.js +++ b/env.js @@ -2,5 +2,6 @@ const TOKEN = process.env.HA_TOKEN; const HA_URL = process.env.HA_URL || "http://127.0.0.1:8123"; const PORT = parseInt(process.env.HA_BRIDGE_PORT || "41234"); const TRANSITION_DURATION_DIVIDER = parseInt(process.env.HA_DURATION_DIVIDER || "1"); +const CONFIG_FILE = process.env.CONFIG_FILE || "./config.js" ; -module.exports = { TOKEN, HA_URL, PORT, TRANSITION_DURATION_DIVIDER }; +module.exports = { TOKEN, HA_URL, PORT, TRANSITION_DURATION_DIVIDER, CONFIG_FILE }; diff --git a/get-config.js b/get-config.js new file mode 100644 index 0000000..c5d81a1 --- /dev/null +++ b/get-config.js @@ -0,0 +1,7 @@ +const { CONFIG_FILE } = require("./env.js"); + +module.exports = { + getConfig: function getConfig() { + return require(CONFIG_FILE); + }, +}; diff --git a/index.js b/index.js index 295220d..9eddf5b 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,8 @@ const dgram = require("node:dgram"); const server = dgram.createSocket("udp4"); +const { getConfig } = require("./get-config.js"); -const { lights } = require("./config.js"); +const { lights } = getConfig(); const light_loop = require("./light-loop.js"); const latest_color = require("./latest_color.js"); const { TOKEN, PORT } = require("./env.js"); @@ -16,12 +17,12 @@ const max_brightness = 0.8; if (!TOKEN) { throw new Error( - "Provide the Home Assistant Long Lived Token as a HA_TOKEN environment variable. Go to /profile in Home Assistant and scroll down.", + "Provide the Home Assistant Long Lived Token as a HA_TOKEN environment variable. Go to /profile in Home Assistant and scroll down." ); } console.log( - `Remember to set the Hyperion output controller type to UDPRAW and set it to output ${lights.length} lights`, + `Remember to set the Hyperion output controller type to UDPRAW and set it to output ${lights.length} lights` ); server.on("message", (msg, rinfo) => { diff --git a/latest_color.js b/latest_color.js index a399640..09403a9 100644 --- a/latest_color.js +++ b/latest_color.js @@ -1,4 +1,6 @@ -const { lights } = require("./config.js"); +const { getConfig } = require("./get-config.js"); + +const { lights } = getConfig(); let latest_color = []; diff --git a/light-loop.js b/light-loop.js index 5025741..60e97c9 100644 --- a/light-loop.js +++ b/light-loop.js @@ -2,7 +2,8 @@ const { HA_URL, TOKEN, TRANSITION_DURATION_DIVIDER } = require("./env.js"); const { sleep } = require("./util.js"); const latest_color = require("./latest_color.js"); -const { lights } = require("./config.js"); +const { getConfig } = require("./get-config.js"); +const { lights } = getConfig(); async function send_color( light_data,