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
This commit is contained in:
@@ -13,7 +13,7 @@ using PowerMonitorManager.Models.Consts;
|
||||
namespace PowerMonitorManager;
|
||||
|
||||
[NetDaemonApp]
|
||||
public sealed class PowerMonitorInit : IPowerMonitorInit
|
||||
public sealed class PowerMonitorInit
|
||||
{
|
||||
private readonly ILogger<PowerMonitorInit> _logger;
|
||||
private readonly IHaContext _haContext;
|
||||
@@ -84,7 +84,7 @@ public sealed class PowerMonitorInit : IPowerMonitorInit
|
||||
}
|
||||
}
|
||||
|
||||
public async Task InitializeAsync()
|
||||
private async Task InitializeAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -98,7 +98,10 @@ public sealed class PowerMonitorInit : IPowerMonitorInit
|
||||
var sensors = GetPowerSensorEntitiesNames();
|
||||
await _entityService.CreateDeviceEntitiesAsync(sensors);
|
||||
|
||||
_logger.LogInformation("PowerMonitor initialized with {SensorCount} tracked sensors", sensors.Count);
|
||||
if (_logger.IsEnabled(LogLevel.Information))
|
||||
{
|
||||
_logger.LogInformation("PowerMonitor initialized with {SensorCount} tracked sensors", sensors.Count);
|
||||
}
|
||||
|
||||
await PollAndUpdateAsync();
|
||||
}
|
||||
@@ -116,7 +119,7 @@ public sealed class PowerMonitorInit : IPowerMonitorInit
|
||||
return sensors;
|
||||
}
|
||||
|
||||
public async Task CleanupEntitiesAsync(CancellationToken cancellationToken = default)
|
||||
private async Task CleanupEntitiesAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
await _entityService.RemoveSystemEntitiesAsync(cancellationToken);
|
||||
}
|
||||
@@ -141,7 +144,10 @@ public sealed class PowerMonitorInit : IPowerMonitorInit
|
||||
_ = PollAndUpdateAsync();
|
||||
});
|
||||
|
||||
_logger.LogInformation("PowerMonitor subscribed to state changes of {MeterId}", _options.MainMeterEntityId);
|
||||
if (_logger.IsEnabled(LogLevel.Information))
|
||||
{
|
||||
_logger.LogInformation("PowerMonitor subscribed to state changes of {MeterId}", _options.MainMeterEntityId);
|
||||
}
|
||||
}
|
||||
|
||||
private void SubscribeToRediscoverButton()
|
||||
@@ -190,7 +196,10 @@ public sealed class PowerMonitorInit : IPowerMonitorInit
|
||||
if (added.Count > 0)
|
||||
{
|
||||
await _entityService.CreateDeviceEntitiesAsync(added);
|
||||
_logger.LogInformation("Created entities for {Count} new sensors", added.Count);
|
||||
if (_logger.IsEnabled(LogLevel.Information))
|
||||
{
|
||||
_logger.LogInformation("Created entities for {Count} new sensors", added.Count);
|
||||
}
|
||||
}
|
||||
|
||||
if (removed.Count > 0)
|
||||
@@ -198,7 +207,10 @@ public sealed class PowerMonitorInit : IPowerMonitorInit
|
||||
await RemoreDevicePriceEntities(removed);
|
||||
}
|
||||
|
||||
_logger.LogInformation("Re-discovery complete: {Total} sensors ({Added} added, {Removed} removed)", newSensors.Count, added.Count, removed.Count);
|
||||
if (_logger.IsEnabled(LogLevel.Information))
|
||||
{
|
||||
_logger.LogInformation("Re-discovery complete: {Total} sensors ({Added} added, {Removed} removed)", newSensors.Count, added.Count, removed.Count);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -211,7 +223,10 @@ public sealed class PowerMonitorInit : IPowerMonitorInit
|
||||
var removedNames = removed.Select(s => s.DeviceName).ToList();
|
||||
await _entityService.RemoveDeviceEntitiesAsync(removedNames);
|
||||
|
||||
_logger.LogInformation("Removed entities for {Count} sensors", removed.Count);
|
||||
if (_logger.IsEnabled(LogLevel.Information))
|
||||
{
|
||||
_logger.LogInformation("Removed entities for {Count} sensors", removed.Count);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task PollAndUpdateAsync()
|
||||
@@ -247,15 +262,13 @@ public sealed class PowerMonitorInit : IPowerMonitorInit
|
||||
_costService.RecordCosts(calculation, nordPoolPrice, (int)Math.Max(1, elapsedSeconds), now.LocalDateTime);
|
||||
|
||||
var isAnomaly = calculation.SmoothedOtherPowerWatts > _options.AnomalyThresholdWatts;
|
||||
|
||||
if (isAnomaly && _anomalyStartTime is null)
|
||||
|
||||
_anomalyStartTime = isAnomaly switch
|
||||
{
|
||||
_anomalyStartTime = now;
|
||||
}
|
||||
else if (!isAnomaly)
|
||||
{
|
||||
_anomalyStartTime = null;
|
||||
}
|
||||
true when _anomalyStartTime is null => now,
|
||||
false => null,
|
||||
_ => _anomalyStartTime
|
||||
};
|
||||
|
||||
var anomalyDuration = _anomalyStartTime.HasValue
|
||||
? now - _anomalyStartTime.Value
|
||||
@@ -288,8 +301,11 @@ public sealed class PowerMonitorInit : IPowerMonitorInit
|
||||
|
||||
_enforcementService.EnforceLimit(calculation.TotalPowerWatts);
|
||||
|
||||
_logger.LogDebug("Poll complete: total={Total}W, other={Other}W (smoothed), devices={Devices}, price={Price}",
|
||||
calculation.TotalPowerWatts, calculation.SmoothedOtherPowerWatts, deviceReadings.Count, effectivePrice);
|
||||
if (_logger.IsEnabled(LogLevel.Debug))
|
||||
{
|
||||
_logger.LogDebug("Poll complete: total={Total}W, other={Other}W (smoothed), devices={Devices}, price={Price}",
|
||||
calculation.TotalPowerWatts, calculation.SmoothedOtherPowerWatts, deviceReadings.Count, effectivePrice);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -312,7 +328,10 @@ public sealed class PowerMonitorInit : IPowerMonitorInit
|
||||
return price;
|
||||
}
|
||||
|
||||
_logger.LogDebug("NordPool price entity returned non-positive value: {State}", state.State);
|
||||
if (_logger.IsEnabled(LogLevel.Debug))
|
||||
{
|
||||
_logger.LogDebug("NordPool price entity returned non-positive value: {State}", state.State);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -321,15 +340,17 @@ public sealed class PowerMonitorInit : IPowerMonitorInit
|
||||
{
|
||||
var isAboveThreshold = totalPowerWatts > _options.HighPowerThresholdWatts;
|
||||
|
||||
if (isAboveThreshold && !_highPowerNotificationActive)
|
||||
switch (isAboveThreshold, _highPowerNotificationActive)
|
||||
{
|
||||
_notificationService.NotifyHighPowerUsage(totalPowerWatts, _options.HighPowerThresholdWatts);
|
||||
_highPowerNotificationActive = true;
|
||||
}
|
||||
else if (!isAboveThreshold && _highPowerNotificationActive)
|
||||
{
|
||||
_notificationService.DismissHighPowerNotification();
|
||||
_highPowerNotificationActive = false;
|
||||
case (true, false):
|
||||
_notificationService.NotifyHighPowerUsage(totalPowerWatts, _options.HighPowerThresholdWatts);
|
||||
_highPowerNotificationActive = true;
|
||||
break;
|
||||
|
||||
case (false, true):
|
||||
_notificationService.DismissHighPowerNotification();
|
||||
_highPowerNotificationActive = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user