#smart-home #tuya #smartlife

rust-tuyapi

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

20 releases

0.9.1 Oct 18, 2024
0.9.0 Jul 18, 2023
0.8.4 Jul 5, 2023
0.8.3 Mar 12, 2023
0.4.1 Nov 9, 2020

#505 in Hardware support

Download history 35/week @ 2024-07-29 9/week @ 2024-08-26 12/week @ 2024-09-23 9/week @ 2024-09-30 7/week @ 2024-10-07 117/week @ 2024-10-14 21/week @ 2024-10-21

156 downloads per month
Used in rust-tuya-mqtt

Custom license

42KB
923 lines

rust-tuyapi

Build Status

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

Acknowledgment

@codetheweb for reverse enginering the protocol.

Prerequisit

You need to know the key and id of the Tuya device. According to me the easiest way to find these is explained at: Tuya Code Extract


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;

// 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),
       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 tuya_device = TuyaDevice::create("ver3.3", Some("fedcba987654321"),
    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, 0)?;

Dependencies

~4–8.5MB
~192K SLoC