Skip to content

Commit

Permalink
Improve get child device ergonomics
Browse files Browse the repository at this point in the history
  • Loading branch information
mihai-dinculescu committed May 10, 2024
1 parent 924acd1 commit 78a8281
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 75 deletions.
10 changes: 8 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ file. This change log follows the conventions of

## [Rust Unreleased][Unreleased]

### Changed

- H100's create child device handler methods now take a `HubDevice` `enum` instead of a `String` and are now `async` to allow for more flexibility. This enables the caller to find child devices by either device ID or nickname.
- `PlugIdentifier` has been renamed to `Plug`.
- `Plug::ByDeviceId` now verifies that the provided device ID is found and returns an `Error::DeviceNotFound` error when it's not.

## [Python Unreleased][Unreleased]

## [Rust v0.7.11][v0.7.11] - 2024-05-04
Expand All @@ -18,8 +24,8 @@ file. This change log follows the conventions of
### Changed

- `ChildDeviceResult` has been renamed to `ChildDeviceHubResult` to facilitate adding support for other devices with children.
- `ColorLightStripHandler` has been renamed to `RgbicLightStripHandler` to better reflect it's purpose.
- `DeviceInfoColorLightStripResult` has been renamed to `DeviceInfoRgbicLightStripResult` to better reflect it's purpose.
- `ColorLightStripHandler` has been renamed to `RgbicLightStripHandler` to better reflect its purpose.
- `DeviceInfoColorLightStripResult` has been renamed to `DeviceInfoRgbicLightStripResult` to better reflect its purpose.

## [Python v0.3.0][py-v0.3.0] - 2024-05-04

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Unofficial Tapo API Client. Works with TP-Link Tapo smart devices. Tested with l
| set_target_temperature | ✓ | | | | | |
| set_temperature_offset | ✓ | | | | | |

\* Obtained by calling `get_child_device_list` on the hub device or `get_device_info` on a child handler.
\* Obtained by calling `get_child_device_list` on the hub device or `get_device_info` on a child device handler.


## Rust
Expand Down
12 changes: 6 additions & 6 deletions tapo/examples/tapo_h100.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::env;

use log::{info, LevelFilter};
use tapo::responses::ChildDeviceHubResult;
use tapo::ApiClient;
use tapo::{ApiClient, HubDevice};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
Expand Down Expand Up @@ -44,7 +44,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
);
}
ChildDeviceHubResult::S200B(device) => {
let s200b = hub.s200b(&device.device_id);
let s200b = hub.s200b(HubDevice::ByDeviceId(&device.device_id)).await?;
let trigger_logs = s200b.get_trigger_logs(5, 0).await?;

info!(
Expand All @@ -53,7 +53,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
);
}
ChildDeviceHubResult::T100(device) => {
let t100 = hub.t100(&device.device_id);
let t100 = hub.t100(HubDevice::ByDeviceId(&device.device_id)).await?;
let trigger_logs = t100.get_trigger_logs(5, 0).await?;

info!(
Expand All @@ -62,7 +62,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
);
}
ChildDeviceHubResult::T110(device) => {
let t110 = hub.t110(&device.device_id);
let t110 = hub.t110(HubDevice::ByDeviceId(&device.device_id)).await?;
let trigger_logs = t110.get_trigger_logs(5, 0).await?;

info!(
Expand All @@ -71,7 +71,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
);
}
ChildDeviceHubResult::T300(device) => {
let t300 = hub.t300(&device.device_id);
let t300 = hub.t300(HubDevice::ByDeviceId(&device.device_id)).await?;
let trigger_logs = t300.get_trigger_logs(5, 0).await?;

info!(
Expand All @@ -84,7 +84,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
);
}
ChildDeviceHubResult::T310(device) | ChildDeviceHubResult::T315(device) => {
let t31x = hub.t315(&device.device_id);
let t31x = hub.t315(HubDevice::ByDeviceId(&device.device_id)).await?;
let temperature_humidity_records = t31x.get_temperature_humidity_records().await?;

info!(
Expand Down
4 changes: 2 additions & 2 deletions tapo/examples/tapo_ke100.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::env;

use log::{info, LevelFilter};
use tapo::responses::TemperatureUnitKE100;
use tapo::ApiClient;
use tapo::{ApiClient, HubDevice};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
Expand All @@ -29,7 +29,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.await?;

// Get a handler for the child device
let device = hub.ke100(device_id);
let device = hub.ke100(HubDevice::ByDeviceId(&device_id)).await?;

// Get the device info of the child device
let device_info = device.get_device_info().await?;
Expand Down
6 changes: 2 additions & 4 deletions tapo/examples/tapo_p300.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use std::{env, thread, time::Duration};

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

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
Expand Down Expand Up @@ -35,9 +35,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
child.nickname, child.device_id, child.device_on,
);

let plug = power_strip
.plug(PlugIdentifier::ByDeviceId(&child.device_id))
.await?;
let plug = power_strip.plug(Plug::ByDeviceId(&child.device_id)).await?;

info!("Turning device on...");
plug.on().await?;
Expand Down
Loading

0 comments on commit 78a8281

Please sign in to comment.