ActionCallback can now handle tuples

This commit is contained in:
Dreaded_X 2024-12-04 01:29:28 +01:00
parent 6b8d0b7d56
commit d39432fa22
Signed by: Dreaded_X
GPG Key ID: FA5F485356B0D2D4
2 changed files with 16 additions and 8 deletions

View File

@ -1,6 +1,6 @@
use std::marker::PhantomData; use std::marker::PhantomData;
use mlua::{FromLua, IntoLua}; use mlua::{FromLua, IntoLuaMulti};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
struct Internal { struct Internal {
@ -8,12 +8,21 @@ struct Internal {
lua: mlua::Lua, lua: mlua::Lua,
} }
#[derive(Debug, Clone, Default)] #[derive(Debug, Clone)]
pub struct ActionCallback<T> { pub struct ActionCallback<T> {
internal: Option<Internal>, internal: Option<Internal>,
phantom: PhantomData<T>, phantom: PhantomData<T>,
} }
impl<T> Default for ActionCallback<T> {
fn default() -> Self {
Self {
internal: None,
phantom: PhantomData::<T>,
}
}
}
impl<T> FromLua for ActionCallback<T> { impl<T> FromLua for ActionCallback<T> {
fn from_lua(value: mlua::Value, lua: &mlua::Lua) -> mlua::Result<Self> { fn from_lua(value: mlua::Value, lua: &mlua::Lua) -> mlua::Result<Self> {
let uuid = uuid::Uuid::new_v4(); let uuid = uuid::Uuid::new_v4();
@ -32,7 +41,7 @@ impl<T> FromLua for ActionCallback<T> {
// TODO: Return proper error here // TODO: Return proper error here
impl<T> ActionCallback<T> impl<T> ActionCallback<T>
where where
T: IntoLua + Sync + Send + Clone + Copy + 'static, T: IntoLuaMulti + Sync + Send + Clone + 'static,
{ {
pub async fn call(&self, state: T) { pub async fn call(&self, state: T) {
let Some(internal) = self.internal.as_ref() else { let Some(internal) = self.internal.as_ref() else {

View File

@ -22,12 +22,11 @@ pub struct Config {
#[device_config(from_lua)] #[device_config(from_lua)]
pub client: WrappedAsyncClient, pub client: WrappedAsyncClient,
// TODO: IntoLua is not implemented for unit type ()
#[device_config(from_lua, default)] #[device_config(from_lua, default)]
pub left_callback: ActionCallback<bool>, pub left_callback: ActionCallback<()>,
#[device_config(from_lua, default)] #[device_config(from_lua, default)]
pub right_callback: ActionCallback<bool>, pub right_callback: ActionCallback<()>,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -75,10 +74,10 @@ impl OnMqtt for HueSwitch {
match action { match action {
zigbee2mqtt_types::vendors::philips::Zigbee929003017102Action::Leftpress => { zigbee2mqtt_types::vendors::philips::Zigbee929003017102Action::Leftpress => {
self.config.left_callback.call(true).await self.config.left_callback.call(()).await
} }
zigbee2mqtt_types::vendors::philips::Zigbee929003017102Action::Rightpress => { zigbee2mqtt_types::vendors::philips::Zigbee929003017102Action::Rightpress => {
self.config.right_callback.call(true).await self.config.right_callback.call(()).await
} }
_ => {} _ => {}
} }