From 2d9e3d26f27edff51c3b9f89fbabdce7689e02e9 Mon Sep 17 00:00:00 2001 From: Dreaded_X Date: Sun, 31 Aug 2025 00:37:43 +0200 Subject: [PATCH] Send laundy notification from lua --- automation_devices/src/washer.rs | 27 ++++++++------------------- config.lua | 13 +++++++++++-- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/automation_devices/src/washer.rs b/automation_devices/src/washer.rs index f6d5a91..8d70735 100644 --- a/automation_devices/src/washer.rs +++ b/automation_devices/src/washer.rs @@ -1,16 +1,16 @@ use std::sync::Arc; use async_trait::async_trait; +use automation_lib::action_callback::ActionCallback; use automation_lib::config::MqttDeviceConfig; use automation_lib::device::{Device, LuaDeviceCreate}; -use automation_lib::event::{self, Event, EventChannel, OnMqtt}; +use automation_lib::event::{self, EventChannel, OnMqtt}; use automation_lib::messages::PowerMessage; use automation_lib::mqtt::WrappedAsyncClient; -use automation_lib::ntfy::{Notification, Priority}; use automation_macro::{LuaDevice, LuaDeviceConfig}; use rumqttc::Publish; use tokio::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard}; -use tracing::{debug, error, trace, warn}; +use tracing::{debug, error, trace}; #[derive(Debug, Clone, LuaDeviceConfig)] pub struct Config { @@ -21,6 +21,10 @@ pub struct Config { pub threshold: f32, #[device_config(rename("event_channel"), from_lua, with(|ec: EventChannel| ec.get_tx()))] pub tx: event::Sender, + + #[device_config(from_lua, default)] + pub done_callback: ActionCallback, + #[device_config(from_lua)] pub client: WrappedAsyncClient, } @@ -96,8 +100,6 @@ impl OnMqtt for Washer { } }; - // debug!(id = self.identifier, power, "Washer state update"); - if power < self.config.threshold && self.state().await.running >= HYSTERESIS { // The washer is done running debug!( @@ -108,21 +110,8 @@ impl OnMqtt for Washer { ); self.state_mut().await.running = 0; - let notification = Notification::new() - .set_title("Laundy is done") - .set_message("Don't forget to hang it!") - .add_tag("womans_clothes") - .set_priority(Priority::High); - if self - .config - .tx - .send(Event::Ntfy(notification)) - .await - .is_err() - { - warn!("There are no receivers on the event channel"); - } + self.config.done_callback.call(self, &()).await; } else if power < self.config.threshold { // Prevent false positives self.state_mut().await.running = 0; diff --git a/config.lua b/config.lua index 387881c..29c208a 100644 --- a/config.lua +++ b/config.lua @@ -29,10 +29,11 @@ local mqtt_client = automation.new_mqtt_client({ tls = host == "zeus" or host == "hephaestus", }) -automation.device_manager:add(Ntfy.new({ +local ntfy = Ntfy.new({ topic = automation.util.get_env("NTFY_TOPIC"), event_channel = automation.device_manager:event_channel(), -})) +}) +automation.device_manager:add(ntfy) automation.device_manager:add(Presence.new({ topic = mqtt_automation("presence/+/#"), @@ -232,6 +233,14 @@ automation.device_manager:add(Washer.new({ client = mqtt_client, threshold = 1, event_channel = automation.device_manager:event_channel(), + done_callback = function() + ntfy:send_notification({ + title = "Laundy is done", + message = "Don't forget to hang it!", + tags = { "womans_clothes" }, + priority = "high", + }) + end, })) automation.device_manager:add(OutletOnOff.new({