6 releases
Uses new Rust 2024
new 0.3.3 | May 3, 2025 |
---|---|
0.3.2 | Apr 24, 2025 |
0.2.0 | Apr 15, 2025 |
0.1.0 | Apr 11, 2025 |
#3 in #rules-engine
606 downloads per month
1MB
17K
SLoC
hab-rs
A Rust rule engine for openHAB (>=4.0).
Write your home automation rules in Rust and benefit from its vast ecosystem and efficiency.
Usage
See the examples
folder for a full example.
Quickstart
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(())
}
License
Licensed under the MIT license (LICENSE-MIT).
Code of Conduct
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.
Dependencies
~13–26MB
~364K SLoC