Compare commits
7 Commits
60bf0f236c
...
5271e5ad81
| Author | SHA1 | Date | |
|---|---|---|---|
|
5271e5ad81
|
|||
|
1d28b43264
|
|||
|
da04fad520
|
|||
|
84e4b30b6a
|
|||
|
95a8a377e8
|
|||
|
23355190ca
|
|||
|
2dbd491b81
|
14
Cargo.lock
generated
14
Cargo.lock
generated
@@ -99,7 +99,6 @@ dependencies = [
|
|||||||
"dotenvy",
|
"dotenvy",
|
||||||
"git-version",
|
"git-version",
|
||||||
"google_home",
|
"google_home",
|
||||||
"hostname",
|
|
||||||
"mlua",
|
"mlua",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
"rumqttc",
|
"rumqttc",
|
||||||
@@ -128,6 +127,7 @@ dependencies = [
|
|||||||
"dyn-clone",
|
"dyn-clone",
|
||||||
"eui48",
|
"eui48",
|
||||||
"google_home",
|
"google_home",
|
||||||
|
"inventory",
|
||||||
"mlua",
|
"mlua",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
"rumqttc",
|
"rumqttc",
|
||||||
@@ -150,7 +150,9 @@ dependencies = [
|
|||||||
"dyn-clone",
|
"dyn-clone",
|
||||||
"futures",
|
"futures",
|
||||||
"google_home",
|
"google_home",
|
||||||
|
"hostname",
|
||||||
"indexmap",
|
"indexmap",
|
||||||
|
"inventory",
|
||||||
"mlua",
|
"mlua",
|
||||||
"rumqttc",
|
"rumqttc",
|
||||||
"serde",
|
"serde",
|
||||||
@@ -167,6 +169,7 @@ name = "automation_macro"
|
|||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"itertools",
|
"itertools",
|
||||||
|
"mlua",
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.106",
|
"syn 2.0.106",
|
||||||
@@ -966,6 +969,15 @@ dependencies = [
|
|||||||
"serde",
|
"serde",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "inventory"
|
||||||
|
version = "0.3.21"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "bc61209c082fbeb19919bee74b176221b27223e27b65d781eb91af24eb1fb46e"
|
||||||
|
dependencies = [
|
||||||
|
"rustversion",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "io-uring"
|
name = "io-uring"
|
||||||
version = "0.7.10"
|
version = "0.7.10"
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ google_home = { path = "./google_home/google_home" }
|
|||||||
google_home_macro = { path = "./google_home/google_home_macro" }
|
google_home_macro = { path = "./google_home/google_home_macro" }
|
||||||
hostname = "0.4.1"
|
hostname = "0.4.1"
|
||||||
indexmap = { version = "2.11.0", features = ["serde"] }
|
indexmap = { version = "2.11.0", features = ["serde"] }
|
||||||
|
inventory = "0.3.21"
|
||||||
itertools = "0.14.0"
|
itertools = "0.14.0"
|
||||||
json_value_merge = "2.0.1"
|
json_value_merge = "2.0.1"
|
||||||
mlua = { version = "0.11.3", features = [
|
mlua = { version = "0.11.3", features = [
|
||||||
@@ -54,7 +55,7 @@ rumqttc = "0.24.0"
|
|||||||
serde = { version = "1.0.219", features = ["derive"] }
|
serde = { version = "1.0.219", features = ["derive"] }
|
||||||
serde_json = "1.0.143"
|
serde_json = "1.0.143"
|
||||||
serde_repr = "0.1.20"
|
serde_repr = "0.1.20"
|
||||||
syn = { version = "2.0.106", features = ["extra-traits", "full"] }
|
syn = { version = "2.0.106" }
|
||||||
thiserror = "2.0.16"
|
thiserror = "2.0.16"
|
||||||
tokio = { version = "1", features = ["rt-multi-thread"] }
|
tokio = { version = "1", features = ["rt-multi-thread"] }
|
||||||
tokio-cron-scheduler = "0.14.0"
|
tokio-cron-scheduler = "0.14.0"
|
||||||
@@ -76,7 +77,6 @@ config = { version = "0.15.15", default-features = false, features = [
|
|||||||
dotenvy = { workspace = true }
|
dotenvy = { workspace = true }
|
||||||
git-version = "0.3.9"
|
git-version = "0.3.9"
|
||||||
google_home = { workspace = true }
|
google_home = { workspace = true }
|
||||||
hostname = { workspace = true }
|
|
||||||
mlua = { workspace = true }
|
mlua = { workspace = true }
|
||||||
reqwest = { workspace = true }
|
reqwest = { workspace = true }
|
||||||
rumqttc = { workspace = true }
|
rumqttc = { workspace = true }
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ bytes = { workspace = true }
|
|||||||
dyn-clone = { workspace = true }
|
dyn-clone = { workspace = true }
|
||||||
eui48 = { workspace = true }
|
eui48 = { workspace = true }
|
||||||
google_home = { workspace = true }
|
google_home = { workspace = true }
|
||||||
|
inventory = { workspace = true }
|
||||||
mlua = { workspace = true }
|
mlua = { workspace = true }
|
||||||
reqwest = { workspace = true }
|
reqwest = { workspace = true }
|
||||||
rumqttc = { workspace = true }
|
rumqttc = { workspace = true }
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use automation_lib::config::InfoConfig;
|
use automation_lib::config::InfoConfig;
|
||||||
use automation_lib::device::{Device, LuaDeviceCreate};
|
use automation_lib::device::{Device, LuaDeviceCreate};
|
||||||
use automation_macro::{LuaDevice, LuaDeviceConfig};
|
use automation_macro::{Device, LuaDeviceConfig};
|
||||||
use google_home::device::Name;
|
use google_home::device::Name;
|
||||||
use google_home::errors::ErrorCode;
|
use google_home::errors::ErrorCode;
|
||||||
use google_home::traits::{
|
use google_home::traits::{
|
||||||
@@ -19,8 +19,8 @@ pub struct Config {
|
|||||||
pub url: String,
|
pub url: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, LuaDevice)]
|
#[derive(Debug, Clone, Device)]
|
||||||
#[traits(OnOff)]
|
#[device(traits(OnOff))]
|
||||||
pub struct AirFilter {
|
pub struct AirFilter {
|
||||||
config: Config,
|
config: Config,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ use automation_lib::error::DeviceConfigError;
|
|||||||
use automation_lib::event::OnMqtt;
|
use automation_lib::event::OnMqtt;
|
||||||
use automation_lib::messages::ContactMessage;
|
use automation_lib::messages::ContactMessage;
|
||||||
use automation_lib::mqtt::WrappedAsyncClient;
|
use automation_lib::mqtt::WrappedAsyncClient;
|
||||||
use automation_macro::{LuaDevice, LuaDeviceConfig};
|
use automation_macro::{Device, LuaDeviceConfig};
|
||||||
use google_home::device;
|
use google_home::device;
|
||||||
use google_home::errors::{DeviceError, ErrorCode};
|
use google_home::errors::{DeviceError, ErrorCode};
|
||||||
use google_home::traits::OpenClose;
|
use google_home::traits::OpenClose;
|
||||||
@@ -48,8 +48,8 @@ struct State {
|
|||||||
is_closed: bool,
|
is_closed: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, LuaDevice)]
|
#[derive(Debug, Clone, Device)]
|
||||||
#[traits(OpenClose)]
|
#[device(traits(OpenClose))]
|
||||||
pub struct ContactSensor {
|
pub struct ContactSensor {
|
||||||
config: Config,
|
config: Config,
|
||||||
state: Arc<RwLock<State>>,
|
state: Arc<RwLock<State>>,
|
||||||
|
|||||||
@@ -3,8 +3,7 @@ use std::net::SocketAddr;
|
|||||||
|
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use automation_lib::device::{Device, LuaDeviceCreate};
|
use automation_lib::device::{Device, LuaDeviceCreate};
|
||||||
use automation_lib::lua::traits::AddAdditionalMethods;
|
use automation_macro::{Device, LuaDeviceConfig};
|
||||||
use automation_macro::{LuaDevice, LuaDeviceConfig};
|
|
||||||
use mlua::LuaSerdeExt;
|
use mlua::LuaSerdeExt;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use tracing::{error, trace, warn};
|
use tracing::{error, trace, warn};
|
||||||
@@ -31,8 +30,8 @@ pub struct Config {
|
|||||||
pub flags: FlagIDs,
|
pub flags: FlagIDs,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, LuaDevice)]
|
#[derive(Debug, Clone, Device)]
|
||||||
#[traits(AddAdditionalMethods)]
|
#[device(add_methods(Self::add_methods))]
|
||||||
pub struct HueBridge {
|
pub struct HueBridge {
|
||||||
config: Config,
|
config: Config,
|
||||||
}
|
}
|
||||||
@@ -84,19 +83,8 @@ impl HueBridge {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl Device for HueBridge {
|
fn add_methods<M: mlua::UserDataMethods<Self>>(methods: &mut M) {
|
||||||
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,
|
|
||||||
{
|
|
||||||
methods.add_async_method(
|
methods.add_async_method(
|
||||||
"set_flag",
|
"set_flag",
|
||||||
async |lua, this, (flag, value): (mlua::Value, bool)| {
|
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 anyhow::Result;
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use automation_macro::{LuaDevice, LuaDeviceConfig};
|
use automation_macro::{Device, LuaDeviceConfig};
|
||||||
use google_home::errors::ErrorCode;
|
use google_home::errors::ErrorCode;
|
||||||
use google_home::traits::OnOff;
|
use google_home::traits::OnOff;
|
||||||
use tracing::{error, trace, warn};
|
use tracing::{error, trace, warn};
|
||||||
@@ -19,8 +19,8 @@ pub struct Config {
|
|||||||
pub scene_id: String,
|
pub scene_id: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, LuaDevice)]
|
#[derive(Debug, Clone, Device)]
|
||||||
#[traits(OnOff)]
|
#[device(traits(OnOff))]
|
||||||
pub struct HueGroup {
|
pub struct HueGroup {
|
||||||
config: Config,
|
config: Config,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ use automation_lib::config::{InfoConfig, MqttDeviceConfig};
|
|||||||
use automation_lib::device::{Device, LuaDeviceCreate};
|
use automation_lib::device::{Device, LuaDeviceCreate};
|
||||||
use automation_lib::event::OnMqtt;
|
use automation_lib::event::OnMqtt;
|
||||||
use automation_lib::mqtt::WrappedAsyncClient;
|
use automation_lib::mqtt::WrappedAsyncClient;
|
||||||
use automation_macro::{LuaDevice, LuaDeviceConfig};
|
use automation_macro::{Device, LuaDeviceConfig};
|
||||||
use rumqttc::{Publish, matches};
|
use rumqttc::{Publish, matches};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use tracing::{debug, trace, warn};
|
use tracing::{debug, trace, warn};
|
||||||
@@ -55,7 +55,7 @@ struct State {
|
|||||||
battery: Option<f32>,
|
battery: Option<f32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, LuaDevice)]
|
#[derive(Debug, Clone, Device)]
|
||||||
pub struct HueSwitch {
|
pub struct HueSwitch {
|
||||||
config: Config,
|
config: Config,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ use automation_lib::device::{Device, LuaDeviceCreate};
|
|||||||
use automation_lib::event::OnMqtt;
|
use automation_lib::event::OnMqtt;
|
||||||
use automation_lib::messages::{RemoteAction, RemoteMessage};
|
use automation_lib::messages::{RemoteAction, RemoteMessage};
|
||||||
use automation_lib::mqtt::WrappedAsyncClient;
|
use automation_lib::mqtt::WrappedAsyncClient;
|
||||||
use automation_macro::{LuaDevice, LuaDeviceConfig};
|
use automation_macro::{Device, LuaDeviceConfig};
|
||||||
use rumqttc::{Publish, matches};
|
use rumqttc::{Publish, matches};
|
||||||
use tracing::{debug, error, trace};
|
use tracing::{debug, error, trace};
|
||||||
|
|
||||||
@@ -29,7 +29,7 @@ pub struct Config {
|
|||||||
pub battery_callback: ActionCallback<(IkeaRemote, f32)>,
|
pub battery_callback: ActionCallback<(IkeaRemote, f32)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, LuaDevice)]
|
#[derive(Debug, Clone, Device)]
|
||||||
pub struct IkeaRemote {
|
pub struct IkeaRemote {
|
||||||
config: Config,
|
config: Config,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ use std::str::Utf8Error;
|
|||||||
|
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use automation_lib::device::{Device, LuaDeviceCreate};
|
use automation_lib::device::{Device, LuaDeviceCreate};
|
||||||
use automation_macro::{LuaDevice, LuaDeviceConfig};
|
use automation_macro::{Device, LuaDeviceConfig};
|
||||||
use bytes::{Buf, BufMut};
|
use bytes::{Buf, BufMut};
|
||||||
use google_home::errors::{self, DeviceError};
|
use google_home::errors::{self, DeviceError};
|
||||||
use google_home::traits::OnOff;
|
use google_home::traits::OnOff;
|
||||||
@@ -21,8 +21,8 @@ pub struct Config {
|
|||||||
pub addr: SocketAddr,
|
pub addr: SocketAddr,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, LuaDevice)]
|
#[derive(Debug, Clone, Device)]
|
||||||
#[traits(OnOff)]
|
#[device(traits(OnOff))]
|
||||||
pub struct KasaOutlet {
|
pub struct KasaOutlet {
|
||||||
config: Config,
|
config: Config,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ mod wake_on_lan;
|
|||||||
mod washer;
|
mod washer;
|
||||||
mod zigbee;
|
mod zigbee;
|
||||||
|
|
||||||
|
use automation_lib::Module;
|
||||||
use automation_lib::device::{Device, LuaDeviceCreate};
|
use automation_lib::device::{Device, LuaDeviceCreate};
|
||||||
use zigbee::light::{LightBrightness, LightColorTemperature, LightOnOff};
|
use zigbee::light::{LightBrightness, LightColorTemperature, LightOnOff};
|
||||||
use zigbee::outlet::{OutletOnOff, OutletPower};
|
use zigbee::outlet::{OutletOnOff, OutletPower};
|
||||||
@@ -30,30 +31,33 @@ pub use self::wake_on_lan::WakeOnLAN;
|
|||||||
pub use self::washer::Washer;
|
pub use self::washer::Washer;
|
||||||
|
|
||||||
macro_rules! register_device {
|
macro_rules! register_device {
|
||||||
($lua:expr, $device:ty) => {
|
($lua:expr, $table:expr, $device:ty) => {
|
||||||
$lua.globals()
|
$table.set(stringify!($device), $lua.create_proxy::<$device>()?)?;
|
||||||
.set(stringify!($device), $lua.create_proxy::<$device>()?)?;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn register_with_lua(lua: &mlua::Lua) -> mlua::Result<()> {
|
pub fn create_module(lua: &mlua::Lua) -> mlua::Result<mlua::Table> {
|
||||||
register_device!(lua, AirFilter);
|
let devices = lua.create_table()?;
|
||||||
register_device!(lua, ContactSensor);
|
|
||||||
register_device!(lua, HueBridge);
|
|
||||||
register_device!(lua, HueGroup);
|
|
||||||
register_device!(lua, HueSwitch);
|
|
||||||
register_device!(lua, IkeaRemote);
|
|
||||||
register_device!(lua, KasaOutlet);
|
|
||||||
register_device!(lua, LightBrightness);
|
|
||||||
register_device!(lua, LightColorTemperature);
|
|
||||||
register_device!(lua, LightOnOff);
|
|
||||||
register_device!(lua, LightSensor);
|
|
||||||
register_device!(lua, Ntfy);
|
|
||||||
register_device!(lua, OutletOnOff);
|
|
||||||
register_device!(lua, OutletPower);
|
|
||||||
register_device!(lua, Presence);
|
|
||||||
register_device!(lua, WakeOnLAN);
|
|
||||||
register_device!(lua, Washer);
|
|
||||||
|
|
||||||
Ok(())
|
register_device!(lua, devices, AirFilter);
|
||||||
|
register_device!(lua, devices, ContactSensor);
|
||||||
|
register_device!(lua, devices, HueBridge);
|
||||||
|
register_device!(lua, devices, HueGroup);
|
||||||
|
register_device!(lua, devices, HueSwitch);
|
||||||
|
register_device!(lua, devices, IkeaRemote);
|
||||||
|
register_device!(lua, devices, KasaOutlet);
|
||||||
|
register_device!(lua, devices, LightBrightness);
|
||||||
|
register_device!(lua, devices, LightColorTemperature);
|
||||||
|
register_device!(lua, devices, LightOnOff);
|
||||||
|
register_device!(lua, devices, LightSensor);
|
||||||
|
register_device!(lua, devices, Ntfy);
|
||||||
|
register_device!(lua, devices, OutletOnOff);
|
||||||
|
register_device!(lua, devices, OutletPower);
|
||||||
|
register_device!(lua, devices, Presence);
|
||||||
|
register_device!(lua, devices, WakeOnLAN);
|
||||||
|
register_device!(lua, devices, Washer);
|
||||||
|
|
||||||
|
Ok(devices)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inventory::submit! {Module::new("devices", create_module)}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ use automation_lib::device::{Device, LuaDeviceCreate};
|
|||||||
use automation_lib::event::OnMqtt;
|
use automation_lib::event::OnMqtt;
|
||||||
use automation_lib::messages::BrightnessMessage;
|
use automation_lib::messages::BrightnessMessage;
|
||||||
use automation_lib::mqtt::WrappedAsyncClient;
|
use automation_lib::mqtt::WrappedAsyncClient;
|
||||||
use automation_macro::{LuaDevice, LuaDeviceConfig};
|
use automation_macro::{Device, LuaDeviceConfig};
|
||||||
use rumqttc::Publish;
|
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};
|
||||||
@@ -34,7 +34,7 @@ pub struct State {
|
|||||||
is_dark: bool,
|
is_dark: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, LuaDevice)]
|
#[derive(Debug, Clone, Device)]
|
||||||
pub struct LightSensor {
|
pub struct LightSensor {
|
||||||
config: Config,
|
config: Config,
|
||||||
state: Arc<RwLock<State>>,
|
state: Arc<RwLock<State>>,
|
||||||
|
|||||||
@@ -3,8 +3,7 @@ use std::convert::Infallible;
|
|||||||
|
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use automation_lib::device::{Device, LuaDeviceCreate};
|
use automation_lib::device::{Device, LuaDeviceCreate};
|
||||||
use automation_lib::lua::traits::AddAdditionalMethods;
|
use automation_macro::{Device, LuaDeviceConfig};
|
||||||
use automation_macro::{LuaDevice, LuaDeviceConfig};
|
|
||||||
use mlua::LuaSerdeExt;
|
use mlua::LuaSerdeExt;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_repr::*;
|
use serde_repr::*;
|
||||||
@@ -118,12 +117,27 @@ pub struct Config {
|
|||||||
pub topic: String,
|
pub topic: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, LuaDevice)]
|
#[derive(Debug, Clone, Device)]
|
||||||
#[traits(AddAdditionalMethods)]
|
#[device(add_methods(Self::add_methods))]
|
||||||
pub struct Ntfy {
|
pub struct Ntfy {
|
||||||
config: Config,
|
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]
|
#[async_trait]
|
||||||
impl LuaDeviceCreate for Ntfy {
|
impl LuaDeviceCreate for Ntfy {
|
||||||
type Config = Config;
|
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::config::MqttDeviceConfig;
|
||||||
use automation_lib::device::{Device, LuaDeviceCreate};
|
use automation_lib::device::{Device, LuaDeviceCreate};
|
||||||
use automation_lib::event::OnMqtt;
|
use automation_lib::event::OnMqtt;
|
||||||
use automation_lib::lua::traits::AddAdditionalMethods;
|
|
||||||
use automation_lib::messages::PresenceMessage;
|
use automation_lib::messages::PresenceMessage;
|
||||||
use automation_lib::mqtt::WrappedAsyncClient;
|
use automation_lib::mqtt::WrappedAsyncClient;
|
||||||
use automation_macro::{LuaDevice, LuaDeviceConfig};
|
use automation_macro::{Device, LuaDeviceConfig};
|
||||||
use rumqttc::Publish;
|
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};
|
||||||
@@ -34,8 +33,8 @@ pub struct State {
|
|||||||
current_overall_presence: bool,
|
current_overall_presence: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, LuaDevice)]
|
#[derive(Debug, Clone, Device)]
|
||||||
#[traits(AddAdditionalMethods)]
|
#[device(add_methods(Self::add_methods))]
|
||||||
pub struct Presence {
|
pub struct Presence {
|
||||||
config: Config,
|
config: Config,
|
||||||
state: Arc<RwLock<State>>,
|
state: Arc<RwLock<State>>,
|
||||||
@@ -49,6 +48,12 @@ impl Presence {
|
|||||||
async fn state_mut(&self) -> RwLockWriteGuard<'_, State> {
|
async fn state_mut(&self) -> RwLockWriteGuard<'_, State> {
|
||||||
self.state.write().await
|
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]
|
#[async_trait]
|
||||||
@@ -125,14 +130,3 @@ impl OnMqtt for Presence {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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::event::OnMqtt;
|
||||||
use automation_lib::messages::ActivateMessage;
|
use automation_lib::messages::ActivateMessage;
|
||||||
use automation_lib::mqtt::WrappedAsyncClient;
|
use automation_lib::mqtt::WrappedAsyncClient;
|
||||||
use automation_macro::{LuaDevice, LuaDeviceConfig};
|
use automation_macro::{Device, LuaDeviceConfig};
|
||||||
use eui48::MacAddress;
|
use eui48::MacAddress;
|
||||||
use google_home::device;
|
use google_home::device;
|
||||||
use google_home::errors::ErrorCode;
|
use google_home::errors::ErrorCode;
|
||||||
@@ -28,7 +28,7 @@ pub struct Config {
|
|||||||
pub client: WrappedAsyncClient,
|
pub client: WrappedAsyncClient,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, LuaDevice)]
|
#[derive(Debug, Clone, Device)]
|
||||||
pub struct WakeOnLAN {
|
pub struct WakeOnLAN {
|
||||||
config: Config,
|
config: Config,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ use automation_lib::device::{Device, LuaDeviceCreate};
|
|||||||
use automation_lib::event::OnMqtt;
|
use automation_lib::event::OnMqtt;
|
||||||
use automation_lib::messages::PowerMessage;
|
use automation_lib::messages::PowerMessage;
|
||||||
use automation_lib::mqtt::WrappedAsyncClient;
|
use automation_lib::mqtt::WrappedAsyncClient;
|
||||||
use automation_macro::{LuaDevice, LuaDeviceConfig};
|
use automation_macro::{Device, 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};
|
||||||
@@ -33,7 +33,7 @@ pub struct State {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Add google home integration
|
// TODO: Add google home integration
|
||||||
#[derive(Debug, Clone, LuaDevice)]
|
#[derive(Debug, Clone, Device)]
|
||||||
pub struct Washer {
|
pub struct Washer {
|
||||||
config: Config,
|
config: Config,
|
||||||
state: Arc<RwLock<State>>,
|
state: Arc<RwLock<State>>,
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ use automation_lib::device::{Device, LuaDeviceCreate};
|
|||||||
use automation_lib::event::OnMqtt;
|
use automation_lib::event::OnMqtt;
|
||||||
use automation_lib::helpers::serialization::state_deserializer;
|
use automation_lib::helpers::serialization::state_deserializer;
|
||||||
use automation_lib::mqtt::WrappedAsyncClient;
|
use automation_lib::mqtt::WrappedAsyncClient;
|
||||||
use automation_macro::{LuaDevice, LuaDeviceConfig, LuaSerialize};
|
use automation_macro::{Device, LuaDeviceConfig, LuaSerialize};
|
||||||
use google_home::device;
|
use google_home::device;
|
||||||
use google_home::errors::ErrorCode;
|
use google_home::errors::ErrorCode;
|
||||||
use google_home::traits::{Brightness, Color, ColorSetting, ColorTemperatureRange, OnOff};
|
use google_home::traits::{Brightness, Color, ColorSetting, ColorTemperatureRange, OnOff};
|
||||||
@@ -88,10 +88,10 @@ impl From<StateColorTemperature> for StateBrightness {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, LuaDevice)]
|
#[derive(Debug, Clone, Device)]
|
||||||
#[traits(<StateOnOff>: OnOff)]
|
#[device(traits(OnOff for <StateOnOff>, <StateBrightness>, <StateColorTemperature>))]
|
||||||
#[traits(<StateBrightness>: OnOff, Brightness)]
|
#[device(traits(Brightness for <StateBrightness>, <StateColorTemperature>))]
|
||||||
#[traits(<StateColorTemperature>: OnOff, Brightness, ColorSetting)]
|
#[device(traits(ColorSetting for <StateColorTemperature>))]
|
||||||
pub struct Light<T: LightState> {
|
pub struct Light<T: LightState> {
|
||||||
config: Config<T>,
|
config: Config<T>,
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ use automation_lib::device::{Device, LuaDeviceCreate};
|
|||||||
use automation_lib::event::OnMqtt;
|
use automation_lib::event::OnMqtt;
|
||||||
use automation_lib::helpers::serialization::state_deserializer;
|
use automation_lib::helpers::serialization::state_deserializer;
|
||||||
use automation_lib::mqtt::WrappedAsyncClient;
|
use automation_lib::mqtt::WrappedAsyncClient;
|
||||||
use automation_macro::{LuaDevice, LuaDeviceConfig, LuaSerialize};
|
use automation_macro::{Device, LuaDeviceConfig, LuaSerialize};
|
||||||
use google_home::device;
|
use google_home::device;
|
||||||
use google_home::errors::ErrorCode;
|
use google_home::errors::ErrorCode;
|
||||||
use google_home::traits::OnOff;
|
use google_home::traits::OnOff;
|
||||||
@@ -80,9 +80,8 @@ impl From<StatePower> for StateOnOff {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, LuaDevice)]
|
#[derive(Debug, Clone, Device)]
|
||||||
#[traits(<StateOnOff>: OnOff)]
|
#[device(traits(OnOff for <StateOnOff>, <StatePower>))]
|
||||||
#[traits(<StatePower>: OnOff)]
|
|
||||||
pub struct Outlet<T: OutletState> {
|
pub struct Outlet<T: OutletState> {
|
||||||
config: Config<T>,
|
config: Config<T>,
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,9 @@ bytes = { workspace = true }
|
|||||||
dyn-clone = { workspace = true }
|
dyn-clone = { workspace = true }
|
||||||
futures = { workspace = true }
|
futures = { workspace = true }
|
||||||
google_home = { workspace = true }
|
google_home = { workspace = true }
|
||||||
|
hostname = { workspace = true }
|
||||||
indexmap = { workspace = true }
|
indexmap = { workspace = true }
|
||||||
|
inventory = { workspace = true }
|
||||||
mlua = { workspace = true }
|
mlua = { workspace = true }
|
||||||
rumqttc = { workspace = true }
|
rumqttc = { workspace = true }
|
||||||
serde = { workspace = true }
|
serde = { workspace = true }
|
||||||
|
|||||||
@@ -1,11 +1 @@
|
|||||||
pub mod serialization;
|
pub mod serialization;
|
||||||
mod timeout;
|
|
||||||
|
|
||||||
pub use timeout::Timeout;
|
|
||||||
|
|
||||||
pub fn register_with_lua(lua: &mlua::Lua) -> mlua::Result<()> {
|
|
||||||
lua.globals()
|
|
||||||
.set("Timeout", lua.create_proxy::<Timeout>()?)?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
#![allow(incomplete_features)]
|
#![allow(incomplete_features)]
|
||||||
#![feature(iterator_try_collect)]
|
#![feature(iterator_try_collect)]
|
||||||
|
|
||||||
|
use tracing::debug;
|
||||||
|
|
||||||
pub mod action_callback;
|
pub mod action_callback;
|
||||||
pub mod config;
|
pub mod config;
|
||||||
pub mod device;
|
pub mod device;
|
||||||
@@ -12,3 +14,37 @@ pub mod lua;
|
|||||||
pub mod messages;
|
pub mod messages;
|
||||||
pub mod mqtt;
|
pub mod mqtt;
|
||||||
pub mod schedule;
|
pub mod schedule;
|
||||||
|
|
||||||
|
type RegisterFn = fn(lua: &mlua::Lua) -> mlua::Result<mlua::Table>;
|
||||||
|
|
||||||
|
pub struct Module {
|
||||||
|
name: &'static str,
|
||||||
|
register_fn: RegisterFn,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Module {
|
||||||
|
pub const fn new(name: &'static str, register_fn: RegisterFn) -> Self {
|
||||||
|
Self { name, register_fn }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const fn get_name(&self) -> &'static str {
|
||||||
|
self.name
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn register(&self, lua: &mlua::Lua) -> mlua::Result<mlua::Table> {
|
||||||
|
(self.register_fn)(lua)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn load_modules(lua: &mlua::Lua) -> mlua::Result<()> {
|
||||||
|
debug!("Loading modules...");
|
||||||
|
for module in inventory::iter::<Module> {
|
||||||
|
debug!(name = module.get_name(), "Registering");
|
||||||
|
let table = module.register(lua)?;
|
||||||
|
lua.register_module(module.get_name(), table)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
inventory::collect!(Module);
|
||||||
|
|||||||
@@ -1 +1,3 @@
|
|||||||
pub mod traits;
|
pub mod traits;
|
||||||
|
|
||||||
|
mod utils;
|
||||||
|
|||||||
@@ -78,9 +78,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;
|
|
||||||
}
|
|
||||||
|
|||||||
31
automation_lib/src/lua/utils/mod.rs
Normal file
31
automation_lib/src/lua/utils/mod.rs
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
mod timeout;
|
||||||
|
|
||||||
|
use std::time::{SystemTime, UNIX_EPOCH};
|
||||||
|
|
||||||
|
pub use timeout::Timeout;
|
||||||
|
|
||||||
|
use crate::Module;
|
||||||
|
|
||||||
|
fn create_module(lua: &mlua::Lua) -> mlua::Result<mlua::Table> {
|
||||||
|
let utils = lua.create_table()?;
|
||||||
|
|
||||||
|
utils.set("Timeout", lua.create_proxy::<Timeout>()?)?;
|
||||||
|
|
||||||
|
let get_hostname = lua.create_function(|_lua, ()| {
|
||||||
|
hostname::get()
|
||||||
|
.map(|name| name.to_str().unwrap_or("unknown").to_owned())
|
||||||
|
.map_err(mlua::ExternalError::into_lua_err)
|
||||||
|
})?;
|
||||||
|
utils.set("get_hostname", get_hostname)?;
|
||||||
|
let get_epoch = lua.create_function(|_lua, ()| {
|
||||||
|
Ok(SystemTime::now()
|
||||||
|
.duration_since(UNIX_EPOCH)
|
||||||
|
.expect("Time is after UNIX EPOCH")
|
||||||
|
.as_millis())
|
||||||
|
})?;
|
||||||
|
utils.set("get_epoch", get_epoch)?;
|
||||||
|
|
||||||
|
Ok(utils)
|
||||||
|
}
|
||||||
|
|
||||||
|
inventory::submit! {Module::new("utils", create_module)}
|
||||||
@@ -11,3 +11,6 @@ itertools = { workspace = true }
|
|||||||
proc-macro2 = { workspace = true }
|
proc-macro2 = { workspace = true }
|
||||||
quote = { workspace = true }
|
quote = { workspace = true }
|
||||||
syn = { 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)]
|
#![feature(iter_intersperse)]
|
||||||
mod impl_device;
|
#![feature(iterator_try_collect)]
|
||||||
|
mod device;
|
||||||
mod lua_device_config;
|
mod lua_device_config;
|
||||||
|
|
||||||
use lua_device_config::impl_lua_device_config_macro;
|
use lua_device_config::impl_lua_device_config_macro;
|
||||||
use quote::quote;
|
use quote::quote;
|
||||||
use syn::{DeriveInput, parse_macro_input};
|
use syn::{DeriveInput, parse_macro_input};
|
||||||
|
|
||||||
use crate::impl_device::impl_device_macro;
|
|
||||||
|
|
||||||
#[proc_macro_derive(LuaDeviceConfig, attributes(device_config))]
|
#[proc_macro_derive(LuaDeviceConfig, attributes(device_config))]
|
||||||
pub fn lua_device_config_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
pub fn lua_device_config_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||||
let ast = parse_macro_input!(input as DeriveInput);
|
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()
|
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))]
|
#[proc_macro_derive(LuaSerialize, attributes(traits))]
|
||||||
pub fn lua_serialize(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
pub fn lua_serialize(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||||
let ast = parse_macro_input!(input as DeriveInput);
|
let ast = parse_macro_input!(input as DeriveInput);
|
||||||
@@ -37,3 +29,45 @@ pub fn lua_serialize(input: proc_macro::TokenStream) -> proc_macro::TokenStream
|
|||||||
}
|
}
|
||||||
.into()
|
.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()
|
||||||
|
}
|
||||||
|
|||||||
91
config.lua
91
config.lua
@@ -1,3 +1,4 @@
|
|||||||
|
local devices = require("devices")
|
||||||
local device_manager = require("device_manager")
|
local device_manager = require("device_manager")
|
||||||
local utils = require("utils")
|
local utils = require("utils")
|
||||||
local secrets = require("secrets")
|
local secrets = require("secrets")
|
||||||
@@ -29,7 +30,7 @@ local mqtt_client = require("mqtt").new({
|
|||||||
tls = host == "zeus" or host == "hephaestus",
|
tls = host == "zeus" or host == "hephaestus",
|
||||||
})
|
})
|
||||||
|
|
||||||
local ntfy = Ntfy.new({
|
local ntfy = devices.Ntfy.new({
|
||||||
topic = secrets.ntfy_topic,
|
topic = secrets.ntfy_topic,
|
||||||
})
|
})
|
||||||
device_manager:add(ntfy)
|
device_manager:add(ntfy)
|
||||||
@@ -71,7 +72,7 @@ local on_presence = {
|
|||||||
end,
|
end,
|
||||||
}
|
}
|
||||||
|
|
||||||
local presence_system = Presence.new({
|
local presence_system = devices.Presence.new({
|
||||||
topic = mqtt_automation("presence/+/#"),
|
topic = mqtt_automation("presence/+/#"),
|
||||||
client = mqtt_client,
|
client = mqtt_client,
|
||||||
callback = function(_, presence)
|
callback = function(_, presence)
|
||||||
@@ -122,7 +123,7 @@ local on_light = {
|
|||||||
self[#self + 1] = f
|
self[#self + 1] = f
|
||||||
end,
|
end,
|
||||||
}
|
}
|
||||||
device_manager:add(LightSensor.new({
|
device_manager:add(devices.LightSensor.new({
|
||||||
identifier = "living_light_sensor",
|
identifier = "living_light_sensor",
|
||||||
topic = mqtt_z2m("living/light"),
|
topic = mqtt_z2m("living/light"),
|
||||||
client = mqtt_client,
|
client = mqtt_client,
|
||||||
@@ -146,7 +147,7 @@ end)
|
|||||||
local hue_ip = "10.0.0.102"
|
local hue_ip = "10.0.0.102"
|
||||||
local hue_token = secrets.hue_token
|
local hue_token = secrets.hue_token
|
||||||
|
|
||||||
local hue_bridge = HueBridge.new({
|
local hue_bridge = devices.HueBridge.new({
|
||||||
identifier = "hue_bridge",
|
identifier = "hue_bridge",
|
||||||
ip = hue_ip,
|
ip = hue_ip,
|
||||||
login = hue_token,
|
login = hue_token,
|
||||||
@@ -163,7 +164,7 @@ on_presence:add(function(presence)
|
|||||||
hue_bridge:set_flag("presence", presence)
|
hue_bridge:set_flag("presence", presence)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
local kitchen_lights = HueGroup.new({
|
local kitchen_lights = devices.HueGroup.new({
|
||||||
identifier = "kitchen_lights",
|
identifier = "kitchen_lights",
|
||||||
ip = hue_ip,
|
ip = hue_ip,
|
||||||
login = hue_token,
|
login = hue_token,
|
||||||
@@ -171,7 +172,7 @@ local kitchen_lights = HueGroup.new({
|
|||||||
scene_id = "7MJLG27RzeRAEVJ",
|
scene_id = "7MJLG27RzeRAEVJ",
|
||||||
})
|
})
|
||||||
device_manager:add(kitchen_lights)
|
device_manager:add(kitchen_lights)
|
||||||
local living_lights = HueGroup.new({
|
local living_lights = devices.HueGroup.new({
|
||||||
identifier = "living_lights",
|
identifier = "living_lights",
|
||||||
ip = hue_ip,
|
ip = hue_ip,
|
||||||
login = hue_token,
|
login = hue_token,
|
||||||
@@ -179,7 +180,7 @@ local living_lights = HueGroup.new({
|
|||||||
scene_id = "SNZw7jUhQ3cXSjkj",
|
scene_id = "SNZw7jUhQ3cXSjkj",
|
||||||
})
|
})
|
||||||
device_manager:add(living_lights)
|
device_manager:add(living_lights)
|
||||||
local living_lights_relax = HueGroup.new({
|
local living_lights_relax = devices.HueGroup.new({
|
||||||
identifier = "living_lights",
|
identifier = "living_lights",
|
||||||
ip = hue_ip,
|
ip = hue_ip,
|
||||||
login = hue_token,
|
login = hue_token,
|
||||||
@@ -188,7 +189,7 @@ local living_lights_relax = HueGroup.new({
|
|||||||
})
|
})
|
||||||
device_manager:add(living_lights_relax)
|
device_manager:add(living_lights_relax)
|
||||||
|
|
||||||
device_manager:add(HueSwitch.new({
|
device_manager:add(devices.HueSwitch.new({
|
||||||
name = "Switch",
|
name = "Switch",
|
||||||
room = "Living",
|
room = "Living",
|
||||||
client = mqtt_client,
|
client = mqtt_client,
|
||||||
@@ -205,7 +206,7 @@ device_manager:add(HueSwitch.new({
|
|||||||
battery_callback = check_battery,
|
battery_callback = check_battery,
|
||||||
}))
|
}))
|
||||||
|
|
||||||
device_manager:add(WakeOnLAN.new({
|
device_manager:add(devices.WakeOnLAN.new({
|
||||||
name = "Zeus",
|
name = "Zeus",
|
||||||
room = "Living Room",
|
room = "Living Room",
|
||||||
topic = mqtt_automation("appliance/living_room/zeus"),
|
topic = mqtt_automation("appliance/living_room/zeus"),
|
||||||
@@ -214,7 +215,7 @@ device_manager:add(WakeOnLAN.new({
|
|||||||
broadcast_ip = "10.0.3.255",
|
broadcast_ip = "10.0.3.255",
|
||||||
}))
|
}))
|
||||||
|
|
||||||
local living_mixer = OutletOnOff.new({
|
local living_mixer = devices.OutletOnOff.new({
|
||||||
name = "Mixer",
|
name = "Mixer",
|
||||||
room = "Living Room",
|
room = "Living Room",
|
||||||
topic = mqtt_z2m("living/mixer"),
|
topic = mqtt_z2m("living/mixer"),
|
||||||
@@ -222,7 +223,7 @@ local living_mixer = OutletOnOff.new({
|
|||||||
})
|
})
|
||||||
turn_off_when_away(living_mixer)
|
turn_off_when_away(living_mixer)
|
||||||
device_manager:add(living_mixer)
|
device_manager:add(living_mixer)
|
||||||
local living_speakers = OutletOnOff.new({
|
local living_speakers = devices.OutletOnOff.new({
|
||||||
name = "Speakers",
|
name = "Speakers",
|
||||||
room = "Living Room",
|
room = "Living Room",
|
||||||
topic = mqtt_z2m("living/speakers"),
|
topic = mqtt_z2m("living/speakers"),
|
||||||
@@ -231,7 +232,7 @@ local living_speakers = OutletOnOff.new({
|
|||||||
turn_off_when_away(living_speakers)
|
turn_off_when_away(living_speakers)
|
||||||
device_manager:add(living_speakers)
|
device_manager:add(living_speakers)
|
||||||
|
|
||||||
device_manager:add(IkeaRemote.new({
|
device_manager:add(devices.IkeaRemote.new({
|
||||||
name = "Remote",
|
name = "Remote",
|
||||||
room = "Living Room",
|
room = "Living Room",
|
||||||
client = mqtt_client,
|
client = mqtt_client,
|
||||||
@@ -258,7 +259,7 @@ device_manager:add(IkeaRemote.new({
|
|||||||
}))
|
}))
|
||||||
|
|
||||||
local function kettle_timeout()
|
local function kettle_timeout()
|
||||||
local timeout = Timeout.new()
|
local timeout = utils.Timeout.new()
|
||||||
|
|
||||||
return function(self, state)
|
return function(self, state)
|
||||||
if state.state and state.power < 100 then
|
if state.state and state.power < 100 then
|
||||||
@@ -271,7 +272,7 @@ local function kettle_timeout()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local kettle = OutletPower.new({
|
local kettle = devices.OutletPower.new({
|
||||||
outlet_type = "Kettle",
|
outlet_type = "Kettle",
|
||||||
name = "Kettle",
|
name = "Kettle",
|
||||||
room = "Kitchen",
|
room = "Kitchen",
|
||||||
@@ -286,7 +287,7 @@ local function set_kettle(_, on)
|
|||||||
kettle:set_on(on)
|
kettle:set_on(on)
|
||||||
end
|
end
|
||||||
|
|
||||||
device_manager:add(IkeaRemote.new({
|
device_manager:add(devices.IkeaRemote.new({
|
||||||
name = "Remote",
|
name = "Remote",
|
||||||
room = "Bedroom",
|
room = "Bedroom",
|
||||||
client = mqtt_client,
|
client = mqtt_client,
|
||||||
@@ -296,7 +297,7 @@ device_manager:add(IkeaRemote.new({
|
|||||||
battery_callback = check_battery,
|
battery_callback = check_battery,
|
||||||
}))
|
}))
|
||||||
|
|
||||||
device_manager:add(IkeaRemote.new({
|
device_manager:add(devices.IkeaRemote.new({
|
||||||
name = "Remote",
|
name = "Remote",
|
||||||
room = "Kitchen",
|
room = "Kitchen",
|
||||||
client = mqtt_client,
|
client = mqtt_client,
|
||||||
@@ -307,7 +308,7 @@ device_manager:add(IkeaRemote.new({
|
|||||||
}))
|
}))
|
||||||
|
|
||||||
local function off_timeout(duration)
|
local function off_timeout(duration)
|
||||||
local timeout = Timeout.new()
|
local timeout = utils.Timeout.new()
|
||||||
|
|
||||||
return function(self, state)
|
return function(self, state)
|
||||||
if state.state then
|
if state.state then
|
||||||
@@ -320,7 +321,7 @@ local function off_timeout(duration)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local bathroom_light = LightOnOff.new({
|
local bathroom_light = devices.LightOnOff.new({
|
||||||
name = "Light",
|
name = "Light",
|
||||||
room = "Bathroom",
|
room = "Bathroom",
|
||||||
topic = mqtt_z2m("bathroom/light"),
|
topic = mqtt_z2m("bathroom/light"),
|
||||||
@@ -329,7 +330,7 @@ local bathroom_light = LightOnOff.new({
|
|||||||
})
|
})
|
||||||
device_manager:add(bathroom_light)
|
device_manager:add(bathroom_light)
|
||||||
|
|
||||||
device_manager:add(Washer.new({
|
device_manager:add(devices.Washer.new({
|
||||||
identifier = "bathroom_washer",
|
identifier = "bathroom_washer",
|
||||||
topic = mqtt_z2m("bathroom/washer"),
|
topic = mqtt_z2m("bathroom/washer"),
|
||||||
client = mqtt_client,
|
client = mqtt_client,
|
||||||
@@ -344,7 +345,7 @@ device_manager:add(Washer.new({
|
|||||||
end,
|
end,
|
||||||
}))
|
}))
|
||||||
|
|
||||||
device_manager:add(OutletOnOff.new({
|
device_manager:add(devices.OutletOnOff.new({
|
||||||
name = "Charger",
|
name = "Charger",
|
||||||
room = "Workbench",
|
room = "Workbench",
|
||||||
topic = mqtt_z2m("workbench/charger"),
|
topic = mqtt_z2m("workbench/charger"),
|
||||||
@@ -352,7 +353,7 @@ device_manager:add(OutletOnOff.new({
|
|||||||
callback = off_timeout(debug and 5 or 20 * 3600),
|
callback = off_timeout(debug and 5 or 20 * 3600),
|
||||||
}))
|
}))
|
||||||
|
|
||||||
local workbench_outlet = OutletOnOff.new({
|
local workbench_outlet = devices.OutletOnOff.new({
|
||||||
name = "Outlet",
|
name = "Outlet",
|
||||||
room = "Workbench",
|
room = "Workbench",
|
||||||
topic = mqtt_z2m("workbench/outlet"),
|
topic = mqtt_z2m("workbench/outlet"),
|
||||||
@@ -361,7 +362,7 @@ local workbench_outlet = OutletOnOff.new({
|
|||||||
turn_off_when_away(workbench_outlet)
|
turn_off_when_away(workbench_outlet)
|
||||||
device_manager:add(workbench_outlet)
|
device_manager:add(workbench_outlet)
|
||||||
|
|
||||||
local workbench_light = LightColorTemperature.new({
|
local workbench_light = devices.LightColorTemperature.new({
|
||||||
name = "Light",
|
name = "Light",
|
||||||
room = "Workbench",
|
room = "Workbench",
|
||||||
topic = mqtt_z2m("workbench/light"),
|
topic = mqtt_z2m("workbench/light"),
|
||||||
@@ -370,8 +371,8 @@ local workbench_light = LightColorTemperature.new({
|
|||||||
turn_off_when_away(workbench_light)
|
turn_off_when_away(workbench_light)
|
||||||
device_manager:add(workbench_light)
|
device_manager:add(workbench_light)
|
||||||
|
|
||||||
local delay_color_temp = Timeout.new()
|
local delay_color_temp = utils.Timeout.new()
|
||||||
device_manager:add(IkeaRemote.new({
|
device_manager:add(devices.IkeaRemote.new({
|
||||||
name = "Remote",
|
name = "Remote",
|
||||||
room = "Workbench",
|
room = "Workbench",
|
||||||
client = mqtt_client,
|
client = mqtt_client,
|
||||||
@@ -394,14 +395,14 @@ device_manager:add(IkeaRemote.new({
|
|||||||
battery_callback = check_battery,
|
battery_callback = check_battery,
|
||||||
}))
|
}))
|
||||||
|
|
||||||
local hallway_top_light = HueGroup.new({
|
local hallway_top_light = devices.HueGroup.new({
|
||||||
identifier = "hallway_top_light",
|
identifier = "hallway_top_light",
|
||||||
ip = hue_ip,
|
ip = hue_ip,
|
||||||
login = hue_token,
|
login = hue_token,
|
||||||
group_id = 83,
|
group_id = 83,
|
||||||
scene_id = "QeufkFDICEHWeKJ7",
|
scene_id = "QeufkFDICEHWeKJ7",
|
||||||
})
|
})
|
||||||
device_manager:add(HueSwitch.new({
|
device_manager:add(devices.HueSwitch.new({
|
||||||
name = "SwitchBottom",
|
name = "SwitchBottom",
|
||||||
room = "Hallway",
|
room = "Hallway",
|
||||||
client = mqtt_client,
|
client = mqtt_client,
|
||||||
@@ -411,7 +412,7 @@ device_manager:add(HueSwitch.new({
|
|||||||
end,
|
end,
|
||||||
battery_callback = check_battery,
|
battery_callback = check_battery,
|
||||||
}))
|
}))
|
||||||
device_manager:add(HueSwitch.new({
|
device_manager:add(devices.HueSwitch.new({
|
||||||
name = "SwitchTop",
|
name = "SwitchTop",
|
||||||
room = "Hallway",
|
room = "Hallway",
|
||||||
client = mqtt_client,
|
client = mqtt_client,
|
||||||
@@ -423,7 +424,7 @@ device_manager:add(HueSwitch.new({
|
|||||||
}))
|
}))
|
||||||
|
|
||||||
local hallway_light_automation = {
|
local hallway_light_automation = {
|
||||||
timeout = Timeout.new(),
|
timeout = utils.Timeout.new(),
|
||||||
forced = false,
|
forced = false,
|
||||||
switch_callback = function(self)
|
switch_callback = function(self)
|
||||||
return function(_, on)
|
return function(_, on)
|
||||||
@@ -480,7 +481,7 @@ local hallway_light_automation = {
|
|||||||
end,
|
end,
|
||||||
}
|
}
|
||||||
|
|
||||||
local hallway_storage = LightBrightness.new({
|
local hallway_storage = devices.LightBrightness.new({
|
||||||
name = "Storage",
|
name = "Storage",
|
||||||
room = "Hallway",
|
room = "Hallway",
|
||||||
topic = mqtt_z2m("hallway/storage"),
|
topic = mqtt_z2m("hallway/storage"),
|
||||||
@@ -490,7 +491,7 @@ local hallway_storage = LightBrightness.new({
|
|||||||
turn_off_when_away(hallway_storage)
|
turn_off_when_away(hallway_storage)
|
||||||
device_manager:add(hallway_storage)
|
device_manager:add(hallway_storage)
|
||||||
|
|
||||||
local hallway_bottom_lights = HueGroup.new({
|
local hallway_bottom_lights = devices.HueGroup.new({
|
||||||
identifier = "hallway_bottom_lights",
|
identifier = "hallway_bottom_lights",
|
||||||
ip = hue_ip,
|
ip = hue_ip,
|
||||||
login = hue_token,
|
login = hue_token,
|
||||||
@@ -511,7 +512,7 @@ hallway_light_automation.group = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
local function presence(duration)
|
local function presence(duration)
|
||||||
local timeout = Timeout.new()
|
local timeout = utils.Timeout.new()
|
||||||
|
|
||||||
return function(_, open)
|
return function(_, open)
|
||||||
if open then
|
if open then
|
||||||
@@ -531,7 +532,7 @@ local function presence(duration)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
device_manager:add(IkeaRemote.new({
|
device_manager:add(devices.IkeaRemote.new({
|
||||||
name = "Remote",
|
name = "Remote",
|
||||||
room = "Hallway",
|
room = "Hallway",
|
||||||
client = mqtt_client,
|
client = mqtt_client,
|
||||||
@@ -539,7 +540,7 @@ device_manager:add(IkeaRemote.new({
|
|||||||
callback = hallway_light_automation:switch_callback(),
|
callback = hallway_light_automation:switch_callback(),
|
||||||
battery_callback = check_battery,
|
battery_callback = check_battery,
|
||||||
}))
|
}))
|
||||||
local hallway_frontdoor = ContactSensor.new({
|
local hallway_frontdoor = devices.ContactSensor.new({
|
||||||
name = "Frontdoor",
|
name = "Frontdoor",
|
||||||
room = "Hallway",
|
room = "Hallway",
|
||||||
sensor_type = "Door",
|
sensor_type = "Door",
|
||||||
@@ -554,7 +555,7 @@ local hallway_frontdoor = ContactSensor.new({
|
|||||||
device_manager:add(hallway_frontdoor)
|
device_manager:add(hallway_frontdoor)
|
||||||
hallway_light_automation.door = hallway_frontdoor
|
hallway_light_automation.door = hallway_frontdoor
|
||||||
|
|
||||||
local hallway_trash = ContactSensor.new({
|
local hallway_trash = devices.ContactSensor.new({
|
||||||
name = "Trash",
|
name = "Trash",
|
||||||
room = "Hallway",
|
room = "Hallway",
|
||||||
sensor_type = "Drawer",
|
sensor_type = "Drawer",
|
||||||
@@ -566,7 +567,7 @@ local hallway_trash = ContactSensor.new({
|
|||||||
device_manager:add(hallway_trash)
|
device_manager:add(hallway_trash)
|
||||||
hallway_light_automation.trash = hallway_trash
|
hallway_light_automation.trash = hallway_trash
|
||||||
|
|
||||||
local guest_light = LightOnOff.new({
|
local guest_light = devices.LightOnOff.new({
|
||||||
name = "Light",
|
name = "Light",
|
||||||
room = "Guest Room",
|
room = "Guest Room",
|
||||||
topic = mqtt_z2m("guest/light"),
|
topic = mqtt_z2m("guest/light"),
|
||||||
@@ -575,14 +576,14 @@ local guest_light = LightOnOff.new({
|
|||||||
turn_off_when_away(guest_light)
|
turn_off_when_away(guest_light)
|
||||||
device_manager:add(guest_light)
|
device_manager:add(guest_light)
|
||||||
|
|
||||||
local bedroom_air_filter = AirFilter.new({
|
local bedroom_air_filter = devices.AirFilter.new({
|
||||||
name = "Air Filter",
|
name = "Air Filter",
|
||||||
room = "Bedroom",
|
room = "Bedroom",
|
||||||
url = "http://10.0.0.103",
|
url = "http://10.0.0.103",
|
||||||
})
|
})
|
||||||
device_manager:add(bedroom_air_filter)
|
device_manager:add(bedroom_air_filter)
|
||||||
|
|
||||||
local bedroom_lights = HueGroup.new({
|
local bedroom_lights = devices.HueGroup.new({
|
||||||
identifier = "bedroom_lights",
|
identifier = "bedroom_lights",
|
||||||
ip = hue_ip,
|
ip = hue_ip,
|
||||||
login = hue_token,
|
login = hue_token,
|
||||||
@@ -590,7 +591,7 @@ local bedroom_lights = HueGroup.new({
|
|||||||
scene_id = "PvRs-lGD4VRytL9",
|
scene_id = "PvRs-lGD4VRytL9",
|
||||||
})
|
})
|
||||||
device_manager:add(bedroom_lights)
|
device_manager:add(bedroom_lights)
|
||||||
local bedroom_lights_relax = HueGroup.new({
|
local bedroom_lights_relax = devices.HueGroup.new({
|
||||||
identifier = "bedroom_lights",
|
identifier = "bedroom_lights",
|
||||||
ip = hue_ip,
|
ip = hue_ip,
|
||||||
login = hue_token,
|
login = hue_token,
|
||||||
@@ -599,7 +600,7 @@ local bedroom_lights_relax = HueGroup.new({
|
|||||||
})
|
})
|
||||||
device_manager:add(bedroom_lights_relax)
|
device_manager:add(bedroom_lights_relax)
|
||||||
|
|
||||||
device_manager:add(HueSwitch.new({
|
device_manager:add(devices.HueSwitch.new({
|
||||||
name = "Switch",
|
name = "Switch",
|
||||||
room = "Bedroom",
|
room = "Bedroom",
|
||||||
client = mqtt_client,
|
client = mqtt_client,
|
||||||
@@ -613,7 +614,7 @@ device_manager:add(HueSwitch.new({
|
|||||||
battery_callback = check_battery,
|
battery_callback = check_battery,
|
||||||
}))
|
}))
|
||||||
|
|
||||||
device_manager:add(ContactSensor.new({
|
device_manager:add(devices.ContactSensor.new({
|
||||||
name = "Balcony",
|
name = "Balcony",
|
||||||
room = "Living Room",
|
room = "Living Room",
|
||||||
sensor_type = "Door",
|
sensor_type = "Door",
|
||||||
@@ -621,21 +622,21 @@ device_manager:add(ContactSensor.new({
|
|||||||
client = mqtt_client,
|
client = mqtt_client,
|
||||||
battery_callback = check_battery,
|
battery_callback = check_battery,
|
||||||
}))
|
}))
|
||||||
device_manager:add(ContactSensor.new({
|
device_manager:add(devices.ContactSensor.new({
|
||||||
name = "Window",
|
name = "Window",
|
||||||
room = "Living Room",
|
room = "Living Room",
|
||||||
topic = mqtt_z2m("living/window"),
|
topic = mqtt_z2m("living/window"),
|
||||||
client = mqtt_client,
|
client = mqtt_client,
|
||||||
battery_callback = check_battery,
|
battery_callback = check_battery,
|
||||||
}))
|
}))
|
||||||
device_manager:add(ContactSensor.new({
|
device_manager:add(devices.ContactSensor.new({
|
||||||
name = "Window",
|
name = "Window",
|
||||||
room = "Bedroom",
|
room = "Bedroom",
|
||||||
topic = mqtt_z2m("bedroom/window"),
|
topic = mqtt_z2m("bedroom/window"),
|
||||||
client = mqtt_client,
|
client = mqtt_client,
|
||||||
battery_callback = check_battery,
|
battery_callback = check_battery,
|
||||||
}))
|
}))
|
||||||
device_manager:add(ContactSensor.new({
|
device_manager:add(devices.ContactSensor.new({
|
||||||
name = "Window",
|
name = "Window",
|
||||||
room = "Guest Room",
|
room = "Guest Room",
|
||||||
topic = mqtt_z2m("guest/window"),
|
topic = mqtt_z2m("guest/window"),
|
||||||
@@ -643,7 +644,7 @@ device_manager:add(ContactSensor.new({
|
|||||||
battery_callback = check_battery,
|
battery_callback = check_battery,
|
||||||
}))
|
}))
|
||||||
|
|
||||||
local storage_light = LightBrightness.new({
|
local storage_light = devices.LightBrightness.new({
|
||||||
name = "Light",
|
name = "Light",
|
||||||
room = "Storage",
|
room = "Storage",
|
||||||
topic = mqtt_z2m("storage/light"),
|
topic = mqtt_z2m("storage/light"),
|
||||||
@@ -652,7 +653,7 @@ local storage_light = LightBrightness.new({
|
|||||||
turn_off_when_away(storage_light)
|
turn_off_when_away(storage_light)
|
||||||
device_manager:add(storage_light)
|
device_manager:add(storage_light)
|
||||||
|
|
||||||
device_manager:add(ContactSensor.new({
|
device_manager:add(devices.ContactSensor.new({
|
||||||
name = "Door",
|
name = "Door",
|
||||||
room = "Storage",
|
room = "Storage",
|
||||||
sensor_type = "Door",
|
sensor_type = "Door",
|
||||||
|
|||||||
26
src/main.rs
26
src/main.rs
@@ -7,12 +7,10 @@ mod web;
|
|||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::process;
|
use std::process;
|
||||||
use std::time::{SystemTime, UNIX_EPOCH};
|
|
||||||
|
|
||||||
use ::config::{Environment, File};
|
use ::config::{Environment, File};
|
||||||
use automation_lib::config::{FulfillmentConfig, MqttConfig};
|
use automation_lib::config::{FulfillmentConfig, MqttConfig};
|
||||||
use automation_lib::device_manager::DeviceManager;
|
use automation_lib::device_manager::DeviceManager;
|
||||||
use automation_lib::helpers;
|
|
||||||
use automation_lib::mqtt::{self, WrappedAsyncClient};
|
use automation_lib::mqtt::{self, WrappedAsyncClient};
|
||||||
use axum::extract::{FromRef, State};
|
use axum::extract::{FromRef, State};
|
||||||
use axum::http::StatusCode;
|
use axum::http::StatusCode;
|
||||||
@@ -30,6 +28,9 @@ use web::{ApiError, User};
|
|||||||
use crate::secret::EnvironmentSecretFile;
|
use crate::secret::EnvironmentSecretFile;
|
||||||
use crate::version::VERSION;
|
use crate::version::VERSION;
|
||||||
|
|
||||||
|
// Force automation_devices to link so that it gets registered as a module
|
||||||
|
extern crate automation_devices;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct AppState {
|
struct AppState {
|
||||||
pub openid_url: String,
|
pub openid_url: String,
|
||||||
@@ -138,6 +139,8 @@ async fn app() -> anyhow::Result<()> {
|
|||||||
})?;
|
})?;
|
||||||
lua.globals().set("print", print)?;
|
lua.globals().set("print", print)?;
|
||||||
|
|
||||||
|
automation_lib::load_modules(&lua)?;
|
||||||
|
|
||||||
let mqtt = lua.create_table()?;
|
let mqtt = lua.create_table()?;
|
||||||
let event_channel = device_manager.event_channel();
|
let event_channel = device_manager.event_channel();
|
||||||
let mqtt_new = lua.create_function(move |lua, config: mlua::Value| {
|
let mqtt_new = lua.create_function(move |lua, config: mlua::Value| {
|
||||||
@@ -158,25 +161,6 @@ async fn app() -> anyhow::Result<()> {
|
|||||||
lua.register_module("variables", lua.to_value(&config.variables)?)?;
|
lua.register_module("variables", lua.to_value(&config.variables)?)?;
|
||||||
lua.register_module("secrets", lua.to_value(&config.secrets)?)?;
|
lua.register_module("secrets", lua.to_value(&config.secrets)?)?;
|
||||||
|
|
||||||
let utils = lua.create_table()?;
|
|
||||||
let get_hostname = lua.create_function(|_lua, ()| {
|
|
||||||
hostname::get()
|
|
||||||
.map(|name| name.to_str().unwrap_or("unknown").to_owned())
|
|
||||||
.map_err(mlua::ExternalError::into_lua_err)
|
|
||||||
})?;
|
|
||||||
utils.set("get_hostname", get_hostname)?;
|
|
||||||
let get_epoch = lua.create_function(|_lua, ()| {
|
|
||||||
Ok(SystemTime::now()
|
|
||||||
.duration_since(UNIX_EPOCH)
|
|
||||||
.expect("Time is after UNIX EPOCH")
|
|
||||||
.as_millis())
|
|
||||||
})?;
|
|
||||||
utils.set("get_epoch", get_epoch)?;
|
|
||||||
lua.register_module("utils", utils)?;
|
|
||||||
|
|
||||||
automation_devices::register_with_lua(&lua)?;
|
|
||||||
helpers::register_with_lua(&lua)?;
|
|
||||||
|
|
||||||
let entrypoint = Path::new(&config.entrypoint);
|
let entrypoint = Path::new(&config.entrypoint);
|
||||||
let fulfillment_config: mlua::Value = lua.load(entrypoint).eval_async().await?;
|
let fulfillment_config: mlua::Value = lua.load(entrypoint).eval_async().await?;
|
||||||
let fulfillment_config: FulfillmentConfig = lua.from_value(fulfillment_config)?;
|
let fulfillment_config: FulfillmentConfig = lua.from_value(fulfillment_config)?;
|
||||||
|
|||||||
Reference in New Issue
Block a user