feat(config)!: Device creation function is now named entry

It now has to be called 'setup', 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 956f818a3b
6 changed files with 40 additions and 27 deletions

View File

@@ -749,8 +749,8 @@ return {
openid_url = "https://login.huizinga.dev/api/oidc", openid_url = "https://login.huizinga.dev/api/oidc",
}, },
mqtt = mqtt_config, mqtt = mqtt_config,
devices = { modules = {
create_devs, setup = create_devs,
ntfy, ntfy,
hue_bridge, hue_bridge,
kitchen_lights, kitchen_lights,

View File

@@ -9,12 +9,14 @@ local FulfillmentConfig
---@class Config ---@class Config
---@field fulfillment FulfillmentConfig ---@field fulfillment FulfillmentConfig
---@field devices Devices? ---@field modules Setup?
---@field mqtt MqttConfig ---@field mqtt MqttConfig
---@field schedule table<string, fun() | fun()[]>? ---@field schedule table<string, fun() | fun()[]>?
local Config local Config
---@alias Devices (DeviceInterface | fun(client: AsyncClient): Devices)[] ---@alias SetupFunction fun(mqtt_client: AsyncClient): SetupInner?
---@alias SetupInner (DeviceInterface | { setup: SetupFunction } | SetupInner)[]
---@alias Setup SetupFunction | SetupInner
---@class MqttConfig ---@class MqttConfig
---@field host string ---@field host string

View File

@@ -140,8 +140,8 @@ async fn app() -> anyhow::Result<()> {
let mqtt_client = mqtt::start(config.mqtt, &device_manager.event_channel()); let mqtt_client = mqtt::start(config.mqtt, &device_manager.event_channel());
if let Some(devices) = config.devices { if let Some(modules) = config.modules {
for device in devices.get(&lua, &mqtt_client).await? { for device in modules.setup(&lua, &mqtt_client).await? {
device_manager.add(device).await; device_manager.add(device).await;
} }
} }

View File

@@ -1,7 +1,7 @@
use std::fs::{self, File}; use std::fs::{self, File};
use std::io::Write; use std::io::Write;
use automation::config::{Config, Devices, FulfillmentConfig}; use automation::config::{Config, FulfillmentConfig, Setups};
use automation_lib::Module; use automation_lib::Module;
use automation_lib::mqtt::{MqttConfig, WrappedAsyncClient}; use automation_lib::mqtt::{MqttConfig, WrappedAsyncClient};
use lua_typed::Typed; use lua_typed::Typed;
@@ -35,7 +35,7 @@ fn config_definitions() -> String {
output += "\n"; output += "\n";
output += &Config::generate_full().expect("Config should have a definition"); output += &Config::generate_full().expect("Config should have a definition");
output += "\n"; output += "\n";
output += &Devices::generate_full().expect("Devices should have a definition"); output += &Setups::generate_full().expect("Setups should have a definition");
output += "\n"; output += "\n";
output += &MqttConfig::generate_full().expect("MqttConfig should have a definition"); output += &MqttConfig::generate_full().expect("MqttConfig should have a definition");
output += "\n"; output += "\n";

View File

@@ -35,10 +35,10 @@ pub struct FulfillmentConfig {
} }
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct Devices(mlua::Value); pub struct Setups(mlua::Value);
impl Devices { impl Setups {
pub async fn get( pub async fn setup(
self, self,
lua: &mlua::Lua, lua: &mlua::Lua,
client: &WrappedAsyncClient, client: &WrappedAsyncClient,
@@ -60,17 +60,22 @@ 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(_)
queue.push_back(f.call_async(client.clone()).await?); if let Ok(device) = Box::from_lua(value.clone(), lua) =>
{
devices.push(device);
} }
_ => Err(mlua::Error::runtime(format!( mlua::Value::Function(f) if name == "setup" => {
"Expected a device, table, or function, instead found: {}", let value: mlua::Value = f.call_async(client.clone()).await?;
value.type_name() if let Some(table) = value.as_table() {
)))?, queue.push_back(table.clone());
}
}
_ => {}
} }
} }
} }
@@ -79,22 +84,26 @@ impl Devices {
} }
} }
impl FromLua for Devices { impl FromLua for Setups {
fn from_lua(value: mlua::Value, _lua: &mlua::Lua) -> mlua::Result<Self> { fn from_lua(value: mlua::Value, _lua: &mlua::Lua) -> mlua::Result<Self> {
Ok(Devices(value)) Ok(Setups(value))
} }
} }
impl Typed for Devices { impl Typed for Setups {
fn type_name() -> String { fn type_name() -> String {
"Devices".into() "Setup".into()
} }
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 | {{ setup: {type_name}Function }} | {type_name}Inner)[]
<WrappedAsyncClient as Typed>::type_name() ---@alias {type_name} {type_name}Function | {type_name}Inner
"#,
)) ))
} }
} }
@@ -103,7 +112,7 @@ impl Typed for Devices {
pub struct Config { pub struct Config {
pub fulfillment: FulfillmentConfig, pub fulfillment: FulfillmentConfig,
#[device_config(from_lua, default)] #[device_config(from_lua, default)]
pub devices: Option<Devices>, pub modules: Option<Setups>,
#[device_config(from_lua)] #[device_config(from_lua)]
pub mqtt: MqttConfig, pub mqtt: MqttConfig,
#[device_config(from_lua, default)] #[device_config(from_lua, default)]

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;