Files
HomeAssistant/ShowerMonitor.Manager/ExhaustFanControlService.cs
T
gitea_admin 41ac93733e Clear Qodana findings in monitor runtime code
- Guard logger calls with IsEnabled so value-type arguments are not boxed
  when the level is disabled
- Make helper members static, narrow write-only properties to init or
  private get, and delete members no call site reaches
- Flatten nested conditionals and convert if/else chains into switch and
  ternary form
- Drop the unused *MonitorInit interfaces; NetDaemon instantiates the
  concrete app classes
- Drop the unused IConfiguration parameter from AddNordPool and
  AddPowerMonitor, and the unused IEntityManager.CreateEntitiesAsync
- Remove the ignored parameters of the boiler run-time tracker stub
- Pin the NordPool DTO price mapping explicitly so the getter use is
  visible to static analysis
- Fail Qodana on any finding and record the registration changes in
  AGENTS.md
2026-11-29 21:42:47 +02:00

105 lines
2.9 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:
// Unknown state: leave the fan untouched.
break;
}
}
}
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;
}
}