#smart-home #tuya #smartlife

rust-async-tuyapi

This package is a rust port of the exellent NodeJS implementation by codetheweb for the Tuya API, adapted for async usage

3 releases (1 stable)

1.0.0 May 25, 2023
0.9.0 May 17, 2023
0.8.1 Jan 20, 2022

#1829 in Parser implementations

Custom license

56KB
1K SLoC

rust-async-tuyapi

Rust async implementation of the Tuya API used to communicate with Tuya/Smart Life devices.

This crate supports musl targets.

Original sync version here: https://github.com/EmilSodergren/rust-tuyapi

Acknowledgment

Prerequisites

You need to know the key and id of the Tuya device. According to me the easiest way to find these is explained at: Step by Step for adding Tuya-bulbs


lib.rs:

Rust Tuyapi

This library can be used to interact with Tuya/Smart Home devices. It utilizes the Tuya protocol version 3.1 and 3.3 to send and receive messages from the devices.

Example

This shows how to turn on a wall socket.

// The dps value is device specific, this socket turns on with key "1"
let mut dps = HashMap::new();
dps.insert("1".to_string(), json!(true));
let current_time = SystemTime::now()
    .duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs() as u32;

let dps = serde_json::to_value(&dps).unwrap();

// Create the payload to be sent, this will be serialized to the JSON format
let payload = Payload::Struct(PayloadStruct{
       dev_id: "123456789abcdef".to_string(),
       gw_id: Some("123456789abcdef".to_string()),
       uid: None,
       t: Some(current_time.to_string()),
       dp_id: None,
       dps: Some(dps),
       });
// Create a TuyaDevice, this is the type used to set/get status to/from a Tuya compatible
// device.
let mut tuya_device = TuyaDevice::new("3.3", "fedcba987654321", None,
    IpAddr::from_str("192.168.0.123").unwrap())?;

// Set the payload state on the Tuya device, an error here will contain
// the error message received from the device.
tuya_device.set(payload).await?;

Dependencies

~5–14MB
~181K SLoC