Only activate the contact sensor presence if there is the overall presence is currently false
This commit is contained in:
parent
e16e30ffe2
commit
1326a8878c
|
@ -5,7 +5,7 @@ use rumqttc::AsyncClient;
|
||||||
use tokio::task::JoinHandle;
|
use tokio::task::JoinHandle;
|
||||||
use tracing::{error, debug, warn};
|
use tracing::{error, debug, warn};
|
||||||
|
|
||||||
use crate::{config::{MqttDeviceConfig, PresenceDeviceConfig}, mqtt::{OnMqtt, ContactMessage, PresenceMessage}};
|
use crate::{config::{MqttDeviceConfig, PresenceDeviceConfig}, mqtt::{OnMqtt, ContactMessage, PresenceMessage}, presence::OnPresence};
|
||||||
|
|
||||||
use super::Device;
|
use super::Device;
|
||||||
|
|
||||||
|
@ -15,6 +15,7 @@ pub struct ContactSensor {
|
||||||
presence: Option<PresenceDeviceConfig>,
|
presence: Option<PresenceDeviceConfig>,
|
||||||
|
|
||||||
client: AsyncClient,
|
client: AsyncClient,
|
||||||
|
overall_presence: bool,
|
||||||
is_closed: bool,
|
is_closed: bool,
|
||||||
handle: Option<JoinHandle<()>>,
|
handle: Option<JoinHandle<()>>,
|
||||||
}
|
}
|
||||||
|
@ -28,6 +29,7 @@ impl ContactSensor {
|
||||||
mqtt,
|
mqtt,
|
||||||
presence,
|
presence,
|
||||||
client,
|
client,
|
||||||
|
overall_presence: false,
|
||||||
is_closed: true,
|
is_closed: true,
|
||||||
handle: None,
|
handle: None,
|
||||||
}
|
}
|
||||||
|
@ -40,6 +42,12 @@ impl Device for ContactSensor {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl OnPresence for ContactSensor {
|
||||||
|
fn on_presence(&mut self, presence: bool) {
|
||||||
|
self.overall_presence = presence;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl OnMqtt for ContactSensor {
|
impl OnMqtt for ContactSensor {
|
||||||
fn on_mqtt(&mut self, message: &rumqttc::Publish) {
|
fn on_mqtt(&mut self, message: &rumqttc::Publish) {
|
||||||
if message.topic != self.mqtt.topic {
|
if message.topic != self.mqtt.topic {
|
||||||
|
@ -82,8 +90,12 @@ impl OnMqtt for ContactSensor {
|
||||||
handle.abort();
|
handle.abort();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Only use the door as an presence sensor if there the current presence is set false
|
||||||
self.client.publish(topic, rumqttc::QoS::AtLeastOnce, false, serde_json::to_string(&PresenceMessage::new(true)).unwrap()).block_on().unwrap();
|
// This is to prevent the house from being marked as present for however long the
|
||||||
|
// timeout is set when leaving the house
|
||||||
|
if !self.overall_presence {
|
||||||
|
self.client.publish(topic, rumqttc::QoS::AtLeastOnce, false, serde_json::to_string(&PresenceMessage::new(true)).unwrap()).block_on().unwrap();
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// Once the door is closed again we start a timeout for removing the presence
|
// Once the door is closed again we start a timeout for removing the presence
|
||||||
let client = self.client.clone();
|
let client = self.client.clone();
|
||||||
|
|
Loading…
Reference in New Issue
Block a user