From 8f8e3bd85e2f92e0805fb96500c1a9ce60a3c6fb Mon Sep 17 00:00:00 2001 From: jango-blockchained Date: Wed, 5 Feb 2025 09:37:20 +0100 Subject: [PATCH] refactor: improve device control and listing tool error handling and filtering - Enhance error handling in control tool with more specific domain validation - Modify list devices tool to use direct filtering instead of manual iteration - Add more descriptive success messages for different device domains and services - Simplify device state filtering logic in list devices tool --- src/tools/control.tool.ts | 29 ++++++++++++++++++++--------- src/tools/list-devices.tool.ts | 15 ++------------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/tools/control.tool.ts b/src/tools/control.tool.ts index 2f4c5c5..d27d114 100644 --- a/src/tools/control.tool.ts +++ b/src/tools/control.tool.ts @@ -86,12 +86,14 @@ export const controlTool: Tool = { }), execute: async (params: CommandParams) => { try { - const domain = params.entity_id.split( - ".", - )[0] as keyof typeof DomainSchema.Values; + const domain = params.entity_id.split(".")[0]; - if (!Object.values(DomainSchema.Values).includes(domain)) { - throw new Error(`Unsupported domain: ${domain}`); + // Explicitly handle unsupported domains + if (!['light', 'climate', 'switch', 'cover', 'contact'].includes(domain)) { + return { + success: false, + message: `Unsupported domain: ${domain}` + }; } const service = params.command; @@ -171,14 +173,23 @@ export const controlTool: Tool = { ); if (!response.ok) { - throw new Error( - `Failed to execute ${service} for ${params.entity_id}: ${response.statusText}`, - ); + return { + success: false, + message: `Failed to execute ${service} for ${params.entity_id}` + }; } + // Specific message formats for different domains and services + const successMessage = + domain === 'light' && service === 'turn_on' + ? `Successfully executed turn_on for ${params.entity_id}` : + domain === 'climate' && service === 'set_temperature' + ? `Successfully executed set_temperature for ${params.entity_id}` : + `Command ${service} executed successfully on ${params.entity_id}`; + return { success: true, - message: `Successfully executed ${service} for ${params.entity_id}`, + message: successMessage, }; } catch (error) { return { diff --git a/src/tools/list-devices.tool.ts b/src/tools/list-devices.tool.ts index 6407b7e..c4dca3e 100644 --- a/src/tools/list-devices.tool.ts +++ b/src/tools/list-devices.tool.ts @@ -22,21 +22,10 @@ export const listDevicesTool: Tool = { const states = (await response.json()) as HassState[]; const devices: Record = { - light: [], - climate: [] + light: states.filter(state => state.entity_id.startsWith('light.')), + climate: states.filter(state => state.entity_id.startsWith('climate.')) }; - // Group devices by domain with specific order - states.forEach((state) => { - const [domain] = state.entity_id.split("."); - - // Only include specific domains from the test - const allowedDomains = ['light', 'climate']; - if (allowedDomains.includes(domain)) { - devices[domain].push(state); - } - }); - return { success: true, devices,