Converted presence notification into lua callback
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user