diff --git a/src/config/index.js b/src/config/index.js index 6cb9c03..195e5c2 100644 --- a/src/config/index.js +++ b/src/config/index.js @@ -2,5 +2,7 @@ module.exports = { FORCE_DB_SYNC: process.env.FORCE_DB_SYNC, NODE_ENV: process.env.NODE_ENV, PORT: process.env.PORT, - DEVICE_ID: process.env.DEVICE_ID + DEVICE_ID: process.env.DEVICE_ID, + HA_URL: process.env.HA_URL, + HA_TOKEN: process.env.HA_TOKEN }; diff --git a/src/server.js b/src/server.js index fecd73b..f51aed8 100644 --- a/src/server.js +++ b/src/server.js @@ -26,6 +26,18 @@ const config = require("./config"); const transConfig = require("./config/transcribe.config"); const { handlePythonOutput } = require("./scripts/py/pythonOutput"); +// === HomeAssistant === +const ha = {} + +ha.client = axios.create({ + baseURL: config.HA_URL, + headers: { + Authorization: `Bearer ${config.HA_TOKEN}`, + "Content-Type": "application/json", + }, +}); + +// === Speaker === let speaker = null; let speakerChannels = null; @@ -144,6 +156,26 @@ async function processor(group, consumer, streamKey) { case "remind": await playAudio("Reminder, " + String(msg.message.value)); break; + + case "light_on": + await playAudio("Turning on light."); + await ha.client.post("/api/services/homeassistant/turn_on", { + entity_id: "light.living_room_living_room" + }).catch(async (err) => { + await playAudio("I have failed you father."); + console.error(err); + }); + break; + + case "light_off": + await playAudio("Turning off light."); + await ha.client.post("/api/services/homeassistant/turn_off", { + entity_id: "light.living_room_living_room" + }).catch(async (err) => { + await playAudio("I have failed you father."); + console.error(err); + }); + break; default: await playAudio("Command, " + String(msg.message.command) + " is undefined.")