Skip to content

Instantly share code, notes, and snippets.

@askvictor
Last active June 23, 2017 20:14
Show Gist options
  • Save askvictor/5f7fecc3c5f7ca578bca to your computer and use it in GitHub Desktop.
Save askvictor/5f7fecc3c5f7ca578bca to your computer and use it in GitHub Desktop.
Squeezebox/LMS send CEC command when play starts
#!/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
'default_volume': 80, # the volume set on the squeezebox. If set, the volume will always revert to this and
# HDMI will be used for volume. If not set, will use squeezebox vol instead of HDMI
}
#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 playlist,mixer\n')
return conn
conn = connect()
cec.init()
cec.list_devices() # necessary for volume to work
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()
print(command)
if command[0] == config['player_mac'] and command[1] == 'playlist':
if command[2] == 'play' or (command[2] == 'pause' and command[3] == '0'):
print("play command received")
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")
if 'default_volume' in config:
if command[0] == config['player_mac'] and command[1] == 'mixer' and command[2] == 'volume':
if command[3][0] == '+':
print ("volume up")
conn.sendall(b'%s mixer volume %s\n' % (config['player_mac'], config['default_volume']))
cec.volume_up()
elif command[3][0] == '-':
print ("volume down")
conn.sendall(b'%s mixer volume %s\n' % (config['player_mac'], config['default_volume']))
cec.volume_down()
@Snyder1977
Copy link

Well done! The script is working well on my raspberry pi. I´m able to turn on my av-receiver via hdmi-cec. Turning off the receiver doesn't seem to work. Could you also let the script turn off a receiver? The possibility to adjust the volume would also be great...

@askvictor
Copy link
Author

Changed subscribe to playlist rather than play, as subscribing to play seems to have stopped working.

Also added volume control - this will control the volume twice (once at squeezebox, once at the amp). comment out these if you want

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment