Compare commits
7 Commits
60bf0f236c
...
5abdc88a35
| Author | SHA1 | Date | |
|---|---|---|---|
|
5abdc88a35
|
|||
|
19cdb37dfb
|
|||
|
3a33e3fa55
|
|||
|
e642562ddb
|
|||
|
a2e65c2d1a
|
|||
|
e26fd9f132
|
|||
|
eb0c80c4ce
|
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -167,6 +167,7 @@ name = "automation_macro"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"itertools",
|
||||
"mlua",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.106",
|
||||
|
||||
@@ -54,7 +54,7 @@ rumqttc = "0.24.0"
|
||||
serde = { version = "1.0.219", features = ["derive"] }
|
||||
serde_json = "1.0.143"
|
||||
serde_repr = "0.1.20"
|
||||
syn = { version = "2.0.106", features = ["extra-traits", "full"] }
|
||||
syn = { version = "2.0.106" }
|
||||
thiserror = "2.0.16"
|
||||
tokio = { version = "1", features = ["rt-multi-thread"] }
|
||||
tokio-cron-scheduler = "0.14.0"
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use async_trait::async_trait;
|
||||
use automation_lib::config::InfoConfig;
|
||||
use automation_lib::device::{Device, LuaDeviceCreate};
|
||||
use automation_macro::{LuaDevice, LuaDeviceConfig};
|
||||
use automation_macro::{Device, LuaDeviceConfig};
|
||||
use google_home::device::Name;
|
||||
use google_home::errors::ErrorCode;
|
||||
use google_home::traits::{
|
||||
@@ -19,8 +19,8 @@ pub struct Config {
|
||||
pub url: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, LuaDevice)]
|
||||
#[traits(OnOff)]
|
||||
#[derive(Debug, Clone, Device)]
|
||||
#[device(traits(OnOff))]
|
||||
pub struct AirFilter {
|
||||
config: Config,
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ use automation_lib::error::DeviceConfigError;
|
||||
use automation_lib::event::OnMqtt;
|
||||
use automation_lib::messages::ContactMessage;
|
||||
use automation_lib::mqtt::WrappedAsyncClient;
|
||||
use automation_macro::{LuaDevice, LuaDeviceConfig};
|
||||
use automation_macro::{Device, LuaDeviceConfig};
|
||||
use google_home::device;
|
||||
use google_home::errors::{DeviceError, ErrorCode};
|
||||
use google_home::traits::OpenClose;
|
||||
@@ -35,9 +35,9 @@ pub struct Config {
|
||||
pub sensor_type: SensorType,
|
||||
|
||||
#[device_config(from_lua, default)]
|
||||
pub callback: ActionCallback<ContactSensor, bool>,
|
||||
pub callback: ActionCallback<(ContactSensor, bool)>,
|
||||
#[device_config(from_lua, default)]
|
||||
pub battery_callback: ActionCallback<ContactSensor, f32>,
|
||||
pub battery_callback: ActionCallback<(ContactSensor, f32)>,
|
||||
|
||||
#[device_config(from_lua)]
|
||||
pub client: WrappedAsyncClient,
|
||||
@@ -48,8 +48,8 @@ struct State {
|
||||
is_closed: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, LuaDevice)]
|
||||
#[traits(OpenClose)]
|
||||
#[derive(Debug, Clone, Device)]
|
||||
#[device(traits(OpenClose))]
|
||||
pub struct ContactSensor {
|
||||
config: Config,
|
||||
state: Arc<RwLock<State>>,
|
||||
@@ -165,14 +165,17 @@ impl OnMqtt for ContactSensor {
|
||||
return;
|
||||
}
|
||||
|
||||
self.config.callback.call(self, &!is_closed).await;
|
||||
self.config.callback.call((self.clone(), !is_closed)).await;
|
||||
|
||||
debug!(id = self.get_id(), "Updating state to {is_closed}");
|
||||
self.state_mut().await.is_closed = is_closed;
|
||||
}
|
||||
|
||||
if let Some(battery) = message.battery {
|
||||
self.config.battery_callback.call(self, &battery).await;
|
||||
self.config
|
||||
.battery_callback
|
||||
.call((self.clone(), battery))
|
||||
.await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,8 +3,7 @@ use std::net::SocketAddr;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use automation_lib::device::{Device, LuaDeviceCreate};
|
||||
use automation_lib::lua::traits::AddAdditionalMethods;
|
||||
use automation_macro::{LuaDevice, LuaDeviceConfig};
|
||||
use automation_macro::{Device, LuaDeviceConfig};
|
||||
use mlua::LuaSerdeExt;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tracing::{error, trace, warn};
|
||||
@@ -31,8 +30,8 @@ pub struct Config {
|
||||
pub flags: FlagIDs,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, LuaDevice)]
|
||||
#[traits(AddAdditionalMethods)]
|
||||
#[derive(Debug, Clone, Device)]
|
||||
#[device(add_methods(Self::add_methods))]
|
||||
pub struct HueBridge {
|
||||
config: Config,
|
||||
}
|
||||
@@ -84,19 +83,8 @@ impl HueBridge {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Device for HueBridge {
|
||||
fn get_id(&self) -> String {
|
||||
self.config.identifier.clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl AddAdditionalMethods for HueBridge {
|
||||
fn add_methods<M: mlua::UserDataMethods<Self>>(methods: &mut M)
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
fn add_methods<M: mlua::UserDataMethods<Self>>(methods: &mut M) {
|
||||
methods.add_async_method(
|
||||
"set_flag",
|
||||
async |lua, this, (flag, value): (mlua::Value, bool)| {
|
||||
@@ -109,3 +97,9 @@ impl AddAdditionalMethods for HueBridge {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
impl Device for HueBridge {
|
||||
fn get_id(&self) -> String {
|
||||
self.config.identifier.clone()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ use std::net::SocketAddr;
|
||||
|
||||
use anyhow::Result;
|
||||
use async_trait::async_trait;
|
||||
use automation_macro::{LuaDevice, LuaDeviceConfig};
|
||||
use automation_macro::{Device, LuaDeviceConfig};
|
||||
use google_home::errors::ErrorCode;
|
||||
use google_home::traits::OnOff;
|
||||
use tracing::{error, trace, warn};
|
||||
@@ -19,8 +19,8 @@ pub struct Config {
|
||||
pub scene_id: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, LuaDevice)]
|
||||
#[traits(OnOff)]
|
||||
#[derive(Debug, Clone, Device)]
|
||||
#[device(traits(OnOff))]
|
||||
pub struct HueGroup {
|
||||
config: Config,
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ use automation_lib::config::{InfoConfig, MqttDeviceConfig};
|
||||
use automation_lib::device::{Device, LuaDeviceCreate};
|
||||
use automation_lib::event::OnMqtt;
|
||||
use automation_lib::mqtt::WrappedAsyncClient;
|
||||
use automation_macro::{LuaDevice, LuaDeviceConfig};
|
||||
use automation_macro::{Device, LuaDeviceConfig};
|
||||
use rumqttc::{Publish, matches};
|
||||
use serde::Deserialize;
|
||||
use tracing::{debug, trace, warn};
|
||||
@@ -21,19 +21,19 @@ pub struct Config {
|
||||
pub client: WrappedAsyncClient,
|
||||
|
||||
#[device_config(from_lua, default)]
|
||||
pub left_callback: ActionCallback<HueSwitch, ()>,
|
||||
pub left_callback: ActionCallback<HueSwitch>,
|
||||
|
||||
#[device_config(from_lua, default)]
|
||||
pub right_callback: ActionCallback<HueSwitch, ()>,
|
||||
pub right_callback: ActionCallback<HueSwitch>,
|
||||
|
||||
#[device_config(from_lua, default)]
|
||||
pub left_hold_callback: ActionCallback<HueSwitch, ()>,
|
||||
pub left_hold_callback: ActionCallback<HueSwitch>,
|
||||
|
||||
#[device_config(from_lua, default)]
|
||||
pub right_hold_callback: ActionCallback<HueSwitch, ()>,
|
||||
pub right_hold_callback: ActionCallback<HueSwitch>,
|
||||
|
||||
#[device_config(from_lua, default)]
|
||||
pub battery_callback: ActionCallback<HueSwitch, f32>,
|
||||
pub battery_callback: ActionCallback<(HueSwitch, f32)>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone, Deserialize)]
|
||||
@@ -55,7 +55,7 @@ struct State {
|
||||
battery: Option<f32>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, LuaDevice)]
|
||||
#[derive(Debug, Clone, Device)]
|
||||
pub struct HueSwitch {
|
||||
config: Config,
|
||||
}
|
||||
@@ -104,19 +104,21 @@ impl OnMqtt for HueSwitch {
|
||||
);
|
||||
|
||||
match action {
|
||||
Action::LeftPressRelease => self.config.left_callback.call(self, &()).await,
|
||||
Action::RightPressRelease => self.config.right_callback.call(self, &()).await,
|
||||
Action::LeftHold => self.config.left_hold_callback.call(self, &()).await,
|
||||
Action::RightHold => self.config.right_hold_callback.call(self, &()).await,
|
||||
Action::LeftPressRelease => self.config.left_callback.call(self.clone()).await,
|
||||
Action::RightPressRelease => {
|
||||
self.config.right_callback.call(self.clone()).await
|
||||
}
|
||||
Action::LeftHold => self.config.left_hold_callback.call(self.clone()).await,
|
||||
Action::RightHold => self.config.right_hold_callback.call(self.clone()).await,
|
||||
// If there is no hold action, the switch will act like a normal release
|
||||
Action::RightHoldRelease => {
|
||||
if !self.config.right_hold_callback.is_set() {
|
||||
self.config.right_callback.call(self, &()).await
|
||||
if self.config.right_hold_callback.is_empty() {
|
||||
self.config.right_callback.call(self.clone()).await
|
||||
}
|
||||
}
|
||||
Action::LeftHoldRelease => {
|
||||
if !self.config.left_hold_callback.is_set() {
|
||||
self.config.left_callback.call(self, &()).await
|
||||
if self.config.left_hold_callback.is_empty() {
|
||||
self.config.left_callback.call(self.clone()).await
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
@@ -124,7 +126,10 @@ impl OnMqtt for HueSwitch {
|
||||
}
|
||||
|
||||
if let Some(battery) = message.battery {
|
||||
self.config.battery_callback.call(self, &battery).await;
|
||||
self.config
|
||||
.battery_callback
|
||||
.call((self.clone(), battery))
|
||||
.await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ use automation_lib::device::{Device, LuaDeviceCreate};
|
||||
use automation_lib::event::OnMqtt;
|
||||
use automation_lib::messages::{RemoteAction, RemoteMessage};
|
||||
use automation_lib::mqtt::WrappedAsyncClient;
|
||||
use automation_macro::{LuaDevice, LuaDeviceConfig};
|
||||
use automation_macro::{Device, LuaDeviceConfig};
|
||||
use rumqttc::{Publish, matches};
|
||||
use tracing::{debug, error, trace};
|
||||
|
||||
@@ -24,12 +24,12 @@ pub struct Config {
|
||||
pub client: WrappedAsyncClient,
|
||||
|
||||
#[device_config(from_lua, default)]
|
||||
pub callback: ActionCallback<IkeaRemote, bool>,
|
||||
pub callback: ActionCallback<(IkeaRemote, bool)>,
|
||||
#[device_config(from_lua, default)]
|
||||
pub battery_callback: ActionCallback<IkeaRemote, f32>,
|
||||
pub battery_callback: ActionCallback<(IkeaRemote, f32)>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, LuaDevice)]
|
||||
#[derive(Debug, Clone, Device)]
|
||||
pub struct IkeaRemote {
|
||||
config: Config,
|
||||
}
|
||||
@@ -88,12 +88,15 @@ impl OnMqtt for IkeaRemote {
|
||||
};
|
||||
|
||||
if let Some(on) = on {
|
||||
self.config.callback.call(self, &on).await;
|
||||
self.config.callback.call((self.clone(), on)).await;
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(battery) = message.battery {
|
||||
self.config.battery_callback.call(self, &battery).await;
|
||||
self.config
|
||||
.battery_callback
|
||||
.call((self.clone(), battery))
|
||||
.await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ use std::str::Utf8Error;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use automation_lib::device::{Device, LuaDeviceCreate};
|
||||
use automation_macro::{LuaDevice, LuaDeviceConfig};
|
||||
use automation_macro::{Device, LuaDeviceConfig};
|
||||
use bytes::{Buf, BufMut};
|
||||
use google_home::errors::{self, DeviceError};
|
||||
use google_home::traits::OnOff;
|
||||
@@ -21,8 +21,8 @@ pub struct Config {
|
||||
pub addr: SocketAddr,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, LuaDevice)]
|
||||
#[traits(OnOff)]
|
||||
#[derive(Debug, Clone, Device)]
|
||||
#[device(traits(OnOff))]
|
||||
pub struct KasaOutlet {
|
||||
config: Config,
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ use automation_lib::device::{Device, LuaDeviceCreate};
|
||||
use automation_lib::event::OnMqtt;
|
||||
use automation_lib::messages::BrightnessMessage;
|
||||
use automation_lib::mqtt::WrappedAsyncClient;
|
||||
use automation_macro::{LuaDevice, LuaDeviceConfig};
|
||||
use automation_macro::{Device, LuaDeviceConfig};
|
||||
use rumqttc::Publish;
|
||||
use tokio::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard};
|
||||
use tracing::{debug, trace, warn};
|
||||
@@ -21,7 +21,7 @@ pub struct Config {
|
||||
pub max: isize,
|
||||
|
||||
#[device_config(from_lua, default)]
|
||||
pub callback: ActionCallback<LightSensor, bool>,
|
||||
pub callback: ActionCallback<(LightSensor, bool)>,
|
||||
|
||||
#[device_config(from_lua)]
|
||||
pub client: WrappedAsyncClient,
|
||||
@@ -34,7 +34,7 @@ pub struct State {
|
||||
is_dark: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, LuaDevice)]
|
||||
#[derive(Debug, Clone, Device)]
|
||||
pub struct LightSensor {
|
||||
config: Config,
|
||||
state: Arc<RwLock<State>>,
|
||||
@@ -114,7 +114,7 @@ impl OnMqtt for LightSensor {
|
||||
|
||||
self.config
|
||||
.callback
|
||||
.call(self, &!self.state().await.is_dark)
|
||||
.call((self.clone(), !self.state().await.is_dark))
|
||||
.await;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,8 +3,7 @@ use std::convert::Infallible;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use automation_lib::device::{Device, LuaDeviceCreate};
|
||||
use automation_lib::lua::traits::AddAdditionalMethods;
|
||||
use automation_macro::{LuaDevice, LuaDeviceConfig};
|
||||
use automation_macro::{Device, LuaDeviceConfig};
|
||||
use mlua::LuaSerdeExt;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_repr::*;
|
||||
@@ -118,12 +117,27 @@ pub struct Config {
|
||||
pub topic: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, LuaDevice)]
|
||||
#[traits(AddAdditionalMethods)]
|
||||
#[derive(Debug, Clone, Device)]
|
||||
#[device(add_methods(Self::add_methods))]
|
||||
pub struct Ntfy {
|
||||
config: Config,
|
||||
}
|
||||
|
||||
impl Ntfy {
|
||||
fn add_methods<M: mlua::UserDataMethods<Self>>(methods: &mut M) {
|
||||
methods.add_async_method(
|
||||
"send_notification",
|
||||
async |lua, this, notification: mlua::Value| {
|
||||
let notification: Notification = lua.from_value(notification)?;
|
||||
|
||||
this.send(notification).await;
|
||||
|
||||
Ok(())
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl LuaDeviceCreate for Ntfy {
|
||||
type Config = Config;
|
||||
@@ -162,21 +176,3 @@ impl Ntfy {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl AddAdditionalMethods for Ntfy {
|
||||
fn add_methods<M: mlua::UserDataMethods<Self>>(methods: &mut M)
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
methods.add_async_method(
|
||||
"send_notification",
|
||||
async |lua, this, notification: mlua::Value| {
|
||||
let notification: Notification = lua.from_value(notification)?;
|
||||
|
||||
this.send(notification).await;
|
||||
|
||||
Ok(())
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,10 +6,9 @@ use automation_lib::action_callback::ActionCallback;
|
||||
use automation_lib::config::MqttDeviceConfig;
|
||||
use automation_lib::device::{Device, LuaDeviceCreate};
|
||||
use automation_lib::event::OnMqtt;
|
||||
use automation_lib::lua::traits::AddAdditionalMethods;
|
||||
use automation_lib::messages::PresenceMessage;
|
||||
use automation_lib::mqtt::WrappedAsyncClient;
|
||||
use automation_macro::{LuaDevice, LuaDeviceConfig};
|
||||
use automation_macro::{Device, LuaDeviceConfig};
|
||||
use rumqttc::Publish;
|
||||
use tokio::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard};
|
||||
use tracing::{debug, trace, warn};
|
||||
@@ -20,7 +19,7 @@ pub struct Config {
|
||||
pub mqtt: MqttDeviceConfig,
|
||||
|
||||
#[device_config(from_lua, default)]
|
||||
pub callback: ActionCallback<Presence, bool>,
|
||||
pub callback: ActionCallback<(Presence, bool)>,
|
||||
|
||||
#[device_config(from_lua)]
|
||||
pub client: WrappedAsyncClient,
|
||||
@@ -34,8 +33,8 @@ pub struct State {
|
||||
current_overall_presence: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, LuaDevice)]
|
||||
#[traits(AddAdditionalMethods)]
|
||||
#[derive(Debug, Clone, Device)]
|
||||
#[device(add_methods(Self::add_methods))]
|
||||
pub struct Presence {
|
||||
config: Config,
|
||||
state: Arc<RwLock<State>>,
|
||||
@@ -49,6 +48,12 @@ impl Presence {
|
||||
async fn state_mut(&self) -> RwLockWriteGuard<'_, State> {
|
||||
self.state.write().await
|
||||
}
|
||||
|
||||
fn add_methods<M: mlua::UserDataMethods<Self>>(methods: &mut M) {
|
||||
methods.add_async_method("overall_presence", async |_lua, this, ()| {
|
||||
Ok(this.state().await.current_overall_presence)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
@@ -118,18 +123,10 @@ impl OnMqtt for Presence {
|
||||
debug!("Overall presence updated: {overall_presence}");
|
||||
self.state_mut().await.current_overall_presence = overall_presence;
|
||||
|
||||
self.config.callback.call(self, &overall_presence).await;
|
||||
self.config
|
||||
.callback
|
||||
.call((self.clone(), overall_presence))
|
||||
.await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl AddAdditionalMethods for Presence {
|
||||
fn add_methods<M: mlua::UserDataMethods<Self>>(methods: &mut M)
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
methods.add_async_method("overall_presence", async |_lua, this, ()| {
|
||||
Ok(this.state().await.current_overall_presence)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ use automation_lib::device::{Device, LuaDeviceCreate};
|
||||
use automation_lib::event::OnMqtt;
|
||||
use automation_lib::messages::ActivateMessage;
|
||||
use automation_lib::mqtt::WrappedAsyncClient;
|
||||
use automation_macro::{LuaDevice, LuaDeviceConfig};
|
||||
use automation_macro::{Device, LuaDeviceConfig};
|
||||
use eui48::MacAddress;
|
||||
use google_home::device;
|
||||
use google_home::errors::ErrorCode;
|
||||
@@ -28,7 +28,7 @@ pub struct Config {
|
||||
pub client: WrappedAsyncClient,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, LuaDevice)]
|
||||
#[derive(Debug, Clone, Device)]
|
||||
pub struct WakeOnLAN {
|
||||
config: Config,
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ use automation_lib::device::{Device, LuaDeviceCreate};
|
||||
use automation_lib::event::OnMqtt;
|
||||
use automation_lib::messages::PowerMessage;
|
||||
use automation_lib::mqtt::WrappedAsyncClient;
|
||||
use automation_macro::{LuaDevice, LuaDeviceConfig};
|
||||
use automation_macro::{Device, LuaDeviceConfig};
|
||||
use rumqttc::Publish;
|
||||
use tokio::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard};
|
||||
use tracing::{debug, error, trace};
|
||||
@@ -21,7 +21,7 @@ pub struct Config {
|
||||
pub threshold: f32,
|
||||
|
||||
#[device_config(from_lua, default)]
|
||||
pub done_callback: ActionCallback<Washer, ()>,
|
||||
pub done_callback: ActionCallback<Washer>,
|
||||
|
||||
#[device_config(from_lua)]
|
||||
pub client: WrappedAsyncClient,
|
||||
@@ -33,7 +33,7 @@ pub struct State {
|
||||
}
|
||||
|
||||
// TODO: Add google home integration
|
||||
#[derive(Debug, Clone, LuaDevice)]
|
||||
#[derive(Debug, Clone, Device)]
|
||||
pub struct Washer {
|
||||
config: Config,
|
||||
state: Arc<RwLock<State>>,
|
||||
@@ -109,7 +109,7 @@ impl OnMqtt for Washer {
|
||||
|
||||
self.state_mut().await.running = 0;
|
||||
|
||||
self.config.done_callback.call(self, &()).await;
|
||||
self.config.done_callback.call(self.clone()).await;
|
||||
} else if power < self.config.threshold {
|
||||
// Prevent false positives
|
||||
self.state_mut().await.running = 0;
|
||||
|
||||
@@ -10,7 +10,7 @@ use automation_lib::device::{Device, LuaDeviceCreate};
|
||||
use automation_lib::event::OnMqtt;
|
||||
use automation_lib::helpers::serialization::state_deserializer;
|
||||
use automation_lib::mqtt::WrappedAsyncClient;
|
||||
use automation_macro::{LuaDevice, LuaDeviceConfig, LuaSerialize};
|
||||
use automation_macro::{Device, LuaDeviceConfig, LuaSerialize};
|
||||
use google_home::device;
|
||||
use google_home::errors::ErrorCode;
|
||||
use google_home::traits::{Brightness, Color, ColorSetting, ColorTemperatureRange, OnOff};
|
||||
@@ -34,7 +34,7 @@ pub struct Config<T: LightState> {
|
||||
pub mqtt: MqttDeviceConfig,
|
||||
|
||||
#[device_config(from_lua, default)]
|
||||
pub callback: ActionCallback<Light<T>, T>,
|
||||
pub callback: ActionCallback<(Light<T>, T)>,
|
||||
|
||||
#[device_config(from_lua)]
|
||||
pub client: WrappedAsyncClient,
|
||||
@@ -88,10 +88,10 @@ impl From<StateColorTemperature> for StateBrightness {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, LuaDevice)]
|
||||
#[traits(<StateOnOff>: OnOff)]
|
||||
#[traits(<StateBrightness>: OnOff, Brightness)]
|
||||
#[traits(<StateColorTemperature>: OnOff, Brightness, ColorSetting)]
|
||||
#[derive(Debug, Clone, Device)]
|
||||
#[device(traits(OnOff for <StateOnOff>, <StateBrightness>, <StateColorTemperature>))]
|
||||
#[device(traits(Brightness for <StateBrightness>, <StateColorTemperature>))]
|
||||
#[device(traits(ColorSetting for <StateColorTemperature>))]
|
||||
pub struct Light<T: LightState> {
|
||||
config: Config<T>,
|
||||
|
||||
@@ -165,7 +165,7 @@ impl OnMqtt for Light<StateOnOff> {
|
||||
|
||||
self.config
|
||||
.callback
|
||||
.call(self, self.state().await.deref())
|
||||
.call((self.clone(), self.state().await.clone()))
|
||||
.await;
|
||||
}
|
||||
}
|
||||
@@ -204,7 +204,7 @@ impl OnMqtt for Light<StateBrightness> {
|
||||
|
||||
self.config
|
||||
.callback
|
||||
.call(self, self.state().await.deref())
|
||||
.call((self.clone(), self.state().await.clone()))
|
||||
.await;
|
||||
}
|
||||
}
|
||||
@@ -245,7 +245,7 @@ impl OnMqtt for Light<StateColorTemperature> {
|
||||
|
||||
self.config
|
||||
.callback
|
||||
.call(self, self.state().await.deref())
|
||||
.call((self.clone(), self.state().await.clone()))
|
||||
.await;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ use automation_lib::device::{Device, LuaDeviceCreate};
|
||||
use automation_lib::event::OnMqtt;
|
||||
use automation_lib::helpers::serialization::state_deserializer;
|
||||
use automation_lib::mqtt::WrappedAsyncClient;
|
||||
use automation_macro::{LuaDevice, LuaDeviceConfig, LuaSerialize};
|
||||
use automation_macro::{Device, LuaDeviceConfig, LuaSerialize};
|
||||
use google_home::device;
|
||||
use google_home::errors::ErrorCode;
|
||||
use google_home::traits::OnOff;
|
||||
@@ -51,7 +51,7 @@ pub struct Config<T: OutletState> {
|
||||
pub outlet_type: OutletType,
|
||||
|
||||
#[device_config(from_lua, default)]
|
||||
pub callback: ActionCallback<Outlet<T>, T>,
|
||||
pub callback: ActionCallback<(Outlet<T>, T)>,
|
||||
|
||||
#[device_config(from_lua)]
|
||||
pub client: WrappedAsyncClient,
|
||||
@@ -80,9 +80,8 @@ impl From<StatePower> for StateOnOff {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, LuaDevice)]
|
||||
#[traits(<StateOnOff>: OnOff)]
|
||||
#[traits(<StatePower>: OnOff)]
|
||||
#[derive(Debug, Clone, Device)]
|
||||
#[device(traits(OnOff for <StateOnOff>, <StatePower>))]
|
||||
pub struct Outlet<T: OutletState> {
|
||||
config: Config<T>,
|
||||
|
||||
@@ -155,7 +154,7 @@ impl OnMqtt for Outlet<StateOnOff> {
|
||||
|
||||
self.config
|
||||
.callback
|
||||
.call(self, self.state().await.deref())
|
||||
.call((self.clone(), self.state().await.clone()))
|
||||
.await;
|
||||
}
|
||||
}
|
||||
@@ -192,7 +191,7 @@ impl OnMqtt for Outlet<StatePower> {
|
||||
|
||||
self.config
|
||||
.callback
|
||||
.call(self, self.state().await.deref())
|
||||
.call((self.clone(), self.state().await.clone()))
|
||||
.await;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,34 +1,28 @@
|
||||
use std::marker::PhantomData;
|
||||
|
||||
use futures::future::try_join_all;
|
||||
use mlua::{FromLua, IntoLua, LuaSerdeExt};
|
||||
use serde::Serialize;
|
||||
use mlua::{FromLua, IntoLuaMulti};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct Internal {
|
||||
pub struct ActionCallback<P> {
|
||||
callbacks: Vec<mlua::Function>,
|
||||
lua: mlua::Lua,
|
||||
_parameters: PhantomData<P>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ActionCallback<T, S> {
|
||||
internal: Option<Internal>,
|
||||
_this: PhantomData<T>,
|
||||
_state: PhantomData<S>,
|
||||
}
|
||||
|
||||
impl<T, S> Default for ActionCallback<T, S> {
|
||||
// NOTE: For some reason the derive macro combined with PhantomData leads to issues where it
|
||||
// requires all types part of P to implement default, even if they never actually get constructed.
|
||||
// By manually implemented Default it works fine.
|
||||
impl<P> Default for ActionCallback<P> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
internal: None,
|
||||
_this: PhantomData::<T>,
|
||||
_state: PhantomData::<S>,
|
||||
callbacks: Default::default(),
|
||||
_parameters: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> FromLua for ActionCallback<T, S> {
|
||||
fn from_lua(value: mlua::Value, lua: &mlua::Lua) -> mlua::Result<Self> {
|
||||
impl<P> FromLua for ActionCallback<P> {
|
||||
fn from_lua(value: mlua::Value, _lua: &mlua::Lua) -> mlua::Result<Self> {
|
||||
let callbacks = match value {
|
||||
mlua::Value::Function(f) => vec![f],
|
||||
mlua::Value::Table(table) => table
|
||||
@@ -49,40 +43,28 @@ impl<T, S> FromLua for ActionCallback<T, S> {
|
||||
};
|
||||
|
||||
Ok(ActionCallback {
|
||||
internal: Some(Internal {
|
||||
callbacks,
|
||||
lua: lua.clone(),
|
||||
}),
|
||||
_this: PhantomData::<T>,
|
||||
_state: PhantomData::<S>,
|
||||
callbacks,
|
||||
_parameters: PhantomData::<P>,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Return proper error here
|
||||
impl<T, S> ActionCallback<T, S>
|
||||
impl<P> ActionCallback<P>
|
||||
where
|
||||
T: IntoLua + Sync + Send + Clone + 'static,
|
||||
S: Serialize,
|
||||
P: IntoLuaMulti + Sync + Clone,
|
||||
{
|
||||
pub async fn call(&self, this: &T, state: &S) {
|
||||
let Some(internal) = self.internal.as_ref() else {
|
||||
return;
|
||||
};
|
||||
|
||||
let state = internal.lua.to_value(state).unwrap();
|
||||
|
||||
pub async fn call(&self, parameters: P) {
|
||||
try_join_all(
|
||||
internal
|
||||
.callbacks
|
||||
self.callbacks
|
||||
.iter()
|
||||
.map(async |f| f.call_async::<()>((this.clone(), state.clone())).await),
|
||||
.map(async |f| f.call_async::<()>(parameters.clone()).await),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
pub fn is_set(&self) -> bool {
|
||||
self.internal.is_some()
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.callbacks.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ impl mlua::UserData for Timeout {
|
||||
|
||||
methods.add_async_method(
|
||||
"start",
|
||||
async |_lua, this, (timeout, callback): (f32, ActionCallback<mlua::Value, bool>)| {
|
||||
async |_lua, this, (timeout, callback): (f32, ActionCallback<()>)| {
|
||||
if let Some(handle) = this.state.write().await.handle.take() {
|
||||
handle.abort();
|
||||
}
|
||||
@@ -42,7 +42,7 @@ impl mlua::UserData for Timeout {
|
||||
async move {
|
||||
tokio::time::sleep(timeout).await;
|
||||
|
||||
callback.call(&mlua::Nil, &false).await;
|
||||
callback.call(()).await;
|
||||
}
|
||||
}));
|
||||
|
||||
|
||||
@@ -78,9 +78,3 @@ pub trait 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;
|
||||
}
|
||||
|
||||
@@ -28,7 +28,13 @@ impl mlua::UserData for WrappedAsyncClient {
|
||||
methods.add_async_method(
|
||||
"send_message",
|
||||
async |_lua, this, (topic, message): (String, mlua::Value)| {
|
||||
let message = serde_json::to_string(&message).unwrap();
|
||||
// serde_json converts nil => "null", but we actually want nil to send an empty
|
||||
// message
|
||||
let message = if message.is_nil() {
|
||||
"".into()
|
||||
} else {
|
||||
serde_json::to_string(&message).unwrap()
|
||||
};
|
||||
|
||||
debug!("message = {message}");
|
||||
|
||||
|
||||
@@ -11,3 +11,6 @@ itertools = { workspace = true }
|
||||
proc-macro2 = { workspace = true }
|
||||
quote = { workspace = true }
|
||||
syn = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
mlua = { workspace = true }
|
||||
|
||||
235
automation_macro/src/device.rs
Normal file
235
automation_macro/src/device.rs
Normal file
@@ -0,0 +1,235 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use proc_macro2::TokenStream as TokenStream2;
|
||||
use quote::{ToTokens, quote};
|
||||
use syn::parse::{Parse, ParseStream};
|
||||
use syn::punctuated::Punctuated;
|
||||
use syn::{Attribute, DeriveInput, Token, parenthesized};
|
||||
|
||||
enum Attr {
|
||||
Trait(TraitAttr),
|
||||
AddMethods(AddMethodsAttr),
|
||||
}
|
||||
|
||||
impl Parse for Attr {
|
||||
fn parse(input: ParseStream) -> syn::Result<Self> {
|
||||
let ident: syn::Ident = input.parse()?;
|
||||
|
||||
let attr;
|
||||
_ = parenthesized!(attr in input);
|
||||
|
||||
let attr = match ident.to_string().as_str() {
|
||||
"traits" => Attr::Trait(attr.parse()?),
|
||||
"add_methods" => Attr::AddMethods(attr.parse()?),
|
||||
_ => {
|
||||
return Err(syn::Error::new(
|
||||
ident.span(),
|
||||
"Expected 'traits' or 'add_methods'",
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
Ok(attr)
|
||||
}
|
||||
}
|
||||
|
||||
struct TraitAttr {
|
||||
traits: Traits,
|
||||
generics: Generics,
|
||||
}
|
||||
|
||||
impl Parse for TraitAttr {
|
||||
fn parse(input: ParseStream) -> syn::Result<Self> {
|
||||
Ok(Self {
|
||||
traits: input.parse()?,
|
||||
generics: input.parse()?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct Traits(Vec<syn::Ident>);
|
||||
|
||||
impl Traits {
|
||||
fn extend(&mut self, other: &Traits) {
|
||||
self.0.extend_from_slice(&other.0);
|
||||
}
|
||||
}
|
||||
|
||||
impl Parse for Traits {
|
||||
fn parse(input: ParseStream) -> syn::Result<Self> {
|
||||
input
|
||||
.call(Punctuated::<_, Token![,]>::parse_separated_nonempty)
|
||||
.map(|traits| traits.into_iter().collect())
|
||||
.map(Self)
|
||||
}
|
||||
}
|
||||
|
||||
impl ToTokens for Traits {
|
||||
fn to_tokens(&self, tokens: &mut TokenStream2) {
|
||||
let Self(traits) = &self;
|
||||
|
||||
tokens.extend(quote! {
|
||||
#(
|
||||
::automation_lib::lua::traits::#traits::add_methods(methods);
|
||||
)*
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct Generics(Vec<syn::AngleBracketedGenericArguments>);
|
||||
|
||||
impl Generics {
|
||||
fn has_generics(&self) -> bool {
|
||||
!self.0.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
impl Parse for Generics {
|
||||
fn parse(input: ParseStream) -> syn::Result<Self> {
|
||||
if !input.peek(Token![for]) {
|
||||
if input.is_empty() {
|
||||
return Ok(Default::default());
|
||||
} else {
|
||||
return Err(input.error("Expected ')' or 'for'"));
|
||||
}
|
||||
}
|
||||
|
||||
_ = input.parse::<syn::Token![for]>()?;
|
||||
|
||||
input
|
||||
.call(Punctuated::<_, Token![,]>::parse_separated_nonempty)
|
||||
.map(|generics| generics.into_iter().collect())
|
||||
.map(Self)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct AddMethodsAttr(syn::Path);
|
||||
|
||||
impl Parse for AddMethodsAttr {
|
||||
fn parse(input: ParseStream) -> syn::Result<Self> {
|
||||
Ok(Self(input.parse()?))
|
||||
}
|
||||
}
|
||||
|
||||
impl ToTokens for AddMethodsAttr {
|
||||
fn to_tokens(&self, tokens: &mut TokenStream2) {
|
||||
let Self(path) = self;
|
||||
|
||||
tokens.extend(quote! {
|
||||
#path
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
struct Implementation {
|
||||
generics: Option<syn::AngleBracketedGenericArguments>,
|
||||
traits: Traits,
|
||||
add_methods: Vec<AddMethodsAttr>,
|
||||
}
|
||||
|
||||
impl quote::ToTokens for Implementation {
|
||||
fn to_tokens(&self, tokens: &mut TokenStream2) {
|
||||
let Self {
|
||||
generics,
|
||||
traits,
|
||||
add_methods,
|
||||
} = &self;
|
||||
|
||||
tokens.extend(quote! {
|
||||
#generics {
|
||||
fn add_methods<M: mlua::UserDataMethods<Self>>(methods: &mut M) {
|
||||
methods.add_async_function("new", async |_lua, config| {
|
||||
let device: Self = LuaDeviceCreate::create(config)
|
||||
.await
|
||||
.map_err(mlua::ExternalError::into_lua_err)?;
|
||||
|
||||
Ok(device)
|
||||
});
|
||||
|
||||
methods.add_method("__box", |_lua, this, _: ()| {
|
||||
let b: Box<dyn Device> = Box::new(this.clone());
|
||||
Ok(b)
|
||||
});
|
||||
|
||||
methods.add_async_method("get_id", async |_lua, this, _: ()| { Ok(this.get_id()) });
|
||||
|
||||
#traits
|
||||
|
||||
#(
|
||||
#add_methods(methods);
|
||||
)*
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
struct Implementations(Vec<Implementation>);
|
||||
|
||||
impl From<Vec<Attr>> for Implementations {
|
||||
fn from(attributes: Vec<Attr>) -> Self {
|
||||
let mut add_methods = Vec::new();
|
||||
let mut all = Traits::default();
|
||||
let mut implementations: HashMap<_, Traits> = HashMap::new();
|
||||
for attribute in attributes {
|
||||
match attribute {
|
||||
Attr::Trait(attribute) => {
|
||||
if attribute.generics.has_generics() {
|
||||
for generic in &attribute.generics.0 {
|
||||
implementations
|
||||
.entry(Some(generic.clone()))
|
||||
.or_default()
|
||||
.extend(&attribute.traits);
|
||||
}
|
||||
} else {
|
||||
all.extend(&attribute.traits);
|
||||
}
|
||||
}
|
||||
Attr::AddMethods(attribute) => add_methods.push(attribute),
|
||||
}
|
||||
}
|
||||
|
||||
if implementations.is_empty() {
|
||||
implementations.entry(None).or_default().extend(&all);
|
||||
} else {
|
||||
for traits in implementations.values_mut() {
|
||||
traits.extend(&all);
|
||||
}
|
||||
}
|
||||
|
||||
Self(
|
||||
implementations
|
||||
.into_iter()
|
||||
.map(|(generics, traits)| Implementation {
|
||||
generics,
|
||||
traits,
|
||||
add_methods: add_methods.clone(),
|
||||
})
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn device(input: &DeriveInput) -> TokenStream2 {
|
||||
let name = &input.ident;
|
||||
|
||||
let Implementations(imp) = match input
|
||||
.attrs
|
||||
.iter()
|
||||
.filter(|attr| attr.path().is_ident("device"))
|
||||
.map(Attribute::parse_args)
|
||||
.try_collect::<Vec<_>>()
|
||||
{
|
||||
Ok(result) => result.into(),
|
||||
Err(err) => return err.into_compile_error(),
|
||||
};
|
||||
|
||||
quote! {
|
||||
#(
|
||||
impl mlua::UserData for #name #imp
|
||||
)*
|
||||
}
|
||||
}
|
||||
@@ -1,88 +0,0 @@
|
||||
use proc_macro2::TokenStream;
|
||||
use quote::{ToTokens, quote};
|
||||
use syn::parse::Parse;
|
||||
use syn::punctuated::Punctuated;
|
||||
use syn::{AngleBracketedGenericArguments, Attribute, DeriveInput, Ident, Path, Token};
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
struct Impl {
|
||||
generics: Option<AngleBracketedGenericArguments>,
|
||||
traits: Vec<Path>,
|
||||
}
|
||||
|
||||
impl Parse for Impl {
|
||||
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
|
||||
let generics = if input.peek(Token![<]) {
|
||||
let generics = input.parse()?;
|
||||
input.parse::<Token![:]>()?;
|
||||
|
||||
Some(generics)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let traits: Punctuated<_, _> = input.parse_terminated(Path::parse, Token![,])?;
|
||||
let traits = traits.into_iter().collect();
|
||||
|
||||
Ok(Impl { generics, traits })
|
||||
}
|
||||
}
|
||||
|
||||
impl Impl {
|
||||
fn generate(&self, name: &Ident) -> TokenStream {
|
||||
let generics = &self.generics;
|
||||
|
||||
// If an identifier is specified, assume it is placed in ::automation_lib::lua::traits,
|
||||
// otherwise use the provided path
|
||||
let traits = self.traits.iter().map(|t| {
|
||||
if let Some(ident) = t.get_ident() {
|
||||
quote! {::automation_lib::lua::traits::#ident }
|
||||
} else {
|
||||
t.to_token_stream()
|
||||
}
|
||||
});
|
||||
|
||||
quote! {
|
||||
impl mlua::UserData for #name #generics {
|
||||
fn add_methods<M: mlua::UserDataMethods<Self>>(methods: &mut M) {
|
||||
methods.add_async_function("new", async |_lua, config| {
|
||||
let device: Self = LuaDeviceCreate::create(config)
|
||||
.await
|
||||
.map_err(mlua::ExternalError::into_lua_err)?;
|
||||
|
||||
Ok(device)
|
||||
});
|
||||
|
||||
methods.add_method("__box", |_lua, this, _: ()| {
|
||||
let b: Box<dyn Device> = Box::new(this.clone());
|
||||
Ok(b)
|
||||
});
|
||||
|
||||
methods.add_async_method("get_id", async |_lua, this, _: ()| { Ok(this.get_id()) });
|
||||
|
||||
#(
|
||||
#traits::add_methods(methods);
|
||||
)*
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn impl_device_macro(ast: &DeriveInput) -> TokenStream {
|
||||
let name = &ast.ident;
|
||||
|
||||
let impls: TokenStream = ast
|
||||
.attrs
|
||||
.iter()
|
||||
.filter(|attr| attr.path().is_ident("traits"))
|
||||
.flat_map(Attribute::parse_args::<Impl>)
|
||||
.map(|im| im.generate(name))
|
||||
.collect();
|
||||
|
||||
if impls.is_empty() {
|
||||
Impl::default().generate(name)
|
||||
} else {
|
||||
impls
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,12 @@
|
||||
#![feature(iter_intersperse)]
|
||||
mod impl_device;
|
||||
#![feature(iterator_try_collect)]
|
||||
mod device;
|
||||
mod lua_device_config;
|
||||
|
||||
use lua_device_config::impl_lua_device_config_macro;
|
||||
use quote::quote;
|
||||
use syn::{DeriveInput, parse_macro_input};
|
||||
|
||||
use crate::impl_device::impl_device_macro;
|
||||
|
||||
#[proc_macro_derive(LuaDeviceConfig, attributes(device_config))]
|
||||
pub fn lua_device_config_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||
let ast = parse_macro_input!(input as DeriveInput);
|
||||
@@ -15,13 +14,6 @@ pub fn lua_device_config_derive(input: proc_macro::TokenStream) -> proc_macro::T
|
||||
impl_lua_device_config_macro(&ast).into()
|
||||
}
|
||||
|
||||
#[proc_macro_derive(LuaDevice, attributes(traits))]
|
||||
pub fn impl_device(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||
let ast = parse_macro_input!(input as DeriveInput);
|
||||
|
||||
impl_device_macro(&ast).into()
|
||||
}
|
||||
|
||||
#[proc_macro_derive(LuaSerialize, attributes(traits))]
|
||||
pub fn lua_serialize(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||
let ast = parse_macro_input!(input as DeriveInput);
|
||||
@@ -37,3 +29,45 @@ pub fn lua_serialize(input: proc_macro::TokenStream) -> proc_macro::TokenStream
|
||||
}
|
||||
.into()
|
||||
}
|
||||
|
||||
/// Derive macro generating an impl for the trait `::mlua::UserData`
|
||||
///
|
||||
/// # Device traits
|
||||
/// The `device(traits)` attribute can be used to tell the macro what traits are implemented so that
|
||||
/// the appropriate methods can automatically be registered.
|
||||
/// If the struct does not have any type parameters the syntax is very simple:
|
||||
/// ```rust
|
||||
/// #[device(traits(TraitA, TraitB))]
|
||||
/// ```
|
||||
///
|
||||
/// If the type does have type parameters you will have to manually specify all variations that
|
||||
/// have the trait available:
|
||||
/// ```rust
|
||||
/// #[device(traits(TraitA, TraitB for <StateA>, <StateB>))]
|
||||
/// ```
|
||||
/// If multiple of these attributes are specified they will all combined appropriately.
|
||||
///
|
||||
///
|
||||
/// ## NOTE
|
||||
/// If your type _has_ type parameters any instance of the traits attribute that does not specify
|
||||
/// any type parameters will have the traits applied to _all_ other type parameter variations
|
||||
/// listed in the other trait attributes. This behavior only applies if there is at least one
|
||||
/// instance with type parameters specified.
|
||||
///
|
||||
/// # Additional methods
|
||||
/// Additional methods can be added by using the `device(add_methods)` attribute. This attribute
|
||||
/// takes the path to a function with the following signature that can register the additional methods:
|
||||
///
|
||||
/// ```rust
|
||||
/// # struct D;
|
||||
/// fn top_secret<M: mlua::UserDataMethods<D>>(methods: &mut M) {}
|
||||
/// ```
|
||||
/// It can then be registered with:
|
||||
/// ```rust
|
||||
/// #[device(add_methods(top_secret))]
|
||||
/// ```
|
||||
#[proc_macro_derive(Device, attributes(device))]
|
||||
pub fn device(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||
let ast = parse_macro_input!(input as DeriveInput);
|
||||
device::device(&ast).into()
|
||||
}
|
||||
|
||||
123
config.lua
123
config.lua
@@ -425,49 +425,57 @@ device_manager:add(HueSwitch.new({
|
||||
local hallway_light_automation = {
|
||||
timeout = Timeout.new(),
|
||||
forced = false,
|
||||
switch_callback = function(self, on)
|
||||
self.timeout:cancel()
|
||||
self.group.set_on(on)
|
||||
self.forced = on
|
||||
end,
|
||||
door_callback = function(self, open)
|
||||
if open then
|
||||
switch_callback = function(self)
|
||||
return function(_, on)
|
||||
self.timeout:cancel()
|
||||
|
||||
self.group.set_on(true)
|
||||
elseif not self.forced then
|
||||
self.timeout:start(debug and 10 or 2 * 60, function()
|
||||
if self.trash == nil or self.trash:open_percent() == 0 then
|
||||
self.group.set_on(false)
|
||||
end
|
||||
end)
|
||||
self.group.set_on(on)
|
||||
self.forced = on
|
||||
end
|
||||
end,
|
||||
trash_callback = function(self, open)
|
||||
if open then
|
||||
self.group.set_on(true)
|
||||
else
|
||||
if
|
||||
not self.timeout:is_waiting()
|
||||
and (self.door == nil or self.door:open_percent() == 0)
|
||||
and not self.forced
|
||||
then
|
||||
self.group.set_on(false)
|
||||
door_callback = function(self)
|
||||
return function(_, open)
|
||||
if open then
|
||||
self.timeout:cancel()
|
||||
|
||||
self.group.set_on(true)
|
||||
elseif not self.forced then
|
||||
self.timeout:start(debug and 10 or 2 * 60, function()
|
||||
if self.trash == nil or self.trash:open_percent() == 0 then
|
||||
self.group.set_on(false)
|
||||
end
|
||||
end)
|
||||
end
|
||||
end
|
||||
end,
|
||||
light_callback = function(self, on)
|
||||
if
|
||||
on
|
||||
and (self.trash == nil or self.trash:open_percent()) == 0
|
||||
and (self.door == nil or self.door:open_percent() == 0)
|
||||
then
|
||||
-- If the door and trash are not open, that means the light got turned on manually
|
||||
self.timeout:cancel()
|
||||
self.forced = true
|
||||
elseif not on then
|
||||
-- The light is never forced when it is off
|
||||
self.forced = false
|
||||
trash_callback = function(self)
|
||||
return function(_, open)
|
||||
if open then
|
||||
self.group.set_on(true)
|
||||
else
|
||||
if
|
||||
not self.timeout:is_waiting()
|
||||
and (self.door == nil or self.door:open_percent() == 0)
|
||||
and not self.forced
|
||||
then
|
||||
self.group.set_on(false)
|
||||
end
|
||||
end
|
||||
end
|
||||
end,
|
||||
light_callback = function(self)
|
||||
return function(_, state)
|
||||
if
|
||||
state.on
|
||||
and (self.trash == nil or self.trash:open_percent()) == 0
|
||||
and (self.door == nil or self.door:open_percent() == 0)
|
||||
then
|
||||
-- If the door and trash are not open, that means the light got turned on manually
|
||||
self.timeout:cancel()
|
||||
self.forced = true
|
||||
elseif not state.on then
|
||||
-- The light is never forced when it is off
|
||||
self.forced = false
|
||||
end
|
||||
end
|
||||
end,
|
||||
}
|
||||
@@ -477,9 +485,7 @@ local hallway_storage = LightBrightness.new({
|
||||
room = "Hallway",
|
||||
topic = mqtt_z2m("hallway/storage"),
|
||||
client = mqtt_client,
|
||||
callback = function(_, state)
|
||||
hallway_light_automation:light_callback(state.state)
|
||||
end,
|
||||
callback = hallway_light_automation:light_callback(),
|
||||
})
|
||||
turn_off_when_away(hallway_storage)
|
||||
device_manager:add(hallway_storage)
|
||||
@@ -504,13 +510,12 @@ hallway_light_automation.group = {
|
||||
end,
|
||||
}
|
||||
|
||||
local frontdoor_presence = {
|
||||
timeout = Timeout.new(),
|
||||
}
|
||||
setmetatable(frontdoor_presence, {
|
||||
__call = function(self, open)
|
||||
local function presence(duration)
|
||||
local timeout = Timeout.new()
|
||||
|
||||
return function(_, open)
|
||||
if open then
|
||||
self.timeout:cancel()
|
||||
timeout:cancel()
|
||||
|
||||
if not presence_system:overall_presence() then
|
||||
mqtt_client:send_message(mqtt_automation("presence/contact/frontdoor"), {
|
||||
@@ -519,21 +524,19 @@ setmetatable(frontdoor_presence, {
|
||||
})
|
||||
end
|
||||
else
|
||||
self.timeout:start(debug and 10 or 15 * 60, function()
|
||||
mqtt_client:send_message(mqtt_automation("presence/contact/frontdoor"), {})
|
||||
timeout:start(duration, function()
|
||||
mqtt_client:send_message(mqtt_automation("presence/contact/frontdoor"), nil)
|
||||
end)
|
||||
end
|
||||
end,
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
device_manager:add(IkeaRemote.new({
|
||||
name = "Remote",
|
||||
room = "Hallway",
|
||||
client = mqtt_client,
|
||||
topic = mqtt_z2m("hallway/remote"),
|
||||
callback = function(_, on)
|
||||
hallway_light_automation:switch_callback(on)
|
||||
end,
|
||||
callback = hallway_light_automation:switch_callback(),
|
||||
battery_callback = check_battery,
|
||||
}))
|
||||
local hallway_frontdoor = ContactSensor.new({
|
||||
@@ -542,14 +545,10 @@ local hallway_frontdoor = ContactSensor.new({
|
||||
sensor_type = "Door",
|
||||
topic = mqtt_z2m("hallway/frontdoor"),
|
||||
client = mqtt_client,
|
||||
presence = {
|
||||
topic = mqtt_automation("presence/contact/frontdoor"),
|
||||
timeout = debug and 10 or 15 * 60,
|
||||
callback = {
|
||||
presence(debug and 10 or 15 * 60),
|
||||
hallway_light_automation:door_callback(),
|
||||
},
|
||||
callback = function(_, open)
|
||||
hallway_light_automation:door_callback(open)
|
||||
frontdoor_presence(open)
|
||||
end,
|
||||
battery_callback = check_battery,
|
||||
})
|
||||
device_manager:add(hallway_frontdoor)
|
||||
@@ -561,9 +560,7 @@ local hallway_trash = ContactSensor.new({
|
||||
sensor_type = "Drawer",
|
||||
topic = mqtt_z2m("hallway/trash"),
|
||||
client = mqtt_client,
|
||||
callback = function(_, open)
|
||||
hallway_light_automation:trash_callback(open)
|
||||
end,
|
||||
callback = hallway_light_automation:trash_callback(),
|
||||
battery_callback = check_battery,
|
||||
})
|
||||
device_manager:add(hallway_trash)
|
||||
|
||||
@@ -7,7 +7,7 @@ mod web;
|
||||
use std::net::SocketAddr;
|
||||
use std::path::Path;
|
||||
use std::process;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
||||
|
||||
use ::config::{Environment, File};
|
||||
use automation_lib::config::{FulfillmentConfig, MqttConfig};
|
||||
@@ -172,6 +172,11 @@ async fn app() -> anyhow::Result<()> {
|
||||
.as_millis())
|
||||
})?;
|
||||
utils.set("get_epoch", get_epoch)?;
|
||||
let sleep = lua.create_async_function(async |_lua, duration: u64| {
|
||||
tokio::time::sleep(Duration::from_millis(duration)).await;
|
||||
Ok(())
|
||||
})?;
|
||||
utils.set("sleep", sleep)?;
|
||||
lua.register_module("utils", utils)?;
|
||||
|
||||
automation_devices::register_with_lua(&lua)?;
|
||||
|
||||
Reference in New Issue
Block a user