Code Monkey home page Code Monkey logo

tuya.net's Introduction



Build Issues Stars License

Tuya.Net is a Tuya API client wrapper for .NET. Communicate with your Tuya-powered devices from anywhere via the cloud and extend your smart home functionality by implementing your custom actions and events.

โœ… Prerequisites

To use this library you must have an active account on the Tuya developer platform and be subscribed to the IoT Core and Authorizaiton services. Once you have access to the developer platform, you can obtain an authorization key from there.

This library is currently aimed to end users who want to extend automation functionality, so you must also have an end user account.

Your end user account must be linked/authorized on your Tuya developer account as well, otherwise you will not have access to your resources. To link your Tuya user account: Go to your project -> Devices -> Link Tuya App Account.

For more information, visit the Tuya official documentation.

๐Ÿš€ Installation

NuGet Package: Tuya.Net Library will be available soon.

๐Ÿ“– Examples

Below are some examples on how to use this library and use cases where it may be useful.

Creating an instance of the client using the builder

var client = TuyaClient.GetBuilder()
    .UsingDataCenter(DataCenter.CentralEurope)
    .UsingClientId("<your_client_id_here>") // replace with your actual client id
    .UsingSecret("<your_client_secret_here>") // replace with your actual client secret
    .UsingLogger(NullLogger<ITuyaClient>.Instance)
    .Build();

Get device list for a given user ID

var devices = await client.DeviceManager.GetDevicesByUserAsync("<user_id_here>")); // replace with the actual end-user id that is linked to your Tuya developer account.
/// do something..

Toggle a lighting device depending on its status

// Retrieve the status of the lighting device to check whether the light is turned on or off.
var device = await client.DeviceManager.GetDeviceAsync("<lighting_device_id_here>"); // replace with your actual lighting device id
var status = device?.StatusList?.FirstOrDefault(ds => ds.Code == "switch_led");

if (status?.Value is not bool)
{
    throw new Exception("Cannot obtain the value of the device status, the switch_led status did not return bool as expected.");
}

// Get the device status (true if the light is turned on, false otherwise)
var isTurnedOn = (bool)status.Value!;

// Create the command to send an instruction to maniuplate the light status
var command = new Command()
{
    Code = "switch_led",
    Value = !isTurnedOn, // switch the value
};

// Send the command and obtain the result from the server.
var result = await client.DeviceManager.SendCommandAsync(device, command); // returns true if the command was executed successfully, false otherwise.

โค๏ธ Contribute

Feel free to contribute to this project by creating pull requests.

Building

To build this project, you require the following:

Using Visual Studio or Rider

Using the Command Line

Testing

Integration tests are available, but some configurations must be passed first before running them. Make sure you add a valid client id and secret to your secrets.json:

Using the CLI, from the repository root:

dotnet user-secrets set "TuyaClientId" "YOUR_TUYA_CLIENT_ID_HERE" --project Tuya.Net.Tests
dotnet user-secrets set "TuyaClientSecret" "YOUR_TUYA_CLIENT_Secret_HERE" --project Tuya.Net.Tests

Naming Conventions

Standard C# naming conventions apply, with the following exception:

  • Private fields are NOT prefixed with an underscore.

tuya.net's People

Contributors

thatbrainiac avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

tuya.net's Issues

Add Home Management support

Tuya API provides endpoints for controlling your home.

e.g. To create a new home for a given user ID:

POST /v1.0/home/create-home

Body:

{
  "home": {
    "name": "My Cozy Home",
    "geo_name": "Building 1",
    "lat": 30.16,
    "lon": 120.5
  },
  "rooms": [
    "Kitchen",
    "Bathroom"
  ],
  "uid": "<user_id_here>"
}

The library currently lacks any home management-related functionality, this can be added to the client by creating a new Home manager and adding/manipulating the respective home-related DTO's (e.g. Home, Room, etc)

Refactoring

Refactor the base project. Create different ways of instantiating the Tuya client.

Add message consumer

Add message consuming functionality to the client to actively listen to events.

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.