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:
@@ -312,6 +312,7 @@ local bedroom_air_filter = devices.AirFilter.new({
|
||||
url = "http://10.0.0.103",
|
||||
})
|
||||
|
||||
--- @type SetupFunction
|
||||
local function create_devs(mqtt_client)
|
||||
on_presence:add(function(presence)
|
||||
mqtt_client:send_message(mqtt_automation("debug") .. "/presence", {
|
||||
@@ -749,8 +750,8 @@ return {
|
||||
openid_url = "https://login.huizinga.dev/api/oidc",
|
||||
},
|
||||
mqtt = mqtt_config,
|
||||
devices = {
|
||||
create_devs,
|
||||
modules = {
|
||||
setup = create_devs,
|
||||
ntfy,
|
||||
hue_bridge,
|
||||
kitchen_lights,
|
||||
|
||||
@@ -9,12 +9,14 @@ local FulfillmentConfig
|
||||
|
||||
---@class Config
|
||||
---@field fulfillment FulfillmentConfig
|
||||
---@field devices Devices?
|
||||
---@field modules Modules?
|
||||
---@field mqtt MqttConfig
|
||||
---@field schedule table<string, fun() | fun()[]>?
|
||||
local Config
|
||||
|
||||
---@alias Devices (DeviceInterface | fun(client: AsyncClient): Devices)[]
|
||||
---@alias SetupFunction fun(mqtt_client: AsyncClient): SetupTable?
|
||||
---@alias SetupTable (DeviceInterface | { setup: SetupFunction? } | SetupTable)[]
|
||||
---@alias Modules SetupFunction | SetupTable
|
||||
|
||||
---@class MqttConfig
|
||||
---@field host string
|
||||
|
||||
@@ -140,8 +140,8 @@ async fn app() -> anyhow::Result<()> {
|
||||
|
||||
let mqtt_client = mqtt::start(config.mqtt, &device_manager.event_channel());
|
||||
|
||||
if let Some(devices) = config.devices {
|
||||
for device in devices.get(&lua, &mqtt_client).await? {
|
||||
if let Some(modules) = config.modules {
|
||||
for device in modules.setup(&lua, &mqtt_client).await? {
|
||||
device_manager.add(device).await;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use std::fs::{self, File};
|
||||
use std::io::Write;
|
||||
|
||||
use automation::config::{Config, Devices, FulfillmentConfig};
|
||||
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 += &Devices::generate_full().expect("Devices 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";
|
||||
|
||||
@@ -35,10 +35,10 @@ pub struct FulfillmentConfig {
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Devices(mlua::Value);
|
||||
pub struct Modules(mlua::Value);
|
||||
|
||||
impl Devices {
|
||||
pub async fn get(
|
||||
impl Modules {
|
||||
pub async fn setup(
|
||||
self,
|
||||
lua: &mlua::Lua,
|
||||
client: &WrappedAsyncClient,
|
||||
@@ -60,17 +60,22 @@ 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) => {
|
||||
queue.push_back(f.call_async(client.clone()).await?);
|
||||
mlua::Value::Table(table) => queue.push_back(table),
|
||||
mlua::Value::UserData(_)
|
||||
if let Ok(device) = Box::from_lua(value.clone(), lua) =>
|
||||
{
|
||||
devices.push(device);
|
||||
}
|
||||
_ => Err(mlua::Error::runtime(format!(
|
||||
"Expected a device, table, or function, instead found: {}",
|
||||
value.type_name()
|
||||
)))?,
|
||||
mlua::Value::Function(f) if name == "setup" => {
|
||||
let value: mlua::Value = f.call_async(client.clone()).await?;
|
||||
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 Modules {
|
||||
fn from_lua(value: mlua::Value, _lua: &mlua::Lua) -> mlua::Result<Self> {
|
||||
Ok(Devices(value))
|
||||
Ok(Modules(value))
|
||||
}
|
||||
}
|
||||
|
||||
impl Typed for Devices {
|
||||
impl Typed for Modules {
|
||||
fn type_name() -> String {
|
||||
"Devices".into()
|
||||
"Modules".into()
|
||||
}
|
||||
|
||||
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 SetupFunction fun(mqtt_client: {client_type}): SetupTable?
|
||||
---@alias SetupTable (DeviceInterface | {{ setup: SetupFunction? }} | SetupTable)[]
|
||||
---@alias {type_name} SetupFunction | SetupTable
|
||||
"#,
|
||||
))
|
||||
}
|
||||
}
|
||||
@@ -103,7 +112,7 @@ impl Typed for Devices {
|
||||
pub struct Config {
|
||||
pub fulfillment: FulfillmentConfig,
|
||||
#[device_config(from_lua, default)]
|
||||
pub devices: Option<Devices>,
|
||||
pub modules: Option<Modules>,
|
||||
#[device_config(from_lua)]
|
||||
pub mqtt: MqttConfig,
|
||||
#[device_config(from_lua, default)]
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
#![feature(if_let_guard)]
|
||||
|
||||
pub mod config;
|
||||
pub mod schedule;
|
||||
pub mod secret;
|
||||
|
||||
Reference in New Issue
Block a user