Skip to main content

Crate denki

Crate denki 

Source
Expand description

§Rust library usage

Requires Rust 1.99 or newer. Add these dependencies to your project:

[dependencies]
denki = "0.1.1"
anyhow = "1"
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }

§Kasa device information

use denki::ops;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let info = ops::sysinfo("192.0.2.42").await?;
    println!("{info}");
    Ok(())
}

§Tapo device information

Create a KLAP session with your Tapo credentials and reuse it for subsequent calls:

use denki::{klap, ops};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let user = std::env::var("TAPO_USER")?;
    let password = std::env::var("TAPO_PASS")?;
    let mut session = klap::handshake("192.0.2.50", &user, &password).await?;
    let info = ops::tapo_device_info(&mut session).await?;
    println!("{info}");
    Ok(())
}

Replace these documentation-only addresses with your device’s local IP before running. The examples read information; they do not change power state. CI compiles these exact examples as doctests without contacting devices.

The low-level ops functions take addresses or sessions directly. They do not resolve CLI aliases or enforce the saved-identity checks used by CLI commands.

§P125 through TPAP/KLAP auto negotiation

The adapter reuses credentials saved by denki login or TAPO_USER/TAPO_PASS. It checks any identity already bound to the address in the alias registry.

let info = denki::tapo_client::info("192.0.2.50").await?;
println!("{}: {}", info.model, info.device_on);

set_power(ip, Some(true)) turns a supported P125 on, Some(false) turns it off, and None toggles its observed state. Writes check capabilities and verify the result with a readback; they are not retried. The client negotiates automatically, and currently does not expose the selected wire protocol.

Modules§

admin
app
CLI application wiring and dispatch.
bulb
Sysinfo for TP-Link KL135/LB130 bulbs and KL430 light strips (mic_type: IOT.SMARTBULB).
cipher
XOR autokey cipher used by TP-Link’s legacy Kasa protocol (port 9999).
cli
commands
creds
Tapo credentials — env vars (TAPO_USER/TAPO_PASS) take precedence over denki/credentials.json in the platform configuration directory (mode 0600 on Unix).
devices
Device capability registry and capability checks.
dimmer
Sysinfo for TP-Link HS220 dimmer switches.
display
effects
energy
Normalized device-reported measurements shared by text, JSON, and watch.
error
Stable error categories shared by the library and CLI.
fmt
hosts
Device alias registry in the platform configuration directory (denki/hosts.json).
klap
KLAP protocol — AES-128-CBC over plain HTTP on port 80 (Tapo P125, etc.)
ops
Device operations — one function per API call, grouped by device type.
output
CLI result collection. Task-local state keeps concurrent library callers isolated.
plug
Sysinfo for TP-Link smart plugs (KP115, HS110, HS105, etc.)
resolve
strip
Sysinfo for TP-Link power strips (HS300, KP303).
tapo
Tapo device info (P125, etc.) via KLAP get_device_info. Response shape: {“error_code”: 0, “result”: {…}} nickname and ssid are base64-encoded in the API response.
tapo_client
Upstream Tapo plug adapter: negotiates TPAP/KLAP and reuses Denki credentials. Existing explicit KLAP aliases continue to use crate::klap.
transport
Network transport for legacy Kasa devices (port 9999). Tapo/KLAP lives in klap.rs.