homelab-codex-ws/services/ha-diag-agent/tests/test_ha_client.py
Oskar Kapala ab8895d28b feat(ha-diag-agent): scaffold service with HA REST client and event emitter
- new per-host service, follows node-agent pattern
- 7 new HA event types defined (routing in supervisor — Phase 5)
- HeartbeatCheck as pipeline validator (pings /api/, emits ha_websocket_dead)
- service.yaml + host configs for piha (ken) and chelsty-infra (chelsty)
- test scaffolding with aiohttp/aiosqlite mocks (15/15 passing)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-29 12:26:34 +02:00

57 lines
1.8 KiB
Python

"""Tests for HAClient using aioresponses to mock aiohttp."""
from __future__ import annotations
import pytest
from aioresponses import aioresponses
from ha_diag.ha_client import HAClient
HA_URL = "http://homeassistant.test:8123"
TOKEN = "test-token"
@pytest.mark.asyncio
async def test_get_api_status_ok():
with aioresponses() as m:
m.get(f"{HA_URL}/api/", payload={"message": "API running."})
async with HAClient(HA_URL, TOKEN) as client:
result = await client.get_api_status()
assert result == {"message": "API running."}
@pytest.mark.asyncio
async def test_get_api_status_unauthorized():
with aioresponses() as m:
m.get(f"{HA_URL}/api/", status=401)
async with HAClient(HA_URL, TOKEN) as client:
with pytest.raises(Exception):
await client.get_api_status()
@pytest.mark.asyncio
async def test_get_states_returns_list():
payload = [{"entity_id": "light.living_room", "state": "on"}]
with aioresponses() as m:
m.get(f"{HA_URL}/api/states", payload=payload)
async with HAClient(HA_URL, TOKEN) as client:
states = await client.get_states()
assert isinstance(states, list)
assert states[0]["entity_id"] == "light.living_room"
@pytest.mark.asyncio
async def test_get_config_returns_dict():
payload = {"version": "2024.1.0", "location_name": "Home"}
with aioresponses() as m:
m.get(f"{HA_URL}/api/config", payload=payload)
async with HAClient(HA_URL, TOKEN) as client:
config = await client.get_config()
assert config["version"] == "2024.1.0"
@pytest.mark.asyncio
async def test_session_required_without_context_manager():
client = HAClient(HA_URL, TOKEN)
with pytest.raises(RuntimeError, match="context manager"):
await client.get_api_status()