ActionCallback now always returns self and state can be anything serializable

This commit is contained in:
2024-12-08 02:50:52 +01:00
parent eefb476d7f
commit 41d2af655b
7 changed files with 40 additions and 29 deletions

View File

@@ -32,7 +32,7 @@ pub struct Config {
#[device_config(from_lua, default)]
pub presence: Option<PresenceDeviceConfig>,
#[device_config(from_lua, default)]
pub callback: ActionCallback<bool>,
pub callback: ActionCallback<ContactSensor, bool>,
#[device_config(from_lua)]
pub client: WrappedAsyncClient,
}
@@ -116,7 +116,7 @@ impl OnMqtt for ContactSensor {
return;
}
self.config.callback.call(!is_closed).await;
self.config.callback.call(self, &!is_closed).await;
debug!(id = self.get_id(), "Updating state to {is_closed}");
self.state_mut().await.is_closed = is_closed;

View File

@@ -21,10 +21,10 @@ pub struct Config {
pub client: WrappedAsyncClient,
#[device_config(from_lua, default)]
pub left_callback: ActionCallback<()>,
pub left_callback: ActionCallback<HueSwitch, ()>,
#[device_config(from_lua, default)]
pub right_callback: ActionCallback<()>,
pub right_callback: ActionCallback<HueSwitch, ()>,
}
#[derive(Debug, Clone)]
@@ -58,7 +58,7 @@ impl LuaDeviceCreate for HueSwitch {
#[async_trait]
impl OnMqtt for HueSwitch {
async fn on_mqtt(&self, message: Publish) {
// Check if the message is from the deviec itself or from a remote
// Check if the message is from the device itself or from a remote
if matches(&message.topic, &self.config.mqtt.topic) {
let action = match serde_json::from_slice::<Zigbee929003017102>(&message.payload) {
Ok(message) => message.action,
@@ -70,8 +70,12 @@ impl OnMqtt for HueSwitch {
debug!(id = Device::get_id(self), "Remote action = {:?}", action);
match action {
Zigbee929003017102Action::LeftPress => self.config.left_callback.call(()).await,
Zigbee929003017102Action::RightPress => self.config.right_callback.call(()).await,
Zigbee929003017102Action::LeftPress => {
self.config.left_callback.call(self, &()).await
}
Zigbee929003017102Action::RightPress => {
self.config.right_callback.call(self, &()).await
}
_ => {}
}
}

View File

@@ -36,7 +36,7 @@ pub struct Config {
pub outlet_type: OutletType,
#[device_config(from_lua, default)]
pub callback: ActionCallback<(IkeaOutlet, bool)>,
pub callback: ActionCallback<IkeaOutlet, bool>,
#[device_config(from_lua)]
pub client: WrappedAsyncClient,
@@ -109,7 +109,7 @@ impl OnMqtt for IkeaOutlet {
return;
}
self.config.callback.call((self.clone(), state)).await;
self.config.callback.call(self, &state).await;
debug!(id = Device::get_id(self), "Updating state to {state}");
self.state_mut().await.last_known_state = state;

View File

@@ -24,7 +24,7 @@ pub struct Config {
pub client: WrappedAsyncClient,
#[device_config(from_lua)]
pub callback: ActionCallback<bool>,
pub callback: ActionCallback<IkeaRemote, bool>,
}
#[derive(Debug, Clone)]
@@ -84,7 +84,7 @@ impl OnMqtt for IkeaRemote {
};
if let Some(on) = on {
self.config.callback.call(on).await;
self.config.callback.call(self, &on).await;
}
}
}