Compare commits
2 Commits
9546585440
...
5947098bfb
| Author | SHA1 | Date | |
|---|---|---|---|
|
5947098bfb
|
|||
|
8a3143a3ea
|
@@ -1,5 +1,6 @@
|
||||
local ntfy = require("config.ntfy")
|
||||
|
||||
--- @class BatteryModule: Module
|
||||
local module = {}
|
||||
|
||||
--- @type {[string]: number}
|
||||
@@ -17,7 +18,7 @@ function module.callback(device, battery)
|
||||
end
|
||||
end
|
||||
|
||||
function module.notify_low_battery()
|
||||
local function notify_low_battery()
|
||||
-- Don't send notifications if there are now devices with low battery
|
||||
if next(low_battery) == nil then
|
||||
print("No devices with low battery")
|
||||
@@ -38,8 +39,9 @@ function module.notify_low_battery()
|
||||
})
|
||||
end
|
||||
|
||||
--- @type Schedule
|
||||
module.schedule = {
|
||||
["0 0 21 */1 * *"] = module.notify_low_battery,
|
||||
["0 0 21 */1 * *"] = notify_low_battery,
|
||||
}
|
||||
|
||||
return module
|
||||
|
||||
@@ -4,6 +4,7 @@ local presence = require("config.presence")
|
||||
local utils = require("automation:utils")
|
||||
local variables = require("automation:variables")
|
||||
|
||||
--- @class DebugModule: Module
|
||||
local module = {}
|
||||
|
||||
if variables.debug == "true" then
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
local debug = require("config.debug")
|
||||
local utils = require("automation:utils")
|
||||
|
||||
--- @class HallwayAutomationModule: Module
|
||||
local module = {}
|
||||
|
||||
local timeout = utils.Timeout.new()
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
local utils = require("automation:utils")
|
||||
|
||||
--- @class HelperModule: Module
|
||||
local module = {}
|
||||
|
||||
--- @param topic string
|
||||
|
||||
@@ -3,6 +3,7 @@ local light = require("config.light")
|
||||
local presence = require("config.presence")
|
||||
local secrets = require("automation:secrets")
|
||||
|
||||
--- @class HueBridgeModule: Module
|
||||
local module = {}
|
||||
|
||||
module.ip = "10.0.0.102"
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
local devices = require("automation:devices")
|
||||
local helper = require("config.helper")
|
||||
|
||||
--- @class LightModule: Module
|
||||
local module = {}
|
||||
|
||||
--- @class OnPresence
|
||||
@@ -34,6 +35,7 @@ function module.setup(mqtt_client)
|
||||
callback = callback,
|
||||
})
|
||||
|
||||
--- @type Module
|
||||
return {
|
||||
module.device,
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
local devices = require("automation:devices")
|
||||
local secrets = require("automation:secrets")
|
||||
|
||||
--- @class NtfyModule: Module
|
||||
local module = {}
|
||||
|
||||
local ntfy_topic = secrets.ntfy_topic
|
||||
@@ -24,6 +25,7 @@ function module.setup()
|
||||
topic = ntfy_topic,
|
||||
})
|
||||
|
||||
--- @type Module
|
||||
return {
|
||||
ntfy,
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ local devices = require("automation:devices")
|
||||
local helper = require("config.helper")
|
||||
local ntfy = require("config.ntfy")
|
||||
|
||||
--- @class PresenceModule: Module
|
||||
local module = {}
|
||||
|
||||
--- @class OnPresence
|
||||
@@ -61,6 +62,7 @@ function module.setup(mqtt_client)
|
||||
})
|
||||
end)
|
||||
|
||||
--- @type Module
|
||||
return {
|
||||
presence,
|
||||
}
|
||||
|
||||
@@ -3,9 +3,9 @@ local devices = require("automation:devices")
|
||||
local helper = require("config.helper")
|
||||
local ntfy = require("config.ntfy")
|
||||
|
||||
--- @type Module
|
||||
local module = {}
|
||||
|
||||
--- @type SetupFunction
|
||||
function module.setup(mqtt_client)
|
||||
local light = devices.LightOnOff.new({
|
||||
name = "Light",
|
||||
@@ -30,6 +30,7 @@ function module.setup(mqtt_client)
|
||||
end,
|
||||
})
|
||||
|
||||
--- @type Module
|
||||
return {
|
||||
light,
|
||||
washer,
|
||||
|
||||
@@ -4,12 +4,12 @@ local helper = require("config.helper")
|
||||
local hue_bridge = require("config.hue_bridge")
|
||||
local windows = require("config.windows")
|
||||
|
||||
--- @type Module
|
||||
local module = {}
|
||||
|
||||
--- @type AirFilter?
|
||||
local air_filter = nil
|
||||
|
||||
--- @type SetupFunction
|
||||
function module.setup(mqtt_client)
|
||||
local lights = devices.HueGroup.new({
|
||||
identifier = "bedroom_lights",
|
||||
@@ -55,6 +55,7 @@ function module.setup(mqtt_client)
|
||||
})
|
||||
windows.add(window)
|
||||
|
||||
--- @type Module
|
||||
return {
|
||||
devices = {
|
||||
lights,
|
||||
|
||||
@@ -4,9 +4,9 @@ local helper = require("config.helper")
|
||||
local presence = require("config.presence")
|
||||
local windows = require("config.windows")
|
||||
|
||||
--- @type Module
|
||||
local module = {}
|
||||
|
||||
--- @type SetupFunction
|
||||
function module.setup(mqtt_client)
|
||||
local light = devices.LightOnOff.new({
|
||||
name = "Light",
|
||||
@@ -25,6 +25,7 @@ function module.setup(mqtt_client)
|
||||
})
|
||||
windows.add(window)
|
||||
|
||||
--- @type Module
|
||||
return {
|
||||
light,
|
||||
window,
|
||||
|
||||
@@ -8,9 +8,9 @@ local presence = require("config.presence")
|
||||
local utils = require("automation:utils")
|
||||
local windows = require("config.windows")
|
||||
|
||||
--- @type Module
|
||||
local module = {}
|
||||
|
||||
--- @type SetupFunction
|
||||
function module.setup(mqtt_client)
|
||||
local main_light = devices.HueGroup.new({
|
||||
identifier = "hallway_main_light",
|
||||
@@ -97,6 +97,7 @@ function module.setup(mqtt_client)
|
||||
windows.add(frontdoor)
|
||||
hallway_automation.set_door(frontdoor)
|
||||
|
||||
--- @type Module
|
||||
return {
|
||||
main_light,
|
||||
storage_light,
|
||||
|
||||
@@ -3,9 +3,9 @@ local devices = require("automation:devices")
|
||||
local helper = require("config.helper")
|
||||
local hue_bridge = require("config.hue_bridge")
|
||||
|
||||
--- @type Module
|
||||
local module = {}
|
||||
|
||||
--- @type SetupFunction
|
||||
function module.setup(mqtt_client)
|
||||
local light = devices.HueGroup.new({
|
||||
identifier = "hallway_top_light",
|
||||
@@ -37,6 +37,7 @@ function module.setup(mqtt_client)
|
||||
battery_callback = battery.callback,
|
||||
})
|
||||
|
||||
--- @type Module
|
||||
return {
|
||||
light,
|
||||
top_switch,
|
||||
|
||||
@@ -4,6 +4,7 @@ local helper = require("config.helper")
|
||||
local hue_bridge = require("config.hue_bridge")
|
||||
local presence = require("config.presence")
|
||||
|
||||
--- @class KitchenModule: Module
|
||||
local module = {}
|
||||
|
||||
--- @type HueGroup?
|
||||
|
||||
@@ -5,9 +5,9 @@ local hue_bridge = require("config.hue_bridge")
|
||||
local presence = require("config.presence")
|
||||
local windows = require("config.windows")
|
||||
|
||||
--- @type Module
|
||||
local module = {}
|
||||
|
||||
--- @type SetupFunction
|
||||
function module.setup(mqtt_client)
|
||||
local lights = devices.HueGroup.new({
|
||||
identifier = "living_lights",
|
||||
@@ -109,6 +109,7 @@ function module.setup(mqtt_client)
|
||||
})
|
||||
windows.add(window)
|
||||
|
||||
--- @type Module
|
||||
return {
|
||||
lights,
|
||||
lights_relax,
|
||||
|
||||
@@ -3,9 +3,9 @@ local devices = require("automation:devices")
|
||||
local helper = require("config.helper")
|
||||
local presence = require("config.presence")
|
||||
|
||||
--- @type Module
|
||||
local module = {}
|
||||
|
||||
--- @type SetupFunction
|
||||
function module.setup(mqtt_client)
|
||||
local light = devices.LightBrightness.new({
|
||||
name = "Light",
|
||||
@@ -31,6 +31,7 @@ function module.setup(mqtt_client)
|
||||
battery_callback = battery.callback,
|
||||
})
|
||||
|
||||
--- @type Module
|
||||
return {
|
||||
light,
|
||||
door,
|
||||
|
||||
@@ -5,9 +5,9 @@ local helper = require("config.helper")
|
||||
local presence = require("config.presence")
|
||||
local utils = require("automation:utils")
|
||||
|
||||
--- @type Module
|
||||
local module = {}
|
||||
|
||||
--- @type SetupFunction
|
||||
function module.setup(mqtt_client)
|
||||
local charger = devices.OutletOnOff.new({
|
||||
name = "Charger",
|
||||
@@ -57,6 +57,7 @@ function module.setup(mqtt_client)
|
||||
battery_callback = battery.callback,
|
||||
})
|
||||
|
||||
--- @type Module
|
||||
return {
|
||||
charger,
|
||||
outlets,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
local ntfy = require("config.ntfy")
|
||||
local presence = require("config.presence")
|
||||
|
||||
--- @class WindowsModule: Module
|
||||
local module = {}
|
||||
|
||||
--- @class OnPresence
|
||||
|
||||
@@ -13,10 +13,14 @@ local FulfillmentConfig
|
||||
---@field mqtt MqttConfig
|
||||
local Config
|
||||
|
||||
---@alias SetupFunction fun(mqtt_client: AsyncClient): Module | DeviceInterface[] | nil
|
||||
|
||||
---@alias Schedule table<string, fun() | fun()[]>
|
||||
|
||||
---@class Module
|
||||
---@field setup (fun(mqtt_client: AsyncClient): Module | DeviceInterface[] | nil)?
|
||||
---@field setup (SetupFunction)?
|
||||
---@field devices (DeviceInterface)[]?
|
||||
---@field schedule table<string, fun() | fun()[]>?
|
||||
---@field schedule Schedule?
|
||||
---@field [number] (Module)[]?
|
||||
local Module
|
||||
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
use std::fs::{self, File};
|
||||
use std::io::Write;
|
||||
|
||||
use automation::config::{Config, FulfillmentConfig, Module as ConfigModule};
|
||||
use automation::config::{
|
||||
Config, FulfillmentConfig, Module as ConfigModule, Schedule, SetupFunction,
|
||||
};
|
||||
use automation_lib::Module;
|
||||
use automation_lib::mqtt::{MqttConfig, WrappedAsyncClient};
|
||||
use lua_typed::Typed;
|
||||
@@ -35,6 +37,10 @@ fn config_definitions() -> String {
|
||||
output += "\n";
|
||||
output += &Config::generate_full().expect("Config should have a definition");
|
||||
output += "\n";
|
||||
output += &SetupFunction::generate_full().expect("SetupFunction should have a definition");
|
||||
output += "\n";
|
||||
output += &Schedule::generate_full().expect("Schedule should have a definition");
|
||||
output += "\n";
|
||||
output += &ConfigModule::generate_full().expect("Module should have a definition");
|
||||
output += "\n";
|
||||
output += &MqttConfig::generate_full().expect("MqttConfig should have a definition");
|
||||
|
||||
@@ -41,11 +41,16 @@ pub struct SetupFunction(mlua::Function);
|
||||
|
||||
impl Typed for SetupFunction {
|
||||
fn type_name() -> String {
|
||||
format!(
|
||||
"fun(mqtt_client: {}): {} | DeviceInterface[] | nil",
|
||||
"SetupFunction".into()
|
||||
}
|
||||
|
||||
fn generate_header() -> Option<String> {
|
||||
Some(format!(
|
||||
"---@alias {} fun(mqtt_client: {}): {} | DeviceInterface[] | nil\n",
|
||||
Self::type_name(),
|
||||
WrappedAsyncClient::type_name(),
|
||||
Module::type_name()
|
||||
)
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,11 +60,34 @@ impl FromLua for SetupFunction {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Schedule(HashMap<String, ActionCallback<()>>);
|
||||
|
||||
impl Typed for Schedule {
|
||||
fn type_name() -> String {
|
||||
"Schedule".into()
|
||||
}
|
||||
|
||||
fn generate_header() -> Option<String> {
|
||||
Some(format!(
|
||||
"---@alias {} {}\n",
|
||||
Self::type_name(),
|
||||
HashMap::<String, ActionCallback<()>>::type_name(),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
impl FromLua for Schedule {
|
||||
fn from_lua(value: mlua::Value, lua: &mlua::Lua) -> mlua::Result<Self> {
|
||||
Ok(Self(FromLua::from_lua(value, lua)?))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Module {
|
||||
pub setup: Option<SetupFunction>,
|
||||
pub devices: Vec<Box<dyn Device>>,
|
||||
pub schedule: HashMap<String, ActionCallback<()>>,
|
||||
pub schedule: Schedule,
|
||||
pub modules: Vec<Module>,
|
||||
}
|
||||
|
||||
@@ -82,7 +110,7 @@ impl Typed for Module {
|
||||
"#,
|
||||
Option::<SetupFunction>::type_name(),
|
||||
Vec::<Box<dyn Device>>::type_name(),
|
||||
HashMap::<String, ActionCallback<()>>::type_name(),
|
||||
Schedule::type_name(),
|
||||
Vec::<Module>::type_name(),
|
||||
))
|
||||
}
|
||||
@@ -181,7 +209,7 @@ impl Modules {
|
||||
}
|
||||
|
||||
devices.extend(module.devices);
|
||||
for (cron, f) in module.schedule {
|
||||
for (cron, f) in module.schedule.0 {
|
||||
scheduler.add_job(cron, f);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user