Compare commits
2 Commits
7db628709a
...
6c7a774d7e
| Author | SHA1 | Date | |
|---|---|---|---|
|
6c7a774d7e
|
|||
|
a6c19eb9b4
|
11
Cargo.lock
generated
11
Cargo.lock
generated
@@ -555,7 +555,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "778e2ac28f6c47af28e4907f13ffd1e1ddbd400980a9abd7c8df189bf578a5ad"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"windows-sys 0.59.0",
|
||||
"windows-sys 0.52.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -1163,17 +1163,16 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "lua_typed"
|
||||
version = "0.1.0"
|
||||
source = "git+https://git.huizinga.dev/Dreaded_X/lua_typed#f6a684291432aae2ef7109712882e7e3ed758d08"
|
||||
source = "git+https://git.huizinga.dev/Dreaded_X/lua_typed#3d29c9dd143737c8bffe4bacae8e701de3c6ee10"
|
||||
dependencies = [
|
||||
"eui48",
|
||||
"lua_typed_macro",
|
||||
"mlua",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lua_typed_macro"
|
||||
version = "0.1.0"
|
||||
source = "git+https://git.huizinga.dev/Dreaded_X/lua_typed#f6a684291432aae2ef7109712882e7e3ed758d08"
|
||||
source = "git+https://git.huizinga.dev/Dreaded_X/lua_typed#3d29c9dd143737c8bffe4bacae8e701de3c6ee10"
|
||||
dependencies = [
|
||||
"convert_case",
|
||||
"itertools",
|
||||
@@ -1568,7 +1567,7 @@ dependencies = [
|
||||
"once_cell",
|
||||
"socket2",
|
||||
"tracing",
|
||||
"windows-sys 0.59.0",
|
||||
"windows-sys 0.52.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -1745,7 +1744,7 @@ dependencies = [
|
||||
"errno",
|
||||
"libc",
|
||||
"linux-raw-sys",
|
||||
"windows-sys 0.59.0",
|
||||
"windows-sys 0.52.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
--- @type SetupTable
|
||||
--- @type Module
|
||||
return {
|
||||
require("config.rooms.bathroom"),
|
||||
require("config.rooms.bedroom"),
|
||||
|
||||
@@ -6,9 +6,9 @@ local devices
|
||||
---@class Action
|
||||
---@field action
|
||||
---| "broadcast"
|
||||
---@field extras table<string, string>?
|
||||
---@field extras (table<string, string>)?
|
||||
---@field label string
|
||||
---@field clear boolean?
|
||||
---@field clear (boolean)?
|
||||
local Action
|
||||
|
||||
---@class AirFilter: DeviceInterface, OnOffInterface
|
||||
@@ -20,49 +20,49 @@ function devices.AirFilter.new(config) end
|
||||
|
||||
---@class AirFilterConfig
|
||||
---@field name string
|
||||
---@field room string?
|
||||
---@field room (string)?
|
||||
---@field url string
|
||||
local AirFilterConfig
|
||||
|
||||
---@class ConfigLightLightStateBrightness
|
||||
---@field name string
|
||||
---@field room string?
|
||||
---@field room (string)?
|
||||
---@field topic string
|
||||
---@field callback fun(_: LightBrightness, _: LightStateBrightness) | fun(_: LightBrightness, _: LightStateBrightness)[]?
|
||||
---@field client AsyncClient?
|
||||
---@field callback (fun(_: LightBrightness, _: LightStateBrightness) | fun(_: LightBrightness, _: LightStateBrightness)[])?
|
||||
---@field client (AsyncClient)?
|
||||
local ConfigLightLightStateBrightness
|
||||
|
||||
---@class ConfigLightLightStateColorTemperature
|
||||
---@field name string
|
||||
---@field room string?
|
||||
---@field room (string)?
|
||||
---@field topic string
|
||||
---@field callback fun(_: LightColorTemperature, _: LightStateColorTemperature) | fun(_: LightColorTemperature, _: LightStateColorTemperature)[]?
|
||||
---@field client AsyncClient?
|
||||
---@field callback (fun(_: LightColorTemperature, _: LightStateColorTemperature) | fun(_: LightColorTemperature, _: LightStateColorTemperature)[])?
|
||||
---@field client (AsyncClient)?
|
||||
local ConfigLightLightStateColorTemperature
|
||||
|
||||
---@class ConfigLightLightStateOnOff
|
||||
---@field name string
|
||||
---@field room string?
|
||||
---@field room (string)?
|
||||
---@field topic string
|
||||
---@field callback fun(_: LightOnOff, _: LightStateOnOff) | fun(_: LightOnOff, _: LightStateOnOff)[]?
|
||||
---@field client AsyncClient?
|
||||
---@field callback (fun(_: LightOnOff, _: LightStateOnOff) | fun(_: LightOnOff, _: LightStateOnOff)[])?
|
||||
---@field client (AsyncClient)?
|
||||
local ConfigLightLightStateOnOff
|
||||
|
||||
---@class ConfigOutletOutletStateOnOff
|
||||
---@field name string
|
||||
---@field room string?
|
||||
---@field room (string)?
|
||||
---@field topic string
|
||||
---@field outlet_type OutletType?
|
||||
---@field callback fun(_: OutletOnOff, _: OutletStateOnOff) | fun(_: OutletOnOff, _: OutletStateOnOff)[]?
|
||||
---@field outlet_type (OutletType)?
|
||||
---@field callback (fun(_: OutletOnOff, _: OutletStateOnOff) | fun(_: OutletOnOff, _: OutletStateOnOff)[])?
|
||||
---@field client AsyncClient
|
||||
local ConfigOutletOutletStateOnOff
|
||||
|
||||
---@class ConfigOutletOutletStatePower
|
||||
---@field name string
|
||||
---@field room string?
|
||||
---@field room (string)?
|
||||
---@field topic string
|
||||
---@field outlet_type OutletType?
|
||||
---@field callback fun(_: OutletPower, _: OutletStatePower) | fun(_: OutletPower, _: OutletStatePower)[]?
|
||||
---@field outlet_type (OutletType)?
|
||||
---@field callback (fun(_: OutletPower, _: OutletStatePower) | fun(_: OutletPower, _: OutletStatePower)[])?
|
||||
---@field client AsyncClient
|
||||
local ConfigOutletOutletStatePower
|
||||
|
||||
@@ -75,12 +75,12 @@ function devices.ContactSensor.new(config) end
|
||||
|
||||
---@class ContactSensorConfig
|
||||
---@field name string
|
||||
---@field room string?
|
||||
---@field room (string)?
|
||||
---@field topic string
|
||||
---@field sensor_type SensorType?
|
||||
---@field callback fun(_: ContactSensor, _: boolean) | fun(_: ContactSensor, _: boolean)[]?
|
||||
---@field battery_callback fun(_: ContactSensor, _: number) | fun(_: ContactSensor, _: number)[]?
|
||||
---@field client AsyncClient?
|
||||
---@field sensor_type (SensorType)?
|
||||
---@field callback (fun(_: ContactSensor, _: boolean) | fun(_: ContactSensor, _: boolean)[])?
|
||||
---@field battery_callback (fun(_: ContactSensor, _: number) | fun(_: ContactSensor, _: number)[])?
|
||||
---@field client (AsyncClient)?
|
||||
local ContactSensorConfig
|
||||
|
||||
---@alias Flag
|
||||
@@ -134,14 +134,14 @@ function devices.HueSwitch.new(config) end
|
||||
|
||||
---@class HueSwitchConfig
|
||||
---@field name string
|
||||
---@field room string?
|
||||
---@field room (string)?
|
||||
---@field topic string
|
||||
---@field client AsyncClient
|
||||
---@field left_callback fun(_: HueSwitch) | fun(_: HueSwitch)[]?
|
||||
---@field right_callback fun(_: HueSwitch) | fun(_: HueSwitch)[]?
|
||||
---@field left_hold_callback fun(_: HueSwitch) | fun(_: HueSwitch)[]?
|
||||
---@field right_hold_callback fun(_: HueSwitch) | fun(_: HueSwitch)[]?
|
||||
---@field battery_callback fun(_: HueSwitch, _: number) | fun(_: HueSwitch, _: number)[]?
|
||||
---@field left_callback (fun(_: HueSwitch) | fun(_: HueSwitch)[])?
|
||||
---@field right_callback (fun(_: HueSwitch) | fun(_: HueSwitch)[])?
|
||||
---@field left_hold_callback (fun(_: HueSwitch) | fun(_: HueSwitch)[])?
|
||||
---@field right_hold_callback (fun(_: HueSwitch) | fun(_: HueSwitch)[])?
|
||||
---@field battery_callback (fun(_: HueSwitch, _: number) | fun(_: HueSwitch, _: number)[])?
|
||||
local HueSwitchConfig
|
||||
|
||||
---@class IkeaRemote: DeviceInterface
|
||||
@@ -153,12 +153,12 @@ function devices.IkeaRemote.new(config) end
|
||||
|
||||
---@class IkeaRemoteConfig
|
||||
---@field name string
|
||||
---@field room string?
|
||||
---@field single_button boolean?
|
||||
---@field room (string)?
|
||||
---@field single_button (boolean)?
|
||||
---@field topic string
|
||||
---@field client AsyncClient
|
||||
---@field callback fun(_: IkeaRemote, _: boolean) | fun(_: IkeaRemote, _: boolean)[]?
|
||||
---@field battery_callback fun(_: IkeaRemote, _: number) | fun(_: IkeaRemote, _: number)[]?
|
||||
---@field callback (fun(_: IkeaRemote, _: boolean) | fun(_: IkeaRemote, _: boolean)[])?
|
||||
---@field battery_callback (fun(_: IkeaRemote, _: number) | fun(_: IkeaRemote, _: number)[])?
|
||||
local IkeaRemoteConfig
|
||||
|
||||
---@class KasaOutlet: DeviceInterface, OnOffInterface
|
||||
@@ -206,7 +206,7 @@ function devices.LightSensor.new(config) end
|
||||
---@field topic string
|
||||
---@field min integer
|
||||
---@field max integer
|
||||
---@field callback fun(_: LightSensor, _: boolean) | fun(_: LightSensor, _: boolean)[]?
|
||||
---@field callback (fun(_: LightSensor, _: boolean) | fun(_: LightSensor, _: boolean)[])?
|
||||
---@field client AsyncClient
|
||||
local LightSensorConfig
|
||||
|
||||
@@ -227,10 +227,10 @@ local LightStateOnOff
|
||||
|
||||
---@class Notification
|
||||
---@field title string
|
||||
---@field message string?
|
||||
---@field tags string[]?
|
||||
---@field priority Priority?
|
||||
---@field actions Action[]?
|
||||
---@field message (string)?
|
||||
---@field tags ((string)[])?
|
||||
---@field priority (Priority)?
|
||||
---@field actions ((Action)[])?
|
||||
local Notification
|
||||
|
||||
---@class Ntfy: DeviceInterface
|
||||
@@ -244,7 +244,7 @@ function devices.Ntfy.new(config) end
|
||||
function Ntfy:send_notification(notification) end
|
||||
|
||||
---@class NtfyConfig
|
||||
---@field url string?
|
||||
---@field url (string)?
|
||||
---@field topic string
|
||||
local NtfyConfig
|
||||
|
||||
@@ -287,7 +287,7 @@ function Presence:overall_presence() end
|
||||
|
||||
---@class PresenceConfig
|
||||
---@field topic string
|
||||
---@field callback fun(_: Presence, _: boolean) | fun(_: Presence, _: boolean)[]?
|
||||
---@field callback (fun(_: Presence, _: boolean) | fun(_: Presence, _: boolean)[])?
|
||||
---@field client AsyncClient
|
||||
local PresenceConfig
|
||||
|
||||
@@ -321,16 +321,16 @@ function devices.Washer.new(config) end
|
||||
---@field identifier string
|
||||
---@field topic string
|
||||
---@field threshold number
|
||||
---@field done_callback fun(_: Washer) | fun(_: Washer)[]?
|
||||
---@field done_callback (fun(_: Washer) | fun(_: Washer)[])?
|
||||
---@field client AsyncClient
|
||||
local WasherConfig
|
||||
|
||||
---@class WolConfig
|
||||
---@field name string
|
||||
---@field room string?
|
||||
---@field room (string)?
|
||||
---@field topic string
|
||||
---@field mac_address string
|
||||
---@field broadcast_ip string?
|
||||
---@field broadcast_ip (string)?
|
||||
---@field client AsyncClient
|
||||
local WolConfig
|
||||
|
||||
|
||||
@@ -3,20 +3,22 @@
|
||||
|
||||
---@class FulfillmentConfig
|
||||
---@field openid_url string
|
||||
---@field ip string?
|
||||
---@field port integer?
|
||||
---@field ip (string)?
|
||||
---@field port (integer)?
|
||||
local FulfillmentConfig
|
||||
|
||||
---@class Config
|
||||
---@field fulfillment FulfillmentConfig
|
||||
---@field modules Modules?
|
||||
---@field modules (Module)[]
|
||||
---@field mqtt MqttConfig
|
||||
---@field schedule table<string, fun() | fun()[]>?
|
||||
---@field schedule (table<string, fun() | fun()[]>)?
|
||||
local Config
|
||||
|
||||
---@alias SetupFunction fun(mqtt_client: AsyncClient): SetupTable?
|
||||
---@alias SetupTable (DeviceInterface | { setup: SetupFunction? } | SetupTable)[]
|
||||
---@alias Modules SetupFunction | SetupTable
|
||||
---@class Module
|
||||
---@field setup (fun(mqtt_client: AsyncClient): Module | DeviceInterface[] | nil)?
|
||||
---@field devices DeviceInterface[]?
|
||||
---@field [number] Module[]?
|
||||
local Module
|
||||
|
||||
---@class MqttConfig
|
||||
---@field host string
|
||||
@@ -24,7 +26,7 @@ local Config
|
||||
---@field client_name string
|
||||
---@field username string
|
||||
---@field password string
|
||||
---@field tls boolean?
|
||||
---@field tls (boolean)?
|
||||
local MqttConfig
|
||||
|
||||
---@class AsyncClient
|
||||
|
||||
@@ -140,11 +140,10 @@ async fn app() -> anyhow::Result<()> {
|
||||
|
||||
let mqtt_client = mqtt::start(config.mqtt, &device_manager.event_channel());
|
||||
|
||||
if let Some(modules) = config.modules {
|
||||
for device in modules.setup(&lua, &mqtt_client).await? {
|
||||
let resolved = config.modules.resolve(&lua, &mqtt_client).await?;
|
||||
for device in resolved.devices {
|
||||
device_manager.add(device).await;
|
||||
}
|
||||
}
|
||||
|
||||
start_scheduler(config.schedule).await?;
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use std::fs::{self, File};
|
||||
use std::io::Write;
|
||||
|
||||
use automation::config::{Config, FulfillmentConfig, Modules};
|
||||
use automation::config::{Config, FulfillmentConfig, Module as ConfigModule};
|
||||
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 += &Modules::generate_full().expect("Setups should have a definition");
|
||||
output += &ConfigModule::generate_full().expect("Module should have a definition");
|
||||
output += "\n";
|
||||
output += &MqttConfig::generate_full().expect("MqttConfig should have a definition");
|
||||
output += "\n";
|
||||
|
||||
181
src/config.rs
181
src/config.rs
@@ -34,85 +34,160 @@ pub struct FulfillmentConfig {
|
||||
pub port: u16,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct SetupFunction(mlua::Function);
|
||||
|
||||
impl Typed for SetupFunction {
|
||||
fn type_name() -> String {
|
||||
format!(
|
||||
"fun(mqtt_client: {}): {} | DeviceInterface[] | nil",
|
||||
WrappedAsyncClient::type_name(),
|
||||
Module::type_name()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl FromLua for SetupFunction {
|
||||
fn from_lua(value: mlua::Value, lua: &mlua::Lua) -> mlua::Result<Self> {
|
||||
Ok(Self(FromLua::from_lua(value, lua)?))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Modules(mlua::Value);
|
||||
pub struct Module {
|
||||
pub setup: Option<SetupFunction>,
|
||||
pub devices: Vec<Box<dyn Device>>,
|
||||
pub modules: Vec<Module>,
|
||||
}
|
||||
|
||||
impl Modules {
|
||||
pub async fn setup(
|
||||
self,
|
||||
lua: &mlua::Lua,
|
||||
client: &WrappedAsyncClient,
|
||||
) -> mlua::Result<Vec<Box<dyn Device>>> {
|
||||
let mut devices = Vec::new();
|
||||
let initial_table = match self.0 {
|
||||
mlua::Value::Table(table) => table,
|
||||
mlua::Value::Function(f) => f.call_async(client.clone()).await?,
|
||||
_ => Err(mlua::Error::runtime(format!(
|
||||
"Expected table or function, instead found: {}",
|
||||
self.0.type_name()
|
||||
)))?,
|
||||
// TODO: Add option to typed to rename field
|
||||
impl Typed for Module {
|
||||
fn type_name() -> String {
|
||||
"Module".into()
|
||||
}
|
||||
|
||||
fn generate_header() -> Option<String> {
|
||||
Some(format!("---@class {}\n", Self::type_name()))
|
||||
}
|
||||
|
||||
fn generate_members() -> Option<String> {
|
||||
Some(format!(
|
||||
r#"---@field setup {}
|
||||
---@field devices DeviceInterface[]?
|
||||
---@field [number] Module[]?
|
||||
"#,
|
||||
Option::<SetupFunction>::type_name()
|
||||
))
|
||||
}
|
||||
|
||||
fn generate_footer() -> Option<String> {
|
||||
let type_name = <Self as Typed>::type_name();
|
||||
Some(format!("local {type_name}\n"))
|
||||
}
|
||||
}
|
||||
|
||||
impl FromLua for Module {
|
||||
fn from_lua(value: mlua::Value, _lua: &mlua::Lua) -> mlua::Result<Self> {
|
||||
// When calling require it might return a result from the searcher indicating how the
|
||||
// module was found, we want to ignore these entries.
|
||||
// TODO: Find a better solution for this
|
||||
if value.is_string() {
|
||||
return Ok(Default::default());
|
||||
}
|
||||
|
||||
let mlua::Value::Table(table) = value else {
|
||||
return Err(mlua::Error::runtime(format!(
|
||||
"Expected module table, instead found: {}",
|
||||
value.type_name()
|
||||
)));
|
||||
};
|
||||
|
||||
let mut queue: VecDeque<mlua::Table> = [initial_table].into();
|
||||
loop {
|
||||
let Some(table) = queue.pop_front() else {
|
||||
break;
|
||||
};
|
||||
let setup = table.get("setup")?;
|
||||
|
||||
for pair in table.pairs() {
|
||||
let (name, value): (String, _) = pair?;
|
||||
let devices = table.get("devices").unwrap_or_else(|_| Vec::new());
|
||||
let mut modules = Vec::new();
|
||||
|
||||
match value {
|
||||
mlua::Value::Table(table) => queue.push_back(table),
|
||||
mlua::Value::UserData(_)
|
||||
if let Ok(device) = Box::from_lua(value.clone(), lua) =>
|
||||
{
|
||||
devices.push(device);
|
||||
}
|
||||
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());
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
for module in table.sequence_values::<Module>() {
|
||||
modules.push(module?);
|
||||
}
|
||||
|
||||
Ok(Module {
|
||||
setup,
|
||||
devices,
|
||||
modules,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Ok(devices)
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Modules(Vec<Module>);
|
||||
|
||||
impl Typed for Modules {
|
||||
fn type_name() -> String {
|
||||
Vec::<Module>::type_name()
|
||||
}
|
||||
}
|
||||
|
||||
impl FromLua for Modules {
|
||||
fn from_lua(value: mlua::Value, _lua: &mlua::Lua) -> mlua::Result<Self> {
|
||||
Ok(Modules(value))
|
||||
fn from_lua(value: mlua::Value, lua: &mlua::Lua) -> mlua::Result<Self> {
|
||||
Ok(Self(FromLua::from_lua(value, lua)?))
|
||||
}
|
||||
}
|
||||
|
||||
impl Typed for Modules {
|
||||
fn type_name() -> String {
|
||||
"Modules".into()
|
||||
impl Modules {
|
||||
pub async fn resolve(
|
||||
self,
|
||||
lua: &mlua::Lua,
|
||||
client: &WrappedAsyncClient,
|
||||
) -> mlua::Result<Resolved> {
|
||||
let mut modules: VecDeque<_> = self.0.into();
|
||||
|
||||
let mut devices = Vec::new();
|
||||
|
||||
loop {
|
||||
let Some(module) = modules.pop_front() else {
|
||||
break;
|
||||
};
|
||||
|
||||
modules.extend(module.modules);
|
||||
|
||||
if let Some(setup) = module.setup {
|
||||
let result: mlua::Value = setup.0.call_async(client.clone()).await?;
|
||||
|
||||
if result.is_nil() {
|
||||
// We ignore nil results
|
||||
} else if let Ok(d) = <Vec<_> as FromLua>::from_lua(result.clone(), lua)
|
||||
&& !d.is_empty()
|
||||
{
|
||||
// This is a shortcut for the common pattern of setup functions that only
|
||||
// return devices
|
||||
devices.extend(d);
|
||||
} else if let Ok(module) = FromLua::from_lua(result.clone(), lua) {
|
||||
modules.push_back(module);
|
||||
} else {
|
||||
return Err(mlua::Error::runtime(
|
||||
"Setup function returned data in an unexpected format",
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
fn generate_header() -> Option<String> {
|
||||
let type_name = Self::type_name();
|
||||
let client_type = WrappedAsyncClient::type_name();
|
||||
|
||||
Some(format!(
|
||||
r#"---@alias SetupFunction fun(mqtt_client: {client_type}): SetupTable?
|
||||
---@alias SetupTable (DeviceInterface | {{ setup: SetupFunction? }} | SetupTable)[]
|
||||
---@alias {type_name} SetupFunction | SetupTable
|
||||
"#,
|
||||
))
|
||||
devices.extend(module.devices);
|
||||
}
|
||||
|
||||
Ok(Resolved { devices })
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Resolved {
|
||||
pub devices: Vec<Box<dyn Device>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, LuaDeviceConfig, Typed)]
|
||||
pub struct Config {
|
||||
pub fulfillment: FulfillmentConfig,
|
||||
#[device_config(from_lua, default)]
|
||||
pub modules: Option<Modules>,
|
||||
pub modules: Modules,
|
||||
#[device_config(from_lua)]
|
||||
pub mqtt: MqttConfig,
|
||||
#[device_config(from_lua, default)]
|
||||
|
||||
Reference in New Issue
Block a user