-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathz906cec.py
executable file
·113 lines (83 loc) · 3.48 KB
/
z906cec.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#! /usr/bin/python3
import cecclient
import z906client
import time
import traceback
import argparse
import logging
argparser = argparse.ArgumentParser(description="Logitech Z906 CEC translator")
argparser.add_argument('--debug', '-d', dest='debug', help='Enable debugging', default=False, action='store_const', const=True)
argparser.add_argument('--port', '-P', dest='port', help='Z906 serial port', default=z906client.SERIAL_PORT)
argparser.add_argument('--input', '-i', dest='input', help='Z906 input to use (1-6)', default=1, type=int)
argparser.add_argument('--address', '-a', dest='enabled', help='Enabled ARC only for certain HDMI ports', action='append')
class Z906Cec():
enabled_hdmi_ports = None
z906 = None
cecClient = None
logger = logging.getLogger("Z906Cec")
def __init__(self, z906_port, z906_input, enabled_ports = None):
self.enabled_hdmi_ports = enabled_ports
self.logger.info("Enabled HDMI ports : " + str(enabled_ports))
# Init the Z906
self.logger.info("Connecting to Z906 ...")
self.z906 = z906client.Z906Client(z906_port)
self.z906.update()
self.z906.power_off()
self.logger.debug("Connected to Z906")
# Init CEC
self.logger.info("Initiating CEC ...")
self.cecClient = cecclient.CecClient("Z906")
self.cecClient.open()
self.logger.debug("CEC initialized")
self.cecClient.setEventCallback(self._cecCallback)
self.logger.info("Ready !")
def __del__(self):
self.logger.info("Powering off Z906")
self.z906.power_off()
def _cecCallback(self, evt):
self.logger.debug("Got event " + evt)
if evt == "level_up":
self.z906.level_up()
self.cecClient.reportAudioStatus(self.z906.get_level(), self.z906.is_muted())
elif evt == "level_down":
self.z906.level_down()
self.cecClient.reportAudioStatus(self.z906.get_level(), self.z906.is_muted())
elif evt == "mute":
self.z906.mute_toggle()
self.cecClient.reportAudioStatus(self.z906.get_level(), self.z906.is_muted())
elif evt == "give_audio_status":
self.cecClient.reportAudioStatus(self.z906.get_level(), self.z906.is_muted())
elif evt == "arc_start":
if self.cecClient.is_enabled():
self.z906.power_on()
self.z906.select_input(z906_input)
elif evt == "arc_stop":
if self.cecClient.is_enabled():
self.z906.power_off()
elif evt == "standby":
self.z906.power_off()
elif evt == "src_changed":
src_port = self.cecClient.get_src_port()
self.logger.info("Source changed to " + src_port)
if not self.enabled_hdmi_ports:
return
for p in self.enabled_hdmi_ports:
if src_port.startswith(p):
self.cecClient.enable()
self.z906.power_on()
self.z906.select_input(z906_input)
return
self.cecClient.disable()
self.z906.power_off()
if __name__ == "__main__":
args = argparser.parse_args()
if args.debug:
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(level=logging.INFO)
z906cec = Z906Cec(args.port, args.input, args.enabled)
while True:
try:
time.sleep(1)
except KeyboardInterrupt:
break