feat: add BatteryMonitor implementation with discovery, notification, and scheduling services

This commit is contained in:
Marius
2026-04-01 10:55:33 +03:00
parent 11c2574235
commit 6e4d684359
114 changed files with 22820 additions and 527 deletions
@@ -0,0 +1,117 @@
using FluentAssertions;
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);
}
}