-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_discovery.py
147 lines (135 loc) · 6.82 KB
/
test_discovery.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
#!/usr/bin/env python
# encoding: utf-8
import types
import unittest
import discovery
import sonos
import upnp
import testhelpers
# Output from my Sonos system, with UUIDs sanitized.
ACTUAL_TOPOLOGY_XML = (
'<ZoneGroups><ZoneGroup Coordinator="RINCON_5CAA000000000000'
'1" ID="RINCON_5CAA0000000000001:13"><ZoneGroupMember'
' UUID="RINCON_5CAA0000000000001" Location="http://192.168.'
'1.100:1400/xml/device_description.xml" ZoneName="Michael&ap'
'os;s Room" Icon="x-rincon-roomicon:living" Configuration=&'
'quot;1" SoftwareVersion="34.7-33240-Wilco_Release" MinComp'
'atibleVersion="33.0-00000" LegacyCompatibleVersion="25.0-0'
'0000" BootSeq="13" WirelessMode="1" WirelessLeaf'
'Only="0" HasConfiguredSSID="1" ChannelFreq="2437'
'" BehindWifiExtender="0" WifiEnabled="1" Orienta'
'tion="0" RoomCalibrationState="4" SecureRegState=&quo'
't;3"/></ZoneGroup><ZoneGroup Coordinator="RINCON_B8'
'E90000000000002" ID="RINCON_B8E90000000000002:67"><Z'
'oneGroupMember UUID="RINCON_B8E90000000000002" Location="h'
'ttp://192.168.1.67:1400/xml/device_description.xml" ZoneName="L'
'iving Room" Icon="x-rincon-roomicon:living" Configuration='
'"1" SoftwareVersion="34.7-33240-Wilco_Release" MinCom'
'patibleVersion="33.0-00000" LegacyCompatibleVersion="25.0-'
'00000" BootSeq="92" WirelessMode="1" WirelessLea'
'fOnly="0" HasConfiguredSSID="1" ChannelFreq="243'
'7" BehindWifiExtender="0" WifiEnabled="1" Orient'
'ation="0" RoomCalibrationState="4" SecureRegState=&qu'
'ot;3"/></ZoneGroup><ZoneGroup Coordinator="RINCON_B'
'8E90000000000003" ID="RINCON_B8E90000000000003:49"><'
'ZoneGroupMember UUID="RINCON_B8E90000000000003" Location="'
'http://192.168.1.69:1400/xml/device_description.xml" ZoneName="'
'Dining Room" Icon="x-rincon-roomicon:dining" Configuration'
'="1" SoftwareVersion="34.7-33240-Wilco_Release" MinCo'
'mpatibleVersion="33.0-00000" LegacyCompatibleVersion="25.0'
'-00000" BootSeq="113" WirelessMode="1" WirelessL'
'eafOnly="0" HasConfiguredSSID="1" ChannelFreq="2'
'437" BehindWifiExtender="0" WifiEnabled="1" Orie'
'ntation="0" RoomCalibrationState="4" SecureRegState=&'
'quot;3"/></ZoneGroup></ZoneGroups>'
)
ACTUAL_TOPOLOGY_PARSED = [
{
'coordinator_uuid': 'RINCON_5CAA0000000000001',
'players': {
'RINCON_5CAA0000000000001': {
'ip': '192.168.1.100',
'name': 'Michael\'s Room',
'uuid': 'RINCON_5CAA0000000000001'
}
}
},
{
'coordinator_uuid': 'RINCON_B8E90000000000002',
'players': {
'RINCON_B8E90000000000002': {
'ip': '192.168.1.67',
'name': 'Living Room',
'uuid': 'RINCON_B8E90000000000002'
}
}
},
{
'coordinator_uuid': 'RINCON_B8E90000000000003',
'players': {
'RINCON_B8E90000000000003': {
'ip': '192.168.1.69',
'name': 'Dining Room',
'uuid': 'RINCON_B8E90000000000003'
}
}
}
]
class ZoneGroupTopologyTests(unittest.TestCase):
def test_location_to_ip(self):
"""Given a Location from a Zone Group Topology we can get the IP of the player."""
location = 'http://192.168.1.69:1400/xml/device_description.xml'
ip = discovery._zone_group_topology_location_to_ip(location)
self.assertEqual(ip, '192.168.1.69')
def test_zone_group_topology(self):
"""Parsing the Zone Group State from my local network gives the right output."""
xml = upnp._unescape(ACTUAL_TOPOLOGY_XML)
resp_arguments = {'ZoneGroupState': xml}
with testhelpers.mock(upnp, 'send_command', resp_arguments):
topology = discovery.query_zone_group_topology('0.0.0.0')
self.assertEqual(topology, ACTUAL_TOPOLOGY_PARSED)
def test_discover_actual_topology(self):
"""Given a topology, sonos.discover() should return Sonos instances for
each speaker in the network."""
with testhelpers.mock(discovery, '_discover_ip', '0.0.0.0'):
with testhelpers.mock(discovery, 'query_zone_group_topology', ACTUAL_TOPOLOGY_PARSED):
speakers = discovery.discover()
self.assertIsInstance(speakers, types.GeneratorType)
speakers = list(speakers)
expected_speakers = [
sonos.Sonos('RINCON_5CAA0000000000001', '192.168.1.100', 'Michael\'s Room'),
sonos.Sonos('RINCON_B8E90000000000002', '192.168.1.67', 'Living Room'),
sonos.Sonos('RINCON_B8E90000000000003', '192.168.1.69', 'Dining Room'),
]
for expected in expected_speakers:
self.assertIn(expected, speakers)
self.assertEqual(len(speakers), len(expected_speakers))
def test_discover_fake_topology_with_two_speakers_in_group(self):
"""sonos.discover() should return one Sonos instance per group, with the
other members of the group available from that instance."""
FAKE_TOPOLOGY_PARSED = [{
'coordinator_uuid': 'RINCON_5CAA0000000000001',
'players': {
'RINCON_5CAA0000000000001': {
'ip': '192.168.1.100',
'name': 'Michael\'s Room',
'uuid': 'RINCON_5CAA0000000000001'
},
'RINCON_B8E90000000000002': {
'ip': '192.168.1.67',
'name': 'Living Room',
'uuid': 'RINCON_B8E90000000000002'
}
}
}]
with testhelpers.mock(discovery, '_discover_ip', '0.0.0.0'):
with testhelpers.mock(discovery, 'query_zone_group_topology', FAKE_TOPOLOGY_PARSED):
speakers = list(discovery.discover())
self.assertEqual(speakers, [
sonos.Sonos('RINCON_5CAA0000000000001', '192.168.1.100', 'Michael\'s Room'),
])
self.assertEqual(speakers[0].other_players, [
sonos.Sonos('RINCON_B8E90000000000002', '192.168.1.67', 'Living Room'),
])
if __name__ == '__main__':
unittest.main()