v2/crates/homecore/README.md
Rust port of Home Assistant's core state machine, event bus, service registry, and entity registry.
P1 scaffold: foundational types, DashMap-backed state machine, and Tokio broadcast event bus. Persistence and full Home Assistant schema compatibility land in P2.
homecore is the heart of the HOMECORE Home Assistant port. It provides:
EntityId → State)SystemEvent) and domain events (DomainEvent)All components are async-first, zero-copy for readers (using Arc<State>), and designed for multi-threaded access without global locks.
domain.entity_id format with Unicode rejectionStateChanged, EntityRegistered, ConfigReloaded (enum variants)Context::parent + user_idattributes map without mutating last_changed timestamp| Capability | Type | Method | Notes |
|---|---|---|---|
| Store entity state | State write | StateMachine::set(entity_id, state, ...) | Per-shard serial; fires StateChanged event |
| Query entity state | State read | StateMachine::get(entity_id) | Zero-copy Arc<State> clone; lock-free |
| List entities by domain | State query | StateMachine::all_by_domain(domain) | Filtered snapshot |
| Fire system event | Event emit | EventBus::fire_system(event) | Broadcast to all subscribers |
| Fire domain event | Event emit | EventBus::fire_domain(topic, data) | Untyped JSON event |
| Subscribe to events | Event receive | EventBus::subscribe_system() / subscribe_domain(topic) | Tokio broadcast channels |
| Register entity | Registry write | EntityRegistry::register(entry) | In-memory only (P1) |
| Register service | Service write | ServiceRegistry::register(name, handler) | Stub; dispatch in P2 |
| Aspect | Home Assistant | homecore |
|---|---|---|
| Language | Python 3 | Rust 1.89+ |
| State store | Python dict + event loop | DashMap + Tokio |
| Persistence | core.entity_registry.yaml + SQLite | In-memory only (P1; SQLite planned P2) |
| Event bus | Python asyncio queue | Tokio broadcast channels |
| Schema validation | voluptuous + JSON Schema | serde + custom validators (planned P2) |
| Thread safety | GIL-bound single-threaded | Lock-free concurrent (DashMap shards) |
| Service dispatch | asyncio event loop + coroutines | mpsc registry stub (P2) |
See benches/state_machine.rs for the criterion harness (run with cargo bench -p homecore).
use homecore::{HomeCore, EntityId, State};
use std::collections::HashMap;
#[tokio::main]
async fn main() {
let homecore = HomeCore::new();
// Set state for a light entity
let light_id = EntityId::parse("light.kitchen").expect("valid entity_id");
let mut attrs = HashMap::new();
attrs.insert("brightness".to_string(), serde_json::json!(200));
homecore
.state_machine()
.set(light_id.clone(), State::new("on", attrs), None, None)
.await
.expect("set state");
// Read state (lock-free)
let state = homecore
.state_machine()
.get(&light_id)
.await;
assert_eq!(state.as_ref().map(|s| s.state.as_str()), Some("on"));
// Subscribe to state changes
let mut rx = homecore.event_bus().subscribe_system();
tokio::spawn(async move {
while let Ok(event) = rx.recv().await {
println!("Event: {:?}", event);
}
});
// Fire a domain event
homecore
.event_bus()
.fire_domain("custom_domain", serde_json::json!({"action": "test"}))
.await;
}
homecore (state machine + event bus + registries)
├─ homecore-api (REST + WebSocket endpoints for state/events)
├─ homecore-recorder (persistence + ruvector semantic index)
├─ homecore-plugins (WASM plugin runtime integration)
├─ homecore-automation (YAML triggers + MiniJinja execution)
├─ homecore-assist (intent recognition + handlers)
├─ homecore-hap (Apple HomeKit bridge)
├─ homecore-migrate (Home Assistant `.storage/` import)
└─ homecore-server (workspace binary orchestrator)