using System.ComponentModel; using System.Text.Json; using HomeAssistant.McpServer.Services; using ModelContextProtocol.Server; namespace HomeAssistant.McpServer.Tools; [McpServerToolType] public sealed class LogbookTools { private readonly IHomeAssistantApiClient _client; public LogbookTools(IHomeAssistantApiClient client) { _client = client; } [McpServerTool, Description( "Get Home Assistant logbook entries. Shows state changes and events for entities. " + "Useful for understanding what happened and when. Filter by entity and/or time range.")] public async Task GetLogbook( [Description("Optional entity ID to filter, e.g. 'switch.boiler'. Leave empty for all.")] string? entityId = null, [Description("Start time in ISO 8601 format. Defaults to 24 hours ago.")] string? startTime = null, [Description("End time in ISO 8601 format. Defaults to now.")] string? endTime = null, 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.GetLogbookAsync(entityId, start, end, cancellationToken) .ConfigureAwait(false); return JsonSerializer.Serialize(result, SerializerOptions); } private static readonly JsonSerializerOptions SerializerOptions = new() { WriteIndented = true }; }