diff --git a/CHANGELOG.md b/CHANGELOG.md index 408fc5b..43d9faf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ file. This change log follows the conventions of ## [Unreleased] +### Added +- `examples/tapo_generic_device_toggle.rs` demonstrates how `device_info` can be used to assess the current status of a generic device and toggle it. + ### Changed - `on_time` is now optional for the `L510` and `L530` devices because the v2 hardware no longer returns it. diff --git a/examples/tapo_toggle_device.rs b/examples/tapo_toggle_device.rs new file mode 100644 index 0000000..d5226a6 --- /dev/null +++ b/examples/tapo_toggle_device.rs @@ -0,0 +1,39 @@ +/// Generic Device Example - Toggle +use std::env; + +use log::{info, LevelFilter}; +use tapo::{ApiClient, GenericDevice}; + +#[tokio::main] +async fn main() -> Result<(), Box> { + let log_level = env::var("RUST_LOG") + .unwrap_or_else(|_| "info".to_string()) + .parse() + .unwrap_or(LevelFilter::Info); + + pretty_env_logger::formatted_timed_builder() + .filter(Some("tapo"), log_level) + .init(); + + let ip_address = env::var("IP_ADDRESS")?; + let tapo_username = env::var("TAPO_USERNAME")?; + let tapo_password = env::var("TAPO_PASSWORD")?; + + let device = + ApiClient::::new(ip_address, tapo_username, tapo_password, true).await?; + + let device_info = device.get_device_info().await?; + + match device_info.device_on { + true => { + info!("Device is on. Turning it off..."); + device.off().await?; + } + false => { + info!("Device is off. Turning it on..."); + device.on().await?; + } + } + + Ok(()) +}