Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
assert sock.sendto.call_count == 9
# select called with the relevant timeout
select.select.assert_called_with(
[sock, sock, sock], [], [], min(TIMEOUT, 0.1))
# SoCo should be created with the IP address received
config.SOCO_CLASS.assert_called_with(IP_ADDR)
# Now test include_visible parameter. include_invisible=True should
# result in calling SoCo.all_zones etc
# Reset gethostbyname, to always return the same value
monkeypatch.setattr('socket.gethostbyname',
Mock(return_value='192.168.1.15'))
config.SOCO_CLASS.return_value = Mock(
all_zones='ALL', visible_zones='VISIBLE')
assert discover(include_invisible=True) == 'ALL'
assert discover(include_invisible=False) == 'VISIBLE'
# if select does not return within timeout SoCo should not be called
# at all
# simulate no data being returned within timeout
select.select.return_value = (0, 1, 1)
discover(timeout=1)
# Check no SoCo instance created
config.SOCO_CLASS.assert_not_called
print('Looking for music files')
for path, dirs, files in os.walk('.'):
for file_ in files:
if not os.path.splitext(file_)[1].startswith('.py'):
music_files.append(os.path.relpath(os.path.join(path, file_)))
print('Found:', music_files[-1])
random_file = choice(music_files)
# urlencode all the path parts (but not the /'s)
random_file = os.path.join(
*[quote(part) for part in os.path.split(random_file)]
)
print('\nPlaying random file:', random_file)
netpath = 'http://{}:{}/{}'.format(machine_ip, port, random_file)
for zone in soco.discover():
if zone.player_name == zone_name:
break
number_in_queue = zone.add_uri_to_queue(netpath)
zone.play_from_queue(number_in_queue)
def onInit(self):
# Discover Sonos players
discovery = discover()
if not discovery:
raise Exception('No Sonos players discovered')
self.zones = list(discovery)
# Set default player
self.defaultZone = None
if self.zones:
defaultZone = self.config.get('defaultzone')
if defaultZone:
self.defaultZone = self.getZone(defaultZone)
else:
self.defaultZone = self.zones[0]
self.interval = None
self.subscribers = []
self.registerForEvents()
' not needed).')
args = parser.parse_args()
if args.playMode:
playMode = args.playMode
else:
playMode = 'SRf'
self.verbose = args.verbose
if self.verbose:
print('SoCo version: ' + soco.__version__)
# what speaker are we working with?
speaker = None
if args.interface:
try:
zones = soco.discover(interface_addr=args.interface)
except:
print('Error: invalid interface address: ' + args.interface)
if self.verbose:
traceback.print_exc()
exit(-2)
else:
zones = soco.discover()
if not zones:
print('Error: discover returned no speakers.')
if args.interface:
print(' Either the interface is down or you have no Sonos'
' speakers on the network.')
else:
print(' Either the interface is down, you have no Sonos'
' speakers on the network, or you may\n need to use -I'
' option.')
return True
return False
players = None
hosts = config.get(CONF_HOSTS, None)
if hosts:
# Support retro compatibility with comma separated list of hosts
# from config
hosts = hosts[0] if len(hosts) == 1 else hosts
hosts = hosts.split(',') if isinstance(hosts, str) else hosts
players = []
for host in hosts:
players.append(soco.SoCo(socket.gethostbyname(host)))
if not players:
players = soco.discover(interface_addr=config.get(CONF_INTERFACE_ADDR))
if not players:
_LOGGER.warning('No Sonos speakers found.')
return False
DEVICES = [SonosDevice(hass, p) for p in players]
add_devices(DEVICES, True)
register_services(hass)
_LOGGER.info('Added %s Sonos speakers', len(players))
return True
def find_random_player():
"""Searches the network for Sonos zones and picks one randomly"""
zones = soco.discover()
if zones:
# picking a random player
player = next(iter(zones))
return player
return None
print('will play: {} on all coordinators'.format(alert_uri))
for zone in zones:
if zone.is_coordinator:
zone.play_uri(uri=alert_uri, title='Sonos Alert')
# wait for alert_duration
time.sleep(alert_duration)
# restore each zone to previous state
for zone in zones:
print('restoring {}'.format(zone.player_name))
zone.snap.restore(fade=fade_back)
if __name__ == '__main__':
all_zones = soco.discover()
# alert uri to send to sonos - this uri must be available to Sonos
alert_sound = 'https://ia800503.us.archive.org/8/items/futuresoundfx-98/futuresoundfx-96.mp3'
play_alert(all_zones, alert_sound, alert_volume=30, alert_duration=3,
fade_back=False)
def get_zone_by_name(zoneName):
'''Returns a SoCo instance if found, or None if not found'''
zone_list = list(soco.discover())
for zone in zone_list:
if zone.player_name == zoneName:
return zone
return None