homeassistant_api
Check the status of the Home Assistant API to ensure it is online and available for managing smart home devices and services.
Instructions
Verify if the Home Assistant API is online
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/tools/homeassistant/index.ts:11-45 (registration)Registers the 'homeassistant_api' MCP tool with no input parameters. The inline handler calls getHomeAssistantApi to check API status and returns a formatted text response.server.tool( "homeassistant_api", "Verify if the Home Assistant API is online", async () => { console.error(`Verificando Home Assistant`); const result = await getHomeAssistantApi('/api/'); if (!result.success) { return formatErrorResponse(`Erro ao pesquisar time: ${result.message}`); } // Formata os dados da resposta const response = result.data; if (!response.results || response.results === 0) { return { content: [{ type: "text" as const, text: response.message }] }; } const responseMessage = response.message; return { content: [{ type: "text" as const, text: responseMessage }] }; } );
- src/utils/api.ts:28-73 (helper)Core helper function that performs authenticated GET requests to the Home Assistant API using axios and environment variables for URL and token.async function getHomeAssistantApi(endpoint: string) { try { const url = `${process.env.HOME_ASSISTANT_URL}${endpoint}`; console.error(`Making request to: ${url}`); // O API key deve ser configurado como variável de ambiente const bearerToken = process.env.HOME_ASSISTANT_TOKEN; if (!bearerToken) { console.error('HOME_ASSISTANT_TOKEN environment variable is not set') process.exit(1) } console.log(bearerToken); console.log(url); const response = await axios.get(url, { headers: { 'Authorization': `Bearer ${bearerToken}` } }); return { data: response.data, success: true }; } catch (error: any) { console.error(`Failed to get Home Assistant API: ${error.message}`); // Tratamento de erros da API de forma estruturada if (error.response) { return { success: false, statusCode: error.response.status, message: error.response.data?.message || error.message, error: error.response.data }; } return { success: false, message: error.message, error }; } }
- src/index.ts:23-23 (registration)Top-level registration call that invokes the module to register the homeassistant_api tool (and others) on the MCP server instance.registerHomeAssistantApiTools(server);
- src/utils/api.ts:12-20 (helper)Helper function used by the tool handler to format error responses in MCP format.export function formatErrorResponse(message: string) { return { content: [{ type: "text" as const, text: message }], isError: true }; }