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:
@@ -8,18 +8,18 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" Version="8.0.0">
|
||||
<PackageReference Include="coverlet.collector" Version="10.1.1">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.3.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.12.0" />
|
||||
<PackageReference Include="xunit" Version="2.9.3" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="4.0.1">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="FluentAssertions" Version="8.8.0" />
|
||||
<PackageReference Include="Moq" Version="4.20.72" />
|
||||
<PackageReference Include="FluentAssertions" Version="8.11.0" />
|
||||
<PackageReference Include="Moq" Version="4.21.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -0,0 +1,244 @@
|
||||
using System.Globalization;
|
||||
using System.Text.Json;
|
||||
using BatteryMonitor.Models;
|
||||
using Moq;
|
||||
using NetDaemon.HassModel;
|
||||
using NetDaemon.HassModel.Entities;
|
||||
|
||||
namespace BatteryMonitor.Manager.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// Shared, deterministic builders and datasets used by the BatteryMonitor tests.
|
||||
/// Keeping entity/state construction and generated inputs here means the test
|
||||
/// classes only describe the scenario under test.
|
||||
/// </summary>
|
||||
internal static class BatteryMonitorTestData
|
||||
{
|
||||
public const string DefaultFriendlyName = "Battery Device";
|
||||
|
||||
/// <summary>
|
||||
/// Describes one entity exposed through <see cref="IHaContext.GetAllEntities"/>.
|
||||
/// </summary>
|
||||
public sealed record EntitySpec
|
||||
{
|
||||
public required string EntityId { get; init; }
|
||||
|
||||
public string? State { get; init; } = "50";
|
||||
|
||||
public string? DeviceClass { get; init; } = BatteryMonitorConsts.BatteryDeviceClass;
|
||||
|
||||
public string? FriendlyName { get; init; } = DefaultFriendlyName;
|
||||
|
||||
/// <summary>When false the context returns no <see cref="EntityState"/> for the entity.</summary>
|
||||
public bool HasState { get; init; } = true;
|
||||
}
|
||||
|
||||
/// <summary>A battery device-class sensor.</summary>
|
||||
public static EntitySpec Battery(string entityId, string? state, string? friendlyName = DefaultFriendlyName)
|
||||
{
|
||||
return new EntitySpec
|
||||
{
|
||||
EntityId = entityId,
|
||||
State = state,
|
||||
FriendlyName = friendlyName
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>A sensor with an arbitrary (or missing) device class.</summary>
|
||||
public static EntitySpec Sensor(string entityId, string? deviceClass, string? state, string? friendlyName = DefaultFriendlyName)
|
||||
{
|
||||
return new EntitySpec
|
||||
{
|
||||
EntityId = entityId,
|
||||
DeviceClass = deviceClass,
|
||||
State = state,
|
||||
FriendlyName = friendlyName
|
||||
};
|
||||
}
|
||||
|
||||
public static EntityState CreateEntityState(EntitySpec spec)
|
||||
{
|
||||
var attributes = new Dictionary<string, object?>(StringComparer.Ordinal);
|
||||
|
||||
if (spec.DeviceClass is not null)
|
||||
{
|
||||
attributes["device_class"] = spec.DeviceClass;
|
||||
}
|
||||
|
||||
if (spec.FriendlyName is not null)
|
||||
{
|
||||
attributes["friendly_name"] = spec.FriendlyName;
|
||||
}
|
||||
|
||||
return new EntityState
|
||||
{
|
||||
State = spec.State,
|
||||
AttributesJson = JsonSerializer.SerializeToElement(attributes)
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wires <see cref="IHaContext.GetState"/> and <see cref="IHaContext.GetAllEntities"/>
|
||||
/// on the mock and returns the created entities in declaration order.
|
||||
/// </summary>
|
||||
public static IReadOnlyList<Entity> ConfigureHaContext(Mock<IHaContext> haContextMock, params EntitySpec[] entities)
|
||||
{
|
||||
var entityList = new List<Entity>(entities.Length);
|
||||
|
||||
foreach (var spec in entities)
|
||||
{
|
||||
if (spec.HasState)
|
||||
{
|
||||
haContextMock.Setup(h => h.GetState(spec.EntityId)).Returns(CreateEntityState(spec));
|
||||
}
|
||||
|
||||
entityList.Add(new Entity(haContextMock.Object, spec.EntityId));
|
||||
}
|
||||
|
||||
haContextMock.Setup(h => h.GetAllEntities()).Returns(entityList);
|
||||
|
||||
return entityList;
|
||||
}
|
||||
|
||||
public static BatterySensorInfo CreateSensorInfo(string entityId, double batteryLevel, string? friendlyName = null)
|
||||
{
|
||||
return new BatterySensorInfo
|
||||
{
|
||||
EntityId = entityId,
|
||||
FriendlyName = friendlyName ?? entityId,
|
||||
BatteryLevel = batteryLevel
|
||||
};
|
||||
}
|
||||
|
||||
public static List<BatterySensorInfo> CreateSensorInfos(params (string EntityId, double BatteryLevel)[] sensors)
|
||||
{
|
||||
return sensors
|
||||
.Select(s => CreateSensorInfo(s.EntityId, s.BatteryLevel))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public static List<BatterySensorInfo> CreateSensorInfos(params (string EntityId, string FriendlyName, double BatteryLevel)[] sensors)
|
||||
{
|
||||
return sensors
|
||||
.Select(s => CreateSensorInfo(s.EntityId, s.BatteryLevel, s.FriendlyName))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates battery sensors with every integer level in <c>0..count-1</c>.
|
||||
/// </summary>
|
||||
public static EntitySpec[] CreateGeneratedBatteryEntities(int count)
|
||||
{
|
||||
return Enumerable.Range(0, count)
|
||||
.Select(i => Battery(
|
||||
$"sensor.generated_battery_{i:D3}",
|
||||
i.ToString(CultureInfo.InvariantCulture),
|
||||
$"Generated Battery {i:D3}"))
|
||||
.ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates notification devices with deterministic, non-monotonic levels so
|
||||
/// ordering behaviour is exercised over more than a handful of rows.
|
||||
/// </summary>
|
||||
public static IReadOnlyList<BatterySensorInfo> CreateGeneratedSensorInfos(int count)
|
||||
{
|
||||
return Enumerable.Range(0, count)
|
||||
.Select(i => CreateSensorInfo(
|
||||
$"sensor.generated_{i:D3}",
|
||||
(i * 37) % 101,
|
||||
$"Generated Device {i:D3}"))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// (threshold, state, expected) rows straddling the strict <c>level < threshold</c>
|
||||
/// boundary, including zero/negative thresholds and int min/max thresholds.
|
||||
/// </summary>
|
||||
public static TheoryData<int, string, bool> ThresholdBoundaryRows { get; } = new()
|
||||
{
|
||||
{ 5, "4", true },
|
||||
{ 5, "5", false },
|
||||
{ 5, "6", false },
|
||||
{ 5, "0", true },
|
||||
{ 5, "-3", true },
|
||||
{ 5, "4.9", true },
|
||||
{ 5, "5.0", false },
|
||||
{ 5, "100", false },
|
||||
{ 1, "0.5", true },
|
||||
{ 1, "1", false },
|
||||
{ 20, "19", true },
|
||||
{ 20, "19.99", true },
|
||||
{ 20, "20", false },
|
||||
{ 0, "-1", true },
|
||||
{ 0, "0", false },
|
||||
{ 0, "1", false },
|
||||
{ -5, "-6", true },
|
||||
{ -5, "-5", false },
|
||||
{ -5, "-4", false },
|
||||
{ 100, "99.9", true },
|
||||
{ 100, "100", false },
|
||||
{ 101, "100", true },
|
||||
{ int.MaxValue, "100", true },
|
||||
{ int.MinValue, "-2147483648", false },
|
||||
{ int.MinValue, "-2147483649", true }
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// States that must never produce a <see cref="BatterySensorInfo"/>, including
|
||||
/// whitespace/malformed strings and a null state.
|
||||
/// </summary>
|
||||
public static TheoryData<string?> UnusableStateRows { get; } = new()
|
||||
{
|
||||
"unavailable",
|
||||
"unknown",
|
||||
"UNAVAILABLE",
|
||||
"UNKNOWN",
|
||||
"",
|
||||
" ",
|
||||
"\t",
|
||||
"not_a_number",
|
||||
"3%",
|
||||
"--1",
|
||||
"low",
|
||||
"1 2",
|
||||
(string?)null
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Cross-product of every excluded keyword (lower and upper case) plus
|
||||
/// non-excluded look-alikes that must not be filtered.
|
||||
/// </summary>
|
||||
public static TheoryData<string, bool> ExcludedEntityIdRows()
|
||||
{
|
||||
var data = new TheoryData<string, bool>();
|
||||
|
||||
foreach (var keyword in BatteryMonitorConsts.ExcludedKeywords)
|
||||
{
|
||||
data.Add($"sensor.my_{keyword}_battery_level", true);
|
||||
data.Add($"SENSOR.MY_{keyword.ToUpperInvariant()}_BATTERY_LEVEL", true);
|
||||
}
|
||||
|
||||
data.Add("sensor.iphone", true);
|
||||
data.Add("sensor.x_phone_x", true);
|
||||
data.Add("sensor.bathroom_battery", false);
|
||||
data.Add("sensor.boiler_temp_battery", false);
|
||||
data.Add("sensor.aquarium_leak_sensor_battery", false);
|
||||
data.Add("sensor.temperature", false);
|
||||
data.Add("", false);
|
||||
data.Add(" ", false);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
/// <summary>Device counts used to exercise notification payload generation.</summary>
|
||||
public static TheoryData<int> NotifyDeviceCounts { get; } = new()
|
||||
{
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
5,
|
||||
10,
|
||||
50
|
||||
};
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
using System.Text.Json;
|
||||
using FluentAssertions;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
@@ -5,50 +6,55 @@ using NetDaemon.HassModel;
|
||||
using NetDaemon.HassModel.Entities;
|
||||
using BatteryMonitor.Models;
|
||||
using BatteryMonitorManager;
|
||||
using static BatteryMonitor.Manager.Tests.BatteryMonitorTestData;
|
||||
|
||||
namespace BatteryMonitor.Manager.Tests;
|
||||
|
||||
public class BatteryNotificationServiceTests
|
||||
{
|
||||
private readonly Mock<IHaContext> _haContextMock;
|
||||
private readonly Mock<ILogger<BatteryNotificationService>> _loggerMock;
|
||||
|
||||
public BatteryNotificationServiceTests()
|
||||
{
|
||||
_haContextMock = new Mock<IHaContext>();
|
||||
_loggerMock = new Mock<ILogger<BatteryNotificationService>>();
|
||||
}
|
||||
private readonly Mock<IHaContext> _haContextMock = new();
|
||||
private readonly Mock<ILogger<BatteryNotificationService>> _loggerMock = new();
|
||||
|
||||
private BatteryNotificationService CreateSut()
|
||||
{
|
||||
return new BatteryNotificationService(_haContextMock.Object, _loggerMock.Object);
|
||||
}
|
||||
|
||||
private void VerifyCallService(string service, Times times)
|
||||
{
|
||||
_haContextMock.Verify(h => h.CallService(
|
||||
BatteryMonitorConsts.PersistentNotificationDomain,
|
||||
service,
|
||||
It.IsAny<ServiceTarget?>(),
|
||||
It.IsAny<object?>()), times);
|
||||
}
|
||||
|
||||
#region NotifyLowBattery
|
||||
|
||||
[Fact]
|
||||
public void NotifyLowBattery_WithDevices_ShouldCallPersistentNotificationCreate()
|
||||
[Theory]
|
||||
[MemberData(nameof(NotifyDeviceCounts), MemberType = typeof(BatteryMonitorTestData))]
|
||||
public void NotifyLowBattery_WithDevices_ShouldCallPersistentNotificationCreate(int deviceCount)
|
||||
{
|
||||
var devices = new List<BatterySensorInfo>
|
||||
{
|
||||
new() { EntityId = "sensor.bathroom_battery", FriendlyName = "Bathroom Battery", BatteryLevel = 3 },
|
||||
new() { EntityId = "sensor.bedroom_battery", FriendlyName = "Bedroom Battery", BatteryLevel = 1 }
|
||||
};
|
||||
var devices = CreateSensorInfos(
|
||||
Enumerable.Range(0, deviceCount)
|
||||
.Select(i => ($"sensor.device_{i:D3}", $"Device {i:D3}", (double)(i % 5)))
|
||||
.ToArray());
|
||||
|
||||
var sut = CreateSut();
|
||||
sut.NotifyLowBattery(devices);
|
||||
|
||||
_haContextMock.Verify(h => h.CallService(
|
||||
BatteryMonitorConsts.PersistentNotificationDomain,
|
||||
BatteryMonitorConsts.PersistentNotificationCreateService,
|
||||
It.IsAny<ServiceTarget?>(),
|
||||
It.IsAny<object?>()), Times.Once);
|
||||
VerifyCallService(BatteryMonitorConsts.PersistentNotificationCreateService, Times.Once());
|
||||
VerifyCallService(BatteryMonitorConsts.PersistentNotificationDismissService, Times.Never());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NotifyLowBattery_WithEmptyList_ShouldNotCallService()
|
||||
[Theory]
|
||||
[InlineData(false)]
|
||||
[InlineData(true)]
|
||||
public void NotifyLowBattery_WithEmptyList_ShouldNotCallService(bool useArray)
|
||||
{
|
||||
var devices = new List<BatterySensorInfo>();
|
||||
IReadOnlyList<BatterySensorInfo> devices = useArray
|
||||
? Array.Empty<BatterySensorInfo>()
|
||||
: new List<BatterySensorInfo>();
|
||||
|
||||
var sut = CreateSut();
|
||||
sut.NotifyLowBattery(devices);
|
||||
@@ -58,6 +64,59 @@ public class BatteryNotificationServiceTests
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<ServiceTarget?>(),
|
||||
It.IsAny<object?>()), Times.Never);
|
||||
_loggerMock.Invocations.Should().BeEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NotifyLowBattery_ShouldSendTitleNotificationIdAndDeviceSummary()
|
||||
{
|
||||
object? payload = null;
|
||||
_haContextMock
|
||||
.Setup(h => h.CallService(
|
||||
BatteryMonitorConsts.PersistentNotificationDomain,
|
||||
BatteryMonitorConsts.PersistentNotificationCreateService,
|
||||
It.IsAny<ServiceTarget?>(),
|
||||
It.IsAny<object?>()))
|
||||
.Callback<string, string, ServiceTarget?, object?>((_, _, _, data) => payload = data);
|
||||
|
||||
var sut = CreateSut();
|
||||
sut.NotifyLowBattery(CreateSensorInfos(
|
||||
("sensor.a", "Device A", 4),
|
||||
("sensor.b", "Device B", 1)));
|
||||
|
||||
payload.Should().NotBeNull();
|
||||
|
||||
using var document = JsonDocument.Parse(JsonSerializer.Serialize(payload));
|
||||
var root = document.RootElement;
|
||||
root.GetProperty("title").GetString().Should().Be(BatteryMonitorConsts.NotificationTitle);
|
||||
root.GetProperty("notification_id").GetString().Should().Be(BatteryMonitorConsts.NotificationId);
|
||||
|
||||
var message = root.GetProperty("message").GetString();
|
||||
message.Should().Contain("Device A").And.Contain("Device B");
|
||||
message.Should().Contain("1%").And.Contain("4%");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NotifyLowBattery_WithDevices_ShouldLogDeviceCount()
|
||||
{
|
||||
var devices = CreateSensorInfos(
|
||||
("sensor.a", "Device A", 2),
|
||||
("sensor.b", "Device B", 3));
|
||||
|
||||
var sut = CreateSut();
|
||||
sut.NotifyLowBattery(devices);
|
||||
|
||||
_loggerMock.Verify(
|
||||
l => l.Log(
|
||||
LogLevel.Information,
|
||||
It.IsAny<EventId>(),
|
||||
It.Is<It.IsAnyType>((state, _) => string.Equals(
|
||||
state!.ToString(),
|
||||
"Sent low battery notification for 2 device(s)",
|
||||
StringComparison.Ordinal)),
|
||||
It.IsAny<Exception?>(),
|
||||
It.IsAny<Func<It.IsAnyType, Exception?, string>>()),
|
||||
Times.Once);
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -70,11 +129,25 @@ public class BatteryNotificationServiceTests
|
||||
var sut = CreateSut();
|
||||
sut.DismissNotification();
|
||||
|
||||
_haContextMock.Verify(h => h.CallService(
|
||||
BatteryMonitorConsts.PersistentNotificationDomain,
|
||||
BatteryMonitorConsts.PersistentNotificationDismissService,
|
||||
It.IsAny<ServiceTarget?>(),
|
||||
It.IsAny<object?>()), Times.Once);
|
||||
VerifyCallService(BatteryMonitorConsts.PersistentNotificationDismissService, Times.Once());
|
||||
VerifyCallService(BatteryMonitorConsts.PersistentNotificationCreateService, Times.Never());
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(1)]
|
||||
[InlineData(2)]
|
||||
[InlineData(5)]
|
||||
public void DismissNotification_WhenCalledRepeatedly_ShouldDismissEachTime(int calls)
|
||||
{
|
||||
var sut = CreateSut();
|
||||
|
||||
for (var i = 0; i < calls; i++)
|
||||
{
|
||||
sut.DismissNotification();
|
||||
}
|
||||
|
||||
VerifyCallService(BatteryMonitorConsts.PersistentNotificationDismissService, Times.Exactly(calls));
|
||||
VerifyCallService(BatteryMonitorConsts.PersistentNotificationCreateService, Times.Never());
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -84,41 +157,88 @@ public class BatteryNotificationServiceTests
|
||||
[Fact]
|
||||
public void BuildNotificationMessage_ShouldOrderByBatteryLevel()
|
||||
{
|
||||
var devices = new List<BatterySensorInfo>
|
||||
{
|
||||
new() { EntityId = "sensor.a", FriendlyName = "Device A", BatteryLevel = 4 },
|
||||
new() { EntityId = "sensor.b", FriendlyName = "Device B", BatteryLevel = 1 },
|
||||
new() { EntityId = "sensor.c", FriendlyName = "Device C", BatteryLevel = 3 }
|
||||
};
|
||||
var devices = CreateSensorInfos(
|
||||
("sensor.a", "Device A", 4),
|
||||
("sensor.b", "Device B", 1),
|
||||
("sensor.c", "Device C", 3));
|
||||
|
||||
var message = BatteryNotificationService.BuildNotificationMessage(devices);
|
||||
|
||||
var lines = message.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
|
||||
lines.Should().HaveCount(4);
|
||||
lines[1].Should().Contain("Device B");
|
||||
lines[2].Should().Contain("Device C");
|
||||
lines[3].Should().Contain("Device A");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BuildNotificationMessage_ShouldKeepInsertionOrderForEqualLevels()
|
||||
{
|
||||
var devices = CreateSensorInfos(
|
||||
("sensor.first", "First Device", 4),
|
||||
("sensor.second", "Second Device", 4),
|
||||
("sensor.third", "Third Device", 1));
|
||||
|
||||
var message = BatteryNotificationService.BuildNotificationMessage(devices);
|
||||
|
||||
var lines = message.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
|
||||
lines.Should().HaveCount(4);
|
||||
lines[1].Should().Contain("Third Device");
|
||||
lines[2].Should().Contain("First Device");
|
||||
lines[3].Should().Contain("Second Device");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BuildNotificationMessage_WithGeneratedDevices_ShouldOrderByAscendingLevel()
|
||||
{
|
||||
var devices = CreateGeneratedSensorInfos(50);
|
||||
|
||||
var message = BatteryNotificationService.BuildNotificationMessage(devices);
|
||||
|
||||
var expectedLines = devices
|
||||
.OrderBy(d => d.BatteryLevel)
|
||||
.Select(d => $"- **{d.FriendlyName}**: {d.BatteryLevel:F0}%");
|
||||
|
||||
var lines = message.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
|
||||
lines.Should().HaveCount(devices.Count + 1);
|
||||
lines.Skip(1).Should().Equal(expectedLines);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BuildNotificationMessage_ShouldContainTitle()
|
||||
{
|
||||
var devices = new List<BatterySensorInfo>
|
||||
{
|
||||
new() { EntityId = "sensor.a", FriendlyName = "Device A", BatteryLevel = 2 }
|
||||
};
|
||||
var devices = CreateSensorInfos(("sensor.a", "Device A", 2));
|
||||
|
||||
var message = BatteryNotificationService.BuildNotificationMessage(devices);
|
||||
|
||||
message.Should().Contain("low battery");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("Bathroom Battery", 3, "- **Bathroom Battery**: 3%")]
|
||||
[InlineData("Test Device", 1, "- **Test Device**: 1%")]
|
||||
[InlineData("Empty Battery", 0, "- **Empty Battery**: 0%")]
|
||||
[InlineData("Negative Battery", -7, "- **Negative Battery**: -7%")]
|
||||
[InlineData("Rounded Down", 12.4, "- **Rounded Down**: 12%")]
|
||||
[InlineData("Rounded Up", 12.6, "- **Rounded Up**: 13%")]
|
||||
[InlineData("Full Battery", 100, "- **Full Battery**: 100%")]
|
||||
[InlineData("", 5, "- ****: 5%")]
|
||||
[InlineData(" ", 4, "- ** **: 4%")]
|
||||
public void BuildNotificationMessage_ShouldFormatDeviceLine(string friendlyName, double batteryLevel, string expectedLine)
|
||||
{
|
||||
var devices = CreateSensorInfos(("sensor.a", friendlyName, batteryLevel));
|
||||
|
||||
var message = BatteryNotificationService.BuildNotificationMessage(devices);
|
||||
|
||||
var lines = message.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
|
||||
lines.Should().HaveCount(2);
|
||||
lines[1].Should().Be(expectedLine);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BuildNotificationMessage_ShouldContainDeviceNameAndLevel()
|
||||
{
|
||||
var devices = new List<BatterySensorInfo>
|
||||
{
|
||||
new() { EntityId = "sensor.a", FriendlyName = "Bathroom Battery", BatteryLevel = 3 }
|
||||
};
|
||||
var devices = CreateSensorInfos(("sensor.a", "Bathroom Battery", 3));
|
||||
|
||||
var message = BatteryNotificationService.BuildNotificationMessage(devices);
|
||||
|
||||
@@ -129,15 +249,36 @@ public class BatteryNotificationServiceTests
|
||||
[Fact]
|
||||
public void BuildNotificationMessage_WithSingleDevice_ShouldFormatCorrectly()
|
||||
{
|
||||
var devices = new List<BatterySensorInfo>
|
||||
{
|
||||
new() { EntityId = "sensor.a", FriendlyName = "Test Device", BatteryLevel = 1 }
|
||||
};
|
||||
var devices = CreateSensorInfos(("sensor.a", "Test Device", 1));
|
||||
|
||||
var message = BatteryNotificationService.BuildNotificationMessage(devices);
|
||||
|
||||
message.Should().Contain("- **Test Device**: 1%");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BuildNotificationMessage_WithDuplicateDevices_ShouldListEveryDevice()
|
||||
{
|
||||
var devices = CreateSensorInfos(
|
||||
("sensor.duplicate_battery", "Duplicate Battery", 2),
|
||||
("sensor.duplicate_battery", "Duplicate Battery", 2));
|
||||
|
||||
var message = BatteryNotificationService.BuildNotificationMessage(devices);
|
||||
|
||||
var lines = message.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
|
||||
lines.Should().HaveCount(3);
|
||||
lines[1].Should().Be("- **Duplicate Battery**: 2%");
|
||||
lines[2].Should().Be("- **Duplicate Battery**: 2%");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BuildNotificationMessage_WithNoDevices_ShouldContainOnlyHeader()
|
||||
{
|
||||
var message = BatteryNotificationService.BuildNotificationMessage(Array.Empty<BatterySensorInfo>());
|
||||
|
||||
message.Should().Be("The following devices have low battery:");
|
||||
message.Should().NotContain("- **");
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
@@ -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