diff --git a/config/config.lua b/config/config.lua index f683ccf..6493863 100644 --- a/config/config.lua +++ b/config/config.lua @@ -750,7 +750,7 @@ return { }, mqtt = mqtt_config, devices = { - create_devs, + devices = create_devs, ntfy, hue_bridge, kitchen_lights, diff --git a/definitions/config.lua b/definitions/config.lua index ad89353..9de3775 100644 --- a/definitions/config.lua +++ b/definitions/config.lua @@ -14,7 +14,9 @@ local FulfillmentConfig ---@field schedule table? 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 ---@field host string diff --git a/src/config.rs b/src/config.rs index 93bd2fb..2f7a29b 100644 --- a/src/config.rs +++ b/src/config.rs @@ -60,17 +60,19 @@ impl Devices { }; for pair in table.pairs() { - let (_, value): (mlua::Value, _) = pair?; + let (name, value): (String, _) = pair?; match value { - mlua::Value::UserData(_) => devices.push(Box::from_lua(value, lua)?), - mlua::Value::Function(f) => { + mlua::Value::Table(table) => queue.push_back(table), + 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?); } - _ => 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 { + let type_name = Self::type_name(); + let client_type = WrappedAsyncClient::type_name(); + Some(format!( - "---@alias {} (DeviceInterface | fun(client: {}): Devices)[]\n", - ::type_name(), - ::type_name() + r#"---@alias {type_name}Function fun(mqtt_client: {client_type}): {type_name}Inner +---@alias {type_name}Inner (DeviceInterface | {{ devices: {type_name}Function }} | {type_name}Inner)[] +---@alias {type_name} {type_name}Function | {type_name}Inner +"#, )) } } diff --git a/src/lib.rs b/src/lib.rs index f1e947d..dbfca74 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,5 @@ +#![feature(if_let_guard)] + pub mod config; pub mod schedule; pub mod secret;