Files
MeterVault/src/Infrastructure/Ingestion/HomeAssistantWorker.cs
T
schmidt.florian 8550ed8d9e
ci / build-test (push) Successful in 1m16s
Ingestion: Home Assistant WebSocket push path
HomeAssistantWebSocketWorker holds a persistent state_changed subscription per HA endpoint that opts in via the connector's WebSocket toggle (HaEndpointConfig.UseWebSocket): auth handshake, subscribe, ingest in real time, capped-backoff reconnect. The REST poll worker skips WS endpoints so each is served once. HaWebSocketProtocol holds the pure handshake/parse logic. Verified by 11 protocol unit tests + a live integration test against an in-process fake HA server. CLAUDE.md updated.

Claude-Session: https://claude.ai/code/session_01Kib2MniVFbD95fkgLgBBnB
2026-07-17 11:05:01 +02:00

131 lines
5.2 KiB
C#

using System.Collections.Concurrent;
using MeterVault.Core.Domain;
using MeterVault.Infrastructure.Persistence;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace MeterVault.Infrastructure.Ingestion;
/// <summary>
/// Polls Home Assistant entity states on each source's configured interval and ingests them
/// (SDD §6.2 REST fallback). Home Assistant can alternatively push to MQTT (handled by
/// <see cref="MqttIngestionWorker"/>) or push to the app's REST API. Tokens are resolved from
/// environment variables named in the endpoint config — never stored in the database.
/// </summary>
public sealed class HomeAssistantWorker(
IServiceScopeFactory scopeFactory,
IHttpClientFactory httpClientFactory,
ILogger<HomeAssistantWorker> logger) : BackgroundService
{
private static readonly TimeSpan TickInterval = TimeSpan.FromSeconds(10);
private readonly IServiceScopeFactory _scopeFactory = scopeFactory;
private readonly IHttpClientFactory _httpClientFactory = httpClientFactory;
private readonly ILogger<HomeAssistantWorker> _logger = logger;
private readonly ConcurrentDictionary<int, DateTimeOffset> _nextPoll = new();
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
using var timer = new PeriodicTimer(TickInterval);
do
{
try
{
await PollDueSourcesAsync(stoppingToken).ConfigureAwait(false);
}
catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested)
{
break;
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Home Assistant poll tick failed; will retry");
}
}
while (await timer.WaitForNextTickAsync(stoppingToken).ConfigureAwait(false));
}
private async Task PollDueSourcesAsync(CancellationToken cancellationToken)
{
await using var scope = _scopeFactory.CreateAsyncScope();
var db = scope.ServiceProvider.GetRequiredService<MeterVaultDbContext>();
var ingestion = scope.ServiceProvider.GetRequiredService<IngestionService>();
var client = new HaStateClient(_httpClientFactory.CreateClient());
// Endpoints using the WebSocket push path are served by HomeAssistantWebSocketWorker; skip
// them here so a source is never both polled and pushed.
var enabled = await db.IngestionEndpoints
.Where(e => e.IsEnabled && e.Type == EndpointType.HomeAssistant)
.ToListAsync(cancellationToken).ConfigureAwait(false);
var endpoints = enabled
.Where(e => !HaEndpointConfig.Parse(e.Config).UseWebSocket)
.ToDictionary(e => e.Id);
if (endpoints.Count == 0)
{
return;
}
var sources = await db.MeterSources
.Where(s => s.IsEnabled && s.SourceType == SourceType.HomeAssistant && s.EndpointId != null)
.ToListAsync(cancellationToken).ConfigureAwait(false);
// Drop schedule entries for sources that are gone/disabled so the dictionary doesn't grow.
var liveIds = sources.Select(s => s.Id).ToHashSet();
foreach (var staleId in _nextPoll.Keys.Where(id => !liveIds.Contains(id)).ToList())
{
_nextPoll.TryRemove(staleId, out _);
}
var now = DateTimeOffset.UtcNow;
foreach (var source in sources)
{
if (_nextPoll.TryGetValue(source.Id, out var due) && due > now)
{
continue;
}
if (source.EndpointId is not { } endpointId || !endpoints.TryGetValue(endpointId, out var endpoint))
{
continue;
}
var config = SourceConfig.Parse(source.Config);
var interval = TimeSpan.FromSeconds(Math.Max(5, config.PollSeconds ?? 60));
_nextPoll[source.Id] = now + interval;
await PollSourceAsync(client, ingestion, endpoint, source, config, cancellationToken).ConfigureAwait(false);
}
}
private async Task PollSourceAsync(
HaStateClient client, IngestionService ingestion, IngestionEndpoint endpoint,
MeterSource source, SourceConfig config, CancellationToken cancellationToken)
{
var endpointConfig = HaEndpointConfig.Parse(endpoint.Config);
var token = endpointConfig.ResolveToken();
if (string.IsNullOrWhiteSpace(endpointConfig.BaseUrl) || string.IsNullOrWhiteSpace(token)
|| string.IsNullOrWhiteSpace(config.EntityId))
{
return;
}
try
{
var state = await client.GetStateAsync(
endpointConfig.BaseUrl, token, config.EntityId, config.Attribute, cancellationToken).ConfigureAwait(false);
if (state is { } value)
{
await ingestion.IngestAsync(source.Id, value.Time, value.Value, cancellationToken).ConfigureAwait(false);
}
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Home Assistant poll failed for entity {EntityId}", config.EntityId);
}
}
}