-
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tapo-py: Add support for the L530, L630, and L900 devices
Addresses #123.
- Loading branch information
1 parent
e9d2953
commit 4923824
Showing
14 changed files
with
558 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
"""L530, L630 & L900 Example""" | ||
|
||
import asyncio | ||
import os | ||
|
||
from tapo import ApiClient, Color | ||
|
||
|
||
async def main(): | ||
tapo_username = os.getenv("TAPO_USERNAME") | ||
tapo_password = os.getenv("TAPO_PASSWORD") | ||
ip_address = os.getenv("IP_ADDRESS") | ||
|
||
client = ApiClient(tapo_username, tapo_password) | ||
device = await client.l530(ip_address) | ||
|
||
print("Turning device on...") | ||
await device.on() | ||
|
||
print("Waiting 2 seconds...") | ||
await asyncio.sleep(2) | ||
|
||
print("Setting the brightness to 30%...") | ||
await device.set_brightness(30) | ||
|
||
print("Setting the color to `Chocolate`...") | ||
await device.set_color(Color.Chocolate) | ||
|
||
print("Waiting 2 seconds...") | ||
await asyncio.sleep(2) | ||
|
||
print("Setting the color to `Deep Sky Blue` using the `hue` and `saturation`...") | ||
await device.set_hue_saturation(195, 100) | ||
|
||
print("Waiting 2 seconds...") | ||
await asyncio.sleep(2) | ||
|
||
print("Setting the color to `Incandescent` using the `color temperature`...") | ||
await device.set_color_temperature(2700) | ||
|
||
print("Waiting 2 seconds...") | ||
await asyncio.sleep(2) | ||
|
||
print("Turning device off...") | ||
await device.off() | ||
|
||
device_info = await device.get_device_info() | ||
print(f"Device info: {device_info.to_dict()}") | ||
|
||
device_usage = await device.get_device_usage() | ||
print(f"Device usage: {device_usage.to_dict()}") | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
use std::sync::Arc; | ||
|
||
use pyo3::prelude::*; | ||
use tapo::{requests::Color, ColorLightHandler}; | ||
use tokio::sync::Mutex; | ||
|
||
use crate::errors::ErrorWrapper; | ||
|
||
#[derive(Clone)] | ||
#[pyclass(name = "ColorColorLightHandler")] | ||
pub struct PyColorLightHandler { | ||
handler: Arc<Mutex<ColorLightHandler>>, | ||
} | ||
|
||
impl PyColorLightHandler { | ||
pub fn new(handler: ColorLightHandler) -> Self { | ||
Self { | ||
handler: Arc::new(Mutex::new(handler)), | ||
} | ||
} | ||
} | ||
|
||
#[pymethods] | ||
impl PyColorLightHandler { | ||
pub fn refresh_session<'a>(&'a self, py: Python<'a>) -> PyResult<&'a PyAny> { | ||
let handler = self.handler.clone(); | ||
pyo3_asyncio::tokio::future_into_py(py, async move { | ||
handler | ||
.lock() | ||
.await | ||
.refresh_session() | ||
.await | ||
.map_err(ErrorWrapper)?; | ||
Ok(()) | ||
}) | ||
} | ||
|
||
pub fn on<'a>(&'a self, py: Python<'a>) -> PyResult<&'a PyAny> { | ||
let handler = self.handler.clone(); | ||
pyo3_asyncio::tokio::future_into_py(py, async move { | ||
handler.lock().await.on().await.map_err(ErrorWrapper)?; | ||
Ok(()) | ||
}) | ||
} | ||
|
||
pub fn off<'a>(&'a self, py: Python<'a>) -> PyResult<&'a PyAny> { | ||
let handler = self.handler.clone(); | ||
pyo3_asyncio::tokio::future_into_py(py, async move { | ||
handler.lock().await.off().await.map_err(ErrorWrapper)?; | ||
Ok(()) | ||
}) | ||
} | ||
|
||
pub fn device_reset<'a>(&'a self, py: Python<'a>) -> PyResult<&'a PyAny> { | ||
let handler = self.handler.clone(); | ||
pyo3_asyncio::tokio::future_into_py(py, async move { | ||
handler | ||
.lock() | ||
.await | ||
.device_reset() | ||
.await | ||
.map_err(ErrorWrapper)?; | ||
Ok(()) | ||
}) | ||
} | ||
|
||
pub fn get_device_info<'a>(&'a self, py: Python<'a>) -> PyResult<&'a PyAny> { | ||
let handler = self.handler.clone(); | ||
pyo3_asyncio::tokio::future_into_py(py, async move { | ||
let result = handler | ||
.lock() | ||
.await | ||
.get_device_info() | ||
.await | ||
.map_err(ErrorWrapper)?; | ||
Ok(result) | ||
}) | ||
} | ||
|
||
pub fn get_device_info_json<'a>(&self, py: Python<'a>) -> PyResult<&'a PyAny> { | ||
let handler = self.handler.clone(); | ||
|
||
pyo3_asyncio::tokio::future_into_py(py, async move { | ||
let result = handler | ||
.lock() | ||
.await | ||
.get_device_info_json() | ||
.await | ||
.map_err(ErrorWrapper)?; | ||
|
||
Python::with_gil(|py| tapo::python::serde_object_to_py_dict(py, &result)) | ||
}) | ||
} | ||
|
||
pub fn get_device_usage<'a>(&'a self, py: Python<'a>) -> PyResult<&'a PyAny> { | ||
let handler = self.handler.clone(); | ||
pyo3_asyncio::tokio::future_into_py(py, async move { | ||
let result = handler | ||
.lock() | ||
.await | ||
.get_device_usage() | ||
.await | ||
.map_err(ErrorWrapper)?; | ||
Ok(result) | ||
}) | ||
} | ||
|
||
pub fn set_brightness<'a>(&'a self, py: Python<'a>, brightness: u8) -> PyResult<&'a PyAny> { | ||
let handler = self.handler.clone(); | ||
pyo3_asyncio::tokio::future_into_py(py, async move { | ||
handler | ||
.lock() | ||
.await | ||
.set_brightness(brightness) | ||
.await | ||
.map_err(ErrorWrapper)?; | ||
Ok(()) | ||
}) | ||
} | ||
|
||
pub fn set_color<'a>(&'a self, py: Python<'a>, color: Color) -> PyResult<&'a PyAny> { | ||
let handler = self.handler.clone(); | ||
pyo3_asyncio::tokio::future_into_py(py, async move { | ||
handler | ||
.lock() | ||
.await | ||
.set_color(color) | ||
.await | ||
.map_err(ErrorWrapper)?; | ||
Ok(()) | ||
}) | ||
} | ||
|
||
pub fn set_hue_saturation<'a>( | ||
&'a self, | ||
py: Python<'a>, | ||
hue: u16, | ||
saturation: u8, | ||
) -> PyResult<&'a PyAny> { | ||
let handler = self.handler.clone(); | ||
pyo3_asyncio::tokio::future_into_py(py, async move { | ||
handler | ||
.lock() | ||
.await | ||
.set_hue_saturation(hue, saturation) | ||
.await | ||
.map_err(ErrorWrapper)?; | ||
Ok(()) | ||
}) | ||
} | ||
|
||
pub fn set_color_temperature<'a>( | ||
&'a self, | ||
py: Python<'a>, | ||
color_temperature: u16, | ||
) -> PyResult<&'a PyAny> { | ||
let handler = self.handler.clone(); | ||
pyo3_asyncio::tokio::future_into_py(py, async move { | ||
handler | ||
.lock() | ||
.await | ||
.set_color_temperature(color_temperature) | ||
.await | ||
.map_err(ErrorWrapper)?; | ||
Ok(()) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.