#!/bin/bash # Test HDMI-CEC functionality set -e SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color echo -e "${BLUE}==================================${NC}" echo -e "${BLUE}HDMI-CEC Test Script${NC}" echo -e "${BLUE}==================================${NC}" echo "" # Check if cec-client is installed echo -e "${YELLOW}1. Checking for cec-client...${NC}" if command -v cec-client &> /dev/null; then echo -e "${GREEN}✓ cec-client found${NC}" CEC_VERSION=$(cec-client --version 2>&1 | head -n1 || echo "unknown") echo " Version: $CEC_VERSION" else echo -e "${RED}✗ cec-client not found${NC}" echo "" echo "Install with:" echo " sudo apt-get install cec-utils" exit 1 fi echo "" # Scan for CEC devices echo -e "${YELLOW}2. Scanning for CEC devices...${NC}" echo "This may take a few seconds..." SCAN_OUTPUT=$(echo "scan" | cec-client -s -d 1 2>&1) echo "$SCAN_OUTPUT" | grep -E "device|found" || echo "$SCAN_OUTPUT" echo "" # Check for TV if echo "$SCAN_OUTPUT" | grep -q "device #0"; then echo -e "${GREEN}✓ TV detected as device 0${NC}" else echo -e "${YELLOW}⚠ TV not detected on device 0${NC}" echo " Check if HDMI-CEC is enabled on your TV" fi echo "" # Query TV power status echo -e "${YELLOW}3. Checking TV power status...${NC}" POW_OUTPUT=$(echo "pow 0" | cec-client -s -d 1 2>&1) echo "$POW_OUTPUT" | grep -i "power status" || echo "Could not determine power status" echo "" # Interactive menu while true; do echo -e "${BLUE}==================================${NC}" echo "Choose an option:" echo " 1) Turn TV ON" echo " 2) Turn TV OFF (standby)" echo " 3) Check TV power status" echo " 4) Scan for devices" echo " 5) Test Display Manager CEC integration" echo " 6) View CEC logs from Display Manager" echo " q) Quit" echo "" read -p "Enter choice: " choice echo "" case $choice in 1) echo -e "${YELLOW}Sending TV power ON command...${NC}" echo "on 0" | cec-client -s -d 1 echo -e "${GREEN}Command sent${NC}" sleep 2 ;; 2) echo -e "${YELLOW}Sending TV standby command...${NC}" echo "standby 0" | cec-client -s -d 1 echo -e "${GREEN}Command sent${NC}" sleep 2 ;; 3) echo -e "${YELLOW}Checking TV power status...${NC}" echo "pow 0" | cec-client -s -d 1 sleep 1 ;; 4) echo -e "${YELLOW}Scanning for CEC devices...${NC}" echo "scan" | cec-client -s -d 1 sleep 1 ;; 5) echo -e "${YELLOW}Testing Display Manager CEC integration...${NC}" echo "" echo "This will run a Python test of the HDMICECController class" echo "" # Run Python test inline (no temp file) cd "$PROJECT_ROOT" python3 << 'PYTEST' import sys import os sys.path.insert(0, 'src') # Load environment configuration with explicit path from dotenv import load_dotenv env_path = os.path.join(os.getcwd(), '.env') if os.path.exists(env_path): load_dotenv(env_path) else: print(f"Warning: .env file not found at {env_path}") from display_manager import HDMICECController # Read configuration from environment (SAME LOGIC as display_manager.py) ENV = os.getenv("ENV", "development") CEC_ENABLED = os.getenv("CEC_ENABLED", "true").lower() in ("true", "1", "yes") # Apply development mode override (same as display_manager.py does) if ENV == "development": print("=" * 70) print("DEVELOPMENT MODE DETECTED") print("=" * 70) print("") print("HDMI-CEC is automatically DISABLED in development mode") print("to prevent constantly switching the TV on/off during testing.") print("") print("To test CEC functionality:") print(" 1. Change ENV=production in .env") print(" 2. Or use options 1-4 in this menu for manual CEC commands") print("") print("Skipping CEC integration test.") print("=" * 70) sys.exit(0) CEC_DEVICE = os.getenv("CEC_DEVICE", "0") # Default to 0 CEC_TURN_OFF_DELAY = int(os.getenv("CEC_TURN_OFF_DELAY", "30")) CEC_POWER_ON_WAIT = int(os.getenv("CEC_POWER_ON_WAIT", "5")) CEC_POWER_OFF_WAIT = int(os.getenv("CEC_POWER_OFF_WAIT", "2")) print("Initializing HDMI-CEC Controller...") print(f"Environment: {ENV}") print(f"Using configuration from .env file:") print(f" CEC_ENABLED={CEC_ENABLED}") print(f" CEC_DEVICE={CEC_DEVICE}") print(f" CEC_TURN_OFF_DELAY={CEC_TURN_OFF_DELAY}") print(f" CEC_POWER_ON_WAIT={CEC_POWER_ON_WAIT}") print(f" CEC_POWER_OFF_WAIT={CEC_POWER_OFF_WAIT}") print("") cec = HDMICECController( enabled=CEC_ENABLED, device=CEC_DEVICE, turn_off_delay=CEC_TURN_OFF_DELAY, power_on_wait=CEC_POWER_ON_WAIT, power_off_wait=CEC_POWER_OFF_WAIT ) if not cec.enabled: print("ERROR: CEC controller could not initialize (cec-client missing?)") sys.exit(1) print(f"\nCEC Controller initialized successfully") print(f" Device: {cec.device}") print(f" Turn-off delay: {cec.turn_off_delay}s") print(f" Power ON wait: {cec.power_on_wait}s") print(f" Power OFF wait: {cec.power_off_wait}s") print(f" Current TV state: {cec.tv_state}") print("") print("=" * 70) print("Test 1: Turn TV ON") print("=" * 70) print(f"Command: 'on {cec.device}'") print("Watch your TV screen - it should power ON now...") print("") result = cec.turn_on() print(f"Result: {'[OK] SUCCESS - TV should be ON' if result else '[FAIL] FAILED'}") if result: print(f"(Waited {cec.power_on_wait}s for TV to boot)") print("") import time print("Waiting 30 seconds to let TV fully initialize...") for i in range(30, 0, -1): print(f" {i}s remaining...", end='\r') time.sleep(1) print("") # New line after countdown print("=" * 70) print("Test 2: Turn TV OFF with delay") print("=" * 70) print(f"Scheduling turn-off in {cec.turn_off_delay}s...") print("") result = cec.turn_off(delayed=True) print(f"Result: Scheduled (timer started)") print("") time.sleep(2) print("=" * 70) print("Test 3: Cancel turn-off") print("=" * 70) cec.cancel_turn_off() print("Result: [OK] Timer cancelled (TV stays ON)") print("") time.sleep(1) print("=" * 70) print("Test 4: Turn TV OFF immediately") print("=" * 70) print(f"Command: 'standby {cec.device}'") print("Watch your TV screen - it should power OFF now...") print("") result = cec.turn_off(delayed=False) print(f"Result: {'[OK] SUCCESS - TV should be OFF' if result else '[FAIL] FAILED'}") if result: print(f"(Waited {cec.power_off_wait}s for TV to enter standby)") print("") print("=" * 70) print("All tests completed!") print("=" * 70) print("") print("Did your TV actually turn ON and OFF?") print("If not, try:") print(f" 1. Increase CEC_POWER_ON_WAIT in .env (currently {cec.power_on_wait}s)") print(" 2. Check TV CEC settings (must be enabled)") print(" 3. Try manual command: echo 'on {cec.device}' | cec-client -s -d 1") PYTEST ;; 6) echo -e "${YELLOW}Viewing CEC-related logs...${NC}" echo "" LOG_FILE="$PROJECT_ROOT/logs/display_manager.log" if [ -f "$LOG_FILE" ]; then echo "Last 20 CEC-related log entries:" echo "================================" grep -i -E "(cec|tv|turn)" "$LOG_FILE" | tail -n 20 || echo "No CEC logs found" else echo "Log file not found: $LOG_FILE" echo "Display Manager may not have run yet" fi echo "" read -p "Press Enter to continue..." ;; q|Q) echo "Exiting..." exit 0 ;; *) echo -e "${RED}Invalid choice${NC}" ;; esac echo "" done