feat: add BatteryMonitor implementation with discovery, notification, and scheduling services
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" Version="8.0.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.3.0" />
|
||||
<PackageReference Include="xunit" Version="2.9.3" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">
|
||||
<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" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="Xunit" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\BatteryMonitor.Manager\BatteryMonitor.Manager.csproj" />
|
||||
<ProjectReference Include="..\BatteryMonitor.Models\BatteryMonitor.Models.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,143 @@
|
||||
using FluentAssertions;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
using NetDaemon.HassModel;
|
||||
using NetDaemon.HassModel.Entities;
|
||||
using BatteryMonitor.Models;
|
||||
using BatteryMonitorManager;
|
||||
|
||||
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 BatteryNotificationService CreateSut()
|
||||
{
|
||||
return new BatteryNotificationService(_haContextMock.Object, _loggerMock.Object);
|
||||
}
|
||||
|
||||
#region NotifyLowBattery
|
||||
|
||||
[Fact]
|
||||
public void NotifyLowBattery_WithDevices_ShouldCallPersistentNotificationCreate()
|
||||
{
|
||||
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 sut = CreateSut();
|
||||
sut.NotifyLowBattery(devices);
|
||||
|
||||
_haContextMock.Verify(h => h.CallService(
|
||||
BatteryMonitorConsts.PersistentNotificationDomain,
|
||||
BatteryMonitorConsts.PersistentNotificationCreateService,
|
||||
It.IsAny<ServiceTarget?>(),
|
||||
It.IsAny<object?>()), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NotifyLowBattery_WithEmptyList_ShouldNotCallService()
|
||||
{
|
||||
var devices = new List<BatterySensorInfo>();
|
||||
|
||||
var sut = CreateSut();
|
||||
sut.NotifyLowBattery(devices);
|
||||
|
||||
_haContextMock.Verify(h => h.CallService(
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<ServiceTarget?>(),
|
||||
It.IsAny<object?>()), Times.Never);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region DismissNotification
|
||||
|
||||
[Fact]
|
||||
public void DismissNotification_ShouldCallPersistentNotificationDismiss()
|
||||
{
|
||||
var sut = CreateSut();
|
||||
sut.DismissNotification();
|
||||
|
||||
_haContextMock.Verify(h => h.CallService(
|
||||
BatteryMonitorConsts.PersistentNotificationDomain,
|
||||
BatteryMonitorConsts.PersistentNotificationDismissService,
|
||||
It.IsAny<ServiceTarget?>(),
|
||||
It.IsAny<object?>()), Times.Once);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region BuildNotificationMessage
|
||||
|
||||
[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 message = BatteryNotificationService.BuildNotificationMessage(devices);
|
||||
|
||||
var lines = message.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
|
||||
lines[1].Should().Contain("Device B");
|
||||
lines[2].Should().Contain("Device C");
|
||||
lines[3].Should().Contain("Device A");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BuildNotificationMessage_ShouldContainTitle()
|
||||
{
|
||||
var devices = new List<BatterySensorInfo>
|
||||
{
|
||||
new() { EntityId = "sensor.a", FriendlyName = "Device A", BatteryLevel = 2 }
|
||||
};
|
||||
|
||||
var message = BatteryNotificationService.BuildNotificationMessage(devices);
|
||||
|
||||
message.Should().Contain("low battery");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BuildNotificationMessage_ShouldContainDeviceNameAndLevel()
|
||||
{
|
||||
var devices = new List<BatterySensorInfo>
|
||||
{
|
||||
new() { EntityId = "sensor.a", FriendlyName = "Bathroom Battery", BatteryLevel = 3 }
|
||||
};
|
||||
|
||||
var message = BatteryNotificationService.BuildNotificationMessage(devices);
|
||||
|
||||
message.Should().Contain("Bathroom Battery");
|
||||
message.Should().Contain("3%");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BuildNotificationMessage_WithSingleDevice_ShouldFormatCorrectly()
|
||||
{
|
||||
var devices = new List<BatterySensorInfo>
|
||||
{
|
||||
new() { EntityId = "sensor.a", FriendlyName = "Test Device", BatteryLevel = 1 }
|
||||
};
|
||||
|
||||
var message = BatteryNotificationService.BuildNotificationMessage(devices);
|
||||
|
||||
message.Should().Contain("- **Test Device**: 1%");
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,322 @@
|
||||
using System.Text.Json;
|
||||
using FluentAssertions;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
using NetDaemon.HassModel;
|
||||
using NetDaemon.HassModel.Entities;
|
||||
using BatteryMonitorManager;
|
||||
using BatteryMonitor.Models;
|
||||
|
||||
namespace BatteryMonitor.Manager.Tests;
|
||||
|
||||
public class BatterySensorDiscoveryServiceTests
|
||||
{
|
||||
private readonly Mock<IHaContext> _haContextMock;
|
||||
private readonly Mock<ILogger<BatterySensorDiscoveryService>> _loggerMock;
|
||||
|
||||
public BatterySensorDiscoveryServiceTests()
|
||||
{
|
||||
_haContextMock = new Mock<IHaContext>();
|
||||
_loggerMock = new Mock<ILogger<BatterySensorDiscoveryService>>();
|
||||
}
|
||||
|
||||
private BatterySensorDiscoveryService CreateSut()
|
||||
{
|
||||
return new BatterySensorDiscoveryService(_haContextMock.Object, _loggerMock.Object);
|
||||
}
|
||||
|
||||
private void SetupEntities(params (string entityId, string? deviceClass, string? state, string? friendlyName)[] 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);
|
||||
}
|
||||
|
||||
#region Discovery
|
||||
|
||||
[Fact]
|
||||
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")
|
||||
);
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DiscoverLowBatterySensors_ShouldExcludePhoneDevices()
|
||||
{
|
||||
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")
|
||||
);
|
||||
|
||||
var sut = CreateSut();
|
||||
var result = sut.DiscoverLowBatterySensors(BatteryMonitorConsts.DefaultBatteryThresholdPercent);
|
||||
|
||||
result.Should().HaveCount(1);
|
||||
result.First().EntityId.Should().Be("sensor.bathroom_battery");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DiscoverLowBatterySensors_ShouldExcludeWatchDevices()
|
||||
{
|
||||
SetupEntities(
|
||||
("sensor.bathroom_battery", "battery", "2", "Bathroom Battery"),
|
||||
("sensor.marius_watch_battery_level", "battery", "1", "Marius Watch Battery")
|
||||
);
|
||||
|
||||
var sut = CreateSut();
|
||||
var result = sut.DiscoverLowBatterySensors(BatteryMonitorConsts.DefaultBatteryThresholdPercent);
|
||||
|
||||
result.Should().HaveCount(1);
|
||||
result.First().EntityId.Should().Be("sensor.bathroom_battery");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DiscoverLowBatterySensors_ShouldExcludeGalaxyDevices()
|
||||
{
|
||||
SetupEntities(
|
||||
("sensor.bathroom_battery", "battery", "2", "Bathroom Battery"),
|
||||
("sensor.sm_s936b_battery_level", "battery", "1", "Galaxy S24 Battery")
|
||||
);
|
||||
|
||||
var sut = CreateSut();
|
||||
var result = sut.DiscoverLowBatterySensors(BatteryMonitorConsts.DefaultBatteryThresholdPercent);
|
||||
|
||||
result.Should().HaveCount(1);
|
||||
result.First().EntityId.Should().Be("sensor.bathroom_battery");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DiscoverLowBatterySensors_ShouldIgnoreNonSensorDomain()
|
||||
{
|
||||
SetupEntities(
|
||||
("binary_sensor.bathroom_battery", "battery", "2", "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_ShouldIgnoreNonBatteryDeviceClass()
|
||||
{
|
||||
SetupEntities(
|
||||
("sensor.bathroom_temperature", "temperature", "2", "Bathroom Temperature"),
|
||||
("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_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");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DiscoverLowBatterySensors_WhenNoBatterySensors_ShouldReturnEmpty()
|
||||
{
|
||||
SetupEntities(
|
||||
("sensor.temperature", "temperature", "22", "Temperature"),
|
||||
("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(
|
||||
("sensor.bathroom_battery", "battery", "50", "Bathroom Battery"),
|
||||
("sensor.bedroom_battery", "battery", "80", "Bedroom Battery")
|
||||
);
|
||||
|
||||
var sut = CreateSut();
|
||||
var result = sut.DiscoverLowBatterySensors(BatteryMonitorConsts.DefaultBatteryThresholdPercent);
|
||||
|
||||
result.Should().BeEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DiscoverLowBatterySensors_ShouldRespectCustomThreshold()
|
||||
{
|
||||
SetupEntities(
|
||||
("sensor.bathroom_battery", "battery", "15", "Bathroom Battery"),
|
||||
("sensor.bedroom_battery", "battery", "25", "Bedroom Battery")
|
||||
);
|
||||
|
||||
var sut = CreateSut();
|
||||
var result = sut.DiscoverLowBatterySensors(20);
|
||||
|
||||
result.Should().HaveCount(1);
|
||||
result.First().EntityId.Should().Be("sensor.bathroom_battery");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DiscoverLowBatterySensors_ShouldPopulateFriendlyName()
|
||||
{
|
||||
SetupEntities(
|
||||
("sensor.bathroom_battery", "battery", "2", "Bathroom 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);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DiscoverLowBatterySensors_ShouldUseEntityIdWhenNoFriendlyName()
|
||||
{
|
||||
SetupEntities(
|
||||
("sensor.bathroom_battery", "battery", "2", null)
|
||||
);
|
||||
|
||||
var sut = CreateSut();
|
||||
var result = sut.DiscoverLowBatterySensors(BatteryMonitorConsts.DefaultBatteryThresholdPercent);
|
||||
|
||||
result.Should().HaveCount(1);
|
||||
result.First().FriendlyName.Should().Be("sensor.bathroom_battery");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region IsBatterySensor
|
||||
|
||||
[Fact]
|
||||
public void IsBatterySensor_WithBatteryDeviceClass_ShouldReturnTrue()
|
||||
{
|
||||
SetupEntities(("sensor.bathroom_battery", "battery", "50", "Bathroom Battery"));
|
||||
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();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#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)]
|
||||
public void IsExcludedDevice_ShouldCorrectlyIdentifyExclusions(string entityId, bool expected)
|
||||
{
|
||||
BatterySensorDiscoveryService.IsExcludedDevice(entityId).Should().Be(expected);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
Reference in New Issue
Block a user