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:
@@ -1,11 +1,12 @@
|
||||
using System.Text.Json;
|
||||
using System.Globalization;
|
||||
using FluentAssertions;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
using NetDaemon.HassModel;
|
||||
using NetDaemon.HassModel.Entities;
|
||||
using BatteryMonitorManager;
|
||||
using BatteryMonitor.Models;
|
||||
using BatteryMonitorManager;
|
||||
using static BatteryMonitor.Manager.Tests.BatteryMonitorTestData;
|
||||
|
||||
namespace BatteryMonitor.Manager.Tests;
|
||||
|
||||
@@ -25,33 +26,9 @@ public class BatterySensorDiscoveryServiceTests
|
||||
return new BatterySensorDiscoveryService(_haContextMock.Object, _loggerMock.Object);
|
||||
}
|
||||
|
||||
private void SetupEntities(params (string entityId, string? deviceClass, string? state, string? friendlyName)[] entities)
|
||||
private IReadOnlyList<Entity> SetupEntities(params EntitySpec[] entities)
|
||||
{
|
||||
var entityList = new List<Entity>();
|
||||
|
||||
foreach (var (entityId, deviceClass, state, friendlyName) in entities)
|
||||
{
|
||||
var attributes = new Dictionary<string, object?>();
|
||||
if (deviceClass is not null)
|
||||
{
|
||||
attributes["device_class"] = deviceClass;
|
||||
}
|
||||
|
||||
if (friendlyName is not null)
|
||||
{
|
||||
attributes["friendly_name"] = friendlyName;
|
||||
}
|
||||
|
||||
var attributesJson = JsonSerializer.SerializeToElement(attributes);
|
||||
var entityState = new EntityState { AttributesJson = attributesJson, State = state };
|
||||
|
||||
_haContextMock.Setup(h => h.GetState(entityId)).Returns(entityState);
|
||||
|
||||
var entity = new Entity(_haContextMock.Object, entityId);
|
||||
entityList.Add(entity);
|
||||
}
|
||||
|
||||
_haContextMock.Setup(h => h.GetAllEntities()).Returns(entityList);
|
||||
return ConfigureHaContext(_haContextMock, entities);
|
||||
}
|
||||
|
||||
#region Discovery
|
||||
@@ -60,9 +37,9 @@ public class BatterySensorDiscoveryServiceTests
|
||||
public void DiscoverLowBatterySensors_ShouldFindLowBatteryDevices()
|
||||
{
|
||||
SetupEntities(
|
||||
("sensor.bathroom_battery", "battery", "3", "Bathroom Battery"),
|
||||
("sensor.bedroom_battery", "battery", "80", "Bedroom Battery"),
|
||||
("sensor.boiler_temp_battery", "battery", "2", "Boiler Temp Battery")
|
||||
Battery("sensor.bathroom_battery", "3", "Bathroom Battery"),
|
||||
Battery("sensor.bedroom_battery", "80", "Bedroom Battery"),
|
||||
Battery("sensor.boiler_temp_battery", "2", "Boiler Temp Battery")
|
||||
);
|
||||
|
||||
var sut = CreateSut();
|
||||
@@ -71,134 +48,110 @@ public class BatterySensorDiscoveryServiceTests
|
||||
result.Should().HaveCount(2);
|
||||
result.Should().Contain(s => s.EntityId == "sensor.bathroom_battery");
|
||||
result.Should().Contain(s => s.EntityId == "sensor.boiler_temp_battery");
|
||||
result.Select(s => s.EntityId).Should().NotContain("sensor.bedroom_battery");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DiscoverLowBatterySensors_ShouldExcludePhoneDevices()
|
||||
[Theory]
|
||||
[InlineData("sensor.erikas_iphone_battery_level", "Erika's iPhone Battery")]
|
||||
[InlineData("sensor.home_phone_battery_state", "Home Phone Battery")]
|
||||
[InlineData("sensor.marius_watch_battery_level", "Marius Watch Battery")]
|
||||
[InlineData("sensor.sm_s936b_battery_level", "Galaxy S24 Battery")]
|
||||
[InlineData("sensor.sm_a536b_battery_level", "Galaxy A53 Battery")]
|
||||
[InlineData("sensor.pixel_8_battery_level", "Pixel 8 Battery")]
|
||||
[InlineData("sensor.oneplus_battery_level", "OnePlus Battery")]
|
||||
[InlineData("sensor.ipad_battery_level", "iPad Battery")]
|
||||
[InlineData("sensor.galaxy_s24_battery", "Galaxy S24 Battery")]
|
||||
[InlineData("SENSOR.SM_S936B_BATTERY_LEVEL", "Galaxy S24 Battery")]
|
||||
public void DiscoverLowBatterySensors_ShouldExcludePersonalDevices(string excludedEntityId, string friendlyName)
|
||||
{
|
||||
SetupEntities(
|
||||
("sensor.bathroom_battery", "battery", "2", "Bathroom Battery"),
|
||||
("sensor.erikas_iphone_battery_level", "battery", "1", "Erika's iPhone Battery"),
|
||||
("sensor.home_phone_battery_state", "battery", "3", "Home Phone Battery")
|
||||
Battery("sensor.bathroom_battery", "2", "Bathroom Battery"),
|
||||
Battery(excludedEntityId, "1", friendlyName)
|
||||
);
|
||||
|
||||
var sut = CreateSut();
|
||||
var result = sut.DiscoverLowBatterySensors(BatteryMonitorConsts.DefaultBatteryThresholdPercent);
|
||||
|
||||
result.Should().HaveCount(1);
|
||||
result.First().EntityId.Should().Be("sensor.bathroom_battery");
|
||||
result.Should().ContainSingle();
|
||||
result[0].EntityId.Should().Be("sensor.bathroom_battery");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DiscoverLowBatterySensors_ShouldExcludeWatchDevices()
|
||||
[Theory]
|
||||
[InlineData("binary_sensor.bathroom_battery", "battery")]
|
||||
[InlineData("sensor.bathroom_temperature", "temperature")]
|
||||
[InlineData("sensor.bathroom_humidity", "humidity")]
|
||||
[InlineData("sensor.no_device_class", null)]
|
||||
[InlineData("sensorx.bathroom_battery", "battery")]
|
||||
[InlineData("switch.bathroom_battery", "battery")]
|
||||
public void DiscoverLowBatterySensors_ShouldIgnoreEntitiesThatAreNotBatterySensors(string entityId, string? deviceClass)
|
||||
{
|
||||
SetupEntities(
|
||||
("sensor.bathroom_battery", "battery", "2", "Bathroom Battery"),
|
||||
("sensor.marius_watch_battery_level", "battery", "1", "Marius Watch Battery")
|
||||
Sensor(entityId, deviceClass, "2", "Ignored Entity"),
|
||||
Battery("sensor.bedroom_battery", "3", "Bedroom Battery")
|
||||
);
|
||||
|
||||
var sut = CreateSut();
|
||||
var result = sut.DiscoverLowBatterySensors(BatteryMonitorConsts.DefaultBatteryThresholdPercent);
|
||||
|
||||
result.Should().HaveCount(1);
|
||||
result.First().EntityId.Should().Be("sensor.bathroom_battery");
|
||||
result.Should().ContainSingle();
|
||||
result[0].EntityId.Should().Be("sensor.bedroom_battery");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DiscoverLowBatterySensors_ShouldExcludeGalaxyDevices()
|
||||
[Theory]
|
||||
[MemberData(nameof(UnusableStateRows), MemberType = typeof(BatteryMonitorTestData))]
|
||||
public void DiscoverLowBatterySensors_ShouldSkipUnusableStates(string? state)
|
||||
{
|
||||
SetupEntities(
|
||||
("sensor.bathroom_battery", "battery", "2", "Bathroom Battery"),
|
||||
("sensor.sm_s936b_battery_level", "battery", "1", "Galaxy S24 Battery")
|
||||
Battery("sensor.bathroom_battery", state, "Bathroom Battery"),
|
||||
Battery("sensor.bedroom_battery", "3", "Bedroom Battery")
|
||||
);
|
||||
|
||||
var sut = CreateSut();
|
||||
var result = sut.DiscoverLowBatterySensors(BatteryMonitorConsts.DefaultBatteryThresholdPercent);
|
||||
|
||||
result.Should().HaveCount(1);
|
||||
result.First().EntityId.Should().Be("sensor.bathroom_battery");
|
||||
result.Should().ContainSingle();
|
||||
result[0].EntityId.Should().Be("sensor.bedroom_battery");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DiscoverLowBatterySensors_ShouldIgnoreNonSensorDomain()
|
||||
public void DiscoverLowBatterySensors_WhenEntityStateIsNull_ShouldSkipSensor()
|
||||
{
|
||||
SetupEntities(
|
||||
("binary_sensor.bathroom_battery", "battery", "2", "Bathroom Battery"),
|
||||
("sensor.bedroom_battery", "battery", "3", "Bedroom Battery")
|
||||
new EntitySpec { EntityId = "sensor.bathroom_battery", FriendlyName = "Bathroom Battery", HasState = false },
|
||||
Battery("sensor.bedroom_battery", "3", "Bedroom Battery")
|
||||
);
|
||||
|
||||
var sut = CreateSut();
|
||||
var result = sut.DiscoverLowBatterySensors(BatteryMonitorConsts.DefaultBatteryThresholdPercent);
|
||||
|
||||
result.Should().HaveCount(1);
|
||||
result.First().EntityId.Should().Be("sensor.bedroom_battery");
|
||||
result.Should().ContainSingle();
|
||||
result[0].EntityId.Should().Be("sensor.bedroom_battery");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DiscoverLowBatterySensors_ShouldIgnoreNonBatteryDeviceClass()
|
||||
[Theory]
|
||||
[MemberData(nameof(ThresholdBoundaryRows), MemberType = typeof(BatteryMonitorTestData))]
|
||||
public void DiscoverLowBatterySensors_ShouldApplyStrictThresholdBoundary(int thresholdPercent, string state, bool expectedIncluded)
|
||||
{
|
||||
SetupEntities(
|
||||
("sensor.bathroom_temperature", "temperature", "2", "Bathroom Temperature"),
|
||||
("sensor.bedroom_battery", "battery", "3", "Bedroom Battery")
|
||||
);
|
||||
SetupEntities(Battery("sensor.boundary_battery", state, "Boundary Battery"));
|
||||
|
||||
var sut = CreateSut();
|
||||
var result = sut.DiscoverLowBatterySensors(BatteryMonitorConsts.DefaultBatteryThresholdPercent);
|
||||
var result = sut.DiscoverLowBatterySensors(thresholdPercent);
|
||||
|
||||
result.Should().HaveCount(1);
|
||||
result.First().EntityId.Should().Be("sensor.bedroom_battery");
|
||||
}
|
||||
result.Should().HaveCount(expectedIncluded ? 1 : 0);
|
||||
|
||||
[Fact]
|
||||
public void DiscoverLowBatterySensors_ShouldSkipUnavailableState()
|
||||
{
|
||||
SetupEntities(
|
||||
("sensor.bathroom_battery", "battery", "unavailable", "Bathroom Battery"),
|
||||
("sensor.bedroom_battery", "battery", "3", "Bedroom Battery")
|
||||
);
|
||||
|
||||
var sut = CreateSut();
|
||||
var result = sut.DiscoverLowBatterySensors(BatteryMonitorConsts.DefaultBatteryThresholdPercent);
|
||||
|
||||
result.Should().HaveCount(1);
|
||||
result.First().EntityId.Should().Be("sensor.bedroom_battery");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DiscoverLowBatterySensors_ShouldSkipUnknownState()
|
||||
{
|
||||
SetupEntities(
|
||||
("sensor.bathroom_battery", "battery", "unknown", "Bathroom Battery"),
|
||||
("sensor.bedroom_battery", "battery", "3", "Bedroom Battery")
|
||||
);
|
||||
|
||||
var sut = CreateSut();
|
||||
var result = sut.DiscoverLowBatterySensors(BatteryMonitorConsts.DefaultBatteryThresholdPercent);
|
||||
|
||||
result.Should().HaveCount(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DiscoverLowBatterySensors_ShouldSkipNonParsableState()
|
||||
{
|
||||
SetupEntities(
|
||||
("sensor.bathroom_battery", "battery", "not_a_number", "Bathroom Battery"),
|
||||
("sensor.bedroom_battery", "battery", "3", "Bedroom Battery")
|
||||
);
|
||||
|
||||
var sut = CreateSut();
|
||||
var result = sut.DiscoverLowBatterySensors(BatteryMonitorConsts.DefaultBatteryThresholdPercent);
|
||||
|
||||
result.Should().HaveCount(1);
|
||||
result.First().EntityId.Should().Be("sensor.bedroom_battery");
|
||||
if (expectedIncluded)
|
||||
{
|
||||
result[0].EntityId.Should().Be("sensor.boundary_battery");
|
||||
result[0].BatteryLevel.Should().Be(double.Parse(state, CultureInfo.InvariantCulture));
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DiscoverLowBatterySensors_WhenNoBatterySensors_ShouldReturnEmpty()
|
||||
{
|
||||
SetupEntities(
|
||||
("sensor.temperature", "temperature", "22", "Temperature"),
|
||||
("switch.some_switch", null, "on", "Switch")
|
||||
Sensor("sensor.temperature", "temperature", "22", "Temperature"),
|
||||
Sensor("switch.some_switch", null, "on", "Switch")
|
||||
);
|
||||
|
||||
var sut = CreateSut();
|
||||
@@ -211,8 +164,9 @@ public class BatterySensorDiscoveryServiceTests
|
||||
public void DiscoverLowBatterySensors_WhenAllAboveThreshold_ShouldReturnEmpty()
|
||||
{
|
||||
SetupEntities(
|
||||
("sensor.bathroom_battery", "battery", "50", "Bathroom Battery"),
|
||||
("sensor.bedroom_battery", "battery", "80", "Bedroom Battery")
|
||||
Battery("sensor.bathroom_battery", "5", "Bathroom Battery"),
|
||||
Battery("sensor.bedroom_battery", "50", "Bedroom Battery"),
|
||||
Battery("sensor.boiler_temp_battery", "100", "Boiler Temp Battery")
|
||||
);
|
||||
|
||||
var sut = CreateSut();
|
||||
@@ -222,77 +176,109 @@ public class BatterySensorDiscoveryServiceTests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DiscoverLowBatterySensors_ShouldRespectCustomThreshold()
|
||||
public void DiscoverLowBatterySensors_WhenThereAreNoEntities_ShouldReturnEmpty()
|
||||
{
|
||||
SetupEntities(
|
||||
("sensor.bathroom_battery", "battery", "15", "Bathroom Battery"),
|
||||
("sensor.bedroom_battery", "battery", "25", "Bedroom Battery")
|
||||
);
|
||||
SetupEntities();
|
||||
|
||||
var sut = CreateSut();
|
||||
var result = sut.DiscoverLowBatterySensors(20);
|
||||
var result = sut.DiscoverLowBatterySensors(BatteryMonitorConsts.DefaultBatteryThresholdPercent);
|
||||
|
||||
result.Should().HaveCount(1);
|
||||
result.First().EntityId.Should().Be("sensor.bathroom_battery");
|
||||
result.Should().NotBeNull();
|
||||
result.Should().BeEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DiscoverLowBatterySensors_ShouldPopulateFriendlyName()
|
||||
public void DiscoverLowBatterySensors_WithDuplicateSensorIds_ShouldReportEachOccurrence()
|
||||
{
|
||||
SetupEntities(
|
||||
("sensor.bathroom_battery", "battery", "2", "Bathroom Battery")
|
||||
Battery("sensor.duplicate_battery", "2", "Duplicate Battery"),
|
||||
Battery("sensor.duplicate_battery", "2", "Duplicate Battery")
|
||||
);
|
||||
|
||||
var sut = CreateSut();
|
||||
var result = sut.DiscoverLowBatterySensors(BatteryMonitorConsts.DefaultBatteryThresholdPercent);
|
||||
|
||||
result.Should().HaveCount(1);
|
||||
result.First().FriendlyName.Should().Be("Bathroom Battery");
|
||||
result.First().BatteryLevel.Should().Be(2);
|
||||
result.Should().HaveCount(2);
|
||||
result.Should().OnlyContain(s => s.EntityId == "sensor.duplicate_battery");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DiscoverLowBatterySensors_ShouldUseEntityIdWhenNoFriendlyName()
|
||||
[Theory]
|
||||
[InlineData("Bathroom Battery", "Bathroom Battery")]
|
||||
[InlineData("", "")]
|
||||
[InlineData(" ", " ")]
|
||||
[InlineData(null, "sensor.bathroom_battery")]
|
||||
public void DiscoverLowBatterySensors_ShouldResolveFriendlyName(string? friendlyName, string expectedFriendlyName)
|
||||
{
|
||||
SetupEntities(
|
||||
("sensor.bathroom_battery", "battery", "2", null)
|
||||
);
|
||||
SetupEntities(Battery("sensor.bathroom_battery", "2", friendlyName));
|
||||
|
||||
var sut = CreateSut();
|
||||
var result = sut.DiscoverLowBatterySensors(BatteryMonitorConsts.DefaultBatteryThresholdPercent);
|
||||
|
||||
result.Should().HaveCount(1);
|
||||
result.First().FriendlyName.Should().Be("sensor.bathroom_battery");
|
||||
result.Should().ContainSingle();
|
||||
result[0].EntityId.Should().Be("sensor.bathroom_battery");
|
||||
result[0].FriendlyName.Should().Be(expectedFriendlyName);
|
||||
result[0].BatteryLevel.Should().Be(2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DiscoverLowBatterySensors_ShouldPreserveFractionalBatteryLevel()
|
||||
{
|
||||
SetupEntities(Battery("sensor.bathroom_battery", "2.5", "Bathroom Battery"));
|
||||
|
||||
var sut = CreateSut();
|
||||
var result = sut.DiscoverLowBatterySensors(BatteryMonitorConsts.DefaultBatteryThresholdPercent);
|
||||
|
||||
result.Should().ContainSingle();
|
||||
result[0].FriendlyName.Should().Be("Bathroom Battery");
|
||||
result[0].BatteryLevel.Should().Be(2.5);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DiscoverLowBatterySensors_WithGeneratedSensors_ShouldReturnOnlySensorsBelowThreshold()
|
||||
{
|
||||
SetupEntities(CreateGeneratedBatteryEntities(101));
|
||||
|
||||
var sut = CreateSut();
|
||||
var result = sut.DiscoverLowBatterySensors(7);
|
||||
|
||||
result.Should().HaveCount(7);
|
||||
result.Select(s => s.BatteryLevel).Should().Equal(Enumerable.Range(0, 7).Select(i => (double)i));
|
||||
result.Select(s => s.FriendlyName).Should().OnlyContain(name => name.StartsWith("Generated Battery", StringComparison.Ordinal));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region IsBatterySensor
|
||||
|
||||
[Fact]
|
||||
public void IsBatterySensor_WithBatteryDeviceClass_ShouldReturnTrue()
|
||||
[Theory]
|
||||
[InlineData("sensor.bathroom_battery", "battery", true)]
|
||||
[InlineData("SENSOR.bathroom_battery", "battery", true)]
|
||||
[InlineData("Sensor.Bathroom_Battery", "BATTERY", true)]
|
||||
[InlineData("sensor.bathroom_battery", "Battery", true)]
|
||||
[InlineData("binary_sensor.bathroom_battery", "battery", false)]
|
||||
[InlineData("binary_sensor.sm_s936b_battery_level", "battery", false)]
|
||||
[InlineData("sensor.bathroom_temperature", "temperature", false)]
|
||||
[InlineData("sensor.no_device_class", null, false)]
|
||||
[InlineData("sensor.bathroom_battery", "", false)]
|
||||
[InlineData("sensorx.bathroom_battery", "battery", false)]
|
||||
[InlineData("sensor", "battery", false)]
|
||||
[InlineData("switch.bathroom_battery", "battery", false)]
|
||||
public void IsBatterySensor_ShouldClassifyEntities(string entityId, string? deviceClass, bool expected)
|
||||
{
|
||||
SetupEntities(("sensor.bathroom_battery", "battery", "50", "Bathroom Battery"));
|
||||
SetupEntities(Sensor(entityId, deviceClass, "50", "Some Entity"));
|
||||
|
||||
var entity = new Entity(_haContextMock.Object, entityId);
|
||||
|
||||
BatterySensorDiscoveryService.IsBatterySensor(entity).Should().Be(expected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsBatterySensor_WhenEntityHasNoState_ShouldReturnFalse()
|
||||
{
|
||||
SetupEntities(new EntitySpec { EntityId = "sensor.bathroom_battery", HasState = false });
|
||||
|
||||
var entity = new Entity(_haContextMock.Object, "sensor.bathroom_battery");
|
||||
|
||||
BatterySensorDiscoveryService.IsBatterySensor(entity).Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsBatterySensor_WithNonSensorDomain_ShouldReturnFalse()
|
||||
{
|
||||
SetupEntities(("binary_sensor.bathroom_battery", "battery", "50", "Bathroom Battery"));
|
||||
var entity = new Entity(_haContextMock.Object, "binary_sensor.bathroom_battery");
|
||||
|
||||
BatterySensorDiscoveryService.IsBatterySensor(entity).Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsBatterySensor_WithNonBatteryDeviceClass_ShouldReturnFalse()
|
||||
{
|
||||
SetupEntities(("sensor.bathroom_temperature", "temperature", "22", "Bathroom Temperature"));
|
||||
var entity = new Entity(_haContextMock.Object, "sensor.bathroom_temperature");
|
||||
|
||||
BatterySensorDiscoveryService.IsBatterySensor(entity).Should().BeFalse();
|
||||
}
|
||||
|
||||
@@ -301,18 +287,7 @@ public class BatterySensorDiscoveryServiceTests
|
||||
#region IsExcludedDevice
|
||||
|
||||
[Theory]
|
||||
[InlineData("sensor.erikas_iphone_battery_level", true)]
|
||||
[InlineData("sensor.home_phone_battery_state", true)]
|
||||
[InlineData("sensor.marius_watch_battery_level", true)]
|
||||
[InlineData("sensor.sm_s936b_battery_level", true)]
|
||||
[InlineData("sensor.sm_a536b_battery_level", true)]
|
||||
[InlineData("sensor.pixel_8_battery_level", true)]
|
||||
[InlineData("sensor.oneplus_battery_level", true)]
|
||||
[InlineData("sensor.ipad_battery_level", true)]
|
||||
[InlineData("sensor.galaxy_s24_battery", true)]
|
||||
[InlineData("sensor.bathroom_battery", false)]
|
||||
[InlineData("sensor.boiler_temp_battery", false)]
|
||||
[InlineData("sensor.aquarium_leak_sensor_battery", false)]
|
||||
[MemberData(nameof(ExcludedEntityIdRows), MemberType = typeof(BatteryMonitorTestData))]
|
||||
public void IsExcludedDevice_ShouldCorrectlyIdentifyExclusions(string entityId, bool expected)
|
||||
{
|
||||
BatterySensorDiscoveryService.IsExcludedDevice(entityId).Should().Be(expected);
|
||||
|
||||
Reference in New Issue
Block a user