WIP: Made config modular
All checks were successful
Build and deploy / Build application (push) Successful in 3m22s
Build and deploy / Build container (push) Successful in 56s
Build and deploy / Deploy container (push) Has been skipped

This commit is contained in:
Dreaded_X 2024-11-30 06:35:36 +01:00
parent 4bb49a381b
commit 4f372aa9f0
Signed by: Dreaded_X
GPG Key ID: FA5F485356B0D2D4
5 changed files with 70 additions and 55 deletions

View File

@ -1,7 +1,7 @@
FROM gcr.io/distroless/cc-debian12:nonroot FROM gcr.io/distroless/cc-debian12:nonroot
ENV AUTOMATION_CONFIG=/app/config.lua ENV AUTOMATION_CONFIG=/app/config.lua
COPY ./config.lua /app/config.lua COPY ./config /app/config
COPY ./automation /app/automation COPY ./automation /app/automation

9
config/helper.lua Normal file
View File

@ -0,0 +1,9 @@
return {
mqtt_z2m = function(topic)
return "zigbee2mqtt/" .. topic
end,
mqtt_automation = function(topic)
return "automation/" .. topic
end,
}

35
config/kettle.lua Normal file
View File

@ -0,0 +1,35 @@
local h = require("helper")
return function(mqtt_client, debug)
local kettle = IkeaOutlet.new({
outlet_type = "Kettle",
name = "Kettle",
room = "Kitchen",
topic = h.mqtt_z2m("kitchen/kettle"),
client = mqtt_client,
timeout = debug and 5 or 300,
})
automation.device_manager:add(kettle)
local function set_kettle(on)
kettle:set_on(on)
end
automation.device_manager:add(IkeaRemote.new({
name = "Remote",
room = "Bedroom",
client = mqtt_client,
topic = h.mqtt_z2m("bedroom/remote"),
single_button = true,
callback = set_kettle,
}))
automation.device_manager:add(IkeaRemote.new({
name = "Remote",
room = "Kitchen",
client = mqtt_client,
topic = h.mqtt_z2m("kitchen/remote"),
single_button = true,
callback = set_kettle,
}))
end

View File

