From 870728f68fcc54a1cb4c3c15386b0b2af5c32ff5 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Mon, 12 Feb 2018 10:59:20 -0800 Subject: [PATCH] Mock Module + Platform default to async (#12347) * Mock Module + Platform default to async * Change checks --- tests/common.py | 27 ++++++++++++--------------- tests/helpers/test_entity_platform.py | 2 +- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/tests/common.py b/tests/common.py index d194ad4d040..9e4575780bc 100644 --- a/tests/common.py +++ b/tests/common.py @@ -344,7 +344,6 @@ class MockModule(object): 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 @@ -352,18 +351,15 @@ class MockModule(object): 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): @@ -374,18 +370,19 @@ class MockPlatform(object): 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): diff --git a/tests/helpers/test_entity_platform.py b/tests/helpers/test_entity_platform.py index a54a6de511a..c9705a73f7b 100644 --- a/tests/helpers/test_entity_platform.py +++ b/tests/helpers/test_entity_platform.py @@ -331,7 +331,7 @@ def test_parallel_updates_async_platform_with_constant(hass): @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)