Add shared test data builders and Qodana configuration
- Extract per-monitor *TestData helper classes and rework tests to use them - Add boiler run-time tracker abstraction (implementation pending) - Drop unused NordPool init/update-result types and stale PLAN.md - Add qodana.yaml with the inspection set to keep the analysis focused
This commit is contained in:
@@ -5,6 +5,7 @@ using NetDaemon.AppModel;
|
||||
using NetDaemon.HassModel;
|
||||
using NetDaemon.HassModel.Entities;
|
||||
using NordPool.Models;
|
||||
using PowerMonitor.Models;
|
||||
using PowerMonitorManager.Interface;
|
||||
using PowerMonitorManager.Models;
|
||||
using PowerMonitorManager.Models.Consts;
|
||||
@@ -60,10 +61,22 @@ public sealed class PowerMonitorInit : IPowerMonitorInit
|
||||
{
|
||||
try
|
||||
{
|
||||
await InitializeAsync();
|
||||
SubscribeToMainMeterChanges();
|
||||
SubscribeToRediscoverButton();
|
||||
SubscribeToLimitBreachDebugButton();
|
||||
if (_options.Enabled)
|
||||
{
|
||||
await InitializeAsync();
|
||||
SubscribeToMainMeterChanges();
|
||||
SubscribeToRediscoverButton();
|
||||
SubscribeToLimitBreachDebugButton();
|
||||
}
|
||||
else
|
||||
{
|
||||
await CleanupEntitiesAsync();
|
||||
|
||||
var sensoresToRemove = GetPowerSensorEntitiesNames();
|
||||
|
||||
await _entityService.RemoveDeviceEntitiesAsync([.. sensoresToRemove.Select(s => s.DeviceName)]);
|
||||
await _entityService.RemoveSystemEntitiesAsync();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -82,7 +95,7 @@ public sealed class PowerMonitorInit : IPowerMonitorInit
|
||||
|
||||
await Task.Delay(TimeSpan.FromSeconds(5));
|
||||
|
||||
var sensors = _discoveryService.DiscoverDeviceSensors();
|
||||
var sensors = GetPowerSensorEntitiesNames();
|
||||
await _entityService.CreateDeviceEntitiesAsync(sensors);
|
||||
|
||||
_logger.LogInformation("PowerMonitor initialized with {SensorCount} tracked sensors", sensors.Count);
|
||||
@@ -96,9 +109,16 @@ public sealed class PowerMonitorInit : IPowerMonitorInit
|
||||
}
|
||||
}
|
||||
|
||||
private IReadOnlyList<PowerSensorInfo> GetPowerSensorEntitiesNames()
|
||||
{
|
||||
var sensors = _discoveryService.DiscoverDeviceSensors();
|
||||
|
||||
return sensors;
|
||||
}
|
||||
|
||||
public async Task CleanupEntitiesAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
await _entityService.CleanupEntitiesAsync(cancellationToken);
|
||||
await _entityService.RemoveSystemEntitiesAsync(cancellationToken);
|
||||
}
|
||||
|
||||
private void SubscribeToMainMeterChanges()
|
||||
@@ -175,9 +195,7 @@ public sealed class PowerMonitorInit : IPowerMonitorInit
|
||||
|
||||
if (removed.Count > 0)
|
||||
{
|
||||
var removedNames = removed.Select(s => s.DeviceName).ToList();
|
||||
await _entityService.CleanupDeviceEntitiesAsync(removedNames);
|
||||
_logger.LogInformation("Removed entities for {Count} sensors", removed.Count);
|
||||
await RemoreDevicePriceEntities(removed);
|
||||
}
|
||||
|
||||
_logger.LogInformation("Re-discovery complete: {Total} sensors ({Added} added, {Removed} removed)", newSensors.Count, added.Count, removed.Count);
|
||||
@@ -188,6 +206,14 @@ public sealed class PowerMonitorInit : IPowerMonitorInit
|
||||
}
|
||||
}
|
||||
|
||||
private async Task RemoreDevicePriceEntities(List<PowerSensorInfo> removed)
|
||||
{
|
||||
var removedNames = removed.Select(s => s.DeviceName).ToList();
|
||||
await _entityService.RemoveDeviceEntitiesAsync(removedNames);
|
||||
|
||||
_logger.LogInformation("Removed entities for {Count} sensors", removed.Count);
|
||||
}
|
||||
|
||||
private async Task PollAndUpdateAsync()
|
||||
{
|
||||
try
|
||||
|
||||
Reference in New Issue
Block a user