Skip to content

Commit

Permalink
feat(app): Add HA & OpenAI init
Browse files Browse the repository at this point in the history
  • Loading branch information
thomashermine committed Oct 19, 2023
1 parent 015fe5c commit 690471f
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 3 deletions.
13 changes: 13 additions & 0 deletions src/_helpers/logs.ts
Original file line number Diff line number Diff line change
@@ -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);
}
20 changes: 20 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -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;
15 changes: 12 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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();
21 changes: 21 additions & 0 deletions src/init.ts
Original file line number Diff line number Diff line change
@@ -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};
}

0 comments on commit 690471f

Please sign in to comment.