Skip to content

Commit

Permalink
Replace aiohttp mock with patch in Advantage Air (#104932)
Browse files Browse the repository at this point in the history
Co-authored-by: J. Nick Koston <nick@koston.org>
  • Loading branch information
Bre77 and bdraco committed Dec 9, 2023
1 parent 6a69565 commit e1df1f9
Show file tree
Hide file tree
Showing 13 changed files with 253 additions and 493 deletions.
25 changes: 22 additions & 3 deletions tests/components/advantage_air/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
"""Tests for the Advantage Air component."""

from unittest.mock import AsyncMock, patch

from homeassistant.components.advantage_air.const import DOMAIN
from homeassistant.const import CONF_IP_ADDRESS, CONF_PORT

from tests.common import MockConfigEntry, load_fixture
from tests.common import MockConfigEntry, load_json_object_fixture

TEST_SYSTEM_DATA = load_fixture("advantage_air/getSystemData.json")
TEST_SET_RESPONSE = load_fixture("advantage_air/setAircon.json")
TEST_SYSTEM_DATA = load_json_object_fixture("getSystemData.json", DOMAIN)
TEST_SET_RESPONSE = None

USER_INPUT = {
CONF_IP_ADDRESS: "1.2.3.4",
Expand All @@ -25,6 +27,22 @@
)


def patch_get(return_value=TEST_SYSTEM_DATA, side_effect=None):
"""Patch the Advantage Air async_get method."""
return patch(
"homeassistant.components.advantage_air.advantage_air.async_get",
new=AsyncMock(return_value=return_value, side_effect=side_effect),
)


def patch_update(return_value=True, side_effect=None):
"""Patch the Advantage Air async_set method."""
return patch(
"homeassistant.components.advantage_air.advantage_air._endpoint.async_update",
new=AsyncMock(return_value=return_value, side_effect=side_effect),
)


async def add_mock_config(hass):
"""Create a fake Advantage Air Config Entry."""
entry = MockConfigEntry(
Expand All @@ -33,6 +51,7 @@ async def add_mock_config(hass):
unique_id="0123456",
data=USER_INPUT,
)

entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
Expand Down
20 changes: 20 additions & 0 deletions tests/components/advantage_air/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""Fixtures for advantage_air."""
from __future__ import annotations

import pytest

from . import patch_get, patch_update


@pytest.fixture
def mock_get():
"""Fixture to patch the Advantage Air async_get method."""
with patch_get() as mock_get:
yield mock_get


@pytest.fixture
def mock_update():
"""Fixture to patch the Advantage Air async_get method."""
with patch_update() as mock_get:
yield mock_get
26 changes: 7 additions & 19 deletions tests/components/advantage_air/test_binary_sensor.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,27 @@
"""Test the Advantage Air Binary Sensor Platform."""
from datetime import timedelta
from unittest.mock import AsyncMock

from homeassistant.config_entries import RELOAD_AFTER_UPDATE_DELAY
from homeassistant.const import STATE_OFF, STATE_ON
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from homeassistant.util import dt as dt_util

from . import (
TEST_SET_RESPONSE,
TEST_SET_URL,
TEST_SYSTEM_DATA,
TEST_SYSTEM_URL,
add_mock_config,
)
from . import add_mock_config

from tests.common import async_fire_time_changed
from tests.test_util.aiohttp import AiohttpClientMocker


async def test_binary_sensor_async_setup_entry(
hass: HomeAssistant,
aioclient_mock: AiohttpClientMocker,
entity_registry: er.EntityRegistry,
mock_get: AsyncMock,
) -> None:
"""Test binary sensor setup."""

aioclient_mock.get(
TEST_SYSTEM_URL,
text=TEST_SYSTEM_DATA,
)
aioclient_mock.get(
TEST_SET_URL,
text=TEST_SET_RESPONSE,
)
await add_mock_config(hass)

assert len(aioclient_mock.mock_calls) == 1

# Test First Air Filter
entity_id = "binary_sensor.myzone_filter"
state = hass.states.get(entity_id)
Expand Down Expand Up @@ -83,6 +67,7 @@ async def test_binary_sensor_async_setup_entry(

assert not hass.states.get(entity_id)

mock_get.reset_mock()
entity_registry.async_update_entity(entity_id=entity_id, disabled_by=None)
await hass.async_block_till_done()

Expand All @@ -91,6 +76,7 @@ async def test_binary_sensor_async_setup_entry(
dt_util.utcnow() + timedelta(seconds=RELOAD_AFTER_UPDATE_DELAY + 1),
)
await hass.async_block_till_done()
assert len(mock_get.mock_calls) == 2

state = hass.states.get(entity_id)
assert state
Expand All @@ -105,6 +91,7 @@ async def test_binary_sensor_async_setup_entry(

assert not hass.states.get(entity_id)

mock_get.reset_mock()
entity_registry.async_update_entity(entity_id=entity_id, disabled_by=None)
await hass.async_block_till_done()

Expand All @@ -113,6 +100,7 @@ async def test_binary_sensor_async_setup_entry(
dt_util.utcnow() + timedelta(seconds=RELOAD_AFTER_UPDATE_DELAY + 1),
)
await hass.async_block_till_done()
assert len(mock_get.mock_calls) == 2

state = hass.states.get(entity_id)
assert state
Expand Down
Loading

0 comments on commit e1df1f9

Please sign in to comment.