Skip to content
С# API for Xiaomi Mi Home devices
C#
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
MiHomeConsole
MiHomeLib Merge pull request #12 from iberisoft/master Aug 25, 2019
MiHomeTests
.gitignore
CNAME Update CNAME Aug 26, 2019
LICENSE.md Create LICENSE.md Sep 12, 2017
MiHome.sln Added tests for the project Oct 26, 2017
README.md
_config.yml Set theme jekyll-theme-cayman Sep 8, 2017
gateway.ico Add files via upload Sep 10, 2017

README.md

C# Library for using xiaomi smart gateway in your automation scenarious

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.

gateway temperature_sensor socket_plug motion_sensor door_window_sensor water_sensor smoke_sensor switch wired wall switch sensor_weather wireless dual wall switch

Warning : This is experimental version. It may be very unstable.

Installation

via nuget package manager

Install-Package MiHomeLib

Setup Gateway

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

Usage examples

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();
    }
}

Supported devices

1. Gateway

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();

2. Temperature and humidity sensor

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}");
};

3. Socket Plug

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();

4. Motion sensor

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 !");
};

5. Door/Window sensor

var windowSensor = miHome.GetDevicesByType<DoorWindowSensor>().First();

windowSensor.OnOpen += (_, __) =>
{
    Console.WriteLine($"{DateTime.Now}: Window opened !");
};

windowSensor.OnClose += (_, __) =>
{
    Console.WriteLine($"{DateTime.Now}: Window closed !");
};

5. Water leak sensor

water_sensor

var waterSensor = miHome.GetDevicesByType<WaterLeakSensor>().First();

waterSensor.OnLeak += (s, e) =>
{
    Console.WriteLine("Water leak detected !");
};

waterSensor.OnNoLeak += (s, e) =>
{
    Console.WriteLine("NO leak detected !");
};

6. Smoke sensor

smoke_sensor

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}");
};

7. Wireless dual wall switch

wireless dual wall switch

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

You can’t perform that action at this time.