feat(config)!: Move util out of global automation table into separate module

Move `automation.util` into a separate module called `utils`.
This commit is contained in:
2025-09-04 03:50:07 +02:00
parent e626caad8a
commit 5aebab28ed
2 changed files with 16 additions and 15 deletions

View File

@@ -1,10 +1,11 @@
local automation = require("automation")
local utils = require("utils")
print(_VERSION)
local host = automation.util.get_hostname()
local host = utils.get_hostname()
print("Running @" .. host)
local debug, value = pcall(automation.util.get_env, "DEBUG")
local debug, value = pcall(utils.get_env, "DEBUG")
if debug and value ~= "true" then
debug = false
end
@@ -26,12 +27,12 @@ local mqtt_client = automation.new_mqtt_client({
port = 8883,
client_name = "automation-" .. host,
username = "mqtt",
password = automation.util.get_env("MQTT_PASSWORD"),
password = utils.get_env("MQTT_PASSWORD"),
tls = host == "zeus" or host == "hephaestus",
})
local ntfy = Ntfy.new({
topic = automation.util.get_env("NTFY_TOPIC"),
topic = utils.get_env("NTFY_TOPIC"),
})
automation.device_manager:add(ntfy)
@@ -106,7 +107,7 @@ end)
on_presence:add(function(presence)
mqtt_client:send_message(mqtt_automation("debug") .. "/presence", {
state = presence,
updated = automation.util.get_epoch(),
updated = utils.get_epoch(),
})
end)
@@ -140,12 +141,12 @@ automation.device_manager:add(LightSensor.new({
on_light:add(function(light)
mqtt_client:send_message(mqtt_automation("debug") .. "/darkness", {
state = not light,
updated = automation.util.get_epoch(),
updated = utils.get_epoch(),
})
end)
local hue_ip = "10.0.0.102"
local hue_token = automation.util.get_env("HUE_TOKEN")
local hue_token = utils.get_env("HUE_TOKEN")
local hue_bridge = HueBridge.new({
identifier = "hue_bridge",
@@ -516,7 +517,7 @@ setmetatable(frontdoor_presence, {
if not presence_system:overall_presence() then
mqtt_client:send_message(mqtt_automation("presence/contact/frontdoor"), {
state = true,
updated = automation.util.get_epoch(),
updated = utils.get_epoch(),
})
end
else

View File

@@ -131,28 +131,28 @@ async fn app() -> anyhow::Result<()> {
automation.set("new_mqtt_client", new_mqtt_client)?;
automation.set("device_manager", device_manager.clone())?;
lua.globals().set("automation", automation)?;
let util = lua.create_table()?;
let utils = lua.create_table()?;
let get_env = lua.create_function(|_lua, name: String| {
std::env::var(name).map_err(mlua::ExternalError::into_lua_err)
})?;
util.set("get_env", get_env)?;
utils.set("get_env", get_env)?;
let get_hostname = lua.create_function(|_lua, ()| {
hostname::get()
.map(|name| name.to_str().unwrap_or("unknown").to_owned())
.map_err(mlua::ExternalError::into_lua_err)
})?;
util.set("get_hostname", get_hostname)?;
utils.set("get_hostname", get_hostname)?;
let get_epoch = lua.create_function(|_lua, ()| {
Ok(SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("Time is after UNIX EPOCH")
.as_millis())
})?;
util.set("get_epoch", get_epoch)?;
automation.set("util", util)?;
utils.set("get_epoch", get_epoch)?;
lua.register_module("automation", automation)?;
lua.register_module("utils", utils)?;
automation_devices::register_with_lua(&lua)?;
helpers::register_with_lua(&lua)?;