m4rcus.TuyaCore

.NET Standard 2.0 API for programmatically controlling a Tuya-compatible Smart Plug Device


Keywords
tuya, jinvoo, smart, plug, device, outlet
License
MIT
Install
Install-Package m4rcus.TuyaCore -Version 1.0.1

Documentation

Tuya Smart Plug API (.NET Standard 2.0)

This .NET Standard 2.0 API allows programmatic control over the basic functions of Tuya Smart Plugs, including getting and setting Power status.

Requirements

  • .NET Core 2.0

Known-working devices:

Many Smart Plug devices compatible with the Tuya Smart Life and Jinvoo Smart app also appear to be compatible with the Tuya protocol.

Installation

Install-Package m4rcus.TuyaCore

Retrieving Tuya Plug ID and LocalKey values:

  1. Install the Tuya Smart Life App onto your device
  2. Install ADB on your computer: https://www.xda-developers.com/install-adb-windows-macos-linux/
  3. Ensure your device has USB debugging enabled
  4. Plug device into your computer
  5. Run filtered ADB logcat via shell:
    > adb shell
    > logcat | grep BindDeviceSuccessPresenter
  6. Add the smart plug in the Tuya App, monitor the adb logcat output for the following
  7. Find the "localKey" and "devId" keys listed in the output, ex:
    12-06 23:58:53.544 17782 17782 D Tuya    : BindDeviceSuccessPresenter updateList devIds:[{"ability":0,"attribute":0,"bv":"5.06","cloudOnline":true,"devId":"0120015260091453a970","encrypt":false,"gwType":"s","i18nTime":0,"iconUrl":"https://images.tuyaus.com/smart/icon/1496461963_0.jpeg","isLocalOnline":false,"isOnline":true,"lat":"","localKey":"5f5f784cd82d449b","lon":"","name":"WiFi Plug ","pv":"2.1","rnFind":false,"runtimeEnv":"prod","supportGroup":false,"switchDp":0,"time":1512626328,"uuid":"0120015260091453a970","verSw":"1.0.4"}]
    
    In this example, the LocalKey is 5f5f784cd82d449b and the Id is 0120015260091453a970
  8. Find the IP address of your device via your router's DHCP leases. The IP address reported by the app is not the local IP address.

Usage

Console utility

> dotnet m4rcus.TuyaCore.Console.dll -i <ip> -k <localKey> -d <deviceId> [status|power-on|power-off]

Querying status, toggling power (async)

using m4rcus.TuyaCore;
var device = new TuyaPlug()
{
    IP = "192.168.0.101",
    LocalKey = "5f5f784cd82d449b",
    Id = "0120015260091453a970"
};
var status = await device.GetStatus();
await device.SetStatus(!status.Powered); // toggle power

Credits

Protocol details from @codetheweb and @clach04: