"""Добавляет устройства в HA через ping (только host).""" import json 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"} DEVICES = [ "192.168.1.147", "192.168.1.149", "192.168.1.35", "192.168.1.49", "192.168.1.1", "192.168.1.3", "192.168.1.10", "192.168.1.111", "192.168.2.7", ] 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=25) 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()[:150].decode('utf-8', 'replace')}" except Exception as e: return None, str(e)[:100] created = 0 for host in DEVICES: fl, e1 = call("/api/config/config_entries/flow", "POST", {"handler": "ping"}) if e1 or not fl: print(f" {host}: старт {e1}") continue res, e2 = call(f"/api/config/config_entries/flow/{fl['flow_id']}", "POST", {"host": host}) if e2: print(f" {host}: {e2}") continue created += 1 print(f" {host}: {(res or {}).get('type')}") print(f"\n создано: {created}")