Compare commits
6 Commits
18f9e20633
...
fe1dba111c
| Author | SHA1 | Date | |
|---|---|---|---|
|
fe1dba111c
|
|||
|
e1d1347b1e
|
|||
|
0436ff57d8
|
|||
|
8d9210247b
|
|||
|
1a5fe54213
|
|||
|
9f44554996
|
@@ -58,6 +58,7 @@ serde_repr = "0.1.10"
|
|||||||
syn = { version = "2.0.60", features = ["extra-traits", "full"] }
|
syn = { version = "2.0.60", features = ["extra-traits", "full"] }
|
||||||
thiserror = "2.0.5"
|
thiserror = "2.0.5"
|
||||||
tokio-cron-scheduler = "0.13.0"
|
tokio-cron-scheduler = "0.13.0"
|
||||||
|
tokio-util = { version = "0.7.11", features = ["full"] }
|
||||||
tracing-subscriber = "0.3.16"
|
tracing-subscriber = "0.3.16"
|
||||||
uuid = "1.8.0"
|
uuid = "1.8.0"
|
||||||
wakey = "0.3.0"
|
wakey = "0.3.0"
|
||||||
|
|||||||
@@ -62,6 +62,7 @@ struct State {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, LuaDevice)]
|
#[derive(Debug, Clone, LuaDevice)]
|
||||||
|
#[traits(OpenClose)]
|
||||||
pub struct ContactSensor {
|
pub struct ContactSensor {
|
||||||
config: Config,
|
config: Config,
|
||||||
state: Arc<RwLock<State>>,
|
state: Arc<RwLock<State>>,
|
||||||
|
|||||||
@@ -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, EventChannel, OnMqtt};
|
use automation_lib::event::{self, Event, 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};
|
use tracing::{debug, error, trace, warn};
|
||||||
|
|
||||||
#[derive(Debug, Clone, LuaDeviceConfig)]
|
#[derive(Debug, Clone, LuaDeviceConfig)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
@@ -21,10 +21,6 @@ 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,
|
||||||
}
|
}
|
||||||
@@ -100,6 +96,8 @@ 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!(
|
||||||
@@ -110,8 +108,21 @@ 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);
|
||||||
|
|
||||||
self.config.done_callback.call(self, &()).await;
|
if self
|
||||||
|
.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;
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ use dyn_clone::DynClone;
|
|||||||
use google_home::traits::OnOff;
|
use google_home::traits::OnOff;
|
||||||
use mlua::ObjectLike;
|
use mlua::ObjectLike;
|
||||||
|
|
||||||
use crate::event::{OnDarkness, OnMqtt, OnPresence};
|
use crate::event::{OnDarkness, OnMqtt, OnNotification, OnPresence};
|
||||||
|
|
||||||
#[async_trait::async_trait]
|
#[async_trait::async_trait]
|
||||||
pub trait LuaDeviceCreate {
|
pub trait LuaDeviceCreate {
|
||||||
@@ -26,6 +26,7 @@ pub trait Device:
|
|||||||
+ Cast<dyn OnMqtt>
|
+ Cast<dyn OnMqtt>
|
||||||
+ Cast<dyn OnPresence>
|
+ Cast<dyn OnPresence>
|
||||||
+ Cast<dyn OnDarkness>
|
+ Cast<dyn OnDarkness>
|
||||||
|
+ Cast<dyn OnNotification>
|
||||||
+ Cast<dyn OnOff>
|
+ Cast<dyn OnOff>
|
||||||
{
|
{
|
||||||
fn get_id(&self) -> String;
|
fn get_id(&self) -> String;
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ use tokio_cron_scheduler::{Job, JobScheduler};
|
|||||||
use tracing::{debug, instrument, trace};
|
use tracing::{debug, instrument, trace};
|
||||||
|
|
||||||
use crate::device::Device;
|
use crate::device::Device;
|
||||||
use crate::event::{Event, EventChannel, OnDarkness, OnMqtt, OnPresence};
|
use crate::event::{Event, EventChannel, OnDarkness, OnMqtt, OnNotification, OnPresence};
|
||||||
|
|
||||||
pub type DeviceMap = HashMap<String, Box<dyn Device>>;
|
pub type DeviceMap = HashMap<String, Box<dyn Device>>;
|
||||||
|
|
||||||
@@ -118,6 +118,22 @@ impl DeviceManager {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
join_all(iter).await;
|
||||||
|
}
|
||||||
|
Event::Ntfy(notification) => {
|
||||||
|
let devices = self.devices.read().await;
|
||||||
|
let iter = devices.iter().map(|(id, device)| {
|
||||||
|
let notification = notification.clone();
|
||||||
|
async move {
|
||||||
|
let device: Option<&dyn OnNotification> = device.cast();
|
||||||
|
if let Some(device) = device {
|
||||||
|
trace!(id, "Handling");
|
||||||
|
device.on_notification(notification).await;
|
||||||
|
trace!(id, "Done");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
join_all(iter).await;
|
join_all(iter).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,11 +3,14 @@ use mlua::FromLua;
|
|||||||
use rumqttc::Publish;
|
use rumqttc::Publish;
|
||||||
use tokio::sync::mpsc;
|
use tokio::sync::mpsc;
|
||||||
|
|
||||||
|
use crate::ntfy::Notification;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum Event {
|
pub enum Event {
|
||||||
MqttMessage(Publish),
|
MqttMessage(Publish),
|
||||||
Darkness(bool),
|
Darkness(bool),
|
||||||
Presence(bool),
|
Presence(bool),
|
||||||
|
Ntfy(Notification),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type Sender = mpsc::Sender<Event>;
|
pub type Sender = mpsc::Sender<Event>;
|
||||||
@@ -45,3 +48,8 @@ pub trait OnPresence: Sync + Send {
|
|||||||
pub trait OnDarkness: Sync + Send {
|
pub trait OnDarkness: Sync + Send {
|
||||||
async fn on_darkness(&self, dark: bool);
|
async fn on_darkness(&self, dark: bool);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
pub trait OnNotification: Sync + Send {
|
||||||
|
async fn on_notification(&self, notification: Notification);
|
||||||
|
}
|
||||||
|
|||||||
@@ -81,9 +81,3 @@ pub trait OpenClose {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl<T> OpenClose for T where T: google_home::traits::OpenClose {}
|
impl<T> OpenClose for T where T: google_home::traits::OpenClose {}
|
||||||
|
|
||||||
pub trait AddAdditionalMethods {
|
|
||||||
fn add_methods<M: mlua::UserDataMethods<Self>>(methods: &mut M)
|
|
||||||
where
|
|
||||||
Self: Sized + 'static;
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -3,18 +3,15 @@ use std::convert::Infallible;
|
|||||||
|
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use automation_macro::{LuaDevice, LuaDeviceConfig};
|
use automation_macro::{LuaDevice, LuaDeviceConfig};
|
||||||
use mlua::LuaSerdeExt;
|
use serde::Serialize;
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
use serde_repr::*;
|
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, EventChannel};
|
use crate::event::{self, Event, EventChannel, OnNotification, OnPresence};
|
||||||
use crate::lua::traits::AddAdditionalMethods;
|
|
||||||
|
|
||||||
#[derive(Debug, Serialize_repr, Deserialize, Clone, Copy)]
|
#[derive(Debug, Serialize_repr, Clone, Copy)]
|
||||||
#[repr(u8)]
|
#[repr(u8)]
|
||||||
#[serde(rename_all = "snake_case")]
|
|
||||||
pub enum Priority {
|
pub enum Priority {
|
||||||
Min = 1,
|
Min = 1,
|
||||||
Low,
|
Low,
|
||||||
@@ -23,7 +20,7 @@ pub enum Priority {
|
|||||||
Max,
|
Max,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
#[derive(Debug, Serialize, Clone)]
|
||||||
#[serde(rename_all = "snake_case", tag = "action")]
|
#[serde(rename_all = "snake_case", tag = "action")]
|
||||||
pub enum ActionType {
|
pub enum ActionType {
|
||||||
Broadcast {
|
Broadcast {
|
||||||
@@ -34,7 +31,7 @@ pub enum ActionType {
|
|||||||
// Http
|
// Http
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
#[derive(Debug, Serialize, Clone)]
|
||||||
pub struct Action {
|
pub struct Action {
|
||||||
#[serde(flatten)]
|
#[serde(flatten)]
|
||||||
pub action: ActionType,
|
pub action: ActionType,
|
||||||
@@ -42,24 +39,24 @@ pub struct Action {
|
|||||||
pub clear: Option<bool>,
|
pub clear: Option<bool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize)]
|
||||||
struct NotificationFinal {
|
struct NotificationFinal {
|
||||||
topic: String,
|
topic: String,
|
||||||
#[serde(flatten)]
|
#[serde(flatten)]
|
||||||
inner: Notification,
|
inner: Notification,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Clone, Deserialize)]
|
#[derive(Debug, Serialize, Clone)]
|
||||||
pub struct Notification {
|
pub struct Notification {
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
title: Option<String>,
|
title: Option<String>,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
message: Option<String>,
|
message: Option<String>,
|
||||||
#[serde(skip_serializing_if = "Vec::is_empty", default = "Default::default")]
|
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||||
tags: Vec<String>,
|
tags: Vec<String>,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
priority: Option<Priority>,
|
priority: Option<Priority>,
|
||||||
#[serde(skip_serializing_if = "Vec::is_empty", default = "Default::default")]
|
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||||
actions: Vec<Action>,
|
actions: Vec<Action>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -123,7 +120,6 @@ pub struct Config {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, LuaDevice)]
|
#[derive(Debug, Clone, LuaDevice)]
|
||||||
#[traits(crate::lua::traits::AddAdditionalMethods)]
|
|
||||||
pub struct Ntfy {
|
pub struct Ntfy {
|
||||||
config: Config,
|
config: Config,
|
||||||
}
|
}
|
||||||
@@ -167,20 +163,45 @@ impl Ntfy {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AddAdditionalMethods for Ntfy {
|
#[async_trait]
|
||||||
fn add_methods<M: mlua::UserDataMethods<Self>>(methods: &mut M)
|
impl OnPresence for Ntfy {
|
||||||
where
|
async fn on_presence(&self, presence: bool) {
|
||||||
Self: Sized + 'static,
|
// Setup extras for the broadcast
|
||||||
{
|
let extras = HashMap::from([
|
||||||
methods.add_async_method(
|
("cmd".into(), "presence".into()),
|
||||||
"send_notification",
|
("state".into(), if presence { "0" } else { "1" }.into()),
|
||||||
|lua, this, notification: mlua::Value| async move {
|
]);
|
||||||
let notification: Notification = lua.from_value(notification)?;
|
|
||||||
|
|
||||||
this.send(notification).await;
|
// Create broadcast action
|
||||||
|
let action = Action {
|
||||||
|
action: ActionType::Broadcast { extras },
|
||||||
|
label: if presence { "Set away" } else { "Set home" }.into(),
|
||||||
|
clear: Some(true),
|
||||||
|
};
|
||||||
|
|
||||||
Ok(())
|
// 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) {
|
||||||
|
self.send(notification).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ 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};
|
||||||
@@ -20,10 +19,6 @@ 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,
|
||||||
}
|
}
|
||||||
@@ -128,8 +123,6 @@ 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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
32
config.lua
32
config.lua
@@ -29,35 +29,15 @@ local mqtt_client = automation.new_mqtt_client({
|
|||||||
tls = host == "zeus" or host == "hephaestus",
|
tls = host == "zeus" or host == "hephaestus",
|
||||||
})
|
})
|
||||||
|
|
||||||
local ntfy = Ntfy.new({
|
automation.device_manager:add(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/+/#"),
|
||||||
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({
|
||||||
@@ -252,14 +232,6 @@ 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({
|
||||||
|
|||||||
Reference in New Issue
Block a user