Compare commits

..

2 Commits

Author SHA1 Message Date
ruv 0979faccd4 feat(homecore-server): seed 10 default entities on boot (--no-seed-entities to opt out)
Companion to the seed_default_services() commit. Dashboard + States
pages now have content on every fresh --db :memory: boot, not just
after `bash scripts/homecore-seed.sh`.

Adds:
  - new CLI flag `--no-seed-entities` (default: enabled)
  - `seed_default_entities(hc)` mirroring the bash script's 10-entity
    set (4 RuView sensing-derived + 6 conventional HA fixtures)
  - Boot log:
        Service registry seeded with 13 default service(s)
        State machine seeded with 10 default entities

Two seeds stay in sync — integrations overwrite the same entity_ids
via /api/states/<id> POST. Run with --no-seed-entities when wiring
real plugins that populate the state machine themselves.

Empirical (after rebuild + fresh restart):
  GET /api/states   → 10 entities
  GET /api/services → 6 domains, 13 services

homecore-server --db :memory: is now enough for the web UI to be
fully populated on first paint.

Co-Authored-By: claude-flow <ruv@ruv.net>
2026-05-26 14:18:28 -04:00
ruv 75f984e515 feat(homecore-server): seed 13 default services across 6 domains on boot
Operators (and the new web UI) saw "No services registered" on every
vanilla boot because nothing in the boot sequence called
`ServiceRegistry::register()`. The Assist pipeline registers intent
handlers — a different surface — but `/api/services` stayed empty
until a plugin or integration loaded.

Adds `seed_default_services()` after `HomeCore::new()`. Each handler
is a `FnHandler` that echoes the call back as a JSON acknowledgement
so the service registry is exercise-able from day one. Integrations
override these by re-registering the same `ServiceName` with a real
handler later.

Seeded set:

  homeassistant: restart, stop, reload_core_config
  light:         turn_on, turn_off, toggle
  switch:        turn_on, turn_off, toggle
  scene:         apply
  automation:    trigger
  homecore:      ping, snapshot_state   (HOMECORE-native)

Boot log now reports:

  Service registry seeded with 13 default service(s)

GET /api/services now returns 6 domains with 13 services total.
The HOMECORE web UI's Services page shows them under proper
domain headings.

