"""Запускает QR-поток YandexStation и сохраняет QR в /tmp/qr.svg.""" import json import sys import urllib.error import urllib.request TOK = open("/root/.ha-token", encoding="utf-8").read().strip() BASE = "http://127.0.0.1:8123" HDR = {"Authorization": "Bearer " + TOK, "Content-Type": "application/json"} def call(path, method="GET", body=None): data = json.dumps(body).encode() if body is not None else None req = urllib.request.Request(BASE + path, data=data, method=method, headers=HDR) try: with urllib.request.urlopen(req, timeout=45) as r: raw = r.read() return (json.loads(raw) if raw else {}), None except urllib.error.HTTPError as e: return None, f"HTTP {e.code}: {e.read()[:200].decode('utf-8', 'replace')}" except Exception as e: return None, str(e)[:120] flow, err = call("/api/config/config_entries/flow", "POST", {"handler": "yandex_station"}) if err: print("старт:", err) sys.exit(1) fid = flow["flow_id"] res, err2 = call(f"/api/config/config_entries/flow/{fid}", "POST", {"method": "qr"}) if err2: print("qr:", err2) sys.exit(1) ph = (res or {}).get("description_placeholders") or {} svg = ph.get("qr_data") or "" url = ph.get("qr_url") or "" open("/tmp/qr.svg", "w", encoding="utf-8").write(svg) open("/tmp/yandex_flow_id", "w").write(fid) open("/tmp/qr_url", "w").write(url) print("svg:", len(svg), "| url:", len(url), "| шаг:", (res or {}).get("step_id"))