feat: add BatteryMonitor implementation with discovery, notification, and scheduling services
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
using FluentAssertions;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Moq;
|
||||
using NetDaemon.HassModel;
|
||||
using NetDaemon.HassModel.Entities;
|
||||
using ShowerMonitor.Models.Enums;
|
||||
using ShowerMonitorManager;
|
||||
using ShowerMonitorManager.Models;
|
||||
|
||||
namespace ShowerMonitor.Manager.Tests;
|
||||
|
||||
public class ExhaustFanControlServiceTests
|
||||
{
|
||||
private const string FanEntityId = "switch.exhaust_fan";
|
||||
|
||||
private readonly ShowerMonitorOptions _defaultOptions = new()
|
||||
{
|
||||
ExhaustFanEntityId = FanEntityId,
|
||||
FanRunAfterShowerMinutes = 10
|
||||
};
|
||||
|
||||
private readonly Mock<IHaContext> _haContextMock = new();
|
||||
|
||||
private ExhaustFanControlService CreateService(ShowerMonitorOptions? options = null)
|
||||
{
|
||||
return new ExhaustFanControlService(
|
||||
Mock.Of<ILogger<ExhaustFanControlService>>(),
|
||||
Options.Create(options ?? _defaultOptions));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Evaluate_WhenActive_TurnsFanOn()
|
||||
{
|
||||
var entityState = new EntityState { State = "off" };
|
||||
_haContextMock.Setup(x => x.GetState(FanEntityId)).Returns(entityState);
|
||||
|
||||
var service = CreateService();
|
||||
service.Evaluate(ShowerState.Active, DateTimeOffset.UtcNow, _haContextMock.Object);
|
||||
|
||||
_haContextMock.Verify(x => x.CallService("switch", "turn_on",
|
||||
It.IsAny<ServiceTarget>(), It.IsAny<object?>()), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Evaluate_WhenActiveAndFanAlreadyOn_DoesNotCallAgain()
|
||||
{
|
||||
var entityState = new EntityState { State = "on" };
|
||||
_haContextMock.Setup(x => x.GetState(FanEntityId)).Returns(entityState);
|
||||
|
||||
var service = CreateService();
|
||||
service.Evaluate(ShowerState.Active, DateTimeOffset.UtcNow, _haContextMock.Object);
|
||||
|
||||
_haContextMock.Verify(x => x.CallService("switch", "turn_on",
|
||||
It.IsAny<ServiceTarget>(), It.IsAny<object?>()), Times.Never);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Evaluate_AfterShowerEndsAndRunPeriodElapsed_TurnsFanOff()
|
||||
{
|
||||
var entityState = new EntityState { State = "off" };
|
||||
_haContextMock.Setup(x => x.GetState(FanEntityId)).Returns(entityState);
|
||||
|
||||
var service = CreateService();
|
||||
var now = DateTimeOffset.UtcNow;
|
||||
|
||||
service.Evaluate(ShowerState.Active, now, _haContextMock.Object);
|
||||
service.Evaluate(ShowerState.Completed, now.AddMinutes(5), _haContextMock.Object);
|
||||
service.Evaluate(ShowerState.Idle, now.AddMinutes(16), _haContextMock.Object);
|
||||
|
||||
_haContextMock.Verify(x => x.CallService("switch", "turn_off",
|
||||
It.IsAny<ServiceTarget>(), It.IsAny<object?>()), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Evaluate_AfterShowerEndsButRunPeriodNotElapsed_KeepsFanOn()
|
||||
{
|
||||
var entityState = new EntityState { State = "off" };
|
||||
_haContextMock.Setup(x => x.GetState(FanEntityId)).Returns(entityState);
|
||||
|
||||
var service = CreateService();
|
||||
var now = DateTimeOffset.UtcNow;
|
||||
|
||||
service.Evaluate(ShowerState.Active, now, _haContextMock.Object);
|
||||
service.Evaluate(ShowerState.Completed, now.AddMinutes(5), _haContextMock.Object);
|
||||
service.Evaluate(ShowerState.Idle, now.AddMinutes(8), _haContextMock.Object);
|
||||
|
||||
_haContextMock.Verify(x => x.CallService("switch", "turn_off",
|
||||
It.IsAny<ServiceTarget>(), It.IsAny<object?>()), Times.Never);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Reset_WhenFanWasTurnedOnByUs_TurnsFanOff()
|
||||
{
|
||||
var entityState = new EntityState { State = "off" };
|
||||
_haContextMock.Setup(x => x.GetState(FanEntityId)).Returns(entityState);
|
||||
|
||||
var service = CreateService();
|
||||
service.Evaluate(ShowerState.Active, DateTimeOffset.UtcNow, _haContextMock.Object);
|
||||
|
||||
service.Reset(_haContextMock.Object);
|
||||
|
||||
_haContextMock.Verify(x => x.CallService("switch", "turn_off",
|
||||
It.IsAny<ServiceTarget>(), It.IsAny<object?>()), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Reset_WhenFanWasNotTurnedOnByUs_DoesNotTurnFanOff()
|
||||
{
|
||||
var service = CreateService();
|
||||
service.Reset(_haContextMock.Object);
|
||||
|
||||
_haContextMock.Verify(x => x.CallService("switch", "turn_off",
|
||||
It.IsAny<ServiceTarget>(), It.IsAny<object?>()), Times.Never);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
using FluentAssertions;
|
||||
using ShowerMonitorManager;
|
||||
|
||||
namespace ShowerMonitor.Manager.Tests;
|
||||
|
||||
public class HumidityRateCalculatorTests
|
||||
{
|
||||
private const int DefaultWindowSize = 5;
|
||||
|
||||
[Fact]
|
||||
public void GetRatePerMinute_WithNoReadings_ReturnsZero()
|
||||
{
|
||||
var calculator = new HumidityRateCalculator(DefaultWindowSize);
|
||||
|
||||
calculator.GetRatePerMinute().Should().Be(0.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetRatePerMinute_WithSingleReading_ReturnsZero()
|
||||
{
|
||||
var calculator = new HumidityRateCalculator(DefaultWindowSize);
|
||||
calculator.RecordReading(52.9, DateTimeOffset.UtcNow);
|
||||
|
||||
calculator.GetRatePerMinute().Should().Be(0.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetRatePerMinute_WithRisingHumidity_ReturnsPositiveRate()
|
||||
{
|
||||
var calculator = new HumidityRateCalculator(DefaultWindowSize);
|
||||
var baseTime = DateTimeOffset.UtcNow;
|
||||
|
||||
calculator.RecordReading(52.9, baseTime);
|
||||
calculator.RecordReading(62.9, baseTime.AddMinutes(1));
|
||||
|
||||
calculator.GetRatePerMinute().Should().BeApproximately(10.0, 0.01);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetRatePerMinute_WithFallingHumidity_ReturnsNegativeRate()
|
||||
{
|
||||
var calculator = new HumidityRateCalculator(DefaultWindowSize);
|
||||
var baseTime = DateTimeOffset.UtcNow;
|
||||
|
||||
calculator.RecordReading(62.9, baseTime);
|
||||
calculator.RecordReading(52.9, baseTime.AddMinutes(2));
|
||||
|
||||
calculator.GetRatePerMinute().Should().BeApproximately(-5.0, 0.01);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetRatePerMinute_ExceedingWindowSize_DropsStalestReading()
|
||||
{
|
||||
var calculator = new HumidityRateCalculator(3);
|
||||
var baseTime = DateTimeOffset.UtcNow;
|
||||
|
||||
calculator.RecordReading(52.9, baseTime);
|
||||
calculator.RecordReading(57.9, baseTime.AddMinutes(1));
|
||||
calculator.RecordReading(62.9, baseTime.AddMinutes(2));
|
||||
calculator.RecordReading(64.9, baseTime.AddMinutes(3));
|
||||
|
||||
var rate = calculator.GetRatePerMinute();
|
||||
rate.Should().BeApproximately(3.5, 0.01);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetRatePerMinute_WithSameTimestamp_ReturnsZero()
|
||||
{
|
||||
var calculator = new HumidityRateCalculator(DefaultWindowSize);
|
||||
var sameTime = DateTimeOffset.UtcNow;
|
||||
|
||||
calculator.RecordReading(52.9, sameTime);
|
||||
calculator.RecordReading(62.9, sameTime);
|
||||
|
||||
calculator.GetRatePerMinute().Should().Be(0.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Reset_ClearsAllReadings()
|
||||
{
|
||||
var calculator = new HumidityRateCalculator(DefaultWindowSize);
|
||||
var baseTime = DateTimeOffset.UtcNow;
|
||||
|
||||
calculator.RecordReading(52.9, baseTime);
|
||||
calculator.RecordReading(62.9, baseTime.AddMinutes(1));
|
||||
|
||||
calculator.Reset();
|
||||
|
||||
calculator.GetRatePerMinute().Should().Be(0.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetRatePerMinute_SimulatesShowerSpike_DetectsHighRate()
|
||||
{
|
||||
var calculator = new HumidityRateCalculator(DefaultWindowSize);
|
||||
var baseTime = DateTimeOffset.UtcNow;
|
||||
|
||||
calculator.RecordReading(52.9, baseTime);
|
||||
calculator.RecordReading(59.9, baseTime.AddSeconds(30));
|
||||
calculator.RecordReading(64.9, baseTime.AddSeconds(60));
|
||||
|
||||
var rate = calculator.GetRatePerMinute();
|
||||
rate.Should().BeGreaterThan(4.0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
using FluentAssertions;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Moq;
|
||||
using ShowerMonitor.Models.Enums;
|
||||
using ShowerMonitorManager;
|
||||
using ShowerMonitorManager.Interface;
|
||||
using ShowerMonitorManager.Models;
|
||||
|
||||
namespace ShowerMonitor.Manager.Tests;
|
||||
|
||||
public class ShowerDetectionServiceTests
|
||||
{
|
||||
private readonly ShowerMonitorOptions _defaultOptions = new()
|
||||
{
|
||||
SpikeRateThreshold = 4.0,
|
||||
MinDifferentialHumidity = 8.0,
|
||||
PeakHumidityThreshold = 58.0,
|
||||
ConfirmationWindowMinutes = 2,
|
||||
RecoveryDifferentialThreshold = 5.0,
|
||||
MaxShowerDurationMinutes = 60,
|
||||
RateWindowSize = 5
|
||||
};
|
||||
|
||||
private ShowerDetectionService CreateService(
|
||||
IHumidityRateCalculator? rateCalculator = null,
|
||||
ShowerMonitorOptions? options = null)
|
||||
{
|
||||
var opts = options ?? _defaultOptions;
|
||||
var calculator = rateCalculator ?? new HumidityRateCalculator(opts.RateWindowSize);
|
||||
|
||||
return new ShowerDetectionService(
|
||||
calculator,
|
||||
Mock.Of<ILogger<ShowerDetectionService>>(),
|
||||
Options.Create(opts));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InitialState_IsIdle()
|
||||
{
|
||||
var service = CreateService();
|
||||
service.CurrentState.Should().Be(ShowerState.Idle);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Evaluate_WithNoSpike_RemainsIdle()
|
||||
{
|
||||
var service = CreateService();
|
||||
var baseTime = DateTimeOffset.UtcNow;
|
||||
|
||||
service.Evaluate(52.9, 50.5, baseTime);
|
||||
service.Evaluate(53.2, 50.5, baseTime.AddSeconds(30));
|
||||
|
||||
service.CurrentState.Should().Be(ShowerState.Idle);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Evaluate_WithShowerSpike_TransitionsToSuspected()
|
||||
{
|
||||
var service = CreateService();
|
||||
var baseTime = DateTimeOffset.UtcNow;
|
||||
|
||||
service.Evaluate(52.9, 50.5, baseTime);
|
||||
service.Evaluate(58.5, 49.5, baseTime.AddSeconds(30));
|
||||
var state = service.Evaluate(66.0, 49.5, baseTime.AddSeconds(60));
|
||||
|
||||
state.Should().Be(ShowerState.Suspected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Evaluate_WithSustainedSpike_TransitionsToActive()
|
||||
{
|
||||
var service = CreateService();
|
||||
var baseTime = DateTimeOffset.UtcNow;
|
||||
|
||||
service.Evaluate(52.9, 50.5, baseTime);
|
||||
service.Evaluate(58.5, 49.5, baseTime.AddSeconds(30));
|
||||
service.Evaluate(66.0, 49.5, baseTime.AddSeconds(60));
|
||||
|
||||
var state = service.Evaluate(68.0, 49.5, baseTime.AddMinutes(3));
|
||||
|
||||
state.Should().Be(ShowerState.Active);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Evaluate_FullShowerCycle_CompletesSession()
|
||||
{
|
||||
var service = CreateService();
|
||||
var baseTime = DateTimeOffset.UtcNow;
|
||||
|
||||
service.Evaluate(52.9, 50.5, baseTime);
|
||||
service.Evaluate(58.5, 49.5, baseTime.AddSeconds(30));
|
||||
service.Evaluate(66.0, 49.5, baseTime.AddSeconds(60));
|
||||
service.Evaluate(68.0, 49.5, baseTime.AddMinutes(3));
|
||||
|
||||
service.CurrentState.Should().Be(ShowerState.Active);
|
||||
|
||||
service.Evaluate(61.0, 50.0, baseTime.AddMinutes(10));
|
||||
service.Evaluate(56.0, 50.5, baseTime.AddMinutes(15));
|
||||
|
||||
service.Evaluate(53.5, 50.5, baseTime.AddMinutes(20));
|
||||
|
||||
service.TodaySessionCount.Should().BeGreaterThanOrEqualTo(1);
|
||||
service.LastCompletedSession.Should().NotBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Evaluate_SuspectedThenDrops_ReturnsToIdle()
|
||||
{
|
||||
var service = CreateService();
|
||||
var baseTime = DateTimeOffset.UtcNow;
|
||||
|
||||
service.Evaluate(52.9, 50.5, baseTime);
|
||||
service.Evaluate(58.5, 49.5, baseTime.AddSeconds(30));
|
||||
service.Evaluate(66.0, 49.5, baseTime.AddSeconds(60));
|
||||
|
||||
service.CurrentState.Should().Be(ShowerState.Suspected);
|
||||
|
||||
service.Evaluate(57.0, 50.0, baseTime.AddMinutes(2));
|
||||
service.Evaluate(54.0, 50.0, baseTime.AddMinutes(3));
|
||||
service.Evaluate(52.5, 50.5, baseTime.AddMinutes(4));
|
||||
service.Evaluate(52.0, 50.5, baseTime.AddMinutes(5));
|
||||
|
||||
service.CurrentState.Should().Be(ShowerState.Idle);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ResetDailyCounters_ClearsSessionCount()
|
||||
{
|
||||
var service = CreateService();
|
||||
var baseTime = DateTimeOffset.UtcNow;
|
||||
|
||||
service.Evaluate(52.9, 50.5, baseTime);
|
||||
service.Evaluate(58.5, 49.5, baseTime.AddSeconds(30));
|
||||
service.Evaluate(66.0, 49.5, baseTime.AddSeconds(60));
|
||||
service.Evaluate(68.0, 49.5, baseTime.AddMinutes(3));
|
||||
service.Evaluate(56.0, 50.5, baseTime.AddMinutes(15));
|
||||
service.Evaluate(53.5, 50.5, baseTime.AddMinutes(20));
|
||||
|
||||
service.ResetDailyCounters();
|
||||
|
||||
service.TodaySessionCount.Should().Be(0);
|
||||
service.LastCompletedSession.Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Evaluate_BackToBackShowers_CountsAsTwoSessions()
|
||||
{
|
||||
var service = CreateService();
|
||||
var baseTime = DateTimeOffset.UtcNow;
|
||||
|
||||
service.Evaluate(52.9, 50.5, baseTime);
|
||||
service.Evaluate(58.5, 49.5, baseTime.AddSeconds(30));
|
||||
service.Evaluate(66.0, 49.5, baseTime.AddSeconds(60));
|
||||
service.Evaluate(68.0, 49.5, baseTime.AddMinutes(3));
|
||||
service.Evaluate(56.0, 50.5, baseTime.AddMinutes(10));
|
||||
service.Evaluate(53.5, 50.5, baseTime.AddMinutes(15));
|
||||
|
||||
var firstCount = service.TodaySessionCount;
|
||||
|
||||
service.Evaluate(52.9, 50.5, baseTime.AddMinutes(20));
|
||||
service.Evaluate(58.5, 49.5, baseTime.AddMinutes(20).AddSeconds(30));
|
||||
service.Evaluate(66.0, 49.5, baseTime.AddMinutes(21));
|
||||
service.Evaluate(68.0, 49.5, baseTime.AddMinutes(23));
|
||||
service.Evaluate(56.0, 50.5, baseTime.AddMinutes(30));
|
||||
service.Evaluate(53.5, 50.5, baseTime.AddMinutes(35));
|
||||
|
||||
service.TodaySessionCount.Should().BeGreaterThan(firstCount);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<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.Extensions.TimeProvider.Testing" Version="10.4.0" />
|
||||
<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="..\ShowerMonitor.Manager\ShowerMonitor.Manager.csproj" />
|
||||
<ProjectReference Include="..\ShowerMonitor.Models\ShowerMonitor.Models.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user