WIP: Working on trait macro
Some checks failed
Build and deploy automation_rs / Run pre-commit checks (push) Failing after 4m28s
Build and deploy automation_rs / Build automation_rs (push) Successful in 4m48s
Build and deploy automation_rs / Build Docker image (push) Successful in 55s
Build and deploy automation_rs / Deploy Docker container (push) Has been skipped

This commit is contained in:
2024-05-31 23:53:38 +02:00
parent 32aa981e31
commit 54fa9f3b66
5 changed files with 591 additions and 7 deletions

View File

@@ -7,6 +7,7 @@ edition = "2021"
[dependencies]
automation_cast = { path = "../automation_cast/" }
automation_macro = { path = "../automation_macro/" }
serde = { version = "1.0.149", features = ["derive"] }
serde_json = "1.0.89"
thiserror = "1.0.37"

View File

@@ -0,0 +1,63 @@
use automation_cast::Cast;
use automation_macro::google_home_traits;
use google_home::errors::ErrorCode;
use google_home::traits::AvailableSpeeds;
google_home_traits! {
GoogleHomeDevice {
"action.devices.traits.OnOff" => trait OnOff {
command_only_on_off: bool,
// This one is optional
query_only_on_off: Option<bool>,
async fn is_on(&self) -> Result<bool, ErrorCode> => on,
"action.devices.commands.OnOff" => async fn set_on(&self, on: bool) -> Result<(), ErrorCode>,
},
"action.devices.traits.Scene" => trait Scene {
scene_reversible: Option<bool>,
"action.devices.commands.ActivateScene" => async fn set_active(&self, activate: bool) -> Result<(), ErrorCode>,
},
"action.devices.traits.FanSpeed" => trait FanSpeed {
reversible: Option<bool>,
command_only_fan_speed: Option<bool>,
available_fan_speeds: AvailableSpeeds,
fn get_fan_speed(&self) -> Result<String, ErrorCode> => current_fan_speed_setting,
"action.devices.commands.SetFanSpeed" => fn set_speed(&self, fan_speed: String),
},
"action.devices.traits.HumiditySetting" => trait HumiditySetting {
query_only_humidity_setting: Option<bool>,
fn get_humidity(&self) -> Result<isize, ErrorCode> => humidity_ambient_percent,
}
}
}
trait Casts:
Cast<dyn OnOff> + Cast<dyn Scene> + Cast<dyn FanSpeed> + Cast<dyn HumiditySetting>
{
}
trait GoogleHomeDevice: Casts {}
struct Device {}
#[async_trait::async_trait]
impl OnOff for Device {
fn command_only_on_off(&self) -> bool {
false
}
async fn is_on(&self) -> Result<bool, ErrorCode> {
Ok(true)
}
async fn set_on(&self, _on: bool) -> Result<(), ErrorCode> {
Ok(())
}
}
fn main() {}