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,30 +34,68 @@ local ntfy = Ntfy.new({
}) })
automation.device_manager:add(ntfy) automation.device_manager:add(ntfy)
local on_presence = {
add = function(self, f)
self[#self + 1] = f
end,
}
automation.device_manager:add(Presence.new({ automation.device_manager:add(Presence.new({
topic = mqtt_automation("presence/+/#"), topic = mqtt_automation("presence/+/#"),
client = mqtt_client, client = mqtt_client,
event_channel = automation.device_manager:event_channel(), event_channel = automation.device_manager:event_channel(),
callback = function(_, presence) callback = function(_, presence)
ntfy:send_notification({ for _, f in ipairs(on_presence) do
title = "Presence", if type(f) == "function" then
message = presence and "Home" or "Away", f(presence)
tags = { "house" }, end
priority = "low", end
actions = {
{
action = "broadcast",
extras = {
cmd = "presence",
state = presence and "0" or "1",
},
label = presence and "Set away" or "Set home",
clear = true,
},
},
})
end, end,
})) }))
on_presence:add(function(presence)
ntfy:send_notification({
title = "Presence",
message = presence and "Home" or "Away",
tags = { "house" },
priority = "low",
actions = {
{
action = "broadcast",
extras = {
cmd = "presence",
state = presence and "0" or "1",
},
label = presence and "Set away" or "Set home",
clear = true,
},
},
})
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({ automation.device_manager:add(DebugBridge.new({
identifier = "debug_bridge", identifier = "debug_bridge",
@@ -78,6 +116,9 @@ local hue_bridge = HueBridge.new({
}, },
}) })
automation.device_manager:add(hue_bridge) automation.device_manager:add(hue_bridge)
on_light:add(function(light)
hue_bridge:set_flag("darkness", not light)
end)
local kitchen_lights = HueGroup.new({ local kitchen_lights = HueGroup.new({
identifier = "kitchen_lights", identifier = "kitchen_lights",
@@ -120,21 +161,6 @@ automation.device_manager:add(HueSwitch.new({
end, 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({ automation.device_manager:add(WakeOnLAN.new({
name = "Zeus", name = "Zeus",
room = "Living Room", room = "Living Room",