refactor!: Rewrote device implementation macro once again

This time with a bit more though put into the design of the code, as a
result the macro should be a lot more robust.

This did result in the macro getting renamed from LuaDevice to Device as
this should be _the_ Device macro.
The attribute also got renamed from traits() to device(traits()) and the
syntax got overhauled to allow for a bit more expression.
This commit is contained in:
2025-09-09 02:48:44 +02:00
parent aad089aa10
commit 2dbd491b81
18 changed files with 268 additions and 139 deletions

View File

@@ -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,
}

View File

@@ -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;
@@ -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>>,

View File

@@ -4,7 +4,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 +31,8 @@ pub struct Config {
pub flags: FlagIDs,
}
#[derive(Debug, Clone, LuaDevice)]
#[traits(AddAdditionalMethods)]
#[derive(Debug, Clone, Device)]
#[device(traits(AddAdditionalMethods))]
pub struct HueBridge {
config: Config,
}

View File

@@ -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,
}

View File

@@ -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};
@@ -55,7 +55,7 @@ struct State {
battery: Option<f32>,
}
#[derive(Debug, Clone, LuaDevice)]
#[derive(Debug, Clone, Device)]
pub struct HueSwitch {
config: Config,
}

View File

@@ -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};
@@ -29,7 +29,7 @@ pub struct Config {
pub battery_callback: ActionCallback<(IkeaRemote, f32)>,
}
#[derive(Debug, Clone, LuaDevice)]
#[derive(Debug, Clone, Device)]
pub struct IkeaRemote {
config: Config,
}

View File

@@ -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,
}

View File

@@ -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};
@@ -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>>,

View File

@@ -4,7 +4,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,8 +118,8 @@ pub struct Config {
pub topic: String,
}
#[derive(Debug, Clone, LuaDevice)]
#[traits(AddAdditionalMethods)]
#[derive(Debug, Clone, Device)]
#[device(traits(AddAdditionalMethods))]
pub struct Ntfy {
config: Config,
}

View File

@@ -9,7 +9,7 @@ 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};
@@ -34,8 +34,8 @@ pub struct State {
current_overall_presence: bool,
}
#[derive(Debug, Clone, LuaDevice)]
#[traits(AddAdditionalMethods)]
#[derive(Debug, Clone, Device)]
#[device(traits(AddAdditionalMethods))]
pub struct Presence {
config: Config,
state: Arc<RwLock<State>>,

View File

@@ -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,
}

View File

@@ -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};
@@ -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>>,

View File

@@ -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};
@@ -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>,

View File

@@ -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;
@@ -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>,