111111
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
using Evergreen.Health.Application.Health.Integrations.HomeAssistant;
|
||||
using Evergreen.Health.Application.Health.Integrations.MiAirX;
|
||||
using Evergreen.Health.Application.Health.Scenes;
|
||||
using Evergreen.Health.Application.Health.Sleep;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Evergreen.Health.Application.Health.Devices.Audio;
|
||||
|
||||
public sealed class BedPresenceSleepMusicAction(IHomeAssistantGateway gateway, TcmMusicTransitionService music,
|
||||
IOptions<HomeAssistantOptions> options) : IBedPresenceAction
|
||||
{
|
||||
private const string Playlist = "balanced";
|
||||
|
||||
public string Key => "speaker-sleep-music";
|
||||
public string Description => "用 Xiaomi Sound Pro 播放舒睡音乐";
|
||||
public string ConditionLabel => "音箱空闲时播放默认舒睡曲目并渐弱音量。";
|
||||
|
||||
public bool SupportsTarget(string entityId) => options.Value.Enabled &&
|
||||
entityId?.StartsWith("media_player.", StringComparison.Ordinal) == true && entityId == options.Value.Entities.Speaker;
|
||||
|
||||
public async Task<string> ExecuteAsync(string entityId, CancellationToken token)
|
||||
{
|
||||
if (!SupportsTarget(entityId)) return "target-not-configured";
|
||||
if (!music.HasPlayableTracks(Playlist)) return "speaker-music-not-configured";
|
||||
var states = await gateway.GetStatesAsync(token);
|
||||
var speaker = states.FirstOrDefault(state => string.Equals(state.EntityId, entityId, StringComparison.OrdinalIgnoreCase));
|
||||
if (speaker is null || !DeviceActionRunner.IsAvailable(speaker)) return "speaker-state-unavailable";
|
||||
if (speaker.State is not ("idle" or "off" or "standby")) return "speaker-not-idle";
|
||||
return await music.TryStartIdlePlaybackAsync(Playlist, token) ? "speaker-playback-sent" : "speaker-not-idle";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user