Converted presence notification into lua callback

This commit is contained in:
2025-08-31 00:54:15 +02:00
parent 2d9e3d26f2
commit 6c9d2c16c1
3 changed files with 27 additions and 37 deletions

View File

@@ -9,7 +9,7 @@ use serde_repr::*;
use tracing::{error, trace, warn};
use crate::device::{Device, LuaDeviceCreate};
use crate::event::{self, Event, EventChannel, OnNotification, OnPresence};
use crate::event::{self, EventChannel, OnNotification};
use crate::lua::traits::AddAdditionalMethods;
#[derive(Debug, Serialize_repr, Deserialize, Clone, Copy)]
@@ -167,42 +167,6 @@ impl Ntfy {
}
}
#[async_trait]
impl OnPresence for Ntfy {
async fn on_presence(&self, presence: bool) {
// Setup extras for the broadcast
let extras = HashMap::from([
("cmd".into(), "presence".into()),
("state".into(), if presence { "0" } else { "1" }.into()),
]);
// Create broadcast action
let action = Action {
action: ActionType::Broadcast { extras },
label: if presence { "Set away" } else { "Set home" }.into(),
clear: Some(true),
};
// Create the notification
let notification = Notification::new()
.set_title("Presence")
.set_message(if presence { "Home" } else { "Away" })
.add_tag("house")
.add_action(action)
.set_priority(Priority::Low);
if self
.config
.tx
.send(Event::Ntfy(notification))
.await
.is_err()
{
warn!("There are no receivers on the event channel");
}
}
}
#[async_trait]
impl OnNotification for Ntfy {
async fn on_notification(&self, notification: Notification) {

View File

@@ -7,6 +7,7 @@ use rumqttc::Publish;
use tokio::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard};
use tracing::{debug, trace, warn};
use crate::action_callback::ActionCallback;
use crate::config::MqttDeviceConfig;
use crate::device::{Device, LuaDeviceCreate};
use crate::event::{self, Event, EventChannel, OnMqtt};
@@ -19,6 +20,10 @@ pub struct Config {
pub mqtt: MqttDeviceConfig,
#[device_config(from_lua, rename("event_channel"), with(|ec: EventChannel| ec.get_tx()))]
pub tx: event::Sender,
#[device_config(from_lua, default)]
pub callback: ActionCallback<Presence, bool>,
#[device_config(from_lua)]
pub client: WrappedAsyncClient,
}
@@ -123,6 +128,8 @@ impl OnMqtt for Presence {
{
warn!("There are no receivers on the event channel");
}
self.config.callback.call(self, &overall_presence).await;
}
}
}

View File

@@ -39,6 +39,25 @@ automation.device_manager:add(Presence.new({
topic = mqtt_automation("presence/+/#"),
client = mqtt_client,
event_channel = automation.device_manager:event_channel(),
callback = 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,
}))
automation.device_manager:add(DebugBridge.new({