LuaDevice macro now uses LuaDeviceCreate trait to create devices from configs

This commit is contained in:
2024-04-29 02:53:21 +02:00
parent 9f636a2572
commit 44a40d4dfa
16 changed files with 116 additions and 84 deletions

View File

@@ -7,7 +7,7 @@ use mlua::FromLua;
use tokio::task::JoinHandle;
use tracing::{debug, error, trace, warn};
use super::Device;
use super::{Device, LuaDeviceCreate};
use crate::config::MqttDeviceConfig;
use crate::device_manager::WrappedDevice;
use crate::devices::DEFAULT_PRESENCE;
@@ -64,7 +64,6 @@ pub struct ContactSensorConfig {
#[derive(Debug, LuaDevice)]
pub struct ContactSensor {
#[config]
config: ContactSensorConfig,
overall_presence: bool,
@@ -72,8 +71,12 @@ pub struct ContactSensor {
handle: Option<JoinHandle<()>>,
}
impl ContactSensor {
async fn create(config: ContactSensorConfig) -> Result<Self, DeviceConfigError> {
#[async_trait]
impl LuaDeviceCreate for ContactSensor {
type Config = ContactSensorConfig;
type Error = DeviceConfigError;
async fn create(config: Self::Config) -> Result<Self, Self::Error> {
trace!(id = config.identifier, "Setting up ContactSensor");
// Make sure the devices implement the required traits