Compare commits

..

9 Commits

Author SHA1 Message Date
295491c5fc feat(config)!: Move mqtt module to actual separate module
Some checks failed
Build and deploy / Deploy container (push) Blocked by required conditions
Build and deploy / build (push) Has been cancelled
The automation:mqtt module now gets loaded in a similar way as the
automation:devices and automation:utils modules.
This leads to a breaking change where instantiating a new mqtt client
the device manager needs to be explicitly passed in.
2025-10-15 03:41:39 +02:00
9f244b3475 feat: Added/expanded Typed impls 2025-10-15 03:41:38 +02:00
15a6e83ad8 feat: Remove automatic automation: module prefix 2025-10-15 03:41:38 +02:00
c727579290 chore: Removed dotenvy
Since secrets can now be set from automation.toml the .env file was no
longer used, so dotenvy can be removed.
2025-10-15 03:41:38 +02:00
19e8663f26 feat: Use Typed type_name for registering proxy 2025-10-15 03:41:38 +02:00
f5c4495cad feat!: Expanded add_methods to extra_user_data
Instead of being a function it now expects a struct with the
PartialUserData trait implemented. This in part ensures the correct
function signature.

It also adds another optional function to PartialUserData that returns
definitions for the added methods.
2025-10-15 03:41:25 +02:00
9bbf0a5422 feat: Specify (optional) interface name in PartialUserData 2025-10-15 00:45:38 +02:00
85e3c7b877 feat: Use PartialUserData on proxy type to add trait methods 2025-10-15 00:45:38 +02:00
a2130005de feat: Improved attribute parsing in device macro 2025-10-15 00:45:37 +02:00
6 changed files with 29 additions and 84 deletions

1
Cargo.lock generated
View File

@@ -98,7 +98,6 @@ dependencies = [
"config",
"git-version",
"google_home",
"inventory",
"mlua",
"reqwest",
"rumqttc",

View File

@@ -76,7 +76,6 @@ config = { version = "0.15.15", default-features = false, features = [
] }
git-version = "0.3.9"
google_home = { workspace = true }
inventory = { workspace = true }
mlua = { workspace = true }
reqwest = { workspace = true }
rumqttc = { workspace = true }

View File

@@ -15,7 +15,20 @@ mod zigbee;
use automation_lib::Module;
use automation_lib::device::{Device, LuaDeviceCreate};
use tracing::{debug, warn};
use tracing::debug;
macro_rules! register_device {
($device:ty) => {
::inventory::submit!(crate::RegisteredDevice::new(
<$device as ::lua_typed::Typed>::type_name,
::mlua::Lua::create_proxy::<$device>
));
crate::register_type!($device);
};
}
pub(crate) use register_device;
type DeviceNameFn = fn() -> String;
type RegisterDeviceFn = fn(lua: &mlua::Lua) -> mlua::Result<mlua::AnyUserData>;
@@ -42,18 +55,6 @@ impl RegisteredDevice {
}
}
macro_rules! register_device {
($device:ty) => {
::inventory::submit!(crate::RegisteredDevice::new(
<$device as ::lua_typed::Typed>::type_name,
::mlua::Lua::create_proxy::<$device>
));
crate::register_type!($device);
};
}
pub(crate) use register_device;
inventory::collect!(RegisteredDevice);
pub fn create_module(lua: &mlua::Lua) -> mlua::Result<mlua::Table> {
@@ -70,9 +71,7 @@ pub fn create_module(lua: &mlua::Lua) -> mlua::Result<mlua::Table> {
Ok(devices)
}
type RegisterTypeFn = fn() -> Option<String>;
pub struct RegisteredType(RegisterTypeFn);
inventory::submit! {Module::new("automation:devices", create_module)}
macro_rules! register_type {
($ty:ty) => {
@@ -81,25 +80,20 @@ macro_rules! register_type {
));
};
}
pub(crate) use register_type;
type RegisterTypeFn = fn() -> Option<String>;
pub struct RegisteredType(RegisterTypeFn);
inventory::collect!(RegisteredType);
fn generate_definitions() -> String {
let mut output = String::new();
output += "---@meta\n\nlocal devices\n\n";
pub fn generate_definitions() {
println!("---@meta\n\nlocal devices\n");
for ty in inventory::iter::<RegisteredType> {
if let Some(def) = ty.0() {
output += &(def + "\n");
} else {
// NOTE: Due to how this works the typed is erased, so we don't know the cause
warn!("Registered type is missing generate_full function");
}
let def = ty.0().unwrap();
println!("{def}");
}
output += "return devices";
output
println!("return devices")
}
inventory::submit! {Module::new("automation:devices", create_module, Some(generate_definitions))}

View File

@@ -17,25 +17,15 @@ pub mod mqtt;
pub mod schedule;
type RegisterFn = fn(lua: &mlua::Lua) -> mlua::Result<mlua::Table>;
type DefinitionsFn = fn() -> String;
pub struct Module {
name: &'static str,
register_fn: RegisterFn,
definitions_fn: Option<DefinitionsFn>,
}
impl Module {
pub const fn new(
name: &'static str,
register_fn: RegisterFn,
definitions_fn: Option<DefinitionsFn>,
) -> Self {
Self {
name,
register_fn,
definitions_fn,
}
pub const fn new(name: &'static str, register_fn: RegisterFn) -> Self {
Self { name, register_fn }
}
pub const fn get_name(&self) -> &'static str {
@@ -45,10 +35,6 @@ impl Module {
pub fn register(&self, lua: &mlua::Lua) -> mlua::Result<mlua::Table> {
(self.register_fn)(lua)
}
pub fn definitions(&self) -> Option<String> {
self.definitions_fn.map(|f| f())
}
}
pub fn load_modules(lua: &mlua::Lua) -> mlua::Result<()> {

View File

@@ -2,7 +2,6 @@ mod timeout;
use std::time::{SystemTime, UNIX_EPOCH};
use lua_typed::Typed;
pub use timeout::Timeout;
use crate::Module;
@@ -29,20 +28,4 @@ fn create_module(lua: &mlua::Lua) -> mlua::Result<mlua::Table> {
Ok(utils)
}
fn generate_definitions() -> String {
let mut output = String::new();
output += "---@meta\n\nlocal utils\n\n";
output += &Timeout::generate_full().expect("Timeout should have generate_full");
output += "\n";
output += "---@return string\nfunction utils.get_hostname() end\n\n";
output += "---@return integer\nfunction utils.get_epoch() end\n\n";
output += "return utils";
output
}
inventory::submit! {Module::new("automation:utils", create_module, Some(generate_definitions))}
inventory::submit! {Module::new("automation:utils", create_module)}

View File

@@ -132,20 +132,4 @@ fn create_module(lua: &mlua::Lua) -> mlua::Result<mlua::Table> {
Ok(mqtt)
}
fn generate_definitions() -> String {
let mut output = String::new();
output += "---@meta\n\nlocal mqtt\n\n";
output += &MqttConfig::generate_full().expect("WrappedAsyncClient should have generate_full");
output += "\n";
output +=
&WrappedAsyncClient::generate_full().expect("WrappedAsyncClient should have generate_full");
output += "\n";
output += "return mqtt";
output
}
inventory::submit! {Module::new("automation:mqtt", create_module, Some(generate_definitions))}
inventory::submit! {Module::new("automation:mqtt", create_module)}