""" Tests for Home Assistant API routes """ from unittest.mock import patch from fastapi.testclient import TestClient from main import app client = TestClient(app) class TestHomeAssistantRoutes: """Test Home Assistant API routes""" @patch("routes.home_assistant.SERVICES") def test_get_entities_success(self, mock_services): """Test successful retrieval of Home Assistant entities""" # Mock the services configuration to enable Home Assistant mock_services.__getitem__.return_value = {"enabled": True} response = client.get("/home-assistant/entities") assert response.status_code == 200 data = response.json() assert "entities" in data assert len(data["entities"]) == 2 # Check first entity (mock data from the route) cpu_entity = data["entities"][0] assert cpu_entity["entity_id"] == "sensor.cpu_usage" assert cpu_entity["state"] == "45.2" assert cpu_entity["attributes"]["unit_of_measurement"] == "%" @patch("routes.home_assistant.SERVICES") def test_get_entities_disabled(self, mock_services): """Test handling when Home Assistant is disabled""" # Mock the services configuration to disable Home Assistant mock_services.__getitem__.return_value = {"enabled": False} response = client.get("/home-assistant/entities") assert response.status_code == 503 assert "not configured" in response.json()["detail"] def test_get_entities_endpoint_exists(self): """Test that the entities endpoint exists""" # This will fail if the route doesn't exist, but we can't test the actual # functionality without mocking the Home Assistant API response = client.get("/home-assistant/entities") # Should return either 200 (success), 500 (API error), or 503 (service unavailable) assert response.status_code in [200, 500, 503] def test_home_assistant_routes_available(self): """Test that Home Assistant routes are available""" # Check that the router is included by testing a known endpoint response = client.get("/home-assistant/entities") # Should not return 404 (route not found) assert response.status_code != 404