Skip to content

Commit

Permalink
Improve Netatmo tests (#107902)
Browse files Browse the repository at this point in the history
* Improve Netatmo tests

* Improve Netatmo tests
  • Loading branch information
joostlek authored Jan 12, 2024
1 parent 68698ca commit 7bcfcfe
Show file tree
Hide file tree
Showing 11 changed files with 152 additions and 115 deletions.
25 changes: 8 additions & 17 deletions tests/components/netatmo/common.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,30 @@
"""Common methods used across tests for Netatmo."""
from contextlib import contextmanager
import json
from unittest.mock import patch
from typing import Any
from unittest.mock import AsyncMock, patch

from homeassistant.components.webhook import async_handle_webhook
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.util.aiohttp import MockRequest

from tests.common import load_fixture
from tests.test_util.aiohttp import AiohttpClientMockResponse

CLIENT_ID = "1234"
CLIENT_SECRET = "5678"

COMMON_RESPONSE = {
"user_id": "91763b24c43d3e344f424e8d",
"home_id": "91763b24c43d3e344f424e8b",
"home_name": "MYHOME",
"user": {"id": "91763b24c43d3e344f424e8b", "email": "[email protected]"},
}

TEST_TIME = 1559347200.0

FAKE_WEBHOOK_ACTIVATION = {
"push_type": "webhook_activation",
}

DEFAULT_PLATFORMS = ["camera", "climate", "light", "sensor"]


async def fake_post_request(*args, **kwargs):
async def fake_post_request(*args: Any, **kwargs: Any):
"""Return fake data."""
if "endpoint" not in kwargs:
return "{}"
Expand Down Expand Up @@ -62,7 +58,7 @@ async def fake_post_request(*args, **kwargs):
)


async def fake_get_image(*args, **kwargs):
async def fake_get_image(*args: Any, **kwargs: Any) -> bytes | str:
"""Return fake data."""
if "endpoint" not in kwargs:
return "{}"
Expand All @@ -73,12 +69,7 @@ async def fake_get_image(*args, **kwargs):
return b"test stream image bytes"


async def fake_post_request_no_data(*args, **kwargs):
"""Fake error during requesting backend data."""
return "{}"


