benq-rm-homeassistant/custom_components/benq_smartboard/serial_test.py
2025-06-06 15:57:10 +07:00

181 lines
5.7 KiB
Python

#!/usr/bin/env python3
"""
Test script for BenQ Smart Board library using USB Serial connection.
"""
import logging
import time
from benq_smartboard_lib import (
BenQSmartBoard,
ConnectionType,
PowerState,
VideoSource,
BenQSmartBoardError
)
# Configure logging
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(message)s'
)
def test_serial_connection():
"""Test the serial connection functionality."""
# Configure your serial port here
SERIAL_PORT = 'COM3' # Windows example, use '/dev/ttyUSB0' for Linux
BAUDRATE = 115200
print(f"Testing BenQ Smart Board via Serial: {SERIAL_PORT} @ {BAUDRATE}")
# Create Smart Board instance with Serial connection
board = BenQSmartBoard(
connection_type=ConnectionType.SERIAL,
serial_port=SERIAL_PORT,
baudrate=BAUDRATE,
timeout=5.0
)
try:
# Connect to the Smart Board
print("Connecting to Smart Board...")
board.connect()
print("Connected successfully!")
# Test basic commands
print("\n=== Testing Basic Commands ===")
# Get current power state
try:
power_state = board.get_power()
print(f"Current power state: {power_state}")
except Exception as e:
print(f"Failed to get power state: {e}")
# Get current video source
try:
video_source = board.get_video_source()
print(f"Current video source: {video_source}")
except Exception as e:
print(f"Failed to get video source: {e}")
# Get current volume
try:
volume = board.get_volume()
print(f"Current volume: {volume}")
except Exception as e:
print(f"Failed to get volume: {e}")
# Get current brightness
try:
brightness = board.get_brightness()
print(f"Current brightness: {brightness}")
except Exception as e:
print(f"Failed to get brightness: {e}")
print("\n=== Testing Set Commands ===")
# Test setting volume
try:
print("Setting volume to 50...")
success = board.set_volume(50)
print(f"Set volume result: {'Success' if success else 'Failed'}")
time.sleep(1)
# Verify the change
new_volume = board.get_volume()
print(f"New volume: {new_volume}")
except Exception as e:
print(f"Failed to set volume: {e}")
# Test setting brightness
try:
print("Setting brightness to 80...")
success = board.set_brightness(80)
print(f"Set brightness result: {'Success' if success else 'Failed'}")
time.sleep(1)
# Verify the change
new_brightness = board.get_brightness()
print(f"New brightness: {new_brightness}")
except Exception as e:
print(f"Failed to set brightness: {e}")
# Test video source switching
try:
print("Switching to HDMI1...")
success = board.set_video_source(VideoSource.HDMI1)
print(f"Set video source result: {'Success' if success else 'Failed'}")
time.sleep(2)
# Verify the change
new_source = board.get_video_source()
print(f"New video source: {new_source}")
except Exception as e:
print(f"Failed to set video source: {e}")
print("\n=== Testing Advanced Commands ===")
# Test RTC reading
try:
rtc_info = board.get_rtc()
print(f"RTC Info: {rtc_info}")
except Exception as e:
print(f"Failed to get RTC info: {e}")
# Test mute functionality
try:
print("Testing mute...")
success = board.set_mute(True)
print(f"Mute ON result: {'Success' if success else 'Failed'}")
time.sleep(2)
success = board.set_mute(False)
print(f"Mute OFF result: {'Success' if success else 'Failed'}")
except Exception as e:
print(f"Failed to test mute: {e}")
print("\n=== Test Completed Successfully ===")
except BenQSmartBoardError as e:
print(f"Smart Board Error: {e}")
except Exception as e:
print(f"Unexpected error: {e}")
logging.exception("Unexpected error occurred")
finally:
# Always disconnect
print("Disconnecting...")
board.disconnect()
print("Disconnected.")
def list_serial_ports():
"""List available serial ports."""
try:
import serial.tools.list_ports
ports = serial.tools.list_ports.comports()
print("Available serial ports:")
for port in ports:
print(f" {port.device} - {port.description}")
return [port.device for port in ports]
except ImportError:
print("pyserial tools not available. Install with: pip install pyserial")
return []
if __name__ == "__main__":
print("BenQ Smart Board Serial Test")
print("=" * 40)
# List available ports
available_ports = list_serial_ports()
if not available_ports:
print("No serial ports found or pyserial not installed.")
print("Make sure to:")
print("1. Install pyserial: pip install pyserial")
print("2. Connect your BenQ Smart Board via USB")
print("3. Update the SERIAL_PORT variable in this script")
else:
print(f"\nFound {len(available_ports)} serial port(s)")
print("\nStarting test...")
test_serial_connection()