114 lines
4.0 KiB
C#
114 lines
4.0 KiB
C#
using FluentAssertions;
|
|
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(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);
|
|
}
|
|
}
|