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

@@ -1,6 +1,6 @@
use async_trait::async_trait;
use google_home::traits;
use rumqttc::{matches, AsyncClient};
use rumqttc::matches;
use tracing::{debug, error, warn};
use crate::config::MqttDeviceConfig;
@@ -26,19 +26,13 @@ impl AudioSetup {
mqtt: MqttDeviceConfig,
mixer: Box<dyn Device>,
speakers: Box<dyn Device>,
client: AsyncClient,
) -> Result<Self, DeviceError> {
// We expect the children devices to implement the OnOff trait
let mixer_id = mixer.get_id().to_owned();
let mixer = As::consume(mixer).ok_or_else(|| DeviceError::OnOffExpected(mixer_id))?;
let mixer = As::consume(mixer).ok_or(DeviceError::OnOffExpected(mixer_id))?;
let speakers_id = speakers.get_id().to_owned();
let speakers =
As::consume(speakers).ok_or_else(|| DeviceError::OnOffExpected(speakers_id))?;
client
.subscribe(mqtt.topic.clone(), rumqttc::QoS::AtLeastOnce)
.await?;
let speakers = As::consume(speakers).ok_or(DeviceError::OnOffExpected(speakers_id))?;
Ok(Self {
identifier: identifier.to_owned(),
@@ -57,6 +51,10 @@ impl Device for AudioSetup {
#[async_trait]
impl OnMqtt for AudioSetup {
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;

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;

View File

@@ -14,7 +14,6 @@ use tracing::{debug, error, warn};
use crate::config::{InfoConfig, MqttDeviceConfig, OutletType};
use crate::devices::Device;
use crate::error::DeviceError;
use crate::mqtt::{OnMqtt, OnOffMessage};
use crate::presence::OnPresence;
@@ -32,20 +31,15 @@ pub struct IkeaOutlet {
}
impl IkeaOutlet {
pub async fn build(
pub fn new(
identifier: &str,
info: InfoConfig,
mqtt: MqttDeviceConfig,
outlet_type: OutletType,
timeout: Option<u64>,
client: AsyncClient,
) -> Result<Self, DeviceError> {
// TODO: Handle potential errors here
client
.subscribe(mqtt.topic.clone(), rumqttc::QoS::AtLeastOnce)
.await?;
Ok(Self {
) -> Self {
Self {
identifier: identifier.to_owned(),
info,
mqtt,
@@ -54,7 +48,7 @@ impl IkeaOutlet {
client,
last_known_state: false,
handle: None,
})
}
}
}
@@ -83,6 +77,10 @@ impl Device for IkeaOutlet {
#[async_trait]
impl OnMqtt for IkeaOutlet {
fn topics(&self) -> Vec<&str> {
vec![&self.mqtt.topic]
}
async fn on_mqtt(&mut self, message: &Publish) {
// Update the internal state based on what the device has reported
if !matches(&message.topic, &self.mqtt.topic) {

View File

@@ -9,12 +9,11 @@ use google_home::{
types::Type,
GoogleHomeDevice,
};
use rumqttc::{matches, AsyncClient, Publish};
use rumqttc::{matches, Publish};
use tracing::{debug, error};
use crate::{
config::{InfoConfig, MqttDeviceConfig},
error::DeviceError,
mqtt::{ActivateMessage, OnMqtt},
};
@@ -30,26 +29,20 @@ pub struct WakeOnLAN {
}
impl WakeOnLAN {
pub async fn build(
pub fn new(
identifier: &str,
info: InfoConfig,
mqtt: MqttDeviceConfig,
mac_address: MacAddress,
broadcast_ip: Ipv4Addr,
client: AsyncClient,
) -> Result<Self, DeviceError> {
// TODO: Handle potential errors here
client
.subscribe(mqtt.topic.clone(), rumqttc::QoS::AtLeastOnce)
.await?;
Ok(Self {
) -> Self {
Self {
identifier: identifier.to_owned(),
info,
mqtt,
mac_address,
broadcast_ip,
})
}
}
}
@@ -61,6 +54,10 @@ impl Device for WakeOnLAN {
#[async_trait]
impl OnMqtt for WakeOnLAN {
fn topics(&self) -> Vec<&str> {
vec![&self.mqtt.topic]
}
async fn on_mqtt(&mut self, message: &Publish) {
if !matches(&message.topic, &self.mqtt.topic) {
return;