forked from Einstein42/sonos-polyglot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sonos_types.py
executable file
·149 lines (123 loc) · 4.94 KB
/
sonos_types.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
from polyglot.nodeserver_api import Node
import soco
import requests
class SonosControl(Node):
def __init__(self, *args, **kwargs):
super(SonosControl, self).__init__(*args, **kwargs)
def _discover(self, **kwargs):
manifest = self.parent.config.get('manifest', {})
self.logger.info("Received Discover command from ISY.")
speakers = soco.discover()
self.logger.info('Found speakers: %s', speakers)
if speakers:
for speaker in speakers:
# ISY only allows 14 character limit on nodes, have to strip the RINCON and use the first 14 chars of the UID
address = speaker.uid[8:22].lower()
lnode = self.parent.get_node(address)
if not lnode:
self.logger.info("New Speaker Found.")
self.parent.speakers.append(SonosSpeaker(self.parent, self.parent.get_node('sonoscontrol'), address, speaker.player_name, speaker.ip_address, manifest))
self.parent.update_config()
else:
self.logger.error('No speakers found')
return True
def query(self, **kwargs):
self.parent.report_drivers()
return True
_drivers = {}
_commands = {'DISCOVER': _discover}
node_def_id = 'sonoscontrol'
class SonosSpeaker(Node):
def __init__(self, parent, primary, address, player_name, ip_address, manifest=None):
self.parent = parent
self.logger = self.parent.poly.logger
self.ip = ip_address
self.zone = soco.SoCo(self.ip)
self.player_name = player_name
self.address = address
self.logger.info("Adding new Sonos Speaker: " + self.player_name + "@" + self.ip + " Current Volume: " + str(self.zone.volume))
super(SonosSpeaker, self).__init__(parent, address, "Sonos " + player_name, primary, manifest)
self.logger.info("Getting current speaker volume, bass and treble...")
self.update_info()
def _update_node(self):
self.logger.info("Updating ISY information with IP address and current volume.")
ip_addr = self.ip.split('.')
for ind, driver in enumerate(('GV1', 'GV2', 'GV3', 'GV4')):
self.set_driver(driver, ip_addr[ind])
self.update_info()
def update_info(self):
try:
self.set_driver('ST', self.zone.volume)
self.set_driver('GV1', self.zone.bass)
self.set_driver('GV2', self.zone.treble)
except requests.exceptions.ConnectionError as e:
self.logger.error('Connection error to ISY: %s', e)
def query(self, **kwargs):
self.update_info()
self.report_driver()
return True
def _play(self, **kwargs):
self.zone.play()
return True
def _stop(self, **kwargs):
self.zone.stop()
return True
def _pause(self, **kwargs):
self.zone.pause()
return True
def _next(self, **kwargs):
self.zone.next()
return True
def _previous(self, **kwargs):
try:
self.zone.previous()
except:
self.logger.info("Error in command 'previous'. This typically means that the station or mode you are in doesn't support it.")
return True
def _partymode(self, **kwargs):
self.zone.partymode()
return True
def _mute(self, **kwargs):
if self.zone.mute:
self.zone.mute = False
else:
self.zone.mute = True
return True
def _volume(self, **kwargs):
val = kwargs.get('value')
if val:
self.zone.volume = int(val)
self.set_driver('ST', int(val), 56)
return True
def _bass(self, **kwargs):
val = kwargs.get('value')
if val > -11 or val < 11:
self.zone.bass = val
self.set_driver('GV1', int(val), 56)
return True
def _treble(self, **kwargs):
val = kwargs.get('value')
if val > -11 or val < 11:
self.zone.treble = val
self.set_driver('GV2', int(val), 56)
return True
def query(self, **kwargs):
self.update_info()
self.report_driver()
return True
_drivers = {'GV1': [0, 56, int], 'GV2': [0, 56, int],
'GV3': [0, 56, int], 'GV4': [0, 56, int],
'ST': [0, 51, int]}
_commands = {'PLAY': _play,
'STOP': _stop,
'DON': _play,
'DOF': _pause,
'PAUSE': _pause,
'NEXT': _next,
'PREVIOUS': _previous,
'PARTYMODE': _partymode,
'MUTE': _mute,
'BASS': _bass,
'TREBLE': _treble,
'VOLUME': _volume}
node_def_id = 'sonosspeaker'