Mock Module + Platform default to async (#12347)

* Mock Module + Platform default to async

* Change checks
This commit is contained in:
Paulus Schoutsen 2018-02-12 10:59:20 -08:00 committed by GitHub
parent 48f40453f7
commit 870728f68f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 16 deletions

View file

@ -344,7 +344,6 @@ class MockModule(object):
self.DOMAIN = domain self.DOMAIN = domain
self.DEPENDENCIES = dependencies or [] self.DEPENDENCIES = dependencies or []
self.REQUIREMENTS = requirements or [] self.REQUIREMENTS = requirements or []
self._setup = setup
if config_schema is not None: if config_schema is not None:
self.CONFIG_SCHEMA = config_schema self.CONFIG_SCHEMA = config_schema
@ -352,18 +351,15 @@ class MockModule(object):
if platform_schema is not None: if platform_schema is not None:
self.PLATFORM_SCHEMA = platform_schema self.PLATFORM_SCHEMA = platform_schema
if setup is not None:
# We run this in executor, wrap it in function
self.setup = lambda *args: setup(*args)
if async_setup is not None: if async_setup is not None:
self.async_setup = async_setup self.async_setup = async_setup
def setup(self, hass, config): if setup is None and async_setup is None:
"""Set up the component. self.async_setup = mock_coro_func(True)
We always define this mock because MagicMock setups will be seen by the
executor as a coroutine, raising an exception.
"""
if self._setup is not None:
return self._setup(hass, config)
return True
class MockPlatform(object): class MockPlatform(object):
@ -374,18 +370,19 @@ class MockPlatform(object):
platform_schema=None, async_setup_platform=None): platform_schema=None, async_setup_platform=None):
"""Initialize the platform.""" """Initialize the platform."""
self.DEPENDENCIES = dependencies or [] self.DEPENDENCIES = dependencies or []
self._setup_platform = setup_platform
if platform_schema is not None: if platform_schema is not None:
self.PLATFORM_SCHEMA = platform_schema self.PLATFORM_SCHEMA = platform_schema
if setup_platform is not None:
# We run this in executor, wrap it in function
self.setup_platform = lambda *args: setup_platform(*args)
if async_setup_platform is not None: if async_setup_platform is not None:
self.async_setup_platform = async_setup_platform self.async_setup_platform = async_setup_platform
def setup_platform(self, hass, config, add_devices, discovery_info=None): if setup_platform is None and async_setup_platform is None:
"""Set up the platform.""" self.async_setup_platform = mock_coro_func()
if self._setup_platform is not None:
self._setup_platform(hass, config, add_devices, discovery_info)
class MockToggleDevice(entity.ToggleEntity): class MockToggleDevice(entity.ToggleEntity):

View file

@ -331,7 +331,7 @@ def test_parallel_updates_async_platform_with_constant(hass):
@asyncio.coroutine @asyncio.coroutine
def test_parallel_updates_sync_platform(hass): def test_parallel_updates_sync_platform(hass):
"""Warn we log when platform setup takes a long time.""" """Warn we log when platform setup takes a long time."""
platform = MockPlatform() platform = MockPlatform(setup_platform=lambda *args: None)
loader.set_component('test_domain.platform', platform) loader.set_component('test_domain.platform', platform)