From 690090016e5f1205968d9682a828a96a8bb18e68 Mon Sep 17 00:00:00 2001 From: Dreaded_X Date: Mon, 14 Aug 2023 03:05:06 +0200 Subject: [PATCH] Turn off the device directly if the timeout is set to zero --- src/devices/contact_sensor.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/devices/contact_sensor.rs b/src/devices/contact_sensor.rs index 8c9c444..99f1f6f 100644 --- a/src/devices/contact_sensor.rs +++ b/src/devices/contact_sensor.rs @@ -59,6 +59,7 @@ impl PresenceDeviceConfig { #[derive(Debug, Clone, Deserialize)] pub struct LightsConfig { lights: Vec, + #[serde(default)] timeout: u64, // Timeout in seconds } @@ -195,9 +196,14 @@ impl OnMqtt for ContactSensor { } else { for (light, previous) in &lights.lights { let mut light = light.write().await; - if !previous && let Some(light) = As::::cast_mut(light.as_mut()) { - light.start_timeout(lights.timeout); - } + if !previous { + // If the timeout is zero just turn the light off directly + if lights.timeout.is_zero() && let Some(light) = As::::cast_mut(light.as_mut()) { + light.set_on(false).await.ok(); + } else if let Some(light) = As::::cast_mut(light.as_mut()) { + light.start_timeout(lights.timeout); + } + } } } }