feat(config)!: Device creation function is now named entry
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:
@@ -750,7 +750,7 @@ return {
|
||||
},
|
||||
mqtt = mqtt_config,
|
||||
devices = {
|
||||
create_devs,
|
||||
devices = create_devs,
|
||||
ntfy,
|
||||
hue_bridge,
|
||||
kitchen_lights,
|
||||
|
||||
@@ -14,7 +14,9 @@ local FulfillmentConfig
|
||||
---@field schedule table<string, fun() | fun()[]>?
|
||||
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
|
||||
|
||||
@@ -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<String> {
|
||||
let type_name = Self::type_name();
|
||||
let client_type = WrappedAsyncClient::type_name();
|
||||
|
||||
Some(format!(
|
||||
"---@alias {} (DeviceInterface | fun(client: {}): Devices)[]\n",
|
||||
<Self as Typed>::type_name(),
|
||||
<WrappedAsyncClient as Typed>::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
|
||||
"#,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
#![feature(if_let_guard)]
|
||||
|
||||
pub mod config;
|
||||
pub mod schedule;
|
||||
pub mod secret;
|
||||
|
||||
Reference in New Issue
Block a user