69 lines
4.2 KiB
C#
69 lines
4.2 KiB
C#
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;
|
|
using Microsoft.Extensions.Options;
|
|
|
|
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)
|
|
{
|
|
_entities = options.Value.Entities;
|
|
Parameters =
|
|
[
|
|
DeviceValues.Number("fragrance_duration", _entities.FragranceDuration, 2, 6, "出香时长"),
|
|
new("fragrance_interval", _entities.FragranceInterval, 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",
|
|
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", "夜灯自动关闭")
|
|
];
|
|
}
|
|
public override string Key => "fragrance";
|
|
public override string Category => nameof(HealthDeviceCategory.Fragrance);
|
|
public override string DisplayName => "智能香氛机";
|
|
public override string PrimaryEntityId => _entities.FragranceSwitch;
|
|
public override IReadOnlyList<DeviceParameterDefinition> Parameters { get; }
|
|
|
|
public override async Task ExecuteSleepAsync(DeviceSceneContext context, PersonalizedSceneConfig config,
|
|
string constitutionCode, CancellationToken cancellationToken)
|
|
{
|
|
await context.RunAsync(new(config.EnableFragrance ? "开启香氛机" : "关闭香氛机",
|
|
PrimaryEntityId, "switch", config.EnableFragrance ? "turn_on" : "turn_off"), cancellationToken);
|
|
if (config.EnableFragrance)
|
|
{
|
|
var level = Math.Clamp(config.FragranceLevel, 1, 3);
|
|
await context.RunAsync(BuildParameterCommand("fragrance_duration", level * 2), cancellationToken);
|
|
await context.RunAsync(BuildParameterCommand("fragrance_interval", 25 - level * 5), cancellationToken);
|
|
}
|
|
await context.RunAsync(new(config.EnableFragrance ? "开启香氛灯" : "关闭香氛灯",
|
|
_entities.FragranceLight, "light", config.EnableFragrance ? "turn_on" : "turn_off",
|
|
config.EnableFragrance ? new Dictionary<string, object?> { ["brightness_pct"] = config.LightBrightnessPercent } : null),
|
|
cancellationToken);
|
|
}
|
|
|
|
public override IEnumerable<DeviceCommand> GetShutdownCommands() =>
|
|
[
|
|
new("关闭香氛机", PrimaryEntityId, "switch", "turn_off", ExpectedState: "off"),
|
|
new("关闭香氛灯", _entities.FragranceLight, "light", "turn_off", ExpectedState: "off"),
|
|
new("关闭感应夜灯", _entities.FragranceAutoLight, "switch", "turn_off", ExpectedState: "off"),
|
|
new("关闭人来增香", _entities.FragranceAutoRelease, "switch", "turn_off", ExpectedState: "off")
|
|
];
|
|
|
|
public override HomeAssistantScenePresetItem? BuildPreview(PersonalizedSceneConfig config, string constitutionCode)
|
|
=> string.IsNullOrWhiteSpace(PrimaryEntityId) ? null : Preview(config.EnableFragrance ? "开启香氛释放" : "关闭香氛释放",
|
|
config.EnableFragrance ? "开启香氛机;香氛灯可用时同步开启柔光" : "当前体质预设不启动香氛",
|
|
new() { ["power"] = config.EnableFragrance ? "开启" : "关闭" });
|
|
}
|