Skip to main content

tuya_rs/
lib.rs

1#![warn(missing_docs)]
2#![deny(unsafe_code)]
3
4//! # tuya-rs
5//!
6//! Tuya v3.3 local protocol implementation in Rust.
7//!
8//! Provides TCP connection, packet encoding/decoding, and AES-ECB encryption
9//! for communicating with Tuya-based IoT devices on the local network.
10//!
11//! ## Quick start
12//!
13//! ```no_run
14//! use tuya_rs::connection::{DeviceConfig, TuyaConnection, TuyaCommand, Transport, build_dps_json, now};
15//! use serde_json::json;
16//!
17//! // Connect to a device
18//! let config = DeviceConfig {
19//!     dev_id: "device_id_here".into(),
20//!     address: "192.168.1.100".into(),
21//!     local_key: "0123456789abcdef".into(),
22//!     ..Default::default()
23//! };
24//! let mut conn = TuyaConnection::connect(&config).unwrap();
25//!
26//! // Query device state — triggers STATUS pushes
27//! conn.send(TuyaCommand::DpQuery, b"{}".to_vec()).unwrap();
28//!
29//! // Set a DPS value (e.g. turn on = DP 1)
30//! let payload = build_dps_json(conn.dev_id(), now(), &[("1", json!(true))]);
31//! conn.send(TuyaCommand::Control, payload.into_bytes()).unwrap();
32//! ```
33//!
34//! ## Discovery
35//!
36//! Find devices on the local network via UDP broadcast:
37//!
38//! ```no_run
39//! use tuya_rs::discovery::discover;
40//! use std::time::Duration;
41//!
42//! let devices = discover(Duration::from_secs(5)).unwrap();
43//! for dev in &devices {
44//!     println!("{} @ {} (v{})", dev.device_id, dev.ip, dev.version);
45//! }
46//! ```
47//!
48//! Note: discovery finds `device_id` and `ip` but **not** the `local_key` —
49//! that requires the Tuya cloud API or tools like
50//! [tinytuya](https://github.com/jasonacox/tinytuya).
51//!
52//! ## Features
53//!
54//! - **Default (local only)**: UDP device discovery, TCP packet codec, AES-128-ECB encryption, CRC32 validation
55//! - **`cloud`**: OEM Mobile API client (login, device discovery, AWS STS map storage)
56
57/// Tuya OEM Mobile API client (cloud feature).
58#[cfg(feature = "cloud")]
59pub mod api;
60/// TCP connection and packet codec for Tuya v3.3 protocol.
61pub mod connection;
62/// AES-128-ECB encryption and RSA utilities.
63pub mod crypto;
64/// UDP device discovery on the local network.
65pub mod discovery;
66/// HMAC-SHA256 request signing for Tuya API (cloud feature).
67#[cfg(feature = "cloud")]
68pub mod signing;