Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock Module + Platform default to async #12347

Merged
merged 2 commits into from
Feb 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 12 additions & 15 deletions tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,26 +344,22 @@ def __init__(self, domain=None, dependencies=None, setup=None,
self.DOMAIN = domain
self.DEPENDENCIES = dependencies or []
self.REQUIREMENTS = requirements or []
self._setup = setup

if config_schema is not None:
self.CONFIG_SCHEMA = config_schema

if platform_schema is not None:
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:
self.async_setup = async_setup

def setup(self, hass, config):
"""Set up the component.

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
if setup is None and async_setup is None:
self.async_setup = mock_coro_func(True)


class MockPlatform(object):
Expand All @@ -374,18 +370,19 @@ def __init__(self, setup_platform=None, dependencies=None,
platform_schema=None, async_setup_platform=None):
"""Initialize the platform."""
self.DEPENDENCIES = dependencies or []
self._setup_platform = setup_platform

if platform_schema is not None:
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:
self.async_setup_platform = async_setup_platform

def setup_platform(self, hass, config, add_devices, discovery_info=None):
"""Set up the platform."""
if self._setup_platform is not None:
self._setup_platform(hass, config, add_devices, discovery_info)
if setup_platform is None and async_setup_platform is None:
self.async_setup_platform = mock_coro_func()


class MockToggleDevice(entity.ToggleEntity):
Expand Down
2 changes: 1 addition & 1 deletion tests/helpers/test_entity_platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ def mock_update(*args, **kwargs):
@asyncio.coroutine
def test_parallel_updates_sync_platform(hass):
"""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)

Expand Down