Added OnPresence trait that allows devices to act on changes in presence
This commit is contained in:
@@ -10,6 +10,7 @@ use tokio::task::JoinHandle;
|
||||
use crate::config::{KettleConfig, InfoConfig, MqttDeviceConfig};
|
||||
use crate::devices::Device;
|
||||
use crate::mqtt::Listener;
|
||||
use crate::presence::OnPresence;
|
||||
|
||||
pub struct IkeaOutlet {
|
||||
identifier: String,
|
||||
@@ -63,12 +64,8 @@ impl TryFrom<&Publish> for StateMessage {
|
||||
type Error = anyhow::Error;
|
||||
|
||||
fn try_from(message: &Publish) -> Result<Self, Self::Error> {
|
||||
match serde_json::from_slice(&message.payload) {
|
||||
Ok(message) => Ok(message),
|
||||
Err(..) => {
|
||||
Err(anyhow::anyhow!("Invalid message payload received: {:?}", message.payload))
|
||||
}
|
||||
}
|
||||
serde_json::from_slice(&message.payload)
|
||||
.or(Err(anyhow::anyhow!("Invalid message payload received: {:?}", message.payload)))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -134,6 +131,19 @@ impl Listener for IkeaOutlet {
|
||||
}
|
||||
}
|
||||
|
||||
impl OnPresence for IkeaOutlet {
|
||||
fn on_presence(&mut self, presence: bool) {
|
||||
// Turn off the outlet when we leave the house
|
||||
if !presence {
|
||||
let client = self.client.clone();
|
||||
let topic = self.mqtt.topic.clone();
|
||||
tokio::spawn(async move {
|
||||
set_on(client, topic, false).await;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl GoogleHomeDevice for IkeaOutlet {
|
||||
fn get_device_type(&self) -> Type {
|
||||
if self.kettle.is_some() {
|
||||
|
||||
@@ -16,11 +16,10 @@ pub struct WakeOnLAN {
|
||||
|
||||
impl WakeOnLAN {
|
||||
pub fn new(identifier: String, info: InfoConfig, mqtt: MqttDeviceConfig, mac_address: String, client: AsyncClient) -> Self {
|
||||
let c = client.clone();
|
||||
let t = mqtt.topic.clone();
|
||||
// @TODO Handle potential errors here
|
||||
tokio::spawn(async move {
|
||||
c.subscribe(t, rumqttc::QoS::AtLeastOnce).await.unwrap();
|
||||
client.subscribe(t, rumqttc::QoS::AtLeastOnce).await.unwrap();
|
||||
});
|
||||
|
||||
Self { identifier, info, mqtt, mac_address }
|
||||
@@ -42,12 +41,8 @@ impl TryFrom<&Publish> for StateMessage {
|
||||
type Error = anyhow::Error;
|
||||
|
||||
fn try_from(message: &Publish) -> Result<Self, Self::Error> {
|
||||
match serde_json::from_slice(&message.payload) {
|
||||
Ok(message) => Ok(message),
|
||||
Err(..) => {
|
||||
Err(anyhow::anyhow!("Invalid message payload received: {:?}", message.payload))
|
||||
}
|
||||
}
|
||||
serde_json::from_slice(&message.payload)
|
||||
.or(Err(anyhow::anyhow!("Invalid message payload received: {:?}", message.payload)))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user