99 lines
3.1 KiB
C#
99 lines
3.1 KiB
C#
using System.Reactive.Linq;
|
|
using System.Text.Json;
|
|
using HomeAssistant.HistoryReplay.HistoryData;
|
|
using HomeAssistant.HistoryReplay.Reporting;
|
|
using Microsoft.Extensions.Logging;
|
|
using NetDaemon.HassModel;
|
|
using NetDaemon.HassModel.Entities;
|
|
|
|
namespace HomeAssistant.HistoryReplay.Replay;
|
|
|
|
public sealed class ReplayHaContext : IHaContext
|
|
{
|
|
private readonly HistoryTimeline _timeline;
|
|
private readonly ReplayTimeProvider _timeProvider;
|
|
private readonly ReplayTimeline _replayTimeline;
|
|
private readonly ILogger<ReplayHaContext> _logger;
|
|
|
|
public ReplayHaContext(
|
|
HistoryTimeline timeline,
|
|
ReplayTimeProvider timeProvider,
|
|
ReplayTimeline replayTimeline,
|
|
ILogger<ReplayHaContext> logger)
|
|
{
|
|
_timeline = timeline;
|
|
_timeProvider = timeProvider;
|
|
_replayTimeline = replayTimeline;
|
|
_logger = logger;
|
|
}
|
|
|
|
public EntityState? GetState(string entityId)
|
|
{
|
|
var currentTime = _timeProvider.GetUtcNow();
|
|
var entry = _timeline.GetStateAt(entityId, currentTime);
|
|
|
|
if (entry is null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
JsonElement? attributesJson = null;
|
|
if (entry.Attributes is { Count: > 0 })
|
|
{
|
|
attributesJson = JsonSerializer.SerializeToElement(entry.Attributes);
|
|
}
|
|
|
|
return new EntityState
|
|
{
|
|
State = entry.State,
|
|
AttributesJson = attributesJson
|
|
};
|
|
}
|
|
|
|
public IReadOnlyList<Entity> GetAllEntities()
|
|
{
|
|
return _timeline.GetEntityIds()
|
|
.Select(id => new Entity(this, id))
|
|
.ToList();
|
|
}
|
|
|
|
public void CallService(string domain, string service, ServiceTarget? target = null, object? data = null)
|
|
{
|
|
var currentTime = _timeProvider.GetUtcNow();
|
|
var targetDescription = target?.EntityIds is not null
|
|
? string.Join(", ", target.EntityIds)
|
|
: "no target";
|
|
|
|
_replayTimeline.AddEvent(new ReplayEvent
|
|
{
|
|
Timestamp = currentTime,
|
|
Monitor = "ServiceCall",
|
|
EventType = ReplayEventType.ServiceCall,
|
|
Message = $"Would call {domain}.{service} on {targetDescription}",
|
|
Details = data is not null ? JsonSerializer.Serialize(data) : null
|
|
});
|
|
|
|
_logger.LogDebug("[REPLAY] {Domain}.{Service} → {Target}", domain, service, targetDescription);
|
|
}
|
|
|
|
public void SendEvent(string eventType, object? data = null)
|
|
{
|
|
_logger.LogDebug("[REPLAY] Would send event: {EventType}", eventType);
|
|
}
|
|
|
|
public IObservable<StateChange> StateAllChanges() => Observable.Empty<StateChange>();
|
|
|
|
public IObservable<Event> Events => Observable.Empty<Event>();
|
|
|
|
public Task<JsonElement?> CallServiceWithResponseAsync(
|
|
string domain, string service, ServiceTarget? target = null, object? data = null)
|
|
{
|
|
CallService(domain, service, target, data);
|
|
return Task.FromResult<JsonElement?>(null);
|
|
}
|
|
|
|
public Area? GetAreaFromEntityId(string entityId) => null;
|
|
|
|
public EntityRegistration? GetEntityRegistration(string entityId) => null;
|
|
}
|