Converted presence notification into lua callback
This commit is contained in:
@@ -9,7 +9,7 @@ use serde_repr::*;
|
|||||||
use tracing::{error, trace, warn};
|
use tracing::{error, trace, warn};
|
||||||
|
|
||||||
use crate::device::{Device, LuaDeviceCreate};
|
use crate::device::{Device, LuaDeviceCreate};
|
||||||
use crate::event::{self, Event, EventChannel, OnNotification, OnPresence};
|
use crate::event::{self, EventChannel, OnNotification};
|
||||||
use crate::lua::traits::AddAdditionalMethods;
|
use crate::lua::traits::AddAdditionalMethods;
|
||||||
|
|
||||||
#[derive(Debug, Serialize_repr, Deserialize, Clone, Copy)]
|
#[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]
|
#[async_trait]
|
||||||
impl OnNotification for Ntfy {
|
impl OnNotification for Ntfy {
|
||||||
async fn on_notification(&self, notification: Notification) {
|
async fn on_notification(&self, notification: Notification) {
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ use rumqttc::Publish;
|
|||||||
use tokio::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard};
|
use tokio::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard};
|
||||||
use tracing::{debug, trace, warn};
|
use tracing::{debug, trace, warn};
|
||||||
|
|
||||||
|
use crate::action_callback::ActionCallback;
|
||||||
use crate::config::MqttDeviceConfig;
|
use crate::config::MqttDeviceConfig;
|
||||||
use crate::device::{Device, LuaDeviceCreate};
|
use crate::device::{Device, LuaDeviceCreate};
|
||||||
use crate::event::{self, Event, EventChannel, OnMqtt};
|
use crate::event::{self, Event, EventChannel, OnMqtt};
|
||||||
@@ -19,6 +20,10 @@ pub struct Config {
|
|||||||
pub mqtt: MqttDeviceConfig,
|
pub mqtt: MqttDeviceConfig,
|
||||||
#[device_config(from_lua, rename("event_channel"), with(|ec: EventChannel| ec.get_tx()))]
|
#[device_config(from_lua, rename("event_channel"), with(|ec: EventChannel| ec.get_tx()))]
|
||||||
pub tx: event::Sender,
|
pub tx: event::Sender,
|
||||||
|
|
||||||
|
#[device_config(from_lua, default)]
|
||||||
|
pub callback: ActionCallback<Presence, bool>,
|
||||||
|
|
||||||
#[device_config(from_lua)]
|
#[device_config(from_lua)]
|
||||||
pub client: WrappedAsyncClient,
|
pub client: WrappedAsyncClient,
|
||||||
}
|
}
|
||||||
@@ -123,6 +128,8 @@ impl OnMqtt for Presence {
|
|||||||
{
|
{
|
||||||
warn!("There are no receivers on the event channel");
|
warn!("There are no receivers on the event channel");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.config.callback.call(self, &overall_presence).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
19
config.lua
19
config.lua
@@ -39,6 +39,25 @@ automation.device_manager:add(Presence.new({
|
|||||||
topic = mqtt_automation("presence/+/#"),
|
topic = mqtt_automation("presence/+/#"),
|
||||||
client = mqtt_client,
|
client = mqtt_client,
|
||||||
event_channel = automation.device_manager:event_channel(),
|
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({
|
automation.device_manager:add(DebugBridge.new({
|
||||||
|
|||||||
Reference in New Issue
Block a user