From 5aa6b1d9cbe76bb2ec6ca41b02bb9b4752602f01 Mon Sep 17 00:00:00 2001 From: Martin Hjelmare Date: Sun, 12 Aug 2018 15:38:10 +0200 Subject: [PATCH 1/2] 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. --- homeassistant/components/mysensors/gateway.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/mysensors/gateway.py b/homeassistant/components/mysensors/gateway.py index 8c80604d1881ef..2f9cf539700d44 100644 --- a/homeassistant/components/mysensors/gateway.py +++ b/homeassistant/components/mysensors/gateway.py @@ -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.cancelled(): + 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. From eecb6487df56ce9345b0e569ff415eb058347dbe Mon Sep 17 00:00:00 2001 From: Martin Hjelmare Date: Thu, 16 Aug 2018 14:03:48 +0200 Subject: [PATCH 2/2] Use done instead of cancelled --- homeassistant/components/mysensors/gateway.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/mysensors/gateway.py b/homeassistant/components/mysensors/gateway.py index 2f9cf539700d44..88725e67940d68 100644 --- a/homeassistant/components/mysensors/gateway.py +++ b/homeassistant/components/mysensors/gateway.py @@ -193,7 +193,7 @@ async def _gw_start(hass, gateway): def gw_stop(event): """Trigger to stop the gateway.""" hass.async_add_job(gateway.stop()) - if not connect_task.cancelled(): + if not connect_task.done(): connect_task.cancel() hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, gw_stop)