hab-rs

A Rust rule engine for openHAB.
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;
struct TestRule;
#[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(),
basic_auth: Some(("yourapitoken".to_string(), Some("".to_string()))),
..Default::default()
};
let mut rule_manager = RuleManager::new(config);
rule_manager.register(Box::new(TestRule));
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.