From dd379e4077189deca62629661ee33a706833bbec Mon Sep 17 00:00:00 2001 From: Dreaded_X Date: Sun, 31 Aug 2025 05:36:01 +0200 Subject: [PATCH] Feature: Send mqtt messages from lua --- automation_lib/src/mqtt.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/automation_lib/src/mqtt.rs b/automation_lib/src/mqtt.rs index b610fb1..8830ab2 100644 --- a/automation_lib/src/mqtt.rs +++ b/automation_lib/src/mqtt.rs @@ -23,7 +23,25 @@ impl DerefMut for WrappedAsyncClient { } } -impl mlua::UserData for WrappedAsyncClient {} +impl mlua::UserData for WrappedAsyncClient { + fn add_methods>(methods: &mut M) { + methods.add_async_method( + "send_message", + |_lua, this, (topic, message): (String, mlua::Value)| async move { + let message = serde_json::to_string(&message).unwrap(); + + debug!("message = {message}"); + + this.0 + .publish(topic, rumqttc::QoS::AtLeastOnce, true, message) + .await + .unwrap(); + + Ok(()) + }, + ); + } +} pub fn start(mut eventloop: EventLoop, event_channel: &EventChannel) { let tx = event_channel.get_tx();