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:
@@ -9,7 +9,7 @@ using BatteryMonitorManager.Models;
|
||||
namespace BatteryMonitorManager;
|
||||
|
||||
[NetDaemonApp]
|
||||
public sealed class BatteryMonitorInit : IBatteryMonitorInit
|
||||
public sealed class BatteryMonitorInit
|
||||
{
|
||||
private readonly ILogger<BatteryMonitorInit> _logger;
|
||||
private readonly IScheduler _scheduler;
|
||||
@@ -46,7 +46,7 @@ public sealed class BatteryMonitorInit : IBatteryMonitorInit
|
||||
}
|
||||
}
|
||||
|
||||
public async Task InitializeAsync()
|
||||
private async Task InitializeAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -54,7 +54,10 @@ public sealed class BatteryMonitorInit : IBatteryMonitorInit
|
||||
|
||||
CheckAndNotify();
|
||||
|
||||
_logger.LogInformation("BatteryMonitor initialized with threshold {Threshold}%", _options.BatteryThresholdPercent);
|
||||
if (_logger.IsEnabled(LogLevel.Information))
|
||||
{
|
||||
_logger.LogInformation("BatteryMonitor initialized with threshold {Threshold}%", _options.BatteryThresholdPercent);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -81,7 +84,7 @@ public sealed class BatteryMonitorInit : IBatteryMonitorInit
|
||||
_logger.LogInformation("Scheduled daily battery check at 08:00");
|
||||
}
|
||||
|
||||
internal void CheckAndNotify()
|
||||
private void CheckAndNotify()
|
||||
{
|
||||
var lowBatteryDevices = _discoveryService.DiscoverLowBatterySensors(_options.BatteryThresholdPercent);
|
||||
|
||||
|
||||
@@ -6,19 +6,10 @@ using BatteryMonitorManager.Interface;
|
||||
|
||||
namespace BatteryMonitorManager;
|
||||
|
||||
public sealed class BatteryNotificationService : IBatteryNotificationService
|
||||
public sealed class BatteryNotificationService(
|
||||
IHaContext haContext,
|
||||
ILogger<BatteryNotificationService> logger) : IBatteryNotificationService
|
||||
{
|
||||
private readonly IHaContext _haContext;
|
||||
private readonly ILogger<BatteryNotificationService> _logger;
|
||||
|
||||
public BatteryNotificationService(
|
||||
IHaContext haContext,
|
||||
ILogger<BatteryNotificationService> logger)
|
||||
{
|
||||
_haContext = haContext;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void NotifyLowBattery(IReadOnlyList<BatterySensorInfo> lowBatteryDevices)
|
||||
{
|
||||
if (lowBatteryDevices.Count == 0)
|
||||
@@ -28,7 +19,7 @@ public sealed class BatteryNotificationService : IBatteryNotificationService
|
||||
|
||||
var message = BuildNotificationMessage(lowBatteryDevices);
|
||||
|
||||
_haContext.CallService(
|
||||
haContext.CallService(
|
||||
BatteryMonitorConsts.PersistentNotificationDomain,
|
||||
BatteryMonitorConsts.PersistentNotificationCreateService,
|
||||
data: new
|
||||
@@ -38,14 +29,17 @@ public sealed class BatteryNotificationService : IBatteryNotificationService
|
||||
notification_id = BatteryMonitorConsts.NotificationId
|
||||
});
|
||||
|
||||
_logger.LogInformation(
|
||||
"Sent low battery notification for {Count} device(s)",
|
||||
lowBatteryDevices.Count);
|
||||
if (logger.IsEnabled(LogLevel.Information))
|
||||
{
|
||||
logger.LogInformation(
|
||||
"Sent low battery notification for {Count} device(s)",
|
||||
lowBatteryDevices.Count);
|
||||
}
|
||||
}
|
||||
|
||||
public void DismissNotification()
|
||||
{
|
||||
_haContext.CallService(
|
||||
haContext.CallService(
|
||||
BatteryMonitorConsts.PersistentNotificationDomain,
|
||||
BatteryMonitorConsts.PersistentNotificationDismissService,
|
||||
data: new
|
||||
@@ -53,7 +47,7 @@ public sealed class BatteryNotificationService : IBatteryNotificationService
|
||||
notification_id = BatteryMonitorConsts.NotificationId
|
||||
});
|
||||
|
||||
_logger.LogDebug("Dismissed low battery notification");
|
||||
logger.LogDebug("Dismissed low battery notification");
|
||||
}
|
||||
|
||||
internal static string BuildNotificationMessage(IReadOnlyList<BatterySensorInfo> devices)
|
||||
|
||||
@@ -8,22 +8,13 @@ using BatteryMonitorManager.Interface;
|
||||
|
||||
namespace BatteryMonitorManager;
|
||||
|
||||
public sealed class BatterySensorDiscoveryService : IBatterySensorDiscoveryService
|
||||
public sealed class BatterySensorDiscoveryService(
|
||||
IHaContext haContext,
|
||||
ILogger<BatterySensorDiscoveryService> logger) : IBatterySensorDiscoveryService
|
||||
{
|
||||
private readonly IHaContext _haContext;
|
||||
private readonly ILogger<BatterySensorDiscoveryService> _logger;
|
||||
|
||||
public BatterySensorDiscoveryService(
|
||||
IHaContext haContext,
|
||||
ILogger<BatterySensorDiscoveryService> logger)
|
||||
{
|
||||
_haContext = haContext;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public IReadOnlyList<BatterySensorInfo> DiscoverLowBatterySensors(int thresholdPercent)
|
||||
{
|
||||
var allEntities = _haContext.GetAllEntities();
|
||||
var allEntities = haContext.GetAllEntities();
|
||||
|
||||
var lowBatterySensors = allEntities
|
||||
.Where(IsBatterySensor)
|
||||
@@ -32,9 +23,12 @@ public sealed class BatterySensorDiscoveryService : IBatterySensorDiscoveryServi
|
||||
.Where(info => info is not null)
|
||||
.ToList();
|
||||
|
||||
_logger.LogInformation(
|
||||
"Battery check: found {Count} sensors below {Threshold}%",
|
||||
lowBatterySensors.Count, thresholdPercent);
|
||||
if (logger.IsEnabled(LogLevel.Information))
|
||||
{
|
||||
logger.LogInformation(
|
||||
"Battery check: found {Count} sensors below {Threshold}%",
|
||||
lowBatterySensors.Count, thresholdPercent);
|
||||
}
|
||||
|
||||
return lowBatterySensors!;
|
||||
}
|
||||
@@ -65,13 +59,21 @@ public sealed class BatterySensorDiscoveryService : IBatterySensorDiscoveryServi
|
||||
|
||||
if (state is null or "unavailable" or "unknown")
|
||||
{
|
||||
_logger.LogDebug("Skipping battery sensor {EntityId}: state is {State}", entity.EntityId, state ?? "null");
|
||||
if (logger.IsEnabled(LogLevel.Debug))
|
||||
{
|
||||
logger.LogDebug("Skipping battery sensor {EntityId}: state is {State}", entity.EntityId, state ?? "null");
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!double.TryParse(state, CultureInfo.InvariantCulture, out var batteryLevel))
|
||||
{
|
||||
_logger.LogDebug("Skipping battery sensor {EntityId}: unable to parse state '{State}'", entity.EntityId, state);
|
||||
if (logger.IsEnabled(LogLevel.Debug))
|
||||
{
|
||||
logger.LogDebug("Skipping battery sensor {EntityId}: unable to parse state '{State}'", entity.EntityId, state);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -91,7 +93,7 @@ public sealed class BatterySensorDiscoveryService : IBatterySensorDiscoveryServi
|
||||
};
|
||||
}
|
||||
|
||||
internal static string? GetJsonStringProperty(JsonElement? element, string propertyName)
|
||||
private static string? GetJsonStringProperty(JsonElement? element, string propertyName)
|
||||
{
|
||||
if (element is null)
|
||||
{
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
namespace BatteryMonitorManager.Interface;
|
||||
|
||||
public interface IBatteryMonitorInit
|
||||
{
|
||||
Task InitializeAsync();
|
||||
}
|
||||
@@ -6,7 +6,7 @@ public sealed class BatteryMonitorOptions
|
||||
{
|
||||
public const string SectionName = "BatteryMonitor";
|
||||
|
||||
public bool Enabled { get; set; }
|
||||
public bool Enabled { get; init; }
|
||||
|
||||
public int BatteryThresholdPercent { get; set; } = BatteryMonitorConsts.DefaultBatteryThresholdPercent;
|
||||
public int BatteryThresholdPercent { get; init; } = BatteryMonitorConsts.DefaultBatteryThresholdPercent;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user