Expand description

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?;

Modules

Structs

  • Protocol v3.4 uses different payloads for ControlNew commands, for example {“protocol”:5,“t”:1,“data”:{“dps”:{“20”:false}}}
  • The PayloadStruct is Serialized to json and sent to the device. The dps field contains the actual commands to set and are device specific.

Enums

  • The Payload enum represents a payload sent to, and recevied from the Tuya devices. It might be a struct (ser/de from json) or a plain string.

Traits

  • This trait is implemented to allow truncated logging of secret data.

Type Definitions