From 6db5831571b96e63dc042ff1d75cca7c732459d8 Mon Sep 17 00:00:00 2001 From: Dreaded_X Date: Sun, 31 Aug 2025 05:40:28 +0200 Subject: [PATCH] Removed old darkness system --- automation_devices/src/light_sensor.rs | 8 +----- automation_lib/src/device.rs | 3 +-- automation_lib/src/device_manager.rs | 15 +----------- automation_lib/src/event.rs | 6 ----- automation_lib/src/messages.rs | 34 -------------------------- config.lua | 1 - 6 files changed, 3 insertions(+), 64 deletions(-) diff --git a/automation_devices/src/light_sensor.rs b/automation_devices/src/light_sensor.rs index 05c3427..e870860 100644 --- a/automation_devices/src/light_sensor.rs +++ b/automation_devices/src/light_sensor.rs @@ -4,7 +4,7 @@ use async_trait::async_trait; use automation_lib::action_callback::ActionCallback; use automation_lib::config::MqttDeviceConfig; use automation_lib::device::{Device, LuaDeviceCreate}; -use automation_lib::event::{self, Event, EventChannel, OnMqtt}; +use automation_lib::event::OnMqtt; use automation_lib::messages::BrightnessMessage; use automation_lib::mqtt::WrappedAsyncClient; use automation_macro::{LuaDevice, LuaDeviceConfig}; @@ -19,8 +19,6 @@ pub struct Config { pub mqtt: MqttDeviceConfig, pub min: isize, pub max: isize, - #[device_config(rename("event_channel"), from_lua, with(|ec: EventChannel| ec.get_tx()))] - pub tx: event::Sender, #[device_config(from_lua, default)] pub callback: ActionCallback, @@ -114,10 +112,6 @@ impl OnMqtt for LightSensor { debug!("Dark state has changed: {is_dark}"); self.state_mut().await.is_dark = is_dark; - if self.config.tx.send(Event::Darkness(is_dark)).await.is_err() { - warn!("There are no receivers on the event channel"); - } - self.config .callback .call(self, &!self.state().await.is_dark) diff --git a/automation_lib/src/device.rs b/automation_lib/src/device.rs index d16c4d4..2b4b859 100644 --- a/automation_lib/src/device.rs +++ b/automation_lib/src/device.rs @@ -5,7 +5,7 @@ use dyn_clone::DynClone; use google_home::traits::OnOff; use mlua::ObjectLike; -use crate::event::{OnDarkness, OnMqtt, OnPresence}; +use crate::event::{OnMqtt, OnPresence}; #[async_trait::async_trait] pub trait LuaDeviceCreate { @@ -25,7 +25,6 @@ pub trait Device: + Cast + Cast + Cast - + Cast + Cast { fn get_id(&self) -> String; diff --git a/automation_lib/src/device_manager.rs b/automation_lib/src/device_manager.rs index d84dae3..96b1cc3 100644 --- a/automation_lib/src/device_manager.rs +++ b/automation_lib/src/device_manager.rs @@ -9,7 +9,7 @@ use tokio_cron_scheduler::{Job, JobScheduler}; use tracing::{debug, instrument, trace}; use crate::device::Device; -use crate::event::{Event, EventChannel, OnDarkness, OnMqtt, OnPresence}; +use crate::event::{Event, EventChannel, OnMqtt, OnPresence}; pub type DeviceMap = HashMap>; @@ -94,19 +94,6 @@ impl DeviceManager { join_all(iter).await; } - Event::Darkness(dark) => { - let devices = self.devices.read().await; - let iter = devices.iter().map(|(id, device)| async move { - let device: Option<&dyn OnDarkness> = device.cast(); - if let Some(device) = device { - trace!(id, "Handling"); - device.on_darkness(dark).await; - trace!(id, "Done"); - } - }); - - join_all(iter).await; - } Event::Presence(presence) => { let devices = self.devices.read().await; let iter = devices.iter().map(|(id, device)| async move { diff --git a/automation_lib/src/event.rs b/automation_lib/src/event.rs index 7cc7270..9062699 100644 --- a/automation_lib/src/event.rs +++ b/automation_lib/src/event.rs @@ -6,7 +6,6 @@ use tokio::sync::mpsc; #[derive(Debug, Clone)] pub enum Event { MqttMessage(Publish), - Darkness(bool), Presence(bool), } @@ -40,8 +39,3 @@ pub trait OnMqtt: Sync + Send { pub trait OnPresence: Sync + Send { async fn on_presence(&self, presence: bool); } - -#[async_trait] -pub trait OnDarkness: Sync + Send { - async fn on_darkness(&self, dark: bool); -} diff --git a/automation_lib/src/messages.rs b/automation_lib/src/messages.rs index 1ab36fb..dcd504a 100644 --- a/automation_lib/src/messages.rs +++ b/automation_lib/src/messages.rs @@ -162,40 +162,6 @@ impl TryFrom for ContactMessage { } } -// Message used to report the current darkness state -#[derive(Debug, Deserialize, Serialize)] -pub struct DarknessMessage { - state: bool, - updated: Option, -} - -impl DarknessMessage { - pub fn new(state: bool) -> Self { - Self { - state, - updated: Some( - SystemTime::now() - .duration_since(UNIX_EPOCH) - .expect("Time is after UNIX EPOCH") - .as_millis(), - ), - } - } - - pub fn is_dark(&self) -> bool { - self.state - } -} - -impl TryFrom for DarknessMessage { - type Error = ParseError; - - fn try_from(message: Publish) -> Result { - serde_json::from_slice(&message.payload) - .or(Err(ParseError::InvalidPayload(message.payload.clone()))) - } -} - // Message used to report the power draw a smart plug #[derive(Debug, Deserialize)] pub struct PowerMessage { diff --git a/config.lua b/config.lua index 6968e87..296c4da 100644 --- a/config.lua +++ b/config.lua @@ -126,7 +126,6 @@ automation.device_manager:add(LightSensor.new({ client = mqtt_client, min = 22000, max = 23500, - event_channel = automation.device_manager:event_channel(), callback = function(_, light) hue_bridge:set_flag("darkness", not light) mqtt_client:send_message(mqtt_automation("debug") .. "/darkness", {