diff --git a/automation_devices/src/hue_bridge.rs b/automation_devices/src/hue_bridge.rs index 3f03480..f4f9d38 100644 --- a/automation_devices/src/hue_bridge.rs +++ b/automation_devices/src/hue_bridge.rs @@ -3,7 +3,6 @@ use std::net::SocketAddr; use async_trait::async_trait; use automation_lib::device::{Device, LuaDeviceCreate}; -use automation_lib::lua::traits::AddAdditionalMethods; use automation_macro::{Device, LuaDeviceConfig}; use mlua::LuaSerdeExt; use serde::{Deserialize, Serialize}; @@ -32,7 +31,7 @@ pub struct Config { } #[derive(Debug, Clone, Device)] -#[device(traits(AddAdditionalMethods))] +#[device(add_methods(Self::add_methods))] pub struct HueBridge { config: Config, } @@ -84,19 +83,8 @@ impl HueBridge { } } } -} -impl Device for HueBridge { - fn get_id(&self) -> String { - self.config.identifier.clone() - } -} - -impl AddAdditionalMethods for HueBridge { - fn add_methods>(methods: &mut M) - where - Self: Sized + 'static, - { + fn add_methods>(methods: &mut M) { methods.add_async_method( "set_flag", async |lua, this, (flag, value): (mlua::Value, bool)| { @@ -109,3 +97,9 @@ impl AddAdditionalMethods for HueBridge { ); } } + +impl Device for HueBridge { + fn get_id(&self) -> String { + self.config.identifier.clone() + } +} diff --git a/automation_devices/src/ntfy.rs b/automation_devices/src/ntfy.rs index 5e8f329..0dd85e5 100644 --- a/automation_devices/src/ntfy.rs +++ b/automation_devices/src/ntfy.rs @@ -3,7 +3,6 @@ use std::convert::Infallible; use async_trait::async_trait; use automation_lib::device::{Device, LuaDeviceCreate}; -use automation_lib::lua::traits::AddAdditionalMethods; use automation_macro::{Device, LuaDeviceConfig}; use mlua::LuaSerdeExt; use serde::{Deserialize, Serialize}; @@ -119,11 +118,26 @@ pub struct Config { } #[derive(Debug, Clone, Device)] -#[device(traits(AddAdditionalMethods))] +#[device(add_methods(Self::add_methods))] pub struct Ntfy { config: Config, } +impl Ntfy { + fn add_methods>(methods: &mut M) { + methods.add_async_method( + "send_notification", + async |lua, this, notification: mlua::Value| { + let notification: Notification = lua.from_value(notification)?; + + this.send(notification).await; + + Ok(()) + }, + ); + } +} + #[async_trait] impl LuaDeviceCreate for Ntfy { type Config = Config; @@ -162,21 +176,3 @@ impl Ntfy { } } } - -impl AddAdditionalMethods for Ntfy { - fn add_methods>(methods: &mut M) - where - Self: Sized + 'static, - { - methods.add_async_method( - "send_notification", - async |lua, this, notification: mlua::Value| { - let notification: Notification = lua.from_value(notification)?; - - this.send(notification).await; - - Ok(()) - }, - ); - } -} diff --git a/automation_devices/src/presence.rs b/automation_devices/src/presence.rs index bc36324..e78cfcc 100644 --- a/automation_devices/src/presence.rs +++ b/automation_devices/src/presence.rs @@ -6,7 +6,6 @@ use automation_lib::action_callback::ActionCallback; use automation_lib::config::MqttDeviceConfig; use automation_lib::device::{Device, LuaDeviceCreate}; use automation_lib::event::OnMqtt; -use automation_lib::lua::traits::AddAdditionalMethods; use automation_lib::messages::PresenceMessage; use automation_lib::mqtt::WrappedAsyncClient; use automation_macro::{Device, LuaDeviceConfig}; @@ -35,7 +34,7 @@ pub struct State { } #[derive(Debug, Clone, Device)] -#[device(traits(AddAdditionalMethods))] +#[device(add_methods(Self::add_methods))] pub struct Presence { config: Config, state: Arc>, @@ -49,6 +48,12 @@ impl Presence { async fn state_mut(&self) -> RwLockWriteGuard<'_, State> { self.state.write().await } + + fn add_methods>(methods: &mut M) { + methods.add_async_method("overall_presence", async |_lua, this, ()| { + Ok(this.state().await.current_overall_presence) + }); + } } #[async_trait] @@ -125,14 +130,3 @@ impl OnMqtt for Presence { } } } - -impl AddAdditionalMethods for Presence { - fn add_methods>(methods: &mut M) - where - Self: Sized + 'static, - { - methods.add_async_method("overall_presence", async |_lua, this, ()| { - Ok(this.state().await.current_overall_presence) - }); - } -} diff --git a/automation_lib/src/lua/traits.rs b/automation_lib/src/lua/traits.rs index 55c2cab..4e1c724 100644 --- a/automation_lib/src/lua/traits.rs +++ b/automation_lib/src/lua/traits.rs @@ -78,9 +78,3 @@ pub trait OpenClose { } } impl OpenClose for T where T: google_home::traits::OpenClose {} - -pub trait AddAdditionalMethods { - fn add_methods>(methods: &mut M) - where - Self: Sized + 'static; -}