#!/usr/bin/env python3 """Manage Home Assistant's Ollama integration entries for forgeway, headlessly. ha_ollama.py setup --agent "" --title "" [--entity-id conversation.x] [--assist] ha_ollama.py ask "" ha_ollama.py list ha_ollama.py delete `setup` is idempotent (same URL → same entry; same agent name → same subentry) and ends with a REAL conversation through the new agent, printed verbatim. REST drives the config flows; the WebSocket API renames the entry and the entity id. The long-lived token comes from `pass hassio/lounge-token` inside this process and only ever travels as a header. HA base URL: --ha (default http://192.168.120.12:8123). """ from __future__ import annotations import argparse import asyncio import json import subprocess import sys import time import urllib.error import urllib.request import websockets TOKEN_ENTRY = "hassio/lounge-token" def token() -> str: return subprocess.run(["pass", TOKEN_ENTRY], capture_output=True, text=True, check=True).stdout.strip() class HA: def __init__(self, base: str): self.base = base.rstrip("/") self.tok = token() def rest(self, method: str, path: str, body: dict | None = None): req = urllib.request.Request( f"{self.base}{path}", data=json.dumps(body).encode() if body is not None else None, headers={"Authorization": f"Bearer {self.tok}", "Content-Type": "application/json"}, method=method, ) try: with urllib.request.urlopen(req, timeout=180) as r: raw = r.read().decode() try: return r.status, json.loads(raw) except ValueError: return r.status, raw except urllib.error.HTTPError as e: raw = e.read().decode("utf-8", "replace") try: return e.code, json.loads(raw) except ValueError: return e.code, raw async def ws(self, *commands: dict) -> list[dict]: url = self.base.replace("https://", "wss://").replace("http://", "ws://") + "/api/websocket" out = [] async with websockets.connect(url, max_size=16_000_000) as sock: await sock.recv() await sock.send(json.dumps({"type": "auth", "access_token": self.tok})) if json.loads(await sock.recv()).get("type") != "auth_ok": raise SystemExit("websocket auth failed") for i, cmd in enumerate(commands, start=1): await sock.send(json.dumps({"id": i, **cmd})) while True: msg = json.loads(await sock.recv()) if msg.get("id") == i: out.append(msg) break return out # ---- helpers ------------------------------------------------------------ def entries(self) -> list[dict]: """Ollama entries with their subentries (REST lists entries; only the WebSocket API lists subentries; HA never exposes entry.data/url).""" st, full = self.rest("GET", "/api/config/config_entries/entry") ents = [e for e in full if e["domain"] == "ollama"] cmds = [{"type": "config_entries/subentries/list", "entry_id": e["entry_id"]} for e in ents] subs = asyncio.run(self.ws(*cmds)) if cmds else [] for e, r in zip(ents, subs): e["subentries"] = r.get("result") or [] return ents def entry_for(self, url: str) -> dict | None: """Match by title == url (fresh entries) or by the mode token we always put in our titles ('local-strict' / 'local-optimistic') on the same host.""" mode = url.rstrip("/").rsplit("/", 1)[-1] host = url.split("//", 1)[-1].split("/", 1)[0] for e in self.entries(): t = e.get("title", "") if t == url or (mode in t and host in t): return e return None def wait_loaded(self, entry_id: str, timeout: float = 90) -> str: """Creating or renaming an entry (re)loads it asynchronously; a subentry flow started meanwhile aborts with entry_not_loaded.""" deadline = time.time() + timeout state = "?" while time.time() < deadline: st, full = self.rest("GET", "/api/config/config_entries/entry") e = next((x for x in full if x["entry_id"] == entry_id), None) state = e["state"] if e else "missing" if state == "loaded": return state time.sleep(2) return state def conversation_entities(self) -> list[dict]: r = asyncio.run(self.ws({"type": "config/entity_registry/list"}))[0].get("result", []) return [x for x in r if x["entity_id"].startswith("conversation.")] # --------------------------------------------------------------------------- def cmd_setup(ha: HA, a: argparse.Namespace) -> int: url = a.forgeway_url.rstrip("/") entry = ha.entry_for(url) if entry: print(f"[entry] reusing {entry['entry_id'][:8]} '{entry['title']}' ({entry['state']})") else: st, flow = ha.rest("POST", "/api/config/config_entries/flow", {"handler": "ollama"}) if st != 200 or flow.get("type") != "form": print(f"[entry] cannot start config flow: {st} {flow}") return 1 st, res = ha.rest("POST", f"/api/config/config_entries/flow/{flow['flow_id']}", {"url": url}) if res.get("type") != "create_entry": # HA validated the URL by calling /api/tags with a 5 s timeout print(f"[entry] flow did not create an entry: {st} {json.dumps(res)[:400]}") return 1 entry = res["result"] print(f"[entry] created {entry['entry_id'][:8]} for {url}") entry_id = entry["entry_id"] if a.title and entry.get("title") != a.title: r = asyncio.run(ha.ws({"type": "config_entries/update", "entry_id": entry_id, "title": a.title}))[0] print(f"[entry] title → {a.title!r}: {'ok' if r.get('success') else r.get('error')}") state = ha.wait_loaded(entry_id) if state != "loaded": print(f"[entry] not loaded (state={state}); HA could not talk to {url}/api/tags") return 1 # conversation subentry entry = next(e for e in ha.entries() if e["entry_id"] == entry_id) subs = entry.get("subentries") or [] sub = next((s for s in subs if s.get("subentry_type") == "conversation" and s.get("title") == a.agent), None) if sub: print(f"[agent] reusing subentry '{a.agent}'") else: st, flow = ha.rest("POST", "/api/config/config_entries/subentries/flow", {"handler": [entry_id, "conversation"]}) if st != 200 or flow.get("type") != "form": print(f"[agent] cannot start subentry flow: {st} {json.dumps(flow)[:300]}") return 1 listed = [o["value"] for f in flow.get("data_schema", []) if f.get("name") == "model" for o in (f.get("selector", {}).get("select", {}).get("options") or []) if isinstance(o, dict)] print(f"[agent] models HA fetched from forgeway: {[m for m in listed if 'gpt-oss' in m] or listed[:3]}") data = {"name": a.agent, "model": a.model, "num_ctx": 8192, "max_history": 20, "keep_alive": -1, "think": False} if a.assist: data["llm_hass_api"] = ["assist"] st, res = ha.rest("POST", f"/api/config/config_entries/subentries/flow/{flow['flow_id']}", data) if res.get("type") != "create_entry": print(f"[agent] subentry flow failed: {st} {json.dumps(res)[:400]}") return 1 print(f"[agent] created '{a.agent}' (model {a.model}, think=False, assist={a.assist})") # entity: the registry leaves name/original_name empty for these entities # (entity name = device name), so match on the subentry id — exact. entry = next(e for e in ha.entries() if e["entry_id"] == entry_id) sub = next((s for s in entry.get("subentries") or [] if s.get("subentry_type") == "conversation" and s.get("title") == a.agent), None) entity_id = None for _ in range(40): for x in ha.conversation_entities(): if sub and x.get("config_subentry_id") == sub["subentry_id"]: entity_id = x["entity_id"] if entity_id: break time.sleep(2) if not entity_id: print("[entity] conversation entity did not appear") return 1 if a.entity_id and entity_id != a.entity_id: r = asyncio.run(ha.ws({"type": "config/entity_registry/update", "entity_id": entity_id, "new_entity_id": a.entity_id}))[0] if r.get("success"): entity_id = a.entity_id time.sleep(2) print(f"[entity] id → {a.entity_id}: {'ok' if r.get('success') else r.get('error')}") print(f"[entity] {entity_id}") return cmd_ask(ha, argparse.Namespace(agent_entity_id=entity_id, question=a.question)) def cmd_ask(ha: HA, a: argparse.Namespace) -> int: started = time.perf_counter() st, res = ha.rest("POST", "/api/conversation/process", {"text": a.question, "agent_id": a.agent_entity_id, "language": "en"}) took = time.perf_counter() - started resp = res.get("response", {}) if isinstance(res, dict) else {} speech = ((resp.get("speech") or {}).get("plain") or {}).get("speech") rtype = resp.get("response_type") code = (resp.get("data") or {}).get("code") print(f"[ask] {a.agent_entity_id}: HTTP {st} response_type={rtype}{' code=' + code if code else ''} took={took:.1f}s") print(f"[ask] Q: {a.question}") print(f"[ask] A: {speech!r}") return 0 if st == 200 and speech and rtype != "error" else 2 def cmd_list(ha: HA, a: argparse.Namespace) -> int: for e in ha.entries(): print(f"{e['entry_id'][:8]} {e['state']:<10} {e['title']}") for s in e.get("subentries") or []: print(f" └─ {s['subentry_type']}: {s['title']}") for x in ha.conversation_entities(): print(f"entity {x['entity_id']} ({x.get('name') or x.get('original_name')})") return 0 def cmd_delete(ha: HA, a: argparse.Namespace) -> int: url = a.forgeway_url.rstrip("/") entry = ha.entry_for(url) if not entry: print(f"[delete] no entry for {url}") return 0 st, res = ha.rest("DELETE", f"/api/config/config_entries/entry/{entry['entry_id']}") print(f"[delete] {entry['entry_id'][:8]} '{entry['title']}': HTTP {st} {json.dumps(res)[:100]}") return 0 if st == 200 else 1 def main() -> int: ap = argparse.ArgumentParser() ap.add_argument("--ha", default="http://192.168.120.12:8123") ap.add_argument("--token-entry", default="hassio/lounge-token", help="pass entry holding the HA long-lived token") sub = ap.add_subparsers(dest="cmd", required=True) s = sub.add_parser("setup"); s.add_argument("forgeway_url"); s.add_argument("--agent", required=True) s.add_argument("--title"); s.add_argument("--entity-id"); s.add_argument("--model", default="gpt-oss:120b") s.add_argument("--assist", action="store_true"); s.add_argument("--question", default="In one short sentence, what is Home Assistant?") q = sub.add_parser("ask"); q.add_argument("agent_entity_id"); q.add_argument("question") sub.add_parser("list") d = sub.add_parser("delete"); d.add_argument("forgeway_url") a = ap.parse_args() global TOKEN_ENTRY TOKEN_ENTRY = a.token_entry ha = HA(a.ha) return {"setup": cmd_setup, "ask": cmd_ask, "list": cmd_list, "delete": cmd_delete}[a.cmd](ha, a) if __name__ == "__main__": raise SystemExit(main())