#!/usr/bin/env python3
"""Live read-only smoke test against a real instance (default: `ken`).

NOT part of the pytest suite: it needs a reachable instance and a token at
`token_path`, so it is a separate opt-in script. Read-only, like everything
else here — it calls GET /api/config, GET /api/states and the WebSocket
`*_list` reads, nothing that writes.

    services/ha-mcp/.venv/bin/python services/ha-mcp/tests/smoke_live.py [instance]
"""
import json
import sys
from pathlib import Path

SERVICE_DIR = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(SERVICE_DIR / "src"))

from ha_mcp import tools  # noqa: E402
from ha_mcp.backend import LiveBackend  # noqa: E402
from ha_mcp.config import get_instance, repo_root  # noqa: E402


def main(argv):
    instance = argv[1] if len(argv) > 1 else None
    name, cfg = get_instance(instance, repo_root())

    status = tools.instance_status(LiveBackend(name, cfg, repo_root()))
    print("== instance_status ==")
    print(json.dumps(status, indent=2, ensure_ascii=False))
    if not status.get("reachable"):
        return 0 if status.get("status") == "offline" else 1

    entity_id = argv[2] if len(argv) > 2 else "sensor.thsalon_temperature"
    print(f"\n== get_state({entity_id}) ==")
    state = tools.get_state(LiveBackend(name, cfg, repo_root()), entity_id)
    print(json.dumps(state, indent=2, ensure_ascii=False))
    return 0


if __name__ == "__main__":
    raise SystemExit(main(sys.argv))
