M3: live ingestion (MQTT/Tasmota + Home Assistant)

- PayloadExtractor: dot-path value/time extraction (Tasmota ENERGY.Total, bare scalars).
- MqttTopicMatcher: standard +/# wildcard matching.
- IngestionService: scale/offset, idempotent upsert on (meter_id, time), and a spurious-
  decrease guard for monotonic registers (allowed only with a reset/swap event) + source
  last-seen status.
- MqttMessageRouter + MqttIngestionWorker (MQTTnet 5): per-endpoint persistent connections,
  topic subscription, graceful degradation; secrets resolved by env-var reference.
- Home Assistant: HaStateClient (REST /api/states parse) + HomeAssistantWorker polling on
  each source's interval. HA-via-MQTT also works through the MQTT path.
- Ingestion workers gated by MeterVault:EnableLiveIngestion (off in tests).

85 tests green (53 Core + 32 integration): Tasmota payload → reading verified end to end.

Follow-up (polish): HA WebSocket push (state_changed) as an alternative to REST poll;
source-topic index caching in the router.

Claude-Session: https://claude.ai/code/session_01WujdMtMJPbxDpDnMeK22rr
This commit is contained in:
2026-07-13 11:45:20 +02:00
parent 5977c81002
commit 4b0cad67df
17 changed files with 1060 additions and 0 deletions
@@ -0,0 +1,145 @@
using System.Collections.Concurrent;
using System.Text.Json;
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());
var endpoints = await db.IngestionEndpoints
.Where(e => e.IsEnabled && e.Type == EndpointType.HomeAssistant)
.ToDictionaryAsync(e => e.Id, cancellationToken).ConfigureAwait(false);
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);
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 = ParseHaEndpoint(endpoint.Config);
var token = string.IsNullOrWhiteSpace(endpointConfig.TokenEnv)
? null
: Environment.GetEnvironmentVariable(endpointConfig.TokenEnv);
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);
}
}
private static HaEndpoint ParseHaEndpoint(string? json)
{
if (string.IsNullOrWhiteSpace(json))
{
return new HaEndpoint();
}
try
{
return JsonSerializer.Deserialize<HaEndpoint>(json,
new JsonSerializerOptions { PropertyNameCaseInsensitive = true }) ?? new HaEndpoint();
}
catch (JsonException)
{
return new HaEndpoint();
}
}
private sealed record HaEndpoint
{
public string? BaseUrl { get; init; }
public string? TokenEnv { get; init; }
}
}