Compare commits

...

1 Commits

Author SHA1 Message Date
4f372aa9f0
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
2024-11-30 06:35:36 +01:00
5 changed files with 70 additions and 55 deletions

View File

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

View File

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