refactor: optimize configuration and tool implementations

- Standardized error handling across tool implementations
- Improved return type consistency for tool execution results
- Simplified configuration parsing and type definitions
- Enhanced type safety for various configuration schemas
- Cleaned up and normalized tool response structures
- Updated SSE and event subscription tool implementations
This commit is contained in:
jango-blockchained
2025-02-04 00:56:45 +01:00
parent 9a02bdaf11
commit bc1dc8278a
65 changed files with 7094 additions and 7675 deletions

View File

@@ -1,77 +1,77 @@
import { mock } from 'bun:test';
import { mock } from "bun:test";
export const LIB_HASS = {
configuration: {
name: 'Home Assistant',
version: '2024.2.0',
location_name: 'Home',
time_zone: 'UTC',
components: ['automation', 'script', 'light', 'switch'],
unit_system: {
temperature: '°C',
length: 'm',
mass: 'kg',
pressure: 'hPa',
volume: 'L'
}
configuration: {
name: "Home Assistant",
version: "2024.2.0",
location_name: "Home",
time_zone: "UTC",
components: ["automation", "script", "light", "switch"],
unit_system: {
temperature: "°C",
length: "m",
mass: "kg",
pressure: "hPa",
volume: "L",
},
services: {
light: {
turn_on: mock(() => Promise.resolve()),
turn_off: mock(() => Promise.resolve()),
toggle: mock(() => Promise.resolve())
},
switch: {
turn_on: mock(() => Promise.resolve()),
turn_off: mock(() => Promise.resolve()),
toggle: mock(() => Promise.resolve())
},
automation: {
trigger: mock(() => Promise.resolve()),
turn_on: mock(() => Promise.resolve()),
turn_off: mock(() => Promise.resolve())
},
script: {
turn_on: mock(() => Promise.resolve()),
turn_off: mock(() => Promise.resolve()),
toggle: mock(() => Promise.resolve())
}
},
services: {
light: {
turn_on: mock(() => Promise.resolve()),
turn_off: mock(() => Promise.resolve()),
toggle: mock(() => Promise.resolve()),
},
states: {
light: {
'light.living_room': {
state: 'on',
attributes: {
brightness: 255,
color_temp: 300,
friendly_name: 'Living Room Light'
}
},
'light.bedroom': {
state: 'off',
attributes: {
friendly_name: 'Bedroom Light'
}
}
switch: {
turn_on: mock(() => Promise.resolve()),
turn_off: mock(() => Promise.resolve()),
toggle: mock(() => Promise.resolve()),
},
automation: {
trigger: mock(() => Promise.resolve()),
turn_on: mock(() => Promise.resolve()),
turn_off: mock(() => Promise.resolve()),
},
script: {
turn_on: mock(() => Promise.resolve()),
turn_off: mock(() => Promise.resolve()),
toggle: mock(() => Promise.resolve()),
},
},
states: {
light: {
"light.living_room": {
state: "on",
attributes: {
brightness: 255,
color_temp: 300,
friendly_name: "Living Room Light",
},
switch: {
'switch.tv': {
state: 'off',
attributes: {
friendly_name: 'TV'
}
}
}
},
"light.bedroom": {
state: "off",
attributes: {
friendly_name: "Bedroom Light",
},
},
},
events: {
subscribe: mock(() => Promise.resolve()),
unsubscribe: mock(() => Promise.resolve()),
fire: mock(() => Promise.resolve())
switch: {
"switch.tv": {
state: "off",
attributes: {
friendly_name: "TV",
},
},
},
connection: {
subscribeEvents: mock(() => Promise.resolve()),
subscribeMessage: mock(() => Promise.resolve()),
sendMessage: mock(() => Promise.resolve()),
close: mock(() => Promise.resolve())
}
};
},
events: {
subscribe: mock(() => Promise.resolve()),
unsubscribe: mock(() => Promise.resolve()),
fire: mock(() => Promise.resolve()),
},
connection: {
subscribeEvents: mock(() => Promise.resolve()),
subscribeMessage: mock(() => Promise.resolve()),
sendMessage: mock(() => Promise.resolve()),
close: mock(() => Promise.resolve()),
},
};

View File

@@ -1,61 +1,61 @@
export class LiteMCP {
name: string;
version: string;
config: any;
name: string;
version: string;
config: any;
constructor(config: any = {}) {
this.name = 'home-assistant';
this.version = '1.0.0';
this.config = config;
}
constructor(config: any = {}) {
this.name = "home-assistant";
this.version = "1.0.0";
this.config = config;
}
async start() {
return Promise.resolve();
}
async start() {
return Promise.resolve();
}
async stop() {
return Promise.resolve();
}
async stop() {
return Promise.resolve();
}
async connect() {
return Promise.resolve();
}
async connect() {
return Promise.resolve();
}
async disconnect() {
return Promise.resolve();
}
async disconnect() {
return Promise.resolve();
}
async callService(domain: string, service: string, data: any = {}) {
return Promise.resolve({ success: true });
}
async callService(domain: string, service: string, data: any = {}) {
return Promise.resolve({ success: true });
}
async getStates() {
return Promise.resolve([]);
}
async getStates() {
return Promise.resolve([]);
}
async getState(entityId: string) {
return Promise.resolve({
entity_id: entityId,
state: 'unknown',
attributes: {},
last_changed: new Date().toISOString(),
last_updated: new Date().toISOString()
});
}
async getState(entityId: string) {
return Promise.resolve({
entity_id: entityId,
state: "unknown",
attributes: {},
last_changed: new Date().toISOString(),
last_updated: new Date().toISOString(),
});
}
async setState(entityId: string, state: string, attributes: any = {}) {
return Promise.resolve({ success: true });
}
async setState(entityId: string, state: string, attributes: any = {}) {
return Promise.resolve({ success: true });
}
onStateChanged(callback: (event: any) => void) {
// Mock implementation
}
onStateChanged(callback: (event: any) => void) {
// Mock implementation
}
onEvent(eventType: string, callback: (event: any) => void) {
// Mock implementation
}
onEvent(eventType: string, callback: (event: any) => void) {
// Mock implementation
}
}
export const createMCP = (config: any = {}) => {
return new LiteMCP(config);
};
return new LiteMCP(config);
};