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:
@@ -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)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user