#smart-home #tuya #smartlife

rust-tuyapi

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

19 releases

0.9.0 Jul 18, 2023
0.8.3 Mar 12, 2023
0.8.2 Aug 26, 2022
0.8.1 Jan 16, 2022
0.4.1 Nov 9, 2020

#1025 in Parser implementations

Download history 1/week @ 2024-02-20 22/week @ 2024-02-27 44/week @ 2024-03-05 6/week @ 2024-03-12

73 downloads per month
Used in rust-tuya-mqtt

Custom license

45KB
970 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: 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;

// 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
~184K SLoC