Compare commits
3 Commits
feature/im
...
a2e65c2d1a
| Author | SHA1 | Date | |
|---|---|---|---|
|
a2e65c2d1a
|
|||
|
e26fd9f132
|
|||
|
eb0c80c4ce
|
@@ -35,9 +35,9 @@ pub struct Config {
|
||||
pub sensor_type: SensorType,
|
||||
|
||||
#[device_config(from_lua, default)]
|
||||
pub callback: ActionCallback<ContactSensor, bool>,
|
||||
pub callback: ActionCallback<(ContactSensor, bool)>,
|
||||
#[device_config(from_lua, default)]
|
||||
pub battery_callback: ActionCallback<ContactSensor, f32>,
|
||||
pub battery_callback: ActionCallback<(ContactSensor, f32)>,
|
||||
|
||||
#[device_config(from_lua)]
|
||||
pub client: WrappedAsyncClient,
|
||||
@@ -165,14 +165,17 @@ impl OnMqtt for ContactSensor {
|
||||
return;
|
||||
}
|
||||
|
||||
self.config.callback.call(self, &!is_closed).await;
|
||||
self.config.callback.call((self.clone(), !is_closed)).await;
|
||||
|
||||
debug!(id = self.get_id(), "Updating state to {is_closed}");
|
||||
self.state_mut().await.is_closed = is_closed;
|
||||
}
|
||||
|
||||
if let Some(battery) = message.battery {
|
||||
self.config.battery_callback.call(self, &battery).await;
|
||||
self.config
|
||||
.battery_callback
|
||||
.call((self.clone(), battery))
|
||||
.await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,19 +21,19 @@ pub struct Config {
|
||||
pub client: WrappedAsyncClient,
|
||||
|
||||
#[device_config(from_lua, default)]
|
||||
pub left_callback: ActionCallback<HueSwitch, ()>,
|
||||
pub left_callback: ActionCallback<HueSwitch>,
|
||||
|
||||
#[device_config(from_lua, default)]
|
||||
pub right_callback: ActionCallback<HueSwitch, ()>,
|
||||
pub right_callback: ActionCallback<HueSwitch>,
|
||||
|
||||
#[device_config(from_lua, default)]
|
||||
pub left_hold_callback: ActionCallback<HueSwitch, ()>,
|
||||
pub left_hold_callback: ActionCallback<HueSwitch>,
|
||||
|
||||
#[device_config(from_lua, default)]
|
||||
pub right_hold_callback: ActionCallback<HueSwitch, ()>,
|
||||
pub right_hold_callback: ActionCallback<HueSwitch>,
|
||||
|
||||
#[device_config(from_lua, default)]
|
||||
pub battery_callback: ActionCallback<HueSwitch, f32>,
|
||||
pub battery_callback: ActionCallback<(HueSwitch, f32)>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone, Deserialize)]
|
||||
@@ -104,19 +104,21 @@ impl OnMqtt for HueSwitch {
|
||||
);
|
||||
|
||||
match action {
|
||||
Action::LeftPressRelease => self.config.left_callback.call(self, &()).await,
|
||||
Action::RightPressRelease => self.config.right_callback.call(self, &()).await,
|
||||
Action::LeftHold => self.config.left_hold_callback.call(self, &()).await,
|
||||
Action::RightHold => self.config.right_hold_callback.call(self, &()).await,
|
||||
Action::LeftPressRelease => self.config.left_callback.call(self.clone()).await,
|
||||
Action::RightPressRelease => {
|
||||
self.config.right_callback.call(self.clone()).await
|
||||
}
|
||||
Action::LeftHold => self.config.left_hold_callback.call(self.clone()).await,
|
||||
Action::RightHold => self.config.right_hold_callback.call(self.clone()).await,
|
||||
// If there is no hold action, the switch will act like a normal release
|
||||
Action::RightHoldRelease => {
|
||||
if !self.config.right_hold_callback.is_set() {
|
||||
self.config.right_callback.call(self, &()).await
|
||||
if self.config.right_hold_callback.is_empty() {
|
||||
self.config.right_callback.call(self.clone()).await
|
||||
}
|
||||
}
|
||||
Action::LeftHoldRelease => {
|
||||
if !self.config.left_hold_callback.is_set() {
|
||||
self.config.left_callback.call(self, &()).await
|
||||
if self.config.left_hold_callback.is_empty() {
|
||||
self.config.left_callback.call(self.clone()).await
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
@@ -124,7 +126,10 @@ impl OnMqtt for HueSwitch {
|
||||
}
|
||||
|
||||
if let Some(battery) = message.battery {
|
||||
self.config.battery_callback.call(self, &battery).await;
|
||||
self.config
|
||||
.battery_callback
|
||||
.call((self.clone(), battery))
|
||||
.await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,9 +24,9 @@ pub struct Config {
|
||||
pub client: WrappedAsyncClient,
|
||||
|
||||
#[device_config(from_lua, default)]
|
||||
pub callback: ActionCallback<IkeaRemote, bool>,
|
||||
pub callback: ActionCallback<(IkeaRemote, bool)>,
|
||||
#[device_config(from_lua, default)]
|
||||
pub battery_callback: ActionCallback<IkeaRemote, f32>,
|
||||
pub battery_callback: ActionCallback<(IkeaRemote, f32)>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, LuaDevice)]
|
||||
@@ -88,12 +88,15 @@ impl OnMqtt for IkeaRemote {
|
||||
};
|
||||
|
||||
if let Some(on) = on {
|
||||
self.config.callback.call(self, &on).await;
|
||||
self.config.callback.call((self.clone(), on)).await;
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(battery) = message.battery {
|
||||
self.config.battery_callback.call(self, &battery).await;
|
||||
self.config
|
||||
.battery_callback
|
||||
.call((self.clone(), battery))
|
||||
.await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ pub struct Config {
|
||||
pub max: isize,
|
||||
|
||||
#[device_config(from_lua, default)]
|
||||
pub callback: ActionCallback<LightSensor, bool>,
|
||||
pub callback: ActionCallback<(LightSensor, bool)>,
|
||||
|
||||
#[device_config(from_lua)]
|
||||
pub client: WrappedAsyncClient,
|
||||
@@ -114,7 +114,7 @@ impl OnMqtt for LightSensor {
|
||||
|
||||
self.config
|
||||
.callback
|
||||
.call(self, &!self.state().await.is_dark)
|
||||
.call((self.clone(), !self.state().await.is_dark))
|
||||
.await;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ pub struct Config {
|
||||
pub mqtt: MqttDeviceConfig,
|
||||
|
||||
#[device_config(from_lua, default)]
|
||||
pub callback: ActionCallback<Presence, bool>,
|
||||
pub callback: ActionCallback<(Presence, bool)>,
|
||||
|
||||
#[device_config(from_lua)]
|
||||
pub client: WrappedAsyncClient,
|
||||
@@ -118,7 +118,10 @@ impl OnMqtt for Presence {
|
||||
debug!("Overall presence updated: {overall_presence}");
|
||||
self.state_mut().await.current_overall_presence = overall_presence;
|
||||
|
||||
self.config.callback.call(self, &overall_presence).await;
|
||||
self.config
|
||||
.callback
|
||||
.call((self.clone(), overall_presence))
|
||||
.await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ pub struct Config {
|
||||
pub threshold: f32,
|
||||
|
||||
#[device_config(from_lua, default)]
|
||||
pub done_callback: ActionCallback<Washer, ()>,
|
||||
pub done_callback: ActionCallback<Washer>,
|
||||
|
||||
#[device_config(from_lua)]
|
||||
pub client: WrappedAsyncClient,
|
||||
@@ -109,7 +109,7 @@ impl OnMqtt for Washer {
|
||||
|
||||
self.state_mut().await.running = 0;
|
||||
|
||||
self.config.done_callback.call(self, &()).await;
|
||||
self.config.done_callback.call(self.clone()).await;
|
||||
} else if power < self.config.threshold {
|
||||
// Prevent false positives
|
||||
self.state_mut().await.running = 0;
|
||||
|
||||
@@ -34,7 +34,7 @@ pub struct Config<T: LightState> {
|
||||
pub mqtt: MqttDeviceConfig,
|
||||
|
||||
#[device_config(from_lua, default)]
|
||||
pub callback: ActionCallback<Light<T>, T>,
|
||||
pub callback: ActionCallback<(Light<T>, T)>,
|
||||
|
||||
#[device_config(from_lua)]
|
||||
pub client: WrappedAsyncClient,
|
||||
@@ -165,7 +165,7 @@ impl OnMqtt for Light<StateOnOff> {
|
||||
|
||||
self.config
|
||||
.callback
|
||||
.call(self, self.state().await.deref())
|
||||
.call((self.clone(), self.state().await.clone()))
|
||||
.await;
|
||||
}
|
||||
}
|
||||
@@ -204,7 +204,7 @@ impl OnMqtt for Light<StateBrightness> {
|
||||
|
||||
self.config
|
||||
.callback
|
||||
.call(self, self.state().await.deref())
|
||||
.call((self.clone(), self.state().await.clone()))
|
||||
.await;
|
||||
}
|
||||
}
|
||||
@@ -245,7 +245,7 @@ impl OnMqtt for Light<StateColorTemperature> {
|
||||
|
||||
self.config
|
||||
.callback
|
||||
.call(self, self.state().await.deref())
|
||||
.call((self.clone(), self.state().await.clone()))
|
||||
.await;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ pub struct Config<T: OutletState> {
|
||||
pub outlet_type: OutletType,
|
||||
|
||||
#[device_config(from_lua, default)]
|
||||
pub callback: ActionCallback<Outlet<T>, T>,
|
||||
pub callback: ActionCallback<(Outlet<T>, T)>,
|
||||
|
||||
#[device_config(from_lua)]
|
||||
pub client: WrappedAsyncClient,
|
||||
@@ -155,7 +155,7 @@ impl OnMqtt for Outlet<StateOnOff> {
|
||||
|
||||
self.config
|
||||
.callback
|
||||
.call(self, self.state().await.deref())
|
||||
.call((self.clone(), self.state().await.clone()))
|
||||
.await;
|
||||
}
|
||||
}
|
||||
@@ -192,7 +192,7 @@ impl OnMqtt for Outlet<StatePower> {
|
||||
|
||||
self.config
|
||||
.callback
|
||||
.call(self, self.state().await.deref())
|
||||
.call((self.clone(), self.state().await.clone()))
|
||||
.await;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,34 +1,28 @@
|
||||
use std::marker::PhantomData;
|
||||
|
||||
use futures::future::try_join_all;
|
||||
use mlua::{FromLua, IntoLua, LuaSerdeExt};
|
||||
use serde::Serialize;
|
||||
use mlua::{FromLua, IntoLuaMulti};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct Internal {
|
||||
pub struct ActionCallback<P> {
|
||||
callbacks: Vec<mlua::Function>,
|
||||
lua: mlua::Lua,
|
||||
_parameters: PhantomData<P>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ActionCallback<T, S> {
|
||||
internal: Option<Internal>,
|
||||
_this: PhantomData<T>,
|
||||
_state: PhantomData<S>,
|
||||
}
|
||||
|
||||
impl<T, S> Default for ActionCallback<T, S> {
|
||||
// NOTE: For some reason the derive macro combined with PhantomData leads to issues where it
|
||||
// requires all types part of P to implement default, even if they never actually get constructed.
|
||||
// By manually implemented Default it works fine.
|
||||
impl<P> Default for ActionCallback<P> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
internal: None,
|
||||
_this: PhantomData::<T>,
|
||||
_state: PhantomData::<S>,
|
||||
callbacks: Default::default(),
|
||||
_parameters: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> FromLua for ActionCallback<T, S> {
|
||||
fn from_lua(value: mlua::Value, lua: &mlua::Lua) -> mlua::Result<Self> {
|
||||
impl<P> FromLua for ActionCallback<P> {
|
||||
fn from_lua(value: mlua::Value, _lua: &mlua::Lua) -> mlua::Result<Self> {
|
||||
let callbacks = match value {
|
||||
mlua::Value::Function(f) => vec![f],
|
||||
mlua::Value::Table(table) => table
|
||||
@@ -49,40 +43,28 @@ impl<T, S> FromLua for ActionCallback<T, S> {
|
||||
};
|
||||
|
||||
Ok(ActionCallback {
|
||||
internal: Some(Internal {
|
||||
callbacks,
|
||||
lua: lua.clone(),
|
||||
}),
|
||||
_this: PhantomData::<T>,
|
||||
_state: PhantomData::<S>,
|
||||
_parameters: PhantomData::<P>,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Return proper error here
|
||||
impl<T, S> ActionCallback<T, S>
|
||||
impl<P> ActionCallback<P>
|
||||
where
|
||||
T: IntoLua + Sync + Send + Clone + 'static,
|
||||
S: Serialize,
|
||||
P: IntoLuaMulti + Sync + Clone,
|
||||
{
|
||||
pub async fn call(&self, this: &T, state: &S) {
|
||||
let Some(internal) = self.internal.as_ref() else {
|
||||
return;
|
||||
};
|
||||
|
||||
let state = internal.lua.to_value(state).unwrap();
|
||||
|
||||
pub async fn call(&self, parameters: P) {
|
||||
try_join_all(
|
||||
internal
|
||||
.callbacks
|
||||
self.callbacks
|
||||
.iter()
|
||||
.map(async |f| f.call_async::<()>((this.clone(), state.clone())).await),
|
||||
.map(async |f| f.call_async::<()>(parameters.clone()).await),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
pub fn is_set(&self) -> bool {
|
||||
self.internal.is_some()
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.callbacks.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ impl mlua::UserData for Timeout {
|
||||
|
||||
methods.add_async_method(
|
||||
"start",
|
||||
async |_lua, this, (timeout, callback): (f32, ActionCallback<mlua::Value, bool>)| {
|
||||
async |_lua, this, (timeout, callback): (f32, ActionCallback<()>)| {
|
||||
if let Some(handle) = this.state.write().await.handle.take() {
|
||||
handle.abort();
|
||||
}
|
||||
@@ -42,7 +42,7 @@ impl mlua::UserData for Timeout {
|
||||
async move {
|
||||
tokio::time::sleep(timeout).await;
|
||||
|
||||
callback.call(&mlua::Nil, &false).await;
|
||||
callback.call(()).await;
|
||||
}
|
||||
}));
|
||||
|
||||
|
||||
@@ -28,7 +28,13 @@ impl mlua::UserData for WrappedAsyncClient {
|
||||
methods.add_async_method(
|
||||
"send_message",
|
||||
async |_lua, this, (topic, message): (String, mlua::Value)| {
|
||||
let message = serde_json::to_string(&message).unwrap();
|
||||
// serde_json converts nil => "null", but we actually want nil to send an empty
|
||||
// message
|
||||
let message = if message.is_nil() {
|
||||
"".into()
|
||||
} else {
|
||||
serde_json::to_string(&message).unwrap()
|
||||
};
|
||||
|
||||
debug!("message = {message}");
|
||||
|
||||
|
||||
59
config.lua
59
config.lua
@@ -425,12 +425,15 @@ device_manager:add(HueSwitch.new({
|
||||
local hallway_light_automation = {
|
||||
timeout = Timeout.new(),
|
||||
forced = false,
|
||||
switch_callback = function(self, on)
|
||||
switch_callback = function(self)
|
||||
return function(_, on)
|
||||
self.timeout:cancel()
|
||||
self.group.set_on(on)
|
||||
self.forced = on
|
||||
end
|
||||
end,
|
||||
door_callback = function(self, open)
|
||||
door_callback = function(self)
|
||||
return function(_, open)
|
||||
if open then
|
||||
self.timeout:cancel()
|
||||
|
||||
@@ -442,8 +445,10 @@ local hallway_light_automation = {
|
||||
end
|
||||
end)
|
||||
end
|
||||
end
|
||||
end,
|
||||
trash_callback = function(self, open)
|
||||
trash_callback = function(self)
|
||||
return function(_, open)
|
||||
if open then
|
||||
self.group.set_on(true)
|
||||
else
|
||||
@@ -455,20 +460,23 @@ local hallway_light_automation = {
|
||||
self.group.set_on(false)
|
||||
end
|
||||
end
|
||||
end
|
||||
end,
|
||||
light_callback = function(self, on)
|
||||
light_callback = function(self)
|
||||
return function(_, state)
|
||||
if
|
||||
on
|
||||
state.on
|
||||
and (self.trash == nil or self.trash:open_percent()) == 0
|
||||
and (self.door == nil or self.door:open_percent() == 0)
|
||||
then
|
||||
-- If the door and trash are not open, that means the light got turned on manually
|
||||
self.timeout:cancel()
|
||||
self.forced = true
|
||||
elseif not on then
|
||||
elseif not state.on then
|
||||
-- The light is never forced when it is off
|
||||
self.forced = false
|
||||
end
|
||||
end
|
||||
end,
|
||||
}
|
||||
|
||||
@@ -477,9 +485,7 @@ local hallway_storage = LightBrightness.new({
|
||||
room = "Hallway",
|
||||
topic = mqtt_z2m("hallway/storage"),
|
||||
client = mqtt_client,
|
||||
callback = function(_, state)
|
||||
hallway_light_automation:light_callback(state.state)
|
||||
end,
|
||||
callback = hallway_light_automation:light_callback(),
|
||||
})
|
||||
turn_off_when_away(hallway_storage)
|
||||
device_manager:add(hallway_storage)
|
||||
@@ -504,13 +510,12 @@ hallway_light_automation.group = {
|
||||
end,
|
||||
}
|
||||
|
||||
local frontdoor_presence = {
|
||||
timeout = Timeout.new(),
|
||||
}
|
||||
setmetatable(frontdoor_presence, {
|
||||
__call = function(self, open)
|
||||
local function presence(duration)
|
||||
local timeout = Timeout.new()
|
||||
|
||||
return function(_, open)
|
||||
if open then
|
||||
self.timeout:cancel()
|
||||
timeout:cancel()
|
||||
|
||||
if not presence_system:overall_presence() then
|
||||
mqtt_client:send_message(mqtt_automation("presence/contact/frontdoor"), {
|
||||
@@ -519,21 +524,19 @@ setmetatable(frontdoor_presence, {
|
||||
})
|
||||
end
|
||||
else
|
||||
self.timeout:start(debug and 10 or 15 * 60, function()
|
||||
mqtt_client:send_message(mqtt_automation("presence/contact/frontdoor"), {})
|
||||
timeout:start(duration, function()
|
||||
mqtt_client:send_message(mqtt_automation("presence/contact/frontdoor"), nil)
|
||||
end)
|
||||
end
|
||||
end,
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
device_manager:add(IkeaRemote.new({
|
||||
name = "Remote",
|
||||
room = "Hallway",
|
||||
client = mqtt_client,
|
||||
topic = mqtt_z2m("hallway/remote"),
|
||||
callback = function(_, on)
|
||||
hallway_light_automation:switch_callback(on)
|
||||
end,
|
||||
callback = hallway_light_automation:switch_callback(),
|
||||
battery_callback = check_battery,
|
||||
}))
|
||||
local hallway_frontdoor = ContactSensor.new({
|
||||
@@ -546,10 +549,10 @@ local hallway_frontdoor = ContactSensor.new({
|
||||
topic = mqtt_automation("presence/contact/frontdoor"),
|
||||
timeout = debug and 10 or 15 * 60,
|
||||
},
|
||||
callback = function(_, open)
|
||||
hallway_light_automation:door_callback(open)
|
||||
frontdoor_presence(open)
|
||||
end,
|
||||
callback = {
|
||||
presence(debug and 10 or 15 * 60),
|
||||
hallway_light_automation:door_callback(),
|
||||
},
|
||||
battery_callback = check_battery,
|
||||
})
|
||||
device_manager:add(hallway_frontdoor)
|
||||
@@ -561,9 +564,7 @@ local hallway_trash = ContactSensor.new({
|
||||
sensor_type = "Drawer",
|
||||
topic = mqtt_z2m("hallway/trash"),
|
||||
client = mqtt_client,
|
||||
callback = function(_, open)
|
||||
hallway_light_automation:trash_callback(open)
|
||||
end,
|
||||
callback = hallway_light_automation:trash_callback(),
|
||||
battery_callback = check_battery,
|
||||
})
|
||||
device_manager:add(hallway_trash)
|
||||
|
||||
Reference in New Issue
Block a user