Added contact sensor that can act as a presence device with timeout
This commit is contained in:
26
src/mqtt.rs
26
src/mqtt.rs
@@ -137,12 +137,16 @@ impl TryFrom<&Publish> for RemoteMessage {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct PresenceMessage {
|
||||
state: bool
|
||||
}
|
||||
|
||||
impl PresenceMessage {
|
||||
pub fn new(state: bool) -> Self {
|
||||
Self { state }
|
||||
}
|
||||
|
||||
pub fn present(&self) -> bool {
|
||||
self.state
|
||||
}
|
||||
@@ -177,3 +181,23 @@ impl TryFrom<&Publish> for BrightnessMessage {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct ContactMessage {
|
||||
contact: bool,
|
||||
}
|
||||
|
||||
impl ContactMessage {
|
||||
pub fn is_closed(&self) -> bool {
|
||||
self.contact
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<&Publish> for ContactMessage {
|
||||
type Error = anyhow::Error;
|
||||
|
||||
fn try_from(message: &Publish) -> Result<Self, Self::Error> {
|
||||
serde_json::from_slice(&message.payload)
|
||||
.or(Err(anyhow::anyhow!("Invalid message payload received: {:?}", message.payload)))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user