-
-
Save Snyder1977/0dc669838e65dab2a818 to your computer and use it in GitHub Desktop.
Squeezebox/LMS send CEC command when play starts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from __future__ import print_function | |
from future.standard_library import install_aliases | |
install_aliases() | |
from urllib.parse import unquote | |
import select, socket | |
import cec | |
config = { | |
'lms_server': ('192.168.1.10', 9090), # IP of your Squeeze Server, and the network control port (usually 9090) | |
'player_mac': '00:00:00:00:00:00', # the MAC address of your squeezebox (or fake MAC if using squeezelite) | |
'cec_output': 5, # the device which we will switch on, and set the input to cec_input | |
'cec_input': 1 # the device which will play (e.g. the raspberry pi) this probably changes vendor to vendor, play around with it | |
} | |
#TODO - option to use JSON RPC? | |
def connect(server=None): | |
if not server: | |
global config | |
server = config['lms_server'] | |
conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
conn.connect((server[0], server[1])) | |
conn.sendall(b'subscribe play\n') | |
return conn | |
conn = connect() | |
while True: | |
try: | |
ready_to_read, ready_to_write, in_error = \ | |
select.select([conn,], [conn,], [], 5) | |
except select.error: | |
conn.shutdown(2) # 0 = done receiving, 1 = done sending, 2 = both | |
conn.close() | |
# connection error event here, maybe reconnect | |
print('connection error, retrying') | |
conn = connect() | |
if len(ready_to_read) > 0: | |
recv = conn.recv(2048) | |
command = unquote(recv.decode().strip()).split() | |
if command[0] == config['player_mac'] and command[1] == 'play': | |
print("play command received") | |
cec.init() | |
receiver = cec.Device(config['cec_output']) | |
if not receiver.is_on(): | |
if receiver.power_on(): | |
print("turned receiver on") | |
else: | |
print("couldn't turn receiver on") | |
if receiver.set_av_input(config['cec_input']): | |
print("set input on receiver") | |
else: | |
print("couldn't set input on receiver") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment