Use async_load_fixture in netatmo tests (#146013)

This commit is contained in:
epenet 2025-06-02 08:59:11 +02:00 committed by GitHub
parent 0cf2ee0bcb
commit 33b99b6627
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 44 additions and 19 deletions

View File

@ -8,13 +8,14 @@ from unittest.mock import patch
from syrupy.assertion import SnapshotAssertion from syrupy.assertion import SnapshotAssertion
from homeassistant.components.netatmo.const import DOMAIN
from homeassistant.components.webhook import async_handle_webhook from homeassistant.components.webhook import async_handle_webhook
from homeassistant.const import Platform from homeassistant.const import Platform
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
from homeassistant.util.aiohttp import MockRequest from homeassistant.util.aiohttp import MockRequest
from tests.common import MockConfigEntry, load_fixture from tests.common import MockConfigEntry, async_load_fixture
from tests.test_util.aiohttp import AiohttpClientMockResponse from tests.test_util.aiohttp import AiohttpClientMockResponse
COMMON_RESPONSE = { COMMON_RESPONSE = {
@ -53,7 +54,7 @@ async def snapshot_platform_entities(
) )
async def fake_post_request(*args: Any, **kwargs: Any): async def fake_post_request(hass: HomeAssistant, *args: Any, **kwargs: Any):
"""Return fake data.""" """Return fake data."""
if "endpoint" not in kwargs: if "endpoint" not in kwargs:
return "{}" return "{}"
@ -75,10 +76,12 @@ async def fake_post_request(*args: Any, **kwargs: Any):
elif endpoint == "homestatus": elif endpoint == "homestatus":
home_id = kwargs.get("params", {}).get("home_id") home_id = kwargs.get("params", {}).get("home_id")
payload = json.loads(load_fixture(f"netatmo/{endpoint}_{home_id}.json")) payload = json.loads(
await async_load_fixture(hass, f"{endpoint}_{home_id}.json", DOMAIN)
)
else: else:
payload = json.loads(load_fixture(f"netatmo/{endpoint}.json")) payload = json.loads(await async_load_fixture(hass, f"{endpoint}.json", DOMAIN))
return AiohttpClientMockResponse( return AiohttpClientMockResponse(
method="POST", method="POST",

View File

@ -1,5 +1,7 @@
"""Provide common Netatmo fixtures.""" """Provide common Netatmo fixtures."""
from collections.abc import Generator
from functools import partial
from time import time from time import time
from unittest.mock import AsyncMock, patch from unittest.mock import AsyncMock, patch
@ -87,13 +89,17 @@ def mock_config_entry_fixture(hass: HomeAssistant) -> MockConfigEntry:
@pytest.fixture(name="netatmo_auth") @pytest.fixture(name="netatmo_auth")
def netatmo_auth() -> AsyncMock: def netatmo_auth(hass: HomeAssistant) -> Generator[None]:
"""Restrict loaded platforms to list given.""" """Restrict loaded platforms to list given."""
with patch( with patch(
"homeassistant.components.netatmo.api.AsyncConfigEntryNetatmoAuth" "homeassistant.components.netatmo.api.AsyncConfigEntryNetatmoAuth"
) as mock_auth: ) as mock_auth:
mock_auth.return_value.async_post_request.side_effect = fake_post_request mock_auth.return_value.async_post_request.side_effect = partial(
mock_auth.return_value.async_post_api_request.side_effect = fake_post_request fake_post_request, hass
)
mock_auth.return_value.async_post_api_request.side_effect = partial(
fake_post_request, hass
)
mock_auth.return_value.async_get_image.side_effect = fake_get_image mock_auth.return_value.async_get_image.side_effect = fake_get_image
mock_auth.return_value.async_addwebhook.side_effect = AsyncMock() mock_auth.return_value.async_addwebhook.side_effect = AsyncMock()
mock_auth.return_value.async_dropwebhook.side_effect = AsyncMock() mock_auth.return_value.async_dropwebhook.side_effect = AsyncMock()

View File

@ -408,7 +408,7 @@ async def test_camera_reconnect_webhook(
"""Fake error during requesting backend data.""" """Fake error during requesting backend data."""
nonlocal fake_post_hits nonlocal fake_post_hits
fake_post_hits += 1 fake_post_hits += 1
return await fake_post_request(*args, **kwargs) return await fake_post_request(hass, *args, **kwargs)
with ( with (
patch( patch(
@ -507,7 +507,7 @@ async def test_setup_component_no_devices(
"""Fake error during requesting backend data.""" """Fake error during requesting backend data."""
nonlocal fake_post_hits nonlocal fake_post_hits
fake_post_hits += 1 fake_post_hits += 1
return await fake_post_request(*args, **kwargs) return await fake_post_request(hass, *args, **kwargs)
with ( with (
patch( patch(
@ -550,7 +550,7 @@ async def test_camera_image_raises_exception(
if "snapshot_720.jpg" in endpoint: if "snapshot_720.jpg" in endpoint:
raise pyatmo.ApiError raise pyatmo.ApiError
return await fake_post_request(*args, **kwargs) return await fake_post_request(hass, *args, **kwargs)
with ( with (
patch( patch(

View File

@ -1,5 +1,6 @@
"""Test the Netatmo diagnostics.""" """Test the Netatmo diagnostics."""
from functools import partial
from unittest.mock import AsyncMock, patch from unittest.mock import AsyncMock, patch
from syrupy.assertion import SnapshotAssertion from syrupy.assertion import SnapshotAssertion
@ -33,7 +34,9 @@ async def test_entry_diagnostics(
"homeassistant.components.netatmo.webhook_generate_url", "homeassistant.components.netatmo.webhook_generate_url",
), ),
): ):
mock_auth.return_value.async_post_api_request.side_effect = fake_post_request mock_auth.return_value.async_post_api_request.side_effect = partial(
fake_post_request, hass
)
mock_auth.return_value.async_addwebhook.side_effect = AsyncMock() mock_auth.return_value.async_addwebhook.side_effect = AsyncMock()
mock_auth.return_value.async_dropwebhook.side_effect = AsyncMock() mock_auth.return_value.async_dropwebhook.side_effect = AsyncMock()
assert await async_setup_component(hass, "netatmo", {}) assert await async_setup_component(hass, "netatmo", {})

View File

@ -1,6 +1,7 @@
"""The tests for Netatmo component.""" """The tests for Netatmo component."""
from datetime import timedelta from datetime import timedelta
from functools import partial
from time import time from time import time
from unittest.mock import AsyncMock, patch from unittest.mock import AsyncMock, patch
@ -68,7 +69,9 @@ async def test_setup_component(
) as mock_impl, ) as mock_impl,
patch("homeassistant.components.netatmo.webhook_generate_url") as mock_webhook, patch("homeassistant.components.netatmo.webhook_generate_url") as mock_webhook,
): ):
mock_auth.return_value.async_post_api_request.side_effect = fake_post_request mock_auth.return_value.async_post_api_request.side_effect = partial(
fake_post_request, hass
)
mock_auth.return_value.async_addwebhook.side_effect = AsyncMock() mock_auth.return_value.async_addwebhook.side_effect = AsyncMock()
mock_auth.return_value.async_dropwebhook.side_effect = AsyncMock() mock_auth.return_value.async_dropwebhook.side_effect = AsyncMock()
assert await async_setup_component(hass, "netatmo", {}) assert await async_setup_component(hass, "netatmo", {})
@ -101,7 +104,7 @@ async def test_setup_component_with_config(
"""Fake error during requesting backend data.""" """Fake error during requesting backend data."""
nonlocal fake_post_hits nonlocal fake_post_hits
fake_post_hits += 1 fake_post_hits += 1
return await fake_post_request(*args, **kwargs) return await fake_post_request(hass, *args, **kwargs)
with ( with (
patch( patch(
@ -184,7 +187,9 @@ async def test_setup_without_https(
"homeassistant.components.netatmo.webhook_generate_url" "homeassistant.components.netatmo.webhook_generate_url"
) as mock_async_generate_url, ) as mock_async_generate_url,
): ):
mock_auth.return_value.async_post_api_request.side_effect = fake_post_request mock_auth.return_value.async_post_api_request.side_effect = partial(
fake_post_request, hass
)
mock_async_generate_url.return_value = "http://example.com" mock_async_generate_url.return_value = "http://example.com"
assert await async_setup_component( assert await async_setup_component(
hass, "netatmo", {"netatmo": {"client_id": "123", "client_secret": "abc"}} hass, "netatmo", {"netatmo": {"client_id": "123", "client_secret": "abc"}}
@ -226,7 +231,9 @@ async def test_setup_with_cloud(
"homeassistant.components.netatmo.webhook_generate_url", "homeassistant.components.netatmo.webhook_generate_url",
), ),
): ):
mock_auth.return_value.async_post_api_request.side_effect = fake_post_request mock_auth.return_value.async_post_api_request.side_effect = partial(
fake_post_request, hass
)
assert await async_setup_component( assert await async_setup_component(
hass, "netatmo", {"netatmo": {"client_id": "123", "client_secret": "abc"}} hass, "netatmo", {"netatmo": {"client_id": "123", "client_secret": "abc"}}
) )
@ -294,7 +301,9 @@ async def test_setup_with_cloudhook(hass: HomeAssistant) -> None:
"homeassistant.components.netatmo.webhook_generate_url", "homeassistant.components.netatmo.webhook_generate_url",
), ),
): ):
mock_auth.return_value.async_post_api_request.side_effect = fake_post_request mock_auth.return_value.async_post_api_request.side_effect = partial(
fake_post_request, hass
)
mock_auth.return_value.async_addwebhook.side_effect = AsyncMock() mock_auth.return_value.async_addwebhook.side_effect = AsyncMock()
mock_auth.return_value.async_dropwebhook.side_effect = AsyncMock() mock_auth.return_value.async_dropwebhook.side_effect = AsyncMock()
assert await async_setup_component(hass, "netatmo", {}) assert await async_setup_component(hass, "netatmo", {})
@ -336,7 +345,7 @@ async def test_setup_component_with_delay(
patch("homeassistant.components.netatmo.webhook_generate_url") as mock_webhook, patch("homeassistant.components.netatmo.webhook_generate_url") as mock_webhook,
patch( patch(
"pyatmo.AbstractAsyncAuth.async_post_api_request", "pyatmo.AbstractAsyncAuth.async_post_api_request",
side_effect=fake_post_request, side_effect=partial(fake_post_request, hass),
) as mock_post_api_request, ) as mock_post_api_request,
patch("homeassistant.components.netatmo.data_handler.PLATFORMS", ["light"]), patch("homeassistant.components.netatmo.data_handler.PLATFORMS", ["light"]),
): ):
@ -405,7 +414,9 @@ async def test_setup_component_invalid_token_scope(hass: HomeAssistant) -> None:
) as mock_impl, ) as mock_impl,
patch("homeassistant.components.netatmo.webhook_generate_url") as mock_webhook, patch("homeassistant.components.netatmo.webhook_generate_url") as mock_webhook,
): ):
mock_auth.return_value.async_post_api_request.side_effect = fake_post_request mock_auth.return_value.async_post_api_request.side_effect = partial(
fake_post_request, hass
)
mock_auth.return_value.async_addwebhook.side_effect = AsyncMock() mock_auth.return_value.async_addwebhook.side_effect = AsyncMock()
mock_auth.return_value.async_dropwebhook.side_effect = AsyncMock() mock_auth.return_value.async_dropwebhook.side_effect = AsyncMock()
assert await async_setup_component(hass, "netatmo", {}) assert await async_setup_component(hass, "netatmo", {})
@ -455,7 +466,9 @@ async def test_setup_component_invalid_token(
"homeassistant.helpers.config_entry_oauth2_flow.OAuth2Session" "homeassistant.helpers.config_entry_oauth2_flow.OAuth2Session"
) as mock_session, ) as mock_session,
): ):
mock_auth.return_value.async_post_api_request.side_effect = fake_post_request mock_auth.return_value.async_post_api_request.side_effect = partial(
fake_post_request, hass
)
mock_auth.return_value.async_addwebhook.side_effect = AsyncMock() mock_auth.return_value.async_addwebhook.side_effect = AsyncMock()
mock_auth.return_value.async_dropwebhook.side_effect = AsyncMock() mock_auth.return_value.async_dropwebhook.side_effect = AsyncMock()
mock_session.return_value.async_ensure_token_valid.side_effect = ( mock_session.return_value.async_ensure_token_valid.side_effect = (