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
@@ -24,15 +24,15 @@ public class OtherPowerCalculationServiceTests
var sut = CreateSut(smoothingWindowSize: 1);
var devices = new Dictionary<string, double>
{
["sensor.device_a_power"] = 500,
["sensor.device_b_power"] = 300
["sensor.sonoff_100215c534_power"] = 8.55,
["sensor.sonoff_10018909cb_power"] = 36.93
};
var result = sut.Calculate(1000, devices);
var result = sut.Calculate(341.97, devices);
result.TotalPowerWatts.Should().Be(1000);
result.KnownDevicesSumWatts.Should().Be(800);
result.RawOtherPowerWatts.Should().Be(200);
result.TotalPowerWatts.Should().Be(341.97);
result.KnownDevicesSumWatts.Should().BeApproximately(45.48, 0.01);
result.RawOtherPowerWatts.Should().BeApproximately(296.49, 0.01);
result.DataQuality.Should().Be("ok");
}
@@ -42,11 +42,11 @@ public class OtherPowerCalculationServiceTests
var sut = CreateSut(smoothingWindowSize: 1);
var devices = new Dictionary<string, double>
{
["sensor.device_a_power"] = 800,
["sensor.device_b_power"] = 500
["sensor.sonoff_100215c534_power"] = 200.0,
["sensor.sonoff_10018909cb_power"] = 180.0
};
var result = sut.Calculate(1000, devices);
var result = sut.Calculate(341.97, devices);
result.RawOtherPowerWatts.Should().Be(0);
result.SmoothedOtherPowerWatts.Should().Be(0);
@@ -59,9 +59,9 @@ public class OtherPowerCalculationServiceTests
var sut = CreateSut(smoothingWindowSize: 1);
var devices = new Dictionary<string, double>();
var result = sut.Calculate(1500, devices);
var result = sut.Calculate(341.97, devices);
result.RawOtherPowerWatts.Should().Be(1500);
result.RawOtherPowerWatts.Should().Be(341.97);
result.KnownDevicesSumWatts.Should().Be(0);
}
@@ -71,15 +71,15 @@ public class OtherPowerCalculationServiceTests
var sut = CreateSut(smoothingWindowSize: 1);
var devices = new Dictionary<string, double>
{
["sensor.a"] = 100,
["sensor.b"] = 200
["sensor.sonoff_100215c534_power"] = 8.55,
["sensor.sonoff_10018909cb_power"] = 36.93
};
var result = sut.Calculate(500, devices);
var result = sut.Calculate(341.97, devices);
result.DevicePowerReadings.Should().HaveCount(2);
result.DevicePowerReadings["sensor.a"].Should().Be(100);
result.DevicePowerReadings["sensor.b"].Should().Be(200);
result.DevicePowerReadings["sensor.sonoff_100215c534_power"].Should().Be(8.55);
result.DevicePowerReadings["sensor.sonoff_10018909cb_power"].Should().Be(36.93);
}
#endregion
@@ -92,12 +92,12 @@ public class OtherPowerCalculationServiceTests
var sut = CreateSut(smoothingWindowSize: 4);
var devices = new Dictionary<string, double>();
var result1 = sut.Calculate(1000, devices);
result1.SmoothedOtherPowerWatts.Should().BeApproximately(1000, 0.1);
var result1 = sut.Calculate(341.97, devices);
result1.SmoothedOtherPowerWatts.Should().BeApproximately(341.97, 0.1);
var result2 = sut.Calculate(500, devices);
result2.SmoothedOtherPowerWatts.Should().BeLessThan(1000);
result2.SmoothedOtherPowerWatts.Should().BeGreaterThan(500);
var result2 = sut.Calculate(115.0, devices);
result2.SmoothedOtherPowerWatts.Should().BeLessThan(341.97);
result2.SmoothedOtherPowerWatts.Should().BeGreaterThan(115.0);
}
[Fact]
@@ -109,10 +109,10 @@ public class OtherPowerCalculationServiceTests
PowerCalculationResult? result = null;
for (var i = 0; i < 50; i++)
{
result = sut.Calculate(300, devices);
result = sut.Calculate(200.0, devices);
}
result!.SmoothedOtherPowerWatts.Should().BeApproximately(300, 0.1);
result!.SmoothedOtherPowerWatts.Should().BeApproximately(200.0, 0.1);
}
#endregion
@@ -124,13 +124,13 @@ public class OtherPowerCalculationServiceTests
{
var sut = CreateSut(smoothingWindowSize: 10);
var devices = new Dictionary<string, double>();
sut.Calculate(1000, devices);
sut.Calculate(1000, devices);
sut.Calculate(341.97, devices);
sut.Calculate(341.97, devices);
sut.Reset();
var result = sut.Calculate(100, devices);
var result = sut.Calculate(115.0, devices);
result.SmoothedOtherPowerWatts.Should().BeApproximately(100, 0.1);
result.SmoothedOtherPowerWatts.Should().BeApproximately(115.0, 0.1);
}
#endregion
@@ -9,15 +9,19 @@ public class PowerCostCalculationServiceTests
{
private readonly PowerCostCalculationService _sut = new();
private const double DefaultTotalWatts = 341.97;
private const double DefaultSmoothedOtherWatts = 131.04;
private const double DefaultPricePerKwh = 0.1791;
private static PowerCalculationResult CreateResult(
double totalWatts = 1000,
double smoothedOtherWatts = 200,
double totalWatts = DefaultTotalWatts,
double smoothedOtherWatts = DefaultSmoothedOtherWatts,
Dictionary<string, double>? deviceReadings = null)
{
var readings = deviceReadings ?? new Dictionary<string, double>
{
["sensor.device_a_power"] = 500,
["sensor.device_b_power"] = 300
["sensor.sonoff_100215c534_power"] = 101.0,
["sensor.sonoff_10018909cb_power"] = 109.93
};
return new PowerCalculationResult
@@ -37,12 +41,12 @@ public class PowerCostCalculationServiceTests
public void CalculateCostIncrement_ShouldComputeCorrectCost()
{
var cost = PowerCostCalculationService.CalculateCostIncrement(
powerWatts: 1000,
pricePerKwh: 0.10,
powerWatts: 341.97,
pricePerKwh: 0.1791,
pollIntervalSeconds: PowerMonitorConsts.DefaultPollIntervalSeconds);
var expected = (1000.0 / PowerMonitorConsts.WattsPerKilowatt)
* 0.10
var expected = (341.97 / PowerMonitorConsts.WattsPerKilowatt)
* 0.1791
* ((double)PowerMonitorConsts.DefaultPollIntervalSeconds / PowerMonitorConsts.SecondsPerHour);
cost.Should().BeApproximately(expected, 0.000001);
}
@@ -50,14 +54,14 @@ public class PowerCostCalculationServiceTests
[Fact]
public void CalculateCostIncrement_WithZeroPower_ShouldReturnZero()
{
var cost = PowerCostCalculationService.CalculateCostIncrement(0, 0.10, 15);
var cost = PowerCostCalculationService.CalculateCostIncrement(0, DefaultPricePerKwh, 15);
cost.Should().Be(0);
}
[Fact]
public void CalculateCostIncrement_WithZeroPrice_ShouldReturnZero()
{
var cost = PowerCostCalculationService.CalculateCostIncrement(1000, 0, 15);
var cost = PowerCostCalculationService.CalculateCostIncrement(341.97, 0, 15);
cost.Should().Be(0);
}
@@ -71,7 +75,7 @@ public class PowerCostCalculationServiceTests
var now = new DateTime(2026, 3, 17, 10, 0, 0);
var result = CreateResult();
_sut.RecordCosts(result, 0.10, 15, now);
_sut.RecordCosts(result, DefaultPricePerKwh, 15, now);
var totalSnapshot = _sut.GetTotalCostSnapshot();
totalSnapshot.CostDaily.Should().BeGreaterThan(0);
@@ -86,22 +90,22 @@ public class PowerCostCalculationServiceTests
var now = new DateTime(2026, 3, 17, 10, 0, 0);
var result = CreateResult();
_sut.RecordCosts(result, 0.10, 15, now);
_sut.RecordCosts(result, DefaultPricePerKwh, 15, now);
var deviceA = _sut.GetDeviceCostSnapshot("sensor.device_a_power");
deviceA.CostDaily.Should().BeGreaterThan(0);
var ac = _sut.GetDeviceCostSnapshot("sensor.sonoff_100215c534_power");
ac.CostDaily.Should().BeGreaterThan(0);
var deviceB = _sut.GetDeviceCostSnapshot("sensor.device_b_power");
deviceB.CostDaily.Should().BeGreaterThan(0);
var induction = _sut.GetDeviceCostSnapshot("sensor.sonoff_10018909cb_power");
induction.CostDaily.Should().BeGreaterThan(0);
}
[Fact]
public void RecordCosts_TotalCostShouldBeHigherThanOtherCost()
{
var now = new DateTime(2026, 3, 17, 10, 0, 0);
var result = CreateResult(totalWatts: 1000, smoothedOtherWatts: 200);
var result = CreateResult(totalWatts: DefaultTotalWatts, smoothedOtherWatts: DefaultSmoothedOtherWatts);
_sut.RecordCosts(result, 0.10, 15, now);
_sut.RecordCosts(result, DefaultPricePerKwh, 15, now);
var total = _sut.GetTotalCostSnapshot();
var other = _sut.GetOtherCostSnapshot();
@@ -114,10 +118,10 @@ public class PowerCostCalculationServiceTests
var now = new DateTime(2026, 3, 17, 10, 0, 0);
var result = CreateResult();
_sut.RecordCosts(result, 0.10, 15, now);
_sut.RecordCosts(result, DefaultPricePerKwh, 15, now);
var firstCost = _sut.GetTotalCostSnapshot().CostDaily;
_sut.RecordCosts(result, 0.10, 15, now.AddSeconds(15));
_sut.RecordCosts(result, DefaultPricePerKwh, 15, now.AddSeconds(15));
var secondCost = _sut.GetTotalCostSnapshot().CostDaily;
secondCost.Should().BeApproximately(firstCost * 2, 0.000001);
@@ -129,19 +133,19 @@ public class PowerCostCalculationServiceTests
var now = new DateTime(2026, 3, 17, 10, 0, 0);
var result1 = CreateResult(deviceReadings: new Dictionary<string, double>
{
["sensor.device_a_power"] = 500
["sensor.sonoff_100215c534_power"] = 101.0
});
_sut.RecordCosts(result1, 0.10, 15, now);
_sut.RecordCosts(result1, DefaultPricePerKwh, 15, now);
_sut.GetAllDeviceCostSnapshots().Should().HaveCount(1);
var result2 = CreateResult(deviceReadings: new Dictionary<string, double>
{
["sensor.device_a_power"] = 500,
["sensor.device_b_power"] = 300
["sensor.sonoff_100215c534_power"] = 101.0,
["sensor.sonoff_10018909cb_power"] = 109.93
});
_sut.RecordCosts(result2, 0.10, 15, now.AddSeconds(15));
_sut.RecordCosts(result2, DefaultPricePerKwh, 15, now.AddSeconds(15));
_sut.GetAllDeviceCostSnapshots().Should().HaveCount(2);
}
@@ -155,9 +159,9 @@ public class PowerCostCalculationServiceTests
var now = new DateTime(2026, 3, 17, 10, 0, 0);
var result = CreateResult();
_sut.RecordCosts(result, 0.15, 15, now);
_sut.RecordCosts(result, 0.2492, 15, now);
_sut.LastKnownPrice.Should().Be(0.15);
_sut.LastKnownPrice.Should().Be(0.2492);
_sut.PriceSource.Should().Be("live");
}
@@ -167,19 +171,19 @@ public class PowerCostCalculationServiceTests
var now = new DateTime(2026, 3, 17, 10, 0, 0);
var result = CreateResult();
_sut.RecordCosts(result, 0.10, 15, now);
_sut.RecordCosts(result, DefaultPricePerKwh, 15, now);
_sut.RecordCosts(result, 0, 15, now.AddSeconds(15));
_sut.LastKnownPrice.Should().Be(0.10);
_sut.LastKnownPrice.Should().Be(DefaultPricePerKwh);
_sut.PriceSource.Should().Be("live");
}
[Fact]
public void SetPrice_ShouldUpdatePriceAndMarkAsStale()
{
_sut.SetPrice(0.20);
_sut.SetPrice(0.1891);
_sut.LastKnownPrice.Should().Be(0.20);
_sut.LastKnownPrice.Should().Be(0.1891);
_sut.PriceSource.Should().Be("stale");
}
@@ -229,7 +233,7 @@ public class PowerCostCalculationServiceTests
public async Task RecordCosts_ConcurrentAccess_ShouldNotLoseData()
{
var now = new DateTime(2026, 3, 17, 10, 0, 0);
var result = CreateResult(totalWatts: 1000, smoothedOtherWatts: 200);
var result = CreateResult(totalWatts: DefaultTotalWatts, smoothedOtherWatts: DefaultSmoothedOtherWatts);
const int callsPerThread = 100;
const int threadCount = 10;
@@ -238,7 +242,7 @@ public class PowerCostCalculationServiceTests
{
for (var i = 0; i < callsPerThread; i++)
{
_sut.RecordCosts(result, 0.10, 15, now.AddSeconds(1));
_sut.RecordCosts(result, DefaultPricePerKwh, 15, now.AddSeconds(1));
}
}))
.ToArray();
@@ -146,8 +146,8 @@ public class PowerMonitorEntityServiceTests
var sensors = new List<PowerSensorInfo>
{
new() { EntityId = "sensor.device_a_power", FriendlyName = "Device A", DeviceName = "device_a" },
new() { EntityId = "sensor.device_b_power", FriendlyName = "Device B", DeviceName = "device_b" }
new() { EntityId = "sensor.sonoff_100215c534_power", FriendlyName = "AC Power", DeviceName = "sonoff_100215c534" },
new() { EntityId = "sensor.sonoff_10018909cb_power", FriendlyName = "Induction Power", DeviceName = "sonoff_10018909cb" }
};
await _sut.CreateDeviceEntitiesAsync(sensors);
@@ -238,10 +238,10 @@ public class PowerMonitorEntityServiceTests
await _sut.UpdateEntitiesAsync(context);
_entityManagerMock.Verify(em => em.SetEntityState("sensor.powermonitor_device_a_cost_15min", It.IsAny<string>()), Times.Once);
_entityManagerMock.Verify(em => em.SetEntityState("sensor.powermonitor_device_a_cost_1h", It.IsAny<string>()), Times.Once);
_entityManagerMock.Verify(em => em.SetEntityState("sensor.powermonitor_device_a_cost_daily", It.IsAny<string>()), Times.Once);
_entityManagerMock.Verify(em => em.SetEntityState("sensor.powermonitor_device_a_cost_monthly", It.IsAny<string>()), Times.Once);
_entityManagerMock.Verify(em => em.SetEntityState("sensor.powermonitor_sonoff_100215c534_cost_15min", It.IsAny<string>()), Times.Once);
_entityManagerMock.Verify(em => em.SetEntityState("sensor.powermonitor_sonoff_100215c534_cost_1h", It.IsAny<string>()), Times.Once);
_entityManagerMock.Verify(em => em.SetEntityState("sensor.powermonitor_sonoff_100215c534_cost_daily", It.IsAny<string>()), Times.Once);
_entityManagerMock.Verify(em => em.SetEntityState("sensor.powermonitor_sonoff_100215c534_cost_monthly", It.IsAny<string>()), Times.Once);
}
[Fact]
@@ -293,31 +293,41 @@ public class PowerMonitorEntityServiceTests
{
Calculation = new PowerCalculationResult
{
TotalPowerWatts = 1000,
KnownDevicesSumWatts = 800,
RawOtherPowerWatts = 200,
SmoothedOtherPowerWatts = 195,
TotalPowerWatts = 341.97,
KnownDevicesSumWatts = 210.93,
RawOtherPowerWatts = 131.04,
SmoothedOtherPowerWatts = 128.5,
DataQuality = "ok",
DevicePowerReadings = new Dictionary<string, double>
{
["sensor.device_a_power"] = 500,
["sensor.device_b_power"] = 300
["sensor.sonoff_100215c534_power"] = 8.55,
["sensor.sonoff_10018909cb_power"] = 36.93,
["sensor.tv_power"] = 101.0,
["sensor.akvariumas_power"] = 21.0,
["sensor.sonoff_1002230a48_power"] = 20.06,
["sensor.temp_power"] = 14.0,
["sensor.balkony_laps_power"] = 9.39
}
},
CurrentNordPoolPrice = 0.10,
CurrentNordPoolPrice = 0.1791,
PriceUnit = "EUR/kWh",
PriceSource = "live",
DeviceCosts = new Dictionary<string, CostSnapshot>
{
["sensor.device_a_power"] = new() { Cost15Min = 0.01, Cost1Hour = 0.04, CostDaily = 0.50, CostMonthly = 15.0 },
["sensor.device_b_power"] = new() { Cost15Min = 0.005, Cost1Hour = 0.02, CostDaily = 0.30, CostMonthly = 9.0 }
["sensor.sonoff_100215c534_power"] = new() { Cost15Min = 0.001, Cost1Hour = 0.004, CostDaily = 0.03, CostMonthly = 0.9 },
["sensor.sonoff_10018909cb_power"] = new() { Cost15Min = 0.002, Cost1Hour = 0.009, CostDaily = 0.11, CostMonthly = 3.3 },
["sensor.tv_power"] = new() { Cost15Min = 0.006, Cost1Hour = 0.024, CostDaily = 0.30, CostMonthly = 9.0 },
["sensor.akvariumas_power"] = new() { Cost15Min = 0.001, Cost1Hour = 0.005, CostDaily = 0.06, CostMonthly = 1.8 },
["sensor.sonoff_1002230a48_power"] = new() { Cost15Min = 0.001, Cost1Hour = 0.005, CostDaily = 0.06, CostMonthly = 1.8 },
["sensor.temp_power"] = new() { Cost15Min = 0.001, Cost1Hour = 0.003, CostDaily = 0.04, CostMonthly = 1.2 },
["sensor.balkony_laps_power"] = new() { Cost15Min = 0.0005, Cost1Hour = 0.002, CostDaily = 0.03, CostMonthly = 0.9 }
},
TotalCost = new CostSnapshot { Cost15Min = 0.02, Cost1Hour = 0.08, CostDaily = 1.00, CostMonthly = 30.0 },
OtherCost = new CostSnapshot { Cost15Min = 0.005, Cost1Hour = 0.02, CostDaily = 0.20, CostMonthly = 6.0 },
OtherCost = new CostSnapshot { Cost15Min = 0.008, Cost1Hour = 0.031, CostDaily = 0.38, CostMonthly = 11.4 },
IsAnomaly = isAnomaly,
AnomalyDuration = TimeSpan.Zero,
AnomalyThresholdWatts = 500,
TrackedSensorCount = 2,
AnomalyThresholdWatts = 1000,
TrackedSensorCount = 7,
DegradedSensors = [],
SystemStatus = "ok"
};
@@ -22,7 +22,7 @@ public class PowerSensorDiscoveryServiceTests
}
private PowerSensorDiscoveryService CreateSut(
string mainMeterEntityId = "sensor.p1_meter_power",
string mainMeterEntityId = "sensor.sonoff_100267d63f_power",
string[]? excludedPatterns = null)
{
var options = Options.Create(new PowerMonitorOptions
@@ -73,9 +73,9 @@ public class PowerSensorDiscoveryServiceTests
public void DiscoverDeviceSensors_ShouldFindPowerSensors()
{
SetupEntities(
("sensor.p1_meter_power", "power", "W", "P1 Meter"),
("sensor.sonoff_power", "power", "W", "Sonoff Power"),
("sensor.zigbee_power", "power", "W", "Zigbee Power"),
("sensor.sonoff_100267d63f_power", "power", "W", "Total Home Power Usage"),
("sensor.sonoff_100215c534_power", "power", "W", "AC Power"),
("sensor.sonoff_10018909cb_power", "power", "W", "Induction Power"),
("sensor.temperature", "temperature", "°C", "Temperature")
);
@@ -83,31 +83,31 @@ public class PowerSensorDiscoveryServiceTests
var result = sut.DiscoverDeviceSensors();
result.Should().HaveCount(2);
result.Should().Contain(s => s.EntityId == "sensor.sonoff_power");
result.Should().Contain(s => s.EntityId == "sensor.zigbee_power");
result.Should().Contain(s => s.EntityId == "sensor.sonoff_100215c534_power");
result.Should().Contain(s => s.EntityId == "sensor.sonoff_10018909cb_power");
}
[Fact]
public void DiscoverDeviceSensors_ShouldExcludeMainMeter()
{
SetupEntities(
("sensor.p1_meter_power", "power", "W", "P1 Meter"),
("sensor.device_power", "power", "W", "Device")
("sensor.sonoff_100267d63f_power", "power", "W", "Total Home Power Usage"),
("sensor.tv_power", "power", "W", "TV Power")
);
var sut = CreateSut();
var result = sut.DiscoverDeviceSensors();
result.Should().HaveCount(1);
result.Should().NotContain(s => s.EntityId == "sensor.p1_meter_power");
result.Should().NotContain(s => s.EntityId == "sensor.sonoff_100267d63f_power");
}
[Fact]
public void DiscoverDeviceSensors_ShouldApplyExclusionPatterns()
{
SetupEntities(
("sensor.p1_meter_power", "power", "W", "P1"),
("sensor.device_power", "power", "W", "Device"),
("sensor.sonoff_100267d63f_power", "power", "W", "Total Home Power Usage"),
("sensor.tv_power", "power", "W", "TV Power"),
("sensor.nordpool_some_power", "power", "W", "NordPool"),
("sensor.powermonitor_other_power", "power", "W", "PowerMonitor")
);
@@ -116,21 +116,22 @@ public class PowerSensorDiscoveryServiceTests
var result = sut.DiscoverDeviceSensors();
result.Should().HaveCount(1);
result.Should().Contain(s => s.EntityId == "sensor.device_power");
result.Should().Contain(s => s.EntityId == "sensor.tv_power");
}
[Fact]
public void DiscoverDeviceSensors_ShouldHandleKilowattUnit()
{
SetupEntities(
("sensor.p1_meter_power", "power", "W", "P1"),
("sensor.device_kw", "power", "kW", "Device kW")
("sensor.sonoff_100267d63f_power", "power", "W", "Total Home Power Usage"),
("sensor.sonoff_1002230e32_power", "power", "kW", "Boiler Power")
);
var sut = CreateSut();
var result = sut.DiscoverDeviceSensors();
result.Should().HaveCount(1);
result.First().EntityId.Should().Be("sensor.sonoff_1002230e32_power");
result.First().UnitOfMeasurement.Should().Be("kW");
}
@@ -152,8 +153,8 @@ public class PowerSensorDiscoveryServiceTests
public void DiscoverDeviceSensors_ShouldUpdateCurrentDeviceSensors()
{
SetupEntities(
("sensor.p1_meter_power", "power", "W", "P1"),
("sensor.device_power", "power", "W", "Device")
("sensor.sonoff_100267d63f_power", "power", "W", "Total Home Power Usage"),
("sensor.akvariumas_power", "power", "W", "Akvariumas Power")
);
var sut = CreateSut();
@@ -168,8 +169,8 @@ public class PowerSensorDiscoveryServiceTests
public void DiscoverDeviceSensors_ReDiscovery_ShouldReplacePreviousList()
{
SetupEntities(
("sensor.p1_meter_power", "power", "W", "P1"),
("sensor.device_a_power", "power", "W", "Device A")
("sensor.sonoff_100267d63f_power", "power", "W", "Total Home Power Usage"),
("sensor.sonoff_100215c534_power", "power", "W", "AC Power")
);
var sut = CreateSut();
@@ -177,9 +178,9 @@ public class PowerSensorDiscoveryServiceTests
sut.CurrentDeviceSensors.Should().HaveCount(1);
SetupEntities(
("sensor.p1_meter_power", "power", "W", "P1"),
("sensor.device_a_power", "power", "W", "Device A"),
("sensor.device_b_power", "power", "W", "Device B")
("sensor.sonoff_100267d63f_power", "power", "W", "Total Home Power Usage"),
("sensor.sonoff_100215c534_power", "power", "W", "AC Power"),
("sensor.sonoff_10018909cb_power", "power", "W", "Induction Power")
);
sut.DiscoverDeviceSensors();
@@ -192,8 +193,9 @@ public class PowerSensorDiscoveryServiceTests
[Theory]
[InlineData("sensor.sonoff_1002230e32_power", "sonoff_1002230e32")]
[InlineData("sensor.zigbee_device_power", "zigbee_device")]
[InlineData("sensor.some_sensor", "some_sensor")]
[InlineData("sensor.sonoff_100215c534_power", "sonoff_100215c534")]
[InlineData("sensor.tv_power", "tv")]
[InlineData("sensor.temp_power", "temp")]
[InlineData("sensor.power", "power")]
public void ExtractDeviceName_ShouldStripDomainAndPowerSuffix(string entityId, string expected)
{
@@ -206,9 +208,9 @@ public class PowerSensorDiscoveryServiceTests
[Theory]
[InlineData("sensor.nordpool_*", "sensor.nordpool_current_price", true)]
[InlineData("sensor.nordpool_*", "sensor.device_power", false)]
[InlineData("sensor.nordpool_*", "sensor.tv_power", false)]
[InlineData("sensor.powermonitor_*", "sensor.powermonitor_other_power", true)]
[InlineData("*_power", "sensor.sonoff_power", true)]
[InlineData("*_power", "sensor.sonoff_100215c534_power", true)]
public void GlobToRegex_ShouldMatchCorrectly(string glob, string input, bool expected)
{
var regex = PowerSensorDiscoveryService.GlobToRegex(glob);
@@ -222,8 +224,8 @@ public class PowerSensorDiscoveryServiceTests
[Fact]
public void IsPowerSensor_WithPowerDeviceClassAndWattUnit_ShouldReturnTrue()
{
SetupEntities(("sensor.test_power", "power", "W", "Test"));
var entity = new Entity(_haContextMock.Object, "sensor.test_power");
SetupEntities(("sensor.sonoff_100215c534_power", "power", "W", "AC Power"));
var entity = new Entity(_haContextMock.Object, "sensor.sonoff_100215c534_power");
var sut = CreateSut();
sut.IsPowerSensor(entity).Should().BeTrue();
@@ -232,8 +234,8 @@ public class PowerSensorDiscoveryServiceTests
[Fact]
public void IsPowerSensor_WithNonSensorDomain_ShouldReturnFalse()
{
SetupEntities(("switch.test_power", "power", "W", "Test"));
var entity = new Entity(_haContextMock.Object, "switch.test_power");
SetupEntities(("switch.sonoff_100215c534_power", "power", "W", "AC Power"));
var entity = new Entity(_haContextMock.Object, "switch.sonoff_100215c534_power");
var sut = CreateSut();
sut.IsPowerSensor(entity).Should().BeFalse();
@@ -242,8 +244,8 @@ public class PowerSensorDiscoveryServiceTests
[Fact]
public void IsPowerSensor_WithTemperatureDeviceClass_ShouldReturnFalse()
{
SetupEntities(("sensor.test_temp", "temperature", "°C", "Test"));
var entity = new Entity(_haContextMock.Object, "sensor.test_temp");
SetupEntities(("sensor.sonoff_100215c534_temp", "temperature", "°C", "AC Temp"));
var entity = new Entity(_haContextMock.Object, "sensor.sonoff_100215c534_temp");
var sut = CreateSut();
sut.IsPowerSensor(entity).Should().BeFalse();