using System.ComponentModel; using System.Text.Json; using HomeAssistant.McpServer.Services; using ModelContextProtocol.Server; namespace HomeAssistant.McpServer.Tools; [McpServerToolType] public sealed class HistoryTools { private readonly IHomeAssistantApiClient _client; public HistoryTools(IHomeAssistantApiClient client) { _client = client; } [McpServerTool, Description( "Get state history for one or more Home Assistant entities over a time period. " + "Returns timestamped state changes. Useful for debugging sensor readings, switch toggling, price changes, etc.")] public async Task GetEntityHistory( [Description("Comma-separated entity IDs, e.g. 'sensor.temperature,sensor.humidity'")] string entityIds, [Description("Start time in ISO 8601 format. Defaults to 24 hours ago. Example: '2026-03-30T00:00:00'")] string? startTime = null, [Description("End time in ISO 8601 format. Defaults to now. Example: '2026-03-31T00:00:00'")] string? endTime = null, [Description("If true, returns minimal data (state + last_changed only). Defaults to true.")] bool minimalResponse = true, CancellationToken cancellationToken = default) { DateTimeOffset? start = string.IsNullOrWhiteSpace(startTime) ? null : DateTimeOffset.Parse(startTime); DateTimeOffset? end = string.IsNullOrWhiteSpace(endTime) ? null : DateTimeOffset.Parse(endTime); var result = await _client.GetHistoryAsync(entityIds, start, end, minimalResponse, cancellationToken) .ConfigureAwait(false); return JsonSerializer.Serialize(result, SerializerOptions); } private static readonly JsonSerializerOptions SerializerOptions = new() { WriteIndented = true }; }