Introducing hass.data (#4121)

* Hello hass.data

* Migrate setup_component to hass.data
This commit is contained in:
Paulus Schoutsen 2016-10-29 14:51:17 -07:00 committed by GitHub
parent 3cc4fdaa34
commit 4163e55dbd
5 changed files with 41 additions and 31 deletions

View File

@ -27,7 +27,6 @@ from homeassistant.helpers import (
event_decorators, service, config_per_platform, extract_domain_configs) event_decorators, service, config_per_platform, extract_domain_configs)
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
_CURRENT_SETUP = []
ATTR_COMPONENT = 'component' ATTR_COMPONENT = 'component'
@ -102,30 +101,37 @@ def _async_setup_component(hass: core.HomeAssistant,
""" """
# pylint: disable=too-many-return-statements,too-many-branches # pylint: disable=too-many-return-statements,too-many-branches
# pylint: disable=too-many-statements # pylint: disable=too-many-statements
if not hasattr(hass, 'setup_lock'):
hass.setup_lock = asyncio.Lock(loop=hass.loop)
if domain in hass.config.components: if domain in hass.config.components:
return True return True
did_lock = False setup_lock = hass.data.get('setup_lock')
if not hass.setup_lock.locked(): if setup_lock is None:
yield from hass.setup_lock.acquire() setup_lock = hass.data['setup_lock'] = asyncio.Lock(loop=hass.loop)
did_lock = True
setup_progress = hass.data.get('setup_progress')
if setup_progress is None:
setup_progress = hass.data['setup_progress'] = []
if domain in setup_progress:
_LOGGER.error('Attempt made to setup %s during setup of %s',
domain, domain)
return False
try: try:
if domain in _CURRENT_SETUP: # Used to indicate to discovery that a setup is ongoing and allow it
_LOGGER.error('Attempt made to setup %s during setup of %s', # to wait till it is done.
domain, domain) did_lock = False
return False if not setup_lock.locked():
yield from setup_lock.acquire()
did_lock = True
setup_progress.append(domain)
config = yield from async_prepare_setup_component(hass, config, domain) config = yield from async_prepare_setup_component(hass, config, domain)
if config is None: if config is None:
return False return False
component = loader.get_component(domain) component = loader.get_component(domain)
_CURRENT_SETUP.append(domain)
try: try:
if hasattr(component, 'async_setup'): if hasattr(component, 'async_setup'):
@ -133,20 +139,18 @@ def _async_setup_component(hass: core.HomeAssistant,
else: else:
result = yield from hass.loop.run_in_executor( result = yield from hass.loop.run_in_executor(
None, component.setup, hass, config) None, component.setup, hass, config)
if result is False:
_LOGGER.error('component %s failed to initialize', domain)
return False
elif result is not True:
_LOGGER.error('component %s did not return boolean if setup '
'was successful. Disabling component.', domain)
loader.set_component(domain, None)
return False
except Exception: # pylint: disable=broad-except except Exception: # pylint: disable=broad-except
_LOGGER.exception('Error during setup of component %s', domain) _LOGGER.exception('Error during setup of component %s', domain)
return False return False
finally:
_CURRENT_SETUP.remove(domain) if result is False:
_LOGGER.error('component %s failed to initialize', domain)
return False
elif result is not True:
_LOGGER.error('component %s did not return boolean if setup '
'was successful. Disabling component.', domain)
loader.set_component(domain, None)
return False
hass.config.components.append(component.DOMAIN) hass.config.components.append(component.DOMAIN)
@ -162,8 +166,9 @@ def _async_setup_component(hass: core.HomeAssistant,
return True return True
finally: finally:
setup_progress.remove(domain)
if did_lock: if did_lock:
hass.setup_lock.release() setup_lock.release()
def prepare_setup_component(hass: core.HomeAssistant, config: dict, def prepare_setup_component(hass: core.HomeAssistant, config: dict,

View File

@ -141,6 +141,8 @@ class HomeAssistant(object):
self.services = ServiceRegistry(self.bus, self.add_job, self.loop) self.services = ServiceRegistry(self.bus, self.add_job, self.loop)
self.states = StateMachine(self.bus, self.loop) self.states = StateMachine(self.bus, self.loop)
self.config = Config() # type: Config self.config = Config() # type: Config
# This is a dictionary that any component can store any data on.
self.data = {}
self.state = CoreState.not_running self.state = CoreState.not_running
self.exit_code = None self.exit_code = None
self._websession = None self._websession = None

View File

@ -113,9 +113,10 @@ def async_load_platform(hass, component, platform, discovered=None,
This method is a coroutine. This method is a coroutine.
""" """
did_lock = False did_lock = False
if hasattr(hass, 'setup_lock') and hass.setup_lock.locked(): setup_lock = hass.data.get('setup_lock')
if setup_lock and setup_lock.locked():
did_lock = True did_lock = True
yield from hass.setup_lock.acquire() yield from setup_lock.acquire()
try: try:
# No need to fire event if we could not setup component # No need to fire event if we could not setup component
@ -123,7 +124,7 @@ def async_load_platform(hass, component, platform, discovered=None,
hass, component, hass_config) hass, component, hass_config)
finally: finally:
if did_lock: if did_lock:
hass.setup_lock.release() setup_lock.release()
if not res: if not res:
return return

View File

@ -134,9 +134,11 @@ class HomeAssistant(ha.HomeAssistant):
self.services = ha.ServiceRegistry(self.bus, self.add_job, self.loop) self.services = ha.ServiceRegistry(self.bus, self.add_job, self.loop)
self.states = StateMachine(self.bus, self.loop, self.remote_api) self.states = StateMachine(self.bus, self.loop, self.remote_api)
self.config = ha.Config() self.config = ha.Config()
self._websession = None # This is a dictionary that any component can store any data on.
self.data = {}
self.state = ha.CoreState.not_running self.state = ha.CoreState.not_running
self.exit_code = None
self._websession = None
self.config.api = local_api self.config.api = local_api
def start(self): def start(self):

View File

@ -138,7 +138,7 @@ class TestHelpersDiscovery:
# We wait for the setup_lock to finish # We wait for the setup_lock to finish
run_coroutine_threadsafe( run_coroutine_threadsafe(
self.hass.setup_lock.acquire(), self.hass.loop).result() self.hass.data['setup_lock'].acquire(), self.hass.loop).result()
self.hass.block_till_done() self.hass.block_till_done()