Skip to content

Commit dfd86d5

Browse files
authored
Convert test fixtures to async (#142052)
1 parent 93162f6 commit dfd86d5

File tree

24 files changed

+158
-194
lines changed

24 files changed

+158
-194
lines changed

tests/auth/providers/test_homeassistant.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@
1919

2020

2121
@pytest.fixture
22-
def data(hass: HomeAssistant) -> hass_auth.Data:
22+
async def data(hass: HomeAssistant) -> hass_auth.Data:
2323
"""Create a loaded data class."""
2424
data = hass_auth.Data(hass)
25-
hass.loop.run_until_complete(data.async_load())
25+
await data.async_load()
2626
return data
2727

2828

2929
@pytest.fixture
30-
def legacy_data(hass: HomeAssistant) -> hass_auth.Data:
30+
async def legacy_data(hass: HomeAssistant) -> hass_auth.Data:
3131
"""Create a loaded legacy data class."""
3232
data = hass_auth.Data(hass)
33-
hass.loop.run_until_complete(data.async_load())
33+
await data.async_load()
3434
data.is_legacy = True
3535
return data
3636

tests/components/api/test_init.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222

2323

2424
@pytest.fixture
25-
def mock_api_client(
25+
async def mock_api_client(
2626
hass: HomeAssistant, hass_client: ClientSessionGenerator
2727
) -> TestClient:
2828
"""Start the Home Assistant HTTP component and return admin API client."""
29-
hass.loop.run_until_complete(async_setup_component(hass, "api", {}))
30-
return hass.loop.run_until_complete(hass_client())
29+
await async_setup_component(hass, "api", {})
30+
return await hass_client()
3131

3232

3333
async def test_api_list_state_entities(

tests/components/cloud/conftest.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,9 @@ def mock_user_data() -> Generator[MagicMock]:
218218

219219

220220
@pytest.fixture
221-
def mock_cloud_fixture(hass: HomeAssistant) -> CloudPreferences:
221+
async def mock_cloud_fixture(hass: HomeAssistant) -> CloudPreferences:
222222
"""Fixture for cloud component."""
223-
hass.loop.run_until_complete(mock_cloud(hass))
223+
await mock_cloud(hass)
224224
return mock_cloud_prefs(hass, {})
225225

226226

tests/components/device_tracker/test_device_trigger.py

+12-14
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,19 @@ def stub_blueprint_populate_autouse(stub_blueprint_populate: None) -> None:
3333

3434

3535
@pytest.fixture(autouse=True)
36-
def setup_zone(hass: HomeAssistant) -> None:
36+
async def setup_zone(hass: HomeAssistant) -> None:
3737
"""Create test zone."""
38-
hass.loop.run_until_complete(
39-
async_setup_component(
40-
hass,
41-
zone.DOMAIN,
42-
{
43-
"zone": {
44-
"name": "test",
45-
"latitude": HOME_LATITUDE,
46-
"longitude": HOME_LONGITUDE,
47-
"radius": 250,
48-
}
49-
},
50-
)
38+
await async_setup_component(
39+
hass,
40+
zone.DOMAIN,
41+
{
42+
"zone": {
43+
"name": "test",
44+
"latitude": HOME_LATITUDE,
45+
"longitude": HOME_LONGITUDE,
46+
"radius": 250,
47+
}
48+
},
5149
)
5250

5351

tests/components/duckdns/test_init.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,16 @@ async def async_set_txt(hass: HomeAssistant, txt: str | None) -> None:
3131

3232

3333
@pytest.fixture
34-
def setup_duckdns(hass: HomeAssistant, aioclient_mock: AiohttpClientMocker) -> None:
34+
async def setup_duckdns(
35+
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
36+
) -> None:
3537
"""Fixture that sets up DuckDNS."""
3638
aioclient_mock.get(
3739
duckdns.UPDATE_URL, params={"domains": DOMAIN, "token": TOKEN}, text="OK"
3840
)
3941

40-
hass.loop.run_until_complete(
41-
async_setup_component(
42-
hass, duckdns.DOMAIN, {"duckdns": {"domain": DOMAIN, "access_token": TOKEN}}
43-
)
42+
await async_setup_component(
43+
hass, duckdns.DOMAIN, {"duckdns": {"domain": DOMAIN, "access_token": TOKEN}}
4444
)
4545

4646

tests/components/freedns/test_init.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,25 @@
1616

1717

1818
@pytest.fixture
19-
def setup_freedns(hass: HomeAssistant, aioclient_mock: AiohttpClientMocker) -> None:
19+
async def setup_freedns(
20+
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
21+
) -> None:
2022
"""Fixture that sets up FreeDNS."""
2123
params = {}
2224
params[ACCESS_TOKEN] = ""
2325
aioclient_mock.get(
2426
UPDATE_URL, params=params, text="Successfully updated 1 domains."
2527
)
2628

27-
hass.loop.run_until_complete(
28-
async_setup_component(
29-
hass,
30-
freedns.DOMAIN,
31-
{
32-
freedns.DOMAIN: {
33-
"access_token": ACCESS_TOKEN,
34-
"scan_interval": UPDATE_INTERVAL,
35-
}
36-
},
37-
)
29+
await async_setup_component(
30+
hass,
31+
freedns.DOMAIN,
32+
{
33+
freedns.DOMAIN: {
34+
"access_token": ACCESS_TOKEN,
35+
"scan_interval": UPDATE_INTERVAL,
36+
}
37+
},
3838
)
3939

4040

tests/components/frontend/test_storage.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313

1414

1515
@pytest.fixture(autouse=True)
16-
def setup_frontend(hass: HomeAssistant) -> None:
16+
async def setup_frontend(hass: HomeAssistant) -> None:
1717
"""Fixture to setup the frontend."""
18-
hass.loop.run_until_complete(async_setup_component(hass, "frontend", {}))
18+
await async_setup_component(hass, "frontend", {})
1919

2020

2121
async def test_get_user_data_empty(

tests/components/geo_location/test_trigger.py

+12-14
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,20 @@ def calls(hass: HomeAssistant) -> list[ServiceCall]:
2929

3030

3131
@pytest.fixture(autouse=True)
32-
def setup_comp(hass: HomeAssistant) -> None:
32+
async def setup_comp(hass: HomeAssistant) -> None:
3333
"""Initialize components."""
3434
mock_component(hass, "group")
35-
hass.loop.run_until_complete(
36-
async_setup_component(
37-
hass,
38-
zone.DOMAIN,
39-
{
40-
"zone": {
41-
"name": "test",
42-
"latitude": 32.880837,
43-
"longitude": -117.237561,
44-
"radius": 250,
45-
}
46-
},
47-
)
35+
await async_setup_component(
36+
hass,
37+
zone.DOMAIN,
38+
{
39+
"zone": {
40+
"name": "test",
41+
"latitude": 32.880837,
42+
"longitude": -117.237561,
43+
"radius": 250,
44+
}
45+
},
4846
)
4947

5048

tests/components/google_assistant/test_google_assistant.py

+19-28
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""The tests for the Google Assistant component."""
22

3-
from asyncio import AbstractEventLoop
43
from http import HTTPStatus
54
import json
65
from unittest.mock import patch
@@ -38,32 +37,28 @@ def auth_header(hass_access_token: str) -> dict[str, str]:
3837

3938

4039
@pytest.fixture
41-
def assistant_client(
42-
event_loop: AbstractEventLoop,
40+
async def assistant_client(
4341
hass: core.HomeAssistant,
4442
hass_client_no_auth: ClientSessionGenerator,
4543
) -> TestClient:
4644
"""Create web client for the Google Assistant API."""
47-
loop = event_loop
48-
loop.run_until_complete(
49-
setup.async_setup_component(
50-
hass,
51-
"google_assistant",
52-
{
53-
"google_assistant": {
54-
"project_id": PROJECT_ID,
55-
"entity_config": {
56-
"light.ceiling_lights": {
57-
"aliases": ["top lights", "ceiling lights"],
58-
"name": "Roof Lights",
59-
}
60-
},
61-
}
62-
},
63-
)
45+
await setup.async_setup_component(
46+
hass,
47+
"google_assistant",
48+
{
49+
"google_assistant": {
50+
"project_id": PROJECT_ID,
51+
"entity_config": {
52+
"light.ceiling_lights": {
53+
"aliases": ["top lights", "ceiling lights"],
54+
"name": "Roof Lights",
55+
}
56+
},
57+
}
58+
},
6459
)
6560

66-
return loop.run_until_complete(hass_client_no_auth())
61+
return await hass_client_no_auth()
6762

6863

6964
@pytest.fixture(autouse=True)
@@ -87,16 +82,12 @@ async def wanted_platforms_only() -> None:
8782

8883

8984
@pytest.fixture
90-
def hass_fixture(
91-
event_loop: AbstractEventLoop, hass: core.HomeAssistant
92-
) -> core.HomeAssistant:
85+
async def hass_fixture(hass: core.HomeAssistant) -> core.HomeAssistant:
9386
"""Set up a Home Assistant instance for these tests."""
94-
loop = event_loop
95-
9687
# We need to do this to get access to homeassistant/turn_(on,off)
97-
loop.run_until_complete(setup.async_setup_component(hass, core.DOMAIN, {}))
88+
await setup.async_setup_component(hass, core.DOMAIN, {})
9889

99-
loop.run_until_complete(setup.async_setup_component(hass, "demo", {}))
90+
await setup.async_setup_component(hass, "demo", {})
10091

10192
return hass
10293

tests/components/hassio/conftest.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def hassio_env(supervisor_is_connected: AsyncMock) -> Generator[None]:
4646

4747

4848
@pytest.fixture
49-
def hassio_stubs(
49+
async def hassio_stubs(
5050
hassio_env: None,
5151
hass: HomeAssistant,
5252
hass_client: ClientSessionGenerator,
@@ -75,27 +75,27 @@ def hassio_stubs(
7575
"homeassistant.components.hassio.issues.SupervisorIssues.setup",
7676
),
7777
):
78-
hass.loop.run_until_complete(async_setup_component(hass, "hassio", {}))
78+
await async_setup_component(hass, "hassio", {})
7979

8080
return hass_api.call_args[0][1]
8181

8282

8383
@pytest.fixture
84-
def hassio_client(
84+
async def hassio_client(
8585
hassio_stubs: RefreshToken, hass: HomeAssistant, hass_client: ClientSessionGenerator
8686
) -> TestClient:
8787
"""Return a Hass.io HTTP client."""
88-
return hass.loop.run_until_complete(hass_client())
88+
return await hass_client()
8989

9090

9191
@pytest.fixture
92-
def hassio_noauth_client(
92+
async def hassio_noauth_client(
9393
hassio_stubs: RefreshToken,
9494
hass: HomeAssistant,
9595
aiohttp_client: ClientSessionGenerator,
9696
) -> TestClient:
9797
"""Return a Hass.io HTTP client without auth."""
98-
return hass.loop.run_until_complete(aiohttp_client(hass.http.app))
98+
return await aiohttp_client(hass.http.app)
9999

100100

101101
@pytest.fixture

tests/components/http/test_cors.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""Test cors for the HTTP component."""
22

3-
from asyncio import AbstractEventLoop
43
from http import HTTPStatus
54
from pathlib import Path
65
from unittest.mock import patch
@@ -55,14 +54,12 @@ async def mock_handler(request):
5554

5655

5756
@pytest.fixture
58-
def client(
59-
event_loop: AbstractEventLoop, aiohttp_client: ClientSessionGenerator
60-
) -> TestClient:
57+
async def client(aiohttp_client: ClientSessionGenerator) -> TestClient:
6158
"""Fixture to set up a web.Application."""
6259
app = web.Application()
6360
setup_cors(app, [TRUSTED_ORIGIN])
6461
app[KEY_ALLOW_CONFIGURED_CORS](app.router.add_get("/", mock_handler))
65-
return event_loop.run_until_complete(aiohttp_client(app))
62+
return await aiohttp_client(app)
6663

6764

6865
async def test_cors_requests(client) -> None:

tests/components/meraki/test_device_tracker.py

+15-22
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""The tests the for Meraki device tracker."""
22

3-
from asyncio import AbstractEventLoop
43
from http import HTTPStatus
54
import json
65

@@ -22,31 +21,25 @@
2221

2322

2423
@pytest.fixture
25-
def meraki_client(
26-
event_loop: AbstractEventLoop,
24+
async def meraki_client(
2725
hass: HomeAssistant,
2826
hass_client: ClientSessionGenerator,
2927
) -> TestClient:
3028
"""Meraki mock client."""
31-
loop = event_loop
32-
33-
async def setup_and_wait():
34-
result = await async_setup_component(
35-
hass,
36-
device_tracker.DOMAIN,
37-
{
38-
device_tracker.DOMAIN: {
39-
CONF_PLATFORM: "meraki",
40-
CONF_VALIDATOR: "validator",
41-
CONF_SECRET: "secret",
42-
}
43-
},
44-
)
45-
await hass.async_block_till_done()
46-
return result
47-
48-
assert loop.run_until_complete(setup_and_wait())
49-
return loop.run_until_complete(hass_client())
29+
assert await async_setup_component(
30+
hass,
31+
device_tracker.DOMAIN,
32+
{
33+
device_tracker.DOMAIN: {
34+
CONF_PLATFORM: "meraki",
35+
CONF_VALIDATOR: "validator",
36+
CONF_SECRET: "secret",
37+
}
38+
},
39+
)
40+
await hass.async_block_till_done()
41+
42+
return await hass_client()
5043

5144

5245
async def test_invalid_or_missing_data(

0 commit comments

Comments
 (0)