YeelightPro 2.4.3

dotnet add package YeelightPro --version 2.4.3
                    
NuGet\Install-Package YeelightPro -Version 2.4.3
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="YeelightPro" Version="2.4.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="YeelightPro" Version="2.4.3" />
                    
Directory.Packages.props
<PackageReference Include="YeelightPro" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add YeelightPro --version 2.4.3
                    
#r "nuget: YeelightPro, 2.4.3"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package YeelightPro@2.4.3
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=YeelightPro&version=2.4.3
                    
Install as a Cake Addin
#tool nuget:?package=YeelightPro&version=2.4.3
                    
Install as a Cake Tool

YeelightPro 局域网本地控制

[个人根据官方局域网通讯协议协议开发,非官方库] [现在已支持Native AOT!!!] Yeelight Pro照明控制系统由硬件、云端、用户App组成。硬件包括网关、灯具、传感器、面板、全面屏等组成。网关通过蓝牙MESH协议与其他蓝牙mesh子设备进行通信,通过Wi-Fi或者有线与云端/本地服务器通信。

本库通过 局域网协议(Ver2.4)与 S21/S21增强版 网关进行本地通讯控制以上描述设备,相关可控制设备详见: https://cn.yeelight.com/zh_CN/pro

(特别说明:非 Yeelight 普通线上产品控制库,也就是连接米家的那些设备,无法使用本库控制。)

Nuget

NUGET

dotnet add package YeelightPro --version 2.4.3

基本使用方法

初始化

       
        private Gateway _gateway;
        _gateway = new Gateway("192.168.0.0");
        //连接记录
        _gateway.CommunicationRecord += (_, r) =>
        {
            //r:GatewayCommunicationRecordEventArgs
        };
        //连接改变事件
        _gateway.ConnectChanged += async (_, e) =>
        {
            //重连操作
            if (!e.Connected)
            {
                while (true)
                {
                    //等待10S
                    await Task.Delay(10000);
                    try
                    {
                        _gateway.Connect();
                        return;
                    }
                    catch (YeelightPro.GatewayRepeatedConnectException ex)
                    {
                             //重复连接 说明已经连接上了
                             //      return;
                    }
                    catch (Exception ex)
                    {
                        //其他则是连接失败
                    }
                }
            }
        };
        //设备事件触发事件
        _gateway.EventTriggered += Gateway_EventTriggered;
        //设备属性变化事件
        _gateway.PropertiesUpdated += Gateway_PropertiesUpdated;
        //连接
        _gateway.Connect();

控制设备

//控制逻辑为:生成控制命令->执行控制命令
//控制命令模板为 GatewayCommandModel

//属性举例:构建控制灯具类设备命令
var lightCmd=new GatewayCommandModel()
{
    //节点设备ID 
    Id=id,
    //期待设置的属性和⽬标值
    Set=new Dictionary<string, object>()
    {
        //从 GatewayNodeDeviceProperties 选择属性名称,属性注释上表明了 操作包含 ‘写’ 的是属于可控的,并且标标明了相关类型可范围
       { GatewayNodeDeviceProperties.Light_Power, true }, //开灯
       { GatewayNodeDeviceProperties.Light_Brightness, 50} //50%的亮度
    },
    //渐变事件 单位 毫秒
    Duration=2000
};
//新增工厂方法协助设置
//在YeelightPro.Models 名明明空间下 xxxSet类,协助生成 Dictionary<string, object>
//new LightSet().SetPower(true).SetBrightness(50).ToSet() 生成-> new Dictionary<string, object>()  {     { GatewayNodeDeviceProperties.Light_Power, true },       { GatewayNodeDeviceProperties.Light_Brightness, 50}   }
//该方法使用后 一定要使用.ToSet()为结尾哈!;
var lightCmd=new GatewayCommandModel()
{
    //节点设备ID 
    Id=id,
    //期待设置的属性和⽬标值
    Set=new LightSet()
        .SetPower(true)
        .SetBrightness(50)
        .SetColorTemperature(3000)
        .ToSet() }
    //渐变事件 单位 毫秒
    Duration=2000
};

//行为举例:构建控制床帘类设备命令
var curtainCmd=new GatewayCommandModel()
{
    //节点设备ID 
    Id=id,
    //行为动作
    Action=new GatewayCommandActionModel()
    {
        //调整窗帘电机动作
        MotorAdjust =new GatewayCommandActionMotorAdjustModel()
        {
            //选择 GatewayCommandActionMotorAdjustModel 里面的常量字段
            Type=GatewayCommandActionMotorAdjustModel.Auto
        }
    }
}

//通过命令组进行命令控制执行
(bool executed, string? msg)=await _gateway.CommandAsync([lightCmd,curtainCmd]);

读取设备属性


//设备属性模型为 GatewayNodeDeviceModel
//所有设备属性名称以及定义都放在了 GatewayNodeDeviceProperties
//其中 设备的详细参数为  GatewayNodeDeviceModel.Params,该类型为System.Text.Json.Nodes.JsonObject
//并且  设备属性更新事件  与 设备触发事件 的相关参数 也为 System.Text.Json.Nodes.JsonObject 类型

//模型属性举例: 获取 pid为 198888的 ⾊温可调灯具 GatewayNodeDeviceType.Light_Temperature

 var device= _client.NodeDevices[198888];
//获取灯的亮度
 var brightness= device.Params[GatewayNodeDeviceProperties.Light_Brightness].GetValue<double>();
//获取灯的色温
var brightness= device.Params[GatewayNodeDeviceProperties.Light_ColorTemperature].GetValue<double>();


//在属性更新时间里也同样
private void Gateway_PropertiesUpdated(object? sender, GatewayPropertiesUpdatedEventArgs e)
{
     if(e.Id==198888)
     {
     //获取灯的上一次值
      var brightness= e.Old[GatewayNodeDeviceProperties.Light_Brightness].GetValue<int>();
      //获取灯的新变化值
      var brightness= e.New[GatewayNodeDeviceProperties.Light_Brightness].GetValue<int>();
     }

}


//在此,为了更确切的使用相关数据,本库提供了相关数据模型,可以通过 扩展方法YeelightProExtension.ParmasConverter<T> 直接转换成相应的模型
//模型 在 YeelightPro.Models 下

//举例:
//模型属性举例: 获取 pid为 198888的 ⾊温可调灯具 GatewayNodeDeviceType.Light_Temperature

 var device= _client.NodeDevices[198888];

 var light= device.Params.ParmasConverter<YeelightPro.Models.LightModel>();
//获取灯的亮度
 var brightness= light.Brightness;
//获取灯的色温
var brightness= light.ColorTemperature

//其他依次类推


Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.4.3 113 1/2/2026
2.4.2 139 1/2/2026
2.4.1 185 12/24/2025
2.4.0 214 7/8/2025