A Rust rule engine for openHAB (>=4.0).
Write your home automation rules in Rust and benefit from its vast ecosystem and efficiency.
See the examples
folder for a full example.
use std::{error::Error, sync::Arc};
use async_trait::async_trait;
use hab_rs::{
event::Event,
item::Item,
rest_api::{Api, configuration::Configuration},
rule::{Rule, RuleManager},
};
use tokio::sync::broadcast::Receiver;
// Define a rule
struct TestRule;
// Implement the Rule trait for your rule struct
#[async_trait]
impl Rule for TestRule {
fn get_name(&self) -> String {
"TestRule".to_string()
}
async fn run(
&mut self,
api: Arc<dyn Api>,
mut event_receiver: Receiver<Arc<Event>>,
) -> Result<(), Box<dyn std::error::Error + Send>> {
let command_item = Item("command_item".to_string());
while let Ok(event) = event_receiver.recv().await {
if let Event::Message(message) = event.as_ref() {
if let Some(command_event) = command_item.received_command(message, None) {
println!("Received command event for command_item: {command_event:?}");
}
}
}
Ok(())
}
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let config = Configuration {
base_path: "http://your.openhab.instance/rest".to_string(),
// openHAB expects the API token as basic auth username with an empty password
basic_auth: Some(("yourapitoken".to_string(), Some("".to_string()))),
..Default::default()
};
// Create the rule manager instance
let mut rule_manager = RuleManager::new(config);
// Register your rules
rule_manager.register(Box::new(TestRule));
// Run the rule manager
rule_manager.run().await;
Ok(())
}
Licensed under the MIT license (LICENSE-MIT).
Contribution to this crate is organized under the terms of the Rust Code of Conduct, the maintainer of this crate, DerFetzer, promises to intervene to uphold that code of conduct.