Refactor Home Assistant API and schema validation

- Completely rewrote HassInstance class with fetch-based API methods
- Updated Home Assistant schemas to be more precise and flexible
- Removed deprecated test environment configuration file
- Enhanced WebSocket client implementation
- Improved test coverage for Home Assistant API and schema validation
- Simplified type definitions and error handling
This commit is contained in:
jango-blockchained
2025-01-30 10:51:25 +01:00
parent 732a727d27
commit 8152313f52
8 changed files with 561 additions and 200 deletions

View File

@@ -1,157 +1,134 @@
import { get_hass } from '../../src/hass/index.js';
import { HassInstance } from '../../src/hass/index.js';
import * as HomeAssistant from '../../src/types/hass.js';
// Mock the entire hass module
jest.mock('../../src/hass/index.js', () => ({
get_hass: jest.fn()
}));
describe('Home Assistant API Integration', () => {
const MOCK_HASS_HOST = 'http://localhost:8123';
const MOCK_HASS_TOKEN = 'mock_token_12345';
const mockHassInstance = {
getStates: jest.fn(),
getState: jest.fn(),
callService: jest.fn(),
subscribeEvents: jest.fn()
};
describe('Home Assistant API', () => {
let hass: HassInstance;
beforeEach(() => {
process.env.HASS_HOST = MOCK_HASS_HOST;
process.env.HASS_TOKEN = MOCK_HASS_TOKEN;
jest.clearAllMocks();
(get_hass as jest.Mock).mockResolvedValue(mockHassInstance);
});
describe('API Connection', () => {
it('should initialize connection with valid credentials', async () => {
const hass = await get_hass();
expect(hass).toBeDefined();
expect(hass).toBe(mockHassInstance);
});
it('should handle connection errors', async () => {
(get_hass as jest.Mock).mockRejectedValueOnce(new Error('Connection failed'));
await expect(get_hass()).rejects.toThrow('Connection failed');
});
it('should handle invalid credentials', async () => {
(get_hass as jest.Mock).mockRejectedValueOnce(new Error('Unauthorized'));
await expect(get_hass()).rejects.toThrow('Unauthorized');
});
it('should handle missing environment variables', async () => {
delete process.env.HASS_HOST;
delete process.env.HASS_TOKEN;
(get_hass as jest.Mock).mockRejectedValueOnce(new Error('Missing required environment variables'));
await expect(get_hass()).rejects.toThrow('Missing required environment variables');
});
hass = new HassInstance('http://localhost:8123', 'test_token');
});
describe('State Management', () => {
const mockStates = [
{
entity_id: 'light.living_room',
state: 'on',
attributes: {
brightness: 255,
friendly_name: 'Living Room Light'
it('should fetch all states', async () => {
const mockStates: HomeAssistant.Entity[] = [
{
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 }
}
},
{
entity_id: 'switch.kitchen',
state: 'off',
attributes: {
friendly_name: 'Kitchen Switch'
}
}
];
];
it('should fetch states successfully', async () => {
mockHassInstance.getStates.mockResolvedValueOnce(mockStates);
const hass = await get_hass();
const states = await hass.getStates();
global.fetch = jest.fn().mockResolvedValueOnce({
ok: true,
json: () => Promise.resolve(mockStates)
});
const states = await hass.fetchStates();
expect(states).toEqual(mockStates);
expect(fetch).toHaveBeenCalledWith(
'http://localhost:8123/api/states',
expect.any(Object)
);
});
it('should get single entity state', async () => {
const mockState = mockStates[0];
mockHassInstance.getState.mockResolvedValueOnce(mockState);
const hass = await get_hass();
const state = await hass.getState('light.living_room');
it('should fetch single state', async () => {
const mockState: HomeAssistant.Entity = {
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 }
};
global.fetch = jest.fn().mockResolvedValueOnce({
ok: true,
json: () => Promise.resolve(mockState)
});
const state = await hass.fetchState('light.living_room');
expect(state).toEqual(mockState);
expect(fetch).toHaveBeenCalledWith(
'http://localhost:8123/api/states/light.living_room',
expect.any(Object)
);
});
it('should handle state fetch errors', async () => {
mockHassInstance.getStates.mockRejectedValueOnce(new Error('Failed to fetch states'));
const hass = await get_hass();
await expect(hass.getStates()).rejects.toThrow('Failed to fetch states');
global.fetch = jest.fn().mockRejectedValueOnce(new Error('Failed to fetch states'));
await expect(hass.fetchStates()).rejects.toThrow('Failed to fetch states');
});
});
describe('Service Calls', () => {
it('should call services successfully', async () => {
mockHassInstance.callService.mockResolvedValueOnce(undefined);
const hass = await get_hass();
it('should call service', async () => {
global.fetch = jest.fn().mockResolvedValueOnce({
ok: true,
json: () => Promise.resolve({})
});
await hass.callService('light', 'turn_on', {
entity_id: 'light.living_room',
brightness: 255
});
expect(mockHassInstance.callService).toHaveBeenCalledWith(
'light',
'turn_on',
{
entity_id: 'light.living_room',
brightness: 255
}
expect(fetch).toHaveBeenCalledWith(
'http://localhost:8123/api/services/light/turn_on',
expect.objectContaining({
method: 'POST',
body: JSON.stringify({
entity_id: 'light.living_room',
brightness: 255
})
})
);
});
it('should handle service call errors', async () => {
mockHassInstance.callService.mockRejectedValueOnce(new Error('Bad Request'));
const hass = await get_hass();
global.fetch = jest.fn().mockRejectedValueOnce(new Error('Service call failed'));
await expect(
hass.callService('invalid_domain', 'invalid_service', {})
).rejects.toThrow('Bad Request');
).rejects.toThrow('Service call failed');
});
});
describe('Event Handling', () => {
describe('Event Subscription', () => {
it('should subscribe to events', async () => {
mockHassInstance.subscribeEvents.mockResolvedValueOnce(undefined);
const hass = await get_hass();
const callback = jest.fn();
const mockWs = {
send: jest.fn(),
close: jest.fn(),
addEventListener: jest.fn()
};
global.WebSocket = jest.fn().mockImplementation(() => mockWs);
await hass.subscribeEvents(callback, 'state_changed');
expect(mockHassInstance.subscribeEvents).toHaveBeenCalledWith(
callback,
'state_changed'
expect(WebSocket).toHaveBeenCalledWith(
'ws://localhost:8123/api/websocket'
);
});
it('should handle event subscription errors', async () => {
mockHassInstance.subscribeEvents.mockRejectedValueOnce(new Error('WebSocket error'));
const hass = await get_hass();
it('should handle subscription errors', async () => {
const callback = jest.fn();
global.WebSocket = jest.fn().mockImplementation(() => {
throw new Error('WebSocket connection failed');
});
await expect(
hass.subscribeEvents(callback, 'state_changed')
).rejects.toThrow('WebSocket error');
});
});
describe('Error Handling', () => {
it('should handle network errors gracefully', async () => {
(get_hass as jest.Mock).mockRejectedValueOnce(new Error('Network error'));
await expect(get_hass()).rejects.toThrow('Network error');
});
it('should handle rate limiting', async () => {
(get_hass as jest.Mock).mockRejectedValueOnce(new Error('Too Many Requests'));
await expect(get_hass()).rejects.toThrow('Too Many Requests');
});
it('should handle server errors', async () => {
(get_hass as jest.Mock).mockRejectedValueOnce(new Error('Internal Server Error'));
await expect(get_hass()).rejects.toThrow('Internal Server Error');
).rejects.toThrow('WebSocket connection failed');
});
});
});

View File

@@ -1,5 +1,6 @@
import { entitySchema, serviceSchema, stateChangedEventSchema, configSchema, automationSchema, deviceControlSchema } from '../../src/schemas/hass.js';
import Ajv, { JSONSchemaType } from 'ajv';
import AjvModule from 'ajv';
const Ajv = AjvModule.default || AjvModule;
describe('Home Assistant Schemas', () => {
const ajv = new Ajv({ allErrors: true });
@@ -77,18 +78,15 @@ describe('Home Assistant Schemas', () => {
describe('Service Schema', () => {
const validate = ajv.compile(serviceSchema);
it('should validate a valid service call', () => {
const validService = {
it('should validate a basic service call', () => {
const basicService = {
domain: 'light',
service: 'turn_on',
target: {
entity_id: 'light.living_room'
},
service_data: {
brightness: 255
entity_id: ['light.living_room']
}
};
expect(validate(validService)).toBe(true);
expect(validate(basicService)).toBe(true);
});
it('should validate service call with multiple targets', () => {
@@ -97,27 +95,31 @@ describe('Home Assistant Schemas', () => {
service: 'turn_on',
target: {
entity_id: ['light.living_room', 'light.kitchen'],
area_id: 'living_room'
device_id: ['device123', 'device456'],
area_id: ['living_room', 'kitchen']
},
service_data: {
brightness: 255
brightness_pct: 100
}
};
expect(validate(multiTargetService)).toBe(true);
});
it('should validate service call without target', () => {
const serviceWithoutTarget = {
it('should validate service call without targets', () => {
const noTargetService = {
domain: 'homeassistant',
service: 'restart'
};
expect(validate(serviceWithoutTarget)).toBe(true);
expect(validate(noTargetService)).toBe(true);
});
it('should reject invalid service call', () => {
it('should reject service call with invalid target type', () => {
const invalidService = {
service: 'turn_on'
// missing domain
domain: 'light',
service: 'turn_on',
target: {
entity_id: 'not_an_array' // should be an array
}
};
expect(validate(invalidService)).toBe(false);
expect(validate.errors).toBeDefined();
@@ -149,9 +151,7 @@ describe('Home Assistant Schemas', () => {
old_state: {
entity_id: 'light.living_room',
state: 'off',
attributes: {
brightness: 0
},
attributes: {},
last_changed: '2024-01-01T00:00:00Z',
last_updated: '2024-01-01T00:00:00Z',
context: {
@@ -202,7 +202,7 @@ describe('Home Assistant Schemas', () => {
expect(validate(newEntityEvent)).toBe(true);
});
it('should reject invalid event type', () => {
it('should reject event with invalid event_type', () => {
const invalidEvent = {
event_type: 'wrong_type',
data: {
@@ -213,18 +213,21 @@ describe('Home Assistant Schemas', () => {
origin: 'LOCAL',
time_fired: '2024-01-01T00:00:00Z',
context: {
id: '123456'
id: '123456',
parent_id: null,
user_id: null
}
};
expect(validate(invalidEvent)).toBe(false);
expect(validate.errors).toBeDefined();
});
});
describe('Config Schema', () => {
const validate = ajv.compile(configSchema);
it('should validate a valid config', () => {
const validConfig = {
it('should validate a minimal config', () => {
const minimalConfig = {
latitude: 52.3731,
longitude: 4.8922,
elevation: 0,
@@ -236,17 +239,17 @@ describe('Home Assistant Schemas', () => {
},
location_name: 'Home',
time_zone: 'Europe/Amsterdam',
components: ['homeassistant', 'light', 'switch'],
components: ['homeassistant'],
version: '2024.1.0'
};
expect(validate(validConfig)).toBe(true);
expect(validate(minimalConfig)).toBe(true);
});
it('should reject config with missing fields', () => {
it('should reject config with missing required fields', () => {
const invalidConfig = {
latitude: 52.3731,
longitude: 4.8922
// missing required fields
// missing other required fields
};
expect(validate(invalidConfig)).toBe(false);
expect(validate.errors).toBeDefined();
@@ -265,31 +268,11 @@ describe('Home Assistant Schemas', () => {
},
location_name: 'Home',
time_zone: 'Europe/Amsterdam',
components: ['homeassistant', 'light', 'switch'],
components: ['homeassistant'],
version: '2024.1.0'
};
expect(validate(invalidConfig)).toBe(false);
});
it('should validate config with all optional fields', () => {
const fullConfig = {
latitude: 52.3731,
longitude: 4.8922,
elevation: 0,
unit_system: {
length: 'km',
mass: 'kg',
temperature: '°C',
volume: 'L'
},
location_name: 'Home',
time_zone: 'Europe/Amsterdam',
components: ['homeassistant', 'light', 'switch', 'climate', 'sensor'],
version: '2024.1.0',
country: 'NL',
language: 'en'
};
expect(validate(fullConfig)).toBe(true);
expect(validate.errors).toBeDefined();
});
});

View File

@@ -0,0 +1,235 @@
import { WebSocket } from 'ws';
import { EventEmitter } from 'events';
import { HassWebSocketClient } from '../../src/websocket/client.js';
import * as HomeAssistant from '../../src/types/hass.js';
// Mock WebSocket
jest.mock('ws');
describe('WebSocket Event Handling', () => {
let client: HassWebSocketClient;
let mockWs: jest.Mocked<WebSocket>;
let eventEmitter: EventEmitter;
beforeEach(() => {
// Setup mock WebSocket
eventEmitter = new EventEmitter();
mockWs = {
on: jest.fn((event, callback) => eventEmitter.on(event, callback)),
send: jest.fn(),
close: jest.fn(),
readyState: WebSocket.OPEN
} as unknown as jest.Mocked<WebSocket>;
(WebSocket as jest.MockedClass<typeof WebSocket>).mockImplementation(() => mockWs);
// Create client instance with required options
client = new HassWebSocketClient('ws://localhost:8123/api/websocket', 'test_token', {
autoReconnect: true,
maxReconnectAttempts: 3,
reconnectDelay: 100
});
});
afterEach(() => {
jest.clearAllMocks();
eventEmitter.removeAllListeners();
client.disconnect();
});
describe('Connection Events', () => {
it('should handle successful connection', (done) => {
client.on('open', () => {
expect(mockWs.send).toHaveBeenCalled();
done();
});
eventEmitter.emit('open');
});
it('should handle connection errors', (done) => {
const error = new Error('Connection failed');
client.on('error', (err: Error) => {
expect(err).toBe(error);
done();
});
eventEmitter.emit('error', error);
});
it('should handle connection close', (done) => {
client.on('disconnected', () => {
expect(mockWs.close).toHaveBeenCalled();
done();
});
eventEmitter.emit('close');
});
});
describe('Authentication', () => {
it('should send authentication message on connect', () => {
const authMessage: HomeAssistant.AuthMessage = {
type: 'auth',
access_token: 'test_token'
};
client.connect();
expect(mockWs.send).toHaveBeenCalledWith(JSON.stringify(authMessage));
});
it('should handle successful authentication', (done) => {
client.on('auth_ok', () => {
done();
});
client.connect();
eventEmitter.emit('message', JSON.stringify({ type: 'auth_ok' }));
});
it('should handle authentication failure', (done) => {
client.on('auth_invalid', () => {
done();
});
client.connect();
eventEmitter.emit('message', JSON.stringify({ type: 'auth_invalid' }));
});
});
describe('Event Subscription', () => {
it('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' }
},
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' }
}
},
origin: 'LOCAL',
time_fired: '2024-01-01T00:00:00Z',
context: { id: '123' }
};
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.emit('message', JSON.stringify({ type: 'event', event: stateEvent }));
});
it('should subscribe to specific events', async () => {
const subscriptionId = 1;
const callback = jest.fn();
// Mock successful subscription
const subscribePromise = client.subscribeEvents('state_changed', callback);
eventEmitter.emit('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.emit('message', JSON.stringify({
type: 'event',
event: {
event_type: 'state_changed',
data: eventData
}
}));
expect(callback).toHaveBeenCalledWith(eventData);
});
it('should unsubscribe from events', async () => {
// First subscribe
const subscriptionId = await client.subscribeEvents('state_changed', () => { });
// Then unsubscribe
const unsubscribePromise = client.unsubscribeEvents(subscriptionId);
eventEmitter.emit('message', JSON.stringify({
id: 2,
type: 'result',
success: true
}));
await expect(unsubscribePromise).resolves.toBeUndefined();
});
});
describe('Message Handling', () => {
it('should handle malformed messages', (done) => {
client.on('error', (error: Error) => {
expect(error.message).toContain('Unexpected token');
done();
});
eventEmitter.emit('message', 'invalid json');
});
it('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.emit('message', JSON.stringify(unknownMessage));
});
});
describe('Reconnection', () => {
it('should attempt to reconnect on connection loss', (done) => {
let reconnectAttempts = 0;
client.on('disconnected', () => {
reconnectAttempts++;
if (reconnectAttempts === 1) {
expect(WebSocket).toHaveBeenCalledTimes(2);
done();
}
});
eventEmitter.emit('close');
});
it('should re-authenticate after reconnection', (done) => {
client.connect();
client.on('auth_ok', () => {
done();
});
eventEmitter.emit('close');
eventEmitter.emit('open');
eventEmitter.emit('message', JSON.stringify({ type: 'auth_ok' }));
});
});
});