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",
|
url = "http://10.0.0.103",
|
||||||
})
|
})
|
||||||
|
|
||||||
|
--- @type SetupFunction
|
||||||
local function create_devs(mqtt_client)
|
local function create_devs(mqtt_client)
|
||||||
on_presence:add(function(presence)
|
on_presence:add(function(presence)
|
||||||
mqtt_client:send_message(mqtt_automation("debug") .. "/presence", {
|
mqtt_client:send_message(mqtt_automation("debug") .. "/presence", {
|
||||||
@@ -749,8 +750,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,
|
||||||
|
|||||||
@@ -9,12 +9,14 @@ local FulfillmentConfig
|
|||||||
|
|
||||||
---@class Config
|
---@class Config
|
||||||
---@field fulfillment FulfillmentConfig
|
---@field fulfillment FulfillmentConfig
|
||||||
---@field devices Devices?
|
---@field modules Modules?
|
||||||
---@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): SetupTable?
|
||||||
|
---@alias SetupTable (DeviceInterface | { setup: SetupFunction? } | SetupTable)[]
|
||||||
|
---@alias Modules SetupFunction | SetupTable
|
||||||
|
|
||||||
---@class MqttConfig
|
---@class MqttConfig
|
||||||
---@field host string
|
---@field host string
|
||||||
|
|||||||
@@ -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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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, Modules};
|
||||||
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 += &Modules::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";
|
||||||
|
|||||||
@@ -35,10 +35,10 @@ pub struct FulfillmentConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
pub struct Devices(mlua::Value);
|
pub struct Modules(mlua::Value);
|
||||||
|
|
||||||
impl Devices {
|
impl Modules {
|
||||||
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 Modules {
|
||||||
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(Modules(value))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Typed for Devices {
|
impl Typed for Modules {
|
||||||
fn type_name() -> String {
|
fn type_name() -> String {
|
||||||
"Devices".into()
|
"Modules".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 SetupFunction fun(mqtt_client: {client_type}): SetupTable?
|
||||||
<Self as Typed>::type_name(),
|
---@alias SetupTable (DeviceInterface | {{ setup: SetupFunction? }} | SetupTable)[]
|
||||||
<WrappedAsyncClient as Typed>::type_name()
|
---@alias {type_name} SetupFunction | SetupTable
|
||||||
|
"#,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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<Modules>,
|
||||||
#[device_config(from_lua)]
|
#[device_config(from_lua)]
|
||||||
pub mqtt: MqttConfig,
|
pub mqtt: MqttConfig,
|
||||||
#[device_config(from_lua, default)]
|
#[device_config(from_lua, default)]
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
Reference in New Issue
Block a user