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