Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial KE100 support. #121

Merged
merged 17 commits into from
Nov 25, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions tapo/examples/tapo_ke100.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/// KE100 TRV Example
use std::env;

use log::{info, LevelFilter};
use tapo::ApiClient;

#[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 tapo_username = env::var("TAPO_USERNAME")?;
let tapo_password = env::var("TAPO_PASSWORD")?;
let ip_address = env::var("IP_ADDRESS")?;
// ID of the KE100 device. Can be obtained from executing `get_child_device_component_list()`` on the hub device.
let device_id = env::var("DEVICE_ID")?;
let target_temperature: u8 = env::var("TARGET_TEMPERATURE")?.parse()?;

let hub = ApiClient::new(tapo_username, tapo_password)?
.h100(ip_address)
.await?;

// Get a handler for the child device
let device = hub.ke100(device_id);

// Get the device info of the child device
let device_info = device.get_device_info().await?;
info!("Device info: {device_info:?}");

// Set temperature on target device
device.set_target_temperature(target_temperature).await?;

// Get the device info of the child device
let device_info = device.get_device_info().await?;
info!("Device info: {device_info:?}");

Ok(())
}
6 changes: 2 additions & 4 deletions tapo/src/api/api_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ impl ApiClient {
&self,
device_id: String,
child_request: TapoRequest,
) -> Result<R, Error>
) -> Result<Option<R>, Error>
where
R: fmt::Debug + DeserializeOwned + TapoResponseExt,
{
Expand All @@ -605,9 +605,7 @@ impl ApiClient {

validate_response(&response)?;

response
.result
.ok_or_else(|| Error::Tapo(TapoResponseError::EmptyResult))
Ok(response.result)
}
}

Expand Down
2 changes: 2 additions & 0 deletions tapo/src/api/child_devices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ mod t100_handler;
mod t110_handler;
mod t300_handler;
mod t31x_handler;
mod ke100_handler;

pub use s200b_handler::*;
pub use t100_handler::*;
pub use t110_handler::*;
pub use t300_handler::*;
pub use t31x_handler::*;
pub use ke100_handler::*;
149 changes: 149 additions & 0 deletions tapo/src/api/child_devices/ke100_handler.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
use crate::api::HubHandler;
use crate::error::{Error,TapoResponseError};
use crate::requests::{EmptyParams, TapoParams, TapoRequest, TrvSetDeviceInfoParams};
use crate::responses::KE100Result;

/// Handler for the [KE100] device.
pub struct KE100Handler<'h> {
hub_handler: &'h HubHandler,
device_id: String,
}

