This library provides simple and flexible C# API for Xiaomi Mi Home devices.
Currently supports only Xiaomi Smart Gateway 2 device and several sensors. Please see the pictures below.
Warning : This is experimental version. It may be very unstable.
via nuget package manager
Install-Package MiHomeLib
Before starting to use this library you should setup development mode on your gateway.
Here is instruction --> https://www.domoticz.com/wiki/Xiaomi_Gateway_(Aqara)
Warning: Mi Home Gateway uses udp multicast for messages handling.
So your app must be hosted in the same LAN as your gateway.
If it is not you have to use multicast routers like udproxy or igmpproxy or vpn briding
Warning : If your app is running on windows machine, make sure that you disabled virtual netweork adapters like VirtualBox, Hyper-V, Npcap, pcap etc.
Because these adapters may prevent proper work of multicast traffic between your machine and gateway
Get all devices in the network
public static void Main(string[] args)
{
// pwd of your gateway (optional, needed only to send commands to your devices)
// and sid of your gateway (optional, use only when you have 2 gateways in your LAN)
using (var miHome = new MiHome("7c4mx86hn658f0f3"))
{
Task.Delay(5000).Wait();
foreach (var miHomeDevice in miHome.GetDevices())
{
Console.WriteLine(miHomeDevice); // all discovered devices
}
Console.ReadLine();
}
}
Get devices by name if you already know sid
public static void Main(string[] args)
{
var map = new Dictionary<string, string>
{
{ "158d0001826509", "T&H sensor living room"}
};
using (var miHome = new MiHome(map))
{
Task.Delay(5000).Wait();
var thSensor = miHome.GetDeviceByName<ThSensor>("T&H sensor living room");
Console.WriteLine(thSensor);
Console.ReadLine();
}
}
var gateway = miHome.GetGateway();
Console.WriteLine(gateway); // Sample output --> Rgb: 0, Illumination: 997, ProtoVersion: 1.0.9
gateway?.EnableLight(); // "white" light by default
Thread.Sleep(5000);
gateway?.DisableLight();
gateway?.StartPlayMusic(1); // Track number 1 (tracks range is 0-8, 10-13, 20-29)
Thread.Sleep(5000);
gateway?.StopPlayMusic();
var thSensor = miHome.GetDeviceBySid<ThSensor>("158d000182dfbc"); // get specific device
Console.WriteLine(thSensor); // Sample output --> Temperature: 22,19°C, Humidity: 74,66%, Voltage: 3,035V
th.OnTemperatureChange += (_, e) =>
{
Console.WriteLine($"New temperature: {e.Temperature}");
};
th.OnHumidityChange += (_, e) =>
{
Console.WriteLine($"New humidity: {e.Humidity}");
};
var socketPlug = miHome.GetDeviceBySid<SocketPlug>("158d00015dc6cc"); // get specific socket plug
Console.WriteLine(socketPlug); // Status: on, Load Power: 3,26V, Power Consumed: 1103W, Voltage: 3,6V
socketPlug.TurnOff();
Thread.Sleep(5000);
socketPlug.TurnOn();
var motionSensor = miHome.GetDevicesByType<MotionSensor>().First();
motionSensor.OnMotion += (_, __) =>
{
Console.WriteLine($"{DateTime.Now}: Motion detected !");
};
motionSensor.OnNoMotion += (_, e) =>
{
Console.WriteLine($"{DateTime.Now}: No motion for {e.Seconds}s !");
};
var windowSensor = miHome.GetDevicesByType<DoorWindowSensor>().First();
windowSensor.OnOpen += (_, __) =>
{
Console.WriteLine($"{DateTime.Now}: Window opened !");
};
windowSensor.OnClose += (_, __) =>
{
Console.WriteLine($"{DateTime.Now}: Window closed !");
};
var waterSensor = miHome.GetDevicesByType<WaterLeakSensor>().First();
waterSensor.OnLeak += (s, e) =>
{
Console.WriteLine("Water leak detected !");
};
waterSensor.OnNoLeak += (s, e) =>
{
Console.WriteLine("NO leak detected !");
};
var smokeSensor = miHome.GetDevicesByType<SmokeSensor>().First();
smokeSensor.OnAlarm += (_, __) =>
{
Console.WriteLine("Smoke detected !");
};
smokeSensor.OnAlarmStopped += (_, __) =>
{
Console.WriteLine("Smoke alarm stopped");
};
smokeSensor.OnDensityChanged += (_, e) =>
{
Console.WriteLine($"Density changed {e.Density}");
};
var switch = miHome.GetDevicesByType<WirelessDualWallSwitch>().First();
switch.OnLeftClick += (_, __) =>
{
Console.WriteLine("Left button clicked !");
};
switch.OnRightDoubleClick += (_, __) =>
{
Console.WriteLine("Right button double clicked !");
};
switch.OnLeftLongClick += (_, __) =>
{
Console.WriteLine("Left button long clicked !");
};
When I buy more devices I will update library