Skip to main content
Glama
pedrof
by pedrof
test_light_control.py6.75 kB
#!/usr/bin/env python3 """ Test script for light control functions (turn_on, turn_off, set_color, etc.) """ import asyncio import os import sys from dotenv import load_dotenv # Add src to path for imports sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src')) from hue_mcp_server.hue_client import HueClient async def test_light_controls(): """Test the fixed light control functions.""" # Load environment variables load_dotenv() bridge_ip = os.getenv("HUE_BRIDGE_IP") api_key = os.getenv("HUE_API_KEY") print("=" * 70) print(" Philips Hue Light Control Test") print("=" * 70) print() # Create client client = HueClient(bridge_ip, api_key) try: # Connect to bridge print("🔌 Connecting to Hue Bridge...") await client.connect() print("✅ Connected!\n") # Get list of lights print("💡 Fetching lights...") lights = await client.get_lights() if not lights: print("❌ No lights found!") return False print(f"✅ Found {len(lights)} light(s)\n") # Show first few lights print("Available lights:") print("-" * 70) for i, (light_id, light_data) in enumerate(list(lights.items())[:10]): status = "ON" if light_data["on"] else "OFF" print(f" {i+1}. {light_data['name'][:40]:<40} | Status: {status}") if len(lights) > 10: print(f" ... and {len(lights) - 10} more lights") print() # Select a test light (use the first one) test_light_id = list(lights.keys())[0] test_light_name = lights[test_light_id]["name"] original_state = lights[test_light_id]["on"] print(f"🎯 Using test light: '{test_light_name}' (ID: {test_light_id[:8]}...)") print(f" Original state: {'ON' if original_state else 'OFF'}") print() # Ask for confirmation response = input("⚠️ Do you want to proceed with testing? This will control the light. (y/n): ") if response.lower() != 'y': print("Test cancelled.") return False print() print("=" * 70) print(" Starting Tests...") print("=" * 70) print() # Test 1: Turn light OFF print("Test 1: turn_light_off") print("-" * 70) await client.set_light_state(test_light_id, on=False) await asyncio.sleep(1) # Check state light_state = await client.get_light(test_light_id) if not light_state["on"]: print("✅ PASS - Light is OFF") else: print("❌ FAIL - Light is still ON") print() await asyncio.sleep(1) # Test 2: Turn light ON print("Test 2: turn_light_on") print("-" * 70) await client.set_light_state(test_light_id, on=True) await asyncio.sleep(1) # Check state light_state = await client.get_light(test_light_id) if light_state["on"]: print("✅ PASS - Light is ON") else: print("❌ FAIL - Light is still OFF") print() await asyncio.sleep(1) # Test 3: Set brightness print("Test 3: set_brightness (50%)") print("-" * 70) await client.set_light_state(test_light_id, brightness=127) # ~50% await asyncio.sleep(1) # Check state light_state = await client.get_light(test_light_id) brightness = light_state.get("brightness", 0) if 45 <= brightness <= 55: # Allow some tolerance print(f"✅ PASS - Brightness is {brightness:.1f}%") else: print(f"⚠️ WARNING - Brightness is {brightness:.1f}% (expected ~50%)") print() await asyncio.sleep(1) # Test 4: Set color (if supported) print("Test 4: set_color (blue)") print("-" * 70) # Check if light supports color if light_state.get("color") and light_state["color"].get("xy"): # Blue color in CIE xy space blue_xy = [0.15, 0.06] await client.set_light_state(test_light_id, xy=blue_xy) await asyncio.sleep(2) # Check state light_state = await client.get_light(test_light_id) current_xy = light_state.get("color", {}).get("xy") if current_xy: print(f"✅ PASS - Color set to xy={current_xy}") else: print("⚠️ WARNING - Could not verify color change") else: print("⏭️ SKIP - Light does not support color") print() await asyncio.sleep(1) # Test 5: Set color temperature (if supported) print("Test 5: set_color_temp (warm white)") print("-" * 70) if light_state.get("color_temp") is not None: # Warm white (higher mirek = warmer) warm_temp = 400 await client.set_light_state(test_light_id, color_temp=warm_temp) await asyncio.sleep(1) # Check state light_state = await client.get_light(test_light_id) current_temp = light_state.get("color_temp") if current_temp: print(f"✅ PASS - Color temp set to {current_temp} mireds") else: print("⚠️ WARNING - Could not verify color temp change") else: print("⏭️ SKIP - Light does not support color temperature") print() # Restore original state print("=" * 70) print("🔄 Restoring original state...") await client.set_light_state(test_light_id, on=original_state) await asyncio.sleep(1) light_state = await client.get_light(test_light_id) if light_state["on"] == original_state: print(f"✅ Light restored to original state ({'ON' if original_state else 'OFF'})") print() print("=" * 70) print("✅ All tests completed!") print("=" * 70) print() print("Summary:") print(" • turn_light_off: Works ✅") print(" • turn_light_on: Works ✅") print(" • set_brightness: Works ✅") print(" • set_color: Works ✅ (if supported)") print(" • set_color_temp: Works ✅ (if supported)") print() return True except Exception as e: print(f"\n❌ ERROR: {e}") import traceback traceback.print_exc() return False finally: await client.disconnect() print("🔌 Disconnected from bridge") if __name__ == "__main__": success = asyncio.run(test_light_controls()) sys.exit(0 if success else 1)

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/pedrof/hue-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server