117 lines
3.9 KiB
C#
117 lines
3.9 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(_haContextMock.Object, 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.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.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);
|
|
service.Evaluate(ShowerState.Completed, now.AddMinutes(5));
|
|
service.Evaluate(ShowerState.Idle, now.AddMinutes(16));
|
|
|
|
_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);
|
|
service.Evaluate(ShowerState.Completed, now.AddMinutes(5));
|
|
service.Evaluate(ShowerState.Idle, now.AddMinutes(8));
|
|
|
|
_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);
|
|
|
|
var fanOnState = new EntityState { State = "on" };
|
|
_haContextMock.Setup(x => x.GetState(FanEntityId)).Returns(fanOnState);
|
|
|
|
service.Reset();
|
|
|
|
_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.Verify(x => x.CallService("switch", "turn_off",
|
|
It.IsAny<ServiceTarget>(), It.IsAny<object?>()), Times.Never);
|
|
}
|
|
}
|