mirror of
https://github.com/home-assistant/core.git
synced 2026-09-26 17:31:15 -04:00
Use ToolResult in calendar (#182539)
Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude
parent
b1f88df13b
commit
a0eb7e30f0
@@ -10,9 +10,14 @@ from homeassistant.components.homeassistant import async_should_expose
|
||||
from homeassistant.components.llm import LLMTools
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers import entity_registry as er, intent
|
||||
from homeassistant.helpers.llm import LLM_API_ASSIST, LLMContext, Tool, ToolInput
|
||||
from homeassistant.helpers.llm import (
|
||||
LLM_API_ASSIST,
|
||||
LLMContext,
|
||||
Tool,
|
||||
ToolInput,
|
||||
ToolResult,
|
||||
)
|
||||
from homeassistant.util import dt as dt_util
|
||||
from homeassistant.util.json import JsonObjectType
|
||||
|
||||
from . import SERVICE_GET_EVENTS
|
||||
from .const import DOMAIN
|
||||
@@ -40,7 +45,7 @@ class CalendarGetEventsTool(Tool):
|
||||
@override
|
||||
async def async_call(
|
||||
self, hass: HomeAssistant, tool_input: ToolInput, llm_context: LLMContext
|
||||
) -> JsonObjectType:
|
||||
) -> ToolResult:
|
||||
"""Query a calendar."""
|
||||
data = self.parameters(tool_input.tool_args)
|
||||
result = intent.async_match_targets(
|
||||
@@ -52,7 +57,7 @@ class CalendarGetEventsTool(Tool):
|
||||
),
|
||||
)
|
||||
if not result.is_match:
|
||||
return {"success": False, "error": "Calendar not found"}
|
||||
return ToolResult(data={"error": "Calendar not found"}, error=True)
|
||||
|
||||
entity_id = result.states[0].entity_id
|
||||
if data["range"] == "today":
|
||||
@@ -82,7 +87,7 @@ class CalendarGetEventsTool(Tool):
|
||||
for event in cast(dict, service_result)[entity_id]["events"]
|
||||
]
|
||||
|
||||
return {"success": True, "result": events}
|
||||
return ToolResult(data={"events": events})
|
||||
|
||||
|
||||
@callback
|
||||
|
||||
@@ -107,24 +107,25 @@ async def test_calendar_get_events_tool(hass: HomeAssistant) -> None:
|
||||
"end_date_time": dt_util.start_of_local_day(now) + timedelta(days=1),
|
||||
}
|
||||
|
||||
assert response == {
|
||||
"success": True,
|
||||
"result": [
|
||||
{
|
||||
"start": "2025-09-17",
|
||||
"end": "2025-09-18",
|
||||
"summary": "Home Assistant 12th birthday",
|
||||
"description": "",
|
||||
"all_day": True,
|
||||
},
|
||||
{
|
||||
"start": "2025-09-17T14:00:00-05:00",
|
||||
"end": "2025-09-18T15:00:00-05:00",
|
||||
"summary": "Champagne",
|
||||
"description": "",
|
||||
},
|
||||
],
|
||||
}
|
||||
assert response == llm.ToolResult(
|
||||
data={
|
||||
"events": [
|
||||
{
|
||||
"start": "2025-09-17",
|
||||
"end": "2025-09-18",
|
||||
"summary": "Home Assistant 12th birthday",
|
||||
"description": "",
|
||||
"all_day": True,
|
||||
},
|
||||
{
|
||||
"start": "2025-09-17T14:00:00-05:00",
|
||||
"end": "2025-09-18T15:00:00-05:00",
|
||||
"summary": "Champagne",
|
||||
"description": "",
|
||||
},
|
||||
]
|
||||
}
|
||||
)
|
||||
|
||||
# The "week" range searches seven days out.
|
||||
calls.clear()
|
||||
@@ -153,7 +154,7 @@ async def test_calendar_get_events_tool_not_found(hass: HomeAssistant) -> None:
|
||||
),
|
||||
llm_context,
|
||||
)
|
||||
assert response == {"success": False, "error": "Calendar not found"}
|
||||
assert response == llm.ToolResult(data={"error": "Calendar not found"}, error=True)
|
||||
|
||||
|
||||
async def test_calendar_get_events_tool_uses_aliases(
|
||||
|
||||
Reference in New Issue
Block a user