4.1 KiB
Home Assistant polls hAPI for Jobs that name allowlisted HA scripts
hAPI needs Home Assistant to carry out actions in the house: things queued while a Person is away, or one-offs not worth configuring in HA. Traffic between the cluster and home only ever starts from home (ADR-0002), so HA has to come to hAPI. A Job is Household data (ADR-0001), created by domain code, Persons, or Bots through /v1/smart_home/jobs. HA polls GET /v1/smart_home/jobs/next every 15–30 s with its built-in rest sensor. An HA automation runs the Job and reports back through rest_command to POST /v1/smart_home/jobs/{id}/result. A Job never names an arbitrary HA action. It names an HA script (hapi_*) plus variables, and HA only runs script.hapi_*, so HA decides what hAPI can ever trigger. Jobs tolerate about 30 s of latency: anything interactive and real-time goes from the Person straight to HA's own app, not through hAPI. This bridge carries only Jobs and their results. HA sends other data to ordinary domain endpoints as a Bot.
Considered Options
- MQTT through a Mosquitto broker in the cluster. HA's supported message bus gives sub-second delivery. Rejected for v1: it means running and exposing a broker, plus an auth bridge so the broker understands hAPI Tokens, plus a single publisher process inside hAPI. HA's client always uses a clean session, so durability would live in hAPI regardless. MQTT remains the upgrade path if real-time Jobs ever matter.
- RabbitMQ / AMQP. HA has no AMQP integration and explicitly doesn't support RabbitMQ's MQTT plugin.
- A custom HA integration (long-poll or SSE against hAPI). This has the cleanest semantics, but it's Python to keep working through every HA release. Since 2026.11 it also can't be installed through the Marketplace from a private Gitea repo.
- hAPI calling HA's REST or WebSocket API. Ruled out by ADR-0002, because traffic only starts from home.
- Jobs naming arbitrary HA actions (e.g.
lock.unlock). Rejected: any holder ofsmart_home:writecould do anything HA can do.
Consequences
- The bridge needs no new infrastructure or credentials. HA is the
home-assistantBot with a Household grant ofsmart_home:write, and its Token lives in HA'ssecrets.yaml. There's nojobsScope:smart_home:writeboth creates Jobs and consumes them, and ADR-0005 allows splitting the Scope later. - Delivery is at-least-once, FIFO by
not_before(or creation time).nexthands out the oldest eligible Job and hides it for a 2-minute lease. After 3 deliveries with no result, the Job becomesfailed(no_result), so one broken Job can't stall the queue. HA scripts must be idempotent: set states, don't toggle. After each result, HA callshomeassistant.update_entityon the sensor so a backlog drains without waiting for the next poll. - Job lifecycle:
pending → succeeded | failed | expired | cancelled. A Job may carry an optionalnot_before.expires_atdefaults to 1 h afternot_before(or after creation), and an expired Job is never delivered. A result is{status, error?, response?}, whereresponseholds the script's response variables. The first result wins, and a later one gets409. Any Household member, or any Bot withsmart_home:write, can cancel apendingJob. Finished Jobs are immutable and are kept for 90 days.hapi jobs sweep, a CronJob every 15 min (ADR-0012), removes them and marks pending Jobs pastexpires_atasexpired; until it does, reads deriveexpiredfromexpires_at. - hAPI checks only the shape of a Job, not what it does. Script names must match
hapi_[a-z0-9_]+, and variables are capped at 16 KB. hAPI keeps no catalogue of HA scripts. HA rejects unknown scripts, which comes back as a failed result. - HA-side configuration is a reference package in this repo (
integrations/home-assistant/hapi_jobs.yaml: rest sensor, automation, rest_command), copied into HA by hand. Thehapi_*scripts themselves live in a separate HA configuration repo. - hAPI records HA's last poll, so a stalled bridge can be detected. Turning that into an alert is part of observability.