Send laundy notification from lua

This commit is contained in:
2025-08-31 00:37:43 +02:00
parent 64c7d950c5
commit 2d9e3d26f2
2 changed files with 19 additions and 21 deletions

View File

@@ -1,16 +1,16 @@
use std::sync::Arc; use std::sync::Arc;
use async_trait::async_trait; use async_trait::async_trait;
use automation_lib::action_callback::ActionCallback;
use automation_lib::config::MqttDeviceConfig; use automation_lib::config::MqttDeviceConfig;
use automation_lib::device::{Device, LuaDeviceCreate}; 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::messages::PowerMessage;
use automation_lib::mqtt::WrappedAsyncClient; use automation_lib::mqtt::WrappedAsyncClient;
use automation_lib::ntfy::{Notification, Priority};
use automation_macro::{LuaDevice, LuaDeviceConfig}; use automation_macro::{LuaDevice, LuaDeviceConfig};
use rumqttc::Publish; use rumqttc::Publish;
use tokio::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard}; use tokio::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard};
use tracing::{debug, error, trace, warn}; use tracing::{debug, error, trace};
#[derive(Debug, Clone, LuaDeviceConfig)] #[derive(Debug, Clone, LuaDeviceConfig)]
pub struct Config { pub struct Config {
@@ -21,6 +21,10 @@ pub struct Config {
pub threshold: f32, pub threshold: f32,
#[device_config(rename("event_channel"), from_lua, with(|ec: EventChannel| ec.get_tx()))] #[device_config(rename("event_channel"), from_lua, with(|ec: EventChannel| ec.get_tx()))]
pub tx: event::Sender, pub tx: event::Sender,
#[device_config(from_lua, default)]
pub done_callback: ActionCallback<Washer, ()>,
#[device_config(from_lua)] #[device_config(from_lua)]
pub client: WrappedAsyncClient, 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 { if power < self.config.threshold && self.state().await.running >= HYSTERESIS {
// The washer is done running // The washer is done running
debug!( debug!(
@@ -108,21 +110,8 @@ impl OnMqtt for Washer {
); );
self.state_mut().await.running = 0; 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 self.config.done_callback.call(self, &()).await;
.config
.tx
.send(Event::Ntfy(notification))
.await
.is_err()
{
warn!("There are no receivers on the event channel");
}
} else if power < self.config.threshold { } else if power < self.config.threshold {
// Prevent false positives // Prevent false positives
self.state_mut().await.running = 0; self.state_mut().await.running = 0;

View File

@@ -29,10 +29,11 @@ local mqtt_client = automation.new_mqtt_client({
tls = host == "zeus" or host == "hephaestus", tls = host == "zeus" or host == "hephaestus",
}) })
automation.device_manager:add(Ntfy.new({ local ntfy = Ntfy.new({
topic = automation.util.get_env("NTFY_TOPIC"), topic = automation.util.get_env("NTFY_TOPIC"),
event_channel = automation.device_manager:event_channel(), event_channel = automation.device_manager:event_channel(),
})) })
automation.device_manager:add(ntfy)
automation.device_manager:add(Presence.new({ automation.device_manager:add(Presence.new({
topic = mqtt_automation("presence/+/#"), topic = mqtt_automation("presence/+/#"),
@@ -232,6 +233,14 @@ automation.device_manager:add(Washer.new({
client = mqtt_client, client = mqtt_client,
threshold = 1, threshold = 1,
event_channel = automation.device_manager:event_channel(), 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({ automation.device_manager:add(OutletOnOff.new({