Co-Authored-By: claude-flow <ruv@ruv.net>
2026-05-26 14:07:52 -04:00
+138 -1
View File
@@ -25,7 +25,8 @@ use anyhow::Result;
use clap::Parser;
use tracing::{info, warn};
use homecore::HomeCore;
use homecore::{Context, EntityId, HomeCore, ServiceCall, ServiceError, ServiceName};
use homecore::service::FnHandler;
use homecore_api::{router, LongLivedTokenStore, SharedState};
use homecore_assist::pipeline::default_pipeline;
use homecore_assist::RegexIntentRecognizer;
@@ -52,6 +53,12 @@ struct Cli {
/// Disable the SQLite recorder for low-resource deployments.
#[arg(long)]
no_recorder: bool,
/// Skip the boot-time entity seeding (10 demo entities including
/// 4 RuView-derived sensors). Use this when wiring real
/// integrations that will populate the state machine themselves.
#[arg(long)]
no_seed_entities: bool,
}
#[tokio::main]
@@ -66,6 +73,23 @@ async fn main() -> Result<()> {
let hc = HomeCore::new();
info!("HomeCore state machine + event bus + service registry online");
// Seed a representative set of built-in services so the web UI
// and HA-wire-compat clients see a populated /api/services on
// first boot. These are no-op handlers (they just echo back the
// call as JSON for observability) — integrations override them
// by registering the same ServiceName later.
seed_default_services(&hc).await;
// Seed 10 representative entities so the web UI's Dashboard +
// States pages have content out of the box. Operators registering
// real integrations / plugins overwrite these by writing the same
// entity_id with new values. Opt out with `--no-seed-entities`.
if !cli.no_seed_entities {
seed_default_entities(&hc);
} else {
info!("Entity seeding disabled by --no-seed-entities");
}
// ── 2. Recorder (optional) ──────────────────────────────────────
if !cli.no_recorder {
match Recorder::open(&cli.db).await {
@@ -154,3 +178,116 @@ fn init_tracing() {
)
.init();
}
/// Register a representative set of built-in services so `/api/services`
/// is non-empty on first boot. Each handler simply echoes the call back
/// as a JSON acknowledgement — integrations override these by
/// re-registering the same `ServiceName` with a real handler later.
///
/// The set covers the HA wire-compat "starter pack" (homeassistant /
/// light / switch / scene / automation domains) plus a `homecore.*`
/// domain so operators can see HOMECORE-native services distinguished
/// from the HA-compat ones.
async fn seed_default_services(hc: &HomeCore) {
let echo = || FnHandler(|call: ServiceCall| async move {
Ok(serde_json::json!({
"called": format!("{}.{}", call.name.domain, call.name.service),
"service_data": call.data,
"acknowledged": true,
}))
});
let svcs = [
// Conventional HA wire-compat services
("homeassistant", "restart"),
("homeassistant", "stop"),
("homeassistant", "reload_core_config"),
("light", "turn_on"),
("light", "turn_off"),
("light", "toggle"),
("switch", "turn_on"),
("switch", "turn_off"),
("switch", "toggle"),
("scene", "apply"),
("automation", "trigger"),
// HOMECORE-native services
("homecore", "ping"),
("homecore", "snapshot_state"),
];
for (domain, service) in svcs {
hc.services()
.register(ServiceName::new(domain, service), echo())
.await;
}
let count = hc.services().registered_services().await.len();
let _ = ServiceError::NotRegistered { domain: String::new(), service: String::new() };
info!("Service registry seeded with {} default service(s)", count);
}
/// Register 10 representative entities so a fresh `--db :memory:`
/// boot has content for the web UI. Mirrors `scripts/homecore-seed.sh`
/// — when both are run the script just overwrites these values, so
/// they stay in sync.
fn seed_default_entities(hc: &HomeCore) {
let entities: Vec<(&str, &str, serde_json::Value)> = vec![
("sensor.living_room_presence", "false", serde_json::json!({
"friendly_name": "Living Room Presence", "device_class": "occupancy",
"source": "RuView ESP32-C6 BFLD"
})),
("sensor.living_room_motion_score", "0.0", serde_json::json!({
"friendly_name": "Living Room Motion Score", "unit_of_measurement": "score",
"icon": "mdi:motion-sensor"
})),
("sensor.bedroom_breathing_rate", "14.5", serde_json::json!({
"friendly_name": "Bedroom Breathing Rate", "unit_of_measurement": "BPM",
"device_class": "frequency", "source": "Seeed MR60BHA2 mmWave"
})),
("sensor.bedroom_heart_rate", "68.0", serde_json::json!({
"friendly_name": "Bedroom Heart Rate", "unit_of_measurement": "BPM",
"device_class": "frequency", "source": "Seeed MR60BHA2 mmWave"
})),
("light.kitchen_ceiling", "on", serde_json::json!({
"friendly_name": "Kitchen Ceiling", "brightness": 230,
"color_temp_kelvin": 4000, "supported_color_modes": ["color_temp"]
})),
("light.living_room_lamp", "off", serde_json::json!({
"friendly_name": "Living Room Lamp", "brightness": 0,
"supported_color_modes": ["brightness"]
})),
("switch.coffee_maker", "off", serde_json::json!({
"friendly_name": "Coffee Maker", "device_class": "outlet"
})),
("binary_sensor.front_door", "off", serde_json::json!({
"friendly_name": "Front Door", "device_class": "door"
})),
("climate.thermostat", "heat", serde_json::json!({
"friendly_name": "Thermostat", "current_temperature": 21.5,
"temperature": 22.0, "hvac_modes": ["off", "heat", "cool", "auto"],
"supported_features": 387
})),
("sensor.air_quality_index", "42", serde_json::json!({
"friendly_name": "Air Quality Index", "unit_of_measurement": "AQI",
"device_class": "aqi"
})),
];
for (id, state, attrs) in entities {
match EntityId::parse(id) {
Ok(eid) => {
hc.states().set(eid, state, attrs, Context::new());
}
Err(e) => warn!("seed_default_entities: bad entity_id {id}: {e}"),
}
}
let _ = ServiceCall {
name: ServiceName::new("homecore", "noop"),
data: serde_json::json!({}),
context: Context::new(),
};
let total = hc.states().all().len();
info!("State machine seeded with {} default entit{}", total,
if total == 1 { "y" } else { "ies" });
}