f919d6b25b
- Wrap using directives in `#region` blocks across the Battery, Boiler and NordPool sources and tests - Expand single-statement guards and loops into braced blocks - Realign wrapped method arguments, chained calls and continuations - Replace target-typed `new()` with explicit generic construction and empty string literals with `string.Empty` - Flatten the boiler config expression and reindent the remaining touched files
304 lines
12 KiB
C#
304 lines
12 KiB
C#
#region
|
|
|
|
using System.Globalization;
|
|
using BatteryMonitor.Models;
|
|
using BatteryMonitorManager;
|
|
using FluentAssertions;
|
|
using Microsoft.Extensions.Logging;
|
|
using Moq;
|
|
using NetDaemon.HassModel;
|
|
using NetDaemon.HassModel.Entities;
|
|
using static BatteryMonitor.Manager.Tests.BatteryMonitorTestData;
|
|
|
|
#endregion
|
|
|
|
namespace BatteryMonitor.Manager.Tests;
|
|
|
|
public class BatterySensorDiscoveryServiceTests
|
|
{
|
|
private readonly Mock<IHaContext> _haContextMock = new Mock<IHaContext>();
|
|
private readonly Mock<ILogger<BatterySensorDiscoveryService>> _loggerMock = new Mock<ILogger<BatterySensorDiscoveryService>>();
|
|
|
|
private BatterySensorDiscoveryService CreateSut()
|
|
{
|
|
return new BatterySensorDiscoveryService(_haContextMock.Object, _loggerMock.Object);
|
|
}
|
|
|
|
private void SetupEntities(params EntitySpec[] entities)
|
|
{
|
|
ConfigureHaContext(_haContextMock, entities);
|
|
}
|
|
|
|
#region IsExcludedDevice
|
|
|
|
[Theory]
|
|
[MemberData(nameof(ExcludedEntityIdRows), MemberType = typeof(BatteryMonitorTestData))]
|
|
public void IsExcludedDevice_ShouldCorrectlyIdentifyExclusions(string entityId, bool expected)
|
|
{
|
|
BatterySensorDiscoveryService.IsExcludedDevice(entityId).Should().Be(expected);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Discovery
|
|
|
|
[Fact]
|
|
public void DiscoverLowBatterySensors_ShouldFindLowBatteryDevices()
|
|
{
|
|
SetupEntities(
|
|
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();
|
|
var result = sut.DiscoverLowBatterySensors(BatteryMonitorConsts.DefaultBatteryThresholdPercent);
|
|
|
|
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");
|
|
}
|
|
|
|
[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(
|
|
Battery("sensor.bathroom_battery", "2", "Bathroom Battery"),
|
|
Battery(excludedEntityId, "1", friendlyName)
|
|
);
|
|
|
|
var sut = CreateSut();
|
|
var result = sut.DiscoverLowBatterySensors(BatteryMonitorConsts.DefaultBatteryThresholdPercent);
|
|
|
|
result.Should().ContainSingle();
|
|
result[0].EntityId.Should().Be("sensor.bathroom_battery");
|
|
}
|
|
|
|
[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(entityId, deviceClass, "2", "Ignored Entity"),
|
|
Battery("sensor.bedroom_battery", "3", "Bedroom Battery")
|
|
);
|
|
|
|
var sut = CreateSut();
|
|
var result = sut.DiscoverLowBatterySensors(BatteryMonitorConsts.DefaultBatteryThresholdPercent);
|
|
|
|
result.Should().ContainSingle();
|
|
result[0].EntityId.Should().Be("sensor.bedroom_battery");
|
|
}
|
|
|
|
[Theory]
|
|
[MemberData(nameof(UnusableStateRows), MemberType = typeof(BatteryMonitorTestData))]
|
|
public void DiscoverLowBatterySensors_ShouldSkipUnusableStates(string? state)
|
|
{
|
|
SetupEntities(
|
|
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().ContainSingle();
|
|
result[0].EntityId.Should().Be("sensor.bedroom_battery");
|
|
}
|
|
|
|
[Fact]
|
|
public void DiscoverLowBatterySensors_WhenEntityStateIsNull_ShouldSkipSensor()
|
|
{
|
|
SetupEntities(
|
|
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().ContainSingle();
|
|
result[0].EntityId.Should().Be("sensor.bedroom_battery");
|
|
}
|
|
|
|
[Theory]
|
|
[MemberData(nameof(ThresholdBoundaryRows), MemberType = typeof(BatteryMonitorTestData))]
|
|
public void DiscoverLowBatterySensors_ShouldApplyStrictThresholdBoundary(int thresholdPercent,
|
|
string state,
|
|
bool expectedIncluded)
|
|
{
|
|
SetupEntities(Battery("sensor.boundary_battery", state, "Boundary Battery"));
|
|
|
|
var sut = CreateSut();
|
|
var result = sut.DiscoverLowBatterySensors(thresholdPercent);
|
|
|
|
result.Should().HaveCount(expectedIncluded ? 1 : 0);
|
|
|
|
if (!expectedIncluded)
|
|
{
|
|
return;
|
|
}
|
|
|
|
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("sensor.temperature", "temperature", "22", "Temperature"),
|
|
Sensor("switch.some_switch", null, "on", "Switch")
|
|
);
|
|
|
|
var sut = CreateSut();
|
|
var result = sut.DiscoverLowBatterySensors(BatteryMonitorConsts.DefaultBatteryThresholdPercent);
|
|
|
|
result.Should().BeEmpty();
|
|
}
|
|
|
|
[Fact]
|
|
public void DiscoverLowBatterySensors_WhenAllAboveThreshold_ShouldReturnEmpty()
|
|
{
|
|
SetupEntities(
|
|
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();
|
|
var result = sut.DiscoverLowBatterySensors(BatteryMonitorConsts.DefaultBatteryThresholdPercent);
|
|
|
|
result.Should().BeEmpty();
|
|
}
|
|
|
|
[Fact]
|
|
public void DiscoverLowBatterySensors_WhenThereAreNoEntities_ShouldReturnEmpty()
|
|
{
|
|
SetupEntities();
|
|
|
|
var sut = CreateSut();
|
|
var result = sut.DiscoverLowBatterySensors(BatteryMonitorConsts.DefaultBatteryThresholdPercent);
|
|
|
|
result.Should().NotBeNull();
|
|
result.Should().BeEmpty();
|
|
}
|
|
|
|
[Fact]
|
|
public void DiscoverLowBatterySensors_WithDuplicateSensorIds_ShouldReportEachOccurrence()
|
|
{
|
|
SetupEntities(
|
|
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(2);
|
|
result.Should().OnlyContain(s => s.EntityId == "sensor.duplicate_battery");
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("Bathroom Battery", "Bathroom Battery")]
|
|
[InlineData("", "")]
|
|
[InlineData(" ", " ")]
|
|
[InlineData(null, "sensor.bathroom_battery")]
|
|
public void DiscoverLowBatterySensors_ShouldResolveFriendlyName(string? friendlyName, string expectedFriendlyName)
|
|
{
|
|
SetupEntities(Battery("sensor.bathroom_battery", "2", friendlyName));
|
|
|
|
var sut = CreateSut();
|
|
var result = sut.DiscoverLowBatterySensors(BatteryMonitorConsts.DefaultBatteryThresholdPercent);
|
|
|
|
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
|
|
|
|
[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(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().BeFalse();
|
|
}
|
|
|
|
#endregion
|
|
} |