This commit is contained in:
fuenmao
2026-11-12 15:45:52 +08:00
parent 61e25175db
commit 718b138033
30 changed files with 1511 additions and 99 deletions
@@ -9,36 +9,52 @@ namespace Evergreen.Health.Application.Health.Devices.Fragrance;
public sealed class FragranceModule : HealthDeviceModule
{
private readonly HomeAssistantEntityOptions _entities;
public FragranceModule(DeviceActionRunner runner, IOptions<HomeAssistantOptions> options) : base(runner)
private readonly bool _wakeDevice;
public FragranceModule(DeviceActionRunner runner, IOptions<HomeAssistantOptions> options)
: this(runner, options, wakeDevice: false) { }
internal FragranceModule(DeviceActionRunner runner, IOptions<HomeAssistantOptions> options, bool wakeDevice) : base(runner)
{
_entities = options.Value.Entities;
Parameters =
_wakeDevice = wakeDevice;
var duration = wakeDevice ? _entities.WakeFragranceDuration : _entities.FragranceDuration;
var intervalEntity = wakeDevice ? _entities.WakeFragranceInterval : _entities.FragranceInterval;
var light = wakeDevice ? _entities.WakeFragranceLight : _entities.FragranceLight;
var quickRelease = wakeDevice ? _entities.WakeFragranceQuickRelease : _entities.FragranceQuickRelease;
var autoRelease = wakeDevice ? _entities.WakeFragranceAutoRelease : _entities.FragranceAutoRelease;
var autoLight = wakeDevice ? _entities.WakeFragranceAutoLight : _entities.FragranceAutoLight;
var lightAutoOff = wakeDevice ? _entities.WakeFragranceLightAutoOff : _entities.FragranceLightAutoOff;
DeviceParameterDefinition[] parameters =
[
DeviceValues.Number("fragrance_duration", _entities.FragranceDuration, 2, 6, "出香时长"),
new("fragrance_interval", _entities.FragranceInterval, value =>
DeviceValues.Number("fragrance_duration", duration, 2, 6, "出香时长"),
new("fragrance_interval", intervalEntity, value =>
{
var interval = DeviceValues.Integer(value, 10, 20, "出香间隔");
if (interval % 5 != 0) throw new InvalidOperationException("出香间隔仅支持 10、15 或 20 分钟");
return new("设置出香间隔", _entities.FragranceInterval, "number", "set_value",
return new("设置出香间隔", intervalEntity, "number", "set_value",
new Dictionary<string, object?> { ["value"] = interval });
}, "出香间隔", 10, 20),
DeviceValues.Switch("fragrance_light", _entities.FragranceLight, "light", "香氛灯"),
new("fragrance_quick_release", _entities.FragranceQuickRelease, _ => new("快速出香",
_entities.FragranceQuickRelease, "button", "press"), "快速出香"),
DeviceValues.Switch("fragrance_auto_release", _entities.FragranceAutoRelease, "switch", "人来增香"),
DeviceValues.Switch("fragrance_auto_light", _entities.FragranceAutoLight, "switch", "感应夜灯"),
DeviceValues.Switch("fragrance_light_auto_off", _entities.FragranceLightAutoOff, "switch", "夜灯自动关闭")
DeviceValues.Switch("fragrance_light", light, "light", "香氛灯"),
new("fragrance_quick_release", quickRelease, _ => BuildQuickRelease(quickRelease), "快速出香"),
DeviceValues.Switch("fragrance_auto_release", autoRelease, "switch", "人来增香"),
DeviceValues.Switch("fragrance_auto_light", autoLight, "switch", "感应夜灯"),
DeviceValues.Switch("fragrance_light_auto_off", lightAutoOff, "switch", "夜灯自动关闭")
];
Parameters = wakeDevice ? parameters.Select(parameter => parameter with { Key = $"wake_{parameter.Key}" }).ToArray() : parameters;
}
public override string Key => "fragrance";
public override string Key => _wakeDevice ? "wake-fragrance" : "fragrance";
public override string Category => nameof(HealthDeviceCategory.Fragrance);
public override string DisplayName => "智能香氛机";
public override string PrimaryEntityId => _entities.FragranceSwitch;
public override string DisplayName => _wakeDevice ? "香氛机 唤醒" : "智能香氛机";
public override string PrimaryEntityId => _wakeDevice ? _entities.WakeFragranceSwitch : _entities.FragranceSwitch;
public override IReadOnlyList<DeviceParameterDefinition> Parameters { get; }
internal static DeviceCommand BuildQuickRelease(string entityId) => new("快速出香", entityId, "button", "press");
public override async Task ExecuteSleepAsync(DeviceSceneContext context, PersonalizedSceneConfig config,
string constitutionCode, CancellationToken cancellationToken)
{
if (_wakeDevice) return;
await context.RunAsync(new(config.EnableFragrance ? "开启香氛机" : "关闭香氛机",
PrimaryEntityId, "switch", config.EnableFragrance ? "turn_on" : "turn_off"), cancellationToken);
if (config.EnableFragrance)
@@ -53,7 +69,7 @@ public sealed class FragranceModule : HealthDeviceModule
cancellationToken);
}
public override IEnumerable<DeviceCommand> GetShutdownCommands() =>
public override IEnumerable<DeviceCommand> GetShutdownCommands() => _wakeDevice ? [] :
[
new("关闭香氛机", PrimaryEntityId, "switch", "turn_off", ExpectedState: "off"),
new("关闭香氛灯", _entities.FragranceLight, "light", "turn_off", ExpectedState: "off"),
@@ -62,7 +78,7 @@ public sealed class FragranceModule : HealthDeviceModule
];
public override HomeAssistantScenePresetItem? BuildPreview(PersonalizedSceneConfig config, string constitutionCode)
=> string.IsNullOrWhiteSpace(PrimaryEntityId) ? null : Preview(config.EnableFragrance ? "开启香氛释放" : "关闭香氛释放",
=> _wakeDevice || string.IsNullOrWhiteSpace(PrimaryEntityId) ? null : Preview(config.EnableFragrance ? "开启香氛释放" : "关闭香氛释放",
config.EnableFragrance ? "开启香氛机;香氛灯可用时同步开启柔光" : "当前体质预设不启动香氛",
new() { ["power"] = config.EnableFragrance ? "开启" : "关闭" });
}