Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e96fa163cd | ||
|
|
cfef80e1e5 | ||
|
|
9b74a4354b | ||
|
|
fca193b5b2 | ||
|
|
cc9eede856 | ||
|
|
f0ff3d5e5a |
62
.github/workflows/deploy-docs.yml
vendored
62
.github/workflows/deploy-docs.yml
vendored
@@ -1,4 +1,5 @@
|
|||||||
name: Deploy Documentation
|
name: Deploy Documentation
|
||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
branches:
|
branches:
|
||||||
@@ -6,29 +7,70 @@ on:
|
|||||||
paths:
|
paths:
|
||||||
- 'docs/**'
|
- 'docs/**'
|
||||||
- 'mkdocs.yml'
|
- 'mkdocs.yml'
|
||||||
|
# Allow manual trigger
|
||||||
|
workflow_dispatch:
|
||||||
|
|
||||||
|
# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
pages: write
|
||||||
|
id-token: write
|
||||||
|
|
||||||
|
# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
|
||||||
|
concurrency:
|
||||||
|
group: "pages"
|
||||||
|
cancel-in-progress: false
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
deploy:
|
build:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
permissions:
|
|
||||||
contents: write
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- name: Checkout repository
|
||||||
|
uses: actions/checkout@v4
|
||||||
with:
|
with:
|
||||||
fetch-depth: 0
|
fetch-depth: 0
|
||||||
- uses: actions/setup-python@v5
|
|
||||||
|
- name: Setup Python
|
||||||
|
uses: actions/setup-python@v5
|
||||||
with:
|
with:
|
||||||
python-version: '3.x'
|
python-version: '3.x'
|
||||||
cache: 'pip'
|
cache: 'pip'
|
||||||
|
|
||||||
|
- name: Setup Pages
|
||||||
|
uses: actions/configure-pages@v4
|
||||||
|
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: |
|
run: |
|
||||||
python -m pip install --upgrade pip
|
python -m pip install --upgrade pip
|
||||||
pip install -r docs/requirements.txt
|
pip install -r docs/requirements.txt
|
||||||
- name: Configure Git
|
|
||||||
|
- name: List mkdocs configuration
|
||||||
run: |
|
run: |
|
||||||
git config --global user.name "github-actions[bot]"
|
echo "Current directory contents:"
|
||||||
git config --global user.email "github-actions[bot]@users.noreply.github.com"
|
ls -la
|
||||||
- name: Build and Deploy
|
echo "MkDocs version:"
|
||||||
|
mkdocs --version
|
||||||
|
echo "MkDocs configuration:"
|
||||||
|
cat mkdocs.yml
|
||||||
|
|
||||||
|
- name: Build documentation
|
||||||
run: |
|
run: |
|
||||||
mkdocs build --strict
|
mkdocs build --strict
|
||||||
mkdocs gh-deploy --force --clean
|
echo "Build output contents:"
|
||||||
|
ls -la site/advanced-homeassistant-mcp
|
||||||
|
|
||||||
|
- name: Upload artifact
|
||||||
|
uses: actions/upload-pages-artifact@v3
|
||||||
|
with:
|
||||||
|
path: ./site/advanced-homeassistant-mcp
|
||||||
|
|
||||||
|
deploy:
|
||||||
|
environment:
|
||||||
|
name: github-pages
|
||||||
|
url: ${{ steps.deployment.outputs.page_url }}
|
||||||
|
needs: build
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Deploy to GitHub Pages
|
||||||
|
id: deployment
|
||||||
|
uses: actions/deploy-pages@v4
|
||||||
94
README.md
94
README.md
@@ -12,12 +12,22 @@ MCP (Model Context Protocol) Server is a lightweight integration tool for Home A
|
|||||||
- 📡 WebSocket/Server-Sent Events (SSE) for state updates
|
- 📡 WebSocket/Server-Sent Events (SSE) for state updates
|
||||||
- 🤖 Simple automation rule management
|
- 🤖 Simple automation rule management
|
||||||
- 🔐 JWT-based authentication
|
- 🔐 JWT-based authentication
|
||||||
|
- 🎤 Real-time device control and monitoring
|
||||||
|
- 🎤 Server-Sent Events (SSE) for live updates
|
||||||
|
- 🎤 Comprehensive logging
|
||||||
|
- 🎤 Optional speech features:
|
||||||
|
- 🎤 Wake word detection ("hey jarvis", "ok google", "alexa")
|
||||||
|
- 🎤 Speech-to-text using fast-whisper
|
||||||
|
- 🎤 Multiple language support
|
||||||
|
- 🎤 GPU acceleration support
|
||||||
|
|
||||||
## Prerequisites 📋
|
## Prerequisites 📋
|
||||||
|
|
||||||
- 🚀 Bun runtime (v1.0.26+)
|
- 🚀 Bun runtime (v1.0.26+)
|
||||||
- 🏡 Home Assistant instance
|
- 🏡 Home Assistant instance
|
||||||
- 🐳 Docker (optional, recommended for deployment)
|
- 🐳 Docker (optional, recommended for deployment and speech features)
|
||||||
|
- 🖥️ Node.js 18+ (optional, for speech features)
|
||||||
|
- 🖥️ NVIDIA GPU with CUDA support (optional, for faster speech processing)
|
||||||
|
|
||||||
## Installation 🛠️
|
## Installation 🛠️
|
||||||
|
|
||||||
@@ -30,7 +40,7 @@ cd homeassistant-mcp
|
|||||||
|
|
||||||
# Copy and edit environment configuration
|
# Copy and edit environment configuration
|
||||||
cp .env.example .env
|
cp .env.example .env
|
||||||
# Edit .env with your Home Assistant credentials
|
# Edit .env with your Home Assistant credentials and speech features settings
|
||||||
|
|
||||||
# Build and start containers
|
# Build and start containers
|
||||||
docker compose up -d --build
|
docker compose up -d --build
|
||||||
@@ -79,33 +89,69 @@ ws.onmessage = (event) => {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
## Current Limitations ⚠️
|
## Speech Features (Optional)
|
||||||
|
|
||||||
- 🎙️ Basic voice command support (work in progress)
|
The MCP Server includes optional speech processing capabilities:
|
||||||
- 🧠 Limited advanced NLP capabilities
|
|
||||||
- 🔗 Minimal third-party device integration
|
|
||||||
- 🐛 Early-stage error handling
|
|
||||||
|
|
||||||
## Contributing 🤝
|
### Prerequisites
|
||||||
|
1. Docker installed and running
|
||||||
|
2. NVIDIA GPU with CUDA support (optional)
|
||||||
|
3. At least 4GB RAM (8GB+ recommended for larger models)
|
||||||
|
|
||||||
1. Fork the repository
|
### Setup
|
||||||
2. Create a feature branch:
|
|
||||||
```bash
|
|
||||||
git checkout -b feature/your-feature
|
|
||||||
```
|
|
||||||
3. Make your changes
|
|
||||||
4. Run tests:
|
|
||||||
```bash
|
|
||||||
bun test
|
|
||||||
```
|
|
||||||
5. Submit a pull request
|
|
||||||
|
|
||||||
## Roadmap 🗺️
|
1. Enable speech features in your .env:
|
||||||
|
```bash
|
||||||
|
ENABLE_SPEECH_FEATURES=true
|
||||||
|
ENABLE_WAKE_WORD=true
|
||||||
|
ENABLE_SPEECH_TO_TEXT=true
|
||||||
|
WHISPER_MODEL_PATH=/models
|
||||||
|
WHISPER_MODEL_TYPE=base
|
||||||
|
```
|
||||||
|
|
||||||
- 🎤 Enhance voice command processing
|
2. Start the speech services:
|
||||||
- 🔌 Improve device compatibility
|
```bash
|
||||||
- 🤖 Expand automation capabilities
|
docker-compose up -d
|
||||||
- 🛡️ Implement more robust error handling
|
```
|
||||||
|
|
||||||
|
### Available Models
|
||||||
|
|
||||||
|
Choose a model based on your needs:
|
||||||
|
- `tiny.en`: Fastest, basic accuracy
|
||||||
|
- `base.en`: Good balance (recommended)
|
||||||
|
- `small.en`: Better accuracy, slower
|
||||||
|
- `medium.en`: High accuracy, resource intensive
|
||||||
|
- `large-v2`: Best accuracy, very resource intensive
|
||||||
|
|
||||||
|
### Usage
|
||||||
|
|
||||||
|
1. Wake word detection listens for:
|
||||||
|
- "hey jarvis"
|
||||||
|
- "ok google"
|
||||||
|
- "alexa"
|
||||||
|
|
||||||
|
2. After wake word detection:
|
||||||
|
- Audio is automatically captured
|
||||||
|
- Speech is transcribed
|
||||||
|
- Commands are processed
|
||||||
|
|
||||||
|
3. Manual transcription is also available:
|
||||||
|
```typescript
|
||||||
|
const speech = speechService.getSpeechToText();
|
||||||
|
const text = await speech.transcribe(audioBuffer);
|
||||||
|
```
|
||||||
|
|
||||||
|
## Configuration
|
||||||
|
|
||||||
|
See [Configuration Guide](docs/configuration.md) for detailed settings.
|
||||||
|
|
||||||
|
## API Documentation
|
||||||
|
|
||||||
|
See [API Documentation](docs/api/index.md) for available endpoints.
|
||||||
|
|
||||||
|
## Development
|
||||||
|
|
||||||
|
See [Development Guide](docs/development/index.md) for contribution guidelines.
|
||||||
|
|
||||||
## License 📄
|
## License 📄
|
||||||
|
|
||||||
|
|||||||
@@ -1,149 +1,149 @@
|
|||||||
import { describe, expect, test } from "bun:test";
|
import { describe, expect, test, beforeEach, afterEach, mock, spyOn } from "bun:test";
|
||||||
import { describe, expect, test, beforeEach, afterEach, mock } from "bun:test";
|
|
||||||
import type { Mock } from "bun:test";
|
import type { Mock } from "bun:test";
|
||||||
import type { Express, Application } from 'express';
|
import type { Elysia } from "elysia";
|
||||||
import type { Logger } from 'winston';
|
|
||||||
|
|
||||||
// Types for our mocks
|
// Create mock instances
|
||||||
interface MockApp {
|
const mockApp = {
|
||||||
use: Mock<() => void>;
|
use: mock(() => mockApp),
|
||||||
listen: Mock<(port: number, callback: () => void) => { close: Mock<() => void> }>;
|
get: mock(() => mockApp),
|
||||||
}
|
post: mock(() => mockApp),
|
||||||
|
listen: mock((port: number, callback?: () => void) => {
|
||||||
interface MockLiteMCPInstance {
|
callback?.();
|
||||||
addTool: Mock<() => void>;
|
return mockApp;
|
||||||
start: Mock<() => Promise<void>>;
|
|
||||||
}
|
|
||||||
|
|
||||||
type MockLogger = {
|
|
||||||
info: Mock<(message: string) => void>;
|
|
||||||
error: Mock<(message: string) => void>;
|
|
||||||
debug: Mock<(message: string) => void>;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Mock express
|
|
||||||
const mockApp: MockApp = {
|
|
||||||
use: mock(() => undefined),
|
|
||||||
listen: mock((port: number, callback: () => void) => {
|
|
||||||
callback();
|
|
||||||
return { close: mock(() => undefined) };
|
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
const mockExpress = mock(() => mockApp);
|
|
||||||
|
|
||||||
// Mock LiteMCP instance
|
// Create mock constructors
|
||||||
const mockLiteMCPInstance: MockLiteMCPInstance = {
|
const MockElysia = mock(() => mockApp);
|
||||||
addTool: mock(() => undefined),
|
const mockCors = mock(() => (app: any) => app);
|
||||||
start: mock(() => Promise.resolve())
|
const mockSwagger = mock(() => (app: any) => app);
|
||||||
|
const mockSpeechService = {
|
||||||
|
initialize: mock(() => Promise.resolve()),
|
||||||
|
shutdown: mock(() => Promise.resolve())
|
||||||
};
|
};
|
||||||
const mockLiteMCP = mock((name: string, version: string) => mockLiteMCPInstance);
|
|
||||||
|
|
||||||
// Mock logger
|
// Mock the modules
|
||||||
const mockLogger: MockLogger = {
|
const mockModules = {
|
||||||
info: mock((message: string) => undefined),
|
Elysia: MockElysia,
|
||||||
error: mock((message: string) => undefined),
|
cors: mockCors,
|
||||||
debug: mock((message: string) => undefined)
|
swagger: mockSwagger,
|
||||||
|
speechService: mockSpeechService,
|
||||||
|
config: mock(() => ({})),
|
||||||
|
resolve: mock((...args: string[]) => args.join('/')),
|
||||||
|
z: { object: mock(() => ({})), enum: mock(() => ({})) }
|
||||||
|
};
|
||||||
|
|
||||||
|
// Mock module resolution
|
||||||
|
const mockResolver = {
|
||||||
|
resolve(specifier: string) {
|
||||||
|
const mocks: Record<string, any> = {
|
||||||
|
'elysia': { Elysia: mockModules.Elysia },
|
||||||
|
'@elysiajs/cors': { cors: mockModules.cors },
|
||||||
|
'@elysiajs/swagger': { swagger: mockModules.swagger },
|
||||||
|
'../speech/index.js': { speechService: mockModules.speechService },
|
||||||
|
'dotenv': { config: mockModules.config },
|
||||||
|
'path': { resolve: mockModules.resolve },
|
||||||
|
'zod': { z: mockModules.z }
|
||||||
|
};
|
||||||
|
return mocks[specifier] || {};
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
describe('Server Initialization', () => {
|
describe('Server Initialization', () => {
|
||||||
let originalEnv: NodeJS.ProcessEnv;
|
let originalEnv: NodeJS.ProcessEnv;
|
||||||
|
let consoleLog: Mock<typeof console.log>;
|
||||||
|
let consoleError: Mock<typeof console.error>;
|
||||||
|
let originalResolve: any;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
// Store original environment
|
// Store original environment
|
||||||
originalEnv = { ...process.env };
|
originalEnv = { ...process.env };
|
||||||
|
|
||||||
// Setup mocks
|
// Mock console methods
|
||||||
(globalThis as any).express = mockExpress;
|
consoleLog = mock(() => { });
|
||||||
(globalThis as any).LiteMCP = mockLiteMCP;
|
consoleError = mock(() => { });
|
||||||
(globalThis as any).logger = mockLogger;
|
console.log = consoleLog;
|
||||||
|
console.error = consoleError;
|
||||||
|
|
||||||
// Reset all mocks
|
// Reset all mocks
|
||||||
mockApp.use.mockReset();
|
for (const key in mockModules) {
|
||||||
mockApp.listen.mockReset();
|
const module = mockModules[key as keyof typeof mockModules];
|
||||||
mockLogger.info.mockReset();
|
if (typeof module === 'object' && module !== null) {
|
||||||
mockLogger.error.mockReset();
|
Object.values(module).forEach(value => {
|
||||||
mockLogger.debug.mockReset();
|
if (typeof value === 'function' && 'mock' in value) {
|
||||||
mockLiteMCP.mockReset();
|
(value as Mock<any>).mockReset();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else if (typeof module === 'function' && 'mock' in module) {
|
||||||
|
(module as Mock<any>).mockReset();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set default environment variables
|
||||||
|
process.env.NODE_ENV = 'test';
|
||||||
|
process.env.PORT = '4000';
|
||||||
|
|
||||||
|
// Setup module resolution mock
|
||||||
|
originalResolve = (globalThis as any).Bun?.resolveSync;
|
||||||
|
(globalThis as any).Bun = {
|
||||||
|
...(globalThis as any).Bun,
|
||||||
|
resolveSync: (specifier: string) => mockResolver.resolve(specifier)
|
||||||
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
// Restore original environment
|
// Restore original environment
|
||||||
process.env = originalEnv;
|
process.env = originalEnv;
|
||||||
|
|
||||||
// Clean up mocks
|
// Restore module resolution
|
||||||
delete (globalThis as any).express;
|
if (originalResolve) {
|
||||||
delete (globalThis as any).LiteMCP;
|
(globalThis as any).Bun.resolveSync = originalResolve;
|
||||||
delete (globalThis as any).logger;
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should start Express server when not in Claude mode', async () => {
|
test('should initialize server with middleware', async () => {
|
||||||
// Set OpenAI mode
|
// Import and initialize server
|
||||||
process.env.PROCESSOR_TYPE = 'openai';
|
const mod = await import('../src/index');
|
||||||
|
|
||||||
// Import the main module
|
// Verify server initialization
|
||||||
await import('../src/index.js');
|
expect(MockElysia.mock.calls.length).toBe(1);
|
||||||
|
expect(mockCors.mock.calls.length).toBe(1);
|
||||||
|
expect(mockSwagger.mock.calls.length).toBe(1);
|
||||||
|
|
||||||
// Verify Express server was initialized
|
// Verify console output
|
||||||
expect(mockExpress.mock.calls.length).toBeGreaterThan(0);
|
const logCalls = consoleLog.mock.calls;
|
||||||
expect(mockApp.use.mock.calls.length).toBeGreaterThan(0);
|
expect(logCalls.some(call =>
|
||||||
expect(mockApp.listen.mock.calls.length).toBeGreaterThan(0);
|
typeof call.args[0] === 'string' &&
|
||||||
|
call.args[0].includes('Server is running on port')
|
||||||
const infoMessages = mockLogger.info.mock.calls.map(([msg]) => msg);
|
)).toBe(true);
|
||||||
expect(infoMessages.some(msg => msg.includes('Server is running on port'))).toBe(true);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should not start Express server in Claude mode', async () => {
|
test('should initialize speech service when enabled', async () => {
|
||||||
// Set Claude mode
|
// Enable speech service
|
||||||
process.env.PROCESSOR_TYPE = 'claude';
|
process.env.SPEECH_ENABLED = 'true';
|
||||||
|
|
||||||
// Import the main module
|
// Import and initialize server
|
||||||
await import('../src/index.js');
|
const mod = await import('../src/index');
|
||||||
|
|
||||||
// Verify Express server was not initialized
|
// Verify speech service initialization
|
||||||
expect(mockExpress.mock.calls.length).toBe(0);
|
expect(mockSpeechService.initialize.mock.calls.length).toBe(1);
|
||||||
expect(mockApp.use.mock.calls.length).toBe(0);
|
|
||||||
expect(mockApp.listen.mock.calls.length).toBe(0);
|
|
||||||
|
|
||||||
const infoMessages = mockLogger.info.mock.calls.map(([msg]) => msg);
|
|
||||||
expect(infoMessages).toContain('Running in Claude mode - Express server disabled');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should initialize LiteMCP in both modes', async () => {
|
test('should handle server shutdown gracefully', async () => {
|
||||||
// Test OpenAI mode
|
// Enable speech service for shutdown test
|
||||||
process.env.PROCESSOR_TYPE = 'openai';
|
process.env.SPEECH_ENABLED = 'true';
|
||||||
await import('../src/index.js');
|
|
||||||
|
|
||||||
expect(mockLiteMCP.mock.calls.length).toBeGreaterThan(0);
|
// Import and initialize server
|
||||||
const [name, version] = mockLiteMCP.mock.calls[0] ?? [];
|
const mod = await import('../src/index');
|
||||||
expect(name).toBe('home-assistant');
|
|
||||||
expect(typeof version).toBe('string');
|
|
||||||
|
|
||||||
// Reset for next test
|
// Simulate SIGTERM
|
||||||
mockLiteMCP.mockReset();
|
process.emit('SIGTERM');
|
||||||
|
|
||||||
// Test Claude mode
|
// Verify shutdown behavior
|
||||||
process.env.PROCESSOR_TYPE = 'claude';
|
expect(mockSpeechService.shutdown.mock.calls.length).toBe(1);
|
||||||
await import('../src/index.js');
|
expect(consoleLog.mock.calls.some(call =>
|
||||||
|
typeof call.args[0] === 'string' &&
|
||||||
expect(mockLiteMCP.mock.calls.length).toBeGreaterThan(0);
|
call.args[0].includes('Shutting down gracefully')
|
||||||
const [name2, version2] = mockLiteMCP.mock.calls[0] ?? [];
|
)).toBe(true);
|
||||||
expect(name2).toBe('home-assistant');
|
|
||||||
expect(typeof version2).toBe('string');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('should handle missing PROCESSOR_TYPE (default to Express server)', async () => {
|
|
||||||
// Remove PROCESSOR_TYPE
|
|
||||||
delete process.env.PROCESSOR_TYPE;
|
|
||||||
|
|
||||||
// Import the main module
|
|
||||||
await import('../src/index.js');
|
|
||||||
|
|
||||||
// Verify Express server was initialized (default behavior)
|
|
||||||
expect(mockExpress.mock.calls.length).toBeGreaterThan(0);
|
|
||||||
expect(mockApp.use.mock.calls.length).toBeGreaterThan(0);
|
|
||||||
expect(mockApp.listen.mock.calls.length).toBeGreaterThan(0);
|
|
||||||
|
|
||||||
const infoMessages = mockLogger.info.mock.calls.map(([msg]) => msg);
|
|
||||||
expect(infoMessages.some(msg => msg.includes('Server is running on port'))).toBe(true);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -1,81 +1,79 @@
|
|||||||
import { describe, expect, test } from "bun:test";
|
import { describe, expect, test, beforeEach, afterEach, mock, spyOn } from "bun:test";
|
||||||
import { SpeechToText, TranscriptionResult, WakeWordEvent, TranscriptionError, TranscriptionOptions } from '../../src/speech/speechToText';
|
import type { Mock } from "bun:test";
|
||||||
import { EventEmitter } from 'events';
|
import { EventEmitter } from "events";
|
||||||
import fs from 'fs';
|
import { SpeechToText, TranscriptionError, type TranscriptionOptions } from "../../src/speech/speechToText";
|
||||||
import path from 'path';
|
import type { SpeechToTextConfig } from "../../src/speech/types";
|
||||||
import { spawn } from 'child_process';
|
import type { ChildProcess } from "child_process";
|
||||||
import { describe, expect, beforeEach, afterEach, it, mock, spyOn } from 'bun:test';
|
|
||||||
|
|
||||||
// Mock child_process spawn
|
interface MockProcess extends EventEmitter {
|
||||||
const spawnMock = mock((cmd: string, args: string[]) => ({
|
stdout: EventEmitter;
|
||||||
stdout: new EventEmitter(),
|
stderr: EventEmitter;
|
||||||
stderr: new EventEmitter(),
|
kill: Mock<() => void>;
|
||||||
on: (event: string, cb: (code: number) => void) => {
|
}
|
||||||
if (event === 'close') setTimeout(() => cb(0), 0);
|
|
||||||
}
|
type SpawnFn = {
|
||||||
}));
|
(cmds: string[], options?: Record<string, unknown>): ChildProcess;
|
||||||
|
};
|
||||||
|
|
||||||
describe('SpeechToText', () => {
|
describe('SpeechToText', () => {
|
||||||
|
let spawnMock: Mock<SpawnFn>;
|
||||||
|
let mockProcess: MockProcess;
|
||||||
let speechToText: SpeechToText;
|
let speechToText: SpeechToText;
|
||||||
const testAudioDir = path.join(import.meta.dir, 'test_audio');
|
|
||||||
const mockConfig = {
|
|
||||||
containerName: 'test-whisper',
|
|
||||||
modelPath: '/models/whisper',
|
|
||||||
modelType: 'base.en'
|
|
||||||
};
|
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
speechToText = new SpeechToText(mockConfig);
|
// Create mock process
|
||||||
// Create test audio directory if it doesn't exist
|
mockProcess = new EventEmitter() as MockProcess;
|
||||||
if (!fs.existsSync(testAudioDir)) {
|
mockProcess.stdout = new EventEmitter();
|
||||||
fs.mkdirSync(testAudioDir, { recursive: true });
|
mockProcess.stderr = new EventEmitter();
|
||||||
}
|
mockProcess.kill = mock(() => { });
|
||||||
// Reset spawn mock
|
|
||||||
spawnMock.mockReset();
|
// Create spawn mock
|
||||||
|
spawnMock = mock((cmds: string[], options?: Record<string, unknown>) => mockProcess as unknown as ChildProcess);
|
||||||
|
(globalThis as any).Bun = { spawn: spawnMock };
|
||||||
|
|
||||||
|
// Initialize SpeechToText
|
||||||
|
const config: SpeechToTextConfig = {
|
||||||
|
modelPath: '/test/model',
|
||||||
|
modelType: 'base.en',
|
||||||
|
containerName: 'test-container'
|
||||||
|
};
|
||||||
|
speechToText = new SpeechToText(config);
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
speechToText.stopWakeWordDetection();
|
// Cleanup
|
||||||
// Clean up test files
|
mockProcess.removeAllListeners();
|
||||||
if (fs.existsSync(testAudioDir)) {
|
mockProcess.stdout.removeAllListeners();
|
||||||
fs.rmSync(testAudioDir, { recursive: true, force: true });
|
mockProcess.stderr.removeAllListeners();
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Initialization', () => {
|
describe('Initialization', () => {
|
||||||
test('should create instance with default config', () => {
|
test('should create instance with default config', () => {
|
||||||
const instance = new SpeechToText({ modelPath: '/models/whisper', modelType: 'base.en' });
|
const config: SpeechToTextConfig = {
|
||||||
expect(instance instanceof EventEmitter).toBe(true);
|
modelPath: '/test/model',
|
||||||
expect(instance instanceof SpeechToText).toBe(true);
|
modelType: 'base.en'
|
||||||
|
};
|
||||||
|
const instance = new SpeechToText(config);
|
||||||
|
expect(instance).toBeDefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should initialize successfully', async () => {
|
test('should initialize successfully', async () => {
|
||||||
const initSpy = spyOn(speechToText, 'initialize');
|
const result = await speechToText.initialize();
|
||||||
await speechToText.initialize();
|
expect(result).toBeUndefined();
|
||||||
expect(initSpy).toHaveBeenCalled();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should not initialize twice', async () => {
|
test('should not initialize twice', async () => {
|
||||||
await speechToText.initialize();
|
await speechToText.initialize();
|
||||||
const initSpy = spyOn(speechToText, 'initialize');
|
const result = await speechToText.initialize();
|
||||||
await speechToText.initialize();
|
expect(result).toBeUndefined();
|
||||||
expect(initSpy.mock.calls.length).toBe(1);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Health Check', () => {
|
describe('Health Check', () => {
|
||||||
test('should return true when Docker container is running', async () => {
|
test('should return true when Docker container is running', async () => {
|
||||||
const mockProcess = {
|
// Setup mock process
|
||||||
stdout: new EventEmitter(),
|
|
||||||
stderr: new EventEmitter(),
|
|
||||||
on: (event: string, cb: (code: number) => void) => {
|
|
||||||
if (event === 'close') setTimeout(() => cb(0), 0);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
spawnMock.mockImplementation(() => mockProcess);
|
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
mockProcess.stdout.emtest('data', Buffer.from('Up 2 hours'));
|
mockProcess.stdout.emit('data', Buffer.from('Up 2 hours'));
|
||||||
}, 0);
|
}, 0);
|
||||||
|
|
||||||
const result = await speechToText.checkHealth();
|
const result = await speechToText.checkHealth();
|
||||||
@@ -83,23 +81,20 @@ describe('SpeechToText', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('should return false when Docker container is not running', async () => {
|
test('should return false when Docker container is not running', async () => {
|
||||||
const mockProcess = {
|
// Setup mock process
|
||||||
stdout: new EventEmitter(),
|
setTimeout(() => {
|
||||||
stderr: new EventEmitter(),
|
mockProcess.stdout.emit('data', Buffer.from('No containers found'));
|
||||||
on: (event: string, cb: (code: number) => void) => {
|
}, 0);
|
||||||
if (event === 'close') setTimeout(() => cb(1), 0);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
spawnMock.mockImplementation(() => mockProcess);
|
|
||||||
|
|
||||||
const result = await speechToText.checkHealth();
|
const result = await speechToText.checkHealth();
|
||||||
expect(result).toBe(false);
|
expect(result).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should handle Docker command errors', async () => {
|
test('should handle Docker command errors', async () => {
|
||||||
spawnMock.mockImplementation(() => {
|
// Setup mock process
|
||||||
throw new Error('Docker not found');
|
setTimeout(() => {
|
||||||
});
|
mockProcess.stderr.emit('data', Buffer.from('Docker error'));
|
||||||
|
}, 0);
|
||||||
|
|
||||||
const result = await speechToText.checkHealth();
|
const result = await speechToText.checkHealth();
|
||||||
expect(result).toBe(false);
|
expect(result).toBe(false);
|
||||||
@@ -108,51 +103,48 @@ describe('SpeechToText', () => {
|
|||||||
|
|
||||||
describe('Wake Word Detection', () => {
|
describe('Wake Word Detection', () => {
|
||||||
test('should detect wake word and emit event', async () => {
|
test('should detect wake word and emit event', async () => {
|
||||||
const testFile = path.join(testAudioDir, 'wake_word_test_123456.wav');
|
// Setup mock process
|
||||||
const testMetadata = `${testFile}.json`;
|
setTimeout(() => {
|
||||||
|
mockProcess.stdout.emit('data', Buffer.from('Wake word detected'));
|
||||||
|
}, 0);
|
||||||
|
|
||||||
return new Promise<void>((resolve) => {
|
const wakeWordPromise = new Promise<void>((resolve) => {
|
||||||
speechToText.startWakeWordDetection(testAudioDir);
|
speechToText.on('wake_word', () => {
|
||||||
|
|
||||||
speechToText.on('wake_word', (event: WakeWordEvent) => {
|
|
||||||
expect(event).toBeDefined();
|
|
||||||
expect(event.audioFile).toBe(testFile);
|
|
||||||
expect(event.metadataFile).toBe(testMetadata);
|
|
||||||
expect(event.timestamp).toBe('123456');
|
|
||||||
resolve();
|
resolve();
|
||||||
});
|
});
|
||||||
|
|
||||||
// Create a test audio file to trigger the event
|
|
||||||
fs.writeFileSync(testFile, 'test audio content');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
speechToText.startWakeWordDetection();
|
||||||
|
await wakeWordPromise;
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should handle non-wake-word files', async () => {
|
test('should handle non-wake-word files', async () => {
|
||||||
const testFile = path.join(testAudioDir, 'regular_audio.wav');
|
// Setup mock process
|
||||||
let eventEmitted = false;
|
setTimeout(() => {
|
||||||
|
mockProcess.stdout.emit('data', Buffer.from('Processing audio'));
|
||||||
|
}, 0);
|
||||||
|
|
||||||
return new Promise<void>((resolve) => {
|
const wakeWordPromise = new Promise<void>((resolve, reject) => {
|
||||||
speechToText.startWakeWordDetection(testAudioDir);
|
const timeout = setTimeout(() => {
|
||||||
|
|
||||||
speechToText.on('wake_word', () => {
|
|
||||||
eventEmitted = true;
|
|
||||||
});
|
|
||||||
|
|
||||||
fs.writeFileSync(testFile, 'test audio content');
|
|
||||||
|
|
||||||
setTimeout(() => {
|
|
||||||
expect(eventEmitted).toBe(false);
|
|
||||||
resolve();
|
resolve();
|
||||||
}, 100);
|
}, 100);
|
||||||
|
|
||||||
|
speechToText.on('wake_word', () => {
|
||||||
|
clearTimeout(timeout);
|
||||||
|
reject(new Error('Wake word should not be detected'));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
speechToText.startWakeWordDetection();
|
||||||
|
await wakeWordPromise;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Audio Transcription', () => {
|
describe('Audio Transcription', () => {
|
||||||
const mockTranscriptionResult: TranscriptionResult = {
|
const mockTranscriptionResult = {
|
||||||
text: 'Hello world',
|
text: 'Test transcription',
|
||||||
segments: [{
|
segments: [{
|
||||||
text: 'Hello world',
|
text: 'Test transcription',
|
||||||
start: 0,
|
start: 0,
|
||||||
end: 1,
|
end: 1,
|
||||||
confidence: 0.95
|
confidence: 0.95
|
||||||
@@ -160,169 +152,100 @@ describe('SpeechToText', () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
test('should transcribe audio successfully', async () => {
|
test('should transcribe audio successfully', async () => {
|
||||||
const mockProcess = {
|
// Setup mock process
|
||||||
stdout: new EventEmitter(),
|
|
||||||
stderr: new EventEmitter(),
|
|
||||||
on: (event: string, cb: (code: number) => void) => {
|
|
||||||
if (event === 'close') setTimeout(() => cb(0), 0);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
spawnMock.mockImplementation(() => mockProcess);
|
|
||||||
|
|
||||||
const transcriptionPromise = speechToText.transcribeAudio('/test/audio.wav');
|
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
mockProcess.stdout.emtest('data', Buffer.from(JSON.stringify(mockTranscriptionResult)));
|
mockProcess.stdout.emit('data', Buffer.from(JSON.stringify(mockTranscriptionResult)));
|
||||||
}, 0);
|
}, 0);
|
||||||
|
|
||||||
const result = await transcriptionPromise;
|
const result = await speechToText.transcribeAudio('/test/audio.wav');
|
||||||
expect(result).toEqual(mockTranscriptionResult);
|
expect(result).toEqual(mockTranscriptionResult);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should handle transcription errors', async () => {
|
test('should handle transcription errors', async () => {
|
||||||
const mockProcess = {
|
// Setup mock process
|
||||||
stdout: new EventEmitter(),
|
|
||||||
stderr: new EventEmitter(),
|
|
||||||
on: (event: string, cb: (code: number) => void) => {
|
|
||||||
if (event === 'close') setTimeout(() => cb(1), 0);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
spawnMock.mockImplementation(() => mockProcess);
|
|
||||||
|
|
||||||
const transcriptionPromise = speechToText.transcribeAudio('/test/audio.wav');
|
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
mockProcess.stderr.emtest('data', Buffer.from('Transcription failed'));
|
mockProcess.stderr.emit('data', Buffer.from('Transcription failed'));
|
||||||
}, 0);
|
}, 0);
|
||||||
|
|
||||||
await expect(transcriptionPromise).rejects.toThrow(TranscriptionError);
|
await expect(speechToText.transcribeAudio('/test/audio.wav')).rejects.toThrow(TranscriptionError);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should handle invalid JSON output', async () => {
|
test('should handle invalid JSON output', async () => {
|
||||||
const mockProcess = {
|
// Setup mock process
|
||||||
stdout: new EventEmitter(),
|
|
||||||
stderr: new EventEmitter(),
|
|
||||||
on: (event: string, cb: (code: number) => void) => {
|
|
||||||
if (event === 'close') setTimeout(() => cb(0), 0);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
spawnMock.mockImplementation(() => mockProcess);
|
|
||||||
|
|
||||||
const transcriptionPromise = speechToText.transcribeAudio('/test/audio.wav');
|
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
mockProcess.stdout.emtest('data', Buffer.from('Invalid JSON'));
|
mockProcess.stdout.emit('data', Buffer.from('Invalid JSON'));
|
||||||
}, 0);
|
}, 0);
|
||||||
|
|
||||||
await expect(transcriptionPromise).rejects.toThrow(TranscriptionError);
|
await expect(speechToText.transcribeAudio('/test/audio.wav')).rejects.toThrow(TranscriptionError);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should pass correct transcription options', async () => {
|
test('should pass correct transcription options', async () => {
|
||||||
const options: TranscriptionOptions = {
|
const options: TranscriptionOptions = {
|
||||||
model: 'large-v2',
|
model: 'base.en',
|
||||||
language: 'en',
|
language: 'en',
|
||||||
temperature: 0.5,
|
temperature: 0,
|
||||||
beamSize: 3,
|
beamSize: 5,
|
||||||
patience: 2,
|
patience: 1,
|
||||||
device: 'cuda'
|
device: 'cpu'
|
||||||
};
|
};
|
||||||
|
|
||||||
const mockProcess = {
|
await speechToText.transcribeAudio('/test/audio.wav', options);
|
||||||
stdout: new EventEmitter(),
|
|
||||||
stderr: new EventEmitter(),
|
|
||||||
on: (event: string, cb: (code: number) => void) => {
|
|
||||||
if (event === 'close') setTimeout(() => cb(0), 0);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
spawnMock.mockImplementation(() => mockProcess);
|
|
||||||
|
|
||||||
const transcriptionPromise = speechToText.transcribeAudio('/test/audio.wav', options);
|
const spawnArgs = spawnMock.mock.calls[0]?.args[1] || [];
|
||||||
|
expect(spawnArgs).toContain('--model');
|
||||||
const expectedArgs = [
|
expect(spawnArgs).toContain(options.model);
|
||||||
'exec',
|
expect(spawnArgs).toContain('--language');
|
||||||
mockConfig.containerName,
|
expect(spawnArgs).toContain(options.language);
|
||||||
'fast-whisper',
|
expect(spawnArgs).toContain('--temperature');
|
||||||
'--model', options.model,
|
expect(spawnArgs).toContain(options.temperature?.toString());
|
||||||
'--language', options.language,
|
expect(spawnArgs).toContain('--beam-size');
|
||||||
'--temperature', String(options.temperature ?? 0),
|
expect(spawnArgs).toContain(options.beamSize?.toString());
|
||||||
'--beam-size', String(options.beamSize ?? 5),
|
expect(spawnArgs).toContain('--patience');
|
||||||
'--patience', String(options.patience ?? 1),
|
expect(spawnArgs).toContain(options.patience?.toString());
|
||||||
'--device', options.device
|
expect(spawnArgs).toContain('--device');
|
||||||
].filter((arg): arg is string => arg !== undefined);
|
expect(spawnArgs).toContain(options.device);
|
||||||
|
|
||||||
const mockCalls = spawnMock.mock.calls;
|
|
||||||
expect(mockCalls.length).toBe(1);
|
|
||||||
const [cmd, args] = mockCalls[0].args;
|
|
||||||
expect(cmd).toBe('docker');
|
|
||||||
expect(expectedArgs.every(arg => args.includes(arg))).toBe(true);
|
|
||||||
|
|
||||||
await transcriptionPromise.catch(() => { });
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Event Handling', () => {
|
describe('Event Handling', () => {
|
||||||
test('should emit progress events', async () => {
|
test('should emit progress events', async () => {
|
||||||
const mockProcess = {
|
const progressPromise = new Promise<void>((resolve) => {
|
||||||
stdout: new EventEmitter(),
|
speechToText.on('progress', (progress) => {
|
||||||
stderr: new EventEmitter(),
|
expect(progress).toEqual({ type: 'stdout', data: 'Processing' });
|
||||||
on: (event: string, cb: (code: number) => void) => {
|
resolve();
|
||||||
if (event === 'close') setTimeout(() => cb(0), 0);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
spawnMock.mockImplementation(() => mockProcess);
|
|
||||||
|
|
||||||
return new Promise<void>((resolve) => {
|
|
||||||
const progressEvents: any[] = [];
|
|
||||||
speechToText.on('progress', (event) => {
|
|
||||||
progressEvents.push(event);
|
|
||||||
if (progressEvents.length === 2) {
|
|
||||||
expect(progressEvents).toEqual([
|
|
||||||
{ type: 'stdout', data: 'Processing' },
|
|
||||||
{ type: 'stderr', data: 'Loading model' }
|
|
||||||
]);
|
|
||||||
resolve();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
void speechToText.transcribeAudio('/test/audio.wav');
|
|
||||||
|
|
||||||
mockProcess.stdout.emtest('data', Buffer.from('Processing'));
|
|
||||||
mockProcess.stderr.emtest('data', Buffer.from('Loading model'));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const transcribePromise = speechToText.transcribeAudio('/test/audio.wav');
|
||||||
|
mockProcess.stdout.emit('data', Buffer.from('Processing'));
|
||||||
|
await Promise.all([transcribePromise.catch(() => { }), progressPromise]);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should emit error events', async () => {
|
test('should emit error events', async () => {
|
||||||
return new Promise<void>((resolve) => {
|
const errorPromise = new Promise<void>((resolve) => {
|
||||||
speechToText.on('error', (error) => {
|
speechToText.on('error', (error) => {
|
||||||
expect(error instanceof Error).toBe(true);
|
expect(error instanceof Error).toBe(true);
|
||||||
expect(error.message).toBe('Test error');
|
expect(error.message).toBe('Test error');
|
||||||
resolve();
|
resolve();
|
||||||
});
|
});
|
||||||
|
|
||||||
speechToText.emtest('error', new Error('Test error'));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
speechToText.emit('error', new Error('Test error'));
|
||||||
|
await errorPromise;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Cleanup', () => {
|
describe('Cleanup', () => {
|
||||||
test('should stop wake word detection', () => {
|
test('should stop wake word detection', () => {
|
||||||
speechToText.startWakeWordDetection(testAudioDir);
|
speechToText.startWakeWordDetection();
|
||||||
speechToText.stopWakeWordDetection();
|
speechToText.stopWakeWordDetection();
|
||||||
// Verify no more file watching events are processed
|
expect(mockProcess.kill.mock.calls.length).toBe(1);
|
||||||
const testFile = path.join(testAudioDir, 'wake_word_test_123456.wav');
|
|
||||||
let eventEmitted = false;
|
|
||||||
speechToText.on('wake_word', () => {
|
|
||||||
eventEmitted = true;
|
|
||||||
});
|
|
||||||
fs.writeFileSync(testFile, 'test audio content');
|
|
||||||
expect(eventEmitted).toBe(false);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should clean up resources on shutdown', async () => {
|
test('should clean up resources on shutdown', async () => {
|
||||||
await speechToText.initialize();
|
await speechToText.initialize();
|
||||||
const shutdownSpy = spyOn(speechToText, 'shutdown');
|
|
||||||
await speechToText.shutdown();
|
await speechToText.shutdown();
|
||||||
expect(shutdownSpy).toHaveBeenCalled();
|
expect(mockProcess.kill.mock.calls.length).toBe(1);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -1,120 +1,182 @@
|
|||||||
import { describe, expect, test } from "bun:test";
|
import { describe, expect, test, beforeEach, afterEach, mock } from "bun:test";
|
||||||
import { jest, describe, it, expect, beforeEach, afterEach } from '@jest/globals';
|
import { EventEmitter } from "events";
|
||||||
import { HassWebSocketClient } from '../../src/websocket/client.js';
|
import { HassWebSocketClient } from "../../src/websocket/client";
|
||||||
import WebSocket from 'ws';
|
import type { MessageEvent, ErrorEvent } from "ws";
|
||||||
import { EventEmitter } from 'events';
|
import { Mock, fn as jestMock } from 'jest-mock';
|
||||||
import * as HomeAssistant from '../../src/types/hass.js';
|
import { expect as jestExpect } from '@jest/globals';
|
||||||
|
|
||||||
// Mock WebSocket
|
|
||||||
// // jest.mock('ws');
|
|
||||||
|
|
||||||
describe('WebSocket Event Handling', () => {
|
describe('WebSocket Event Handling', () => {
|
||||||
let client: HassWebSocketClient;
|
let client: HassWebSocketClient;
|
||||||
let mockWebSocket: jest.Mocked<WebSocket>;
|
let mockWebSocket: any;
|
||||||
|
let onOpenCallback: () => void;
|
||||||
|
let onCloseCallback: () => void;
|
||||||
|
let onErrorCallback: (event: any) => void;
|
||||||
|
let onMessageCallback: (event: any) => void;
|
||||||
let eventEmitter: EventEmitter;
|
let eventEmitter: EventEmitter;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
// Clear all mocks
|
|
||||||
jest.clearAllMocks();
|
|
||||||
|
|
||||||
// Create event emitter for mocking WebSocket events
|
|
||||||
eventEmitter = new EventEmitter();
|
eventEmitter = new EventEmitter();
|
||||||
|
|
||||||
// Create mock WebSocket instance
|
// Initialize callbacks first
|
||||||
|
onOpenCallback = () => { };
|
||||||
|
onCloseCallback = () => { };
|
||||||
|
onErrorCallback = () => { };
|
||||||
|
onMessageCallback = () => { };
|
||||||
|
|
||||||
mockWebSocket = {
|
mockWebSocket = {
|
||||||
on: jest.fn((event: string, listener: (...args: any[]) => void) => {
|
|
||||||
eventEmitter.on(event, listener);
|
|
||||||
return mockWebSocket;
|
|
||||||
}),
|
|
||||||
send: mock(),
|
send: mock(),
|
||||||
close: mock(),
|
close: mock(),
|
||||||
readyState: WebSocket.OPEN,
|
readyState: 1,
|
||||||
removeAllListeners: mock(),
|
OPEN: 1,
|
||||||
// Add required WebSocket properties
|
onopen: null,
|
||||||
binaryType: 'arraybuffer',
|
onclose: null,
|
||||||
bufferedAmount: 0,
|
onerror: null,
|
||||||
extensions: '',
|
onmessage: null
|
||||||
protocol: '',
|
};
|
||||||
url: 'ws://test.com',
|
|
||||||
isPaused: () => false,
|
|
||||||
ping: mock(),
|
|
||||||
pong: mock(),
|
|
||||||
terminate: mock()
|
|
||||||
} as unknown as jest.Mocked<WebSocket>;
|
|
||||||
|
|
||||||
// Mock WebSocket constructor
|
// Define setters that store the callbacks
|
||||||
(WebSocket as unknown as jest.Mock).mockImplementation(() => mockWebSocket);
|
Object.defineProperties(mockWebSocket, {
|
||||||
|
onopen: {
|
||||||
|
get() { return onOpenCallback; },
|
||||||
|
set(callback: () => void) { onOpenCallback = callback; }
|
||||||
|
},
|
||||||
|
onclose: {
|
||||||
|
get() { return onCloseCallback; },
|
||||||
|
set(callback: () => void) { onCloseCallback = callback; }
|
||||||
|
},
|
||||||
|
onerror: {
|
||||||
|
get() { return onErrorCallback; },
|
||||||
|
set(callback: (event: any) => void) { onErrorCallback = callback; }
|
||||||
|
},
|
||||||
|
onmessage: {
|
||||||
|
get() { return onMessageCallback; },
|
||||||
|
set(callback: (event: any) => void) { onMessageCallback = callback; }
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// Create client instance
|
// @ts-expect-error - Mock WebSocket implementation
|
||||||
client = new HassWebSocketClient('ws://test.com', 'test-token');
|
global.WebSocket = mock(() => mockWebSocket);
|
||||||
|
|
||||||
|
client = new HassWebSocketClient('ws://localhost:8123/api/websocket', 'test-token');
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
eventEmitter.removeAllListeners();
|
if (eventEmitter) {
|
||||||
client.disconnect();
|
eventEmitter.removeAllListeners();
|
||||||
|
}
|
||||||
|
if (client) {
|
||||||
|
client.disconnect();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should handle connection events', () => {
|
test('should handle connection events', async () => {
|
||||||
// Simulate open event
|
const connectPromise = client.connect();
|
||||||
eventEmitter.emtest('open');
|
onOpenCallback();
|
||||||
|
await connectPromise;
|
||||||
// Verify authentication message was sent
|
expect(client.isConnected()).toBe(true);
|
||||||
expect(mockWebSocket.send).toHaveBeenCalledWith(
|
|
||||||
expect.stringContaining('"type":"auth"')
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should handle authentication response', () => {
|
test('should handle authentication response', async () => {
|
||||||
// Simulate auth_ok message
|
const connectPromise = client.connect();
|
||||||
eventEmitter.emtest('message', JSON.stringify({ type: 'auth_ok' }));
|
onOpenCallback();
|
||||||
|
|
||||||
// Verify client is ready for commands
|
onMessageCallback({
|
||||||
expect(mockWebSocket.readyState).toBe(WebSocket.OPEN);
|
data: JSON.stringify({
|
||||||
|
type: 'auth_required'
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
onMessageCallback({
|
||||||
|
data: JSON.stringify({
|
||||||
|
type: 'auth_ok'
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
await connectPromise;
|
||||||
|
expect(client.isAuthenticated()).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should handle auth failure', () => {
|
test('should handle auth failure', async () => {
|
||||||
// Simulate auth_invalid message
|
const connectPromise = client.connect();
|
||||||
eventEmitter.emtest('message', JSON.stringify({
|
onOpenCallback();
|
||||||
type: 'auth_invalid',
|
|
||||||
message: 'Invalid token'
|
|
||||||
}));
|
|
||||||
|
|
||||||
// Verify client attempts to close connection
|
onMessageCallback({
|
||||||
expect(mockWebSocket.close).toHaveBeenCalled();
|
data: JSON.stringify({
|
||||||
|
type: 'auth_required'
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
onMessageCallback({
|
||||||
|
data: JSON.stringify({
|
||||||
|
type: 'auth_invalid',
|
||||||
|
message: 'Invalid password'
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
await expect(connectPromise).rejects.toThrow('Authentication failed');
|
||||||
|
expect(client.isAuthenticated()).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should handle connection errors', () => {
|
test('should handle connection errors', async () => {
|
||||||
// Create error spy
|
const errorPromise = new Promise((resolve) => {
|
||||||
const errorSpy = mock();
|
client.on('error', resolve);
|
||||||
client.on('error', errorSpy);
|
});
|
||||||
|
|
||||||
// Simulate error
|
const connectPromise = client.connect().catch(() => { });
|
||||||
const testError = new Error('Test error');
|
onOpenCallback();
|
||||||
eventEmitter.emtest('error', testError);
|
|
||||||
|
|
||||||
// Verify error was handled
|
const errorEvent = {
|
||||||
expect(errorSpy).toHaveBeenCalledWith(testError);
|
error: new Error('Connection failed'),
|
||||||
|
message: 'Connection failed',
|
||||||
|
target: mockWebSocket
|
||||||
|
};
|
||||||
|
|
||||||
|
onErrorCallback(errorEvent);
|
||||||
|
|
||||||
|
const error = await errorPromise;
|
||||||
|
expect(error).toBeDefined();
|
||||||
|
expect((error as Error).message).toBe('Connection failed');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should handle disconnection', () => {
|
test('should handle disconnection', async () => {
|
||||||
// Create close spy
|
const connectPromise = client.connect();
|
||||||
const closeSpy = mock();
|
onOpenCallback();
|
||||||
client.on('close', closeSpy);
|
await connectPromise;
|
||||||
|
|
||||||
// Simulate close
|
const disconnectPromise = new Promise((resolve) => {
|
||||||
eventEmitter.emtest('close');
|
client.on('disconnected', resolve);
|
||||||
|
});
|
||||||
|
|
||||||
// Verify close was handled
|
onCloseCallback();
|
||||||
expect(closeSpy).toHaveBeenCalled();
|
|
||||||
|
await disconnectPromise;
|
||||||
|
expect(client.isConnected()).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should handle event messages', () => {
|
test('should handle event messages', async () => {
|
||||||
// Create event spy
|
const connectPromise = client.connect();
|
||||||
const eventSpy = mock();
|
onOpenCallback();
|
||||||
client.on('event', eventSpy);
|
|
||||||
|
onMessageCallback({
|
||||||
|
data: JSON.stringify({
|
||||||
|
type: 'auth_required'
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
onMessageCallback({
|
||||||
|
data: JSON.stringify({
|
||||||
|
type: 'auth_ok'
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
await connectPromise;
|
||||||
|
|
||||||
|
const eventPromise = new Promise((resolve) => {
|
||||||
|
client.on('state_changed', resolve);
|
||||||
|
});
|
||||||
|
|
||||||
// Simulate event message
|
|
||||||
const eventData = {
|
const eventData = {
|
||||||
|
id: 1,
|
||||||
type: 'event',
|
type: 'event',
|
||||||
event: {
|
event: {
|
||||||
event_type: 'state_changed',
|
event_type: 'state_changed',
|
||||||
@@ -124,217 +186,63 @@ describe('WebSocket Event Handling', () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
eventEmitter.emtest('message', JSON.stringify(eventData));
|
|
||||||
|
|
||||||
// Verify event was handled
|
onMessageCallback({
|
||||||
expect(eventSpy).toHaveBeenCalledWith(eventData.event);
|
data: JSON.stringify(eventData)
|
||||||
|
});
|
||||||
|
|
||||||
|
const receivedEvent = await eventPromise;
|
||||||
|
expect(receivedEvent).toEqual(eventData.event.data);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Connection Events', () => {
|
test('should subscribe to specific events', async () => {
|
||||||
test('should handle successful connection', (done) => {
|
const connectPromise = client.connect();
|
||||||
client.on('open', () => {
|
onOpenCallback();
|
||||||
expect(mockWebSocket.send).toHaveBeenCalled();
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
|
|
||||||
eventEmitter.emtest('open');
|
onMessageCallback({
|
||||||
|
data: JSON.stringify({
|
||||||
|
type: 'auth_required'
|
||||||
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should handle connection errors', (done) => {
|
onMessageCallback({
|
||||||
const error = new Error('Connection failed');
|
data: JSON.stringify({
|
||||||
client.on('error', (err: Error) => {
|
type: 'auth_ok'
|
||||||
expect(err).toBe(error);
|
})
|
||||||
done();
|
|
||||||
});
|
|
||||||
|
|
||||||
eventEmitter.emtest('error', error);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should handle connection close', (done) => {
|
await connectPromise;
|
||||||
client.on('disconnected', () => {
|
|
||||||
expect(mockWebSocket.close).toHaveBeenCalled();
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
|
|
||||||
eventEmitter.emtest('close');
|
const subscriptionId = await client.subscribeEvents('state_changed', (data) => {
|
||||||
|
// Empty callback for type satisfaction
|
||||||
});
|
});
|
||||||
|
expect(mockWebSocket.send).toHaveBeenCalled();
|
||||||
|
expect(subscriptionId).toBeDefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Authentication', () => {
|
test('should unsubscribe from events', async () => {
|
||||||
test('should send authentication message on connect', () => {
|
const connectPromise = client.connect();
|
||||||
const authMessage: HomeAssistant.AuthMessage = {
|
onOpenCallback();
|
||||||
type: 'auth',
|
|
||||||
access_token: 'test_token'
|
|
||||||
};
|
|
||||||
|
|
||||||
client.connect();
|
onMessageCallback({
|
||||||
expect(mockWebSocket.send).toHaveBeenCalledWith(JSON.stringify(authMessage));
|
data: JSON.stringify({
|
||||||
|
type: 'auth_required'
|
||||||
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should handle successful authentication', (done) => {
|
onMessageCallback({
|
||||||
client.on('auth_ok', () => {
|
data: JSON.stringify({
|
||||||
done();
|
type: 'auth_ok'
|
||||||
});
|
})
|
||||||
|
|
||||||
client.connect();
|
|
||||||
eventEmitter.emtest('message', JSON.stringify({ type: 'auth_ok' }));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should handle authentication failure', (done) => {
|
await connectPromise;
|
||||||
client.on('auth_invalid', () => {
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
|
|
||||||
client.connect();
|
const subscriptionId = await client.subscribeEvents('state_changed', (data) => {
|
||||||
eventEmitter.emtest('message', JSON.stringify({ type: 'auth_invalid' }));
|
// Empty callback for type satisfaction
|
||||||
});
|
});
|
||||||
});
|
await client.unsubscribeEvents(subscriptionId);
|
||||||
|
|
||||||
describe('Event Subscription', () => {
|
expect(mockWebSocket.send).toHaveBeenCalled();
|
||||||
test('should handle state changed events', (done) => {
|
|
||||||
const stateEvent: HomeAssistant.StateChangedEvent = {
|
|
||||||
event_type: 'state_changed',
|
|
||||||
data: {
|
|
||||||
entity_id: 'light.living_room',
|
|
||||||
new_state: {
|
|
||||||
entity_id: 'light.living_room',
|
|
||||||
state: 'on',
|
|
||||||
attributes: { brightness: 255 },
|
|
||||||
last_changed: '2024-01-01T00:00:00Z',
|
|
||||||
last_updated: '2024-01-01T00:00:00Z',
|
|
||||||
context: {
|
|
||||||
id: '123',
|
|
||||||
parent_id: null,
|
|
||||||
user_id: null
|
|
||||||
}
|
|
||||||
},
|
|
||||||
old_state: {
|
|
||||||
entity_id: 'light.living_room',
|
|
||||||
state: 'off',
|
|
||||||
attributes: {},
|
|
||||||
last_changed: '2024-01-01T00:00:00Z',
|
|
||||||
last_updated: '2024-01-01T00:00:00Z',
|
|
||||||
context: {
|
|
||||||
id: '122',
|
|
||||||
parent_id: null,
|
|
||||||
user_id: null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
origin: 'LOCAL',
|
|
||||||
time_fired: '2024-01-01T00:00:00Z',
|
|
||||||
context: {
|
|
||||||
id: '123',
|
|
||||||
parent_id: null,
|
|
||||||
user_id: null
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
client.on('event', (event) => {
|
|
||||||
expect(event.data.entity_id).toBe('light.living_room');
|
|
||||||
expect(event.data.new_state.state).toBe('on');
|
|
||||||
expect(event.data.old_state.state).toBe('off');
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
|
|
||||||
eventEmitter.emtest('message', JSON.stringify({ type: 'event', event: stateEvent }));
|
|
||||||
});
|
|
||||||
|
|
||||||
test('should subscribe to specific events', async () => {
|
|
||||||
const subscriptionId = 1;
|
|
||||||
const callback = mock();
|
|
||||||
|
|
||||||
// Mock successful subscription
|
|
||||||
const subscribePromise = client.subscribeEvents('state_changed', callback);
|
|
||||||
eventEmitter.emtest('message', JSON.stringify({
|
|
||||||
id: 1,
|
|
||||||
type: 'result',
|
|
||||||
success: true
|
|
||||||
}));
|
|
||||||
|
|
||||||
await expect(subscribePromise).resolves.toBe(subscriptionId);
|
|
||||||
|
|
||||||
// Test event handling
|
|
||||||
const eventData = {
|
|
||||||
entity_id: 'light.living_room',
|
|
||||||
state: 'on'
|
|
||||||
};
|
|
||||||
eventEmitter.emtest('message', JSON.stringify({
|
|
||||||
type: 'event',
|
|
||||||
event: {
|
|
||||||
event_type: 'state_changed',
|
|
||||||
data: eventData
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
|
|
||||||
expect(callback).toHaveBeenCalledWith(eventData);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('should unsubscribe from events', async () => {
|
|
||||||
// First subscribe
|
|
||||||
const subscriptionId = await client.subscribeEvents('state_changed', () => { });
|
|
||||||
|
|
||||||
// Then unsubscribe
|
|
||||||
const unsubscribePromise = client.unsubscribeEvents(subscriptionId);
|
|
||||||
eventEmitter.emtest('message', JSON.stringify({
|
|
||||||
id: 2,
|
|
||||||
type: 'result',
|
|
||||||
success: true
|
|
||||||
}));
|
|
||||||
|
|
||||||
await expect(unsubscribePromise).resolves.toBeUndefined();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('Message Handling', () => {
|
|
||||||
test('should handle malformed messages', (done) => {
|
|
||||||
client.on('error', (error: Error) => {
|
|
||||||
expect(error.message).toContain('Unexpected token');
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
|
|
||||||
eventEmitter.emtest('message', 'invalid json');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('should handle unknown message types', (done) => {
|
|
||||||
const unknownMessage = {
|
|
||||||
type: 'unknown_type',
|
|
||||||
data: {}
|
|
||||||
};
|
|
||||||
|
|
||||||
client.on('error', (error: Error) => {
|
|
||||||
expect(error.message).toContain('Unknown message type');
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
|
|
||||||
eventEmitter.emtest('message', JSON.stringify(unknownMessage));
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('Reconnection', () => {
|
|
||||||
test('should attempt to reconnect on connection loss', (done) => {
|
|
||||||
let reconnectAttempts = 0;
|
|
||||||
client.on('disconnected', () => {
|
|
||||||
reconnectAttempts++;
|
|
||||||
if (reconnectAttempts === 1) {
|
|
||||||
expect(WebSocket).toHaveBeenCalledTimes(2);
|
|
||||||
done();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
eventEmitter.emtest('close');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('should re-authenticate after reconnection', (done) => {
|
|
||||||
client.connect();
|
|
||||||
|
|
||||||
client.on('auth_ok', () => {
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
|
|
||||||
eventEmitter.emtest('close');
|
|
||||||
eventEmitter.emtest('open');
|
|
||||||
eventEmitter.emtest('message', JSON.stringify({ type: 'auth_ok' }));
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -4,103 +4,267 @@ This document provides detailed information about configuring the Home Assistant
|
|||||||
|
|
||||||
## Configuration File Structure
|
## Configuration File Structure
|
||||||
|
|
||||||
The MCP Server uses a hierarchical configuration structure:
|
The MCP Server uses environment variables for configuration, with support for different environments (development, test, production):
|
||||||
|
|
||||||
```yaml
|
```bash
|
||||||
server:
|
# .env, .env.development, or .env.test
|
||||||
host: 0.0.0.0
|
PORT=4000
|
||||||
port: 8123
|
NODE_ENV=development
|
||||||
log_level: INFO
|
HASS_HOST=http://192.168.178.63:8123
|
||||||
|
HASS_TOKEN=your_token_here
|
||||||
security:
|
JWT_SECRET=your_secret_key
|
||||||
jwt_secret: YOUR_SECRET_KEY
|
|
||||||
allowed_origins:
|
|
||||||
- http://localhost:3000
|
|
||||||
- https://your-domain.com
|
|
||||||
|
|
||||||
devices:
|
|
||||||
scan_interval: 30
|
|
||||||
default_timeout: 10
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Server Settings
|
## Server Settings
|
||||||
|
|
||||||
### Basic Server Configuration
|
### Basic Server Configuration
|
||||||
- `host`: Server binding address (default: 0.0.0.0)
|
- `PORT`: Server port number (default: 4000)
|
||||||
- `port`: Server port number (default: 8123)
|
- `NODE_ENV`: Environment mode (development, production, test)
|
||||||
- `log_level`: Logging level (INFO, DEBUG, WARNING, ERROR)
|
- `HASS_HOST`: Home Assistant instance URL
|
||||||
|
- `HASS_TOKEN`: Home Assistant long-lived access token
|
||||||
|
|
||||||
### Security Settings
|
### Security Settings
|
||||||
- `jwt_secret`: Secret key for JWT token generation
|
- `JWT_SECRET`: Secret key for JWT token generation
|
||||||
- `allowed_origins`: CORS allowed origins list
|
- `RATE_LIMIT`: Rate limiting configuration
|
||||||
- `ssl_cert`: Path to SSL certificate (optional)
|
- `windowMs`: Time window in milliseconds (default: 15 minutes)
|
||||||
- `ssl_key`: Path to SSL private key (optional)
|
- `max`: Maximum requests per window (default: 100)
|
||||||
|
|
||||||
### Device Management
|
### WebSocket Settings
|
||||||
- `scan_interval`: Device state scan interval in seconds
|
- `SSE`: Server-Sent Events configuration
|
||||||
- `default_timeout`: Default device command timeout
|
- `MAX_CLIENTS`: Maximum concurrent clients (default: 1000)
|
||||||
- `retry_attempts`: Number of retry attempts for failed commands
|
- `PING_INTERVAL`: Keep-alive ping interval in ms (default: 30000)
|
||||||
|
|
||||||
|
### Speech Features (Optional)
|
||||||
|
- `ENABLE_SPEECH_FEATURES`: Enable speech processing features (default: false)
|
||||||
|
- `ENABLE_WAKE_WORD`: Enable wake word detection (default: false)
|
||||||
|
- `ENABLE_SPEECH_TO_TEXT`: Enable speech-to-text conversion (default: false)
|
||||||
|
- `WHISPER_MODEL_PATH`: Path to Whisper models directory (default: /models)
|
||||||
|
- `WHISPER_MODEL_TYPE`: Whisper model type (default: base)
|
||||||
|
- Available models: tiny.en, base.en, small.en, medium.en, large-v2
|
||||||
|
|
||||||
## Environment Variables
|
## Environment Variables
|
||||||
|
|
||||||
Environment variables override configuration file settings:
|
All configuration is managed through environment variables:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
MCP_HOST=0.0.0.0
|
# Server
|
||||||
MCP_PORT=8123
|
PORT=4000
|
||||||
MCP_LOG_LEVEL=INFO
|
NODE_ENV=development
|
||||||
MCP_JWT_SECRET=your-secret-key
|
|
||||||
|
# Home Assistant
|
||||||
|
HASS_HOST=http://your-hass-instance:8123
|
||||||
|
HASS_TOKEN=your_token_here
|
||||||
|
|
||||||
|
# Security
|
||||||
|
JWT_SECRET=your-secret-key
|
||||||
|
|
||||||
|
# Logging
|
||||||
|
LOG_LEVEL=info
|
||||||
|
LOG_DIR=logs
|
||||||
|
LOG_MAX_SIZE=20m
|
||||||
|
LOG_MAX_DAYS=14d
|
||||||
|
LOG_COMPRESS=true
|
||||||
|
LOG_REQUESTS=true
|
||||||
|
|
||||||
|
# Speech Features (Optional)
|
||||||
|
ENABLE_SPEECH_FEATURES=false
|
||||||
|
ENABLE_WAKE_WORD=false
|
||||||
|
ENABLE_SPEECH_TO_TEXT=false
|
||||||
|
WHISPER_MODEL_PATH=/models
|
||||||
|
WHISPER_MODEL_TYPE=base
|
||||||
```
|
```
|
||||||
|
|
||||||
## Advanced Configuration
|
## Advanced Configuration
|
||||||
|
|
||||||
### Rate Limiting
|
### Security Rate Limiting
|
||||||
```yaml
|
Rate limiting is enabled by default to protect against brute force attacks:
|
||||||
rate_limit:
|
|
||||||
enabled: true
|
|
||||||
requests_per_minute: 100
|
|
||||||
burst: 20
|
|
||||||
```
|
|
||||||
|
|
||||||
### Caching
|
```typescript
|
||||||
```yaml
|
RATE_LIMIT: {
|
||||||
cache:
|
windowMs: 15 * 60 * 1000, // 15 minutes
|
||||||
enabled: true
|
max: 100 // limit each IP to 100 requests per window
|
||||||
ttl: 300 # seconds
|
}
|
||||||
max_size: 1000 # entries
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Logging
|
### Logging
|
||||||
```yaml
|
The server uses Bun's built-in logging capabilities with additional configuration:
|
||||||
logging:
|
|
||||||
file: /var/log/mcp-server.log
|
```typescript
|
||||||
max_size: 10MB
|
LOGGING: {
|
||||||
backup_count: 5
|
LEVEL: "info", // debug, info, warn, error
|
||||||
format: "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
|
DIR: "logs",
|
||||||
|
MAX_SIZE: "20m",
|
||||||
|
MAX_DAYS: "14d",
|
||||||
|
COMPRESS: true,
|
||||||
|
TIMESTAMP_FORMAT: "YYYY-MM-DD HH:mm:ss:ms",
|
||||||
|
LOG_REQUESTS: true
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Speech-to-Text Configuration
|
||||||
|
When speech features are enabled, you can configure the following options:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
SPEECH: {
|
||||||
|
ENABLED: false, // Master switch for all speech features
|
||||||
|
WAKE_WORD_ENABLED: false, // Enable wake word detection
|
||||||
|
SPEECH_TO_TEXT_ENABLED: false, // Enable speech-to-text
|
||||||
|
WHISPER_MODEL_PATH: "/models", // Path to Whisper models
|
||||||
|
WHISPER_MODEL_TYPE: "base", // Model type to use
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Available Whisper models:
|
||||||
|
- `tiny.en`: Fastest, lowest accuracy
|
||||||
|
- `base.en`: Good balance of speed and accuracy
|
||||||
|
- `small.en`: Better accuracy, slower
|
||||||
|
- `medium.en`: High accuracy, much slower
|
||||||
|
- `large-v2`: Best accuracy, very slow
|
||||||
|
|
||||||
|
For production deployments, we recommend using system tools like `logrotate` for log management.
|
||||||
|
|
||||||
|
Example logrotate configuration (`/etc/logrotate.d/mcp-server`):
|
||||||
|
```
|
||||||
|
/var/log/mcp-server.log {
|
||||||
|
daily
|
||||||
|
rotate 7
|
||||||
|
compress
|
||||||
|
delaycompress
|
||||||
|
missingok
|
||||||
|
notifempty
|
||||||
|
create 644 mcp mcp
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Best Practices
|
## Best Practices
|
||||||
|
|
||||||
1. Always use environment variables for sensitive information
|
1. Always use environment variables for sensitive information
|
||||||
2. Keep configuration files in a secure location
|
2. Keep .env files secure and never commit them to version control
|
||||||
3. Regularly backup your configuration
|
3. Use different environment files for development, test, and production
|
||||||
4. Use SSL in production environments
|
4. Enable SSL/TLS in production (preferably via reverse proxy)
|
||||||
5. Monitor log files for issues
|
5. Monitor log files for issues
|
||||||
|
6. Regularly rotate logs in production
|
||||||
|
7. Start with smaller Whisper models and upgrade if needed
|
||||||
|
8. Consider GPU acceleration for larger Whisper models
|
||||||
|
|
||||||
## Validation
|
## Validation
|
||||||
|
|
||||||
The server validates configuration on startup:
|
The server validates configuration on startup using Zod schemas:
|
||||||
- Required fields are checked
|
- Required fields are checked (e.g., HASS_TOKEN)
|
||||||
- Value types are verified
|
- Value types are verified
|
||||||
- Ranges are validated
|
- Enums are validated (e.g., LOG_LEVEL, WHISPER_MODEL_TYPE)
|
||||||
- Security settings are assessed
|
- Default values are applied when not specified
|
||||||
|
|
||||||
## Troubleshooting
|
## Troubleshooting
|
||||||
|
|
||||||
Common configuration issues:
|
Common configuration issues:
|
||||||
1. Permission denied accessing files
|
1. Missing required environment variables
|
||||||
2. Invalid YAML syntax
|
2. Invalid environment variable values
|
||||||
3. Missing required fields
|
3. Permission issues with log directories
|
||||||
4. Type mismatches in values
|
4. Rate limiting too restrictive
|
||||||
|
5. Speech model loading failures
|
||||||
|
6. Docker not available for speech features
|
||||||
|
7. Insufficient system resources for larger models
|
||||||
|
|
||||||
See the [Troubleshooting Guide](troubleshooting.md) for solutions.
|
See the [Troubleshooting Guide](troubleshooting.md) for solutions.
|
||||||
|
|
||||||
|
# Configuration Guide
|
||||||
|
|
||||||
|
This document describes all available configuration options for the Home Assistant MCP Server.
|
||||||
|
|
||||||
|
## Environment Variables
|
||||||
|
|
||||||
|
### Required Settings
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Server Configuration
|
||||||
|
PORT=3000 # Server port
|
||||||
|
HOST=localhost # Server host
|
||||||
|
|
||||||
|
# Home Assistant
|
||||||
|
HASS_URL=http://localhost:8123 # Home Assistant URL
|
||||||
|
HASS_TOKEN=your_token # Long-lived access token
|
||||||
|
|
||||||
|
# Security
|
||||||
|
JWT_SECRET=your_secret # JWT signing secret
|
||||||
|
```
|
||||||
|
|
||||||
|
### Optional Settings
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Rate Limiting
|
||||||
|
RATE_LIMIT_WINDOW=60000 # Time window in ms (default: 60000)
|
||||||
|
RATE_LIMIT_MAX=100 # Max requests per window (default: 100)
|
||||||
|
|
||||||
|
# Logging
|
||||||
|
LOG_LEVEL=info # debug, info, warn, error (default: info)
|
||||||
|
LOG_DIR=logs # Log directory (default: logs)
|
||||||
|
LOG_MAX_SIZE=10m # Max log file size (default: 10m)
|
||||||
|
LOG_MAX_FILES=5 # Max number of log files (default: 5)
|
||||||
|
|
||||||
|
# WebSocket/SSE
|
||||||
|
WS_HEARTBEAT=30000 # WebSocket heartbeat interval in ms (default: 30000)
|
||||||
|
SSE_RETRY=3000 # SSE retry interval in ms (default: 3000)
|
||||||
|
|
||||||
|
# Speech Features
|
||||||
|
ENABLE_SPEECH_FEATURES=false # Enable speech processing (default: false)
|
||||||
|
ENABLE_WAKE_WORD=false # Enable wake word detection (default: false)
|
||||||
|
ENABLE_SPEECH_TO_TEXT=false # Enable speech-to-text (default: false)
|
||||||
|
|
||||||
|
# Speech Model Configuration
|
||||||
|
WHISPER_MODEL_PATH=/models # Path to whisper models (default: /models)
|
||||||
|
WHISPER_MODEL_TYPE=base # Model type: tiny|base|small|medium|large-v2 (default: base)
|
||||||
|
WHISPER_LANGUAGE=en # Primary language (default: en)
|
||||||
|
WHISPER_TASK=transcribe # Task type: transcribe|translate (default: transcribe)
|
||||||
|
WHISPER_DEVICE=cuda # Processing device: cpu|cuda (default: cuda if available, else cpu)
|
||||||
|
|
||||||
|
# Wake Word Configuration
|
||||||
|
WAKE_WORDS=hey jarvis,ok google,alexa # Comma-separated wake words (default: hey jarvis)
|
||||||
|
WAKE_WORD_SENSITIVITY=0.5 # Detection sensitivity 0-1 (default: 0.5)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Speech Features
|
||||||
|
|
||||||
|
### Model Selection
|
||||||
|
|
||||||
|
Choose a model based on your needs:
|
||||||
|
|
||||||
|
| Model | Size | Memory Required | Speed | Accuracy |
|
||||||
|
|------------|-------|-----------------|-------|----------|
|
||||||
|
| tiny.en | 75MB | 1GB | Fast | Basic |
|
||||||
|
| base.en | 150MB | 2GB | Good | Good |
|
||||||
|
| small.en | 500MB | 4GB | Med | Better |
|
||||||
|
| medium.en | 1.5GB | 8GB | Slow | High |
|
||||||
|
| large-v2 | 3GB | 16GB | Slow | Best |
|
||||||
|
|
||||||
|
### GPU Acceleration
|
||||||
|
|
||||||
|
When `WHISPER_DEVICE=cuda`:
|
||||||
|
- NVIDIA GPU with CUDA support required
|
||||||
|
- Significantly faster processing
|
||||||
|
- Higher memory requirements
|
||||||
|
|
||||||
|
### Wake Word Detection
|
||||||
|
|
||||||
|
- Multiple wake words supported via comma-separated list
|
||||||
|
- Adjustable sensitivity (0-1):
|
||||||
|
- Lower values: Fewer false positives, may miss some triggers
|
||||||
|
- Higher values: More responsive, may have false triggers
|
||||||
|
- Default (0.5): Balanced detection
|
||||||
|
|
||||||
|
### Best Practices
|
||||||
|
|
||||||
|
1. Model Selection:
|
||||||
|
- Start with `base.en` model
|
||||||
|
- Upgrade if better accuracy needed
|
||||||
|
- Downgrade if performance issues
|
||||||
|
|
||||||
|
2. Resource Management:
|
||||||
|
- Monitor memory usage
|
||||||
|
- Use GPU acceleration when available
|
||||||
|
- Consider model size vs available resources
|
||||||
|
|
||||||
|
3. Wake Word Configuration:
|
||||||
|
- Use distinct wake words
|
||||||
|
- Adjust sensitivity based on environment
|
||||||
|
- Limit number of wake words for better performance
|
||||||
212
docs/features/speech.md
Normal file
212
docs/features/speech.md
Normal file
@@ -0,0 +1,212 @@
|
|||||||
|
# Speech Features
|
||||||
|
|
||||||
|
The Home Assistant MCP Server includes powerful speech processing capabilities powered by fast-whisper and custom wake word detection. This guide explains how to set up and use these features effectively.
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
|
||||||
|
The speech processing system consists of two main components:
|
||||||
|
1. Wake Word Detection - Listens for specific trigger phrases
|
||||||
|
2. Speech-to-Text - Transcribes spoken commands using fast-whisper
|
||||||
|
|
||||||
|
## Setup
|
||||||
|
|
||||||
|
### Prerequisites
|
||||||
|
|
||||||
|
1. Docker environment:
|
||||||
|
```bash
|
||||||
|
docker --version # Should be 20.10.0 or higher
|
||||||
|
```
|
||||||
|
|
||||||
|
2. For GPU acceleration:
|
||||||
|
- NVIDIA GPU with CUDA support
|
||||||
|
- NVIDIA Container Toolkit installed
|
||||||
|
- NVIDIA drivers 450.80.02 or higher
|
||||||
|
|
||||||
|
### Installation
|
||||||
|
|
||||||
|
1. Enable speech features in your `.env`:
|
||||||
|
```bash
|
||||||
|
ENABLE_SPEECH_FEATURES=true
|
||||||
|
ENABLE_WAKE_WORD=true
|
||||||
|
ENABLE_SPEECH_TO_TEXT=true
|
||||||
|
```
|
||||||
|
|
||||||
|
2. Configure model settings:
|
||||||
|
```bash
|
||||||
|
WHISPER_MODEL_PATH=/models
|
||||||
|
WHISPER_MODEL_TYPE=base
|
||||||
|
WHISPER_LANGUAGE=en
|
||||||
|
WHISPER_TASK=transcribe
|
||||||
|
WHISPER_DEVICE=cuda # or cpu
|
||||||
|
```
|
||||||
|
|
||||||
|
3. Start the services:
|
||||||
|
```bash
|
||||||
|
docker-compose up -d
|
||||||
|
```
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
### Wake Word Detection
|
||||||
|
|
||||||
|
The wake word detector continuously listens for configured trigger phrases. Default wake words:
|
||||||
|
- "hey jarvis"
|
||||||
|
- "ok google"
|
||||||
|
- "alexa"
|
||||||
|
|
||||||
|
Custom wake words can be configured:
|
||||||
|
```bash
|
||||||
|
WAKE_WORDS=computer,jarvis,assistant
|
||||||
|
```
|
||||||
|
|
||||||
|
When a wake word is detected:
|
||||||
|
1. The system starts recording audio
|
||||||
|
2. Audio is processed through the speech-to-text pipeline
|
||||||
|
3. The resulting command is processed by the server
|
||||||
|
|
||||||
|
### Speech-to-Text
|
||||||
|
|
||||||
|
#### Automatic Transcription
|
||||||
|
|
||||||
|
After wake word detection:
|
||||||
|
1. Audio is automatically captured (default: 5 seconds)
|
||||||
|
2. The audio is transcribed using the configured whisper model
|
||||||
|
3. The transcribed text is processed as a command
|
||||||
|
|
||||||
|
#### Manual Transcription
|
||||||
|
|
||||||
|
You can also manually transcribe audio using the API:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// Using the TypeScript client
|
||||||
|
import { SpeechService } from '@ha-mcp/client';
|
||||||
|
|
||||||
|
const speech = new SpeechService();
|
||||||
|
|
||||||
|
// Transcribe from audio buffer
|
||||||
|
const buffer = await getAudioBuffer();
|
||||||
|
const text = await speech.transcribe(buffer);
|
||||||
|
|
||||||
|
// Transcribe from file
|
||||||
|
const text = await speech.transcribeFile('command.wav');
|
||||||
|
```
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
// Using the REST API
|
||||||
|
POST /api/speech/transcribe
|
||||||
|
Content-Type: multipart/form-data
|
||||||
|
|
||||||
|
file: <audio file>
|
||||||
|
```
|
||||||
|
|
||||||
|
### Event Handling
|
||||||
|
|
||||||
|
The system emits various events during speech processing:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
speech.on('wakeWord', (word: string) => {
|
||||||
|
console.log(`Wake word detected: ${word}`);
|
||||||
|
});
|
||||||
|
|
||||||
|
speech.on('listening', () => {
|
||||||
|
console.log('Listening for command...');
|
||||||
|
});
|
||||||
|
|
||||||
|
speech.on('transcribing', () => {
|
||||||
|
console.log('Processing speech...');
|
||||||
|
});
|
||||||
|
|
||||||
|
speech.on('transcribed', (text: string) => {
|
||||||
|
console.log(`Transcribed text: ${text}`);
|
||||||
|
});
|
||||||
|
|
||||||
|
speech.on('error', (error: Error) => {
|
||||||
|
console.error('Speech processing error:', error);
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
## Performance Optimization
|
||||||
|
|
||||||
|
### Model Selection
|
||||||
|
|
||||||
|
Choose an appropriate model based on your needs:
|
||||||
|
|
||||||
|
1. Resource-constrained environments:
|
||||||
|
- Use `tiny.en` or `base.en`
|
||||||
|
- Run on CPU if GPU unavailable
|
||||||
|
- Limit concurrent processing
|
||||||
|
|
||||||
|
2. High-accuracy requirements:
|
||||||
|
- Use `small.en` or `medium.en`
|
||||||
|
- Enable GPU acceleration
|
||||||
|
- Increase audio quality
|
||||||
|
|
||||||
|
3. Production environments:
|
||||||
|
- Use `base.en` or `small.en`
|
||||||
|
- Enable GPU acceleration
|
||||||
|
- Configure appropriate timeouts
|
||||||
|
|
||||||
|
### GPU Acceleration
|
||||||
|
|
||||||
|
When using GPU acceleration:
|
||||||
|
|
||||||
|
1. Monitor GPU memory usage:
|
||||||
|
```bash
|
||||||
|
nvidia-smi -l 1
|
||||||
|
```
|
||||||
|
|
||||||
|
2. Adjust model size if needed:
|
||||||
|
```bash
|
||||||
|
WHISPER_MODEL_TYPE=small # Decrease if GPU memory limited
|
||||||
|
```
|
||||||
|
|
||||||
|
3. Configure processing device:
|
||||||
|
```bash
|
||||||
|
WHISPER_DEVICE=cuda # Use GPU
|
||||||
|
WHISPER_DEVICE=cpu # Use CPU if GPU unavailable
|
||||||
|
```
|
||||||
|
|
||||||
|
## Troubleshooting
|
||||||
|
|
||||||
|
### Common Issues
|
||||||
|
|
||||||
|
1. Wake word detection not working:
|
||||||
|
- Check microphone permissions
|
||||||
|
- Adjust `WAKE_WORD_SENSITIVITY`
|
||||||
|
- Verify wake words configuration
|
||||||
|
|
||||||
|
2. Poor transcription quality:
|
||||||
|
- Check audio input quality
|
||||||
|
- Try a larger model
|
||||||
|
- Verify language settings
|
||||||
|
|
||||||
|
3. Performance issues:
|
||||||
|
- Monitor resource usage
|
||||||
|
- Consider smaller model
|
||||||
|
- Check GPU acceleration status
|
||||||
|
|
||||||
|
### Logging
|
||||||
|
|
||||||
|
Enable debug logging for detailed information:
|
||||||
|
```bash
|
||||||
|
LOG_LEVEL=debug
|
||||||
|
```
|
||||||
|
|
||||||
|
Speech-specific logs will be tagged with `[SPEECH]` prefix.
|
||||||
|
|
||||||
|
## Security Considerations
|
||||||
|
|
||||||
|
1. Audio Privacy:
|
||||||
|
- Audio is processed locally
|
||||||
|
- No data sent to external services
|
||||||
|
- Temporary files automatically cleaned
|
||||||
|
|
||||||
|
2. Access Control:
|
||||||
|
- Speech endpoints require authentication
|
||||||
|
- Rate limiting applies to transcription
|
||||||
|
- Configurable command restrictions
|
||||||
|
|
||||||
|
3. Resource Protection:
|
||||||
|
- Timeouts prevent hanging
|
||||||
|
- Memory limits enforced
|
||||||
|
- Graceful error handling
|
||||||
@@ -55,7 +55,8 @@
|
|||||||
"husky": "^9.0.11",
|
"husky": "^9.0.11",
|
||||||
"prettier": "^3.2.5",
|
"prettier": "^3.2.5",
|
||||||
"supertest": "^6.3.3",
|
"supertest": "^6.3.3",
|
||||||
"uuid": "^11.0.5"
|
"uuid": "^11.0.5",
|
||||||
|
"@types/bun": "latest"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
"bun": ">=1.0.0"
|
"bun": ">=1.0.0"
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
test audio content
|
|
||||||
@@ -1,183 +1,256 @@
|
|||||||
import WebSocket from "ws";
|
import WebSocket from "ws";
|
||||||
import { EventEmitter } from "events";
|
import { EventEmitter } from "events";
|
||||||
|
|
||||||
|
interface HassMessage {
|
||||||
|
type: string;
|
||||||
|
id?: number;
|
||||||
|
[key: string]: any;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface HassAuthMessage extends HassMessage {
|
||||||
|
type: "auth";
|
||||||
|
access_token: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface HassEventMessage extends HassMessage {
|
||||||
|
type: "event";
|
||||||
|
event: {
|
||||||
|
event_type: string;
|
||||||
|
data: any;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
interface HassSubscribeMessage extends HassMessage {
|
||||||
|
type: "subscribe_events";
|
||||||
|
event_type?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface HassUnsubscribeMessage extends HassMessage {
|
||||||
|
type: "unsubscribe_events";
|
||||||
|
subscription: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface HassResultMessage extends HassMessage {
|
||||||
|
type: "result";
|
||||||
|
success: boolean;
|
||||||
|
error?: string;
|
||||||
|
}
|
||||||
|
|
||||||
export class HassWebSocketClient extends EventEmitter {
|
export class HassWebSocketClient extends EventEmitter {
|
||||||
private ws: WebSocket | null = null;
|
private ws: WebSocket | null = null;
|
||||||
private messageId = 1;
|
|
||||||
private authenticated = false;
|
private authenticated = false;
|
||||||
|
private messageId = 1;
|
||||||
|
private subscriptions = new Map<number, (data: any) => void>();
|
||||||
|
private url: string;
|
||||||
|
private token: string;
|
||||||
private reconnectAttempts = 0;
|
private reconnectAttempts = 0;
|
||||||
private maxReconnectAttempts = 5;
|
private maxReconnectAttempts = 3;
|
||||||
private reconnectDelay = 1000;
|
|
||||||
private subscriptions = new Map<string, (data: any) => void>();
|
|
||||||
|
|
||||||
constructor(
|
constructor(url: string, token: string) {
|
||||||
private url: string,
|
|
||||||
private token: string,
|
|
||||||
private options: {
|
|
||||||
autoReconnect?: boolean;
|
|
||||||
maxReconnectAttempts?: number;
|
|
||||||
reconnectDelay?: number;
|
|
||||||
} = {},
|
|
||||||
) {
|
|
||||||
super();
|
super();
|
||||||
this.maxReconnectAttempts = options.maxReconnectAttempts || 5;
|
this.url = url;
|
||||||
this.reconnectDelay = options.reconnectDelay || 1000;
|
this.token = token;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async connect(): Promise<void> {
|
public async connect(): Promise<void> {
|
||||||
|
if (this.ws && this.ws.readyState === WebSocket.OPEN) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
this.ws = new WebSocket(this.url);
|
this.ws = new WebSocket(this.url);
|
||||||
|
|
||||||
this.ws.on("open", () => {
|
this.ws.onopen = () => {
|
||||||
|
this.emit('connect');
|
||||||
this.authenticate();
|
this.authenticate();
|
||||||
});
|
|
||||||
|
|
||||||
this.ws.on("message", (data: string) => {
|
|
||||||
const message = JSON.parse(data);
|
|
||||||
this.handleMessage(message);
|
|
||||||
});
|
|
||||||
|
|
||||||
this.ws.on("close", () => {
|
|
||||||
this.handleDisconnect();
|
|
||||||
});
|
|
||||||
|
|
||||||
this.ws.on("error", (error) => {
|
|
||||||
this.emit("error", error);
|
|
||||||
reject(error);
|
|
||||||
});
|
|
||||||
|
|
||||||
this.once("auth_ok", () => {
|
|
||||||
this.authenticated = true;
|
|
||||||
this.reconnectAttempts = 0;
|
|
||||||
resolve();
|
resolve();
|
||||||
});
|
};
|
||||||
|
|
||||||
this.once("auth_invalid", () => {
|
this.ws.onclose = () => {
|
||||||
reject(new Error("Authentication failed"));
|
this.authenticated = false;
|
||||||
});
|
this.emit('disconnect');
|
||||||
|
this.handleReconnect();
|
||||||
|
};
|
||||||
|
|
||||||
|
this.ws.onerror = (event: WebSocket.ErrorEvent) => {
|
||||||
|
this.emit('error', event);
|
||||||
|
reject(event);
|
||||||
|
};
|
||||||
|
|
||||||
|
this.ws.onmessage = (event: WebSocket.MessageEvent) => {
|
||||||
|
if (typeof event.data === 'string') {
|
||||||
|
this.handleMessage(event.data);
|
||||||
|
}
|
||||||
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
reject(error);
|
reject(error);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private authenticate(): void {
|
public isConnected(): boolean {
|
||||||
this.send({
|
return this.ws !== null && this.ws.readyState === WebSocket.OPEN;
|
||||||
type: "auth",
|
|
||||||
access_token: this.token,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private handleMessage(message: any): void {
|
public isAuthenticated(): boolean {
|
||||||
switch (message.type) {
|
return this.authenticated;
|
||||||
case "auth_required":
|
|
||||||
this.authenticate();
|
|
||||||
break;
|
|
||||||
case "auth_ok":
|
|
||||||
this.emit("auth_ok");
|
|
||||||
break;
|
|
||||||
case "auth_invalid":
|
|
||||||
this.emit("auth_invalid");
|
|
||||||
break;
|
|
||||||
case "event":
|
|
||||||
this.handleEvent(message);
|
|
||||||
break;
|
|
||||||
case "result":
|
|
||||||
this.emit(`result_${message.id}`, message);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private handleEvent(message: any): void {
|
|
||||||
const subscription = this.subscriptions.get(message.event.event_type);
|
|
||||||
if (subscription) {
|
|
||||||
subscription(message.event.data);
|
|
||||||
}
|
|
||||||
this.emit("event", message.event);
|
|
||||||
}
|
|
||||||
|
|
||||||
private handleDisconnect(): void {
|
|
||||||
this.authenticated = false;
|
|
||||||
this.emit("disconnected");
|
|
||||||
|
|
||||||
if (
|
|
||||||
this.options.autoReconnect &&
|
|
||||||
this.reconnectAttempts < this.maxReconnectAttempts
|
|
||||||
) {
|
|
||||||
setTimeout(
|
|
||||||
() => {
|
|
||||||
this.reconnectAttempts++;
|
|
||||||
this.connect().catch((error) => {
|
|
||||||
this.emit("error", error);
|
|
||||||
});
|
|
||||||
},
|
|
||||||
this.reconnectDelay * Math.pow(2, this.reconnectAttempts),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public async subscribeEvents(
|
|
||||||
eventType: string,
|
|
||||||
callback: (data: any) => void,
|
|
||||||
): Promise<number> {
|
|
||||||
if (!this.authenticated) {
|
|
||||||
throw new Error("Not authenticated");
|
|
||||||
}
|
|
||||||
|
|
||||||
const id = this.messageId++;
|
|
||||||
this.subscriptions.set(eventType, callback);
|
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
this.send({
|
|
||||||
id,
|
|
||||||
type: "subscribe_events",
|
|
||||||
event_type: eventType,
|
|
||||||
});
|
|
||||||
|
|
||||||
this.once(`result_${id}`, (message) => {
|
|
||||||
if (message.success) {
|
|
||||||
resolve(id);
|
|
||||||
} else {
|
|
||||||
reject(new Error(message.error?.message || "Subscription failed"));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public async unsubscribeEvents(subscription: number): Promise<void> {
|
|
||||||
if (!this.authenticated) {
|
|
||||||
throw new Error("Not authenticated");
|
|
||||||
}
|
|
||||||
|
|
||||||
const id = this.messageId++;
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
this.send({
|
|
||||||
id,
|
|
||||||
type: "unsubscribe_events",
|
|
||||||
subscription,
|
|
||||||
});
|
|
||||||
|
|
||||||
this.once(`result_${id}`, (message) => {
|
|
||||||
if (message.success) {
|
|
||||||
resolve();
|
|
||||||
} else {
|
|
||||||
reject(new Error(message.error?.message || "Unsubscribe failed"));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private send(message: any): void {
|
|
||||||
if (this.ws?.readyState === WebSocket.OPEN) {
|
|
||||||
this.ws.send(JSON.stringify(message));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public disconnect(): void {
|
public disconnect(): void {
|
||||||
if (this.ws) {
|
if (this.ws) {
|
||||||
this.ws.close();
|
this.ws.close();
|
||||||
this.ws = null;
|
this.ws = null;
|
||||||
|
this.authenticated = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private authenticate(): void {
|
||||||
|
const authMessage: HassAuthMessage = {
|
||||||
|
type: "auth",
|
||||||
|
access_token: this.token
|
||||||
|
};
|
||||||
|
this.send(authMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
private handleMessage(data: string): void {
|
||||||
|
try {
|
||||||
|
const message = JSON.parse(data) as HassMessage;
|
||||||
|
|
||||||
|
switch (message.type) {
|
||||||
|
case "auth_ok":
|
||||||
|
this.authenticated = true;
|
||||||
|
this.emit('authenticated', message);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "auth_invalid":
|
||||||
|
this.authenticated = false;
|
||||||
|
this.emit('auth_failed', message);
|
||||||
|
this.disconnect();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "event":
|
||||||
|
this.handleEvent(message as HassEventMessage);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "result": {
|
||||||
|
const resultMessage = message as HassResultMessage;
|
||||||
|
if (resultMessage.success) {
|
||||||
|
this.emit('result', resultMessage);
|
||||||
|
} else {
|
||||||
|
this.emit('error', new Error(resultMessage.error || 'Unknown error'));
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
this.emit('error', new Error(`Unknown message type: ${message.type}`));
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
this.emit('error', error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private handleEvent(message: HassEventMessage): void {
|
||||||
|
this.emit('event', message.event);
|
||||||
|
const callback = this.subscriptions.get(message.id || 0);
|
||||||
|
if (callback) {
|
||||||
|
callback(message.event.data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async subscribeEvents(eventType: string | undefined, callback: (data: any) => void): Promise<number> {
|
||||||
|
if (!this.authenticated) {
|
||||||
|
throw new Error('Not authenticated');
|
||||||
|
}
|
||||||
|
|
||||||
|
const id = this.messageId++;
|
||||||
|
const message: HassSubscribeMessage = {
|
||||||
|
id,
|
||||||
|
type: "subscribe_events",
|
||||||
|
event_type: eventType
|
||||||
|
};
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const handleResult = (result: HassResultMessage) => {
|
||||||
|
if (result.id === id) {
|
||||||
|
this.removeListener('result', handleResult);
|
||||||
|
this.removeListener('error', handleError);
|
||||||
|
|
||||||
|
if (result.success) {
|
||||||
|
this.subscriptions.set(id, callback);
|
||||||
|
resolve(id);
|
||||||
|
} else {
|
||||||
|
reject(new Error(result.error || 'Failed to subscribe'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleError = (error: Error) => {
|
||||||
|
this.removeListener('result', handleResult);
|
||||||
|
this.removeListener('error', handleError);
|
||||||
|
reject(error);
|
||||||
|
};
|
||||||
|
|
||||||
|
this.on('result', handleResult);
|
||||||
|
this.on('error', handleError);
|
||||||
|
|
||||||
|
this.send(message);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public async unsubscribeEvents(subscription: number): Promise<boolean> {
|
||||||
|
if (!this.authenticated) {
|
||||||
|
throw new Error('Not authenticated');
|
||||||
|
}
|
||||||
|
|
||||||
|
const message: HassUnsubscribeMessage = {
|
||||||
|
id: this.messageId++,
|
||||||
|
type: "unsubscribe_events",
|
||||||
|
subscription
|
||||||
|
};
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const handleResult = (result: HassResultMessage) => {
|
||||||
|
if (result.id === message.id) {
|
||||||
|
this.removeListener('result', handleResult);
|
||||||
|
this.removeListener('error', handleError);
|
||||||
|
|
||||||
|
if (result.success) {
|
||||||
|
this.subscriptions.delete(subscription);
|
||||||
|
resolve(true);
|
||||||
|
} else {
|
||||||
|
reject(new Error(result.error || 'Failed to unsubscribe'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleError = (error: Error) => {
|
||||||
|
this.removeListener('result', handleResult);
|
||||||
|
this.removeListener('error', handleError);
|
||||||
|
reject(error);
|
||||||
|
};
|
||||||
|
|
||||||
|
this.on('result', handleResult);
|
||||||
|
this.on('error', handleError);
|
||||||
|
|
||||||
|
this.send(message);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private send(message: HassMessage): void {
|
||||||
|
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
|
||||||
|
throw new Error('WebSocket is not connected');
|
||||||
|
}
|
||||||
|
this.ws.send(JSON.stringify(message));
|
||||||
|
}
|
||||||
|
|
||||||
|
private handleReconnect(): void {
|
||||||
|
if (this.reconnectAttempts < this.maxReconnectAttempts) {
|
||||||
|
this.reconnectAttempts++;
|
||||||
|
setTimeout(() => {
|
||||||
|
this.connect().catch(() => { });
|
||||||
|
}, 1000 * Math.pow(2, this.reconnectAttempts));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user