f0bbd4f10b
PowerMonitor now ships enabled with power-limit enforcement settings: watt limit, recovery and cooldown windows, and switch-off plus pause/restore entity lists. Remove the unused BoilerRunTimeTracker, its DI registration and tests, the StatusCleanedUp constant, and the boiler replay runner; simplify BoilerConfigReader, control-service logging and entity parsing. Switch CostAccumulator to System.Threading.Lock, update the Shower exhaust fan service and tests, drop the legacy HistoryReplay/McpServer sources and reporting tooling, and expand AGENTS.md with the current project wiring and DI notes.
117 lines
3.9 KiB
C#
117 lines
3.9 KiB
C#
using LightMonitor.Models;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
using Moq;
|
|
using NetDaemon.HassModel;
|
|
using NetDaemon.HassModel.Entities;
|
|
using LightMonitorManager;
|
|
using LightMonitorManager.Models;
|
|
|
|
namespace LightMonitor.Manager.Tests;
|
|
|
|
public class LightControlServiceTests
|
|
{
|
|
private const string LightEntityId = "light.sonoff_1002214d40";
|
|
private const string SunEntityId = "sun.sun";
|
|
|
|
private readonly LightMonitorOptions _defaultOptions = new()
|
|
{
|
|
LightEntityId = LightEntityId,
|
|
SunEntityId = SunEntityId
|
|
};
|
|
|
|
private readonly Mock<IHaContext> _haContextMock = new();
|
|
|
|
private LightControlService CreateService(LightMonitorOptions? options = null)
|
|
{
|
|
return new LightControlService(
|
|
_haContextMock.Object,
|
|
Mock.Of<ILogger<LightControlService>>(),
|
|
Options.Create(options ?? _defaultOptions));
|
|
}
|
|
|
|
[Fact]
|
|
public void TurnOnLight_WhenDarkAndLightIsOff_ShouldTurnOnLight()
|
|
{
|
|
_haContextMock.Setup(x => x.GetState(SunEntityId))
|
|
.Returns(new EntityState { State = LightMonitorConsts.SunBelowHorizon });
|
|
_haContextMock.Setup(x => x.GetState(LightEntityId))
|
|
.Returns(new EntityState { State = "off" });
|
|
|
|
var service = CreateService();
|
|
service.TurnOnLight();
|
|
|
|
_haContextMock.Verify(x => x.CallService("light", "turn_on",
|
|
It.IsAny<ServiceTarget>(), It.IsAny<object?>()), Times.Once);
|
|
}
|
|
|
|
[Fact]
|
|
public void TurnOnLight_WhenDarkAndLightIsAlreadyOn_ShouldNotCallService()
|
|
{
|
|
_haContextMock.Setup(x => x.GetState(SunEntityId))
|
|
.Returns(new EntityState { State = LightMonitorConsts.SunBelowHorizon });
|
|
_haContextMock.Setup(x => x.GetState(LightEntityId))
|
|
.Returns(new EntityState { State = "on" });
|
|
|
|
var service = CreateService();
|
|
service.TurnOnLight();
|
|
|
|
_haContextMock.Verify(x => x.CallService("light", "turn_on",
|
|
It.IsAny<ServiceTarget>(), It.IsAny<object?>()), Times.Never);
|
|
}
|
|
|
|
[Fact]
|
|
public void TurnOnLight_WhenSunIsAboveHorizon_ShouldNotTurnOnLight()
|
|
{
|
|
_haContextMock.Setup(x => x.GetState(SunEntityId))
|
|
.Returns(new EntityState { State = LightMonitorConsts.SunAboveHorizon });
|
|
|
|
var service = CreateService();
|
|
service.TurnOnLight();
|
|
|
|
_haContextMock.Verify(x => x.CallService("light", "turn_on",
|
|
It.IsAny<ServiceTarget>(), It.IsAny<object?>()), Times.Never);
|
|
}
|
|
|
|
[Fact]
|
|
public void TurnOnLight_WhenSunStateIsNull_ShouldNotTurnOnLight()
|
|
{
|
|
_haContextMock.Setup(x => x.GetState(SunEntityId))
|
|
.Returns((EntityState?)null);
|
|
|
|
var service = CreateService();
|
|
service.TurnOnLight();
|
|
|
|
_haContextMock.Verify(x => x.CallService("light", "turn_on",
|
|
It.IsAny<ServiceTarget>(), It.IsAny<object?>()), Times.Never);
|
|
}
|
|
|
|
[Fact]
|
|
public void TurnOnLight_WhenSunStateIsUnavailable_ShouldNotTurnOnLight()
|
|
{
|
|
_haContextMock.Setup(x => x.GetState(SunEntityId))
|
|
.Returns(new EntityState { State = "unavailable" });
|
|
|
|
var service = CreateService();
|
|
service.TurnOnLight();
|
|
|
|
_haContextMock.Verify(x => x.CallService("light", "turn_on",
|
|
It.IsAny<ServiceTarget>(), It.IsAny<object?>()), Times.Never);
|
|
}
|
|
|
|
[Fact]
|
|
public void TurnOnLight_WhenLightStateIsNull_ShouldTurnOnLight()
|
|
{
|
|
_haContextMock.Setup(x => x.GetState(SunEntityId))
|
|
.Returns(new EntityState { State = LightMonitorConsts.SunBelowHorizon });
|
|
_haContextMock.Setup(x => x.GetState(LightEntityId))
|
|
.Returns((EntityState?)null);
|
|
|
|
var service = CreateService();
|
|
service.TurnOnLight();
|
|
|
|
_haContextMock.Verify(x => x.CallService("light", "turn_on",
|
|
It.IsAny<ServiceTarget>(), It.IsAny<object?>()), Times.Once);
|
|
}
|
|
}
|