sengled

Crates.iosengled
lib.rssengled
version0.1.1
sourcesrc
created_at2024-03-12 04:56:53.612413
updated_at2024-03-12 04:58:34.69346
descriptionTiny API wrapper over Sengled smart devices
homepage
repository
max_upload_size
id1170116
size65,349
Zander (voximity)

documentation

README

sengled-rs

Tiny API wrapper over Sengled smart home devices. Loose port of ha-sengledapi to Rust.

Installation

Use cargo add sengled.

Usage

See some examples.

#[tokio::main]
async fn main() {
    // log in
    let mut client =
        sengled::Client::new("username", "password").with_preferred_qos(sengled::QoS::AtMostOnce);

    client.login_and_start().await.unwrap();

    // get wifi devices
    let devices = client.wifi_devices().await.unwrap();

    // turn all of them on by setting "switch" to "1" and print their names
    for mut device in devices {
        device.set_attribute(&client, "switch", "1").await.unwrap();

        println!("{}", device.get_attribute_or("name", "unknown"));

        // also, maybe explore other attributes to see what else you can do...
        // println!("{:#?}", &device.attributes);
    }

    // close the client, ensuring MQTT messages are actually sent
    client.close().await.unwrap();
}

Optimization

You can use .with_preferred_qos on sengled::Client to control the MQTT QoS by which MQTT messages are delivered.

You can use .with_skip_server_check() on sengled::Client before starting the client to skip the server check. By default, the wrapper will contact an API endpoint and gather some information about the target MQTT broker server. In my testing, this has not changed, so when using .with_skip_server_check(), the wrapper will use constant defaults and save an API call.

You can use .start(session) instead of .login_and_start() if you already have the jsessionId for your Sengled account. .login_and_start() returns this jsessionId so you can cache this value and use .start(session) later to save an API call.

Commit count: 0

cargo fmt