From 43676fcaf402994f18735bbd71a62fca54567594 Mon Sep 17 00:00:00 2001 From: ehendrix23 Date: Tue, 27 Nov 2018 12:41:25 -0700 Subject: [PATCH] Moved stop method and registering STOP_EVENT outside of init (#18582) * Moved stop method and registering outside of init Moved the cleanup to a seperate method and perform registering for the event in setup. * Removed use of global variable Removed use of global variable. * Removed API_SESSIONS Removed unused declaration API_SESSIONS. --- homeassistant/components/august.py | 33 ++++++++++++++---------------- 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/homeassistant/components/august.py b/homeassistant/components/august.py index 1f12abd3d4e..2073f680e00 100644 --- a/homeassistant/components/august.py +++ b/homeassistant/components/august.py @@ -11,7 +11,6 @@ import voluptuous as vol from requests import RequestException import homeassistant.helpers.config_validation as cv -from homeassistant.core import callback from homeassistant.const import ( CONF_PASSWORD, CONF_USERNAME, CONF_TIMEOUT, EVENT_HOMEASSISTANT_STOP) from homeassistant.helpers import discovery @@ -141,11 +140,11 @@ def setup(hass, config): from requests import Session conf = config[DOMAIN] + api_http_session = None try: api_http_session = Session() except RequestException as ex: _LOGGER.warning("Creating HTTP session failed with: %s", str(ex)) - api_http_session = None api = Api(timeout=conf.get(CONF_TIMEOUT), http_session=api_http_session) @@ -157,6 +156,20 @@ def setup(hass, config): install_id=conf.get(CONF_INSTALL_ID), access_token_cache_file=hass.config.path(AUGUST_CONFIG_FILE)) + def close_http_session(event): + """Close API sessions used to connect to August.""" + _LOGGER.debug("Closing August HTTP sessions") + if api_http_session: + try: + api_http_session.close() + except RequestException: + pass + + _LOGGER.debug("August HTTP session closed.") + + hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, close_http_session) + _LOGGER.debug("Registered for HASS stop event") + return setup_august(hass, config, api, authenticator) @@ -178,22 +191,6 @@ class AugustData: self._door_state_by_id = {} self._activities_by_id = {} - @callback - def august_api_stop(event): - """Close the API HTTP session.""" - _LOGGER.debug("Closing August HTTP session") - - try: - self._api.http_session.close() - self._api.http_session = None - except RequestException: - pass - _LOGGER.debug("August HTTP session closed.") - - self._hass.bus.listen_once( - EVENT_HOMEASSISTANT_STOP, august_api_stop) - _LOGGER.debug("Registered for HASS stop event") - @property def house_ids(self): """Return a list of house_ids."""