Skip to content

Xiaomi Door Window sensor 2 (MCCGQ02HL)

Sergey edited this page Jul 1, 2024 · 2 revisions

For Xiaomi Gateway 3

using var gw3 = new XiaomiGateway3("<gateway ip>", "<gateway token>");
{
    gw3.OnDeviceDiscovered += x =>
    {
        if(x is XiaomiDoorWindowSensor2 device && device.Did == "<specific did here>")
        {
            Console.WriteLine(device.ToString());
            // Contact can be Opened/Closed/Not closed after specific timeout (default 1 min) 
            device.OnContactChange += (oldContactState) => Console.WriteLine($"Door/Window contact is {device.Contact}");
            device.OnBatteryPercentChange += (oldBatteryValue) => Console.WriteLine($"Battery is {device.BatteryPercent}");
            device.OnLightChange += (oldLightValue) => Console.WriteLine($"Light is {device.Light}");
        }
    };

    gw3.DiscoverDevices();
}