use anyhow::Result; pub struct Tasmota { device: String, } impl Tasmota { pub fn new(device: impl ToString) -> Self { Self { device: device.to_string(), } } pub fn name(&self) -> &str { &self.device } fn command(&self, command: &str) -> String { format!("http://{}/cm?cmnd={}", self.device, command) } async fn post(&self, command: &str) -> Result { Ok(reqwest::Client::new() .post(&self.command(command)) .send() .await? .text() .await?) } async fn get(&self, command: &str) -> Result { Ok(reqwest::Client::new() .get(&self.command(command)) .send() .await? .text() .await?) } pub async fn turn_on_led(&self) -> Result<()> { self.post("LedPower=1").await?; Ok(()) } pub async fn turn_off_led(&self) -> Result<()> { self.post("LedPower=2").await?; Ok(()) } pub async fn switch_on(&self) -> Result<()> { self.post("Power0=1").await?; Ok(()) } pub async fn switch_off(&self) -> Result<()> { self.post("Power0=0").await?; Ok(()) } pub async fn read_power_usage(&self) -> Result { let res = self.get("Status=8").await?; Ok(res.parse()?) } }