mirror of
https://github.com/home-assistant/core.git
synced 2025-07-09 14:27:07 +00:00
Use async_load_fixture in netatmo tests (#146013)
This commit is contained in:
parent
0cf2ee0bcb
commit
33b99b6627
@ -8,13 +8,14 @@ from unittest.mock import patch
|
||||
|
||||
from syrupy.assertion import SnapshotAssertion
|
||||
|
||||
from homeassistant.components.netatmo.const import DOMAIN
|
||||
from homeassistant.components.webhook import async_handle_webhook
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
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
|
||||
|
||||
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."""
|
||||
if "endpoint" not in kwargs:
|
||||
return "{}"
|
||||
@ -75,10 +76,12 @@ async def fake_post_request(*args: Any, **kwargs: Any):
|
||||
|
||||
elif endpoint == "homestatus":
|
||||
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:
|
||||
payload = json.loads(load_fixture(f"netatmo/{endpoint}.json"))
|
||||
payload = json.loads(await async_load_fixture(hass, f"{endpoint}.json", DOMAIN))
|
||||
|
||||
return AiohttpClientMockResponse(
|
||||
method="POST",
|
||||
|
@ -1,5 +1,7 @@
|
||||
"""Provide common Netatmo fixtures."""
|
||||
|
||||
from collections.abc import Generator
|
||||
from functools import partial
|
||||
from time import time
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
@ -87,13 +89,17 @@ def mock_config_entry_fixture(hass: HomeAssistant) -> MockConfigEntry:
|
||||
|
||||
|
||||
@pytest.fixture(name="netatmo_auth")
|
||||
def netatmo_auth() -> AsyncMock:
|
||||
def netatmo_auth(hass: HomeAssistant) -> Generator[None]:
|
||||
"""Restrict loaded platforms to list given."""
|
||||
with patch(
|
||||
"homeassistant.components.netatmo.api.AsyncConfigEntryNetatmoAuth"
|
||||
) as mock_auth:
|
||||
mock_auth.return_value.async_post_request.side_effect = fake_post_request
|
||||
mock_auth.return_value.async_post_api_request.side_effect = fake_post_request
|
||||
mock_auth.return_value.async_post_request.side_effect = partial(
|
||||
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_addwebhook.side_effect = AsyncMock()
|
||||
mock_auth.return_value.async_dropwebhook.side_effect = AsyncMock()
|
||||
|
@ -408,7 +408,7 @@ async def test_camera_reconnect_webhook(
|
||||
"""Fake error during requesting backend data."""
|
||||
nonlocal fake_post_hits
|
||||
fake_post_hits += 1
|
||||
return await fake_post_request(*args, **kwargs)
|
||||
return await fake_post_request(hass, *args, **kwargs)
|
||||
|
||||
with (
|
||||
patch(
|
||||
@ -507,7 +507,7 @@ async def test_setup_component_no_devices(
|
||||
"""Fake error during requesting backend data."""
|
||||
nonlocal fake_post_hits
|
||||
fake_post_hits += 1
|
||||
return await fake_post_request(*args, **kwargs)
|
||||
return await fake_post_request(hass, *args, **kwargs)
|
||||
|
||||
with (
|
||||
patch(
|
||||
@ -550,7 +550,7 @@ async def test_camera_image_raises_exception(
|
||||
if "snapshot_720.jpg" in endpoint:
|
||||
raise pyatmo.ApiError
|
||||
|
||||
return await fake_post_request(*args, **kwargs)
|
||||
return await fake_post_request(hass, *args, **kwargs)
|
||||
|
||||
with (
|
||||
patch(
|
||||
|
@ -1,5 +1,6 @@
|
||||
"""Test the Netatmo diagnostics."""
|
||||
|
||||
from functools import partial
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
from syrupy.assertion import SnapshotAssertion
|
||||
@ -33,7 +34,9 @@ async def test_entry_diagnostics(
|
||||
"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_dropwebhook.side_effect = AsyncMock()
|
||||
assert await async_setup_component(hass, "netatmo", {})
|
||||
|
@ -1,6 +1,7 @@
|
||||
"""The tests for Netatmo component."""
|
||||
|
||||
from datetime import timedelta
|
||||
from functools import partial
|
||||
from time import time
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
@ -68,7 +69,9 @@ async def test_setup_component(
|
||||
) as mock_impl,
|
||||
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_dropwebhook.side_effect = AsyncMock()
|
||||
assert await async_setup_component(hass, "netatmo", {})
|
||||
@ -101,7 +104,7 @@ async def test_setup_component_with_config(
|
||||
"""Fake error during requesting backend data."""
|
||||
nonlocal fake_post_hits
|
||||
fake_post_hits += 1
|
||||
return await fake_post_request(*args, **kwargs)
|
||||
return await fake_post_request(hass, *args, **kwargs)
|
||||
|
||||
with (
|
||||
patch(
|
||||
@ -184,7 +187,9 @@ async def test_setup_without_https(
|
||||
"homeassistant.components.netatmo.webhook_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"
|
||||
assert await async_setup_component(
|
||||
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",
|
||||
),
|
||||
):
|
||||
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(
|
||||
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",
|
||||
),
|
||||
):
|
||||
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_dropwebhook.side_effect = AsyncMock()
|
||||
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(
|
||||
"pyatmo.AbstractAsyncAuth.async_post_api_request",
|
||||
side_effect=fake_post_request,
|
||||
side_effect=partial(fake_post_request, hass),
|
||||
) as mock_post_api_request,
|
||||
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,
|
||||
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_dropwebhook.side_effect = AsyncMock()
|
||||
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"
|
||||
) 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_dropwebhook.side_effect = AsyncMock()
|
||||
mock_session.return_value.async_ensure_token_valid.side_effect = (
|
||||
|
Loading…
x
Reference in New Issue
Block a user