Added contact sensor that can act as a presence device with timeout

This commit is contained in:
2023-01-05 04:29:27 +01:00
parent d0c92e8e18
commit 9a239a88ec
5 changed files with 147 additions and 2 deletions

View File

@@ -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)))
}
}