feat!: Improve lua module registration

Instead of having to call all the module registration functions in one
place it is possible for each module to register itself in a global registry.
During startup all the all the modules will be registered
automatically.

This does currently have one weakness, to need to ensure that the crate
is linked.
This commit is contained in:
2025-09-10 01:24:21 +02:00
parent d008bb0786
commit 07e2d18a28
7 changed files with 56 additions and 4 deletions

View File

@@ -13,6 +13,7 @@ bytes = { workspace = true }
dyn-clone = { workspace = true }
eui48 = { workspace = true }
google_home = { workspace = true }
inventory = { workspace = true }
mlua = { workspace = true }
reqwest = { workspace = true }
rumqttc = { workspace = true }

View File

@@ -12,6 +12,7 @@ mod wake_on_lan;
mod washer;
mod zigbee;
use automation_lib::Module;
use automation_lib::device::{Device, LuaDeviceCreate};
use zigbee::light::{LightBrightness, LightColorTemperature, LightOnOff};
use zigbee::outlet::{OutletOnOff, OutletPower};
@@ -36,7 +37,7 @@ macro_rules! register_device {
};
}
pub fn register_with_lua(lua: &mlua::Lua) -> mlua::Result<()> {
pub fn register_with_lua(lua: &mlua::Lua) -> mlua::Result<mlua::Table> {
register_device!(lua, AirFilter);
register_device!(lua, ContactSensor);
register_device!(lua, HueBridge);
@@ -55,5 +56,8 @@ pub fn register_with_lua(lua: &mlua::Lua) -> mlua::Result<()> {
register_device!(lua, WakeOnLAN);
register_device!(lua, Washer);
Ok(())
// For now return an empty table and keep the devices in the global table
lua.create_table()
}
inventory::submit! {Module::new("devices", register_with_lua)}