diff --git a/automation_lib/src/ntfy.rs b/automation_lib/src/ntfy.rs index 443f5f9..0bdb64b 100644 --- a/automation_lib/src/ntfy.rs +++ b/automation_lib/src/ntfy.rs @@ -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) { diff --git a/automation_lib/src/presence.rs b/automation_lib/src/presence.rs index b246c82..8a65535 100644 --- a/automation_lib/src/presence.rs +++ b/automation_lib/src/presence.rs @@ -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, + #[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; } } } diff --git a/config.lua b/config.lua index 29c208a..15a2425 100644 --- a/config.lua +++ b/config.lua @@ -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({