logs.gokuls.in

1 pull request merged across 1 repo

bahdotsh/hopchat

Home Assistant already drives hundreds of brands on the LAN behind one REST API. This adds it as a set of ordinary home backends, one per category, so anything HA can drive goes through the same tools the model already uses (light_set, tv_*), plus a new switch category.

What changes

  • Devices behind one address. Device.unit names the thing behind an address (an HA entity), and is its id. Devices that are their own address are saved exactly as before.
  • Scenes per light. LightDriver::scenes takes the device, since HA lights each list their own effects.
  • /home ha URL TOKEN. Connects to the URL given and lists its entities in /devices by their HA names. It reads the entity registry over the WebSocket API to leave out hidden, disabled and settings/diagnostic entities, and anything from HA's own Wiz or webOS integrations, which home drives directly. The hub is kept so scans list its devices again, and it is the one place the token lives: HA devices don't carry it, home hands the hub to each one as it drives it. Home talks to one HA at a time, so connecting with a new token or address updates every HA device at once. A lone address or a token in the address's place gets an error instead of a guess.
  • /home ha find. Looks for an HA announcing itself over mDNS and says its address, for you to type with the token. The token is never sent to an address home only heard of: anything on the LAN can announce itself as HA. A token typed alone does the same, and says so.
  • A scan that outlives its hub is ignored. Everything a hub reports during a scan names the hub, so a listing that arrives after /home ha forget or a move to another HA can't bring the old hub's entities back or speak for the new one's.
  • Renames in HA. The hub that answers to the same address and token is the one a scan has been listing all along, so an entity renamed in HA is kept up with. A new token at the same address, or a new address, still has to vouch for each device by entity id and name before home drives it.
  • HA lights (ha_light): colour modes and effects are read when listed, and kept current on every listing. A colour light does white tones too, as HA converts them. Setting a colour stops a running effect.
  • HA TVs (ha_tv): only media players HA marks as TVs. Apps and inputs both map to HA sources, matched loosely ("hdmi 1" finds HDMI_1).
  • Switch category (new): switch_set / switch_status, with HA switches (ha_switch). There's no "all", asking for "the switch" with several around gets a question back, and testing one only reads it. A plug may be running the fridge.
  • HA's own refusal messages are passed on, e.g. "does not support action media_player.volume_set". Errors that reach a group never name the hub's address. A call that ran out of time *before reaching* HA (its name didn't resolve, nothing accepted the connection) is "can't reach", not "still on it": nothing was sent.
  • Driving calls get 20 s, since an integration that goes through a cloud can take a while; status reads get 5 s; listings get 60 s, since a big home's states run to megabytes. A hub that is down marks its devices offline at once rather than stalling the scan. Home talks to at most 16 devices at once ("all the lights" in a big home), so a request doesn't open a thread and a connection per light.

Testing

  • 134 unit tests pass; clippy is clean. A fake HA on a loopback port covers the REST and WebSocket client offline: refused tokens, missing entities, registry filtering, an unreachable hub, volume stepping, a stale listing after a forget or a hub change, a rename on the same hub, and that the token appears nowhere but the hub.
  • cargo test -- --ignored a_slow_listing (11 s) proves a listing outlasts the 5 s status budget.
  • Live, against HA's demo: integration: HA_URL=… HA_TOKEN=… cargo test -- --ignored live drives the HA backends directly and runs /home ha end to end through to the tool calls. Both pass.
  • model_picks_the_right_tools with qwen2.5:7b now includes a plug: "put the kettle on" picks switch_set, and "turn off the lights" leaves the plug alone.

Not covered / caveats

  • /home ha find (mDNS) is untested, because the Docker HA couldn't announce itself on the LAN. It only reports an address, so nothing is sent on its say-so.
  • https goes through native-tls with the platform's trusted roots, but hasn't been tried against a real HA over https.
  • Nobody has looked at the 🔌 switch rows in the Devices panel yet.
  • Not downgrade-safe, like the backend split: an older hopchat keeps HA devices but drops their entity id when it saves, so they can't be driven afterwards.
  • The HA token is stored once, in home.json (written 0600 on Unix, plainly elsewhere). Devices listed by an earlier build of this branch give theirs up when read.
  • Each HA tool call still holds home's one thread for up to 20 s + 5 s, as a webOS call holds it for its own timeouts; a per-request budget in ask is a follow-up.