Use own struct to deserialize hue switch state and added hold actions
This commit is contained in:
parent
47d509cec1
commit
746e19eb8c
3056
Cargo.lock
generated
3056
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
|
@ -64,7 +64,6 @@ tokio-util = { version = "0.7.11", features = ["full"] }
|
||||||
tracing-subscriber = "0.3.16"
|
tracing-subscriber = "0.3.16"
|
||||||
uuid = "1.8.0"
|
uuid = "1.8.0"
|
||||||
wakey = "0.3.0"
|
wakey = "0.3.0"
|
||||||
zigbee2mqtt-types = { version = "0.4.0", features = ["debug", "philips"] }
|
|
||||||
air_filter_types = { git = "https://git.huizinga.dev/Dreaded_X/airfilter", tag = "v0.4.4" }
|
air_filter_types = { git = "https://git.huizinga.dev/Dreaded_X/airfilter", tag = "v0.4.4" }
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
|
@ -19,7 +19,6 @@ impls = { workspace = true }
|
||||||
serde = { workspace = true }
|
serde = { workspace = true }
|
||||||
reqwest = { workspace = true } # Use rustls, since the other packages also use rustls
|
reqwest = { workspace = true } # Use rustls, since the other packages also use rustls
|
||||||
anyhow = { workspace = true }
|
anyhow = { workspace = true }
|
||||||
zigbee2mqtt-types = { workspace = true }
|
|
||||||
axum = { workspace = true }
|
axum = { workspace = true }
|
||||||
bytes = { workspace = true }
|
bytes = { workspace = true }
|
||||||
thiserror = { workspace = true }
|
thiserror = { workspace = true }
|
||||||
|
|
|
@ -6,8 +6,8 @@ use automation_lib::event::OnMqtt;
|
||||||
use automation_lib::mqtt::WrappedAsyncClient;
|
use automation_lib::mqtt::WrappedAsyncClient;
|
||||||
use automation_macro::LuaDeviceConfig;
|
use automation_macro::LuaDeviceConfig;
|
||||||
use rumqttc::{matches, Publish};
|
use rumqttc::{matches, Publish};
|
||||||
|
use serde::Deserialize;
|
||||||
use tracing::{debug, trace, warn};
|
use tracing::{debug, trace, warn};
|
||||||
use zigbee2mqtt_types::philips::{Zigbee929003017102, Zigbee929003017102Action};
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, LuaDeviceConfig)]
|
#[derive(Debug, Clone, LuaDeviceConfig)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
|
@ -25,6 +25,30 @@ pub struct Config {
|
||||||
|
|
||||||
#[device_config(from_lua, default)]
|
#[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, ()>,
|
||||||
|
|
||||||
|
#[device_config(from_lua, default)]
|
||||||
|
pub right_hold_callback: ActionCallback<HueSwitch, ()>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Deserialize)]
|
||||||
|
#[serde(rename_all = "snake_case")]
|
||||||
|
enum Action {
|
||||||
|
LeftPress,
|
||||||
|
LeftPressRelease,
|
||||||
|
LeftHold,
|
||||||
|
LeftHoldRelease,
|
||||||
|
RightPress,
|
||||||
|
RightPressRelease,
|
||||||
|
RightHold,
|
||||||
|
RightHoldRelease,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Deserialize)]
|
||||||
|
struct State {
|
||||||
|
action: Action,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
|
@ -60,7 +84,7 @@ impl OnMqtt for HueSwitch {
|
||||||
async fn on_mqtt(&self, message: Publish) {
|
async fn on_mqtt(&self, message: Publish) {
|
||||||
// Check if the message is from the device 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) {
|
if matches(&message.topic, &self.config.mqtt.topic) {
|
||||||
let action = match serde_json::from_slice::<Zigbee929003017102>(&message.payload) {
|
let action = match serde_json::from_slice::<State>(&message.payload) {
|
||||||
Ok(message) => message.action,
|
Ok(message) => message.action,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
warn!(id = Device::get_id(self), "Failed to parse message: {err}");
|
warn!(id = Device::get_id(self), "Failed to parse message: {err}");
|
||||||
|
@ -70,12 +94,10 @@ impl OnMqtt for HueSwitch {
|
||||||
debug!(id = Device::get_id(self), "Remote action = {:?}", action);
|
debug!(id = Device::get_id(self), "Remote action = {:?}", action);
|
||||||
|
|
||||||
match action {
|
match action {
|
||||||
Zigbee929003017102Action::LeftPress => {
|
Action::LeftPressRelease => self.config.left_callback.call(self, &()).await,
|
||||||
self.config.left_callback.call(self, &()).await
|
Action::LeftHold => self.config.left_hold_callback.call(self, &()).await,
|
||||||
}
|
Action::RightPressRelease => self.config.right_callback.call(self, &()).await,
|
||||||
Zigbee929003017102Action::RightPress => {
|
Action::RightHold => self.config.right_hold_callback.call(self, &()).await,
|
||||||
self.config.right_callback.call(self, &()).await
|
|
||||||
}
|
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user