/
onkyo.py
executable file
·84 lines (67 loc) · 2.9 KB
/
onkyo.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
#!/usr/bin/env python
from core import eISCP, command_to_iscp, iscp_to_command
import commands
class Onkyo(object):
def __init__(self):
receiver = self.discoverReceiver()
self.Host = receiver.host
self.DefaultMusicVolume = 20
self.VolumeLevels = []
self.volumeThread = None
self.volumeDivider = 7
def TogglePower(self):
currentPowerState = self.PowerState()
if currentPowerState == 0:
self.setPowerState(1)
else:
self.setPowerState(0)
def PowerState(self):
with eISCP(self.Host) as receiver:
response = receiver.raw("PWRQSTN")
if response == "PWR00":
return 0
else:
return 1
def setPowerState(self, state):
with eISCP(self.Host) as receiver:
print("Setting Onkyo Power State to %s" % state)
receiver.raw("PWR0%s" % state)
time.sleep(5) # Give the receiver some time to wake up
if state == 1:
# Only change default volume if the current setting is Spotify/Network
selectedInput = receiver.raw("SLIQSTN")
print("Current selected input is %s" % selectedInput)
if selectedInput == "SLI2B":
print("Setting default volume level")
if(self.VolumeLevel() > self.DefaultMusicVolume):
self.setVolumeLevel(self.DefaultMusicVolume) #On start set the volume level to default
def setVolumeLevel(self, volumeLevel):
if self.PowerState() == 1:
with eISCP(self.Host) as receiver:
response = receiver.raw("MVL%s" % str(hex(volumeLevel))[2:])
print("Response from setting volume level is: %s" % str(response))
print(hex(volumeLevel))
print("Volume level set to %s" % volumeLevel)
def VolumeLevel(self):
if self.PowerState() == 1:
with eISCP(self.Host) as receiver:
response = receiver.raw("MVLQSTN")
decimalVolume = int(response[3:], 16)
return decimalVolume
else:
return 0
def rotateVolume(self, volumeLevel):
isNegative = False
if volumeLevel < 0:
isNegative = True
volume = abs(volumeLevel/self.volumeDivider)
currentVolume = self.VolumeLevel()
print("Current Volume is %s" % currentVolume)
if isNegative:
self.setVolumeLevel(currentVolume - volume)
else:
self.setVolumeLevel(currentVolume + volume)
def discoverReceiver(self):
receivers = eISCP.discover(timeout=1)
receiver = receivers[0]
return receiver