1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use crate::tapo_vendor::error::Error;
#[cfg(any())]
use crate::tapo_vendor::responses::ChildDeviceComponentList;
use crate::tapo_vendor::responses::{
ChildDeviceListPowerStripResult, DeviceInfoPowerStripResult, PowerStripPlugResult,
};
use super::{Plug, PowerStripPlugHandler};
tapo_handler! {
/// Handler for the [P300](https://www.tp-link.com/en/search/?q=P300) and
/// [P306](https://www.tp-link.com/us/search/?q=P306) devices.
PowerStripHandler(DeviceInfoPowerStripResult),
device_management,
}
impl PowerStripHandler {
/// Returns *child device list* as [`Vec<PowerStripPlugResult>`].
/// It is not guaranteed to contain all the properties returned from the Tapo API.
/// If the deserialization fails, or if a property that you care about it's not present,
#[cfg_attr(
any(),
doc = "try [`PowerStripHandler::get_child_device_list_json`] (requires the `debug` feature)."
)]
#[cfg_attr(
not(any()),
doc = "try `PowerStripHandler::get_child_device_list_json` (requires the `debug` feature)."
)]
pub async fn get_child_device_list(&self) -> Result<Vec<PowerStripPlugResult>, Error> {
self.client
.read()
.await
.get_child_device_list::<ChildDeviceListPowerStripResult>(0)
.await
.map(|r| r.plugs)
}
/// Returns *child device list* as [`serde_json::Value`].
/// It contains all the properties returned from the Tapo API.
#[cfg(any())]
pub async fn get_child_device_list_json(&self) -> Result<serde_json::Value, Error> {
self.client.read().await.get_child_device_list(0).await
}
/// Returns *child device component list* as [`Vec<ChildDeviceComponentList>`].
/// This information is useful in debugging or when investigating new functionality to add.
#[cfg(any())]
pub async fn get_child_device_component_list(
&self,
) -> Result<Vec<ChildDeviceComponentList>, Error> {
self.client
.read()
.await
.get_child_device_component_list()
.await
}
}
/// Child device handler builders.
impl PowerStripHandler {
/// Returns a [`PowerStripPlugHandler`] for the given [`Plug`].
///
/// # Arguments
///
/// * `identifier` - a PowerStrip plug identifier.
///
/// # Example
///
/// ```ignore
/// # use tapo::{ApiClient, Plug};
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// // Connect to the hub
/// let power_strip = ApiClient::new("tapo-username@example.com", "tapo-password")
/// .p300("192.168.1.100")
/// .await?;
/// // Get a handler for the child device
/// let device_id = "0000000000000000000000000000000000000000".to_string();
/// let device = power_strip.plug(Plug::ByDeviceId(device_id)).await?;
/// // Get the device info of the child device
/// let device_info = device.get_device_info().await?;
/// # Ok(())
/// # }
/// ```
pub async fn plug(&self, identifier: Plug) -> Result<PowerStripPlugHandler, Error> {
let children = self.get_child_device_list().await?;
let device_id = match identifier {
Plug::ByDeviceId(device_id) => children
.iter()
.find(|child| child.device_id == device_id)
.ok_or_else(|| Error::DeviceNotFound)?
.device_id
.clone(),
Plug::ByNickname(nickname) => children
.iter()
.find(|child| child.nickname == nickname)
.ok_or_else(|| Error::DeviceNotFound)?
.device_id
.clone(),
Plug::ByPosition(position) => children
.iter()
.find(|child| child.position == position)
.ok_or_else(|| Error::DeviceNotFound)?
.device_id
.clone(),
};
Ok(PowerStripPlugHandler::new(self.client.clone(), device_id))
}
/// Returns a [`PowerStripPlugHandler`] for the given `device_id` without
/// first listing the power strip's plugs to verify the device exists. The
/// device id is trusted; if it is wrong, subsequent operations on the
/// returned handler will fail at request time. Use this when you already
/// have a valid device id (e.g. from a prior
/// [`PowerStripHandler::get_child_device_list`] call) to avoid the extra
/// validation round-trip performed by [`PowerStripHandler::plug`].
pub fn plug_unchecked(&self, device_id: impl Into<String>) -> PowerStripPlugHandler {
PowerStripPlugHandler::new(self.client.clone(), device_id.into())
}
}