diff --git a/homeassistant/components/camera/push.py b/homeassistant/components/camera/push.py index def5c53dd3f..f5ea336d576 100644 --- a/homeassistant/components/camera/push.py +++ b/homeassistant/components/camera/push.py @@ -21,6 +21,8 @@ import homeassistant.util.dt as dt_util _LOGGER = logging.getLogger(__name__) +DEPENDENCIES = ['http'] + CONF_BUFFER_SIZE = 'buffer' CONF_IMAGE_FIELD = 'field' diff --git a/homeassistant/components/device_tracker/__init__.py b/homeassistant/components/device_tracker/__init__.py index 74cb0a77fef..408672a974f 100644 --- a/homeassistant/components/device_tracker/__init__.py +++ b/homeassistant/components/device_tracker/__init__.py @@ -330,19 +330,18 @@ class DeviceTracker: }) # update known_devices.yaml - self.hass.async_add_job( + self.hass.async_create_task( self.async_update_config( self.hass.config.path(YAML_DEVICES), dev_id, device) ) - @asyncio.coroutine - def async_update_config(self, path, dev_id, device): + async def async_update_config(self, path, dev_id, device): """Add device to YAML configuration file. This method is a coroutine. """ - with (yield from self._is_updating): - yield from self.hass.async_add_job( + async with self._is_updating: + await self.hass.async_add_executor_job( update_config, self.hass.config.path(YAML_DEVICES), dev_id, device) @@ -681,8 +680,7 @@ def async_setup_scanner_platform(hass: HomeAssistantType, config: ConfigType, # Initial scan of each mac we also tell about host name for config seen = set() # type: Any - @asyncio.coroutine - def async_device_tracker_scan(now: dt_util.dt.datetime): + async def async_device_tracker_scan(now: dt_util.dt.datetime): """Handle interval matches.""" if update_lock.locked(): _LOGGER.warning( @@ -690,18 +688,18 @@ def async_setup_scanner_platform(hass: HomeAssistantType, config: ConfigType, "scan interval %s", platform, interval) return - with (yield from update_lock): - found_devices = yield from scanner.async_scan_devices() + async with update_lock: + found_devices = await scanner.async_scan_devices() for mac in found_devices: if mac in seen: host_name = None else: - host_name = yield from scanner.async_get_device_name(mac) + host_name = await scanner.async_get_device_name(mac) seen.add(mac) try: - extra_attributes = (yield from + extra_attributes = (await scanner.async_get_extra_attributes(mac)) except NotImplementedError: extra_attributes = dict() diff --git a/homeassistant/components/websocket_api.py b/homeassistant/components/websocket_api.py index 36811337ec1..c25f4418263 100644 --- a/homeassistant/components/websocket_api.py +++ b/homeassistant/components/websocket_api.py @@ -325,7 +325,6 @@ class ActiveConnection: await wsock.prepare(request) self.debug("Connected") - # Get a reference to current task so we can cancel our connection self._handle_task = asyncio.Task.current_task(loop=self.hass.loop) @callback diff --git a/tests/common.py b/tests/common.py index e7445751783..c56cadc16f9 100644 --- a/tests/common.py +++ b/tests/common.py @@ -123,14 +123,30 @@ def async_test_home_assistant(loop): INSTANCES.append(hass) orig_async_add_job = hass.async_add_job + orig_async_add_executor_job = hass.async_add_executor_job + orig_async_create_task = hass.async_create_task def async_add_job(target, *args): - """Add a magic mock.""" + """Add job.""" if isinstance(target, Mock): return mock_coro(target(*args)) return orig_async_add_job(target, *args) + def async_add_executor_job(target, *args): + """Add executor job.""" + if isinstance(target, Mock): + return mock_coro(target(*args)) + return orig_async_add_executor_job(target, *args) + + def async_create_task(coroutine): + """Create task.""" + if isinstance(coroutine, Mock): + return mock_coro() + return orig_async_create_task(coroutine) + hass.async_add_job = async_add_job + hass.async_add_executor_job = async_add_executor_job + hass.async_create_task = async_create_task hass.config.location_name = 'test home' hass.config.config_dir = get_test_config_dir() diff --git a/tests/components/camera/test_push.py b/tests/components/camera/test_push.py index 78053e540f5..f9a3c62aa4a 100644 --- a/tests/components/camera/test_push.py +++ b/tests/components/camera/test_push.py @@ -30,7 +30,7 @@ async def test_bad_posting(aioclient_mock, hass, aiohttp_client): assert resp.status == 400 -async def test_posting_url(aioclient_mock, hass, aiohttp_client): +async def test_posting_url(hass, aiohttp_client): """Test that posting to api endpoint works.""" await async_setup_component(hass, 'camera', { 'camera': { @@ -38,7 +38,7 @@ async def test_posting_url(aioclient_mock, hass, aiohttp_client): 'name': 'config_test', }}) - client = await async_setup_auth(hass, aiohttp_client) + client = await aiohttp_client(hass.http.app) files = {'image': io.BytesIO(b'fake')} # initial state diff --git a/tests/helpers/test_template.py b/tests/helpers/test_template.py index 37614f9ee45..6f426c290c5 100644 --- a/tests/helpers/test_template.py +++ b/tests/helpers/test_template.py @@ -511,8 +511,8 @@ class TestHelpersTemplate(unittest.TestCase): def test_regex_match(self): """Test regex_match method.""" - tpl = template.Template(""" -{{ '123-456-7890' | regex_match('(\d{3})-(\d{3})-(\d{4})') }} + tpl = template.Template(r""" +{{ '123-456-7890' | regex_match('(\\d{3})-(\\d{3})-(\\d{4})') }} """, self.hass) self.assertEqual('True', tpl.render()) @@ -528,8 +528,8 @@ class TestHelpersTemplate(unittest.TestCase): def test_regex_search(self): """Test regex_search method.""" - tpl = template.Template(""" -{{ '123-456-7890' | regex_search('(\d{3})-(\d{3})-(\d{4})') }} + tpl = template.Template(r""" +{{ '123-456-7890' | regex_search('(\\d{3})-(\\d{3})-(\\d{4})') }} """, self.hass) self.assertEqual('True', tpl.render()) @@ -545,8 +545,8 @@ class TestHelpersTemplate(unittest.TestCase): def test_regex_replace(self): """Test regex_replace method.""" - tpl = template.Template(""" -{{ 'Hello World' | regex_replace('(Hello\s)',) }} + tpl = template.Template(r""" +{{ 'Hello World' | regex_replace('(Hello\\s)',) }} """, self.hass) self.assertEqual('World', tpl.render()) diff --git a/tests/test_util/aiohttp.py b/tests/test_util/aiohttp.py index 813eb84707c..d662f3b1955 100644 --- a/tests/test_util/aiohttp.py +++ b/tests/test_util/aiohttp.py @@ -12,6 +12,8 @@ from yarl import URL from aiohttp.client_exceptions import ClientResponseError +from homeassistant.const import EVENT_HOMEASSISTANT_CLOSE + retype = type(re.compile('')) @@ -216,7 +218,18 @@ def mock_aiohttp_client(): """Context manager to mock aiohttp client.""" mocker = AiohttpClientMocker() + def create_session(hass, *args): + session = mocker.create_session(hass.loop) + + async def close_session(event): + """Close session.""" + await session.close() + + hass.bus.async_listen_once(EVENT_HOMEASSISTANT_CLOSE, close_session) + + return session + with mock.patch( 'homeassistant.helpers.aiohttp_client.async_create_clientsession', - side_effect=lambda hass, *args: mocker.create_session(hass.loop)): + side_effect=create_session): yield mocker