diff --git a/automation_devices/src/hue_bridge.rs b/automation_devices/src/hue_bridge.rs index 0f37c6b..8f431ff 100644 --- a/automation_devices/src/hue_bridge.rs +++ b/automation_devices/src/hue_bridge.rs @@ -3,12 +3,15 @@ use std::net::SocketAddr; use async_trait::async_trait; use automation_lib::device::{Device, LuaDeviceCreate}; -use automation_lib::event::{OnDarkness, OnPresence}; +use automation_lib::event::OnPresence; +use automation_lib::lua::traits::AddAdditionalMethods; use automation_macro::{LuaDevice, LuaDeviceConfig}; +use mlua::LuaSerdeExt; use serde::{Deserialize, Serialize}; use tracing::{error, trace, warn}; -#[derive(Debug)] +#[derive(Debug, Deserialize)] +#[serde(rename_all = "snake_case")] pub enum Flag { Presence, Darkness, @@ -30,6 +33,7 @@ pub struct Config { } #[derive(Debug, Clone, LuaDevice)] +#[traits(AddAdditionalMethods)] pub struct HueBridge { config: Config, } @@ -89,6 +93,24 @@ impl Device for HueBridge { } } +impl AddAdditionalMethods for HueBridge { + fn add_methods>(methods: &mut M) + where + Self: Sized + 'static, + { + methods.add_async_method( + "set_flag", + |lua, this, (flag, value): (mlua::Value, bool)| async move { + let flag: Flag = lua.from_value(flag)?; + + this.set_flag(flag, value).await; + + Ok(()) + }, + ); + } +} + #[async_trait] impl OnPresence for HueBridge { async fn on_presence(&self, presence: bool) { @@ -96,11 +118,3 @@ impl OnPresence for HueBridge { self.set_flag(Flag::Presence, presence).await; } } - -#[async_trait] -impl OnDarkness for HueBridge { - async fn on_darkness(&self, dark: bool) { - trace!("Bridging darkness to hue"); - self.set_flag(Flag::Darkness, dark).await; - } -} diff --git a/automation_devices/src/light_sensor.rs b/automation_devices/src/light_sensor.rs index 66e93cc..05c3427 100644 --- a/automation_devices/src/light_sensor.rs +++ b/automation_devices/src/light_sensor.rs @@ -1,6 +1,7 @@ use std::sync::Arc; 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}; @@ -20,6 +21,10 @@ pub struct Config { 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, + #[device_config(from_lua)] pub client: WrappedAsyncClient, } @@ -88,6 +93,7 @@ impl OnMqtt for LightSensor { } }; + // TODO: Move this logic to lua at some point debug!("Illuminance: {illuminance}"); let is_dark = if illuminance <= self.config.min { trace!("It is dark"); @@ -111,6 +117,11 @@ impl OnMqtt for LightSensor { 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) + .await; } } } diff --git a/config.lua b/config.lua index d2bd30d..132d3a2 100644 --- a/config.lua +++ b/config.lua @@ -68,7 +68,7 @@ automation.device_manager:add(DebugBridge.new({ local hue_ip = "10.0.0.102" local hue_token = automation.util.get_env("HUE_TOKEN") -automation.device_manager:add(HueBridge.new({ +local hue_bridge = HueBridge.new({ identifier = "hue_bridge", ip = hue_ip, login = hue_token, @@ -76,7 +76,8 @@ automation.device_manager:add(HueBridge.new({ presence = 41, darkness = 43, }, -})) +}) +automation.device_manager:add(hue_bridge) local kitchen_lights = HueGroup.new({ identifier = "kitchen_lights", @@ -126,6 +127,9 @@ automation.device_manager:add(LightSensor.new({ min = 22000, max = 23500, event_channel = automation.device_manager:event_channel(), + callback = function(_, light) + hue_bridge:set_flag("darkness", not light) + end, })) automation.device_manager:add(WakeOnLAN.new({