impl<'h> KE100Handler<'h> {
pub(crate) fn new(hub_handler: &'h HubHandler, device_id: String) -> Self {
Self {
hub_handler,
device_id,
}
}

/// Returns *device info* as [`KE100Result`].
/// It is not guaranteed to contain all the properties returned from the Tapo API.
pub async fn get_device_info(&self) -> Result<KE100Result, Error> {
let request = TapoRequest::GetDeviceInfo(TapoParams::new(EmptyParams));

self.hub_handler
.control_child(self.device_id.clone(), request)
.await?
.ok_or_else(|| Error::Tapo(TapoResponseError::EmptyResult))
}

/// Returns *device info* as [`serde_json::Value`].
/// It contains all the properties returned from the Tapo API.
pub async fn get_device_info_json(&self) -> Result<serde_json::Value, Error> {
let request = TapoRequest::GetDeviceInfo(TapoParams::new(EmptyParams));

self.hub_handler
.control_child(self.device_id.clone(), request)
.await?
.ok_or_else(|| Error::Tapo(TapoResponseError::EmptyResult))
}


/// Sets the *target temperature*.
///
/// # Arguments
///
/// * `target_temperature` - between min_control_temp and max_control_temp
pub async fn set_target_temperature(&self, target_temperature: u8) -> Result<(), Error> {

//let control_range = self.get_control_range().await?;
let device_info = self.get_device_info().await?;

if target_temperature < device_info.min_control_temperature || target_temperature > device_info.max_control_temperature {
return Err(Error::Validation {
field: "target_temperature".to_string(),
message: format!("Target temperature must be between {} (min_control_temp) and {} (max_control_temp)", device_info.min_control_temperature, device_info.max_control_temperature),
});
}

let json = serde_json::to_value(TrvSetDeviceInfoParams::new().target_temperature(target_temperature)?)?;
let request = TapoRequest::SetDeviceInfo(Box::new(TapoParams::new(json)));

self.hub_handler
.control_child::<serde_json::Value>(self.device_id.clone(), request)
.await?;

Ok(())
}

/// Sets the *minimal control temperature*.
///
/// # Arguments
///
/// * `min_control_temperature`
pub async fn set_min_control_temperature(&self, min_control_temperature: u8) -> Result<(), Error> {
let json = serde_json::to_value(TrvSetDeviceInfoParams::new().min_control_temperature(min_control_temperature)?)?;
let request = TapoRequest::SetDeviceInfo(Box::new(TapoParams::new(json)));

self.hub_handler
.control_child::<serde_json::Value>(self.device_id.clone(), request)
.await?;

Ok(())
}

/// Sets the *maximum control temperature*.
///
/// # Arguments
///
/// * `max_control_temperature`
pub async fn set_max_control_temperature(&self, max_control_temperature: u8) -> Result<(), Error> {
let json = serde_json::to_value(TrvSetDeviceInfoParams::new().max_control_temperature(max_control_temperature)?)?;
let request = TapoRequest::SetDeviceInfo(Box::new(TapoParams::new(json)));

self.hub_handler
.control_child::<serde_json::Value>(self.device_id.clone(), request)
.await?;

Ok(())
}

/// Sets frost protection on the device to *on* or *off*.
///
/// # Arguments
///
/// * `frost_protection_on` - true/false
pub async fn set_frost_protection(&self, frost_protection_on: bool) -> Result<(), Error> {
let json = serde_json::to_value(TrvSetDeviceInfoParams::new().frost_protection_on(frost_protection_on)?)?;
let request = TapoRequest::SetDeviceInfo(Box::new(TapoParams::new(json)));

self.hub_handler
.control_child::<serde_json::Value>(self.device_id.clone(), request)
.await?;

Ok(())
}

/// Sets child protection on the device to *on* or *off*.
///
/// # Arguments
///
/// * `child_protection_on` - true/false
pub async fn set_child_protection(&self, child_protection_on: bool) -> Result<(), Error> {
let json = serde_json::to_value(TrvSetDeviceInfoParams::new().child_protection(child_protection_on)?)?;
let request = TapoRequest::SetDeviceInfo(Box::new(TapoParams::new(json)));

self.hub_handler
.control_child::<serde_json::Value>(self.device_id.clone(), request)
.await?;

Ok(())
}
mihai-dinculescu marked this conversation as resolved.
Show resolved Hide resolved

/// Sets the *temperature offset*.
mihai-dinculescu marked this conversation as resolved.
Show resolved Hide resolved
///
/// # Arguments
///
/// * `temperature_offset` - between 5 and 30
pub async fn set_temperature_offset(&self, temperature_offset: i8) -> Result<(), Error> {
let json = serde_json::to_value(TrvSetDeviceInfoParams::new().temperature_offset(temperature_offset)?)?;
let request = TapoRequest::SetDeviceInfo(Box::new(TapoParams::new(json)));

self.hub_handler
.control_child::<serde_json::Value>(self.device_id.clone(), request)
.await?;

Ok(())
}
}
8 changes: 5 additions & 3 deletions tapo/src/api/child_devices/s200b_handler.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::api::HubHandler;
use crate::error::Error;
use crate::error::{Error,TapoResponseError};
use crate::requests::{EmptyParams, GetTriggerLogsParams, TapoParams, TapoRequest};
use crate::responses::S200BResult;
use crate::responses::{S200BLog, TriggerLogsResult};
Expand All @@ -25,7 +25,8 @@ impl<'h> S200BHandler<'h> {

self.hub_handler
.control_child(self.device_id.clone(), request)
.await
.await?
.ok_or_else(|| Error::Tapo(TapoResponseError::EmptyResult))
}

