Fix mysensors connection task blocking setup (#15938)

* Fix mysensors connection task blocking setup

* Schedule the connection task without having the core track the task
  to avoid blocking setup.
* Cancel the connection task, if not cancelled already, when
  home assistant stops.

* Use done instead of cancelled
pull/16027/head
Martin Hjelmare 2018-08-16 14:19:42 +02:00 committed by Paulus Schoutsen
parent 11eb29f520
commit d540a084dd
1 changed files with 5 additions and 1 deletions

View File

@ -186,12 +186,16 @@ def _discover_mysensors_platform(hass, platform, new_devices):
async def _gw_start(hass, gateway):
"""Start the gateway."""
# Don't use hass.async_create_task to avoid holding up setup indefinitely.
connect_task = hass.loop.create_task(gateway.start())
@callback
def gw_stop(event):
"""Trigger to stop the gateway."""
hass.async_add_job(gateway.stop())
if not connect_task.done():
connect_task.cancel()
await gateway.start()
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, gw_stop)
if gateway.device == 'mqtt':
# Gatways connected via mqtt doesn't send gateway ready message.