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
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -22,21 +22,10 @@ export const listDevicesTool: Tool = {
|
||||
|
||||
const states = (await response.json()) as HassState[];
|
||||
const devices: Record<string, HassState[]> = {
|
||||
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,
|
||||
|
||||
Reference in New Issue
Block a user