feat: add BatteryMonitor implementation with discovery, notification, and scheduling services
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
namespace LightMonitorManager.Interface;
|
||||
|
||||
public interface ILightControlService
|
||||
{
|
||||
void TurnOnLight();
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace LightMonitorManager.Interface;
|
||||
|
||||
public interface ILightMonitorInit
|
||||
{
|
||||
Task InitializeAsync();
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace LightMonitorManager.Interface;
|
||||
|
||||
public interface IPresenceService
|
||||
{
|
||||
bool IsAnyoneHome();
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using NetDaemon.HassModel;
|
||||
using NetDaemon.HassModel.Entities;
|
||||
using LightMonitor.Models;
|
||||
using LightMonitorManager.Interface;
|
||||
using LightMonitorManager.Models;
|
||||
|
||||
namespace LightMonitorManager;
|
||||
|
||||
public sealed class LightControlService : ILightControlService
|
||||
{
|
||||
private readonly IHaContext _haContext;
|
||||
private readonly ILogger<LightControlService> _logger;
|
||||
private readonly LightMonitorOptions _options;
|
||||
|
||||
public LightControlService(
|
||||
IHaContext haContext,
|
||||
ILogger<LightControlService> logger,
|
||||
IOptions<LightMonitorOptions> options)
|
||||
{
|
||||
_haContext = haContext;
|
||||
_logger = logger;
|
||||
_options = options.Value;
|
||||
}
|
||||
|
||||
public void TurnOnLight()
|
||||
{
|
||||
var sunState = _haContext.GetState(_options.SunEntityId)?.State;
|
||||
|
||||
if (!string.Equals(sunState, LightMonitorConsts.SunBelowHorizon, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
_logger.LogDebug("Sun is not below horizon (state={SunState}), skipping light turn on", sunState);
|
||||
return;
|
||||
}
|
||||
|
||||
var lightState = _haContext.GetState(_options.LightEntityId)?.State;
|
||||
|
||||
if (string.Equals(lightState, LightMonitorConsts.LightOnState, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
_logger.LogDebug("Light {EntityId} is already on, skipping", _options.LightEntityId);
|
||||
return;
|
||||
}
|
||||
|
||||
_haContext.CallService("light", "turn_on", ServiceTarget.FromEntity(_options.LightEntityId));
|
||||
_logger.LogInformation("Light {EntityId} turned ON (dark outside + person home)", _options.LightEntityId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<RootNamespace>LightMonitorManager</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
|
||||
<_Parameter1>LightMonitor.Manager.Tests</_Parameter1>
|
||||
</AssemblyAttribute>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\HomeAssistant.Entities\HomeAssistant.Entities.csproj" />
|
||||
<ProjectReference Include="..\LightMonitor.Models\LightMonitor.Models.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="10.0.5" />
|
||||
<PackageReference Include="NetDaemon.HassModel" Version="26.11.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,105 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using NetDaemon.AppModel;
|
||||
using NetDaemon.HassModel;
|
||||
using NetDaemon.HassModel.Entities;
|
||||
using LightMonitor.Models;
|
||||
using LightMonitorManager.Interface;
|
||||
using LightMonitorManager.Models;
|
||||
|
||||
namespace LightMonitorManager;
|
||||
|
||||
[NetDaemonApp]
|
||||
public sealed class LightMonitorInit : ILightMonitorInit
|
||||
{
|
||||
private readonly IHaContext _haContext;
|
||||
private readonly ILogger<LightMonitorInit> _logger;
|
||||
private readonly IPresenceService _presenceService;
|
||||
private readonly ILightControlService _lightControlService;
|
||||
private readonly LightMonitorOptions _options;
|
||||
|
||||
public LightMonitorInit(
|
||||
IHaContext haContext,
|
||||
ILogger<LightMonitorInit> logger,
|
||||
IPresenceService presenceService,
|
||||
ILightControlService lightControlService,
|
||||
IOptions<LightMonitorOptions> options)
|
||||
{
|
||||
_haContext = haContext;
|
||||
_logger = logger;
|
||||
_presenceService = presenceService;
|
||||
_lightControlService = lightControlService;
|
||||
_options = options.Value;
|
||||
|
||||
_ = InitializeAsync();
|
||||
}
|
||||
|
||||
public Task InitializeAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
SubscribeToSunStateChanges();
|
||||
SubscribeToPersonStateChanges();
|
||||
_logger.LogInformation("LightMonitor initialized with {PersonCount} tracked persons", _options.PersonEntityIds.Length);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error during LightMonitor initialization");
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private void SubscribeToSunStateChanges()
|
||||
{
|
||||
var sunEntity = new Entity(_haContext, _options.SunEntityId);
|
||||
sunEntity.StateChanges()
|
||||
.Subscribe(e =>
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!string.Equals(e.New?.State, LightMonitorConsts.SunBelowHorizon, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_logger.LogInformation("Sun went below horizon, checking if anyone is home");
|
||||
|
||||
if (_presenceService.IsAnyoneHome())
|
||||
{
|
||||
_lightControlService.TurnOnLight();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error handling sun state change");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void SubscribeToPersonStateChanges()
|
||||
{
|
||||
foreach (var personEntityId in _options.PersonEntityIds)
|
||||
{
|
||||
var personEntity = new Entity(_haContext, personEntityId);
|
||||
personEntity.StateChanges()
|
||||
.Subscribe(e =>
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!string.Equals(e.New?.State, LightMonitorConsts.PersonHomeState, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_logger.LogInformation("Person {EntityId} arrived home, checking if it is dark", personEntityId);
|
||||
_lightControlService.TurnOnLight();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error handling person state change for {EntityId}", personEntityId);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using NetDaemon.AppModel;
|
||||
using LightMonitorManager.Interface;
|
||||
using LightMonitorManager.Models;
|
||||
|
||||
namespace LightMonitorManager;
|
||||
|
||||
public static class LightMonitorServiceRegistration
|
||||
{
|
||||
public static IServiceCollection AddLightMonitor(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
var options = configuration
|
||||
.GetSection(LightMonitorOptions.SectionName)
|
||||
.Get<LightMonitorOptions>() ?? new LightMonitorOptions();
|
||||
|
||||
if (!options.Enabled)
|
||||
{
|
||||
return services;
|
||||
}
|
||||
|
||||
services
|
||||
.AddNetDaemonApp<LightMonitorInit>()
|
||||
.AddScoped<IPresenceService, PresenceService>()
|
||||
.AddScoped<ILightControlService, LightControlService>();
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using static LightMonitor.Models.LightMonitorConsts;
|
||||
|
||||
namespace LightMonitorManager.Models;
|
||||
|
||||
public sealed class LightMonitorOptions
|
||||
{
|
||||
public const string SectionName = "LightMonitor";
|
||||
|
||||
public bool Enabled { get; set; }
|
||||
public string LightEntityId { get; set; } = DefaultLightEntityId;
|
||||
public string SunEntityId { get; set; } = DefaultSunEntityId;
|
||||
public string[] PersonEntityIds { get; set; } = DefaultPersonEntityIds;
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using NetDaemon.HassModel;
|
||||
using LightMonitor.Models;
|
||||
using LightMonitorManager.Interface;
|
||||
using LightMonitorManager.Models;
|
||||
|
||||
namespace LightMonitorManager;
|
||||
|
||||
public sealed class PresenceService : IPresenceService
|
||||
{
|
||||
private readonly IHaContext _haContext;
|
||||
private readonly ILogger<PresenceService> _logger;
|
||||
private readonly LightMonitorOptions _options;
|
||||
|
||||
public PresenceService(
|
||||
IHaContext haContext,
|
||||
ILogger<PresenceService> logger,
|
||||
IOptions<LightMonitorOptions> options)
|
||||
{
|
||||
_haContext = haContext;
|
||||
_logger = logger;
|
||||
_options = options.Value;
|
||||
}
|
||||
|
||||
public bool IsAnyoneHome()
|
||||
{
|
||||
foreach (var personEntityId in _options.PersonEntityIds)
|
||||
{
|
||||
var state = _haContext.GetState(personEntityId);
|
||||
|
||||
if (state?.State is null or "unavailable" or "unknown")
|
||||
{
|
||||
_logger.LogDebug("Person entity {EntityId} state unavailable", personEntityId);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (string.Equals(state.State, LightMonitorConsts.PersonHomeState, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
_logger.LogDebug("Person {EntityId} is home", personEntityId);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
_logger.LogDebug("No person is currently home");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user