Files
tischlerCtrl/agents/tapo/tapo-fork/tapo-py/src/api/plug_handler.rs
seb 028763bdb2 feat(tapo-agent): add schedule/countdown timer API support
- Fork tapo crate to add missing schedule/timer APIs
- Add get_countdown_rules, get_schedule_rules, get_next_event methods
- New readings: countdown_active, countdown_remain, schedule_count,
  schedule_active_count, next_event_time
- Add local compilation to build script alongside cross-compilation
- Implement offline polling - device collection continues when server disconnects
- Add more device readings: on_time, signal_level, rssi, runtime_today/month, energy_month

Vendored tapo fork in tapo-fork/ with minimal changes to add schedule APIs.
2025-12-23 00:46:42 +01:00

80 lines
2.3 KiB
Rust

use std::ops::{Deref, DerefMut};
use std::sync::Arc;
use pyo3::prelude::*;
use pyo3::types::PyDict;
use tapo::responses::{DeviceInfoPlugResult, DeviceUsageResult};
use tapo::{DeviceManagementExt as _, PlugHandler};
use tokio::sync::RwLock;
use crate::call_handler_method;
#[derive(Clone)]
#[pyclass(name = "PlugHandler")]
pub struct PyPlugHandler {
inner: Arc<RwLock<PlugHandler>>,
}
impl PyPlugHandler {
pub fn new(handler: PlugHandler) -> Self {
Self {
inner: Arc::new(RwLock::new(handler)),
}
}
}
#[pymethods]
impl PyPlugHandler {
pub async fn refresh_session(&self) -> PyResult<()> {
let handler = self.inner.clone();
call_handler_method!(
handler.write().await.deref_mut(),
PlugHandler::refresh_session,
discard_result
)
}
pub async fn on(&self) -> PyResult<()> {
let handler = self.inner.clone();
call_handler_method!(handler.read().await.deref(), PlugHandler::on)
}
pub async fn off(&self) -> PyResult<()> {
let handler = self.inner.clone();
call_handler_method!(handler.read().await.deref(), PlugHandler::off)
}
pub async fn device_reboot(&self, delay_s: u16) -> PyResult<()> {
let handler = self.inner.clone();
call_handler_method!(
handler.read().await.deref(),
PlugHandler::device_reboot,
delay_s
)
}
pub async fn device_reset(&self) -> PyResult<()> {
let handler = self.inner.clone();
call_handler_method!(handler.read().await.deref(), PlugHandler::device_reset)
}
pub async fn get_device_info(&self) -> PyResult<DeviceInfoPlugResult> {
let handler = self.inner.clone();
call_handler_method!(handler.read().await.deref(), PlugHandler::get_device_info)
}
pub async fn get_device_info_json(&self) -> PyResult<Py<PyDict>> {
let handler = self.inner.clone();
let result = call_handler_method!(
handler.read().await.deref(),
PlugHandler::get_device_info_json
)?;
Python::attach(|py| tapo::python::serde_object_to_py_dict(py, &result))
}
pub async fn get_device_usage(&self) -> PyResult<DeviceUsageResult> {
let handler = self.inner.clone();
call_handler_method!(handler.read().await.deref(), PlugHandler::get_device_usage)
}
}