home-assistant-libs / pychromecast

Library for Python 3 to communicate with the Google Chromecast.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Assesrtion error hit in zeroconf

soreau opened this issue · comments

I hit this assertion from my program which causes an infinite rapid connection loop. This is the repeated backtrace:

[name(192.168.1.150):8009] Error communicating with socket, resetting connection
[name(192.168.1.150):8009] Unhandled exception in worker thread, attempting reconnect
Traceback (most recent call last):
  File "/home/scott/.local/lib/python3.8/site-packages/pychromecast/socket_client.py", line 538, in run
    if self.run_once(timeout=POLL_TIME_BLOCKING) == 1:
  File "/home/scott/.local/lib/python3.8/site-packages/pychromecast/socket_client.py", line 561, in run_once
    if not self._check_connection():
  File "/home/scott/.local/lib/python3.8/site-packages/pychromecast/socket_client.py", line 690, in _check_connection
    self.initialize_connection()
  File "/home/scott/.local/lib/python3.8/site-packages/pychromecast/socket_client.py", line 295, in initialize_connection
    host, port, service_info = get_host_from_service(
  File "/home/scott/.local/lib/python3.8/site-packages/pychromecast/dial.py", line 38, in get_host_from_service
    service_info = zconf.get_service_info("_googlecast._tcp.local.", service.data)
  File "/home/scott/.local/lib/python3.8/site-packages/zeroconf/_core.py", line 540, in get_service_info
    if info.request(self, timeout, question_type):
  File "/home/scott/.local/lib/python3.8/site-packages/zeroconf/_services/info.py", line 459, in request
    assert zc.loop is not None and zc.loop.is_running()
AssertionError

This happens when I run catt-qt here, and subsequently power cycle a chromecast, but strangely does not happen when I try to add a connection listener to this example. This patch fixes it. This is with Zeroconf version: 0.38.7

This is a bug in catt-qt. You need to have a running loop to resolve a service and it needs to run inside the event loop.

Thanks for the quick reply. Can you elaborate about how to do this? By event loop, do you mean Qt's event loop or something else? Any pointers to the code would be appreciated.

if I use

async def myWork():
    chromecasts, browser = pychromecast.get_listed_chromecasts(friendly_names=[name])
    chromecast = chromecast[0]

loop = asyncio.get_event_loop()
loop.run_until_complete(myWork())

it waits the full timeout of 3 seconds and says index out of range for chromecasts[0].

Turns out I just needed to wrap the relevant setup code with these calls to run in the main event loop.

loop = QEventLoop()
# setup code
loop.exec()

Thanks for your help.