Started actually using the google home trait macro

This commit is contained in:
2024-07-06 00:34:15 +02:00
parent d84ff8ec8e
commit 9aa16e3ef8
18 changed files with 94 additions and 229 deletions

View File

@@ -134,7 +134,7 @@ impl GoogleHomeDevice for AirFilter {
#[async_trait]
impl OnOff for AirFilter {
async fn is_on(&self) -> Result<bool, ErrorCode> {
async fn on(&self) -> Result<bool, ErrorCode> {
Ok(self.last_known_state.state != AirFilterFanState::Off)
}
@@ -153,7 +153,7 @@ impl OnOff for AirFilter {
#[async_trait]
impl FanSpeed for AirFilter {
fn available_speeds(&self) -> AvailableSpeeds {
fn available_fan_speeds(&self) -> AvailableSpeeds {
AvailableSpeeds {
speeds: vec![
Speed {
@@ -189,7 +189,7 @@ impl FanSpeed for AirFilter {
}
}
async fn current_speed(&self) -> String {
fn current_fan_speed_setting(&self) -> Result<String, ErrorCode> {
let speed = match self.last_known_state.state {
AirFilterFanState::Off => "off",
AirFilterFanState::Low => "low",
@@ -197,17 +197,18 @@ impl FanSpeed for AirFilter {
AirFilterFanState::High => "high",
};
speed.into()
Ok(speed.into())
}
async fn set_speed(&self, speed: &str) -> Result<(), ErrorCode> {
let state = if speed == "off" {
async fn set_fan_speed(&mut self, fan_speed: String) -> Result<(), ErrorCode> {
let fan_speed = fan_speed.as_str();
let state = if fan_speed == "off" {
AirFilterFanState::Off
} else if speed == "low" {
} else if fan_speed == "low" {
AirFilterFanState::Low
} else if speed == "medium" {
} else if fan_speed == "medium" {
AirFilterFanState::Medium
} else if speed == "high" {
} else if fan_speed == "high" {
AirFilterFanState::High
} else {
return Err(google_home::errors::DeviceError::TransientError.into());
@@ -225,7 +226,7 @@ impl HumiditySetting for AirFilter {
Some(true)
}
async fn humidity_ambient_percent(&self) -> isize {
self.last_known_state.humidity.round() as isize
fn humidity_ambient_percent(&self) -> Result<isize, ErrorCode> {
Ok(self.last_known_state.humidity.round() as isize)
}
}

View File

@@ -92,7 +92,7 @@ impl OnMqtt for AudioSetup {
) {
match action {
RemoteAction::On => {
if mixer.is_on().await.unwrap() {
if mixer.on().await.unwrap() {
speakers.set_on(false).await.unwrap();
mixer.set_on(false).await.unwrap();
} else {
@@ -101,9 +101,9 @@ impl OnMqtt for AudioSetup {
}
},
RemoteAction::BrightnessMoveUp => {
if !mixer.is_on().await.unwrap() {
if !mixer.on().await.unwrap() {
mixer.set_on(true).await.unwrap();
} else if speakers.is_on().await.unwrap() {
} else if speakers.on().await.unwrap() {
speakers.set_on(false).await.unwrap();
} else {
speakers.set_on(true).await.unwrap();

View File

@@ -155,7 +155,7 @@ impl OnMqtt for ContactSensor {
for (light, previous) in &mut trigger.devices {
let mut light = light.write().await;
if let Some(light) = light.as_mut().cast_mut() as Option<&mut dyn OnOff> {
*previous = light.is_on().await.unwrap();
*previous = light.on().await.unwrap();
light.set_on(true).await.ok();
}
}

View File

@@ -152,7 +152,7 @@ impl OnOff for HueGroup {
Ok(())
}
async fn is_on(&self) -> Result<bool, ErrorCode> {
async fn on(&self) -> Result<bool, ErrorCode> {
let res = reqwest::Client::new()
.get(self.url_get_state())
.send()

View File

@@ -205,7 +205,7 @@ impl GoogleHomeDevice for IkeaOutlet {
#[async_trait]
impl traits::OnOff for IkeaOutlet {
async fn is_on(&self) -> Result<bool, ErrorCode> {
async fn on(&self) -> Result<bool, ErrorCode> {
Ok(self.last_known_state)
}

View File

@@ -207,7 +207,7 @@ impl Response {
#[async_trait]
impl traits::OnOff for KasaOutlet {
async fn is_on(&self) -> Result<bool, errors::ErrorCode> {
async fn on(&self) -> Result<bool, errors::ErrorCode> {
let mut stream = TcpStream::connect(self.config.addr)
.await
.or::<DeviceError>(Err(DeviceError::DeviceOffline))?;

View File

@@ -103,7 +103,7 @@ impl GoogleHomeDevice for WakeOnLAN {
#[async_trait]
impl traits::Scene for WakeOnLAN {
async fn set_active(&self, activate: bool) -> Result<(), ErrorCode> {
async fn set_active(&mut self, activate: bool) -> Result<(), ErrorCode> {
if activate {
debug!(
id = Device::get_id(self),