Skip to content

Commit

Permalink
Improve the ergonomics of the send() method
Browse files Browse the repository at this point in the history
  • Loading branch information
mihai-dinculescu committed Jan 26, 2024
1 parent 4923824 commit 482f8db
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 83 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ file. This change log follows the conventions of

## [Rust Unreleased][Unreleased]

### Changed

- The `send()` method of the `.set()` API now takes a reference to the device handler in order to allow for better ergonomics.

## [Python Unreleased][Unreleased]

### Added
Expand Down
2 changes: 1 addition & 1 deletion tapo/examples/tapo_l530.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.set()
.brightness(50)
.color(Color::HotPink)
.send()
.send(&device)
.await?;

info!("Waiting 2 seconds...");
Expand Down
2 changes: 1 addition & 1 deletion tapo/examples/tapo_l930.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.set()
.brightness(50)
.color(Color::HotPink)
.send()
.send(&device)
.await?;

info!("Waiting 2 seconds...");
Expand Down
2 changes: 2 additions & 0 deletions tapo/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod child_devices;
mod color_light_handler;
mod color_light_strip_handler;
mod generic_device_handler;
mod handler_ext;
mod hub_handler;
mod light_handler;
mod plug_energy_monitoring_handler;
Expand All @@ -14,6 +15,7 @@ pub use child_devices::*;
pub use color_light_handler::*;
pub use color_light_strip_handler::*;
pub use generic_device_handler::*;
pub use handler_ext::*;
pub use hub_handler::*;
pub use light_handler::*;
pub use plug_energy_monitoring_handler::*;
Expand Down
4 changes: 3 additions & 1 deletion tapo/src/api/api_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ use crate::responses::{

const TERMINAL_UUID: &str = "00-00-00-00-00-00";

/// Implemented by all ApiClient implementations.
#[async_trait]
pub(crate) trait ApiClientExt: std::fmt::Debug + Send + Sync {
pub trait ApiClientExt: std::fmt::Debug + Send + Sync {
/// Sets the device info.
async fn set_device_info(&self, device_info_params: serde_json::Value) -> Result<(), Error>;
}

Expand Down
39 changes: 20 additions & 19 deletions tapo/src/api/color_light_handler.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::api::ApiClient;
use crate::error::Error;
use crate::requests::{Color, ColorLightSetDeviceInfoParams};
use crate::responses::{DeviceInfoColorLightResult, DeviceUsageEnergyMonitoringResult};

use super::{ApiClient, ApiClientExt, HandlerExt};

/// Handler for the [L530](https://www.tapo.com/en/search/?q=L530), [L630](https://www.tapo.com/en/search/?q=L630) and [L900](https://www.tapo.com/en/search/?q=L900) devices.
pub struct ColorLightHandler {
client: ApiClient,
Expand All @@ -21,18 +22,12 @@ impl ColorLightHandler {

/// Turns *on* the device.
pub async fn on(&self) -> Result<(), Error> {
ColorLightSetDeviceInfoParams::new(&self.client)
.on()
.send()
.await
ColorLightSetDeviceInfoParams::new().on().send(self).await
}

/// Turns *off* the device.
pub async fn off(&self) -> Result<(), Error> {
ColorLightSetDeviceInfoParams::new(&self.client)
.off()
.send()
.await
ColorLightSetDeviceInfoParams::new().off().send(self).await
}

/// *Hardware resets* the device.
Expand Down Expand Up @@ -82,13 +77,13 @@ impl ColorLightHandler {
/// .set()
/// .brightness(50)
/// .color(Color::HotPink)
/// .send()
/// .send(&device)
/// .await?;
/// # Ok(())
/// # }
/// ```
pub fn set(&self) -> ColorLightSetDeviceInfoParams {
ColorLightSetDeviceInfoParams::new(&self.client)
ColorLightSetDeviceInfoParams::new()
}

/// Sets the *brightness* and turns *on* the device.
Expand All @@ -97,9 +92,9 @@ impl ColorLightHandler {
///
/// * `brightness` - between 1 and 100
pub async fn set_brightness(&self, brightness: u8) -> Result<(), Error> {
ColorLightSetDeviceInfoParams::new(&self.client)
ColorLightSetDeviceInfoParams::new()
.brightness(brightness)
.send()
.send(self)
.await
}

Expand All @@ -109,9 +104,9 @@ impl ColorLightHandler {
///
/// * `color` - one of [crate::requests::Color] as defined in the Google Home app
pub async fn set_color(&self, color: Color) -> Result<(), Error> {
ColorLightSetDeviceInfoParams::new(&self.client)
ColorLightSetDeviceInfoParams::new()
.color(color)
.send()
.send(self)
.await
}

Expand All @@ -122,9 +117,9 @@ impl ColorLightHandler {
/// * `hue` - between 1 and 360
/// * `saturation` - between 1 and 100
pub async fn set_hue_saturation(&self, hue: u16, saturation: u8) -> Result<(), Error> {
ColorLightSetDeviceInfoParams::new(&self.client)
ColorLightSetDeviceInfoParams::new()
.hue_saturation(hue, saturation)
.send()
.send(self)
.await
}

Expand All @@ -134,9 +129,15 @@ impl ColorLightHandler {
///
/// * `color_temperature` - between 2500 and 6500
pub async fn set_color_temperature(&self, color_temperature: u16) -> Result<(), Error> {
ColorLightSetDeviceInfoParams::new(&self.client)
ColorLightSetDeviceInfoParams::new()
.color_temperature(color_temperature)
.send()
.send(self)
.await
}
}

impl HandlerExt for ColorLightHandler {
fn get_client(&self) -> &impl ApiClientExt {
&self.client
}
}
39 changes: 20 additions & 19 deletions tapo/src/api/color_light_strip_handler.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::api::ApiClient;
use crate::error::Error;
use crate::requests::{Color, ColorLightSetDeviceInfoParams, LightingEffect};
use crate::responses::{DeviceInfoColorLightStripResult, DeviceUsageEnergyMonitoringResult};

use super::{ApiClient, ApiClientExt, HandlerExt};

/// Handler for the [L920](https://www.tapo.com/en/search/?q=L920) and [L930](https://www.tapo.com/en/search/?q=L930) devices.
pub struct ColorLightStripHandler {
client: ApiClient,
Expand All @@ -21,18 +22,12 @@ impl ColorLightStripHandler {

/// Turns *on* the device.
pub async fn on(&self) -> Result<(), Error> {
ColorLightSetDeviceInfoParams::new(&self.client)
.on()
.send()
.await
ColorLightSetDeviceInfoParams::new().on().send(self).await
}

/// Turns *off* the device.
pub async fn off(&self) -> Result<(), Error> {
ColorLightSetDeviceInfoParams::new(&self.client)
.off()
.send()
.await
ColorLightSetDeviceInfoParams::new().off().send(self).await
}

/// *Hardware resets* the device.
Expand Down Expand Up @@ -83,13 +78,13 @@ impl ColorLightStripHandler {
/// .set()
/// .brightness(50)
/// .color(Color::HotPink)
/// .send()
/// .send(&device)
/// .await?;
/// # Ok(())
/// # }
/// ```
pub fn set(&self) -> ColorLightSetDeviceInfoParams {
ColorLightSetDeviceInfoParams::new(&self.client)
ColorLightSetDeviceInfoParams::new()
}

/// Sets the *brightness* and turns *on* the device.
Expand All @@ -99,9 +94,9 @@ impl ColorLightStripHandler {
///
/// * `brightness` - between 1 and 100
pub async fn set_brightness(&self, brightness: u8) -> Result<(), Error> {
ColorLightSetDeviceInfoParams::new(&self.client)
ColorLightSetDeviceInfoParams::new()
.brightness(brightness)
.send()
.send(self)
.await
}

Expand All @@ -112,9 +107,9 @@ impl ColorLightStripHandler {
///
/// * `color` - one of [crate::requests::Color] as defined in the Google Home app
pub async fn set_color(&self, color: Color) -> Result<(), Error> {
ColorLightSetDeviceInfoParams::new(&self.client)
ColorLightSetDeviceInfoParams::new()
.color(color)
.send()
.send(self)
.await
}

Expand All @@ -126,9 +121,9 @@ impl ColorLightStripHandler {
/// * `hue` - between 1 and 360
/// * `saturation` - between 1 and 100
pub async fn set_hue_saturation(&self, hue: u16, saturation: u8) -> Result<(), Error> {
ColorLightSetDeviceInfoParams::new(&self.client)
ColorLightSetDeviceInfoParams::new()
.hue_saturation(hue, saturation)
.send()
.send(self)
.await
}

Expand All @@ -139,9 +134,9 @@ impl ColorLightStripHandler {
///
/// * `color_temperature` - between 2500 and 6500
pub async fn set_color_temperature(&self, color_temperature: u16) -> Result<(), Error> {
ColorLightSetDeviceInfoParams::new(&self.client)
ColorLightSetDeviceInfoParams::new()
.color_temperature(color_temperature)
.send()
.send(self)
.await
}

Expand All @@ -159,3 +154,9 @@ impl ColorLightStripHandler {
.await
}
}

impl HandlerExt for ColorLightStripHandler {
fn get_client(&self) -> &impl ApiClientExt {
&self.client
}
}
7 changes: 7 additions & 0 deletions tapo/src/api/handler_ext.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use super::ApiClientExt;

/// Implemented by all device handlers.
pub trait HandlerExt: Send + Sync {
/// Returns the client used by this handler.
fn get_client(&self) -> &impl ApiClientExt;
}
2 changes: 1 addition & 1 deletion tapo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
//! .set()
//! .brightness(50)
//! .color(Color::HotPink)
//! .send()
//! .send(&device)
//! .await?;
//!
//! println!("Waiting 2 seconds...");
Expand Down
Loading

0 comments on commit 482f8db

Please sign in to comment.