Skip to main content

tapo/api/child_devices/
ke100_handler.rs

1use crate::error::Error;
2use crate::requests::TemperatureUnitKE100;
3use crate::requests::{TapoParams, TapoRequest, TrvSetDeviceInfoParams};
4use crate::responses::KE100Result;
5
6tapo_child_handler! {
7    /// Handler for the [KE100](https://www.tp-link.com/en/search/?q=KE100) devices.
8    KE100Handler(KE100Result),
9}
10
11impl KE100Handler {
12    /// Sets *child protection* on the device to *on* or *off*.
13    ///
14    /// # Arguments
15    ///
16    /// * `on`
17    pub async fn set_child_protection(&self, on: bool) -> Result<(), Error> {
18        let json = serde_json::to_value(TrvSetDeviceInfoParams::new().child_protection(on)?)?;
19        let request = TapoRequest::SetDeviceInfo(Box::new(TapoParams::new(json)));
20
21        self.client
22            .read()
23            .await
24            .control_child::<serde_json::Value>(self.device_id.clone(), request)
25            .await?;
26
27        Ok(())
28    }
29
30    /// Sets *frost protection* on the device to *on* or *off*.
31    ///
32    /// # Arguments
33    ///
34    /// * `on`
35    pub async fn set_frost_protection(&self, on: bool) -> Result<(), Error> {
36        let json = serde_json::to_value(TrvSetDeviceInfoParams::new().frost_protection_on(on)?)?;
37        let request = TapoRequest::SetDeviceInfo(Box::new(TapoParams::new(json)));
38
39        self.client
40            .read()
41            .await
42            .control_child::<serde_json::Value>(self.device_id.clone(), request)
43            .await?;
44
45        Ok(())
46    }
47
48    /// Sets the *maximum control temperature*.
49    ///
50    /// # Arguments
51    ///
52    /// * `value`
53    /// * `unit`
54    pub async fn set_max_control_temperature(
55        &self,
56        value: u8,
57        unit: TemperatureUnitKE100,
58    ) -> Result<(), Error> {
59        let json = serde_json::to_value(
60            TrvSetDeviceInfoParams::new().max_control_temperature(value, unit)?,
61        )?;
62        let request = TapoRequest::SetDeviceInfo(Box::new(TapoParams::new(json)));
63
64        self.client
65            .read()
66            .await
67            .control_child::<serde_json::Value>(self.device_id.clone(), request)
68            .await?;
69
70        Ok(())
71    }
72
73    /// Sets the *minimum control temperature*.
74    ///
75    /// # Arguments
76    ///
77    /// * `value`
78    /// * `unit`
79    pub async fn set_min_control_temperature(
80        &self,
81        value: u8,
82        unit: TemperatureUnitKE100,
83    ) -> Result<(), Error> {
84        let json = serde_json::to_value(
85            TrvSetDeviceInfoParams::new().min_control_temperature(value, unit)?,
86        )?;
87        let request = TapoRequest::SetDeviceInfo(Box::new(TapoParams::new(json)));
88
89        self.client
90            .read()
91            .await
92            .control_child::<serde_json::Value>(self.device_id.clone(), request)
93            .await?;
94
95        Ok(())
96    }
97
98    /// Sets the *target temperature*.
99    ///
100    /// # Arguments
101    ///
102    /// * `value` - between `min_control_temperature` and `max_control_temperature`
103    /// * `unit`
104    pub async fn set_target_temperature(
105        &self,
106        value: u8,
107        unit: TemperatureUnitKE100,
108    ) -> Result<(), Error> {
109        let device_info = self.get_device_info().await?;
110
111        if value < device_info.min_control_temperature
112            || value > device_info.max_control_temperature
113        {
114            return Err(Error::Validation {
115                field: "target_temperature".to_string(),
116                message: format!(
117                    "Target temperature must be between {} (min_control_temperature) and {} (max_control_temperature)",
118                    device_info.min_control_temperature, device_info.max_control_temperature
119                ),
120            });
121        }
122
123        let json =
124            serde_json::to_value(TrvSetDeviceInfoParams::new().target_temperature(value, unit)?)?;
125        let request = TapoRequest::SetDeviceInfo(Box::new(TapoParams::new(json)));
126
127        self.client
128            .read()
129            .await
130            .control_child::<serde_json::Value>(self.device_id.clone(), request)
131            .await?;
132
133        Ok(())
134    }
135
136    /// Sets the *temperature offset*.
137    ///
138    /// # Arguments
139    ///
140    /// * `value` - between -10 and 10
141    /// * `unit`
142    pub async fn set_temperature_offset(
143        &self,
144        value: i8,
145        unit: TemperatureUnitKE100,
146    ) -> Result<(), Error> {
147        let json =
148            serde_json::to_value(TrvSetDeviceInfoParams::new().temperature_offset(value, unit)?)?;
149        let request = TapoRequest::SetDeviceInfo(Box::new(TapoParams::new(json)));
150
151        self.client
152            .read()
153            .await
154            .control_child::<serde_json::Value>(self.device_id.clone(), request)
155            .await?;
156
157        Ok(())
158    }
159}