Skip to content

Commit

Permalink
Fix zeroconf thread in asyncio loop #503
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed May 3, 2024
1 parent 186dfcb commit 2b8790c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
6 changes: 4 additions & 2 deletions custom_components/yandex_station/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,10 @@ async def found_local_speaker(info: dict):

zeroconf = await utils.get_zeroconf_singleton(hass)

listener = YandexIOListener(hass.loop)
listener.start(found_local_speaker, zeroconf)
listener = YandexIOListener(
lambda info: hass.create_task(found_local_speaker(info))
)
listener.start(zeroconf)

hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, listener.stop)

Expand Down
12 changes: 5 additions & 7 deletions custom_components/yandex_station/core/yandex_glagol.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,14 +254,13 @@ def debug_msg(self, data: dict):


class YandexIOListener:
add_handlerer = None
add_handler = None
browser = None

def __init__(self, loop):
self.loop = loop
def __init__(self, add_handler: Callable):
self.add_handler = add_handler

def start(self, handlerer: Callable, zeroconf: Zeroconf):
self.add_handlerer = handlerer
def start(self, zeroconf: Zeroconf):
self.browser = ServiceBrowser(
zeroconf, "_yandexio._tcp.local.", handlers=[self._zeroconf_handler]
)
Expand All @@ -287,15 +286,14 @@ def _zeroconf_handler(
for k, v in info.properties.items()
}

coro = self.add_handlerer(
self.add_handler(
{
"device_id": properties["deviceId"],
"platform": properties["platform"],
"host": str(ipaddress.ip_address(info.addresses[0])),
"port": info.port,
}
)
self.loop.create_task(coro)

except Exception as e:
_LOGGER.debug("Can't get zeroconf info", exc_info=e)

0 comments on commit 2b8790c

Please sign in to comment.