去掉老床垫相关功能

This commit is contained in:
fuenmao
2026-11-09 16:10:05 +08:00
parent 2d44cc3966
commit 3ca90c4cf3
25 changed files with 227 additions and 466 deletions
@@ -20,7 +20,7 @@ internal sealed class LightTransitionWorker : ILightTransitionRuntime
private readonly TimeProvider _time;
private readonly string _entityId;
private readonly string? _sleepEffect;
private readonly bool _controlsMassage;
private readonly bool _isPrimaryLight;
private readonly ILogger<HomeAssistantLightTransitionService> _logger;
private readonly SemaphoreSlim _operationLock = new(1, 1);
private readonly object _statusLock = new();
@@ -38,7 +38,7 @@ internal sealed class LightTransitionWorker : ILightTransitionRuntime
{
_entityId = light?.EntityId ?? options.Value.Entities.AmbientLight;
_sleepEffect = light?.SleepEffect;
_controlsMassage = light is null;
_isPrimaryLight = light is null;
_logger = logger;
_homeAssistant = homeAssistant;
_lightOff = lightOff;
@@ -80,7 +80,7 @@ internal sealed class LightTransitionWorker : ILightTransitionRuntime
var data = BuildSleepStartData(light, colorTemperatureKelvin);
await _homeAssistant.CallServiceAsync("light", "turn_on", data, cancellationToken);
if (_controlsMassage) _safety.MarkOn();
if (_isPrimaryLight) _safety.MarkOn();
SetStatus(true, true, "sleep", SleepStartPercent, 0, "助眠渐暗已启动,每5秒降低1%亮度");
Launch("sleep", SleepStartPercent);
return GetStatus();
@@ -95,7 +95,7 @@ internal sealed class LightTransitionWorker : ILightTransitionRuntime
{
await StopCurrentAsync();
var light = await ReadAvailableLightAsync(cancellationToken);
var knownOffAt = _controlsMassage ? _safety.OffAt : null;
var knownOffAt = _isPrimaryLight ? _safety.OffAt : null;
var isOff = string.Equals(light.State, "off", StringComparison.OrdinalIgnoreCase) ||
(knownOffAt is not null && (light.LastUpdated is null || light.LastUpdated <= knownOffAt));
var brightness = light.GetDoubleAttribute("brightness");
@@ -105,7 +105,7 @@ internal sealed class LightTransitionWorker : ILightTransitionRuntime
if (current >= WakeTargetPercent && !isOff)
{
if (_controlsMassage) _safety.MarkOn();
if (_isPrimaryLight) _safety.MarkOn();
SetStatus(true, false, "wake", current, WakeTargetPercent, "当前亮度已达到70%或更高,保持原亮度");
return GetStatus();
}
@@ -119,7 +119,7 @@ internal sealed class LightTransitionWorker : ILightTransitionRuntime
}, cancellationToken);
}
if (_controlsMassage) _safety.MarkOn();
if (_isPrimaryLight) _safety.MarkOn();
SetStatus(true, true, "wake", current, WakeTargetPercent, "晨起唤醒已启动,每5秒增加1%亮度");
Launch("wake", current);
return GetStatus();
@@ -151,7 +151,7 @@ internal sealed class LightTransitionWorker : ILightTransitionRuntime
JsonElement modes = default;
var hasModes = state.Attributes.ValueKind == JsonValueKind.Object &&
state.Attributes.TryGetProperty("supported_color_modes", out modes) && modes.ValueKind == JsonValueKind.Array;
if (_controlsMassage && !hasModes)
if (_isPrimaryLight && !hasModes)
{
data["color_temp_kelvin"] = Math.Clamp(kelvin, 2200, 5000);
return data;
@@ -208,17 +208,17 @@ internal sealed class LightTransitionWorker : ILightTransitionRuntime
var light = await ReadAvailableLightAsync(cancellationToken);
if (!string.Equals(light.State, "on", StringComparison.OrdinalIgnoreCase))
{
if (_controlsMassage) _safety.MarkOff(_time.GetUtcNow());
if (mode == "sleep" && _controlsMassage)
if (_isPrimaryLight) _safety.MarkOff(_time.GetUtcNow());
if (mode == "sleep" && _isPrimaryLight)
{
try
{
await _lightOff.NotifyAsync(cancellationToken);
SetStatus(true, false, mode, 0, 0, "灯已关闭,床垫按摩已停止");
SetStatus(true, false, mode, 0, 0, "灯已关闭,渐变已停止");
}
catch (Exception exception) when (exception is not OperationCanceledException)
{
_logger.LogWarning(exception, "灯已关闭,但床垫按摩停止失败");
_logger.LogWarning(exception, "灯已关闭,但关灯事件处理失败");
SetStatus(false, false, mode, 0, 0, exception.Message);
}
}
@@ -245,16 +245,16 @@ internal sealed class LightTransitionWorker : ILightTransitionRuntime
{
["entity_id"] = LightEntityId
}, cancellationToken);
if (_controlsMassage) _safety.MarkOff(_time.GetUtcNow());
SetStatus(true, false, mode, 0, 0, _controlsMassage ? "灯光已关闭,正在停止床垫按摩" : "助眠渐暗已完成,灯光已关闭");
if (_isPrimaryLight) _safety.MarkOff(_time.GetUtcNow());
SetStatus(true, false, mode, 0, 0, "助眠渐暗已完成,灯光已关闭");
try
{
if (_controlsMassage) await _lightOff.NotifyAsync(cancellationToken);
SetStatus(true, false, mode, 0, 0, _controlsMassage ? "助眠渐暗已完成,灯光与床垫按摩已关闭" : "助眠渐暗已完成,灯光已关闭");
if (_isPrimaryLight) await _lightOff.NotifyAsync(cancellationToken);
SetStatus(true, false, mode, 0, 0, "助眠渐暗已完成,灯光已关闭");
}
catch (Exception exception) when (exception is not OperationCanceledException)
{
_logger.LogWarning(exception, "助眠渐暗已完成,但床垫按摩停止失败");
_logger.LogWarning(exception, "助眠渐暗已完成,但关灯事件处理失败");
SetStatus(false, false, mode, 0, 0, exception.Message);
}
return;