Added IkeaRemote
This commit is contained in:
parent
157bbf923f
commit
a353ba3d08
94
src/devices/ikea_remote.rs
Normal file
94
src/devices/ikea_remote.rs
Normal file
|
@ -0,0 +1,94 @@
|
||||||
|
use automation_macro::LuaDeviceConfig;
|
||||||
|
use axum::async_trait;
|
||||||
|
use rumqttc::{matches, Publish};
|
||||||
|
use tracing::{debug, error, trace};
|
||||||
|
|
||||||
|
use super::LuaDeviceCreate;
|
||||||
|
use crate::action_callback::ActionCallback;
|
||||||
|
use crate::config::{InfoConfig, MqttDeviceConfig};
|
||||||
|
use crate::devices::Device;
|
||||||
|
use crate::event::OnMqtt;
|
||||||
|
use crate::messages::RemoteMessage;
|
||||||
|
use crate::mqtt::WrappedAsyncClient;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, LuaDeviceConfig)]
|
||||||
|
pub struct Config {
|
||||||
|
#[device_config(flatten)]
|
||||||
|
pub info: InfoConfig,
|
||||||
|
|
||||||
|
#[device_config(default)]
|
||||||
|
pub single_button: bool,
|
||||||
|
|
||||||
|
#[device_config(flatten)]
|
||||||
|
pub mqtt: MqttDeviceConfig,
|
||||||
|
|
||||||
|
#[device_config(from_lua)]
|
||||||
|
pub client: WrappedAsyncClient,
|
||||||
|
|
||||||
|
#[device_config(from_lua)]
|
||||||
|
pub callback: ActionCallback<bool>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct IkeaRemote {
|
||||||
|
config: Config,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Device for IkeaRemote {
|
||||||
|
fn get_id(&self) -> String {
|
||||||
|
self.config.info.identifier()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl LuaDeviceCreate for IkeaRemote {
|
||||||
|
type Config = Config;
|
||||||
|
type Error = rumqttc::ClientError;
|
||||||
|
|
||||||
|
async fn create(config: Self::Config) -> Result<Self, Self::Error> {
|
||||||
|
trace!(id = config.info.identifier(), "Setting up IkeaRemote");
|
||||||
|
|
||||||
|
config
|
||||||
|
.client
|
||||||
|
.subscribe(&config.mqtt.topic, rumqttc::QoS::AtLeastOnce)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(Self { config })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl OnMqtt for IkeaRemote {
|
||||||
|
async fn on_mqtt(&self, message: Publish) {
|
||||||
|
// Check if the message is from the deviec itself or from a remote
|
||||||
|
debug!(id = Device::get_id(self), "Mqtt message received");
|
||||||
|
if matches(&message.topic, &self.config.mqtt.topic) {
|
||||||
|
let action = match RemoteMessage::try_from(message) {
|
||||||
|
Ok(message) => message.action(),
|
||||||
|
Err(err) => {
|
||||||
|
error!(id = Device::get_id(self), "Failed to parse message: {err}");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
debug!(id = Device::get_id(self), "Remote action = {:?}", action);
|
||||||
|
|
||||||
|
let on = if self.config.single_button {
|
||||||
|
match action {
|
||||||
|
crate::messages::RemoteAction::On => Some(true),
|
||||||
|
crate::messages::RemoteAction::BrightnessMoveUp => Some(false),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
match action {
|
||||||
|
crate::messages::RemoteAction::On => Some(true),
|
||||||
|
crate::messages::RemoteAction::Off => Some(false),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Some(on) = on {
|
||||||
|
self.config.callback.call(on).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,6 +5,7 @@ mod debug_bridge;
|
||||||
mod hue_bridge;
|
mod hue_bridge;
|
||||||
mod hue_group;
|
mod hue_group;
|
||||||
mod ikea_outlet;
|
mod ikea_outlet;
|
||||||
|
mod ikea_remote;
|
||||||
mod kasa_outlet;
|
mod kasa_outlet;
|
||||||
mod light_sensor;
|
mod light_sensor;
|
||||||
mod ntfy;
|
mod ntfy;
|
||||||
|
@ -28,6 +29,7 @@ pub use self::debug_bridge::DebugBridge;
|
||||||
pub use self::hue_bridge::HueBridge;
|
pub use self::hue_bridge::HueBridge;
|
||||||
pub use self::hue_group::HueGroup;
|
pub use self::hue_group::HueGroup;
|
||||||
pub use self::ikea_outlet::IkeaOutlet;
|
pub use self::ikea_outlet::IkeaOutlet;
|
||||||
|
pub use self::ikea_remote::IkeaRemote;
|
||||||
pub use self::kasa_outlet::KasaOutlet;
|
pub use self::kasa_outlet::KasaOutlet;
|
||||||
pub use self::light_sensor::LightSensor;
|
pub use self::light_sensor::LightSensor;
|
||||||
pub use self::ntfy::{Notification, Ntfy};
|
pub use self::ntfy::{Notification, Ntfy};
|
||||||
|
@ -104,6 +106,7 @@ impl_device!(DebugBridge);
|
||||||
impl_device!(HueBridge);
|
impl_device!(HueBridge);
|
||||||
impl_device!(HueGroup);
|
impl_device!(HueGroup);
|
||||||
impl_device!(IkeaOutlet);
|
impl_device!(IkeaOutlet);
|
||||||
|
impl_device!(IkeaRemote);
|
||||||
impl_device!(KasaOutlet);
|
impl_device!(KasaOutlet);
|
||||||
impl_device!(LightSensor);
|
impl_device!(LightSensor);
|
||||||
impl_device!(Ntfy);
|
impl_device!(Ntfy);
|
||||||
|
@ -119,6 +122,7 @@ pub fn register_with_lua(lua: &mlua::Lua) -> mlua::Result<()> {
|
||||||
register_device!(lua, HueBridge);
|
register_device!(lua, HueBridge);
|
||||||
register_device!(lua, HueGroup);
|
register_device!(lua, HueGroup);
|
||||||
register_device!(lua, IkeaOutlet);
|
register_device!(lua, IkeaOutlet);
|
||||||
|
register_device!(lua, IkeaRemote);
|
||||||
register_device!(lua, KasaOutlet);
|
register_device!(lua, KasaOutlet);
|
||||||
register_device!(lua, LightSensor);
|
register_device!(lua, LightSensor);
|
||||||
register_device!(lua, Ntfy);
|
register_device!(lua, Ntfy);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user