Devices now handles subscribing to mqtt topics

This commit is contained in:
2023-04-12 04:37:16 +02:00
parent 34e5274e0b
commit 92c8f3074f
12 changed files with 93 additions and 63 deletions

View File

@@ -7,7 +7,6 @@ use tracing::{debug, error, warn};
use crate::{
config::{MqttDeviceConfig, PresenceDeviceConfig},
error::DeviceError,
mqtt::{ContactMessage, OnMqtt, PresenceMessage},
presence::OnPresence,
};
@@ -27,17 +26,13 @@ pub struct ContactSensor {
}
impl ContactSensor {
pub async fn build(
pub fn new(
identifier: &str,
mqtt: MqttDeviceConfig,
presence: Option<PresenceDeviceConfig>,
client: AsyncClient,
) -> Result<Self, DeviceError> {
client
.subscribe(mqtt.topic.clone(), rumqttc::QoS::AtLeastOnce)
.await?;
Ok(Self {
) -> Self {
Self {
identifier: identifier.to_owned(),
mqtt,
presence,
@@ -45,7 +40,7 @@ impl ContactSensor {
overall_presence: false,
is_closed: true,
handle: None,
})
}
}
}
@@ -64,6 +59,10 @@ impl OnPresence for ContactSensor {
#[async_trait]
impl OnMqtt for ContactSensor {
fn topics(&self) -> Vec<&str> {
vec![&self.mqtt.topic]
}
async fn on_mqtt(&mut self, message: &rumqttc::Publish) {
if !matches(&message.topic, &self.mqtt.topic) {
return;