feat(config)!: Config now returns the mqtt config instead of the client

Instead the client is now created on the rust side based on the config.
Devices that require the mqtt client will now instead need to be
constructor using a function. This function receives the mqtt client.
This commit is contained in:
2025-10-19 04:58:53 +02:00
parent 7b7279017f
commit 2db4af7427
10 changed files with 70 additions and 122 deletions

View File

@@ -11,6 +11,7 @@ use automation::secret::EnvironmentSecretFile;
use automation::version::VERSION;
use automation::web::{ApiError, User};
use automation_lib::device_manager::DeviceManager;
use automation_lib::mqtt;
use axum::extract::{FromRef, State};
use axum::http::StatusCode;
use axum::routing::post;
@@ -139,8 +140,10 @@ async fn app() -> anyhow::Result<()> {
let entrypoint = Path::new(&setup.entrypoint);
let config: Config = lua.load(entrypoint).eval_async().await?;
let mqtt_client = mqtt::start(config.mqtt, &device_manager.event_channel());
if let Some(devices) = config.devices {
for device in devices.get(&lua, &config.mqtt).await? {
for device in devices.get(&lua, &mqtt_client).await? {
device_manager.add(device).await;
}
}

View File

@@ -3,6 +3,7 @@ use std::io::Write;
use automation::config::{Config, Devices, FulfillmentConfig};
use automation_lib::Module;
use automation_lib::mqtt::{MqttConfig, WrappedAsyncClient};
use lua_typed::Typed;
use tracing::{info, warn};
@@ -35,6 +36,11 @@ fn config_definitions() -> String {
output += &Config::generate_full().expect("Config should have a definition");
output += "\n";
output += &Devices::generate_full().expect("Devices should have a definition");
output += "\n";
output += &MqttConfig::generate_full().expect("MqttConfig should have a definition");
output += "\n";
output +=
&WrappedAsyncClient::generate_full().expect("WrappedAsyncClient should have a definition");
output
}

View File

@@ -3,7 +3,7 @@ use std::net::{Ipv4Addr, SocketAddr};
use automation_lib::action_callback::ActionCallback;
use automation_lib::device::Device;
use automation_lib::mqtt::WrappedAsyncClient;
use automation_lib::mqtt::{MqttConfig, WrappedAsyncClient};
use automation_macro::LuaDeviceConfig;
use lua_typed::Typed;
use mlua::FromLua;
@@ -105,7 +105,7 @@ pub struct Config {
#[device_config(from_lua, default)]
pub devices: Option<Devices>,
#[device_config(from_lua)]
pub mqtt: WrappedAsyncClient,
pub mqtt: MqttConfig,
#[device_config(from_lua, default)]
#[typed(default)]
pub schedule: HashMap<String, ActionCallback<()>>,