"""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()