@ -1,3 +1,4 @@
print(package.path)
print("Hello from lua") print("Hello from lua")
local host = automation.util.get_hostname() local host = automation.util.get_hostname()
@ -8,13 +9,7 @@ if debug and value ~= "true" then
debug = false debug = false
end end
local function mqtt_z2m(topic) local h = require("helper")
return "zigbee2mqtt/" .. topic
end
local function mqtt_automation(topic)
return "automation/" .. topic
end
automation.fulfillment = { automation.fulfillment = {
openid_url = "https://login.huizinga.dev/api/oidc", openid_url = "https://login.huizinga.dev/api/oidc",
@ -37,14 +32,14 @@ automation.device_manager:add(Ntfy.new({
})) }))
automation.device_manager:add(Presence.new({ automation.device_manager:add(Presence.new({
topic = mqtt_automation("presence/+/#"), topic = h.mqtt_automation("presence/+/#"),
client = mqtt_client, client = mqtt_client,
event_channel = automation.device_manager:event_channel(), event_channel = automation.device_manager:event_channel(),
})) }))
automation.device_manager:add(DebugBridge.new({ automation.device_manager:add(DebugBridge.new({
identifier = "debug_bridge", identifier = "debug_bridge",
topic = mqtt_automation("debug"), topic = h.mqtt_automation("debug"),
client = mqtt_client, client = mqtt_client,
})) }))
@ -63,7 +58,7 @@ automation.device_manager:add(HueBridge.new({
automation.device_manager:add(LightSensor.new({ automation.device_manager:add(LightSensor.new({
identifier = "living_light_sensor", identifier = "living_light_sensor",
topic = mqtt_z2m("living/light"), topic = h.mqtt_z2m("living/light"),
client = mqtt_client, client = mqtt_client,
min = 22000, min = 22000,
max = 23500, max = 23500,
@ -73,7 +68,7 @@ automation.device_manager:add(LightSensor.new({
automation.device_manager:add(WakeOnLAN.new({ automation.device_manager:add(WakeOnLAN.new({
name = "Zeus", name = "Zeus",
room = "Living Room", room = "Living Room",
topic = mqtt_automation("appliance/living_room/zeus"), topic = h.mqtt_automation("appliance/living_room/zeus"),
client = mqtt_client, client = mqtt_client,
mac_address = "30:9c:23:60:9c:13", mac_address = "30:9c:23:60:9c:13",
broadcast_ip = "10.0.0.255", broadcast_ip = "10.0.0.255",
@ -88,7 +83,7 @@ automation.device_manager:add(IkeaRemote.new({
name = "Remote", name = "Remote",
room = "Living", room = "Living",
client = mqtt_client, client = mqtt_client,
topic = mqtt_z2m("living/remote"), topic = h.mqtt_z2m("living/remote"),
single_button = true, single_button = true,
callback = function(on) callback = function(on)
if on then if on then
@ -109,49 +104,20 @@ automation.device_manager:add(IkeaRemote.new({
end, end,
})) }))
local kettle = IkeaOutlet.new({ require("kettle")(mqtt_client, debug)
outlet_type = "Kettle",
name = "Kettle",
room = "Kitchen",
topic = mqtt_z2m("kitchen/kettle"),
client = mqtt_client,
timeout = debug and 5 or 300,
})
automation.device_manager:add(kettle)
function set_kettle(on)
kettle:set_on(on)
end
automation.device_manager:add(IkeaRemote.new({
name = "Remote",
room = "Bedroom",
client = mqtt_client,
topic = mqtt_z2m("bedroom/remote"),
single_button = true,
callback = set_kettle,
}))
automation.device_manager:add(IkeaRemote.new({
name = "Remote",
room = "Kitchen",
client = mqtt_client,
topic = mqtt_z2m("kitchen/remote"),
single_button = true,
callback = set_kettle,
}))
automation.device_manager:add(IkeaOutlet.new({ automation.device_manager:add(IkeaOutlet.new({
outlet_type = "Light", outlet_type = "Light",
name = "Light", name = "Light",
room = "Bathroom", room = "Bathroom",
topic = mqtt_z2m("bathroom/light"), topic = h.mqtt_z2m("bathroom/light"),
client = mqtt_client, client = mqtt_client,
timeout = debug and 60 or 45 * 60, timeout = debug and 60 or 45 * 60,
})) }))
automation.device_manager:add(Washer.new({ automation.device_manager:add(Washer.new({
identifier = "bathroom_washer", identifier = "bathroom_washer",
topic = mqtt_z2m("bathroom/washer"), topic = h.mqtt_z2m("bathroom/washer"),
client = mqtt_client, client = mqtt_client,
threshold = 1, threshold = 1,
event_channel = automation.device_manager:event_channel(), event_channel = automation.device_manager:event_channel(),
@ -161,7 +127,7 @@ automation.device_manager:add(IkeaOutlet.new({
outlet_type = "Charger", outlet_type = "Charger",
name = "Charger", name = "Charger",
room = "Workbench", room = "Workbench",
topic = mqtt_z2m("workbench/charger"), topic = h.mqtt_z2m("workbench/charger"),
client = mqtt_client, client = mqtt_client,
timeout = debug and 5 or 20 * 3600, timeout = debug and 5 or 20 * 3600,
})) }))
@ -169,7 +135,7 @@ automation.device_manager:add(IkeaOutlet.new({
automation.device_manager:add(IkeaOutlet.new({ automation.device_manager:add(IkeaOutlet.new({
name = "Outlet", name = "Outlet",
room = "Workbench", room = "Workbench",
topic = mqtt_z2m("workbench/outlet"), topic = h.mqtt_z2m("workbench/outlet"),
client = mqtt_client, client = mqtt_client,
})) }))
@ -187,7 +153,7 @@ automation.device_manager:add(IkeaRemote.new({
name = "Remote", name = "Remote",
room = "Hallway", room = "Hallway",
client = mqtt_client, client = mqtt_client,
topic = mqtt_z2m("hallway/remote"), topic = h.mqtt_z2m("hallway/remote"),
callback = function(on) callback = function(on)
hallway_lights:set_on(on) hallway_lights:set_on(on)
end, end,
@ -195,10 +161,10 @@ automation.device_manager:add(IkeaRemote.new({
automation.device_manager:add(ContactSensor.new({ automation.device_manager:add(ContactSensor.new({
identifier = "hallway_frontdoor", identifier = "hallway_frontdoor",
topic = mqtt_z2m("hallway/frontdoor"), topic = h.mqtt_z2m("hallway/frontdoor"),
client = mqtt_client, client = mqtt_client,
presence = { presence = {
topic = mqtt_automation("presence/contact/frontdoor"), topic = h.mqtt_automation("presence/contact/frontdoor"),
timeout = debug and 10 or 15 * 60, timeout = debug and 10 or 15 * 60,
}, },
trigger = { trigger = {
@ -209,7 +175,7 @@ automation.device_manager:add(ContactSensor.new({
automation.device_manager:add(ContactSensor.new({ automation.device_manager:add(ContactSensor.new({
identifier = "hallway_trash", identifier = "hallway_trash",
topic = mqtt_z2m("hallway/trash"), topic = h.mqtt_z2m("hallway/trash"),
client = mqtt_client, client = mqtt_client,
trigger = { trigger = {
devices = { hallway_lights }, devices = { hallway_lights },

View File

@ -113,10 +113,15 @@ async fn app() -> anyhow::Result<()> {
devices::register_with_lua(&lua)?; devices::register_with_lua(&lua)?;
// TODO: Make this not hardcoded let config_dir = std::env::var("AUTOMATION_CONFIG").unwrap_or("./config".into());
let config_filename = std::env::var("AUTOMATION_CONFIG").unwrap_or("./config.lua".into()); let config_main = Path::new(&config_dir).join("main.lua");
let config_path = Path::new(&config_filename); lua.globals()
match lua.load(config_path).exec_async().await { .get::<mlua::Table>("package")
.unwrap()
.set("path", format!("{}/?.lua;", config_dir))
.unwrap();
match lua.load(config_main).exec_async().await {
Err(error) => { Err(error) => {
println!("{error}"); println!("{error}");
Err(error) Err(error)