using Evergreen.Health.Application.Health.Devices.Contracts; using Evergreen.Health.Application.Health.Integrations.HomeAssistant; using Evergreen.Health.Application.Health.Models; using Evergreen.Health.Application.Health.Scenes; namespace Evergreen.Health.Application.Health.Devices.Lighting; /// 额外场景灯复用标准参数和单灯渐变;型号专属模式通过配置选择,不套用原灯的关灯事件/唤醒钩子。 public sealed class ConfiguredSleepLightModule : HealthDeviceModule { private readonly SceneLightOptions _light; private readonly ILightTransitionRuntime _transition; public ConfiguredSleepLightModule(DeviceActionRunner runner, SceneLightOptions light, ILightTransitionRuntime transition) : base(runner) => (_light, _transition) = (light, transition); public override string Key => _light.Key; public override string Category => nameof(HealthDeviceCategory.Lighting); public override string DisplayName => _light.Name; public override string PrimaryEntityId => _light.EntityId; public override IReadOnlyList Parameters => []; public override async Task ExecuteParameterAsync(DeviceCommand command, CancellationToken token) { await _transition.CancelAsync(token); await base.ExecuteParameterAsync(command, token); } public override async Task SetPowerAsync(bool turnOn, CancellationToken token) { if (turnOn && _light.StartSleepOnManualPowerOn) { await _transition.StartSleepAsync(2700, token); return; } await _transition.CancelAsync(token); if (turnOn) { await base.SetPowerAsync(true, token); return; } var command = GetShutdownCommands().Single(); await Runner.SendAsync(command, token); await Runner.VerifyParameterAsync(command, token); } public override async Task ExecuteSleepAsync(DeviceSceneContext context, PersonalizedSceneConfig config, string constitutionCode, CancellationToken token) { if (!context.States.TryGetValue(PrimaryEntityId, out var state) || !DeviceActionRunner.IsAvailable(state)) { context.Results.Add(new() { Name = $"开启{DisplayName}助眠渐暗", EntityId = PrimaryEntityId, Status = "Skipped", Message = "场景灯当前离线或不可用" }); return; } await context.ExternalAsync($"开启{DisplayName}助眠渐暗", PrimaryEntityId, async cancellation => { await _transition.StartSleepAsync(config.LightColorTemperatureKelvin, cancellation); }, token); } public override IEnumerable GetShutdownCommands() => [new($"关闭{DisplayName}", PrimaryEntityId, "light", "turn_off", ExpectedState: "off")]; public override HomeAssistantScenePresetItem? BuildPreview(PersonalizedSceneConfig config, string constitutionCode) => Preview("开启助眠渐暗灯光", $"{_light.SleepEffect ?? "助眠渐暗"} · 从50%开始,每5秒降低1%,最终关灯", new() { ["brightness"] = "50%→关闭", ["effect"] = _light.SleepEffect }); }