Turn off the device directly if the timeout is set to zero

This commit is contained in:
2023-08-14 03:05:06 +02:00
parent b97b682a5e
commit 690090016e

View File

@@ -59,6 +59,7 @@ impl PresenceDeviceConfig {
#[derive(Debug, Clone, Deserialize)]
pub struct LightsConfig {
lights: Vec<String>,
#[serde(default)]
timeout: u64, // Timeout in seconds
}
@@ -195,12 +196,17 @@ impl OnMqtt for ContactSensor {
} else {
for (light, previous) in &lights.lights {
let mut light = light.write().await;
if !previous && let Some(light) = As::<dyn Timeout>::cast_mut(light.as_mut()) {
if !previous {
// If the timeout is zero just turn the light off directly
if lights.timeout.is_zero() && let Some(light) = As::<dyn OnOff>::cast_mut(light.as_mut()) {
light.set_on(false).await.ok();
} else if let Some(light) = As::<dyn Timeout>::cast_mut(light.as_mut()) {
light.start_timeout(lights.timeout);
}
}
}
}
}
// Check if this contact sensor works as a presence device
// If not we are done here