Compare commits

..

2 Commits

Author SHA1 Message Date
8b4ea06957 refactor: Split config
Some checks are pending
Build and deploy / build (push) Waiting to run
Build and deploy / Deploy container (push) Blocked by required conditions
2025-10-20 05:00:10 +02:00
be77711caf 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.
2025-10-20 04:59:41 +02:00
4 changed files with 17 additions and 17 deletions

View File

@@ -1,4 +1,4 @@
--- @type SetupInner
--- @type SetupTable
return {
require("config.rooms.bathroom"),
require("config.rooms.bedroom"),

View File

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

View File

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

View File

@@ -35,9 +35,9 @@ pub struct FulfillmentConfig {
}
#[derive(Debug, Default)]
pub struct Setups(mlua::Value);
pub struct Modules(mlua::Value);
impl Setups {
impl Modules {
pub async fn setup(
self,
lua: &mlua::Lua,
@@ -84,15 +84,15 @@ impl Setups {
}
}
impl FromLua for Setups {
impl FromLua for Modules {
fn from_lua(value: mlua::Value, _lua: &mlua::Lua) -> mlua::Result<Self> {
Ok(Setups(value))
Ok(Modules(value))
}
}
impl Typed for Setups {
impl Typed for Modules {
fn type_name() -> String {
"Setup".into()
"Modules".into()
}
fn generate_header() -> Option<String> {
@@ -100,9 +100,9 @@ impl Typed for Setups {
let client_type = WrappedAsyncClient::type_name();
Some(format!(
r#"---@alias {type_name}Function fun(mqtt_client: {client_type}): {type_name}Inner?
---@alias {type_name}Inner (DeviceInterface | {{ setup: {type_name}Function }} | {type_name}Inner)[]
---@alias {type_name} {type_name}Function | {type_name}Inner
r#"---@alias SetupFunction fun(mqtt_client: {client_type}): SetupTable?
---@alias SetupTable (DeviceInterface | {{ setup: SetupFunction? }} | SetupTable)[]
---@alias {type_name} SetupFunction | SetupTable
"#,
))
}
@@ -112,7 +112,7 @@ impl Typed for Setups {
pub struct Config {
pub fulfillment: FulfillmentConfig,
#[device_config(from_lua, default)]
pub modules: Option<Setups>,
pub modules: Option<Modules>,
#[device_config(from_lua)]
pub mqtt: MqttConfig,
#[device_config(from_lua, default)]