Bootstrap / Component setup async (#6264)

* Bootstrap / Entiy setup async

* Cleanup add_job stuff / return task/future object

* Address paulus comments / part 1

* fix install pip

* Cleanup bootstrap / move config stuff to config.py

* Make demo async

* Further bootstrap improvement

* Address Martin's comments

* Fix initial tests

* Fix final tests

* Fix bug with prepare loader

* Remove no longer needed things

* Log error when invalid config

* More cleanup

* Cleanups platform events & fix lint

* Use a non blocking add_entities callback for platform

* Fix Autoamtion is setup befor entity is ready

* Better automation fix

* Address paulus comments

* Typo

* fix lint

* rename functions

* fix tests

* fix test

* change exceptions

* fix spell
This commit is contained in:
Pascal Vizeli 2017-03-01 05:33:19 +01:00 committed by Paulus Schoutsen
parent 383b0914b3
commit 41f558b181
109 changed files with 764 additions and 848 deletions

View file

@ -12,8 +12,8 @@ from contextlib import contextmanager
from aiohttp import web
from homeassistant import core as ha, loader
from homeassistant.bootstrap import (
setup_component, async_prepare_setup_component)
from homeassistant.bootstrap import setup_component, DATA_SETUP
from homeassistant.config import async_process_component_config
from homeassistant.helpers.dispatcher import async_dispatcher_send
from homeassistant.helpers.entity import ToggleEntity
from homeassistant.helpers.restore_state import DATA_RESTORE_CACHE
@ -93,13 +93,16 @@ def async_test_home_assistant(loop):
hass = ha.HomeAssistant(loop)
orig_async_add_job = hass.async_add_job
def async_add_job(target, *args):
"""Add a magic mock."""
if isinstance(target, MagicMock):
return
hass._async_add_job_tracking(target, *args)
return orig_async_add_job(target, *args)
hass.async_add_job = async_add_job
hass.async_track_tasks()
hass.config.location_name = 'test home'
hass.config.config_dir = get_test_config_dir()
@ -230,7 +233,7 @@ def mock_state_change_event(hass, new_state, old_state=None):
def mock_http_component(hass, api_password=None):
"""Mock the HTTP component."""
hass.http = MagicMock(api_password=api_password)
hass.config.components.add('http')
mock_component(hass, 'http')
hass.http.views = {}
def mock_register_view(view):
@ -268,6 +271,19 @@ def mock_mqtt_component(hass):
return mock_mqtt
def mock_component(hass, component):
"""Mock a component is setup."""
setup_tasks = hass.data.get(DATA_SETUP)
if setup_tasks is None:
setup_tasks = hass.data[DATA_SETUP] = {}
if component not in setup_tasks:
AssertionError("Component {} is already setup".format(component))
hass.config.components.add(component)
setup_tasks[component] = asyncio.Task(mock_coro(True), loop=hass.loop)
class MockModule(object):
"""Representation of a fake module."""
@ -439,10 +455,10 @@ def assert_setup_component(count, domain=None):
"""
config = {}
@asyncio.coroutine
@ha.callback
def mock_psc(hass, config_input, domain):
"""Mock the prepare_setup_component to capture config."""
res = yield from async_prepare_setup_component(
res = async_process_component_config(
hass, config_input, domain)
config[domain] = None if res is None else res.get(domain)
_LOGGER.debug('Configuration for %s, Validated: %s, Original %s',
@ -450,7 +466,7 @@ def assert_setup_component(count, domain=None):
return res
assert isinstance(config, dict)
with patch('homeassistant.bootstrap.async_prepare_setup_component',
with patch('homeassistant.config.async_process_component_config',
mock_psc):
yield config