← All source files

V17 / d6d373561692

HomeOperations.cs

Lilith/Core/HomeAutomation/HomeOperations.cs

Unchanged from the preceding version
CS
using System.Text.Json;
using Lilith.Core.Commands;

namespace Lilith.Core.HomeAutomation;

internal static class HomeOperations
{
    internal static readonly HashSet<string> Names=["home_devices_operation","home_state_operation","home_power_operation","home_brightness_operation","home_color_operation"];
    internal static bool Mutates(string name)=>name is "home_power_operation" or "home_brightness_operation" or "home_color_operation";
    internal static async Task<string> ExecuteAsync(ProviderClient provider,string name,IReadOnlyDictionary<string,string> inputs,CancellationToken ct)
    {
        string action=name switch {"home_devices_operation"=>"devices","home_state_operation"=>"state","home_power_operation"=>"power","home_brightness_operation"=>"brightness","home_color_operation"=>"color",_=>throw new ArgumentException("Unknown home task.")};
        return JsonSerializer.Serialize(await (provider.Home??throw new InvalidOperationException("Home automation is not configured.")).ExecuteAsync(action,inputs.GetValueOrDefault("device")??"",inputs.GetValueOrDefault("value")??"",ct));
    }
}
public static class HomeCommands
{
    public static void Register(ChatCommandRegistry commands,Func<ProviderClient> provider)
    {
        commands.RegisterAsync("home-pending","Review exact pending home-device tasks.",async c=>new(true,await (provider().Home??throw new InvalidOperationException("Home automation not configured.")).ReviewAsync(c.CancellationToken)));
        foreach(bool approve in new[]{true,false})
        {
            bool decision=approve;
            commands.RegisterAsync(approve?"home-approve":"home-reject","Decide an exact home proposal: PROPOSAL_ID.",async c=>new(true,await provider().DecideHomeAsync(c.Arguments.Trim(),decision,c.CancellationToken)));
        }
    }
}