feat: add BatteryMonitor implementation with discovery, notification, and scheduling services
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text.Json;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace HomeAssistant.HistoryReplay.HomeAssistantClient;
|
||||
|
||||
public sealed class HomeAssistantApiClient : IHomeAssistantApiClient
|
||||
{
|
||||
private readonly HttpClient _httpClient;
|
||||
|
||||
public HomeAssistantApiClient(HttpClient httpClient, IOptions<HomeAssistantSettings> settings)
|
||||
{
|
||||
_httpClient = httpClient;
|
||||
_httpClient.BaseAddress = settings.Value.BaseUrl;
|
||||
_httpClient.DefaultRequestHeaders.Authorization =
|
||||
new AuthenticationHeaderValue("Bearer", settings.Value.Token);
|
||||
_httpClient.DefaultRequestHeaders.Accept.Add(
|
||||
new MediaTypeWithQualityHeaderValue("application/json"));
|
||||
_httpClient.Timeout = TimeSpan.FromSeconds(HomeAssistantSettings.DefaultTimeoutSeconds);
|
||||
}
|
||||
|
||||
public async Task<JsonElement> GetHistoryAsync(
|
||||
string entityIds,
|
||||
DateTimeOffset? startTime = null,
|
||||
DateTimeOffset? endTime = null,
|
||||
bool minimalResponse = true,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var start = startTime ?? DateTimeOffset.UtcNow.AddDays(-1);
|
||||
var path = $"/api/history/period/{start:O}?filter_entity_id={Uri.EscapeDataString(entityIds)}";
|
||||
|
||||
if (endTime.HasValue)
|
||||
path += $"&end_time={Uri.EscapeDataString(endTime.Value.ToString("O"))}";
|
||||
|
||||
if (minimalResponse)
|
||||
path += "&minimal_response";
|
||||
|
||||
return await GetJsonAsync(path, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private async Task<JsonElement> GetJsonAsync(string path, CancellationToken cancellationToken)
|
||||
{
|
||||
var response = await _httpClient.GetAsync(path, cancellationToken).ConfigureAwait(false);
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
|
||||
var doc = await JsonDocument.ParseAsync(stream, cancellationToken: cancellationToken).ConfigureAwait(false);
|
||||
return doc.RootElement.Clone();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
namespace HomeAssistant.HistoryReplay.HomeAssistantClient;
|
||||
|
||||
public sealed class HomeAssistantSettings
|
||||
{
|
||||
public const string SectionName = "HomeAssistant";
|
||||
public const int DefaultPort = 8123;
|
||||
public const int DefaultTimeoutSeconds = 30;
|
||||
|
||||
public string Host { get; set; } = string.Empty;
|
||||
public int Port { get; set; } = DefaultPort;
|
||||
public string Token { get; set; } = string.Empty;
|
||||
public bool Ssl { get; set; }
|
||||
|
||||
public Uri BaseUrl => new UriBuilder(Ssl ? "https" : "http", Host, Port).Uri;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using System.Text.Json;
|
||||
|
||||
namespace HomeAssistant.HistoryReplay.HomeAssistantClient;
|
||||
|
||||
public interface IHomeAssistantApiClient
|
||||
{
|
||||
Task<JsonElement> GetHistoryAsync(
|
||||
string entityIds,
|
||||
DateTimeOffset? startTime = null,
|
||||
DateTimeOffset? endTime = null,
|
||||
bool minimalResponse = true,
|
||||
CancellationToken cancellationToken = default);
|
||||
}
|
||||
Reference in New Issue
Block a user