/// Returns a list of trigger logs.
Expand All @@ -46,6 +47,7 @@ impl<'h> S200BHandler<'h> {

self.hub_handler
.control_child(self.device_id.clone(), child_request)
.await
.await?
.ok_or_else(|| Error::Tapo(TapoResponseError::EmptyResult))
}
}
8 changes: 5 additions & 3 deletions tapo/src/api/child_devices/t100_handler.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::api::HubHandler;
use crate::error::Error;
use crate::error::{Error, TapoResponseError};
use crate::requests::{EmptyParams, GetTriggerLogsParams, TapoParams, TapoRequest};
use crate::responses::T100Result;
use crate::responses::{T100Log, TriggerLogsResult};
Expand All @@ -25,7 +25,8 @@ impl<'h> T100Handler<'h> {

self.hub_handler
.control_child(self.device_id.clone(), request)
.await
.await?
.ok_or_else(|| Error::Tapo(TapoResponseError::EmptyResult))
}

/// Returns a list of trigger logs.
Expand All @@ -46,6 +47,7 @@ impl<'h> T100Handler<'h> {

self.hub_handler
.control_child(self.device_id.clone(), child_request)
.await
.await?
.ok_or_else(|| Error::Tapo(TapoResponseError::EmptyResult))
}
}
8 changes: 5 additions & 3 deletions tapo/src/api/child_devices/t110_handler.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::api::HubHandler;
use crate::error::Error;
use crate::error::{Error, TapoResponseError};
use crate::requests::{EmptyParams, GetTriggerLogsParams, TapoParams, TapoRequest};
use crate::responses::T110Result;
use crate::responses::{T110Log, TriggerLogsResult};
Expand All @@ -25,7 +25,8 @@ impl<'h> T110Handler<'h> {

self.hub_handler
.control_child(self.device_id.clone(), request)
.await
.await?
.ok_or_else(|| Error::Tapo(TapoResponseError::EmptyResult))
}

/// Returns a list of trigger logs.
Expand All @@ -46,6 +47,7 @@ impl<'h> T110Handler<'h> {

self.hub_handler
.control_child(self.device_id.clone(), child_request)
.await
.await?
.ok_or_else(|| Error::Tapo(TapoResponseError::EmptyResult))
}
}
15 changes: 10 additions & 5 deletions tapo/src/api/child_devices/t300_handler.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::api::HubHandler;
use crate::error::Error;
use crate::error::{Error,TapoResponseError};
use crate::requests::{EmptyParams, GetTriggerLogsParams, TapoParams, TapoRequest};
use crate::responses::T300Result;
use crate::responses::{T300Log, TriggerLogsResult};
Expand All @@ -25,7 +25,8 @@ impl<'h> T300Handler<'h> {

self.hub_handler
.control_child(self.device_id.clone(), request)
.await
.await?
.ok_or_else(|| Error::Tapo(TapoResponseError::EmptyResult))
}

/// Returns a list of trigger logs.
Expand All @@ -44,8 +45,12 @@ impl<'h> T300Handler<'h> {
let child_params = GetTriggerLogsParams::new(page_size, start_id);
let child_request = TapoRequest::GetTriggerLogs(Box::new(TapoParams::new(child_params)));

self.hub_handler
.control_child(self.device_id.clone(), child_request)
.await
let result = self
.hub_handler
.control_child::<TriggerLogsResult<T300Log>>(self.device_id.clone(), child_request)
.await?
.ok_or_else(|| Error::Tapo(TapoResponseError::EmptyResult));

Ok(result?)
}
}
10 changes: 6 additions & 4 deletions tapo/src/api/child_devices/t31x_handler.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::api::HubHandler;
use crate::error::Error;
use crate::error::{Error, TapoResponseError};
use crate::requests::{EmptyParams, TapoParams, TapoRequest};
use crate::responses::{T31XResult, TemperatureHumidityRecords, TemperatureHumidityRecordsRaw};

Expand All @@ -24,7 +24,8 @@ impl<'h> T31XHandler<'h> {

self.hub_handler
.control_child(self.device_id.clone(), request)
.await
.await?
.ok_or_else(|| Error::Tapo(TapoResponseError::EmptyResult))
}

/// Returns *temperature and humidity records* from the last 24 hours at 15 minute intervals as [`TemperatureHumidityRecords`].
Expand All @@ -37,8 +38,9 @@ impl<'h> T31XHandler<'h> {
let result = self
.hub_handler
.control_child::<TemperatureHumidityRecordsRaw>(self.device_id.clone(), request)
.await?;
.await?
.ok_or_else(|| Error::Tapo(TapoResponseError::EmptyResult));

Ok(result.try_into()?)
Ok(result?.try_into()?)
}
}
Loading