From 4218efddcda217dd8c28f7ea463a52f96bd19196 Mon Sep 17 00:00:00 2001 From: Lewis Juggins Date: Fri, 5 Oct 2018 08:59:34 +0100 Subject: [PATCH] Cleanly stop tradfri on shutdown (#17114) * Tradfri shutdown fix * Bump version * Bump version * Fix * Derp * Remove unnecessary shutdown event --- homeassistant/components/tradfri/__init__.py | 10 +++++++++- homeassistant/components/tradfri/config_flow.py | 3 +++ requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/tradfri/__init__.py b/homeassistant/components/tradfri/__init__.py index 51195d0a168..e9ee8d0898b 100644 --- a/homeassistant/components/tradfri/__init__.py +++ b/homeassistant/components/tradfri/__init__.py @@ -9,6 +9,7 @@ import logging import voluptuous as vol from homeassistant import config_entries +from homeassistant.const import EVENT_HOMEASSISTANT_STOP import homeassistant.helpers.config_validation as cv from homeassistant.util.json import load_json @@ -17,7 +18,7 @@ from .const import ( from . import config_flow # noqa pylint_disable=unused-import -REQUIREMENTS = ['pytradfri[async]==5.6.0'] +REQUIREMENTS = ['pytradfri[async]==6.0.1'] DOMAIN = 'tradfri' CONFIG_FILE = '.tradfri_psk.conf' @@ -87,6 +88,13 @@ async def async_setup_entry(hass, entry): psk=entry.data[CONF_KEY], loop=hass.loop ) + + async def on_hass_stop(event): + """Close connection when hass stops.""" + await factory.shutdown() + + hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, on_hass_stop) + api = factory.request gateway = Gateway() diff --git a/homeassistant/components/tradfri/config_flow.py b/homeassistant/components/tradfri/config_flow.py index f3c52f42a9a..a3452e50c4d 100644 --- a/homeassistant/components/tradfri/config_flow.py +++ b/homeassistant/components/tradfri/config_flow.py @@ -166,9 +166,12 @@ async def get_gateway_info(hass, host, identity, key): psk=key, loop=hass.loop ) + api = factory.request gateway = Gateway() gateway_info_result = await api(gateway.get_gateway_info()) + + await factory.shutdown() except (OSError, RequestError): # We're also catching OSError as PyTradfri doesn't catch that one yet # Upstream PR: https://github.com/ggravlingen/pytradfri/pull/189 diff --git a/requirements_all.txt b/requirements_all.txt index fca9e771b52..c634c194c7e 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1217,7 +1217,7 @@ pytouchline==0.7 pytrackr==0.0.5 # homeassistant.components.tradfri -pytradfri[async]==5.6.0 +pytradfri[async]==6.0.1 # homeassistant.components.sensor.trafikverket_weatherstation pytrafikverket==0.1.5.8 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 10b8307d337..4f27664e7df 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -188,7 +188,7 @@ python-nest==4.0.3 pythonwhois==2.4.3 # homeassistant.components.tradfri -pytradfri[async]==5.6.0 +pytradfri[async]==6.0.1 # homeassistant.components.device_tracker.unifi pyunifi==2.13