Files
automation_rs/config/presence.lua
Dreaded_X 1766a7b175
All checks were successful
Build and deploy / build (push) Successful in 10m15s
Build and deploy / Deploy container (push) Has been skipped
WIP
2025-10-19 07:21:57 +02:00

76 lines
1.5 KiB
Lua

local devices = require("automation:devices")
local helper = require("config.helper")
local ntfy = require("config.ntfy")
local module = {}
--- @class OnPresence
--- @field [integer] fun(presence: boolean)
local callbacks = {}
--- @param callback fun(presence: boolean)
function module.add_callback(callback)
table.insert(callbacks, callback)
end
--- @param device OnOffInterface
function module.turn_off_when_away(device)
module.add_callback(function(presence)
if not presence then
device:set_on(false)
end
end)
end
--- HACK: This is placeholder function until the actual presence device has been created
--- @type fun(): boolean?
function module.overall_presence()
return nil
end
--- @param _ DeviceInterface
--- @param presence boolean
local function callback(_, presence)
for _, f in ipairs(callbacks) do
f(presence)
end
end
--- @type Presence?
module.device = nil
--- @param mqtt_client AsyncClient
function module.setup(mqtt_client)
module.device = devices.Presence.new({
topic = helper.mqtt_automation("presence/+/#"),
client = mqtt_client,
callback = callback,
})
module.add_callback(function(p)
ntfy.device:send_notification({
title = "Presence",
message = p and "Home" or "Away",
tags = { "house" },
priority = "low",
actions = {
{
action = "broadcast",
extras = {
cmd = "presence",
state = p and "0" or "1",
},
label = p and "Set away" or "Set home",
clear = true,
},
},
})
end)
return {
module.device,
}
end
return module