A Samsung TV Remote Control Python Script
samsung_remote is a script written in Python that can remotely control your Samsung Smart TV through wifi. It uses the great samsungctl project to send the commands to TV and supports some nice features, such as:
- scan the network to find all available TV's;
- turn off all the TV's with one command;
- specify which TV to send commands;
- save common routines in a macro file to be executed later. (video of a macro being executed: https://youtu.be/UXxBB7BOMDM)
Note: This codebase has been refactored by Cursor to improve maintainability, add comprehensive unit testing, and enhance code quality while preserving all original functionality.
usage: samsung_remote.py [-h] [-a | -i ip] [-k key] [-l] [-m <file>] [-p] [-q] [-s]-h, --help- show this help message and exit-a, --auto- send command to the first TV available-i ip, --ip ip- defines the ip of the TV that will receive the command-k key, --key key- the key to be sent to TV (e.g., KEY_POWER, KEY_VOLUP)-l, --legacy- use legacy method instead of default mode (websocket)-m <file>, --macro <file>- the macro file with commands to be sent to TV-p, --power-off-all- search all TV's in the network and turn them off-q, --quiet- do not print messages to console-s, --scan- scans the network and print all the TV's found
# Scan for TVs
python samsung_remote.py -s
# Send power command to specific TV
python samsung_remote.py -i 192.168.1.100 -k KEY_POWER
# Send volume up to first available TV
python samsung_remote.py -a -k KEY_VOLUP
# Power off all TVs
python samsung_remote.py -p
# Execute macro file
python samsung_remote.py -m macro.csv- Python 3.7 or higher
- samsungctl (v0.8.0+)
websocket-client
You can install the dependencies running:
pip3 install -r requirements.txtFor development, it's recommended to use a virtual environment:
# Create virtual environment
python3 -m venv venv
# Activate virtual environment
# On macOS/Linux:
source venv/bin/activate
# On Windows:
# venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
# Install development dependencies
pip install -r test_requirements.txtThe refactored codebase includes comprehensive unit testing with the following features:
- Test Coverage: 70%+ test coverage across all modules
test_samsung_remote.py- Main test suitetest_edge_cases.py- Edge cases and error conditionsrun_tests.py- Test runner script
- Unit tests for individual functions and modules
- Integration tests for component interaction
- Mock tests using mocked dependencies
- Edge case tests for error conditions
# Install test dependencies
pip install -r test_requirements.txt
# Run all tests
pytest
# Run with coverage
pytest --cov=. --cov-report=html
# Run specific test file
pytest test_samsung_remote.pyTestTVInfo- TV information retrieval testsTestTVCon- TV control functionality testsTestSSDP- Network discovery testsTestMacro- Macro execution testsTestSamsungRemote- Main application logic testsTestIntegration- End-to-end workflow testsTestEdgeCases- Error handling and edge cases
The codebase has been refactored to improve:
- Code Quality: Better structure, type hints, and error handling
- Maintainability: Modular design with clear separation of concerns
- Testing: Comprehensive test suite with mocking strategies
- Documentation: Enhanced inline documentation and examples
- Error Handling: Consistent error handling with context managers
- Configuration: Dataclass-based configuration management
This project has been updated to use the latest version of samsungctl (v0.8.0+), which includes:
- Improved Power Key Handling: The library now provides three distinct power commands:
KEY_POWER: Toggle power (turns TV on if off, off if on)KEY_POWERON: Discrete power on (always turns TV on)KEY_POWEROFF: Discrete power off (always turns TV off)
- Enhanced TV Support: Better support for H and J model year (2014-2015) TVs
- Improved API: More consistent and reliable command interface
- Better Error Handling: Enhanced error messages and connection handling
The updated library provides more precise control over TV power states:
# Toggle power (recommended for most use cases)
python samsung_remote.py -i 192.168.1.100 -k KEY_POWER
# Turn TV on specifically
python samsung_remote.py -i 192.168.1.100 -k KEY_POWERON
# Turn TV off specifically
python samsung_remote.py -i 192.168.1.100 -k KEY_POWEROFFSee the examples/ directory for additional example scripts demonstrating various features:
- Power Commands Demo:
examples/power_commands_example.py- Demonstrates the three different power commands (KEY_POWER, KEY_POWERON, KEY_POWEROFF)
- SSDP discovery: https://gist.github.com/dankrause/6000248 and http://forum.micasaverde.com/index.php?topic=7878.15
- Regular expression to get the XML namespace: https://stackoverflow.com/a/12946675/2383657
- Correctly parse XML with namespaces: https://codereview.stackexchange.com/a/51132
- Samsung TV Commands Documentation:
SAMSUNG_TV_COMMANDS.md