feat: add BatteryMonitor implementation with discovery, notification, and scheduling services

This commit is contained in:
Marius
2026-04-01 10:55:33 +03:00
parent 11c2574235
commit 6e4d684359
114 changed files with 22820 additions and 527 deletions
@@ -10,8 +10,8 @@ public class BoilerMonitorConfigTests
{
var config = new BoilerMonitorConfig
{
BoilerPowerEntityId = "sensor.power",
BoilerSwitchEntityId = "switch.boiler",
BoilerPowerEntityId = "sensor.sonoff_1002230e32_power",
BoilerSwitchEntityId = "switch.sonoff_1002230e32",
PollIntervalSeconds = 30,
HeatingPowerThresholdWatts = 5.0
};
@@ -25,7 +25,7 @@ public class BoilerMonitorConfigTests
var config = new BoilerMonitorConfig
{
BoilerPowerEntityId = null,
BoilerSwitchEntityId = "switch.boiler",
BoilerSwitchEntityId = "switch.sonoff_1002230e32",
PollIntervalSeconds = 30,
HeatingPowerThresholdWatts = 5.0
};
@@ -38,7 +38,7 @@ public class BoilerMonitorConfigTests
{
var config = new BoilerMonitorConfig
{
BoilerPowerEntityId = "sensor.power",
BoilerPowerEntityId = "sensor.sonoff_1002230e32_power",
BoilerSwitchEntityId = null,
PollIntervalSeconds = 30,
HeatingPowerThresholdWatts = 5.0
@@ -52,8 +52,8 @@ public class BoilerMonitorConfigTests
{
var config = new BoilerMonitorConfig
{
BoilerPowerEntityId = "sensor.power",
BoilerSwitchEntityId = "switch.boiler",
BoilerPowerEntityId = "sensor.sonoff_1002230e32_power",
BoilerSwitchEntityId = "switch.sonoff_1002230e32",
PollIntervalSeconds = null,
HeatingPowerThresholdWatts = 5.0
};
@@ -66,8 +66,8 @@ public class BoilerMonitorConfigTests
{
var config = new BoilerMonitorConfig
{
BoilerPowerEntityId = "sensor.power",
BoilerSwitchEntityId = "switch.boiler",
BoilerPowerEntityId = "sensor.sonoff_1002230e32_power",
BoilerSwitchEntityId = "switch.sonoff_1002230e32",
PollIntervalSeconds = 30,
HeatingPowerThresholdWatts = null
};
@@ -81,7 +81,7 @@ public class BoilerMonitorConfigTests
var config = new BoilerMonitorConfig
{
BoilerPowerEntityId = " ",
BoilerSwitchEntityId = "switch.boiler",
BoilerSwitchEntityId = "switch.sonoff_1002230e32",
PollIntervalSeconds = 30,
HeatingPowerThresholdWatts = 5.0
};
@@ -94,8 +94,8 @@ public class BoilerMonitorConfigTests
{
var config = new BoilerMonitorConfig
{
BoilerPowerEntityId = "sensor.power",
BoilerSwitchEntityId = "switch.boiler",
BoilerPowerEntityId = "sensor.sonoff_1002230e32_power",
BoilerSwitchEntityId = "switch.sonoff_1002230e32",
PollIntervalSeconds = 0,
HeatingPowerThresholdWatts = 5.0
};
@@ -108,8 +108,8 @@ public class BoilerMonitorConfigTests
{
var config = new BoilerMonitorConfig
{
BoilerPowerEntityId = "sensor.power",
BoilerSwitchEntityId = "switch.boiler",
BoilerPowerEntityId = "sensor.sonoff_1002230e32_power",
BoilerSwitchEntityId = "switch.sonoff_1002230e32",
PollIntervalSeconds = 30,
HeatingPowerThresholdWatts = 0.0
};
@@ -122,8 +122,8 @@ public class BoilerMonitorConfigTests
{
var config = new BoilerMonitorConfig
{
BoilerPowerEntityId = "sensor.power",
BoilerSwitchEntityId = "switch.boiler",
BoilerPowerEntityId = "sensor.sonoff_1002230e32_power",
BoilerSwitchEntityId = "switch.sonoff_1002230e32",
PollIntervalSeconds = -1,
HeatingPowerThresholdWatts = 5.0
};
@@ -48,8 +48,8 @@ public class BoilerMonitorEntityServiceTests
public void ReadPersistedState_WithValidState_ShouldReturnPersistedValues()
{
// Arrange
SetupEntityState(TodayRuntimeEntityId, "3600");
SetupEntityState(SessionCountEntityId, "5");
SetupEntityState(TodayRuntimeEntityId, "7200");
SetupEntityState(SessionCountEntityId, "3");
SetupEntityState(SkipPriceCapEntityId, "ON");
SetupEntityState(BudgetOverrideEntityId, "OFF");
@@ -58,8 +58,8 @@ public class BoilerMonitorEntityServiceTests
// Assert
result.Should().NotBeNull();
result!.AccumulatedRunTime.Should().Be(TimeSpan.FromSeconds(3600));
result.SessionCount.Should().Be(5);
result!.AccumulatedRunTime.Should().Be(TimeSpan.FromSeconds(7200));
result.SessionCount.Should().Be(3);
result.SkipPriceCapState.Should().Be("ON");
result.BudgetOverrideState.Should().Be("OFF");
}
@@ -87,7 +87,7 @@ public class BoilerMonitorEntityServiceTests
{
// Arrange
SetupEntityState(TodayRuntimeEntityId, "unavailable");
SetupEntityState(SessionCountEntityId, "5");
SetupEntityState(SessionCountEntityId, "3");
SetupEntityState(SkipPriceCapEntityId, "OFF");
SetupEntityState(BudgetOverrideEntityId, "OFF");
@@ -103,7 +103,7 @@ public class BoilerMonitorEntityServiceTests
{
// Arrange
SetupEntityState(TodayRuntimeEntityId, "unknown");
SetupEntityState(SessionCountEntityId, "3");
SetupEntityState(SessionCountEntityId, "2");
SetupEntityState(SkipPriceCapEntityId, "OFF");
SetupEntityState(BudgetOverrideEntityId, "OFF");
@@ -118,7 +118,7 @@ public class BoilerMonitorEntityServiceTests
public void ReadPersistedState_WhenSessionCountIsUnavailable_ShouldReturnNull()
{
// Arrange
SetupEntityState(TodayRuntimeEntityId, "1800");
SetupEntityState(TodayRuntimeEntityId, "3600");
SetupEntityState(SessionCountEntityId, "unavailable");
SetupEntityState(SkipPriceCapEntityId, "OFF");
SetupEntityState(BudgetOverrideEntityId, "OFF");
@@ -135,7 +135,7 @@ public class BoilerMonitorEntityServiceTests
{
// Arrange
SetupEntityState(TodayRuntimeEntityId, null);
SetupEntityState(SessionCountEntityId, "3");
SetupEntityState(SessionCountEntityId, "2");
SetupEntityState(SkipPriceCapEntityId, "OFF");
SetupEntityState(BudgetOverrideEntityId, "OFF");
@@ -150,7 +150,7 @@ public class BoilerMonitorEntityServiceTests
public void ReadPersistedState_WhenSessionCountEntityDoesNotExist_ShouldReturnNull()
{
// Arrange
SetupEntityState(TodayRuntimeEntityId, "1800");
SetupEntityState(TodayRuntimeEntityId, "3600");
SetupEntityState(SessionCountEntityId, null);
SetupEntityState(SkipPriceCapEntityId, "OFF");
SetupEntityState(BudgetOverrideEntityId, "OFF");
@@ -167,7 +167,7 @@ public class BoilerMonitorEntityServiceTests
{
// Arrange
SetupEntityState(TodayRuntimeEntityId, "not_a_number");
SetupEntityState(SessionCountEntityId, "3");
SetupEntityState(SessionCountEntityId, "2");
SetupEntityState(SkipPriceCapEntityId, "OFF");
SetupEntityState(BudgetOverrideEntityId, "OFF");
@@ -182,7 +182,7 @@ public class BoilerMonitorEntityServiceTests
public void ReadPersistedState_WhenSessionCountIsNonNumeric_ShouldReturnNull()
{
// Arrange
SetupEntityState(TodayRuntimeEntityId, "1800");
SetupEntityState(TodayRuntimeEntityId, "3600");
SetupEntityState(SessionCountEntityId, "abc");
SetupEntityState(SkipPriceCapEntityId, "OFF");
SetupEntityState(BudgetOverrideEntityId, "OFF");
@@ -199,7 +199,7 @@ public class BoilerMonitorEntityServiceTests
{
// Arrange
SetupEntityState(TodayRuntimeEntityId, "-100");
SetupEntityState(SessionCountEntityId, "3");
SetupEntityState(SessionCountEntityId, "2");
SetupEntityState(SkipPriceCapEntityId, "OFF");
SetupEntityState(BudgetOverrideEntityId, "OFF");
@@ -214,7 +214,7 @@ public class BoilerMonitorEntityServiceTests
public void ReadPersistedState_WhenSessionCountIsNegative_ShouldReturnNull()
{
// Arrange
SetupEntityState(TodayRuntimeEntityId, "1800");
SetupEntityState(TodayRuntimeEntityId, "3600");
SetupEntityState(SessionCountEntityId, "-1");
SetupEntityState(SkipPriceCapEntityId, "OFF");
SetupEntityState(BudgetOverrideEntityId, "OFF");
@@ -230,8 +230,8 @@ public class BoilerMonitorEntityServiceTests
public void ReadPersistedState_WhenSwitchEntitiesAreNull_ShouldStillReturnWithNullSwitchStates()
{
// Arrange
SetupEntityState(TodayRuntimeEntityId, "900");
SetupEntityState(SessionCountEntityId, "2");
SetupEntityState(TodayRuntimeEntityId, "1800");
SetupEntityState(SessionCountEntityId, "1");
SetupEntityState(SkipPriceCapEntityId, null);
SetupEntityState(BudgetOverrideEntityId, null);
@@ -240,8 +240,8 @@ public class BoilerMonitorEntityServiceTests
// Assert
result.Should().NotBeNull();
result!.AccumulatedRunTime.Should().Be(TimeSpan.FromSeconds(900));
result.SessionCount.Should().Be(2);
result!.AccumulatedRunTime.Should().Be(TimeSpan.FromSeconds(1800));
result.SessionCount.Should().Be(1);
result.SkipPriceCapState.Should().BeNull();
result.BudgetOverrideState.Should().BeNull();
}
@@ -253,7 +253,7 @@ public class BoilerMonitorEntityServiceTests
{
// Arrange
SetupEntityState(TodayRuntimeEntityId, state);
SetupEntityState(SessionCountEntityId, "3");
SetupEntityState(SessionCountEntityId, "2");
SetupEntityState(SkipPriceCapEntityId, "OFF");
SetupEntityState(BudgetOverrideEntityId, "OFF");
@@ -268,8 +268,8 @@ public class BoilerMonitorEntityServiceTests
public void ReadPersistedState_WithDecimalRuntime_ShouldParseCorrectly()
{
// Arrange
SetupEntityState(TodayRuntimeEntityId, "1800.5");
SetupEntityState(SessionCountEntityId, "2");
SetupEntityState(TodayRuntimeEntityId, "134.66");
SetupEntityState(SessionCountEntityId, "1");
SetupEntityState(SkipPriceCapEntityId, "OFF");
SetupEntityState(BudgetOverrideEntityId, "OFF");
@@ -278,7 +278,8 @@ public class BoilerMonitorEntityServiceTests
// Assert
result.Should().NotBeNull();
result!.AccumulatedRunTime.Should().Be(TimeSpan.FromSeconds(1800.5));
result!.AccumulatedRunTime.Should().Be(TimeSpan.FromSeconds(134.66));
result.SessionCount.Should().Be(1);
}
#endregion
@@ -71,7 +71,7 @@ public class WaterReadyServiceTests
var sut = CreateSut();
// Act
var result = sut.IsWaterReady("on", 2000.0, BoilerMonitorConsts.DefaultHeatingPowerThresholdWatts);
var result = sut.IsWaterReady("on", 2150.0, BoilerMonitorConsts.DefaultHeatingPowerThresholdWatts);
// Assert
result.Should().BeFalse();
@@ -202,7 +202,7 @@ public class WaterReadyServiceTests
sut.NotifyTurnedOn();
// Act — high power during grace period is still "not ready" (heating)
var result = sut.IsWaterReady("on", 2000.0, BoilerMonitorConsts.DefaultHeatingPowerThresholdWatts);
var result = sut.IsWaterReady("on", 2150.0, BoilerMonitorConsts.DefaultHeatingPowerThresholdWatts);
// Assert
result.Should().BeFalse();
@@ -420,7 +420,7 @@ public class WaterReadyServiceTests
var sut = CreateSut();
// Act — water is not ready (high power), no cooldown should be triggered
sut.IsWaterReady("on", 2000.0, BoilerMonitorConsts.DefaultHeatingPowerThresholdWatts);
sut.IsWaterReady("on", 2150.0, BoilerMonitorConsts.DefaultHeatingPowerThresholdWatts);
// Assert
sut.IsInCooldown.Should().BeFalse();