TP-Link Smart Device Client Library
A pure Rust library for communicating with TP-Link smart devices including smart plugs, smart lights, smart dimmers, and power strips.
Features
- 🔍 Device Discovery: Automatic network discovery via UDP broadcast
- 🔧 Device Objects: Type-safe device objects with embedded client functionality
- 🏠 Smart Home Control: Support for plugs, lights, dimmers, and power strips
- 🎨 Color Control: Full HSL/RGB/CSS color support for smart lights
- ⚡ Async/Await: Built on Tokio for high-performance async operations
- 🛡️ Type Safety: Compile-time prevention of calling wrong methods on wrong device types
- 📦 Zero Dependencies: Pure Rust implementation with minimal external dependencies
🚀 Quick Start
Add this to your Cargo.toml:
[]
= "0.1.0"
= { = "1", = ["full"] }
For local development or monorepo usage:
[]
= { = "path/to/tplink" }
= { = "1", = ["full"] }
📖 Basic Usage
Device Discovery & Control
The new API provides device objects with embedded clients for type-safe operations:
use ;
async
Using the Device Registry
For applications that need persistent device management:
use ;
async
🎯 Device-Specific Operations
Smart Plugs
use Device;
// Assuming you have a smart plug device
if let SmartPlug = device
Smart Lights
use Device;
if let SmartLight = device
Smart Dimmers
use Device;
if let SmartDimmer = device
Power Strips
use Device;
if let PowerStrip = device
🔄 Migrating from Function-Based API
Old way (function-based):
// ❌ Old API - not type safe, easy to make mistakes
use ;
turn_plug_on.await?;
set_light_brightness.await?; // Wrong! This is a plug, not a light
New way (device-based):
// ✅ New API - type safe, self-documenting
use ;
let devices = discover.await?;
for device in devices
🔧 Advanced Usage
Custom Client Configuration
use ;
use Arc;
let client = new;
let registry = with_client;
// Use the same client across multiple registries or operations
let devices = discover_devices_with_client.await?;
Error Handling
use ;
match plug.turn_on.await
📋 Supported Devices
| Device Type | Models | Features |
|---|---|---|
| Smart Plugs | HS100, HS110, KP100+ | On/Off, Scheduling, Energy Monitoring* |
| Smart Lights | LB100, LB110, LB120, LB130+ | On/Off, Dimming*, Color* |
| Smart Dimmers | ES20M(US), KS230(US) | Dimming, Inactivity Timer |
| Power Strips | HS300, KP303+ | Individual Socket Control, Energy Monitoring |
*Feature availability depends on specific model
🛠️ Protocol Details
TP-Link smart devices use:
- Discovery: UDP broadcast on port 9999
- Communication: TCP connections on port 9999
- Encryption: XOR cipher with rotating key (0xAB initial)
- Format: JSON messages with 4-byte length prefix
🔍 Troubleshooting
Device not found:
- Ensure device is on the same network
- Check firewall settings for UDP port 9999
- Verify device is powered on and connected to WiFi
Connection timeouts:
- Device may be temporarily offline
- Check IP address is correct
- Network congestion can cause delays
Color commands not working:
- Verify the device supports color (
light.is_color()) - Use valid CSS color formats:
#RRGGBB,rgb(),hsl(), named colors
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
📄 License
This project is licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT License (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.