/mi-home

С# API for Xiaomi Mi Home devices

Primary LanguageC#MIT LicenseMIT

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.

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 or you have to use multicast routers like udproxy or igmpproxy or vpn briding

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

Tracks:

Alarms

0 - Police car 1    
1 - Police car 2    
2 - Accident
3 - Countdown
4 - Ghost
5 - Sniper rifle
6 - Battle
7 - Air raid
8 - Bark

Doorbells

10 - Doorbell
11 - Knock at a door
12 - Amuse
13 - Alarm clock

Alarm clock

20 - MiMix
21 - Enthusiastic
22 - GuitarClassic
23 - IceWorldPiano
24 - LeisureTime
25 - ChildHood
26 - MorningStreamLiet
27 - MusicBox
28 - Orange
29 - Thinker

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); // Sample output --> Status: on, Inuse: 1, 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 !");
};

When I buy more devices I will update library