This project provides an automated HDMI-CEC monitoring solution that manages the Kodi media player based on TV state changes. It intelligently controls Kodi by starting and stopping the application in response to TV power states and HDMI source switching, creating a seamless integration between your Raspberry Pi and TV.
The monitor actively listens to HDMI-CEC commands from your TV and responds appropriately. When the TV enters standby mode, it gracefully stops Kodi after a configurable delay. When the TV switches to the HDMI input where the Raspberry Pi is connected, it automatically launches Kodi. This automation enhances the user experience by eliminating the need for manual intervention in starting and stopping the media center.
.
└── cec-monitor.py # Main script that handles CEC monitoring and Kodi control
- Raspberry Pi with HDMI-CEC support
- Python 3.x installed
- Kodi media player installed
cec-utilspackage installed for CEC control- Sudo privileges for CEC commands
- Clone the repository to your Raspberry Pi:
git clone [repository-url]
cd [repository-name]- Install required system packages:
sudo apt-get update
sudo apt-get install cec-utils procps- Run the monitor script:
sudo python3 cec-monitor.py- The script will:
- Detect the physical address of your Raspberry Pi
- Start monitoring CEC commands
- Automatically manage Kodi based on TV state changes
When your TV enters standby mode:
Received from TV to all (0 to 15): STANDBY (0x36)
> Script detects STANDBY command
> Initiates 10-second countdown
> Gracefully stops Kodi process
When your TV powers on and selects Raspberry Pi input:
Received from TV to all (0 to 15): ACTIVE_SOURCE (0x82):
phys-addr: 1.0.0.0
> Script detects ACTIVE_SOURCE command
> Verifies physical address matches Raspberry Pi
> Launches Kodi automatically
When switching to Raspberry Pi input:
Received from TV to all (0 to 15): ROUTING_CHANGE (0x80):
orig-phys-addr: 0.0.0.0
new-phys-addr: 1.0.0.0
> Script detects source change to Raspberry Pi
> Starts Kodi if not already running
When switching away from Raspberry Pi:
Received from TV to all (0 to 15): ACTIVE_SOURCE (0x82):
phys-addr: 0.0.0.0
> Script detects different source activation
> Initiates Kodi shutdown sequence
The script implements smart delays to prevent unwanted Kodi shutdowns:
# Default delay before Kodi shutdown
time.sleep(10) # 10 seconds delay
# Optional extended delay (uncomment to use)
# time.sleep(300) # 5 minutes delayExample of successful monitoring initialization:
Physical Address: 1.0.0.0
Starting Monitoring
Monitoring CEC with physical address 1.0.0.0
The script implements a monitoring system that processes HDMI-CEC commands to control Kodi's execution state. It continuously monitors the CEC bus for relevant commands and manages Kodi accordingly.
TV CEC Command ──► CEC Monitor ──► Command Parser ──► Kodi Control
│ │ │ │
│ │ │ │
└──► STANDBY └──► Detection └──► Delayed └──► Start/Stop
ACTIVE_SOURCE Threading Processing Kodi Process
Component Interactions:
- CEC Monitor continuously listens for CEC commands using
cec-ctl - When a command is detected, it's parsed to identify the type (STANDBY/ACTIVE_SOURCE)
- Physical address matching ensures commands are relevant to this device
- Threading handles delayed Kodi shutdown to prevent premature termination
- Process management handles Kodi start/stop operations with proper signal handling
- Error handling ensures robust operation even if Kodi or CEC commands fail
- State tracking prevents duplicate operations on Kodi