Skip to content

Commit

Permalink
Add get_current_power to power monitoring plugs (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal-Szczepaniak authored Aug 21, 2023
1 parent 8ff579d commit a7c45fe
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Unofficial Tapo API Client. Works with TP-Link Tapo smart devices. Tested with l
| get_device_usage | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
| get_energy_usage | | | | | | ✓ |
| get_energy_data | | | | | | ✓ |
| get_current_power | | | | | | ✓ |
| set_brightness | | ✓ | ✓ | ✓ | | |
| set_color | | | ✓ | ✓ | | |
| set_hue_saturation | | | ✓ | ✓ | | |
Expand Down
3 changes: 3 additions & 0 deletions examples/tapo_p110.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let device_usage = device.get_device_usage().await?;
info!("Device usage: {device_usage:?}");

let current_power = device.get_current_power().await?;
info!("Current power: {current_power:?}");

let energy_usage = device.get_energy_usage().await?;
info!("Energy usage: {energy_usage:?}");

Expand Down
15 changes: 12 additions & 3 deletions src/api/api_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ use crate::requests::{
TapoRequest,
};
use crate::responses::{
validate_response, ControlChildResult, DecodableResultExt, DeviceUsageResult, EnergyDataResult,
EnergyUsageResult, HandshakeResult, TapoMultipleResponse, TapoResponse, TapoResponseExt,
TapoResult, TokenResult,
validate_response, ControlChildResult, CurrentPowerResult, DecodableResultExt,
DeviceUsageResult, EnergyDataResult, EnergyUsageResult, HandshakeResult, TapoMultipleResponse,
TapoResponse, TapoResponseExt, TapoResult, TokenResult,
};

const TERMINAL_UUID: &str = "00-00-00-00-00-00";
Expand Down Expand Up @@ -534,6 +534,15 @@ impl ApiClient {
.ok_or_else(|| Error::Tapo(TapoResponseError::EmptyResult))
}

pub(crate) async fn get_current_power(&self) -> Result<CurrentPowerResult, Error> {
debug!("Get Current power...");
let request = TapoRequest::GetCurrentPower(TapoParams::new(EmptyParams));

self.execute_secure_passthrough_request::<CurrentPowerResult>(request, true)
.await?
.ok_or_else(|| Error::Tapo(TapoResponseError::EmptyResult))
}

pub(crate) async fn get_child_device_list<R>(&self) -> Result<R, Error>
where
R: fmt::Debug + DeserializeOwned + TapoResponseExt + DecodableResultExt,
Expand Down
8 changes: 7 additions & 1 deletion src/api/energy_monitoring_plug_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use crate::api::{ApiClient, ApiClientExt};
use crate::error::Error;
use crate::requests::{EnergyDataInterval, GenericSetDeviceInfoParams};
use crate::responses::{
DeviceUsageResult, EnergyDataResult, EnergyUsageResult, PlugDeviceInfoResult,
CurrentPowerResult, DeviceUsageResult, EnergyDataResult, EnergyUsageResult,
PlugDeviceInfoResult,
};

/// Handler for the [P110](https://www.tapo.com/en/search/?q=P110) & [P115](https://www.tapo.com/en/search/?q=P115) devices.
Expand Down Expand Up @@ -65,4 +66,9 @@ impl EnergyMonitoringPlugHandler {
) -> Result<EnergyDataResult, Error> {
self.client.get_energy_data(interval).await
}

/// Returns *energy usage* as [`CurrentPowerResult`].
pub async fn get_current_power(&self) -> Result<CurrentPowerResult, Error> {
self.client.get_current_power().await
}
}
1 change: 1 addition & 0 deletions src/requests/tapo_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub(crate) enum TapoRequest {
GetDeviceUsage(TapoParams<EmptyParams>),
GetEnergyUsage(TapoParams<EmptyParams>),
GetEnergyData(TapoParams<GetEnergyDataParams>),
GetCurrentPower(TapoParams<EmptyParams>),
GetChildDeviceList(TapoParams<EmptyParams>),
GetChildDeviceComponentList(TapoParams<EmptyParams>),
ControlChild(Box<TapoParams<ControlChildParams>>),
Expand Down
2 changes: 2 additions & 0 deletions src/responses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

mod child_device_list_result;
mod control_child_result;
mod current_power_result;
mod decodable_result_ext;
mod device_info_result;
mod device_usage_result;
Expand All @@ -14,6 +15,7 @@ mod token_result;
mod trigger_logs_result;

pub use child_device_list_result::*;
pub use current_power_result::*;
pub use device_info_result::*;
pub use device_usage_result::*;
pub use energy_data_result::*;
Expand Down
11 changes: 11 additions & 0 deletions src/responses/current_power_result.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use serde::{Deserialize, Serialize};

use crate::responses::TapoResponseExt;

/// Contains local time, current power and the energy usage and runtime for today and for the current month.
#[derive(Debug, Serialize, Deserialize)]
pub struct CurrentPowerResult {
/// Current power in watts (W)
pub current_power: u64,
}
impl TapoResponseExt for CurrentPowerResult {}

0 comments on commit a7c45fe

Please sign in to comment.