No description
Find a file
vratk0529-arch-desktop ceba774e8f hacore 0.2.0: shared env, time, schedules, timers, HA API; scoped dashboard input
Three programs carried their own copies of the same pieces, and the
Node-RED flows being ported need more of them. They live here now, once,
with tests: env (quote-tolerant reads, from pv-control), time (the zone
compiled in, because the scratch images make chrono::Local UTC), the
change-detected retained Publisher with String keys, schedule (wall-clock
triggers for the cron injects, firing only within two minutes of the time
so a restart at 10:00 never rings an 08:00 alarm), timing (Node-RED's
stoptimer, trigger-with-extend and rate-limit nodes over Instant) and
ha_api (service calls and entity state over Home Assistant's REST API,
with bounded timeouts and an error, not a panic, without HA_TOKEN).

hacore's own env reads go through env too. A docker env-file hands
MQTT_SERVER_URI="..." over with its quotes on, and paho got it like that.
MQTT_USERNAME/PASSWORD are now optional.

MQTT handlers take closures, so a handler can carry its context instead
of every program parking its state in a global, and every matching
handler runs: before, only the first match did, and a second handler on
an overlapping filter silently never ran.

Dashboard input is opt-in per area: dashboard::listen(&["solar"], f)
subscribes dashboard/TX/solar/# instead of init_all subscribing all of
dashboard/TX/# for everyone. Every program was storing every other
program's inputs in its own SQLite, and no broker ACL could be narrower
than the whole dashboard. set_handler is gone.

Influx speaks the 2.x write API as well as 1.x, chosen by INFLUX_VERSION
or by INFLUX_TOKEN being set, so moving off the deprecated 1.x server is
a change to .env rather than to every program. A refused write now logs
the server's reason, not just the status.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-15 18:29:35 +02:00
src hacore 0.2.0: shared env, time, schedules, timers, HA API; scoped dashboard input 2026-10-15 18:29:35 +02:00
.gitignore Initial test commit 2026-05-23 19:43:45 +02:00
Cargo.lock hacore 0.2.0: shared env, time, schedules, timers, HA API; scoped dashboard input 2026-10-15 18:29:35 +02:00
Cargo.toml hacore 0.2.0: shared env, time, schedules, timers, HA API; scoped dashboard input 2026-10-15 18:29:35 +02:00
README.md hacore 0.2.0: shared env, time, schedules, timers, HA API; scoped dashboard input 2026-10-15 18:29:35 +02:00

hacore

Shared library for the home-automation programs (pv-control, lights, teplomery, audio, lora-watch, notify): MQTT, the Home Assistant dashboard and REST API, InfluxDB, a small SQLite store, and the pieces every controller ends up needing.

hacore = { git = "https://forgejo.vratk0529.com/vratk0529/hacore.git", tag = "v0.2.0" }

hacore::init_all() loads .env, opens db.sqlite, connects to MQTT and configures Influx and the HA API. Dashboard input is opt-in per area with dashboard::listen.

Modules

Module What
mqtt Client, attach_handler (closures or fns; every matching handler runs), create_var/attach_variable + fetch_values, publish / publish_retained / publish_opts; resubscribes after reconnects
dashboard HA over MQTT: set / set_retained to dashboard/RX/<key>, listen(&["area"], handler) for dashboard/TX/<area>/#, get
publisher Publisher: change-detected, retained dashboard state (number, text, boolean, metric) with a 5-minute heartbeat
ha_api HA REST: call_service, call_service_logged, state
influx Line-protocol writer for InfluxDB 1.x or 2.x: write, write_value, *_throttled, Point
db SQLite key/value store with an in-memory cache
env env_str, env_f64, env_u64, env_i64, env_bool, env_string_or: env vars tolerant of the quotes docker --env-file passes through
time timezone(), now(): local time without zoneinfo (the images are scratch, where chrono::Local is UTC; don't use it)
schedule Trigger / Schedule: "sat 08:00; mon-fri 07:50; daily 00:00", fires once per day within 2 minutes of the time, so a restart never fires a past alarm
timing Node-RED timing nodes over Instant: Deadline (stoptimer), Debounce / DebounceMap (trigger with extend), RateLimit (delay, rate mode, drop)
types Value, DbError

Environment

All read through hacore::env, so quoted values work.

Variable Default
MQTT_SERVER_URI required e.g. mqtt://mqtt.lan:1883
MQTT_CLIENT_ID required the program's name
MQTT_USERNAME anonymous the program's own broker user
MQTT_PASSWORD none
MQTT_RECONNECT_MIN_SECS 1 reconnect backoff start
MQTT_RECONNECT_MAX_SECS 30 reconnect backoff cap
INFLUX_URL http://127.0.0.1:8086
INFLUX_VERSION 2 if INFLUX_TOKEN is set, else 1 which write API
INFLUX_DB home 1.x database
INFLUX_USERNAME none 1.x basic auth
INFLUX_PASSWORD none 1.x basic auth
INFLUX_TOKEN none 2.x API token (write access to the bucket)
INFLUX_ORG none 2.x organisation, required by 2.x
INFLUX_BUCKET INFLUX_DB's value 2.x bucket
HA_URL http://homeassistant.lan:8123
HA_TOKEN none long-lived access token; without it ha_api calls return an error
TZ Europe/Bratislava IANA zone name, compiled in via chrono-tz

Moving a program from InfluxDB 1.x to 2.x: create a bucket named like the old database (or a DBRP mapping for it), then set INFLUX_TOKEN, INFLUX_ORG and, if the bucket is named differently, INFLUX_BUCKET; nothing in the program changes.

Upgrading from 0.1

  • dashboard::set_handler(f) is gone, and init_all no longer subscribes to dashboard/TX/#. Call dashboard::listen(&["solar"], f) with the key prefixes the program owns; keys still arrive in full (solar/inverter/power).
  • mqtt::attach_handler runs every matching handler, not only the first.
  • Replace local env helpers, timezone code and Publisher copies with hacore::env, hacore::time and hacore::publisher::Publisher.