feat: add BatteryMonitor implementation with discovery, notification, and scheduling services
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" Version="8.0.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.3.0" />
|
||||
<PackageReference Include="xunit" Version="2.9.3" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="FluentAssertions" Version="8.8.0" />
|
||||
<PackageReference Include="Moq" Version="4.20.72" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="Xunit" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\LightMonitor.Manager\LightMonitor.Manager.csproj" />
|
||||
<ProjectReference Include="..\LightMonitor.Models\LightMonitor.Models.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,146 @@
|
||||
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 PresenceServiceTests
|
||||
{
|
||||
private readonly Mock<IHaContext> _haContextMock = new();
|
||||
|
||||
private readonly LightMonitorOptions _defaultOptions = new()
|
||||
{
|
||||
PersonEntityIds = ["person.marius", "person.erika", "person.home_phone"]
|
||||
};
|
||||
|
||||
private PresenceService CreateService(LightMonitorOptions? options = null)
|
||||
{
|
||||
return new PresenceService(
|
||||
_haContextMock.Object,
|
||||
Mock.Of<ILogger<PresenceService>>(),
|
||||
Options.Create(options ?? _defaultOptions));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsAnyoneHome_WhenOnePersonIsHome_ShouldReturnTrue()
|
||||
{
|
||||
_haContextMock.Setup(x => x.GetState("person.marius"))
|
||||
.Returns(new EntityState { State = "home" });
|
||||
_haContextMock.Setup(x => x.GetState("person.erika"))
|
||||
.Returns(new EntityState { State = "not_home" });
|
||||
_haContextMock.Setup(x => x.GetState("person.home_phone"))
|
||||
.Returns(new EntityState { State = "not_home" });
|
||||
|
||||
var service = CreateService();
|
||||
|
||||
service.IsAnyoneHome().Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsAnyoneHome_WhenAllPersonsAreHome_ShouldReturnTrue()
|
||||
{
|
||||
_haContextMock.Setup(x => x.GetState("person.marius"))
|
||||
.Returns(new EntityState { State = "home" });
|
||||
_haContextMock.Setup(x => x.GetState("person.erika"))
|
||||
.Returns(new EntityState { State = "home" });
|
||||
_haContextMock.Setup(x => x.GetState("person.home_phone"))
|
||||
.Returns(new EntityState { State = "home" });
|
||||
|
||||
var service = CreateService();
|
||||
|
||||
service.IsAnyoneHome().Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsAnyoneHome_WhenNoPersonIsHome_ShouldReturnFalse()
|
||||
{
|
||||
_haContextMock.Setup(x => x.GetState("person.marius"))
|
||||
.Returns(new EntityState { State = "not_home" });
|
||||
_haContextMock.Setup(x => x.GetState("person.erika"))
|
||||
.Returns(new EntityState { State = "not_home" });
|
||||
_haContextMock.Setup(x => x.GetState("person.home_phone"))
|
||||
.Returns(new EntityState { State = "not_home" });
|
||||
|
||||
var service = CreateService();
|
||||
|
||||
service.IsAnyoneHome().Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsAnyoneHome_WhenStateIsUnavailable_ShouldSkipAndReturnFalse()
|
||||
{
|
||||
_haContextMock.Setup(x => x.GetState("person.marius"))
|
||||
.Returns(new EntityState { State = "unavailable" });
|
||||
_haContextMock.Setup(x => x.GetState("person.erika"))
|
||||
.Returns(new EntityState { State = "unknown" });
|
||||
_haContextMock.Setup(x => x.GetState("person.home_phone"))
|
||||
.Returns((EntityState?)null);
|
||||
|
||||
var service = CreateService();
|
||||
|
||||
service.IsAnyoneHome().Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsAnyoneHome_WhenStateIsNull_ShouldSkipAndReturnFalse()
|
||||
{
|
||||
_haContextMock.Setup(x => x.GetState("person.marius"))
|
||||
.Returns((EntityState?)null);
|
||||
_haContextMock.Setup(x => x.GetState("person.erika"))
|
||||
.Returns((EntityState?)null);
|
||||
_haContextMock.Setup(x => x.GetState("person.home_phone"))
|
||||
.Returns((EntityState?)null);
|
||||
|
||||
var service = CreateService();
|
||||
|
||||
service.IsAnyoneHome().Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsAnyoneHome_WhenMixedStatesWithOneHome_ShouldReturnTrue()
|
||||
{
|
||||
_haContextMock.Setup(x => x.GetState("person.marius"))
|
||||
.Returns(new EntityState { State = "unavailable" });
|
||||
_haContextMock.Setup(x => x.GetState("person.erika"))
|
||||
.Returns(new EntityState { State = "home" });
|
||||
_haContextMock.Setup(x => x.GetState("person.home_phone"))
|
||||
.Returns((EntityState?)null);
|
||||
|
||||
var service = CreateService();
|
||||
|
||||
service.IsAnyoneHome().Should().BeTrue();
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("HOME")]
|
||||
[InlineData("Home")]
|
||||
[InlineData("home")]
|
||||
public void IsAnyoneHome_CaseInsensitiveHomeState_ShouldReturnTrue(string homeState)
|
||||
{
|
||||
_haContextMock.Setup(x => x.GetState("person.marius"))
|
||||
.Returns(new EntityState { State = homeState });
|
||||
_haContextMock.Setup(x => x.GetState("person.erika"))
|
||||
.Returns(new EntityState { State = "not_home" });
|
||||
_haContextMock.Setup(x => x.GetState("person.home_phone"))
|
||||
.Returns(new EntityState { State = "not_home" });
|
||||
|
||||
var service = CreateService();
|
||||
|
||||
service.IsAnyoneHome().Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsAnyoneHome_WithEmptyPersonList_ShouldReturnFalse()
|
||||
{
|
||||
var options = new LightMonitorOptions { PersonEntityIds = [] };
|
||||
var service = CreateService(options);
|
||||
|
||||
service.IsAnyoneHome().Should().BeFalse();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user