cf8c9434c8
- Drop the leftover unused using, parameter and date-time attribute reader - Merge the remaining enum guard into its pattern - Make the NordPool price DTO init-only - Replace the four non-serializable theory matrices with single tests that iterate the same rows, so xUnit can still report them - Replace Array.Empty with collection expressions and drop unused locals, redundant default arguments and duplicated tuple names - Throw from the unreachable shower default instead of an empty section - Cover the configurable PowerMonitor option overrides in a test so the init setters have a caller
105 lines
3.1 KiB
C#
105 lines
3.1 KiB
C#
using Microsoft.Extensions.Options;
|
|
using NetDaemon.HassModel;
|
|
using NetDaemon.HassModel.Entities;
|
|
using ShowerMonitor.Models.Enums;
|
|
using ShowerMonitorManager.Interface;
|
|
using ShowerMonitorManager.Models;
|
|
|
|
namespace ShowerMonitorManager;
|
|
|
|
public sealed class ExhaustFanControlService(
|
|
IHaContext haContext,
|
|
IOptions<ShowerMonitorOptions> options) : IExhaustFanControlService
|
|
{
|
|
private readonly ShowerMonitorOptions _options = options.Value;
|
|
private readonly Lock _lock = new();
|
|
|
|
private bool _fanTurnedOnByUs;
|
|
private DateTimeOffset? _showerEndedAt;
|
|
|
|
public void Evaluate(ShowerState state, DateTimeOffset timestamp)
|
|
{
|
|
lock (_lock)
|
|
{
|
|
switch (state)
|
|
{
|
|
case ShowerState.Active:
|
|
case ShowerState.Suspected:
|
|
TurnOnFanIfNeeded();
|
|
_showerEndedAt = null;
|
|
break;
|
|
|
|
case ShowerState.Declining:
|
|
case ShowerState.Completed:
|
|
_showerEndedAt ??= timestamp;
|
|
EvaluatePostShowerFanOff(timestamp);
|
|
break;
|
|
|
|
case ShowerState.Idle:
|
|
EvaluatePostShowerFanOff(timestamp);
|
|
break;
|
|
|
|
default:
|
|
// Every ShowerState member is handled above; fail loudly if one is added.
|
|
throw new ArgumentOutOfRangeException(nameof(state), state, "Unhandled shower state.");
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Reset()
|
|
{
|
|
lock (_lock)
|
|
{
|
|
if (_fanTurnedOnByUs)
|
|
{
|
|
var fanState = haContext.GetState(_options.ExhaustFanEntityId)?.State;
|
|
|
|
if (string.Equals(fanState, "on", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
haContext.CallService("switch", "turn_off", ServiceTarget.FromEntity(_options.ExhaustFanEntityId));
|
|
}
|
|
}
|
|
|
|
_fanTurnedOnByUs = false;
|
|
_showerEndedAt = null;
|
|
}
|
|
}
|
|
|
|
private void TurnOnFanIfNeeded()
|
|
{
|
|
if (_fanTurnedOnByUs)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var fanState = haContext.GetState(_options.ExhaustFanEntityId)?.State;
|
|
if (string.Equals(fanState, "on", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
return;
|
|
}
|
|
|
|
haContext.CallService("switch", "turn_on", ServiceTarget.FromEntity(_options.ExhaustFanEntityId));
|
|
_fanTurnedOnByUs = true;
|
|
}
|
|
|
|
private void EvaluatePostShowerFanOff(DateTimeOffset timestamp)
|
|
{
|
|
if (!_fanTurnedOnByUs || !_showerEndedAt.HasValue)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var elapsed = timestamp - _showerEndedAt.Value;
|
|
var runAfterDuration = TimeSpan.FromMinutes(_options.FanRunAfterShowerMinutes);
|
|
|
|
if (elapsed < runAfterDuration)
|
|
{
|
|
return;
|
|
}
|
|
|
|
haContext.CallService("switch", "turn_off", ServiceTarget.FromEntity(_options.ExhaustFanEntityId));
|
|
_fanTurnedOnByUs = false;
|
|
_showerEndedAt = null;
|
|
}
|
|
}
|