diff --git a/snap.py b/snap.py index 4f5970935478df83cb6feeb01e2f783dcdcc0299..d5a3d057da70636caf6e881e50b79499d64d41f3 100644 --- a/snap.py +++ b/snap.py @@ -1,6 +1,5 @@ from zeroconf import ServiceBrowser, Zeroconf import snapcast.control -import asyncio import ipaddress import netifaces @@ -17,7 +16,7 @@ class Scanner: self.services.append(info) -def get_snapcast_server(): +def get_snapcast_server(loop): '''Scan for services then find snapcast server.''' zeroconf = Zeroconf() listener = Scanner() @@ -26,8 +25,6 @@ def get_snapcast_server(): while listener.services == []: pass - # Snapcast library only supports asynchronous so need to wrap in async loop - loop = asyncio.get_event_loop() server_ip_bytes = listener.services[0].address server_ip = str(ipaddress.ip_address(server_ip_bytes)) # Snapserver.__init__ does not work in snapcast==2.0.8 @@ -37,7 +34,6 @@ def get_snapcast_server(): create_server_coroutine = snapcast.control.create_server( loop, server_ip, reconnect=True) server = loop.run_until_complete(create_server_coroutine) - loop.close() return server @@ -66,9 +62,11 @@ def get_mac_addresses(): return mac_addresses -def get_snapcast_client(server): +def get_snapcast_client(loop): '''Scan through list of clients to find correct one.''' + server = get_snapcast_server(loop) + mac_addresses = get_mac_addresses() for client in server.clients: - print(client.name) - print(client.identifier) - # TODO Scroll through list of clients and find right one + # print(client.identifier) + if client.identifier in mac_addresses: + return client