async def simulate_webhook(hass, webhook_id, response):
async def simulate_webhook(hass: HomeAssistant, webhook_id: str, response) -> None:
"""Simulate a webhook event."""
request = MockRequest(
method="POST",
Expand All @@ -90,7 +81,7 @@ async def simulate_webhook(hass, webhook_id, response):


@contextmanager
def selected_platforms(platforms):
def selected_platforms(platforms: list[Platform]) -> AsyncMock:
"""Restrict loaded platforms to list given."""
with patch(
"homeassistant.components.netatmo.data_handler.PLATFORMS", platforms
Expand Down
6 changes: 4 additions & 2 deletions tests/components/netatmo/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
from pyatmo.const import ALL_SCOPES
import pytest

from homeassistant.core import HomeAssistant

from .common import fake_get_image, fake_post_request

from tests.common import MockConfigEntry


@pytest.fixture(name="config_entry")
def mock_config_entry_fixture(hass):
def mock_config_entry_fixture(hass: HomeAssistant) -> MockConfigEntry:
"""Mock a config entry."""
mock_entry = MockConfigEntry(
domain="netatmo",
Expand Down Expand Up @@ -55,7 +57,7 @@ def mock_config_entry_fixture(hass):


@pytest.fixture(name="netatmo_auth")
def netatmo_auth():
def netatmo_auth() -> AsyncMock:
"""Restrict loaded platforms to list given."""
with patch(
"homeassistant.components.netatmo.api.AsyncConfigEntryNetatmoAuth"
Expand Down
58 changes: 31 additions & 27 deletions tests/components/netatmo/test_camera.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"""The tests for Netatmo camera."""
from datetime import timedelta
from typing import Any
from unittest.mock import AsyncMock, patch

import pyatmo
import pytest
import requests_mock

from homeassistant.components import camera
from homeassistant.components.camera import STATE_STREAMING
Expand All @@ -14,21 +14,21 @@
SERVICE_SET_PERSON_AWAY,
SERVICE_SET_PERSONS_HOME,
)
from homeassistant.const import CONF_WEBHOOK_ID
from homeassistant.const import CONF_WEBHOOK_ID, Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.util import dt as dt_util

from .common import fake_post_request, selected_platforms, simulate_webhook

from tests.common import async_capture_events, async_fire_time_changed
from tests.common import MockConfigEntry, async_capture_events, async_fire_time_changed


async def test_setup_component_with_webhook(
hass: HomeAssistant, config_entry, netatmo_auth
hass: HomeAssistant, config_entry: MockConfigEntry, netatmo_auth: AsyncMock
) -> None:
"""Test setup with webhook."""
with selected_platforms(["camera"]):
with selected_platforms([Platform.CAMERA]):
assert await hass.config_entries.async_setup(config_entry.entry_id)

await hass.async_block_till_done()
Expand Down Expand Up @@ -134,10 +134,10 @@ async def test_setup_component_with_webhook(


async def test_camera_image_local(
hass: HomeAssistant, config_entry, requests_mock: requests_mock.Mocker, netatmo_auth
hass: HomeAssistant, config_entry: MockConfigEntry, netatmo_auth: AsyncMock
) -> None:
"""Test retrieval or local camera image."""
with selected_platforms(["camera"]):
with selected_platforms([Platform.CAMERA]):
assert await hass.config_entries.async_setup(config_entry.entry_id)

await hass.async_block_till_done()
Expand All @@ -162,10 +162,10 @@ async def test_camera_image_local(


async def test_camera_image_vpn(
hass: HomeAssistant, config_entry, requests_mock: requests_mock.Mocker, netatmo_auth
hass: HomeAssistant, config_entry: MockConfigEntry, netatmo_auth: AsyncMock
) -> None:
"""Test retrieval of remote camera image."""
with selected_platforms(["camera"]):
with selected_platforms([Platform.CAMERA]):
assert await hass.config_entries.async_setup(config_entry.entry_id)

await hass.async_block_till_done()
Expand All @@ -188,10 +188,10 @@ async def test_camera_image_vpn(


async def test_service_set_person_away(
hass: HomeAssistant, config_entry, netatmo_auth
hass: HomeAssistant, config_entry: MockConfigEntry, netatmo_auth: AsyncMock
) -> None:
"""Test service to set person as away."""
with selected_platforms(["camera"]):
with selected_platforms([Platform.CAMERA]):
assert await hass.config_entries.async_setup(config_entry.entry_id)

await hass.async_block_till_done()
Expand Down Expand Up @@ -227,10 +227,10 @@ async def test_service_set_person_away(


async def test_service_set_person_away_invalid_person(
hass: HomeAssistant, config_entry, netatmo_auth
hass: HomeAssistant, config_entry: MockConfigEntry, netatmo_auth: AsyncMock
) -> None:
"""Test service to set invalid person as away."""
with selected_platforms(["camera"]):
with selected_platforms([Platform.CAMERA]):
assert await hass.config_entries.async_setup(config_entry.entry_id)

await hass.async_block_till_done()
Expand All @@ -255,10 +255,10 @@ async def test_service_set_person_away_invalid_person(


async def test_service_set_persons_home_invalid_person(
hass: HomeAssistant, config_entry, netatmo_auth
hass: HomeAssistant, config_entry: MockConfigEntry, netatmo_auth: AsyncMock
) -> None:
"""Test service to set invalid persons as home."""
with selected_platforms(["camera"]):
with selected_platforms([Platform.CAMERA]):
assert await hass.config_entries.async_setup(config_entry.entry_id)

await hass.async_block_till_done()
Expand All @@ -283,10 +283,10 @@ async def test_service_set_persons_home_invalid_person(


async def test_service_set_persons_home(
hass: HomeAssistant, config_entry, netatmo_auth
hass: HomeAssistant, config_entry: MockConfigEntry, netatmo_auth: AsyncMock
) -> None:
"""Test service to set persons as home."""
with selected_platforms(["camera"]):
with selected_platforms([Platform.CAMERA]):
assert await hass.config_entries.async_setup(config_entry.entry_id)

await hass.async_block_till_done()
Expand All @@ -309,10 +309,10 @@ async def test_service_set_persons_home(


async def test_service_set_camera_light(
hass: HomeAssistant, config_entry, netatmo_auth
hass: HomeAssistant, config_entry: MockConfigEntry, netatmo_auth: AsyncMock
) -> None:
"""Test service to set the outdoor camera light mode."""
with selected_platforms(["camera"]):
with selected_platforms([Platform.CAMERA]):
assert await hass.config_entries.async_setup(config_entry.entry_id)

await hass.async_block_till_done()
Expand Down Expand Up @@ -341,10 +341,10 @@ async def test_service_set_camera_light(


async def test_service_set_camera_light_invalid_type(
hass: HomeAssistant, config_entry, netatmo_auth
hass: HomeAssistant, config_entry: MockConfigEntry, netatmo_auth: AsyncMock
) -> None:
"""Test service to set the indoor camera light mode."""
with selected_platforms(["camera"]):
with selected_platforms([Platform.CAMERA]):
assert await hass.config_entries.async_setup(config_entry.entry_id)

await hass.async_block_till_done()
Expand All @@ -371,11 +371,13 @@ async def test_service_set_camera_light_invalid_type(
assert "NACamera <Hall> does not have a floodlight" in excinfo.value.args[0]


async def test_camera_reconnect_webhook(hass: HomeAssistant, config_entry) -> None:
async def test_camera_reconnect_webhook(
hass: HomeAssistant, config_entry: MockConfigEntry
) -> None:
"""Test webhook event on camera reconnect."""
fake_post_hits = 0

async def fake_post(*args, **kwargs):
async def fake_post(*args: Any, **kwargs: Any):
"""Fake error during requesting backend data."""
nonlocal fake_post_hits
fake_post_hits += 1
Expand Down Expand Up @@ -427,7 +429,7 @@ async def fake_post(*args, **kwargs):


async def test_webhook_person_event(
hass: HomeAssistant, config_entry, netatmo_auth
hass: HomeAssistant, config_entry: MockConfigEntry, netatmo_auth: AsyncMock
) -> None:
"""Test that person events are handled."""
with selected_platforms(["camera"]):
Expand Down Expand Up @@ -465,7 +467,9 @@ async def test_webhook_person_event(
assert test_netatmo_event


async def test_setup_component_no_devices(hass: HomeAssistant, config_entry) -> None:
async def test_setup_component_no_devices(
hass: HomeAssistant, config_entry: MockConfigEntry
) -> None:
"""Test setup with no devices."""
fake_post_hits = 0

Expand Down Expand Up @@ -495,12 +499,12 @@ async def fake_post_no_data(*args, **kwargs):


async def test_camera_image_raises_exception(
hass: HomeAssistant, config_entry, requests_mock: requests_mock.Mocker
hass: HomeAssistant, config_entry: MockConfigEntry
) -> None:
"""Test setup with no devices."""
fake_post_hits = 0

async def fake_post(*args, **kwargs):
async def fake_post(*args: Any, **kwargs: Any):
"""Return fake data."""
nonlocal fake_post_hits
fake_post_hits += 1
Expand Down
Loading

0 comments on commit 7bcfcfe

Please sign in to comment.