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
190 lines
6.5 KiB
C#
190 lines
6.5 KiB
C#
#region
|
|
|
|
using LightMonitor.Models;
|
|
using NetDaemon.HassModel.Entities;
|
|
|
|
#endregion
|
|
|
|
namespace LightMonitor.Manager.Tests;
|
|
|
|
/// <summary>
|
|
/// Deterministic, reusable fixtures for the LightMonitor tests.
|
|
/// Keeping the option builder, state builders and generated state tables here
|
|
/// avoids copy-pasted setup across the test classes.
|
|
/// </summary>
|
|
internal static class LightMonitorTestData
|
|
{
|
|
public const string LightEntityId = "light.sonoff_1002214d40";
|
|
public const string SunEntityId = "sun.sun";
|
|
|
|
public const string PrimaryPersonEntityId = "person.marius";
|
|
public const string SecondaryPersonEntityId = "person.erika";
|
|
public const string PhonePersonEntityId = "person.home_phone";
|
|
|
|
public static readonly string[] PersonEntityIds =
|
|
[
|
|
PrimaryPersonEntityId,
|
|
SecondaryPersonEntityId,
|
|
PhonePersonEntityId
|
|
];
|
|
|
|
private static readonly string?[] PresenceStates =
|
|
[
|
|
LightMonitorConsts.PersonHomeState,
|
|
"not_home",
|
|
LightMonitorConsts.UnavailableState,
|
|
null
|
|
];
|
|
|
|
/// <summary>
|
|
/// Sun states that should (<c>true</c>) or should not (<c>false</c>) cause a light turn on.
|
|
/// "below_horizon" is compared case-insensitively; unavailable, unknown, blank and
|
|
/// missing states must never turn the light on.
|
|
/// </summary>
|
|
public static TheoryData<string?, bool> SunStateTurnOnCases { get; } = new TheoryData<string?, bool>
|
|
{
|
|
{ LightMonitorConsts.SunBelowHorizon, true },
|
|
{ LightMonitorConsts.SunBelowHorizon.ToUpperInvariant(), true },
|
|
{ "Below_Horizon", true },
|
|
{ LightMonitorConsts.SunAboveHorizon, false },
|
|
{ LightMonitorConsts.SunAboveHorizon.ToUpperInvariant(), false },
|
|
{ LightMonitorConsts.UnavailableState, false },
|
|
{ LightMonitorConsts.UnknownState, false },
|
|
{ string.Empty, false },
|
|
{ " ", false },
|
|
{ null, false }
|
|
};
|
|
|
|
/// <summary>
|
|
/// Light states evaluated while the sun is below the horizon. Only "on" (any casing)
|
|
/// suppresses the turn on call; everything else, including missing state, turns it on.
|
|
/// </summary>
|
|
public static TheoryData<string?, bool> LightStateTurnOnCases { get; } = new TheoryData<string?, bool>
|
|
{
|
|
{ LightMonitorConsts.LightOnState, false },
|
|
{ LightMonitorConsts.LightOnState.ToUpperInvariant(), false },
|
|
{ "On", false },
|
|
{ "off", true },
|
|
{ "OFF", true },
|
|
{ LightMonitorConsts.UnavailableState, true },
|
|
{ LightMonitorConsts.UnknownState, true },
|
|
{ string.Empty, true },
|
|
{ " ", true },
|
|
{ null, true }
|
|
};
|
|
|
|
/// <summary>
|
|
/// Person states checked against the "arrived home" transition.
|
|
/// Only "home" in any casing counts.
|
|
/// </summary>
|
|
public static TheoryData<string?, bool> PersonNewStateCases { get; } = new TheoryData<string?, bool>
|
|
{
|
|
{ LightMonitorConsts.PersonHomeState, true },
|
|
{ "HOME", true },
|
|
{ "Home", true },
|
|
{ "hOmE", true },
|
|
{ "not_home", false },
|
|
{ "not_home ", false },
|
|
{ LightMonitorConsts.UnavailableState, false },
|
|
{ LightMonitorConsts.UnknownState, false },
|
|
{ string.Empty, false },
|
|
{ " ", false },
|
|
{ null, false }
|
|
};
|
|
|
|
/// <summary>
|
|
/// Old person states for a transition to "home". Recovery from unavailable, unknown or
|
|
/// missing state must be ignored; every other old state is a real arrival.
|
|
/// </summary>
|
|
public static TheoryData<string?, bool> PersonArrivalTransitions { get; } = new TheoryData<string?, bool>
|
|
{
|
|
{ "not_home", true },
|
|
{ "work", true },
|
|
{ string.Empty, true },
|
|
{ LightMonitorConsts.UnavailableState, false },
|
|
{ LightMonitorConsts.UnknownState, false },
|
|
{ null, false }
|
|
};
|
|
|
|
/// <summary>
|
|
/// Old sun states for a transition to "below_horizon". Recovery from unavailable, unknown
|
|
/// or missing state must be ignored.
|
|
/// </summary>
|
|
public static TheoryData<string?, bool> SunBelowHorizonTransitions { get; } = new TheoryData<string?, bool>
|
|
{
|
|
{ LightMonitorConsts.SunAboveHorizon, true },
|
|
{ string.Empty, true },
|
|
{ LightMonitorConsts.UnavailableState, false },
|
|
{ LightMonitorConsts.UnknownState, false },
|
|
{ null, false }
|
|
};
|
|
|
|
/// <summary>
|
|
/// Every 4^3 = 64 combination of home / not_home / unavailable / missing state across the
|
|
/// three configured person entities.
|
|
/// </summary>
|
|
public static TheoryData<string?[]> PresenceStateMatrix { get; } = CreatePresenceStateMatrix();
|
|
|
|
/// <summary>
|
|
/// Person id configurations that resolve to "nobody tracked".
|
|
/// </summary>
|
|
public static TheoryData<string[]> NoPersonEntityIdSets { get; } =
|
|
[
|
|
[],
|
|
[string.Empty],
|
|
[" "]
|
|
];
|
|
|
|
/// <summary>
|
|
/// Builds light monitor options with production-compatible defaults.
|
|
/// </summary>
|
|
public static LightMonitorOptions CreateOptions(
|
|
string lightEntityId = LightEntityId,
|
|
string sunEntityId = SunEntityId,
|
|
IEnumerable<string>? personEntityIds = null,
|
|
bool enabled = true)
|
|
{
|
|
return new LightMonitorOptions
|
|
{
|
|
Enabled = enabled,
|
|
LightEntityId = lightEntityId,
|
|
SunEntityId = sunEntityId,
|
|
PersonEntityIds = personEntityIds?.ToArray() ?? [.. PersonEntityIds]
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// Builds an <see cref="EntityState" /> carrying <paramref name="state" />.
|
|
/// </summary>
|
|
public static EntityState CreateState(string? state)
|
|
{
|
|
return new EntityState { State = state };
|
|
}
|
|
|
|
/// <summary>
|
|
/// Builds an <see cref="EntityState" />, or <c>null</c> when the entity has no state at all
|
|
/// (as returned by <c>IHaContext.GetState</c> for a missing entity).
|
|
/// </summary>
|
|
public static EntityState? CreateOptionalState(string? state)
|
|
{
|
|
return state is null ? null : CreateState(state);
|
|
}
|
|
|
|
private static TheoryData<string?[]> CreatePresenceStateMatrix()
|
|
{
|
|
var matrix = new TheoryData<string?[]>();
|
|
|
|
foreach (var first in PresenceStates)
|
|
{
|
|
foreach (var second in PresenceStates)
|
|
{
|
|
foreach (var third in PresenceStates)
|
|
{
|
|
matrix.Add([first, second, third]);
|
|
}
|
|
}
|
|
}
|
|
|
|
return matrix;
|
|
}
|
|
} |