feat(config)!: Device creation function is now named entry
All checks were successful
Build and deploy / build (push) Successful in 11m22s
Build and deploy / Deploy container (push) Has been skipped

It now has to be called 'devices', this makes it possible to just
include the table as a whole in devices and it will automatically call
the correct function.
This commit is contained in:
2025-10-20 04:08:55 +02:00
parent a0c5189ada
commit 460aba6f9d
4 changed files with 22 additions and 12 deletions

View File

@@ -750,7 +750,7 @@ return {
}, },
mqtt = mqtt_config, mqtt = mqtt_config,
devices = { devices = {
create_devs, devices = create_devs,
ntfy, ntfy,
hue_bridge, hue_bridge,
kitchen_lights, kitchen_lights,

View File

@@ -14,7 +14,9 @@ local FulfillmentConfig
---@field schedule table<string, fun() | fun()[]>? ---@field schedule table<string, fun() | fun()[]>?
local Config local Config
---@alias Devices (DeviceInterface | fun(client: AsyncClient): Devices)[] ---@alias DevicesFunction fun(mqtt_client: AsyncClient): DevicesInner
---@alias DevicesInner (DeviceInterface | { devices: DevicesFunction } | DevicesInner)[]
---@alias Devices DevicesFunction | DevicesInner
---@class MqttConfig ---@class MqttConfig
---@field host string ---@field host string

View File

@@ -60,17 +60,19 @@ impl Devices {
}; };
for pair in table.pairs() { for pair in table.pairs() {
let (_, value): (mlua::Value, _) = pair?; let (name, value): (String, _) = pair?;
match value { match value {
mlua::Value::UserData(_) => devices.push(Box::from_lua(value, lua)?), mlua::Value::Table(table) => queue.push_back(table),
mlua::Value::Function(f) => { mlua::Value::UserData(_)
if let Ok(device) = Box::from_lua(value.clone(), lua) =>
{
devices.push(device);
}
mlua::Value::Function(f) if name == "devices" => {
queue.push_back(f.call_async(client.clone()).await?); queue.push_back(f.call_async(client.clone()).await?);
} }
_ => Err(mlua::Error::runtime(format!( _ => {}
"Expected a device, table, or function, instead found: {}",
value.type_name()
)))?,
} }
} }
} }
@@ -91,10 +93,14 @@ impl Typed for Devices {
} }
fn generate_header() -> Option<String> { fn generate_header() -> Option<String> {
let type_name = Self::type_name();
let client_type = WrappedAsyncClient::type_name();
Some(format!( Some(format!(
"---@alias {} (DeviceInterface | fun(client: {}): Devices)[]\n", r#"---@alias {type_name}Function fun(mqtt_client: {client_type}): {type_name}Inner
<Self as Typed>::type_name(), ---@alias {type_name}Inner (DeviceInterface | {{ devices: {type_name}Function }} | {type_name}Inner)[]
<WrappedAsyncClient as Typed>::type_name() ---@alias {type_name} {type_name}Function | {type_name}Inner
"#,
)) ))
} }
} }

View File

@@ -1,3 +1,5 @@
#![feature(if_let_guard)]
pub mod config; pub mod config;
pub mod schedule; pub mod schedule;
pub mod secret; pub mod secret;