Skip to content

Commit

Permalink
Add tapo_generic_device_toggle example
Browse files Browse the repository at this point in the history
  • Loading branch information
mihai-dinculescu committed Feb 19, 2023
1 parent 6f4d957 commit 5b9f838
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
39 changes: 39 additions & 0 deletions examples/tapo_toggle_device.rs
Original file line number Diff line number Diff line change
@@ -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<dyn std::error::Error>> {
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::<GenericDevice>::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(())
}

0 comments on commit 5b9f838

Please sign in to comment.