Apply code formatting cleanup across monitor projects
- Reindent appsettings.json and the MSBuild project files - Collapse single-statement guard blocks and tidy using and member order - Normalize XML doc indentation and file endings - Reorder BatteryMonitor test-data helpers and move the exclusion tests into their own region - Carry the same formatting through the boiler config, control, entity and test files
This commit is contained in:
@@ -15,21 +15,21 @@ namespace PowerMonitorManager;
|
||||
[NetDaemonApp]
|
||||
public sealed class PowerMonitorInit
|
||||
{
|
||||
private readonly ILogger<PowerMonitorInit> _logger;
|
||||
private readonly IHaContext _haContext;
|
||||
private readonly PowerMonitorOptions _options;
|
||||
private readonly IPowerSensorDiscoveryService _discoveryService;
|
||||
private readonly IOtherPowerCalculationService _calculationService;
|
||||
private readonly IPowerCostCalculationService _costService;
|
||||
private readonly IPowerMonitorEntityService _entityService;
|
||||
private readonly IPowerNotificationService _notificationService;
|
||||
private readonly IPowerSensorDiscoveryService _discoveryService;
|
||||
private readonly IPowerLimitEnforcementService _enforcementService;
|
||||
private readonly IPowerMonitorEntityService _entityService;
|
||||
private readonly IHaContext _haContext;
|
||||
private readonly ILogger<PowerMonitorInit> _logger;
|
||||
private readonly IPowerNotificationService _notificationService;
|
||||
private readonly PowerMonitorOptions _options;
|
||||
private readonly TimeProvider _timeProvider;
|
||||
private DateTimeOffset? _anomalyStartTime;
|
||||
private bool _highPowerNotificationActive;
|
||||
private DateTimeOffset? _lastPollTimestamp;
|
||||
|
||||
private int _mainMeterConsecutiveFailures;
|
||||
private DateTimeOffset? _anomalyStartTime;
|
||||
private DateTimeOffset? _lastPollTimestamp;
|
||||
private bool _highPowerNotificationActive;
|
||||
|
||||
public PowerMonitorInit(
|
||||
IHaContext haContext,
|
||||
@@ -99,9 +99,7 @@ public sealed class PowerMonitorInit
|
||||
await _entityService.CreateDeviceEntitiesAsync(sensors);
|
||||
|
||||
if (_logger.IsEnabled(LogLevel.Information))
|
||||
{
|
||||
_logger.LogInformation("PowerMonitor initialized with {SensorCount} tracked sensors", sensors.Count);
|
||||
}
|
||||
|
||||
await PollAndUpdateAsync();
|
||||
}
|
||||
@@ -115,7 +113,7 @@ public sealed class PowerMonitorInit
|
||||
private IReadOnlyList<PowerSensorInfo> GetPowerSensorEntitiesNames()
|
||||
{
|
||||
var sensors = _discoveryService.DiscoverDeviceSensors();
|
||||
|
||||
|
||||
return sensors;
|
||||
}
|
||||
|
||||
@@ -136,18 +134,13 @@ public sealed class PowerMonitorInit
|
||||
meter.StateChanges()
|
||||
.Subscribe(e =>
|
||||
{
|
||||
if (e.Old?.State == e.New?.State)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (e.Old?.State == e.New?.State) return;
|
||||
|
||||
_ = PollAndUpdateAsync();
|
||||
});
|
||||
|
||||
if (_logger.IsEnabled(LogLevel.Information))
|
||||
{
|
||||
_logger.LogInformation("PowerMonitor subscribed to state changes of {MeterId}", _options.MainMeterEntityId);
|
||||
}
|
||||
}
|
||||
|
||||
private void SubscribeToRediscoverButton()
|
||||
@@ -197,20 +190,14 @@ public sealed class PowerMonitorInit
|
||||
{
|
||||
await _entityService.CreateDeviceEntitiesAsync(added);
|
||||
if (_logger.IsEnabled(LogLevel.Information))
|
||||
{
|
||||
_logger.LogInformation("Created entities for {Count} new sensors", added.Count);
|
||||
}
|
||||
}
|
||||
|
||||
if (removed.Count > 0)
|
||||
{
|
||||
await RemoreDevicePriceEntities(removed);
|
||||
}
|
||||
if (removed.Count > 0) await RemoreDevicePriceEntities(removed);
|
||||
|
||||
if (_logger.IsEnabled(LogLevel.Information))
|
||||
{
|
||||
_logger.LogInformation("Re-discovery complete: {Total} sensors ({Added} added, {Removed} removed)", newSensors.Count, added.Count, removed.Count);
|
||||
}
|
||||
_logger.LogInformation("Re-discovery complete: {Total} sensors ({Added} added, {Removed} removed)",
|
||||
newSensors.Count, added.Count, removed.Count);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -222,11 +209,9 @@ public sealed class PowerMonitorInit
|
||||
{
|
||||
var removedNames = removed.Select(s => s.DeviceName).ToList();
|
||||
await _entityService.RemoveDeviceEntitiesAsync(removedNames);
|
||||
|
||||
|
||||
if (_logger.IsEnabled(LogLevel.Information))
|
||||
{
|
||||
_logger.LogInformation("Removed entities for {Count} sensors", removed.Count);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task PollAndUpdateAsync()
|
||||
@@ -237,8 +222,9 @@ public sealed class PowerMonitorInit
|
||||
if (totalPower is null)
|
||||
{
|
||||
_mainMeterConsecutiveFailures++;
|
||||
_logger.LogWarning("Main meter unavailable ({Failures}/{Max})", _mainMeterConsecutiveFailures, PowerMonitorConsts.MainMeterMaxConsecutiveFailures);
|
||||
|
||||
_logger.LogWarning("Main meter unavailable ({Failures}/{Max})", _mainMeterConsecutiveFailures,
|
||||
PowerMonitorConsts.MainMeterMaxConsecutiveFailures);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -302,10 +288,10 @@ public sealed class PowerMonitorInit
|
||||
_enforcementService.EnforceLimit(calculation.TotalPowerWatts);
|
||||
|
||||
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);
|
||||
}
|
||||
_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)
|
||||
{
|
||||
@@ -316,23 +302,18 @@ public sealed class PowerMonitorInit
|
||||
private double ReadNordPoolPrice()
|
||||
{
|
||||
var state = _haContext.GetState(NordPoolConsts.CurrentPriceEntityId);
|
||||
|
||||
|
||||
if (state?.State is null or "unavailable" or "unknown")
|
||||
{
|
||||
_logger.LogDebug("NordPool price entity unavailable, using last known price");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (double.TryParse(state.State, CultureInfo.InvariantCulture, out var price) && price > 0)
|
||||
{
|
||||
return price;
|
||||
}
|
||||
if (double.TryParse(state.State, CultureInfo.InvariantCulture, out var price) && price > 0) return price;
|
||||
|
||||
if (_logger.IsEnabled(LogLevel.Debug))
|
||||
{
|
||||
_logger.LogDebug("NordPool price entity returned non-positive value: {State}", state.State);
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -357,10 +338,7 @@ public sealed class PowerMonitorInit
|
||||
private double? ReadMainMeterPower()
|
||||
{
|
||||
var state = _haContext.GetState(_options.MainMeterEntityId);
|
||||
if (state?.State is null or "unavailable" or "unknown")
|
||||
{
|
||||
return null;
|
||||
}
|
||||
if (state?.State is null or "unavailable" or "unknown") return null;
|
||||
|
||||
return double.TryParse(state.State, CultureInfo.InvariantCulture, out var value)
|
||||
? value
|
||||
@@ -384,10 +362,11 @@ public sealed class PowerMonitorInit
|
||||
|
||||
if (double.TryParse(state.State, CultureInfo.InvariantCulture, out var value))
|
||||
{
|
||||
var powerWatts = string.Equals(sensor.UnitOfMeasurement, PowerMonitorConsts.KilowattUnit, StringComparison.OrdinalIgnoreCase)
|
||||
var powerWatts = string.Equals(sensor.UnitOfMeasurement, PowerMonitorConsts.KilowattUnit,
|
||||
StringComparison.OrdinalIgnoreCase)
|
||||
? value * PowerMonitorConsts.WattsPerKilowatt
|
||||
: value;
|
||||
|
||||
|
||||
readings[sensor.EntityId] = powerWatts;
|
||||
}
|
||||
else
|
||||
@@ -399,4 +378,4 @@ public sealed class PowerMonitorInit
|
||||
|
||||
return (readings, degraded);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user