From 0d700f6a636c4fbc6d00d1ea84fdf806c4e52668 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sat, 25 Apr 2020 14:32:55 -0700 Subject: [PATCH] Fix tests for Python 3.8 (#34672) --- .../ambiclimate/test_config_flow.py | 3 +- tests/components/awair/test_sensor.py | 24 ++++----- tests/components/axis/test_config_flow.py | 10 ++-- tests/components/cast/test_media_player.py | 9 ++-- tests/components/cloud/test_account_link.py | 18 +++---- tests/components/config/test_core.py | 38 ++++++-------- .../components/coolmaster/test_config_flow.py | 9 ++-- tests/components/device_tracker/test_init.py | 3 +- tests/components/freebox/test_config_flow.py | 8 ++- tests/components/hassio/test_init.py | 9 ++-- tests/components/hisense_aehw4a1/test_init.py | 18 +++---- tests/components/homeassistant/test_init.py | 21 ++++---- tests/components/ipma/test_weather.py | 13 ++--- tests/components/izone/test_config_flow.py | 14 ++---- .../components/luftdaten/test_config_flow.py | 19 +++---- tests/components/microsoft_face/test_init.py | 26 ++++------ tests/components/mikrotik/test_init.py | 15 +++--- tests/components/mqtt/test_init.py | 50 +++++++++---------- tests/components/mqtt/test_light.py | 50 +++++++++---------- tests/components/mqtt/test_light_json.py | 38 +++++++------- tests/components/mqtt/test_light_template.py | 6 +-- tests/components/notion/test_config_flow.py | 3 +- .../openalpr_cloud/test_image_processing.py | 3 +- .../openalpr_local/test_image_processing.py | 2 +- .../opentherm_gw/test_config_flow.py | 2 +- .../components/owntracks/test_config_flow.py | 3 +- .../rainmachine/test_config_flow.py | 3 +- tests/components/ring/test_config_flow.py | 2 +- tests/components/rmvtransport/test_sensor.py | 19 +++---- tests/components/sentry/test_config_flow.py | 3 +- tests/components/shell_command/test_init.py | 3 +- tests/components/smhi/test_config_flow.py | 3 +- tests/components/smhi/test_weather.py | 3 +- tests/components/solarlog/test_config_flow.py | 3 +- tests/components/tesla/test_config_flow.py | 3 +- tests/components/tradfri/conftest.py | 7 +-- tests/components/tradfri/test_config_flow.py | 49 ++++++++++-------- tests/components/tradfri/test_init.py | 13 +++-- tests/components/upnp/test_init.py | 3 +- tests/conftest.py | 5 +- 40 files changed, 248 insertions(+), 285 deletions(-) diff --git a/tests/components/ambiclimate/test_config_flow.py b/tests/components/ambiclimate/test_config_flow.py index acf3717b898..045eac1328b 100644 --- a/tests/components/ambiclimate/test_config_flow.py +++ b/tests/components/ambiclimate/test_config_flow.py @@ -1,7 +1,6 @@ """Tests for the Ambiclimate config flow.""" -from unittest.mock import Mock, patch - import ambiclimate +from asynctest import Mock, patch from homeassistant import data_entry_flow from homeassistant.components.ambiclimate import config_flow diff --git a/tests/components/awair/test_sensor.py b/tests/components/awair/test_sensor.py index 2d88a5019b1..8ae5bb8017f 100644 --- a/tests/components/awair/test_sensor.py +++ b/tests/components/awair/test_sensor.py @@ -4,7 +4,8 @@ from contextlib import contextmanager from datetime import timedelta import json import logging -from unittest.mock import patch + +from asynctest import patch from homeassistant.components.awair.sensor import ( ATTR_LAST_API_UPDATE, @@ -28,7 +29,7 @@ from homeassistant.const import ( from homeassistant.setup import async_setup_component from homeassistant.util.dt import parse_datetime, utcnow -from tests.common import async_fire_time_changed, load_fixture, mock_coro +from tests.common import async_fire_time_changed, load_fixture DISCOVERY_CONFIG = {"sensor": {"platform": "awair", "access_token": "qwerty"}} @@ -68,9 +69,9 @@ def alter_time(retval): async def setup_awair(hass, config=None, data_fixture=AIR_DATA_FIXTURE): """Load the Awair platform.""" devices_json = json.loads(load_fixture("awair_devices.json")) - devices_mock = mock_coro(devices_json) + devices_mock = devices_json devices_patch = patch("python_awair.AwairClient.devices", return_value=devices_mock) - air_data_mock = mock_coro(data_fixture) + air_data_mock = data_fixture air_data_patch = patch( "python_awair.AwairClient.air_data_latest", return_value=air_data_mock ) @@ -233,8 +234,7 @@ async def test_availability(hass): future = NOW + timedelta(minutes=30) data_patch = patch( - "python_awair.AwairClient.air_data_latest", - return_value=mock_coro(AIR_DATA_FIXTURE), + "python_awair.AwairClient.air_data_latest", return_value=AIR_DATA_FIXTURE, ) with data_patch, alter_time(future): @@ -246,9 +246,7 @@ async def test_availability(hass): future = NOW + timedelta(hours=1) fixture = AIR_DATA_FIXTURE_UPDATED fixture[0][ATTR_TIMESTAMP] = str(future) - data_patch = patch( - "python_awair.AwairClient.air_data_latest", return_value=mock_coro(fixture) - ) + data_patch = patch("python_awair.AwairClient.air_data_latest", return_value=fixture) with data_patch, alter_time(future): async_fire_time_changed(hass, future) @@ -258,9 +256,7 @@ async def test_availability(hass): future = NOW + timedelta(minutes=90) fixture = AIR_DATA_FIXTURE_EMPTY - data_patch = patch( - "python_awair.AwairClient.air_data_latest", return_value=mock_coro(fixture) - ) + data_patch = patch("python_awair.AwairClient.air_data_latest", return_value=fixture) with data_patch, alter_time(future): async_fire_time_changed(hass, future) @@ -276,7 +272,7 @@ async def test_async_update(hass): future = NOW + timedelta(minutes=10) data_patch = patch( "python_awair.AwairClient.air_data_latest", - return_value=mock_coro(AIR_DATA_FIXTURE_UPDATED), + return_value=AIR_DATA_FIXTURE_UPDATED, ) with data_patch, alter_time(future): @@ -300,7 +296,7 @@ async def test_throttle_async_update(hass): future = NOW + timedelta(minutes=1) data_patch = patch( "python_awair.AwairClient.air_data_latest", - return_value=mock_coro(AIR_DATA_FIXTURE_UPDATED), + return_value=AIR_DATA_FIXTURE_UPDATED, ) with data_patch, alter_time(future): diff --git a/tests/components/axis/test_config_flow.py b/tests/components/axis/test_config_flow.py index 2e4c3e9f8be..e0bf06e3468 100644 --- a/tests/components/axis/test_config_flow.py +++ b/tests/components/axis/test_config_flow.py @@ -1,12 +1,12 @@ """Test Axis config flow.""" -from unittest.mock import Mock, patch +from asynctest import Mock, patch from homeassistant.components import axis from homeassistant.components.axis import config_flow from .test_device import MAC, MODEL, NAME, setup_axis_integration -from tests.common import MockConfigEntry, mock_coro +from tests.common import MockConfigEntry def setup_mock_axis_device(mock_device): @@ -80,7 +80,7 @@ async def test_manual_configuration_update_configuration(hass): with patch( "homeassistant.components.axis.config_flow.get_device", - return_value=mock_coro(mock_device), + return_value=mock_device, ): result = await hass.config_entries.flow.async_configure( result["flow_id"], @@ -113,7 +113,7 @@ async def test_flow_fails_already_configured(hass): with patch( "homeassistant.components.axis.config_flow.get_device", - return_value=mock_coro(mock_device), + return_value=mock_device, ): result = await hass.config_entries.flow.async_configure( result["flow_id"], @@ -232,7 +232,7 @@ async def test_flow_create_entry_multiple_existing_entries_of_same_model(hass): async def test_zeroconf_flow(hass): """Test that zeroconf discovery for new devices work.""" - with patch.object(axis, "get_device", return_value=mock_coro(Mock())): + with patch.object(axis, "get_device", return_value=Mock()): result = await hass.config_entries.flow.async_init( config_flow.DOMAIN, data={ diff --git a/tests/components/cast/test_media_player.py b/tests/components/cast/test_media_player.py index 41cc50f33ae..e2c1064218d 100644 --- a/tests/components/cast/test_media_player.py +++ b/tests/components/cast/test_media_player.py @@ -1,9 +1,9 @@ """The tests for the Cast Media player platform.""" # pylint: disable=protected-access from typing import Optional -from unittest.mock import MagicMock, Mock, patch from uuid import UUID +from asynctest import MagicMock, Mock, patch import attr import pytest @@ -14,7 +14,7 @@ from homeassistant.exceptions import PlatformNotReady from homeassistant.helpers.typing import HomeAssistantType from homeassistant.setup import async_setup_component -from tests.common import MockConfigEntry, mock_coro +from tests.common import MockConfigEntry @pytest.fixture(autouse=True) @@ -468,7 +468,6 @@ async def test_entry_setup_no_config(hass: HomeAssistantType): with patch( "homeassistant.components.cast.media_player._async_setup_platform", - return_value=mock_coro(), ) as mock_setup: await cast.async_setup_entry(hass, MockConfigEntry(), None) @@ -484,7 +483,6 @@ async def test_entry_setup_single_config(hass: HomeAssistantType): with patch( "homeassistant.components.cast.media_player._async_setup_platform", - return_value=mock_coro(), ) as mock_setup: await cast.async_setup_entry(hass, MockConfigEntry(), None) @@ -500,7 +498,6 @@ async def test_entry_setup_list_config(hass: HomeAssistantType): with patch( "homeassistant.components.cast.media_player._async_setup_platform", - return_value=mock_coro(), ) as mock_setup: await cast.async_setup_entry(hass, MockConfigEntry(), None) @@ -517,7 +514,7 @@ async def test_entry_setup_platform_not_ready(hass: HomeAssistantType): with patch( "homeassistant.components.cast.media_player._async_setup_platform", - return_value=mock_coro(exception=Exception), + side_effect=Exception, ) as mock_setup: with pytest.raises(PlatformNotReady): await cast.async_setup_entry(hass, MockConfigEntry(), None) diff --git a/tests/components/cloud/test_account_link.py b/tests/components/cloud/test_account_link.py index a8c247cc985..a5f3bef7353 100644 --- a/tests/components/cloud/test_account_link.py +++ b/tests/components/cloud/test_account_link.py @@ -2,8 +2,8 @@ import asyncio import logging from time import time -from unittest.mock import Mock, patch +from asynctest import CoroutineMock, Mock, patch import pytest from homeassistant import config_entries, data_entry_flow @@ -11,7 +11,7 @@ from homeassistant.components.cloud import account_link from homeassistant.helpers import config_entry_oauth2_flow from homeassistant.util.dt import utcnow -from tests.common import async_fire_time_changed, mock_coro, mock_platform +from tests.common import async_fire_time_changed, mock_platform TEST_DOMAIN = "oauth2_test" @@ -42,12 +42,10 @@ async def test_setup_provide_implementation(hass): with patch( "homeassistant.components.cloud.account_link._get_services", - side_effect=lambda _: mock_coro( - [ - {"service": "test", "min_version": "0.1.0"}, - {"service": "too_new", "min_version": "100.0.0"}, - ] - ), + return_value=[ + {"service": "test", "min_version": "0.1.0"}, + {"service": "too_new", "min_version": "100.0.0"}, + ], ): assert ( await config_entry_oauth2_flow.async_get_implementations( @@ -77,7 +75,7 @@ async def test_get_services_cached(hass): with patch.object(account_link, "CACHE_TIMEOUT", 0), patch( "hass_nabucasa.account_link.async_fetch_available_services", - side_effect=lambda _: mock_coro(services), + side_effect=lambda _: services, ) as mock_fetch: assert await account_link._get_services(hass) == 1 @@ -111,7 +109,7 @@ async def test_implementation(hass, flow_handler): flow_finished = asyncio.Future() helper = Mock( - async_get_authorize_url=Mock(return_value=mock_coro("http://example.com/auth")), + async_get_authorize_url=CoroutineMock(return_value="http://example.com/auth"), async_get_tokens=Mock(return_value=flow_finished), ) diff --git a/tests/components/config/test_core.py b/tests/components/config/test_core.py index 8caa0f3e6fb..a722333c037 100644 --- a/tests/components/config/test_core.py +++ b/tests/components/config/test_core.py @@ -1,6 +1,5 @@ """Test hassbian config.""" -from unittest.mock import patch - +from asynctest import patch import pytest from homeassistant.bootstrap import async_setup_component @@ -9,8 +8,6 @@ from homeassistant.components.websocket_api.const import TYPE_RESULT from homeassistant.const import CONF_UNIT_SYSTEM, CONF_UNIT_SYSTEM_IMPERIAL from homeassistant.util import dt as dt_util, location -from tests.common import mock_coro - ORIG_TIME_ZONE = dt_util.DEFAULT_TIME_ZONE @@ -31,7 +28,7 @@ async def test_validate_config_ok(hass, hass_client): with patch( "homeassistant.components.config.core.async_check_ha_config_file", - return_value=mock_coro(), + return_value=None, ): resp = await client.post("/api/config/core/check_config") @@ -42,7 +39,7 @@ async def test_validate_config_ok(hass, hass_client): with patch( "homeassistant.components.config.core.async_check_ha_config_file", - return_value=mock_coro("beer"), + return_value="beer", ): resp = await client.post("/api/config/core/check_config") @@ -121,8 +118,7 @@ async def test_websocket_bad_core_update(hass, client): async def test_detect_config(hass, client): """Test detect config.""" with patch( - "homeassistant.util.location.async_detect_location_info", - return_value=mock_coro(None), + "homeassistant.util.location.async_detect_location_info", return_value=None, ): await client.send_json({"id": 1, "type": "config/core/detect"}) @@ -136,20 +132,18 @@ async def test_detect_config_fail(hass, client): """Test detect config.""" with patch( "homeassistant.util.location.async_detect_location_info", - return_value=mock_coro( - location.LocationInfo( - ip=None, - country_code=None, - country_name=None, - region_code=None, - region_name=None, - city=None, - zip_code=None, - latitude=None, - longitude=None, - use_metric=True, - time_zone="Europe/Amsterdam", - ) + return_value=location.LocationInfo( + ip=None, + country_code=None, + country_name=None, + region_code=None, + region_name=None, + city=None, + zip_code=None, + latitude=None, + longitude=None, + use_metric=True, + time_zone="Europe/Amsterdam", ), ): await client.send_json({"id": 1, "type": "config/core/detect"}) diff --git a/tests/components/coolmaster/test_config_flow.py b/tests/components/coolmaster/test_config_flow.py index c71f308dece..81219c41ff8 100644 --- a/tests/components/coolmaster/test_config_flow.py +++ b/tests/components/coolmaster/test_config_flow.py @@ -1,11 +1,9 @@ """Test the Coolmaster config flow.""" -from unittest.mock import patch +from asynctest import patch from homeassistant import config_entries, setup from homeassistant.components.coolmaster.const import AVAILABLE_MODES, DOMAIN -from tests.common import mock_coro - def _flow_data(): options = {"host": "1.1.1.1"} @@ -27,10 +25,9 @@ async def test_form(hass): "homeassistant.components.coolmaster.config_flow.CoolMasterNet.devices", return_value=[1], ), patch( - "homeassistant.components.coolmaster.async_setup", return_value=mock_coro(True) + "homeassistant.components.coolmaster.async_setup", return_value=True ) as mock_setup, patch( - "homeassistant.components.coolmaster.async_setup_entry", - return_value=mock_coro(True), + "homeassistant.components.coolmaster.async_setup_entry", return_value=True, ) as mock_setup_entry: result2 = await hass.config_entries.flow.async_configure( result["flow_id"], _flow_data() diff --git a/tests/components/device_tracker/test_init.py b/tests/components/device_tracker/test_init.py index 8ecc341d4d2..6f384faa15c 100644 --- a/tests/components/device_tracker/test_init.py +++ b/tests/components/device_tracker/test_init.py @@ -3,9 +3,8 @@ from datetime import datetime, timedelta import json import logging import os -from unittest.mock import Mock, call -from asynctest import patch +from asynctest import Mock, call, patch import pytest from homeassistant.components import zone diff --git a/tests/components/freebox/test_config_flow.py b/tests/components/freebox/test_config_flow.py index 68e787e1ba0..4d8fadf0654 100644 --- a/tests/components/freebox/test_config_flow.py +++ b/tests/components/freebox/test_config_flow.py @@ -23,7 +23,13 @@ def mock_controller_connect(): """Mock a successful connection.""" with patch("homeassistant.components.freebox.router.Freepybox") as service_mock: service_mock.return_value.open = CoroutineMock() - service_mock.return_value.system.get_config = CoroutineMock() + service_mock.return_value.system.get_config = CoroutineMock( + return_value={ + "mac": "abcd", + "model_info": {"pretty_name": "Pretty Model"}, + "firmware_version": "123", + } + ) service_mock.return_value.lan.get_hosts_list = CoroutineMock() service_mock.return_value.connection.get_status = CoroutineMock() service_mock.return_value.close = CoroutineMock() diff --git a/tests/components/hassio/test_init.py b/tests/components/hassio/test_init.py index 2751062dedf..26caec65b40 100644 --- a/tests/components/hassio/test_init.py +++ b/tests/components/hassio/test_init.py @@ -1,7 +1,7 @@ """The tests for the hassio component.""" import os -from unittest.mock import Mock, patch +from asynctest import patch import pytest from homeassistant.auth.const import GROUP_ID_ADMIN @@ -9,8 +9,6 @@ from homeassistant.components import frontend from homeassistant.components.hassio import STORAGE_KEY from homeassistant.setup import async_setup_component -from tests.common import mock_coro - MOCK_ENVIRON = {"HASSIO": "127.0.0.1", "HASSIO_TOKEN": "abcdefgh"} @@ -193,8 +191,7 @@ async def test_fail_setup_without_environ_var(hass): async def test_warn_when_cannot_connect(hass, caplog): """Fail warn when we cannot connect.""" with patch.dict(os.environ, MOCK_ENVIRON), patch( - "homeassistant.components.hassio.HassIO.is_connected", - Mock(return_value=mock_coro(None)), + "homeassistant.components.hassio.HassIO.is_connected", return_value=None, ): result = await async_setup_component(hass, "hassio", {}) assert result @@ -311,7 +308,7 @@ async def test_service_calls_core(hassio_env, hass, aioclient_mock): assert aioclient_mock.call_count == 4 with patch( - "homeassistant.config.async_check_ha_config_file", return_value=mock_coro() + "homeassistant.config.async_check_ha_config_file", return_value=None ) as mock_check_config: await hass.services.async_call("homeassistant", "restart") await hass.async_block_till_done() diff --git a/tests/components/hisense_aehw4a1/test_init.py b/tests/components/hisense_aehw4a1/test_init.py index 638fbe8f943..f2af78fe160 100644 --- a/tests/components/hisense_aehw4a1/test_init.py +++ b/tests/components/hisense_aehw4a1/test_init.py @@ -1,24 +1,21 @@ """Tests for the Hisense AEH-W4A1 init file.""" -from unittest.mock import patch - +from asynctest import patch from pyaehw4a1 import exceptions from homeassistant import config_entries, data_entry_flow from homeassistant.components import hisense_aehw4a1 from homeassistant.setup import async_setup_component -from tests.common import mock_coro - async def test_creating_entry_sets_up_climate_discovery(hass): """Test setting up Hisense AEH-W4A1 loads the climate component.""" with patch( "homeassistant.components.hisense_aehw4a1.config_flow.AehW4a1.discovery", - return_value=mock_coro(["1.2.3.4"]), + return_value=["1.2.3.4"], ): with patch( "homeassistant.components.hisense_aehw4a1.climate.async_setup_entry", - return_value=mock_coro(True), + return_value=True, ) as mock_setup: result = await hass.config_entries.flow.async_init( hisense_aehw4a1.DOMAIN, context={"source": config_entries.SOURCE_USER} @@ -41,11 +38,11 @@ async def test_configuring_hisense_w4a1_create_entry(hass): """Test that specifying config will create an entry.""" with patch( "homeassistant.components.hisense_aehw4a1.config_flow.AehW4a1.check", - return_value=mock_coro(True), + return_value=True, ): with patch( "homeassistant.components.hisense_aehw4a1.async_setup_entry", - return_value=mock_coro(True), + return_value=True, ) as mock_setup: await async_setup_component( hass, @@ -65,7 +62,7 @@ async def test_configuring_hisense_w4a1_not_creates_entry_for_device_not_found(h ): with patch( "homeassistant.components.hisense_aehw4a1.async_setup_entry", - return_value=mock_coro(True), + return_value=True, ) as mock_setup: await async_setup_component( hass, @@ -80,8 +77,7 @@ async def test_configuring_hisense_w4a1_not_creates_entry_for_device_not_found(h async def test_configuring_hisense_w4a1_not_creates_entry_for_empty_import(hass): """Test that specifying config will not create an entry.""" with patch( - "homeassistant.components.hisense_aehw4a1.async_setup_entry", - return_value=mock_coro(True), + "homeassistant.components.hisense_aehw4a1.async_setup_entry", return_value=True, ) as mock_setup: await async_setup_component(hass, hisense_aehw4a1.DOMAIN, {}) await hass.async_block_till_done() diff --git a/tests/components/homeassistant/test_init.py b/tests/components/homeassistant/test_init.py index 38a76b7c3fb..fddd149942e 100644 --- a/tests/components/homeassistant/test_init.py +++ b/tests/components/homeassistant/test_init.py @@ -2,8 +2,8 @@ # pylint: disable=protected-access import asyncio import unittest -from unittest.mock import Mock, patch +from asynctest import Mock, patch import pytest import voluptuous as vol import yaml @@ -37,7 +37,6 @@ from tests.common import ( async_capture_events, async_mock_service, get_test_home_assistant, - mock_coro, mock_service, patch_yaml_files, ) @@ -215,15 +214,15 @@ class TestComponentsCore(unittest.TestCase): assert mock_error.called assert mock_process.called is False - @patch("homeassistant.core.HomeAssistant.async_stop", return_value=mock_coro()) + @patch("homeassistant.core.HomeAssistant.async_stop", return_value=None) def test_stop_homeassistant(self, mock_stop): """Test stop service.""" stop(self.hass) self.hass.block_till_done() assert mock_stop.called - @patch("homeassistant.core.HomeAssistant.async_stop", return_value=mock_coro()) - @patch("homeassistant.config.async_check_ha_config_file", return_value=mock_coro()) + @patch("homeassistant.core.HomeAssistant.async_stop", return_value=None) + @patch("homeassistant.config.async_check_ha_config_file", return_value=None) def test_restart_homeassistant(self, mock_check, mock_restart): """Test stop service.""" restart(self.hass) @@ -231,7 +230,7 @@ class TestComponentsCore(unittest.TestCase): assert mock_restart.called assert mock_check.called - @patch("homeassistant.core.HomeAssistant.async_stop", return_value=mock_coro()) + @patch("homeassistant.core.HomeAssistant.async_stop", return_value=None) @patch( "homeassistant.config.async_check_ha_config_file", side_effect=HomeAssistantError("Test error"), @@ -243,8 +242,8 @@ class TestComponentsCore(unittest.TestCase): assert mock_check.called assert not mock_restart.called - @patch("homeassistant.core.HomeAssistant.async_stop", return_value=mock_coro()) - @patch("homeassistant.config.async_check_ha_config_file", return_value=mock_coro()) + @patch("homeassistant.core.HomeAssistant.async_stop", return_value=None) + @patch("homeassistant.config.async_check_ha_config_file", return_value=None) def test_check_config(self, mock_check, mock_stop): """Test stop service.""" check_config(self.hass) @@ -271,8 +270,7 @@ async def test_turn_on_to_not_block_for_domains_without_service(hass): service = hass.services._services["homeassistant"]["turn_on"] with patch( - "homeassistant.core.ServiceRegistry.async_call", - side_effect=lambda *args: mock_coro(), + "homeassistant.core.ServiceRegistry.async_call", return_value=None, ) as mock_call: await service.func(service_call) @@ -296,8 +294,7 @@ async def test_entity_update(hass): await async_setup_component(hass, "homeassistant", {}) with patch( - "homeassistant.helpers.entity_component.async_update_entity", - return_value=mock_coro(), + "homeassistant.helpers.entity_component.async_update_entity", return_value=None, ) as mock_update: await hass.services.async_call( "homeassistant", diff --git a/tests/components/ipma/test_weather.py b/tests/components/ipma/test_weather.py index 7a6e1160f24..b3d398377f0 100644 --- a/tests/components/ipma/test_weather.py +++ b/tests/components/ipma/test_weather.py @@ -1,6 +1,7 @@ """The tests for the IPMA weather component.""" from collections import namedtuple -from unittest.mock import patch + +from asynctest import patch from homeassistant.components import weather from homeassistant.components.weather import ( @@ -22,7 +23,7 @@ from homeassistant.components.weather import ( from homeassistant.setup import async_setup_component from homeassistant.util.dt import now -from tests.common import MockConfigEntry, mock_coro +from tests.common import MockConfigEntry TEST_CONFIG = {"name": "HomeTown", "latitude": "40.00", "longitude": "-8.00"} @@ -128,7 +129,7 @@ async def test_setup_configuration(hass): """Test for successfully setting up the IPMA platform.""" with patch( "homeassistant.components.ipma.weather.async_get_location", - return_value=mock_coro(MockLocation()), + return_value=MockLocation(), ): assert await async_setup_component( hass, @@ -153,7 +154,7 @@ async def test_setup_config_flow(hass): """Test for successfully setting up the IPMA platform.""" with patch( "homeassistant.components.ipma.weather.async_get_location", - return_value=mock_coro(MockLocation()), + return_value=MockLocation(), ): entry = MockConfigEntry(domain="ipma", data=TEST_CONFIG) await hass.config_entries.async_forward_entry_setup(entry, WEATHER_DOMAIN) @@ -175,7 +176,7 @@ async def test_daily_forecast(hass): """Test for successfully getting daily forecast.""" with patch( "homeassistant.components.ipma.weather.async_get_location", - return_value=mock_coro(MockLocation()), + return_value=MockLocation(), ): assert await async_setup_component( hass, @@ -201,7 +202,7 @@ async def test_hourly_forecast(hass): """Test for successfully getting daily forecast.""" with patch( "homeassistant.components.ipma.weather.async_get_location", - return_value=mock_coro(MockLocation()), + return_value=MockLocation(), ): assert await async_setup_component( hass, diff --git a/tests/components/izone/test_config_flow.py b/tests/components/izone/test_config_flow.py index 5deafeb08a7..942d95cc503 100644 --- a/tests/components/izone/test_config_flow.py +++ b/tests/components/izone/test_config_flow.py @@ -1,14 +1,11 @@ """Tests for iZone.""" -from unittest.mock import Mock, patch - +from asynctest import Mock, patch import pytest from homeassistant import config_entries, data_entry_flow from homeassistant.components.izone.const import DISPATCH_CONTROLLER_DISCOVERED, IZONE -from tests.common import mock_coro - @pytest.fixture def mock_disco(): @@ -24,7 +21,7 @@ def _mock_start_discovery(hass, mock_disco): def do_disovered(*args): async_dispatcher_send(hass, DISPATCH_CONTROLLER_DISCOVERED, True) - return mock_coro(mock_disco) + return mock_disco return do_disovered @@ -36,7 +33,7 @@ async def test_not_found(hass, mock_disco): "homeassistant.components.izone.config_flow.async_start_discovery_service" ) as start_disco, patch( "homeassistant.components.izone.config_flow.async_stop_discovery_service", - return_value=mock_coro(), + return_value=None, ) as stop_disco: start_disco.side_effect = _mock_start_discovery(hass, mock_disco) result = await hass.config_entries.flow.async_init( @@ -59,13 +56,12 @@ async def test_found(hass, mock_disco): mock_disco.pi_disco.controllers["blah"] = object() with patch( - "homeassistant.components.izone.climate.async_setup_entry", - return_value=mock_coro(True), + "homeassistant.components.izone.climate.async_setup_entry", return_value=True, ) as mock_setup, patch( "homeassistant.components.izone.config_flow.async_start_discovery_service" ) as start_disco, patch( "homeassistant.components.izone.async_start_discovery_service", - return_value=mock_coro(), + return_value=None, ): start_disco.side_effect = _mock_start_discovery(hass, mock_disco) result = await hass.config_entries.flow.async_init( diff --git a/tests/components/luftdaten/test_config_flow.py b/tests/components/luftdaten/test_config_flow.py index 8718db88ce1..e0e54a6b790 100644 --- a/tests/components/luftdaten/test_config_flow.py +++ b/tests/components/luftdaten/test_config_flow.py @@ -1,13 +1,14 @@ """Define tests for the Luftdaten config flow.""" from datetime import timedelta -from unittest.mock import patch + +from asynctest import patch from homeassistant import data_entry_flow from homeassistant.components.luftdaten import DOMAIN, config_flow from homeassistant.components.luftdaten.const import CONF_SENSOR_ID from homeassistant.const import CONF_SCAN_INTERVAL, CONF_SHOW_ON_MAP -from tests.common import MockConfigEntry, mock_coro +from tests.common import MockConfigEntry async def test_duplicate_error(hass): @@ -29,7 +30,7 @@ async def test_communication_error(hass): flow = config_flow.LuftDatenFlowHandler() flow.hass = hass - with patch("luftdaten.Luftdaten.get_data", return_value=mock_coro(None)): + with patch("luftdaten.Luftdaten.get_data", return_value=None): result = await flow.async_step_user(user_input=conf) assert result["errors"] == {CONF_SENSOR_ID: "invalid_sensor"} @@ -41,8 +42,8 @@ async def test_invalid_sensor(hass): flow = config_flow.LuftDatenFlowHandler() flow.hass = hass - with patch("luftdaten.Luftdaten.get_data", return_value=mock_coro(False)), patch( - "luftdaten.Luftdaten.validate_sensor", return_value=mock_coro(False) + with patch("luftdaten.Luftdaten.get_data", return_value=False), patch( + "luftdaten.Luftdaten.validate_sensor", return_value=False ): result = await flow.async_step_user(user_input=conf) assert result["errors"] == {CONF_SENSOR_ID: "invalid_sensor"} @@ -66,8 +67,8 @@ async def test_step_import(hass): flow = config_flow.LuftDatenFlowHandler() flow.hass = hass - with patch("luftdaten.Luftdaten.get_data", return_value=mock_coro(True)), patch( - "luftdaten.Luftdaten.validate_sensor", return_value=mock_coro(True) + with patch("luftdaten.Luftdaten.get_data", return_value=True), patch( + "luftdaten.Luftdaten.validate_sensor", return_value=True ): result = await flow.async_step_import(import_config=conf) @@ -91,8 +92,8 @@ async def test_step_user(hass): flow = config_flow.LuftDatenFlowHandler() flow.hass = hass - with patch("luftdaten.Luftdaten.get_data", return_value=mock_coro(True)), patch( - "luftdaten.Luftdaten.validate_sensor", return_value=mock_coro(True) + with patch("luftdaten.Luftdaten.get_data", return_value=True), patch( + "luftdaten.Luftdaten.validate_sensor", return_value=True ): result = await flow.async_step_user(user_input=conf) diff --git a/tests/components/microsoft_face/test_init.py b/tests/components/microsoft_face/test_init.py index 803ca006965..478a3fb29bd 100644 --- a/tests/components/microsoft_face/test_init.py +++ b/tests/components/microsoft_face/test_init.py @@ -1,6 +1,7 @@ """The tests for the microsoft face platform.""" import asyncio -from unittest.mock import patch + +from asynctest import patch from homeassistant.components import camera, microsoft_face as mf from homeassistant.components.microsoft_face import ( @@ -18,12 +19,7 @@ from homeassistant.components.microsoft_face import ( from homeassistant.const import ATTR_NAME from homeassistant.setup import setup_component -from tests.common import ( - assert_setup_component, - get_test_home_assistant, - load_fixture, - mock_coro, -) +from tests.common import assert_setup_component, get_test_home_assistant, load_fixture def create_group(hass, name): @@ -97,7 +93,7 @@ class TestMicrosoftFaceSetup: @patch( "homeassistant.components.microsoft_face.MicrosoftFace.update_store", - return_value=mock_coro(), + return_value=None, ) def test_setup_component(self, mock_update): """Set up component.""" @@ -106,7 +102,7 @@ class TestMicrosoftFaceSetup: @patch( "homeassistant.components.microsoft_face.MicrosoftFace.update_store", - return_value=mock_coro(), + return_value=None, ) def test_setup_component_wrong_api_key(self, mock_update): """Set up component without api key.""" @@ -115,7 +111,7 @@ class TestMicrosoftFaceSetup: @patch( "homeassistant.components.microsoft_face.MicrosoftFace.update_store", - return_value=mock_coro(), + return_value=None, ) def test_setup_component_test_service(self, mock_update): """Set up component.""" @@ -171,7 +167,7 @@ class TestMicrosoftFaceSetup: @patch( "homeassistant.components.microsoft_face.MicrosoftFace.update_store", - return_value=mock_coro(), + return_value=None, ) def test_service_groups(self, mock_update, aioclient_mock): """Set up component, test groups services.""" @@ -258,7 +254,7 @@ class TestMicrosoftFaceSetup: @patch( "homeassistant.components.microsoft_face.MicrosoftFace.update_store", - return_value=mock_coro(), + return_value=None, ) def test_service_train(self, mock_update, aioclient_mock): """Set up component, test train groups services.""" @@ -278,7 +274,7 @@ class TestMicrosoftFaceSetup: @patch( "homeassistant.components.camera.async_get_image", - return_value=mock_coro(camera.Image("image/jpeg", b"Test")), + return_value=camera.Image("image/jpeg", b"Test"), ) def test_service_face(self, camera_mock, aioclient_mock): """Set up component, test person face services.""" @@ -318,7 +314,7 @@ class TestMicrosoftFaceSetup: @patch( "homeassistant.components.microsoft_face.MicrosoftFace.update_store", - return_value=mock_coro(), + return_value=None, ) def test_service_status_400(self, mock_update, aioclient_mock): """Set up component, test groups services with error.""" @@ -340,7 +336,7 @@ class TestMicrosoftFaceSetup: @patch( "homeassistant.components.microsoft_face.MicrosoftFace.update_store", - return_value=mock_coro(), + return_value=None, ) def test_service_status_timeout(self, mock_update, aioclient_mock): """Set up component, test groups services with timeout.""" diff --git a/tests/components/mikrotik/test_init.py b/tests/components/mikrotik/test_init.py index ea7e22239b2..1a634916781 100644 --- a/tests/components/mikrotik/test_init.py +++ b/tests/components/mikrotik/test_init.py @@ -1,12 +1,12 @@ """Test Mikrotik setup process.""" -from unittest.mock import Mock, patch +from asynctest import CoroutineMock, Mock, patch from homeassistant.components import mikrotik from homeassistant.setup import async_setup_component from . import MOCK_DATA -from tests.common import MockConfigEntry, mock_coro +from tests.common import MockConfigEntry async def test_setup_with_no_config(hass): @@ -23,9 +23,9 @@ async def test_successful_config_entry(hass): with patch.object(mikrotik, "MikrotikHub") as mock_hub, patch( "homeassistant.helpers.device_registry.async_get_registry", - return_value=mock_coro(mock_registry), + return_value=mock_registry, ): - mock_hub.return_value.async_setup.return_value = mock_coro(True) + mock_hub.return_value.async_setup = CoroutineMock(return_value=True) mock_hub.return_value.serial_num = "12345678" mock_hub.return_value.model = "RB750" mock_hub.return_value.hostname = "mikrotik" @@ -55,7 +55,7 @@ async def test_hub_fail_setup(hass): entry.add_to_hass(hass) with patch.object(mikrotik, "MikrotikHub") as mock_hub: - mock_hub.return_value.async_setup.return_value = mock_coro(False) + mock_hub.return_value.async_setup = CoroutineMock(return_value=False) assert await mikrotik.async_setup_entry(hass, entry) is False assert mikrotik.DOMAIN not in hass.data @@ -67,10 +67,9 @@ async def test_unload_entry(hass): entry.add_to_hass(hass) with patch.object(mikrotik, "MikrotikHub") as mock_hub, patch( - "homeassistant.helpers.device_registry.async_get_registry", - return_value=mock_coro(Mock()), + "homeassistant.helpers.device_registry.async_get_registry", return_value=Mock(), ): - mock_hub.return_value.async_setup.return_value = mock_coro(True) + mock_hub.return_value.async_setup = CoroutineMock(return_value=True) mock_hub.return_value.serial_num = "12345678" mock_hub.return_value.model = "RB750" mock_hub.return_value.hostname = "mikrotik" diff --git a/tests/components/mqtt/test_init.py b/tests/components/mqtt/test_init.py index 290b70953af..ca5a89a6e63 100644 --- a/tests/components/mqtt/test_init.py +++ b/tests/components/mqtt/test_init.py @@ -3,8 +3,8 @@ from datetime import datetime, timedelta import json import ssl import unittest -from unittest import mock +from asynctest import CoroutineMock, MagicMock, call, mock_open, patch import pytest import voluptuous as vol @@ -31,7 +31,6 @@ from tests.common import ( async_mock_mqtt_component, fire_mqtt_message, get_test_home_assistant, - mock_coro, mock_device_registry, mock_mqtt_component, mock_registry, @@ -56,9 +55,9 @@ def entity_reg(hass): @pytest.fixture def mock_mqtt(): """Make sure connection is established.""" - with mock.patch("homeassistant.components.mqtt.MQTT") as mock_mqtt: - mock_mqtt.return_value.async_connect.return_value = mock_coro(True) - mock_mqtt.return_value.async_disconnect.return_value = mock_coro(True) + with patch("homeassistant.components.mqtt.MQTT") as mock_mqtt: + mock_mqtt.return_value.async_connect = CoroutineMock(return_value=True) + mock_mqtt.return_value.async_disconnect = CoroutineMock(return_value=True) yield mock_mqtt @@ -67,7 +66,7 @@ async def async_mock_mqtt_client(hass, config=None): if config is None: config = {mqtt.CONF_BROKER: "mock-broker"} - with mock.patch("paho.mqtt.client.Client") as mock_client: + with patch("paho.mqtt.client.Client") as mock_client: mock_client().connect.return_value = 0 mock_client().subscribe.return_value = (0, 0) mock_client().unsubscribe.return_value = (0, 0) @@ -583,12 +582,12 @@ class TestMQTTCallbacks(unittest.TestCase): # Fake that the client is connected self.hass.data["mqtt"].connected = True - calls_a = mock.MagicMock() + calls_a = MagicMock() mqtt.subscribe(self.hass, "test/state", calls_a) self.hass.block_till_done() assert calls_a.called - calls_b = mock.MagicMock() + calls_b = MagicMock() mqtt.subscribe(self.hass, "test/state", calls_b) self.hass.block_till_done() assert calls_b.called @@ -639,9 +638,9 @@ class TestMQTTCallbacks(unittest.TestCase): self.hass.block_till_done() expected = [ - mock.call("test/state", 2), - mock.call("test/state", 0), - mock.call("test/state", 1), + call("test/state", 2), + call("test/state", 0), + call("test/state", 1), ] assert self.hass.data["mqtt"]._mqttc.subscribe.mock_calls == expected @@ -653,7 +652,7 @@ class TestMQTTCallbacks(unittest.TestCase): self.hass.data["mqtt"]._mqtt_on_connect(None, None, None, 0) self.hass.block_till_done() - expected.append(mock.call("test/state", 1)) + expected.append(call("test/state", 1)) assert self.hass.data["mqtt"]._mqttc.subscribe.mock_calls == expected @@ -661,9 +660,9 @@ async def test_setup_embedded_starts_with_no_config(hass): """Test setting up embedded server with no config.""" client_config = ("localhost", 1883, "user", "pass", None, "3.1.1") - with mock.patch( + with patch( "homeassistant.components.mqtt.server.async_start", - return_value=mock_coro(return_value=(True, client_config)), + return_value=(True, client_config), ) as _start: await async_mock_mqtt_client(hass, {}) assert _start.call_count == 1 @@ -673,11 +672,10 @@ async def test_setup_embedded_with_embedded(hass): """Test setting up embedded server with no config.""" client_config = ("localhost", 1883, "user", "pass", None, "3.1.1") - with mock.patch( + with patch( "homeassistant.components.mqtt.server.async_start", - return_value=mock_coro(return_value=(True, client_config)), + return_value=(True, client_config), ) as _start: - _start.return_value = mock_coro(return_value=(True, client_config)) await async_mock_mqtt_client(hass, {"embedded": None}) assert _start.call_count == 1 @@ -686,7 +684,7 @@ async def test_setup_fails_if_no_connect_broker(hass): """Test for setup failure if connection to broker is missing.""" entry = MockConfigEntry(domain=mqtt.DOMAIN, data={mqtt.CONF_BROKER: "test-broker"}) - with mock.patch("paho.mqtt.client.Client") as mock_client: + with patch("paho.mqtt.client.Client") as mock_client: mock_client().connect = lambda *args: 1 assert not await mqtt.async_setup_entry(hass, entry) @@ -695,8 +693,8 @@ async def test_setup_raises_ConfigEntryNotReady_if_no_connect_broker(hass): """Test for setup failure if connection to broker is missing.""" entry = MockConfigEntry(domain=mqtt.DOMAIN, data={mqtt.CONF_BROKER: "test-broker"}) - with mock.patch("paho.mqtt.client.Client") as mock_client: - mock_client().connect = mock.Mock(side_effect=OSError("Connection error")) + with patch("paho.mqtt.client.Client") as mock_client: + mock_client().connect = MagicMock(side_effect=OSError("Connection error")) with pytest.raises(ConfigEntryNotReady): await mqtt.async_setup_entry(hass, entry) @@ -808,7 +806,7 @@ async def test_mqtt_subscribes_topics_on_connect(hass): mqtt.Subscription("still/pending", None, 1), ] - hass.add_job = mock.MagicMock() + hass.add_job = MagicMock() hass.data["mqtt"]._mqtt_on_connect(None, None, 0, 0) await hass.async_block_till_done() @@ -874,7 +872,7 @@ async def test_dump_service(hass): """Test that we can dump a topic.""" await async_mock_mqtt_component(hass) - mock_open = mock.mock_open() + mopen = mock_open() await hass.services.async_call( "mqtt", "dump", {"topic": "bla/#", "duration": 3}, blocking=True @@ -882,11 +880,11 @@ async def test_dump_service(hass): async_fire_mqtt_message(hass, "bla/1", "test1") async_fire_mqtt_message(hass, "bla/2", "test2") - with mock.patch("homeassistant.components.mqtt.open", mock_open): + with patch("homeassistant.components.mqtt.open", mopen): async_fire_time_changed(hass, utcnow() + timedelta(seconds=3)) await hass.async_block_till_done() - writes = mock_open.return_value.write.mock_calls + writes = mopen.return_value.write.mock_calls assert len(writes) == 2 assert writes[0][1][0] == "bla/1,test1\n" assert writes[1][1][0] == "bla/2,test2\n" @@ -1251,7 +1249,7 @@ async def test_debug_info_wildcard(hass, mqtt_mock): ] start_dt = datetime(2019, 1, 1, 0, 0, 0) - with mock.patch("homeassistant.util.dt.utcnow") as dt_utcnow: + with patch("homeassistant.util.dt.utcnow") as dt_utcnow: dt_utcnow.return_value = start_dt async_fire_mqtt_message(hass, "sensor/abc", "123") @@ -1293,7 +1291,7 @@ async def test_debug_info_filter_same(hass, mqtt_mock): dt1 = datetime(2019, 1, 1, 0, 0, 0) dt2 = datetime(2019, 1, 1, 0, 0, 1) - with mock.patch("homeassistant.util.dt.utcnow") as dt_utcnow: + with patch("homeassistant.util.dt.utcnow") as dt_utcnow: dt_utcnow.return_value = dt1 async_fire_mqtt_message(hass, "sensor/abc", "123") async_fire_mqtt_message(hass, "sensor/abc", "123") diff --git a/tests/components/mqtt/test_light.py b/tests/components/mqtt/test_light.py index 45473d6f448..205e7400eb3 100644 --- a/tests/components/mqtt/test_light.py +++ b/tests/components/mqtt/test_light.py @@ -153,8 +153,7 @@ light: payload_off: "off" """ -from unittest import mock -from unittest.mock import patch +from asynctest import call, patch from homeassistant.components import light, mqtt from homeassistant.components.mqtt.discovery import async_start @@ -188,7 +187,6 @@ from tests.common import ( MockConfigEntry, assert_setup_component, async_fire_mqtt_message, - mock_coro, ) from tests.components.light import common @@ -673,7 +671,7 @@ async def test_sending_mqtt_commands_and_optimistic(hass, mqtt_mock): ) with patch( "homeassistant.helpers.restore_state.RestoreEntity.async_get_last_state", - return_value=mock_coro(fake_state), + return_value=fake_state, ): with assert_setup_component(1, light.DOMAIN): assert await async_setup_component(hass, light.DOMAIN, config) @@ -716,12 +714,12 @@ async def test_sending_mqtt_commands_and_optimistic(hass, mqtt_mock): mqtt_mock.async_publish.assert_has_calls( [ - mock.call("test_light_rgb/set", "on", 2, False), - mock.call("test_light_rgb/rgb/set", "255,128,0", 2, False), - mock.call("test_light_rgb/brightness/set", 50, 2, False), - mock.call("test_light_rgb/hs/set", "359.0,78.0", 2, False), - mock.call("test_light_rgb/white_value/set", 80, 2, False), - mock.call("test_light_rgb/xy/set", "0.14,0.131", 2, False), + call("test_light_rgb/set", "on", 2, False), + call("test_light_rgb/rgb/set", "255,128,0", 2, False), + call("test_light_rgb/brightness/set", 50, 2, False), + call("test_light_rgb/hs/set", "359.0,78.0", 2, False), + call("test_light_rgb/white_value/set", 80, 2, False), + call("test_light_rgb/xy/set", "0.14,0.131", 2, False), ], any_order=True, ) @@ -760,8 +758,8 @@ async def test_sending_mqtt_rgb_command_with_template(hass, mqtt_mock): mqtt_mock.async_publish.assert_has_calls( [ - mock.call("test_light_rgb/set", "on", 0, False), - mock.call("test_light_rgb/rgb/set", "#ff803f", 0, False), + call("test_light_rgb/set", "on", 0, False), + call("test_light_rgb/rgb/set", "#ff803f", 0, False), ], any_order=True, ) @@ -795,8 +793,8 @@ async def test_sending_mqtt_color_temp_command_with_template(hass, mqtt_mock): mqtt_mock.async_publish.assert_has_calls( [ - mock.call("test_light_color_temp/set", "on", 0, False), - mock.call("test_light_color_temp/color_temp/set", "10", 0, False), + call("test_light_color_temp/set", "on", 0, False), + call("test_light_color_temp/color_temp/set", "10", 0, False), ], any_order=True, ) @@ -980,8 +978,8 @@ async def test_on_command_first(hass, mqtt_mock): # test_light/bright: 50 mqtt_mock.async_publish.assert_has_calls( [ - mock.call("test_light/set", "ON", 0, False), - mock.call("test_light/bright", 50, 0, False), + call("test_light/set", "ON", 0, False), + call("test_light/bright", 50, 0, False), ], any_order=True, ) @@ -1015,8 +1013,8 @@ async def test_on_command_last(hass, mqtt_mock): # test_light/set: 'ON' mqtt_mock.async_publish.assert_has_calls( [ - mock.call("test_light/bright", 50, 0, False), - mock.call("test_light/set", "ON", 0, False), + call("test_light/bright", 50, 0, False), + call("test_light/set", "ON", 0, False), ], any_order=True, ) @@ -1072,8 +1070,8 @@ async def test_on_command_brightness(hass, mqtt_mock): mqtt_mock.async_publish.assert_has_calls( [ - mock.call("test_light/rgb", "255,128,0", 0, False), - mock.call("test_light/bright", 50, 0, False), + call("test_light/rgb", "255,128,0", 0, False), + call("test_light/bright", 50, 0, False), ], any_order=True, ) @@ -1102,8 +1100,8 @@ async def test_on_command_rgb(hass, mqtt_mock): # test_light/set: 'ON' mqtt_mock.async_publish.assert_has_calls( [ - mock.call("test_light/rgb", "127,127,127", 0, False), - mock.call("test_light/set", "ON", 0, False), + call("test_light/rgb", "127,127,127", 0, False), + call("test_light/set", "ON", 0, False), ], any_order=True, ) @@ -1138,8 +1136,8 @@ async def test_on_command_rgb_template(hass, mqtt_mock): # test_light/set: 'ON' mqtt_mock.async_publish.assert_has_calls( [ - mock.call("test_light/rgb", "127/127/127", 0, False), - mock.call("test_light/set", "ON", 0, False), + call("test_light/rgb", "127/127/127", 0, False), + call("test_light/set", "ON", 0, False), ], any_order=True, ) @@ -1174,8 +1172,8 @@ async def test_effect(hass, mqtt_mock): # test_light/set: 'ON' mqtt_mock.async_publish.assert_has_calls( [ - mock.call("test_light/effect/set", "rainbow", 0, False), - mock.call("test_light/set", "ON", 0, False), + call("test_light/effect/set", "rainbow", 0, False), + call("test_light/set", "ON", 0, False), ], any_order=True, ) diff --git a/tests/components/mqtt/test_light_json.py b/tests/components/mqtt/test_light_json.py index 824085ea833..1e3ac34af89 100644 --- a/tests/components/mqtt/test_light_json.py +++ b/tests/components/mqtt/test_light_json.py @@ -88,8 +88,8 @@ light: brightness_scale: 99 """ import json -from unittest import mock -from unittest.mock import patch + +from asynctest import call, patch from homeassistant.components import light from homeassistant.const import ( @@ -123,7 +123,7 @@ from .test_common import ( help_test_update_with_json_attrs_not_dict, ) -from tests.common import async_fire_mqtt_message, mock_coro +from tests.common import async_fire_mqtt_message from tests.components.light import common DEFAULT_CONFIG = { @@ -323,7 +323,7 @@ async def test_sending_mqtt_commands_and_optimistic(hass, mqtt_mock): with patch( "homeassistant.helpers.restore_state.RestoreEntity.async_get_last_state", - return_value=mock_coro(fake_state), + return_value=fake_state, ): assert await async_setup_component( hass, @@ -397,7 +397,7 @@ async def test_sending_mqtt_commands_and_optimistic(hass, mqtt_mock): mqtt_mock.async_publish.assert_has_calls( [ - mock.call( + call( "test_light_rgb/set", JsonValidator( '{"state": "ON", "color": {"r": 0, "g": 123, "b": 255,' @@ -407,7 +407,7 @@ async def test_sending_mqtt_commands_and_optimistic(hass, mqtt_mock): 2, False, ), - mock.call( + call( "test_light_rgb/set", JsonValidator( '{"state": "ON", "color": {"r": 255, "g": 56, "b": 59,' @@ -417,7 +417,7 @@ async def test_sending_mqtt_commands_and_optimistic(hass, mqtt_mock): 2, False, ), - mock.call( + call( "test_light_rgb/set", JsonValidator( '{"state": "ON", "color": {"r": 255, "g": 128, "b": 0,' @@ -471,7 +471,7 @@ async def test_sending_hs_color(hass, mqtt_mock): mqtt_mock.async_publish.assert_has_calls( [ - mock.call( + call( "test_light_rgb/set", JsonValidator( '{"state": "ON", "color": {"h": 210.824, "s": 100.0},' @@ -480,7 +480,7 @@ async def test_sending_hs_color(hass, mqtt_mock): 0, False, ), - mock.call( + call( "test_light_rgb/set", JsonValidator( '{"state": "ON", "color": {"h": 359.0, "s": 78.0},' @@ -489,7 +489,7 @@ async def test_sending_hs_color(hass, mqtt_mock): 0, False, ), - mock.call( + call( "test_light_rgb/set", JsonValidator( '{"state": "ON", "color": {"h": 30.118, "s": 100.0},' @@ -532,19 +532,19 @@ async def test_sending_rgb_color_no_brightness(hass, mqtt_mock): mqtt_mock.async_publish.assert_has_calls( [ - mock.call( + call( "test_light_rgb/set", JsonValidator('{"state": "ON", "color": {"r": 0, "g": 24, "b": 50}}'), 0, False, ), - mock.call( + call( "test_light_rgb/set", JsonValidator('{"state": "ON", "color": {"r": 50, "g": 11, "b": 11}}'), 0, False, ), - mock.call( + call( "test_light_rgb/set", JsonValidator('{"state": "ON", "color": {"r": 255, "g": 128, "b": 0}}'), 0, @@ -585,7 +585,7 @@ async def test_sending_rgb_color_with_brightness(hass, mqtt_mock): mqtt_mock.async_publish.assert_has_calls( [ - mock.call( + call( "test_light_rgb/set", JsonValidator( '{"state": "ON", "color": {"r": 0, "g": 123, "b": 255},' @@ -594,7 +594,7 @@ async def test_sending_rgb_color_with_brightness(hass, mqtt_mock): 0, False, ), - mock.call( + call( "test_light_rgb/set", JsonValidator( '{"state": "ON", "color": {"r": 255, "g": 56, "b": 59},' @@ -603,7 +603,7 @@ async def test_sending_rgb_color_with_brightness(hass, mqtt_mock): 0, False, ), - mock.call( + call( "test_light_rgb/set", JsonValidator( '{"state": "ON", "color": {"r": 255, "g": 128, "b": 0},' @@ -647,7 +647,7 @@ async def test_sending_xy_color(hass, mqtt_mock): mqtt_mock.async_publish.assert_has_calls( [ - mock.call( + call( "test_light_rgb/set", JsonValidator( '{"state": "ON", "color": {"x": 0.14, "y": 0.131},' @@ -656,7 +656,7 @@ async def test_sending_xy_color(hass, mqtt_mock): 0, False, ), - mock.call( + call( "test_light_rgb/set", JsonValidator( '{"state": "ON", "color": {"x": 0.654, "y": 0.301},' @@ -665,7 +665,7 @@ async def test_sending_xy_color(hass, mqtt_mock): 0, False, ), - mock.call( + call( "test_light_rgb/set", JsonValidator( '{"state": "ON", "color": {"x": 0.611, "y": 0.375},' diff --git a/tests/components/mqtt/test_light_template.py b/tests/components/mqtt/test_light_template.py index 37617192dd5..20b5ecefd89 100644 --- a/tests/components/mqtt/test_light_template.py +++ b/tests/components/mqtt/test_light_template.py @@ -26,7 +26,7 @@ If your light doesn't support white value feature, omit `white_value_template`. If your light doesn't support RGB feature, omit `(red|green|blue)_template`. """ -from unittest.mock import patch +from asynctest import patch from homeassistant.components import light from homeassistant.const import ( @@ -60,7 +60,7 @@ from .test_common import ( help_test_update_with_json_attrs_not_dict, ) -from tests.common import assert_setup_component, async_fire_mqtt_message, mock_coro +from tests.common import assert_setup_component, async_fire_mqtt_message from tests.components.light import common DEFAULT_CONFIG = { @@ -287,7 +287,7 @@ async def test_sending_mqtt_commands_and_optimistic(hass, mqtt_mock): with patch( "homeassistant.helpers.restore_state.RestoreEntity.async_get_last_state", - return_value=mock_coro(fake_state), + return_value=fake_state, ): with assert_setup_component(1, light.DOMAIN): assert await async_setup_component( diff --git a/tests/components/notion/test_config_flow.py b/tests/components/notion/test_config_flow.py index 60ca4c07fb5..7d3ddb1cf4b 100644 --- a/tests/components/notion/test_config_flow.py +++ b/tests/components/notion/test_config_flow.py @@ -1,7 +1,6 @@ """Define tests for the Notion config flow.""" -from unittest.mock import patch - import aionotion +from asynctest import patch import pytest from homeassistant import data_entry_flow diff --git a/tests/components/openalpr_cloud/test_image_processing.py b/tests/components/openalpr_cloud/test_image_processing.py index 4aec9e68709..f5a246bcb4d 100644 --- a/tests/components/openalpr_cloud/test_image_processing.py +++ b/tests/components/openalpr_cloud/test_image_processing.py @@ -1,6 +1,7 @@ """The tests for the openalpr cloud platform.""" import asyncio -from unittest.mock import PropertyMock, patch + +from asynctest import PropertyMock, patch from homeassistant.components import camera, image_processing as ip from homeassistant.components.openalpr_cloud.image_processing import OPENALPR_API_URL diff --git a/tests/components/openalpr_local/test_image_processing.py b/tests/components/openalpr_local/test_image_processing.py index f28ee6f02d4..3f62c906974 100644 --- a/tests/components/openalpr_local/test_image_processing.py +++ b/tests/components/openalpr_local/test_image_processing.py @@ -1,5 +1,5 @@ """The tests for the openalpr local platform.""" -from unittest.mock import MagicMock, PropertyMock, patch +from asynctest import MagicMock, PropertyMock, patch import homeassistant.components.image_processing as ip from homeassistant.const import ATTR_ENTITY_PICTURE diff --git a/tests/components/opentherm_gw/test_config_flow.py b/tests/components/opentherm_gw/test_config_flow.py index f57ad20f5d5..2dec360eca0 100644 --- a/tests/components/opentherm_gw/test_config_flow.py +++ b/tests/components/opentherm_gw/test_config_flow.py @@ -1,7 +1,7 @@ """Test the Opentherm Gateway config flow.""" import asyncio -from unittest.mock import patch +from asynctest import patch from pyotgw.vars import OTGW_ABOUT from serial import SerialException diff --git a/tests/components/owntracks/test_config_flow.py b/tests/components/owntracks/test_config_flow.py index ae999afe305..514a559ac1d 100644 --- a/tests/components/owntracks/test_config_flow.py +++ b/tests/components/owntracks/test_config_flow.py @@ -1,6 +1,5 @@ """Tests for OwnTracks config flow.""" -from unittest.mock import Mock, patch - +from asynctest import Mock, patch import pytest from homeassistant import data_entry_flow diff --git a/tests/components/rainmachine/test_config_flow.py b/tests/components/rainmachine/test_config_flow.py index fca0f624a29..4351470cff5 100644 --- a/tests/components/rainmachine/test_config_flow.py +++ b/tests/components/rainmachine/test_config_flow.py @@ -1,6 +1,5 @@ """Define tests for the OpenUV config flow.""" -from unittest.mock import patch - +from asynctest import patch from regenmaschine.errors import RainMachineError from homeassistant import data_entry_flow diff --git a/tests/components/ring/test_config_flow.py b/tests/components/ring/test_config_flow.py index 5712106333f..421e8a26694 100644 --- a/tests/components/ring/test_config_flow.py +++ b/tests/components/ring/test_config_flow.py @@ -1,5 +1,5 @@ """Test the Ring config flow.""" -from unittest.mock import Mock, patch +from asynctest import Mock, patch from homeassistant import config_entries, setup from homeassistant.components.ring import DOMAIN diff --git a/tests/components/rmvtransport/test_sensor.py b/tests/components/rmvtransport/test_sensor.py index b34ba3d1229..58c9192de7d 100644 --- a/tests/components/rmvtransport/test_sensor.py +++ b/tests/components/rmvtransport/test_sensor.py @@ -1,11 +1,10 @@ """The tests for the rmvtransport platform.""" import datetime -from unittest.mock import patch + +from asynctest import patch from homeassistant.setup import async_setup_component -from tests.common import mock_coro - VALID_CONFIG_MINIMAL = { "sensor": {"platform": "rmvtransport", "next_departure": [{"station": "3000010"}]} } @@ -163,8 +162,7 @@ def get_no_departures_mock(): async def test_rmvtransport_min_config(hass): """Test minimal rmvtransport configuration.""" with patch( - "RMVtransport.RMVtransport.get_departures", - return_value=mock_coro(get_departures_mock()), + "RMVtransport.RMVtransport.get_departures", return_value=get_departures_mock(), ): assert await async_setup_component(hass, "sensor", VALID_CONFIG_MINIMAL) is True @@ -183,8 +181,7 @@ async def test_rmvtransport_min_config(hass): async def test_rmvtransport_name_config(hass): """Test custom name configuration.""" with patch( - "RMVtransport.RMVtransport.get_departures", - return_value=mock_coro(get_departures_mock()), + "RMVtransport.RMVtransport.get_departures", return_value=get_departures_mock(), ): assert await async_setup_component(hass, "sensor", VALID_CONFIG_NAME) @@ -195,8 +192,7 @@ async def test_rmvtransport_name_config(hass): async def test_rmvtransport_misc_config(hass): """Test misc configuration.""" with patch( - "RMVtransport.RMVtransport.get_departures", - return_value=mock_coro(get_departures_mock()), + "RMVtransport.RMVtransport.get_departures", return_value=get_departures_mock(), ): assert await async_setup_component(hass, "sensor", VALID_CONFIG_MISC) @@ -208,8 +204,7 @@ async def test_rmvtransport_misc_config(hass): async def test_rmvtransport_dest_config(hass): """Test destination configuration.""" with patch( - "RMVtransport.RMVtransport.get_departures", - return_value=mock_coro(get_departures_mock()), + "RMVtransport.RMVtransport.get_departures", return_value=get_departures_mock(), ): assert await async_setup_component(hass, "sensor", VALID_CONFIG_DEST) @@ -227,7 +222,7 @@ async def test_rmvtransport_no_departures(hass): """Test for no departures.""" with patch( "RMVtransport.RMVtransport.get_departures", - return_value=mock_coro(get_no_departures_mock()), + return_value=get_no_departures_mock(), ): assert await async_setup_component(hass, "sensor", VALID_CONFIG_MINIMAL) diff --git a/tests/components/sentry/test_config_flow.py b/tests/components/sentry/test_config_flow.py index 7ce34c13f53..22c4367ddf2 100644 --- a/tests/components/sentry/test_config_flow.py +++ b/tests/components/sentry/test_config_flow.py @@ -1,6 +1,5 @@ """Test the sentry config flow.""" -from unittest.mock import patch - +from asynctest import patch from sentry_sdk.utils import BadDsn from homeassistant import config_entries, setup diff --git a/tests/components/shell_command/test_init.py b/tests/components/shell_command/test_init.py index a5e8cb1d946..156741d8c9b 100644 --- a/tests/components/shell_command/test_init.py +++ b/tests/components/shell_command/test_init.py @@ -4,7 +4,8 @@ import os import tempfile from typing import Tuple import unittest -from unittest.mock import Mock, patch + +from asynctest import Mock, patch from homeassistant.components import shell_command from homeassistant.setup import setup_component diff --git a/tests/components/smhi/test_config_flow.py b/tests/components/smhi/test_config_flow.py index b983f8af487..6065c323b91 100644 --- a/tests/components/smhi/test_config_flow.py +++ b/tests/components/smhi/test_config_flow.py @@ -1,6 +1,5 @@ """Tests for SMHI config flow.""" -from unittest.mock import Mock, patch - +from asynctest import Mock, patch from smhi.smhi_lib import Smhi as SmhiApi, SmhiForecastException from homeassistant.components.smhi import config_flow diff --git a/tests/components/smhi/test_weather.py b/tests/components/smhi/test_weather.py index 3485a108d5b..357b670a38a 100644 --- a/tests/components/smhi/test_weather.py +++ b/tests/components/smhi/test_weather.py @@ -2,7 +2,8 @@ import asyncio from datetime import datetime import logging -from unittest.mock import Mock, patch + +from asynctest import Mock, patch from homeassistant.components.smhi import weather as weather_smhi from homeassistant.components.smhi.const import ATTR_SMHI_CLOUDINESS diff --git a/tests/components/solarlog/test_config_flow.py b/tests/components/solarlog/test_config_flow.py index 7828290560a..4d8c11074db 100644 --- a/tests/components/solarlog/test_config_flow.py +++ b/tests/components/solarlog/test_config_flow.py @@ -1,6 +1,5 @@ """Test the solarlog config flow.""" -from unittest.mock import patch - +from asynctest import patch import pytest from homeassistant import config_entries, data_entry_flow, setup diff --git a/tests/components/tesla/test_config_flow.py b/tests/components/tesla/test_config_flow.py index 8698fddbeab..24cdb375fde 100644 --- a/tests/components/tesla/test_config_flow.py +++ b/tests/components/tesla/test_config_flow.py @@ -1,6 +1,5 @@ """Test the Tesla config flow.""" -from unittest.mock import patch - +from asynctest import patch from teslajsonpy import TeslaException from homeassistant import config_entries, data_entry_flow, setup diff --git a/tests/components/tradfri/conftest.py b/tests/components/tradfri/conftest.py index d835c06f256..bb1a0e7cc64 100644 --- a/tests/components/tradfri/conftest.py +++ b/tests/components/tradfri/conftest.py @@ -1,10 +1,7 @@ """Common tradfri test fixtures.""" -from unittest.mock import patch - +from asynctest import patch import pytest -from tests.common import mock_coro - @pytest.fixture def mock_gateway_info(): @@ -19,5 +16,5 @@ def mock_gateway_info(): def mock_entry_setup(): """Mock entry setup.""" with patch("homeassistant.components.tradfri.async_setup_entry") as mock_setup: - mock_setup.return_value = mock_coro(True) + mock_setup.return_value = True yield mock_setup diff --git a/tests/components/tradfri/test_config_flow.py b/tests/components/tradfri/test_config_flow.py index 18fb55eda2f..2a4a831575a 100644 --- a/tests/components/tradfri/test_config_flow.py +++ b/tests/components/tradfri/test_config_flow.py @@ -1,12 +1,11 @@ """Test the Tradfri config flow.""" -from unittest.mock import patch - +from asynctest import patch import pytest from homeassistant import data_entry_flow from homeassistant.components.tradfri import config_flow -from tests.common import MockConfigEntry, mock_coro +from tests.common import MockConfigEntry @pytest.fixture @@ -20,9 +19,7 @@ def mock_auth(): async def test_user_connection_successful(hass, mock_auth, mock_entry_setup): """Test a successful connection.""" - mock_auth.side_effect = lambda hass, host, code: mock_coro( - {"host": host, "gateway_id": "bla"} - ) + mock_auth.side_effect = lambda hass, host, code: {"host": host, "gateway_id": "bla"} flow = await hass.config_entries.flow.async_init( "tradfri", context={"source": "user"} @@ -80,9 +77,7 @@ async def test_user_connection_bad_key(hass, mock_auth, mock_entry_setup): async def test_discovery_connection(hass, mock_auth, mock_entry_setup): """Test a connection via discovery.""" - mock_auth.side_effect = lambda hass, host, code: mock_coro( - {"host": host, "gateway_id": "bla"} - ) + mock_auth.side_effect = lambda hass, host, code: {"host": host, "gateway_id": "bla"} flow = await hass.config_entries.flow.async_init( "tradfri", context={"source": "zeroconf"}, data={"host": "123.123.123.123"} @@ -104,9 +99,12 @@ async def test_discovery_connection(hass, mock_auth, mock_entry_setup): async def test_import_connection(hass, mock_auth, mock_entry_setup): """Test a connection via import.""" - mock_auth.side_effect = lambda hass, host, code: mock_coro( - {"host": host, "gateway_id": "bla", "identity": "mock-iden", "key": "mock-key"} - ) + mock_auth.side_effect = lambda hass, host, code: { + "host": host, + "gateway_id": "bla", + "identity": "mock-iden", + "key": "mock-key", + } flow = await hass.config_entries.flow.async_init( "tradfri", @@ -132,9 +130,12 @@ async def test_import_connection(hass, mock_auth, mock_entry_setup): async def test_import_connection_no_groups(hass, mock_auth, mock_entry_setup): """Test a connection via import and no groups allowed.""" - mock_auth.side_effect = lambda hass, host, code: mock_coro( - {"host": host, "gateway_id": "bla", "identity": "mock-iden", "key": "mock-key"} - ) + mock_auth.side_effect = lambda hass, host, code: { + "host": host, + "gateway_id": "bla", + "identity": "mock-iden", + "key": "mock-key", + } flow = await hass.config_entries.flow.async_init( "tradfri", @@ -160,9 +161,12 @@ async def test_import_connection_no_groups(hass, mock_auth, mock_entry_setup): async def test_import_connection_legacy(hass, mock_gateway_info, mock_entry_setup): """Test a connection via import.""" - mock_gateway_info.side_effect = lambda hass, host, identity, key: mock_coro( - {"host": host, "identity": identity, "key": key, "gateway_id": "mock-gateway"} - ) + mock_gateway_info.side_effect = lambda hass, host, identity, key: { + "host": host, + "identity": identity, + "key": key, + "gateway_id": "mock-gateway", + } result = await hass.config_entries.flow.async_init( "tradfri", @@ -187,9 +191,12 @@ async def test_import_connection_legacy_no_groups( hass, mock_gateway_info, mock_entry_setup ): """Test a connection via legacy import and no groups allowed.""" - mock_gateway_info.side_effect = lambda hass, host, identity, key: mock_coro( - {"host": host, "identity": identity, "key": key, "gateway_id": "mock-gateway"} - ) + mock_gateway_info.side_effect = lambda hass, host, identity, key: { + "host": host, + "identity": identity, + "key": key, + "gateway_id": "mock-gateway", + } result = await hass.config_entries.flow.async_init( "tradfri", diff --git a/tests/components/tradfri/test_init.py b/tests/components/tradfri/test_init.py index cf9034df8d6..bdb45ee4306 100644 --- a/tests/components/tradfri/test_init.py +++ b/tests/components/tradfri/test_init.py @@ -1,9 +1,9 @@ """Tests for Tradfri setup.""" -from unittest.mock import patch +from asynctest import patch from homeassistant.setup import async_setup_component -from tests.common import MockConfigEntry, mock_coro +from tests.common import MockConfigEntry async def test_config_yaml_host_not_imported(hass): @@ -51,9 +51,12 @@ async def test_config_json_host_not_imported(hass): async def test_config_json_host_imported(hass, mock_gateway_info, mock_entry_setup): """Test that we import a configured host.""" - mock_gateway_info.side_effect = lambda hass, host, identity, key: mock_coro( - {"host": host, "identity": identity, "key": key, "gateway_id": "mock-gateway"} - ) + mock_gateway_info.side_effect = lambda hass, host, identity, key: { + "host": host, + "identity": identity, + "key": key, + "gateway_id": "mock-gateway", + } with patch( "homeassistant.components.tradfri.load_json", diff --git a/tests/components/upnp/test_init.py b/tests/components/upnp/test_init.py index a2df00aba2d..464ccc90675 100644 --- a/tests/components/upnp/test_init.py +++ b/tests/components/upnp/test_init.py @@ -1,7 +1,8 @@ """Test UPnP/IGD setup process.""" from ipaddress import IPv4Address -from unittest.mock import patch + +from asynctest import patch from homeassistant.components import upnp from homeassistant.components.upnp.device import Device diff --git a/tests/conftest.py b/tests/conftest.py index 700240bf627..ccc16c8ef82 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,8 +1,8 @@ """Set up some common test helper things.""" import functools import logging -from unittest.mock import patch +from asynctest import patch import pytest import requests_mock as _requests_mock @@ -26,7 +26,6 @@ from tests.common import ( # noqa: E402, isort:skip INSTANCES, MockUser, async_test_home_assistant, - mock_coro, mock_storage as mock_storage, ) from tests.test_util.aiohttp import mock_aiohttp_client # noqa: E402, isort:skip @@ -128,7 +127,7 @@ def mock_device_tracker_conf(): side_effect=mock_update_config, ), patch( "homeassistant.components.device_tracker.legacy.async_load_config", - side_effect=lambda *args: mock_coro(devices), + side_effect=lambda *args: devices, ): yield devices