From 690471fb1870a1d4210218bd17cabc7e86d47580 Mon Sep 17 00:00:00 2001 From: Thomas Hermine Date: Thu, 19 Oct 2023 14:41:20 +0200 Subject: [PATCH] feat(app): Add HA & OpenAI init --- src/_helpers/logs.ts | 13 +++++++++++++ src/config.ts | 20 ++++++++++++++++++++ src/index.ts | 15 ++++++++++++--- src/init.ts | 21 +++++++++++++++++++++ 4 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 src/_helpers/logs.ts create mode 100644 src/config.ts create mode 100644 src/init.ts diff --git a/src/_helpers/logs.ts b/src/_helpers/logs.ts new file mode 100644 index 0000000..690c439 --- /dev/null +++ b/src/_helpers/logs.ts @@ -0,0 +1,13 @@ + +/** + * Log a message to the console with proper formatting + */ + +export async function log(category, severity, ...messages) { + const emojiForCategory = { + 'openai': '🧠', + 'ha': '🏠', + 'app': '🤖', + } + return console.log(emojiForCategory[category], `[${category.toUpperCase()}]`, ...messages); +} \ No newline at end of file diff --git a/src/config.ts b/src/config.ts new file mode 100644 index 0000000..ec7aac0 --- /dev/null +++ b/src/config.ts @@ -0,0 +1,20 @@ +export const { + NODE_ENV = 'development', +} = process.env; + +// Default values depending on the environment +const DEFAULT_HOME_ASSISTANT_HOST = (NODE_ENV === 'production') ? 'http://supervisor/core' : 'http://homeassistant.local'; +const DEFAULT_HOME_ASSISTANT_PORT = (NODE_ENV === 'production') ? 80 : 8123; +const DEFAULT_HOME_ASSISTANT_TOKEN = process.env.SUPERVISOR_TOKEN; + +// ===================================================================================================================== +// Config +// ===================================================================================================================== +// Config — Directly from ENV or reasonable defaults for the environment + +export const { + OPENAI_TOKEN, + HOME_ASSISTANT_HOST = DEFAULT_HOME_ASSISTANT_HOST, + HOME_ASSISTANT_PORT = DEFAULT_HOME_ASSISTANT_PORT, + HOME_ASSISTANT_TOKEN = DEFAULT_HOME_ASSISTANT_TOKEN, +} = process.env; \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 74857e9..d56b0a5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,9 +1,18 @@ import 'dotenv/config'; +import {init} from './init'; +import {log} from './_helpers/logs'; + async function main() { - console.log("Hello, world!"); - console.log('Here are your environnment variables:'); - console.log(process.env); + + // Init + // =================================================================================================================== + log('app','info','Starting up...'); + log('app','info','Here are your environment variables:'); + log('app','info',process.env); + + const {ha, openai } = await init(); + log('app','info','Initialized.'); } main(); \ No newline at end of file diff --git a/src/init.ts b/src/init.ts new file mode 100644 index 0000000..073d66b --- /dev/null +++ b/src/init.ts @@ -0,0 +1,21 @@ +import 'dotenv/config' +import homeassistant from 'homeassistant'; +import OpenAI from 'openai'; +import {HOME_ASSISTANT_TOKEN, HOME_ASSISTANT_HOST, OPENAI_TOKEN} from './config' +import {log} from './_helpers/logs' + + +export async function init() { + const ha = new homeassistant({ + host: HOME_ASSISTANT_HOST, + port: 8123, + token: HOME_ASSISTANT_TOKEN, + }); + const haStatus = await ha.status(); + log('ha','debug',haStatus.message); + const openai = new OpenAI({ + apiKey: OPENAI_TOKEN, + }); + log('openai','debug','Initialized.'); + return {ha,openai}; +} \ No newline at end of file