#!/usr/bin/env python3
"""
Test script to verify Hue Bridge connection and basic functionality.
"""
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_connection():
"""Test connection to Hue Bridge and basic operations."""
# Load environment variables
load_dotenv()
bridge_ip = os.getenv("HUE_BRIDGE_IP")
api_key = os.getenv("HUE_API_KEY")
print("=" * 60)
print("Philips Hue MCP Server - Connection Test")
print("=" * 60)
print()
# Verify credentials
if not bridge_ip:
print("β ERROR: HUE_BRIDGE_IP not set in .env file")
return False
if not api_key:
print("β ERROR: HUE_API_KEY not set in .env file")
return False
print(f"π Bridge IP: {bridge_ip}")
print(f"π API Key: {api_key[:10]}..." if len(api_key) > 10 else f"π API Key: {api_key}")
print()
# Create client
print("π Connecting to Hue Bridge...")
client = HueClient(bridge_ip, api_key)
try:
# Test connection
await client.connect()
print("β
Connected successfully!")
print()
# Test getting lights
print("π‘ Fetching lights...")
lights = await client.get_lights()
print(f"β
Found {len(lights)} light(s)")
print()
if lights:
print("Light Details:")
print("-" * 60)
for light_id, light_data in lights.items():
status = "ON" if light_data["on"] else "OFF"
brightness = light_data.get("brightness")
brightness_str = f" | Brightness: {brightness:.0f}%" if brightness else ""
print(f" β’ {light_data['name']}")
print(f" ID: {light_id} | Status: {status}{brightness_str}")
print()
# Test getting groups
print("π Fetching groups/rooms...")
groups = await client.get_groups()
print(f"β
Found {len(groups)} group(s)")
print()
if groups:
print("Group Details:")
print("-" * 60)
for group_id, group_data in groups.items():
light_count = len(group_data.get("lights", []))
print(f" β’ {group_data['name']}")
print(f" ID: {group_id} | Type: {group_data['type']} | Lights: {light_count}")
print()
# Test getting scenes
print("π¨ Fetching scenes...")
scenes = await client.get_scenes()
print(f"β
Found {len(scenes)} scene(s)")
print()
if scenes:
print("Scene Details (showing first 5):")
print("-" * 60)
for i, (scene_id, scene_data) in enumerate(list(scenes.items())[:5]):
print(f" β’ {scene_data['name']}")
print(f" ID: {scene_id}")
if len(scenes) > 5:
print(f" ... and {len(scenes) - 5} more scenes")
print()
print("=" * 60)
print("β
All tests passed! Your Hue Bridge is ready to use.")
print("=" * 60)
print()
print("Next steps:")
print(" 1. Run the MCP server: python -m hue_mcp_server.server")
print(" 2. Or configure it with Claude Desktop (see README.md)")
print()
return True
except Exception as e:
print(f"β ERROR: {e}")
print()
print("Troubleshooting:")
print(" 1. Verify your bridge IP is correct")
print(" 2. Check that your API key is valid")
print(" 3. Ensure you're on the same network as the bridge")
print(" 4. Try pressing the bridge button and generating a new API key")
return False
finally:
await client.disconnect()
print("π Disconnected from bridge")
if __name__ == "__main__":
success = asyncio.run(test_connection())
sys.exit(0 if success else 1)