Git Product home page Git Product logo

mi-home's Introduction

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

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

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

When I buy more devices I will update library

mi-home's People

Contributors

niksan321 avatar sergey-brutsky avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.