-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
tapo_generic_device_toggle
example
- Loading branch information
1 parent
6f4d957
commit 5b9f838
Showing
2 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |