@iflow-mcp/coolver-home-assistant-mcp
Version:
Enable Cursor, VS Code, Claude Code or any MCP-enabled IDE to help you vibecode and manage Home Assistant: create automations, design dashboards, tweak themes, modify configs, and deploy changes using natural language
453 lines • 16 kB
JavaScript
import axios from 'axios';
import { readFileSync } from 'fs';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// Read version from package.json
let MCP_VERSION = 'unknown';
try {
const packagePath = join(__dirname, '..', 'package.json');
const packageJson = JSON.parse(readFileSync(packagePath, 'utf-8'));
MCP_VERSION = packageJson.version;
}
catch (error) {
console.error('Warning: Could not read MCP version from package.json');
}
export class HAClient {
client;
constructor(config) {
this.client = axios.create({
baseURL: config.baseURL,
headers: {
'Authorization': `Bearer ${config.token}`,
'Content-Type': 'application/json',
'X-MCP-Client-Version': MCP_VERSION,
},
timeout: 30000,
});
}
// Files API
async readFile(path) {
const response = await this.client.get(`/api/files/read`, {
params: { path },
});
return response.data.content;
}
async writeFile(path, content, commitMessage) {
await this.client.post(`/api/files/write`, {
path,
content,
commit_message: commitMessage,
});
}
async listFiles(directory = '/') {
const response = await this.client.get(`/api/files/list`, {
params: { directory },
});
return response.data.files;
}
async deleteFile(path) {
await this.client.delete(`/api/files/delete`, {
params: { path },
});
}
// Entities API
async listEntities(domain) {
const response = await this.client.get(`/api/entities/list`, {
params: domain ? { domain } : {},
});
return response.data.entities;
}
async getEntityState(entityId) {
const response = await this.client.get(`/api/entities/state/${entityId}`);
return response.data;
}
async renameEntity(oldEntityId, newEntityId) {
const response = await this.client.post(`/api/entities/rename`, {
old_entity_id: oldEntityId,
new_entity_id: newEntityId,
});
return response.data;
}
// Entity Registry API
async getEntityRegistryList() {
const response = await this.client.get(`/api/registries/entities/list`);
return response.data.entities;
}
async getEntityRegistryEntry(entityId) {
const response = await this.client.get(`/api/registries/entities/${entityId}`);
return response.data.entity;
}
async updateEntityRegistry(entityId, updateData) {
const response = await this.client.post(`/api/registries/entities/update`, {
entity_id: entityId,
...updateData,
});
return response.data;
}
async removeEntityRegistryEntry(entityId) {
const response = await this.client.post(`/api/registries/entities/remove`, {
entity_id: entityId,
});
return response.data;
}
async findDeadEntities() {
const response = await this.client.get(`/api/registries/entities/dead`);
return response.data;
}
// Area Registry API
async getAreaRegistryList() {
const response = await this.client.get(`/api/registries/areas/list`);
return response.data.areas;
}
async getAreaRegistryEntry(areaId) {
const response = await this.client.get(`/api/registries/areas/${areaId}`);
return response.data.area;
}
async createAreaRegistryEntry(name, aliases) {
const response = await this.client.post(`/api/registries/areas/create`, {
name,
aliases,
});
return response.data;
}
async updateAreaRegistryEntry(areaId, name, aliases) {
const response = await this.client.post(`/api/registries/areas/update`, {
area_id: areaId,
name,
aliases,
});
return response.data;
}
async deleteAreaRegistryEntry(areaId) {
const response = await this.client.post(`/api/registries/areas/delete`, {
area_id: areaId,
});
return response.data;
}
// Device Registry API
async getDeviceRegistryList() {
const response = await this.client.get(`/api/registries/devices/list`);
return response.data.devices;
}
async getDeviceRegistryEntry(deviceId) {
const response = await this.client.get(`/api/registries/devices/${deviceId}`);
return response.data.device;
}
async updateDeviceRegistry(deviceId, updateData) {
const response = await this.client.post(`/api/registries/devices/update`, {
device_id: deviceId,
...updateData,
});
return response.data;
}
async removeDeviceRegistryEntry(deviceId) {
const response = await this.client.post(`/api/registries/devices/remove`, {
device_id: deviceId,
});
return response.data;
}
// Helpers API
async createHelper(type, config, commitMessage) {
const response = await this.client.post(`/api/helpers/create`, {
type,
config,
commit_message: commitMessage,
});
return response.data;
}
async listHelpers() {
const response = await this.client.get(`/api/helpers/list`);
return response.data;
}
async deleteHelper(entityId, commitMessage) {
const response = await this.client.delete(`/api/helpers/delete/${entityId}`, {
params: { commit_message: commitMessage },
});
return response.data;
}
// Automations API
async createAutomation(config, commitMessage) {
const response = await this.client.post(`/api/automations/create`, {
...config,
commit_message: commitMessage,
});
return response.data;
}
async listAutomations() {
const response = await this.client.get(`/api/automations/list`);
return response.data.automations;
}
async deleteAutomation(automationId, commitMessage) {
const response = await this.client.delete(`/api/automations/delete/${automationId}`, {
params: { commit_message: commitMessage },
});
return response.data;
}
// Scripts API
async createScript(config, commitMessage) {
const response = await this.client.post(`/api/scripts/create`, {
...config,
commit_message: commitMessage,
});
return response.data;
}
async listScripts() {
const response = await this.client.get(`/api/scripts/list`);
return response.data.scripts;
}
async deleteScript(scriptId, commitMessage) {
const response = await this.client.delete(`/api/scripts/delete/${scriptId}`, {
params: { commit_message: commitMessage },
});
return response.data;
}
// Git/Backup API
async gitCommit(message) {
const response = await this.client.post(`/api/backup/commit`, {
message,
});
return response.data;
}
async gitHistory(limit = 20) {
const response = await this.client.get(`/api/backup/history`, {
params: { limit },
});
return response.data.commits;
}
async gitRollback(commitHash) {
const response = await this.client.post(`/api/backup/rollback/${commitHash}`);
return response.data;
}
async gitDiff(commit1, commit2) {
const response = await this.client.get(`/api/backup/diff`, {
params: { commit1, commit2 },
});
return response.data;
}
// System API
async checkConfig() {
const response = await this.client.post(`/api/system/check-config`);
return response.data;
}
async reloadConfig(component = 'all') {
const response = await this.client.post(`/api/system/reload?component=${component}`);
return response.data;
}
async getLogs(limit = 100, level) {
const response = await this.client.get(`/api/logs`, {
params: { limit, level },
});
return response.data;
}
async getLogbookEntries(params = {}) {
const response = await this.client.get(`/api/logbook`, {
params,
});
return response.data;
}
async healthCheck() {
const response = await this.client.get(`/api/health`);
return response.data;
}
// HACS API
async hacsInstall() {
const response = await this.client.post(`/api/hacs/install`);
return response.data;
}
async hacsUninstall() {
const response = await this.client.post(`/api/hacs/uninstall`);
return response.data;
}
async hacsStatus() {
const response = await this.client.get(`/api/hacs/status`);
return response.data;
}
async hacsListRepositories() {
const response = await this.client.get(`/api/hacs/repositories`);
return response.data;
}
async hacsInstallRepository(repository, category = 'integration') {
const response = await this.client.post(`/api/hacs/install_repository`, { repository, category });
return response.data;
}
async hacsSearch(query, category) {
const response = await this.client.get(`/api/hacs/search`, {
params: { query, category },
});
return response.data;
}
async hacsUpdateAll() {
const response = await this.client.post(`/api/hacs/update_all`);
return response.data;
}
async hacsGetRepositoryDetails(repositoryId) {
const response = await this.client.get(`/api/hacs/repository/${repositoryId}`);
return response.data;
}
// Add-ons API
async listStoreAddons() {
const response = await this.client.get(`/api/addons/store`);
return response.data;
}
async listAvailableAddons() {
const response = await this.client.get(`/api/addons/available`);
return response.data;
}
async listInstalledAddons() {
const response = await this.client.get(`/api/addons/installed`);
return response.data;
}
async getAddonInfo(slug) {
const response = await this.client.get(`/api/addons/${slug}/info`);
return response.data;
}
async getAddonLogs(slug, lines) {
const response = await this.client.get(`/api/addons/${slug}/logs`, {
params: lines ? { lines } : {},
});
return response.data;
}
async installAddon(slug) {
const response = await this.client.post(`/api/addons/${slug}/install`, {}, {
timeout: 600000, // 10 minutes for installation
});
return response.data;
}
async uninstallAddon(slug) {
const response = await this.client.post(`/api/addons/${slug}/uninstall`);
return response.data;
}
async startAddon(slug) {
const response = await this.client.post(`/api/addons/${slug}/start`);
return response.data;
}
async stopAddon(slug) {
const response = await this.client.post(`/api/addons/${slug}/stop`);
return response.data;
}
async restartAddon(slug) {
const response = await this.client.post(`/api/addons/${slug}/restart`);
return response.data;
}
async updateAddon(slug) {
const response = await this.client.post(`/api/addons/${slug}/update`, {}, {
timeout: 600000, // 10 minutes for update
});
return response.data;
}
async getAddonOptions(slug) {
const response = await this.client.get(`/api/addons/${slug}/options`);
return response.data;
}
async setAddonOptions(slug, options) {
const response = await this.client.post(`/api/addons/${slug}/options`, { options });
return response.data;
}
async listRepositories() {
const response = await this.client.get(`/api/addons/repositories`);
return response.data;
}
async addRepository(repositoryUrl) {
const response = await this.client.post(`/api/addons/repositories/add`, { repository_url: repositoryUrl });
return response.data;
}
// Lovelace Dashboard API
async analyzeEntitiesForDashboard() {
const response = await this.client.get(`/api/lovelace/analyze`);
return response.data;
}
async previewDashboard() {
const response = await this.client.get(`/api/lovelace/preview`);
return response.data;
}
async applyDashboard(dashboardConfig, createBackup = true, filename = 'ai-dashboard.yaml', registerDashboard = true, commitMessage) {
const response = await this.client.post(`/api/lovelace/apply`, {
dashboard_config: dashboardConfig,
create_backup: createBackup,
filename: filename,
register_dashboard: registerDashboard,
commit_message: commitMessage,
});
return response.data;
}
async deleteDashboard(filename, removeFromConfig = true, createBackup = true, commitMessage) {
const response = await this.client.delete(`/api/lovelace/delete/${filename}`, {
params: {
remove_from_config: removeFromConfig,
create_backup: createBackup,
commit_message: commitMessage,
}
});
return response.data;
}
// System API
async restartHomeAssistant() {
const response = await this.client.post(`/api/system/restart`);
return response.data;
}
// Checkpoint API
async createCheckpoint(userRequest) {
const response = await this.client.post(`/api/backup/checkpoint`, null, {
params: { user_request: userRequest },
});
return response.data;
}
async endCheckpoint() {
const response = await this.client.post(`/api/backup/checkpoint/end`);
return response.data;
}
// Service API
async callService(domain, service, serviceData, target) {
const response = await this.client.post(`/api/entities/call_service`, {
domain,
service,
service_data: serviceData,
target: target,
});
return response.data;
}
// Themes API
async listThemes() {
const response = await this.client.get(`/api/themes/list`);
return response.data.themes || [];
}
async getTheme(themeName) {
const response = await this.client.get(`/api/themes/get`, {
params: { theme_name: themeName },
});
return response.data;
}
async createTheme(themeName, themeConfig, commitMessage) {
const response = await this.client.post(`/api/themes/create`, {
theme_name: themeName,
theme_config: themeConfig,
commit_message: commitMessage,
});
return response.data;
}
async updateTheme(themeName, themeConfig, commitMessage) {
const response = await this.client.put(`/api/themes/update`, {
theme_name: themeName,
theme_config: themeConfig,
commit_message: commitMessage,
});
return response.data;
}
async deleteTheme(themeName, commitMessage) {
const response = await this.client.delete(`/api/themes/delete`, {
params: { theme_name: themeName, commit_message: commitMessage },
});
return response.data;
}
async reloadThemes() {
const response = await this.client.post(`/api/themes/reload`);
return response.data;
}
async checkThemeConfig() {
const response = await this.client.get(`/api/themes/check_config`);
return response.data;
}
}
//# sourceMappingURL=ha-client.js.map