Add Docker support and enhance configuration management
- Introduced Dockerfile for building and running the application in a containerized environment. - Added .dockerignore to exclude unnecessary files from the Docker context. - Updated README.md with detailed Docker installation instructions and Node.js version management using nvm. - Refactored environment variable handling in src/index.ts and src/config/hass.config.ts for improved configuration management. - Enhanced TypeScript configuration to include JSON module resolution and updated exclusion patterns. - Updated .gitignore to include additional files for better environment management.
This commit is contained in:
31
src/index.ts
31
src/index.ts
@@ -3,6 +3,10 @@ import { LiteMCP } from 'litemcp';
|
||||
import { z } from 'zod';
|
||||
import { DomainSchema } from './schemas.js';
|
||||
|
||||
// Configuration
|
||||
const HASS_HOST = process.env.HASS_HOST || 'http://192.168.178.63:8123';
|
||||
const HASS_TOKEN = process.env.HASS_TOKEN;
|
||||
|
||||
interface CommandParams {
|
||||
command: string;
|
||||
entity_id: string;
|
||||
@@ -41,9 +45,9 @@ async function main() {
|
||||
parameters: z.object({}),
|
||||
execute: async () => {
|
||||
try {
|
||||
const response = await fetch(`${process.env.HASS_HOST}/api/states`, {
|
||||
const response = await fetch(`${HASS_HOST}/api/states`, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${process.env.HASS_TOKEN}`,
|
||||
Authorization: `Bearer ${HASS_TOKEN}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
@@ -185,15 +189,26 @@ async function main() {
|
||||
|
||||
// Call Home Assistant service
|
||||
try {
|
||||
await hass.services[domain][service](serviceData);
|
||||
const response = await fetch(`${HASS_HOST}/api/services/${domain}/${service}`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Authorization: `Bearer ${HASS_TOKEN}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(serviceData),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to execute ${service} for ${params.entity_id}: ${response.statusText}`);
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: `Successfully executed ${service} for ${params.entity_id}`
|
||||
};
|
||||
} catch (error) {
|
||||
throw new Error(`Failed to execute ${service} for ${params.entity_id}: ${error instanceof Error ? error.message : 'Unknown error occurred'}`);
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: `Successfully executed ${service} for ${params.entity_id}`
|
||||
};
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
|
||||
Reference in New Issue
Block a user