Presence and light sensor call all function in array

This commit is contained in:
2025-08-31 21:53:35 +02:00
parent 9616017c8f
commit 1530875045

View File

@@ -34,11 +34,24 @@ local ntfy = Ntfy.new({
})
automation.device_manager:add(ntfy)
local on_presence = {
add = function(self, f)
self[#self + 1] = f
end,
}
automation.device_manager:add(Presence.new({
topic = mqtt_automation("presence/+/#"),
client = mqtt_client,
event_channel = automation.device_manager:event_channel(),
callback = function(_, presence)
for _, f in ipairs(on_presence) do
if type(f) == "function" then
f(presence)
end
end
end,
}))
on_presence:add(function(presence)
ntfy:send_notification({
title = "Presence",
message = presence and "Home" or "Away",
@@ -56,8 +69,33 @@ automation.device_manager:add(Presence.new({
},
},
})
end)
local on_light = {
add = function(self, f)
self[#self + 1] = f
end,
}
automation.device_manager:add(LightSensor.new({
identifier = "living_light_sensor",
topic = mqtt_z2m("living/light"),
client = mqtt_client,
min = 22000,
max = 23500,
callback = function(_, light)
for _, f in ipairs(on_light) do
if type(f) == "function" then
f(light)
end
end
end,
}))
on_light:add(function(light)
mqtt_client:send_message(mqtt_automation("debug") .. "/darkness", {
state = not light,
updated = automation.util.get_epoch(),
})
end)
automation.device_manager:add(DebugBridge.new({
identifier = "debug_bridge",
@@ -78,6 +116,9 @@ local hue_bridge = HueBridge.new({
},
})
automation.device_manager:add(hue_bridge)
on_light:add(function(light)
hue_bridge:set_flag("darkness", not light)
end)
local kitchen_lights = HueGroup.new({
identifier = "kitchen_lights",
@@ -120,21 +161,6 @@ automation.device_manager:add(HueSwitch.new({
end,
}))
automation.device_manager:add(LightSensor.new({
identifier = "living_light_sensor",
topic = mqtt_z2m("living/light"),
client = mqtt_client,
min = 22000,
max = 23500,
callback = function(_, light)
hue_bridge:set_flag("darkness", not light)
mqtt_client:send_message(mqtt_automation("debug") .. "/darkness", {
state = not light,
updated = automation.util.get_epoch(),
})
end,
}))
automation.device_manager:add(WakeOnLAN.new({
name = "Zeus",
room = "Living Room",