Improved how devices are created, ntfy and presence are now treated like any other device
All checks were successful
Build and deploy automation_rs / Build automation_rs (push) Successful in 5m30s
Build and deploy automation_rs / Build Docker image (push) Successful in 55s
Build and deploy automation_rs / Deploy Docker container (push) Has been skipped

This commit is contained in:
2024-04-27 02:55:53 +02:00
parent 8c327095fd
commit 9385f27125
22 changed files with 409 additions and 519 deletions

View File

@@ -9,7 +9,7 @@ use tracing::{debug, error, trace, warn};
use super::Device;
use crate::config::MqttDeviceConfig;
use crate::device_manager::{DeviceConfig, WrappedDevice};
use crate::device_manager::WrappedDevice;
use crate::devices::{As, DEFAULT_PRESENCE};
use crate::error::DeviceConfigError;
use crate::event::{OnMqtt, OnPresence};
@@ -51,6 +51,7 @@ pub struct TriggerConfig {
#[derive(Debug, Clone, LuaDeviceConfig)]
pub struct ContactSensorConfig {
identifier: String,
#[device_config(flatten)]
mqtt: MqttDeviceConfig,
#[device_config(from_lua)]
@@ -61,13 +62,22 @@ pub struct ContactSensorConfig {
client: WrappedAsyncClient,
}
#[async_trait]
impl DeviceConfig for ContactSensorConfig {
async fn create(&self, identifier: &str) -> Result<Box<dyn Device>, DeviceConfigError> {
trace!(id = identifier, "Setting up ContactSensor");
#[derive(Debug, LuaDevice)]
pub struct ContactSensor {
#[config]
config: ContactSensorConfig,
overall_presence: bool,
is_closed: bool,
handle: Option<JoinHandle<()>>,
}
impl ContactSensor {
async fn create(config: ContactSensorConfig) -> Result<Self, DeviceConfigError> {
trace!(id = config.identifier, "Setting up ContactSensor");
// Make sure the devices implement the required traits
if let Some(trigger) = &self.trigger {
if let Some(trigger) = &config.trigger {
for (device, _) in &trigger.devices {
let id = device.read().await.get_id().to_owned();
if !As::<dyn OnOff>::is(device.read().await.as_ref()) {
@@ -81,32 +91,18 @@ impl DeviceConfig for ContactSensorConfig {
}
}
let device = ContactSensor {
identifier: identifier.into(),
config: self.clone(),
Ok(Self {
config: config.clone(),
overall_presence: DEFAULT_PRESENCE,
is_closed: true,
handle: None,
};
Ok(Box::new(device))
})
}
}
#[derive(Debug, LuaDevice)]
pub struct ContactSensor {
identifier: String,
#[config]
config: ContactSensorConfig,
overall_presence: bool,
is_closed: bool,
handle: Option<JoinHandle<()>>,
}
impl Device for ContactSensor {
fn get_id(&self) -> &str {
&self.identifier
fn get_id(&self) -> String {
self.config.identifier.clone()
}
}
@@ -127,7 +123,10 @@ impl OnMqtt for ContactSensor {
let is_closed = match ContactMessage::try_from(message) {
Ok(state) => state.is_closed(),
Err(err) => {
error!(id = self.identifier, "Failed to parse message: {err}");
error!(
id = self.config.identifier,
"Failed to parse message: {err}"
);
return;
}
};
@@ -136,7 +135,7 @@ impl OnMqtt for ContactSensor {
return;
}
debug!(id = self.identifier, "Updating state to {is_closed}");
debug!(id = self.config.identifier, "Updating state to {is_closed}");
self.is_closed = is_closed;
if let Some(trigger) = &mut self.config.trigger {
@@ -205,7 +204,7 @@ impl OnMqtt for ContactSensor {
} else {
// Once the door is closed again we start a timeout for removing the presence
let client = self.config.client.clone();
let id = self.identifier.clone();
let id = self.config.identifier.clone();
let timeout = presence.timeout;
let topic = presence.mqtt.topic.clone();
self.handle = Some(tokio::spawn(async move {