feat: add BatteryMonitor implementation with discovery, notification, and scheduling services
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
using HomeAssistant.Entities.Interfaces;
|
||||
using HomeAssistant.Models;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace HomeAssistant.HistoryReplay.Replay;
|
||||
|
||||
public sealed class NoOpEntityManager(ILogger<NoOpEntityManager> logger) : IEntityManager
|
||||
{
|
||||
public Task CreateEntitiesAsync(ICollection<EntityCreateModel> entities)
|
||||
{
|
||||
logger.LogDebug("[REPLAY] Would create {Count} entities", entities.Count);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task CreateEntityAsync(EntityCreateModel createModel)
|
||||
{
|
||||
logger.LogDebug("[REPLAY] Would create entity: {EntityId}", createModel.EntityId);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task SetEntityState(string entityId, string state)
|
||||
{
|
||||
logger.LogDebug("[REPLAY] Would set state for {EntityId}: {State}", entityId, state);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task SetEntityAttributes(string entityId, object attributes)
|
||||
{
|
||||
logger.LogDebug("[REPLAY] Would set attributes for {EntityId}", entityId);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task RemoveEntity(string entityId)
|
||||
{
|
||||
logger.LogDebug("[REPLAY] Would remove entity: {EntityId}", entityId);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
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;
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
namespace HomeAssistant.HistoryReplay.Replay;
|
||||
|
||||
public sealed class ReplayTimeProvider : TimeProvider
|
||||
{
|
||||
private readonly Lock _lock = new();
|
||||
private DateTimeOffset _currentUtcTime;
|
||||
private readonly TimeZoneInfo _localTimeZone;
|
||||
|
||||
public ReplayTimeProvider(DateTimeOffset initialTime)
|
||||
{
|
||||
_currentUtcTime = initialTime.ToUniversalTime();
|
||||
_localTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time");
|
||||
}
|
||||
|
||||
public override DateTimeOffset GetUtcNow()
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
return _currentUtcTime;
|
||||
}
|
||||
}
|
||||
|
||||
public override TimeZoneInfo LocalTimeZone => _localTimeZone;
|
||||
|
||||
public void AdvanceTo(DateTimeOffset utcTime)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
_currentUtcTime = utcTime.ToUniversalTime();
|
||||
}
|
||||
}
|
||||
|
||||
public void AdvanceBy(TimeSpan duration)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
_currentUtcTime += duration;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user