Compare commits

..

2 Commits

Author SHA1 Message Date
40776ffe4c refactor: Split config
Some checks failed
Build and deploy / Deploy container (push) Blocked by required conditions
Build and deploy / build (push) Has been cancelled
2025-10-20 04:48:33 +02:00
3f61ec7252 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:48:33 +02:00
4 changed files with 17 additions and 17 deletions

View File

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

View File

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

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