Moved hue bridge on darkness to lua
This commit is contained in:
@@ -3,12 +3,15 @@ use std::net::SocketAddr;
|
|||||||
|
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use automation_lib::device::{Device, LuaDeviceCreate};
|
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 automation_macro::{LuaDevice, LuaDeviceConfig};
|
||||||
|
use mlua::LuaSerdeExt;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use tracing::{error, trace, warn};
|
use tracing::{error, trace, warn};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Deserialize)]
|
||||||
|
#[serde(rename_all = "snake_case")]
|
||||||
pub enum Flag {
|
pub enum Flag {
|
||||||
Presence,
|
Presence,
|
||||||
Darkness,
|
Darkness,
|
||||||
@@ -30,6 +33,7 @@ pub struct Config {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, LuaDevice)]
|
#[derive(Debug, Clone, LuaDevice)]
|
||||||
|
#[traits(AddAdditionalMethods)]
|
||||||
pub struct HueBridge {
|
pub struct HueBridge {
|
||||||
config: Config,
|
config: Config,
|
||||||
}
|
}
|
||||||
@@ -89,6 +93,24 @@ impl Device for HueBridge {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl AddAdditionalMethods for HueBridge {
|
||||||
|
fn add_methods<M: mlua::UserDataMethods<Self>>(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]
|
#[async_trait]
|
||||||
impl OnPresence for HueBridge {
|
impl OnPresence for HueBridge {
|
||||||
async fn on_presence(&self, presence: bool) {
|
async fn on_presence(&self, presence: bool) {
|
||||||
@@ -96,11 +118,3 @@ impl OnPresence for HueBridge {
|
|||||||
self.set_flag(Flag::Presence, presence).await;
|
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
|
use automation_lib::action_callback::ActionCallback;
|
||||||
use automation_lib::config::MqttDeviceConfig;
|
use automation_lib::config::MqttDeviceConfig;
|
||||||
use automation_lib::device::{Device, LuaDeviceCreate};
|
use automation_lib::device::{Device, LuaDeviceCreate};
|
||||||
use automation_lib::event::{self, Event, EventChannel, OnMqtt};
|
use automation_lib::event::{self, Event, EventChannel, OnMqtt};
|
||||||
@@ -20,6 +21,10 @@ pub struct Config {
|
|||||||
pub max: isize,
|
pub max: isize,
|
||||||
#[device_config(rename("event_channel"), from_lua, with(|ec: EventChannel| ec.get_tx()))]
|
#[device_config(rename("event_channel"), from_lua, with(|ec: EventChannel| ec.get_tx()))]
|
||||||
pub tx: event::Sender,
|
pub tx: event::Sender,
|
||||||
|
|
||||||
|
#[device_config(from_lua, default)]
|
||||||
|
pub callback: ActionCallback<LightSensor, bool>,
|
||||||
|
|
||||||
#[device_config(from_lua)]
|
#[device_config(from_lua)]
|
||||||
pub client: WrappedAsyncClient,
|
pub client: WrappedAsyncClient,
|
||||||
}
|
}
|
||||||
@@ -88,6 +93,7 @@ impl OnMqtt for LightSensor {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// TODO: Move this logic to lua at some point
|
||||||
debug!("Illuminance: {illuminance}");
|
debug!("Illuminance: {illuminance}");
|
||||||
let is_dark = if illuminance <= self.config.min {
|
let is_dark = if illuminance <= self.config.min {
|
||||||
trace!("It is dark");
|
trace!("It is dark");
|
||||||
@@ -111,6 +117,11 @@ impl OnMqtt for LightSensor {
|
|||||||
if self.config.tx.send(Event::Darkness(is_dark)).await.is_err() {
|
if self.config.tx.send(Event::Darkness(is_dark)).await.is_err() {
|
||||||
warn!("There are no receivers on the event channel");
|
warn!("There are no receivers on the event channel");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.config
|
||||||
|
.callback
|
||||||
|
.call(self, &!self.state().await.is_dark)
|
||||||
|
.await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -68,7 +68,7 @@ automation.device_manager:add(DebugBridge.new({
|
|||||||
local hue_ip = "10.0.0.102"
|
local hue_ip = "10.0.0.102"
|
||||||
local hue_token = automation.util.get_env("HUE_TOKEN")
|
local hue_token = automation.util.get_env("HUE_TOKEN")
|
||||||
|
|
||||||
automation.device_manager:add(HueBridge.new({
|
local hue_bridge = HueBridge.new({
|
||||||
identifier = "hue_bridge",
|
identifier = "hue_bridge",
|
||||||
ip = hue_ip,
|
ip = hue_ip,
|
||||||
login = hue_token,
|
login = hue_token,
|
||||||
@@ -76,7 +76,8 @@ automation.device_manager:add(HueBridge.new({
|
|||||||
presence = 41,
|
presence = 41,
|
||||||
darkness = 43,
|
darkness = 43,
|
||||||
},
|
},
|
||||||
}))
|
})
|
||||||
|
automation.device_manager:add(hue_bridge)
|
||||||
|
|
||||||
local kitchen_lights = HueGroup.new({
|
local kitchen_lights = HueGroup.new({
|
||||||
identifier = "kitchen_lights",
|
identifier = "kitchen_lights",
|
||||||
@@ -126,6 +127,9 @@ automation.device_manager:add(LightSensor.new({
|
|||||||
min = 22000,
|
min = 22000,
|
||||||
max = 23500,
|
max = 23500,
|
||||||
event_channel = automation.device_manager:event_channel(),
|
event_channel = automation.device_manager:event_channel(),
|
||||||
|
callback = function(_, light)
|
||||||
|
hue_bridge:set_flag("darkness", not light)
|
||||||
|
end,
|
||||||
}))
|
}))
|
||||||
|
|
||||||
automation.device_manager:add(WakeOnLAN.new({
|
automation.device_manager:add(WakeOnLAN.new({
|
||||||
|
|||||||
Reference in New Issue
Block a user