Replace aiohttp mock with patch in Advantage Air (#104932)

Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
Brett Adams 2023-12-09 11:15:57 +10:00 committed by GitHub
parent 6a6956573f
commit e1df1f9ffe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 252 additions and 492 deletions

View file

@ -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",
@ -25,6 +27,22 @@ TEST_SET_THING_URL = (
)
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(
@ -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()

View file

@ -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

View file

@ -1,5 +1,6 @@
"""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
@ -7,37 +8,20 @@ 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)
@ -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()
@ -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
@ -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()
@ -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

View file

@ -1,20 +1,10 @@
"""Test the Advantage Air Climate Platform."""
from json import loads
from unittest.mock import AsyncMock
from advantage_air import ApiError
import pytest
from homeassistant.components.advantage_air.climate import (
ADVANTAGE_AIR_COOL_TARGET,
ADVANTAGE_AIR_HEAT_TARGET,
HASS_FAN_MODES,
HASS_HVAC_MODES,
)
from homeassistant.components.advantage_air.const import (
ADVANTAGE_AIR_STATE_CLOSE,
ADVANTAGE_AIR_STATE_OFF,
ADVANTAGE_AIR_STATE_ON,
ADVANTAGE_AIR_STATE_OPEN,
)
from homeassistant.components.climate import (
ATTR_CURRENT_TEMPERATURE,
ATTR_FAN_MODE,
@ -37,35 +27,20 @@ from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import entity_registry as er
from . import (
TEST_SET_RESPONSE,
TEST_SET_URL,
TEST_SYSTEM_DATA,
TEST_SYSTEM_URL,
add_mock_config,
)
from tests.test_util.aiohttp import AiohttpClientMocker
from . import add_mock_config, patch_update
async def test_climate_async_setup_entry(
async def test_climate_myzone_main(
hass: HomeAssistant,
aioclient_mock: AiohttpClientMocker,
entity_registry: er.EntityRegistry,
mock_get: AsyncMock,
mock_update: AsyncMock,
) -> None:
"""Test climate platform."""
"""Test climate platform main entity."""
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)
# Test MyZone Climate Entity
# Test MyZone main climate entity
entity_id = "climate.myzone"
state = hass.states.get(entity_id)
assert state
@ -80,19 +55,24 @@ async def test_climate_async_setup_entry(
assert entry.unique_id == "uniqueid-ac1"
# Test setting HVAC Mode
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_HVAC_MODE,
{ATTR_ENTITY_ID: [entity_id], ATTR_HVAC_MODE: HVACMode.COOL},
blocking=True,
)
mock_update.assert_called_once()
mock_update.reset_mock()
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_HVAC_MODE,
{ATTR_ENTITY_ID: [entity_id], ATTR_HVAC_MODE: HVACMode.FAN_ONLY},
blocking=True,
)
assert aioclient_mock.mock_calls[-2][0] == "GET"
assert aioclient_mock.mock_calls[-2][1].path == "/setAircon"
data = loads(aioclient_mock.mock_calls[-2][1].query["json"])
assert data["ac1"]["info"]["state"] == ADVANTAGE_AIR_STATE_ON
assert data["ac1"]["info"]["mode"] == HASS_HVAC_MODES[HVACMode.FAN_ONLY]
assert aioclient_mock.mock_calls[-1][0] == "GET"
assert aioclient_mock.mock_calls[-1][1].path == "/getSystemData"
mock_update.assert_called_once()
mock_update.reset_mock()
# Test Turning Off with HVAC Mode
await hass.services.async_call(
@ -101,26 +81,17 @@ async def test_climate_async_setup_entry(
{ATTR_ENTITY_ID: [entity_id], ATTR_HVAC_MODE: HVACMode.OFF},
blocking=True,
)
assert aioclient_mock.mock_calls[-2][0] == "GET"
assert aioclient_mock.mock_calls[-2][1].path == "/setAircon"
data = loads(aioclient_mock.mock_calls[-2][1].query["json"])
assert data["ac1"]["info"]["state"] == ADVANTAGE_AIR_STATE_OFF
assert aioclient_mock.mock_calls[-1][0] == "GET"
assert aioclient_mock.mock_calls[-1][1].path == "/getSystemData"
mock_update.assert_called_once()
mock_update.reset_mock()
# Test changing Fan Mode
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_FAN_MODE,
{ATTR_ENTITY_ID: [entity_id], ATTR_FAN_MODE: FAN_LOW},
blocking=True,
)
assert aioclient_mock.mock_calls[-2][0] == "GET"
assert aioclient_mock.mock_calls[-2][1].path == "/setAircon"
data = loads(aioclient_mock.mock_calls[-2][1].query["json"])
assert data["ac1"]["info"]["fan"] == HASS_FAN_MODES[FAN_LOW]
assert aioclient_mock.mock_calls[-1][0] == "GET"
assert aioclient_mock.mock_calls[-1][1].path == "/getSystemData"
mock_update.assert_called_once()
mock_update.reset_mock()
# Test changing Temperature
await hass.services.async_call(
@ -129,12 +100,8 @@ async def test_climate_async_setup_entry(
{ATTR_ENTITY_ID: [entity_id], ATTR_TEMPERATURE: 25},
blocking=True,
)
assert aioclient_mock.mock_calls[-2][0] == "GET"
assert aioclient_mock.mock_calls[-2][1].path == "/setAircon"
data = loads(aioclient_mock.mock_calls[-2][1].query["json"])
assert data["ac1"]["info"]["setTemp"] == 25
assert aioclient_mock.mock_calls[-1][0] == "GET"
assert aioclient_mock.mock_calls[-1][1].path == "/getSystemData"
mock_update.assert_called_once()
mock_update.reset_mock()
# Test Turning On
await hass.services.async_call(
@ -143,12 +110,8 @@ async def test_climate_async_setup_entry(
{ATTR_ENTITY_ID: [entity_id]},
blocking=True,
)
assert aioclient_mock.mock_calls[-2][0] == "GET"
assert aioclient_mock.mock_calls[-2][1].path == "/setAircon"
data = loads(aioclient_mock.mock_calls[-2][1].query["json"])
assert data["ac1"]["info"]["state"] == ADVANTAGE_AIR_STATE_OFF
assert aioclient_mock.mock_calls[-1][0] == "GET"
assert aioclient_mock.mock_calls[-1][1].path == "/getSystemData"
mock_update.assert_called_once()
mock_update.reset_mock()
# Test Turning Off
await hass.services.async_call(
@ -157,12 +120,19 @@ async def test_climate_async_setup_entry(
{ATTR_ENTITY_ID: [entity_id]},
blocking=True,
)
assert aioclient_mock.mock_calls[-2][0] == "GET"
assert aioclient_mock.mock_calls[-2][1].path == "/setAircon"
data = loads(aioclient_mock.mock_calls[-2][1].query["json"])
assert data["ac1"]["info"]["state"] == ADVANTAGE_AIR_STATE_ON
assert aioclient_mock.mock_calls[-1][0] == "GET"
assert aioclient_mock.mock_calls[-1][1].path == "/getSystemData"
mock_update.assert_called_once()
mock_update.reset_mock()
async def test_climate_myzone_zone(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
mock_get: AsyncMock,
mock_update: AsyncMock,
) -> None:
"""Test climate platform myzone zone entity."""
await add_mock_config(hass)
# Test Climate Zone Entity
entity_id = "climate.myzone_zone_open_with_sensor"
@ -184,14 +154,8 @@ async def test_climate_async_setup_entry(
{ATTR_ENTITY_ID: [entity_id], ATTR_HVAC_MODE: HVACMode.FAN_ONLY},
blocking=True,
)
assert aioclient_mock.mock_calls[-2][0] == "GET"
assert aioclient_mock.mock_calls[-2][1].path == "/setAircon"
data = loads(aioclient_mock.mock_calls[-2][1].query["json"])
assert data["ac1"]["zones"]["z01"]["state"] == ADVANTAGE_AIR_STATE_OPEN
assert aioclient_mock.mock_calls[-1][0] == "GET"
assert aioclient_mock.mock_calls[-1][1].path == "/getSystemData"
mock_update.assert_called_once()
mock_update.reset_mock()
# Test Climate Zone Off
await hass.services.async_call(
@ -200,13 +164,8 @@ async def test_climate_async_setup_entry(
{ATTR_ENTITY_ID: [entity_id], ATTR_HVAC_MODE: HVACMode.OFF},
blocking=True,
)
assert aioclient_mock.mock_calls[-2][0] == "GET"
assert aioclient_mock.mock_calls[-2][1].path == "/setAircon"
data = loads(aioclient_mock.mock_calls[-2][1].query["json"])
assert data["ac1"]["zones"]["z01"]["state"] == ADVANTAGE_AIR_STATE_CLOSE
assert aioclient_mock.mock_calls[-1][0] == "GET"
assert aioclient_mock.mock_calls[-1][1].path == "/getSystemData"
mock_update.assert_called_once()
mock_update.reset_mock()
await hass.services.async_call(
CLIMATE_DOMAIN,
@ -214,11 +173,19 @@ async def test_climate_async_setup_entry(
{ATTR_ENTITY_ID: [entity_id], ATTR_TEMPERATURE: 25},
blocking=True,
)
mock_update.assert_called_once()
mock_update.reset_mock()
assert aioclient_mock.mock_calls[-2][0] == "GET"
assert aioclient_mock.mock_calls[-2][1].path == "/setAircon"
assert aioclient_mock.mock_calls[-1][0] == "GET"
assert aioclient_mock.mock_calls[-1][1].path == "/getSystemData"
async def test_climate_myauto_main(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
mock_get: AsyncMock,
mock_update: AsyncMock,
) -> None:
"""Test climate platform zone entity."""
await add_mock_config(hass)
# Test MyAuto Climate Entity
entity_id = "climate.myauto"
@ -231,44 +198,34 @@ async def test_climate_async_setup_entry(
assert entry
assert entry.unique_id == "uniqueid-ac3"
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_TEMPERATURE,
{
ATTR_ENTITY_ID: [entity_id],
ATTR_TARGET_TEMP_LOW: 21,
ATTR_TARGET_TEMP_HIGH: 23,
},
blocking=True,
)
assert aioclient_mock.mock_calls[-2][0] == "GET"
assert aioclient_mock.mock_calls[-2][1].path == "/setAircon"
data = loads(aioclient_mock.mock_calls[-2][1].query["json"])
assert data["ac3"]["info"][ADVANTAGE_AIR_HEAT_TARGET] == 21
assert data["ac3"]["info"][ADVANTAGE_AIR_COOL_TARGET] == 23
with patch_update() as mock_update:
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_TEMPERATURE,
{
ATTR_ENTITY_ID: [entity_id],
ATTR_TARGET_TEMP_LOW: 21,
ATTR_TARGET_TEMP_HIGH: 23,
},
blocking=True,
)
mock_update.assert_called_once()
async def test_climate_async_failed_update(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
hass: HomeAssistant,
mock_get: AsyncMock,
mock_update: AsyncMock,
) -> None:
"""Test climate change failure."""
aioclient_mock.get(
TEST_SYSTEM_URL,
text=TEST_SYSTEM_DATA,
)
aioclient_mock.get(
TEST_SET_URL,
exc=SyntaxError,
)
await add_mock_config(hass)
with pytest.raises(HomeAssistantError):
mock_update.side_effect = ApiError
await add_mock_config(hass)
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_TEMPERATURE,
{ATTR_ENTITY_ID: ["climate.myzone"], ATTR_TEMPERATURE: 25},
blocking=True,
)
assert aioclient_mock.mock_calls[-1][0] == "GET"
assert aioclient_mock.mock_calls[-1][1].path == "/setAircon"
mock_update.assert_called_once()

View file

@ -1,23 +1,18 @@
"""Test the Advantage Air config flow."""
from unittest.mock import patch
from unittest.mock import AsyncMock, patch
from advantage_air import ApiError
from homeassistant import config_entries, data_entry_flow
from homeassistant.components.advantage_air.const import DOMAIN
from homeassistant.core import HomeAssistant
from . import TEST_SYSTEM_DATA, TEST_SYSTEM_URL, USER_INPUT
from tests.test_util.aiohttp import AiohttpClientMocker
from . import TEST_SYSTEM_DATA, USER_INPUT
async def test_form(hass: HomeAssistant, aioclient_mock: AiohttpClientMocker) -> None:
async def test_form(hass: HomeAssistant) -> None:
"""Test that form shows up."""
aioclient_mock.get(
TEST_SYSTEM_URL,
text=TEST_SYSTEM_DATA,
)
result1 = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
@ -26,6 +21,9 @@ async def test_form(hass: HomeAssistant, aioclient_mock: AiohttpClientMocker) ->
assert result1["errors"] == {}
with patch(
"homeassistant.components.advantage_air.config_flow.advantage_air.async_get",
new=AsyncMock(return_value=TEST_SYSTEM_DATA),
) as mock_get, patch(
"homeassistant.components.advantage_air.async_setup_entry",
return_value=True,
) as mock_setup_entry:
@ -34,43 +32,44 @@ async def test_form(hass: HomeAssistant, aioclient_mock: AiohttpClientMocker) ->
USER_INPUT,
)
await hass.async_block_till_done()
mock_setup_entry.assert_called_once()
mock_get.assert_called_once()
assert len(aioclient_mock.mock_calls) == 1
assert result2["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
assert result2["title"] == "testname"
assert result2["data"] == USER_INPUT
assert len(mock_setup_entry.mock_calls) == 1
# Test Duplicate Config Flow
result3 = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
result4 = await hass.config_entries.flow.async_configure(
result3["flow_id"],
USER_INPUT,
)
with patch(
"homeassistant.components.advantage_air.config_flow.advantage_air.async_get",
new=AsyncMock(return_value=TEST_SYSTEM_DATA),
) as mock_get:
result4 = await hass.config_entries.flow.async_configure(
result3["flow_id"],
USER_INPUT,
)
assert result4["type"] == data_entry_flow.FlowResultType.ABORT
async def test_form_cannot_connect(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
async def test_form_cannot_connect(hass: HomeAssistant) -> None:
"""Test we handle cannot connect error."""
aioclient_mock.get(
TEST_SYSTEM_URL,
exc=SyntaxError,
)
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
USER_INPUT,
)
with patch(
"homeassistant.components.advantage_air.config_flow.advantage_air.async_get",
new=AsyncMock(side_effect=ApiError),
) as mock_get:
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
USER_INPUT,
)
mock_get.assert_called_once()
assert result2["type"] == data_entry_flow.FlowResultType.FORM
assert result2["step_id"] == "user"
assert result2["errors"] == {"base": "cannot_connect"}
assert len(aioclient_mock.mock_calls) == 1

View file

@ -1,10 +1,6 @@
"""Test the Advantage Air Cover Platform."""
from json import loads
from unittest.mock import AsyncMock
from homeassistant.components.advantage_air.const import (
ADVANTAGE_AIR_STATE_CLOSE,
ADVANTAGE_AIR_STATE_OPEN,
)
from homeassistant.components.cover import (
ATTR_POSITION,
DOMAIN as COVER_DOMAIN,
@ -17,34 +13,17 @@ from homeassistant.const import ATTR_ENTITY_ID, STATE_OPEN
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from . import (
TEST_SET_RESPONSE,
TEST_SET_THING_URL,
TEST_SET_URL,
TEST_SYSTEM_DATA,
TEST_SYSTEM_URL,
add_mock_config,
)
from tests.test_util.aiohttp import AiohttpClientMocker
from . import add_mock_config
async def test_ac_cover(
hass: HomeAssistant,
aioclient_mock: AiohttpClientMocker,
entity_registry: er.EntityRegistry,
mock_get: AsyncMock,
mock_update: AsyncMock,
) -> None:
"""Test cover platform."""
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)
# Test Cover Zone Entity
@ -65,12 +44,8 @@ async def test_ac_cover(
{ATTR_ENTITY_ID: [entity_id]},
blocking=True,
)
assert aioclient_mock.mock_calls[-2][0] == "GET"
assert aioclient_mock.mock_calls[-2][1].path == "/setAircon"
data = loads(aioclient_mock.mock_calls[-2][1].query["json"])
assert data["ac3"]["zones"]["z01"]["state"] == ADVANTAGE_AIR_STATE_CLOSE
assert aioclient_mock.mock_calls[-1][0] == "GET"
assert aioclient_mock.mock_calls[-1][1].path == "/getSystemData"
mock_update.assert_called_once()
mock_update.reset_mock()
await hass.services.async_call(
COVER_DOMAIN,
@ -78,13 +53,8 @@ async def test_ac_cover(
{ATTR_ENTITY_ID: [entity_id]},
blocking=True,
)
assert aioclient_mock.mock_calls[-2][0] == "GET"
assert aioclient_mock.mock_calls[-2][1].path == "/setAircon"
data = loads(aioclient_mock.mock_calls[-2][1].query["json"])
assert data["ac3"]["zones"]["z01"]["state"] == ADVANTAGE_AIR_STATE_OPEN
assert data["ac3"]["zones"]["z01"]["value"] == 100
assert aioclient_mock.mock_calls[-1][0] == "GET"
assert aioclient_mock.mock_calls[-1][1].path == "/getSystemData"
mock_update.assert_called_once()
mock_update.reset_mock()
await hass.services.async_call(
COVER_DOMAIN,
@ -92,12 +62,8 @@ async def test_ac_cover(
{ATTR_ENTITY_ID: [entity_id], ATTR_POSITION: 50},
blocking=True,
)
assert aioclient_mock.mock_calls[-2][0] == "GET"
assert aioclient_mock.mock_calls[-2][1].path == "/setAircon"
data = loads(aioclient_mock.mock_calls[-2][1].query["json"])
assert data["ac3"]["zones"]["z01"]["value"] == 50
assert aioclient_mock.mock_calls[-1][0] == "GET"
assert aioclient_mock.mock_calls[-1][1].path == "/getSystemData"
mock_update.assert_called_once()
mock_update.reset_mock()
await hass.services.async_call(
COVER_DOMAIN,
@ -105,12 +71,8 @@ async def test_ac_cover(
{ATTR_ENTITY_ID: [entity_id], ATTR_POSITION: 0},
blocking=True,
)
assert aioclient_mock.mock_calls[-2][0] == "GET"
assert aioclient_mock.mock_calls[-2][1].path == "/setAircon"
data = loads(aioclient_mock.mock_calls[-2][1].query["json"])
assert data["ac3"]["zones"]["z01"]["state"] == ADVANTAGE_AIR_STATE_CLOSE
assert aioclient_mock.mock_calls[-1][0] == "GET"
assert aioclient_mock.mock_calls[-1][1].path == "/getSystemData"
mock_update.assert_called_once()
mock_update.reset_mock()
# Test controlling multiple Cover Zone Entity
await hass.services.async_call(
@ -124,9 +86,9 @@ async def test_ac_cover(
},
blocking=True,
)
data = loads(aioclient_mock.mock_calls[-2][1].query["json"])
assert data["ac3"]["zones"]["z01"]["state"] == ADVANTAGE_AIR_STATE_CLOSE
assert data["ac3"]["zones"]["z02"]["state"] == ADVANTAGE_AIR_STATE_CLOSE
assert len(mock_update.mock_calls) == 2
mock_update.reset_mock()
await hass.services.async_call(
COVER_DOMAIN,
SERVICE_OPEN_COVER,
@ -138,27 +100,18 @@ async def test_ac_cover(
},
blocking=True,
)
data = loads(aioclient_mock.mock_calls[-2][1].query["json"])
assert data["ac3"]["zones"]["z01"]["state"] == ADVANTAGE_AIR_STATE_OPEN
assert data["ac3"]["zones"]["z02"]["state"] == ADVANTAGE_AIR_STATE_OPEN
assert len(mock_update.mock_calls) == 2
async def test_things_cover(
hass: HomeAssistant,
aioclient_mock: AiohttpClientMocker,
entity_registry: er.EntityRegistry,
mock_get: AsyncMock,
mock_update: AsyncMock,
) -> None:
"""Test cover platform."""
aioclient_mock.get(
TEST_SYSTEM_URL,
text=TEST_SYSTEM_DATA,
)
aioclient_mock.get(
TEST_SET_THING_URL,
text=TEST_SET_RESPONSE,
)
await add_mock_config(hass)
# Test Blind 1 Entity
@ -171,7 +124,7 @@ async def test_things_cover(
entry = entity_registry.async_get(entity_id)
assert entry
assert entry.unique_id == "uniqueid-200"
assert entry.unique_id == f"uniqueid-{thing_id}"
await hass.services.async_call(
COVER_DOMAIN,
@ -179,13 +132,8 @@ async def test_things_cover(
{ATTR_ENTITY_ID: [entity_id]},
blocking=True,
)
assert aioclient_mock.mock_calls[-2][0] == "GET"
assert aioclient_mock.mock_calls[-2][1].path == "/setThings"
data = loads(aioclient_mock.mock_calls[-2][1].query["json"]).get(thing_id)
assert data["id"] == thing_id
assert data["value"] == 0
assert aioclient_mock.mock_calls[-1][0] == "GET"
assert aioclient_mock.mock_calls[-1][1].path == "/getSystemData"
mock_update.assert_called_once()
mock_update.reset_mock()
await hass.services.async_call(
COVER_DOMAIN,
@ -193,10 +141,4 @@ async def test_things_cover(
{ATTR_ENTITY_ID: [entity_id]},
blocking=True,
)
assert aioclient_mock.mock_calls[-2][0] == "GET"
assert aioclient_mock.mock_calls[-2][1].path == "/setThings"
data = loads(aioclient_mock.mock_calls[-2][1].query["json"]).get(thing_id)
assert data["id"] == thing_id
assert data["value"] == 100
assert aioclient_mock.mock_calls[-1][0] == "GET"
assert aioclient_mock.mock_calls[-1][1].path == "/getSystemData"
mock_update.assert_called_once()

View file

@ -1,28 +1,24 @@
"""Test the Advantage Air Diagnostics."""
from unittest.mock import AsyncMock
from syrupy.assertion import SnapshotAssertion
from homeassistant.core import HomeAssistant
from . import TEST_SYSTEM_DATA, TEST_SYSTEM_URL, add_mock_config
from . import add_mock_config
from tests.components.diagnostics import get_diagnostics_for_config_entry
from tests.test_util.aiohttp import AiohttpClientMocker
from tests.typing import ClientSessionGenerator
async def test_select_async_setup_entry(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
aioclient_mock: AiohttpClientMocker,
snapshot: SnapshotAssertion,
mock_get: AsyncMock,
) -> None:
"""Test select platform."""
aioclient_mock.get(
TEST_SYSTEM_URL,
text=TEST_SYSTEM_DATA,
)
entry = await add_mock_config(hass)
diag = await get_diagnostics_for_config_entry(hass, hass_client, entry)
assert diag == snapshot

View file

@ -1,22 +1,17 @@
"""Test the Advantage Air Initialization."""
from unittest.mock import AsyncMock
from advantage_air import ApiError
from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant
from . import TEST_SYSTEM_DATA, TEST_SYSTEM_URL, add_mock_config
from tests.test_util.aiohttp import AiohttpClientMocker
from . import add_mock_config, patch_get
async def test_async_setup_entry(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
async def test_async_setup_entry(hass: HomeAssistant, mock_get: AsyncMock) -> None:
"""Test a successful setup entry and unload."""
aioclient_mock.get(
TEST_SYSTEM_URL,
text=TEST_SYSTEM_DATA,
)
entry = await add_mock_config(hass)
assert entry.state is ConfigEntryState.LOADED
@ -25,15 +20,9 @@ async def test_async_setup_entry(
assert entry.state is ConfigEntryState.NOT_LOADED
async def test_async_setup_entry_failure(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
async def test_async_setup_entry_failure(hass: HomeAssistant) -> None:
"""Test a unsuccessful setup entry."""
aioclient_mock.get(
TEST_SYSTEM_URL,
exc=SyntaxError,
)
entry = await add_mock_config(hass)
with patch_get(side_effect=ApiError):
entry = await add_mock_config(hass)
assert entry.state is ConfigEntryState.SETUP_RETRY

View file

@ -1,10 +1,8 @@
"""Test the Advantage Air Switch Platform."""
from json import loads
from homeassistant.components.advantage_air.const import (
ADVANTAGE_AIR_STATE_OFF,
ADVANTAGE_AIR_STATE_ON,
)
from unittest.mock import AsyncMock
from homeassistant.components.light import (
ATTR_BRIGHTNESS,
DOMAIN as LIGHT_DOMAIN,
@ -15,34 +13,17 @@ from homeassistant.const import ATTR_ENTITY_ID, STATE_OFF, STATE_ON
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from . import (
TEST_SET_LIGHT_URL,
TEST_SET_RESPONSE,
TEST_SET_THING_URL,
TEST_SYSTEM_DATA,
TEST_SYSTEM_URL,
add_mock_config,
)
from tests.test_util.aiohttp import AiohttpClientMocker
from . import add_mock_config
async def test_light(
hass: HomeAssistant,
aioclient_mock: AiohttpClientMocker,
entity_registry: er.EntityRegistry,
mock_get: AsyncMock,
mock_update: AsyncMock,
) -> None:
"""Test light setup."""
aioclient_mock.get(
TEST_SYSTEM_URL,
text=TEST_SYSTEM_DATA,
)
aioclient_mock.get(
TEST_SET_LIGHT_URL,
text=TEST_SET_RESPONSE,
)
await add_mock_config(hass)
# Test Light Entity
@ -62,13 +43,9 @@ async def test_light(
{ATTR_ENTITY_ID: [entity_id]},
blocking=True,
)
assert aioclient_mock.mock_calls[-2][0] == "GET"
assert aioclient_mock.mock_calls[-2][1].path == "/setLights"
data = loads(aioclient_mock.mock_calls[-2][1].query["json"]).get(light_id)
assert data["id"] == light_id
assert data["state"] == ADVANTAGE_AIR_STATE_ON
assert aioclient_mock.mock_calls[-1][0] == "GET"
assert aioclient_mock.mock_calls[-1][1].path == "/getSystemData"
mock_update.assert_called_once()
mock_update.reset_mock()
await hass.services.async_call(
LIGHT_DOMAIN,
@ -76,13 +53,8 @@ async def test_light(
{ATTR_ENTITY_ID: [entity_id]},
blocking=True,
)
assert aioclient_mock.mock_calls[-2][0] == "GET"
assert aioclient_mock.mock_calls[-2][1].path == "/setLights"
data = loads(aioclient_mock.mock_calls[-2][1].query["json"]).get(light_id)
assert data["id"] == light_id
assert data["state"] == ADVANTAGE_AIR_STATE_OFF
assert aioclient_mock.mock_calls[-1][0] == "GET"
assert aioclient_mock.mock_calls[-1][1].path == "/getSystemData"
mock_update.assert_called_once()
mock_update.reset_mock()
# Test Dimmable Light Entity
entity_id = "light.light_b"
@ -98,13 +70,8 @@ async def test_light(
{ATTR_ENTITY_ID: [entity_id]},
blocking=True,
)
assert aioclient_mock.mock_calls[-2][0] == "GET"
assert aioclient_mock.mock_calls[-2][1].path == "/setLights"
data = loads(aioclient_mock.mock_calls[-2][1].query["json"]).get(light_id)
assert data["id"] == light_id
assert data["state"] == ADVANTAGE_AIR_STATE_ON
assert aioclient_mock.mock_calls[-1][0] == "GET"
assert aioclient_mock.mock_calls[-1][1].path == "/getSystemData"
mock_update.assert_called_once()
mock_update.reset_mock()
await hass.services.async_call(
LIGHT_DOMAIN,
@ -112,32 +79,17 @@ async def test_light(
{ATTR_ENTITY_ID: [entity_id], ATTR_BRIGHTNESS: 128},
blocking=True,
)
assert aioclient_mock.mock_calls[-2][0] == "GET"
assert aioclient_mock.mock_calls[-2][1].path == "/setLights"
data = loads(aioclient_mock.mock_calls[-2][1].query["json"]).get(light_id)
assert data["id"] == light_id
assert data["value"] == 50
assert data["state"] == ADVANTAGE_AIR_STATE_ON
assert aioclient_mock.mock_calls[-1][0] == "GET"
assert aioclient_mock.mock_calls[-1][1].path == "/getSystemData"
mock_update.assert_called_once()
async def test_things_light(
hass: HomeAssistant,
aioclient_mock: AiohttpClientMocker,
entity_registry: er.EntityRegistry,
mock_get: AsyncMock,
mock_update: AsyncMock,
) -> None:
"""Test things lights."""
aioclient_mock.get(
TEST_SYSTEM_URL,
text=TEST_SYSTEM_DATA,
)
aioclient_mock.get(
TEST_SET_THING_URL,
text=TEST_SET_RESPONSE,
)
await add_mock_config(hass)
# Test Switch Entity
@ -149,7 +101,7 @@ async def test_things_light(
entry = entity_registry.async_get(entity_id)
assert entry
assert entry.unique_id == "uniqueid-204"
assert entry.unique_id == f"uniqueid-{light_id}"
await hass.services.async_call(
LIGHT_DOMAIN,
@ -157,13 +109,8 @@ async def test_things_light(
{ATTR_ENTITY_ID: [entity_id]},
blocking=True,
)
assert aioclient_mock.mock_calls[-2][0] == "GET"
assert aioclient_mock.mock_calls[-2][1].path == "/setThings"
data = loads(aioclient_mock.mock_calls[-2][1].query["json"]).get(light_id)
assert data["id"] == light_id
assert data["value"] == 0
assert aioclient_mock.mock_calls[-1][0] == "GET"
assert aioclient_mock.mock_calls[-1][1].path == "/getSystemData"
mock_update.assert_called_once()
mock_update.reset_mock()
await hass.services.async_call(
LIGHT_DOMAIN,
@ -171,10 +118,4 @@ async def test_things_light(
{ATTR_ENTITY_ID: [entity_id], ATTR_BRIGHTNESS: 128},
blocking=True,
)
assert aioclient_mock.mock_calls[-2][0] == "GET"
assert aioclient_mock.mock_calls[-2][1].path == "/setThings"
data = loads(aioclient_mock.mock_calls[-2][1].query["json"]).get(light_id)
assert data["id"] == light_id
assert data["value"] == 50
assert aioclient_mock.mock_calls[-1][0] == "GET"
assert aioclient_mock.mock_calls[-1][1].path == "/getSystemData"
mock_update.assert_called_once()

View file

@ -1,5 +1,7 @@
"""Test the Advantage Air Select Platform."""
from json import loads
from unittest.mock import AsyncMock
from homeassistant.components.select import (
ATTR_OPTION,
@ -10,37 +12,19 @@ from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from . import (
TEST_SET_RESPONSE,
TEST_SET_URL,
TEST_SYSTEM_DATA,
TEST_SYSTEM_URL,
add_mock_config,
)
from tests.test_util.aiohttp import AiohttpClientMocker
from . import add_mock_config
async def test_select_async_setup_entry(
hass: HomeAssistant,
aioclient_mock: AiohttpClientMocker,
entity_registry: er.EntityRegistry,
mock_get: AsyncMock,
mock_update: AsyncMock,
) -> None:
"""Test select platform."""
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 MyZone Select Entity
entity_id = "select.myzone_myzone"
state = hass.states.get(entity_id)
@ -57,10 +41,4 @@ async def test_select_async_setup_entry(
{ATTR_ENTITY_ID: entity_id, ATTR_OPTION: "Zone 3"},
blocking=True,
)
assert len(aioclient_mock.mock_calls) == 3
assert aioclient_mock.mock_calls[-2][0] == "GET"
assert aioclient_mock.mock_calls[-2][1].path == "/setAircon"
data = loads(aioclient_mock.mock_calls[-2][1].query["json"])
assert data["ac1"]["info"]["myZone"] == 3
assert aioclient_mock.mock_calls[-1][0] == "GET"
assert aioclient_mock.mock_calls[-1][1].path == "/getSystemData"
mock_update.assert_called_once()

View file

@ -1,6 +1,6 @@
"""Test the Advantage Air Sensor Platform."""
from datetime import timedelta
from json import loads
from unittest.mock import AsyncMock
from homeassistant.components.advantage_air.const import DOMAIN as ADVANTAGE_AIR_DOMAIN
from homeassistant.components.advantage_air.sensor import (
@ -13,37 +13,21 @@ 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_sensor_platform(
hass: HomeAssistant,
aioclient_mock: AiohttpClientMocker,
entity_registry: er.EntityRegistry,
mock_get: AsyncMock,
mock_update: AsyncMock,
) -> None:
"""Test sensor platform."""
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 TimeToOn Sensor
entity_id = "sensor.myzone_time_to_on"
state = hass.states.get(entity_id)
@ -55,19 +39,15 @@ async def test_sensor_platform(
assert entry.unique_id == "uniqueid-ac1-timetoOn"
value = 20
await hass.services.async_call(
ADVANTAGE_AIR_DOMAIN,
ADVANTAGE_AIR_SERVICE_SET_TIME_TO,
{ATTR_ENTITY_ID: [entity_id], ADVANTAGE_AIR_SET_COUNTDOWN_VALUE: value},
blocking=True,
)
assert len(aioclient_mock.mock_calls) == 3
assert aioclient_mock.mock_calls[-2][0] == "GET"
assert aioclient_mock.mock_calls[-2][1].path == "/setAircon"
data = loads(aioclient_mock.mock_calls[-2][1].query["json"])
assert data["ac1"]["info"]["countDownToOn"] == value
assert aioclient_mock.mock_calls[-1][0] == "GET"
assert aioclient_mock.mock_calls[-1][1].path == "/getSystemData"
mock_update.assert_called_once()
mock_update.reset_mock()
# Test First TimeToOff Sensor
entity_id = "sensor.myzone_time_to_off"
@ -86,13 +66,8 @@ async def test_sensor_platform(
{ATTR_ENTITY_ID: [entity_id], ADVANTAGE_AIR_SET_COUNTDOWN_VALUE: value},
blocking=True,
)
assert len(aioclient_mock.mock_calls) == 5
assert aioclient_mock.mock_calls[-2][0] == "GET"
assert aioclient_mock.mock_calls[-2][1].path == "/setAircon"
data = loads(aioclient_mock.mock_calls[-2][1].query["json"])
assert data["ac1"]["info"]["countDownToOff"] == value
assert aioclient_mock.mock_calls[-1][0] == "GET"
assert aioclient_mock.mock_calls[-1][1].path == "/getSystemData"
mock_update.assert_called_once()
mock_update.reset_mock()
# Test First Zone Vent Sensor
entity_id = "sensor.myzone_zone_open_with_sensor_vent"
@ -139,6 +114,7 @@ async def test_sensor_platform(
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()
@ -147,6 +123,7 @@ async def test_sensor_platform(
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

View file

@ -1,10 +1,8 @@
"""Test the Advantage Air Switch Platform."""
from json import loads
from homeassistant.components.advantage_air.const import (
ADVANTAGE_AIR_STATE_OFF,
ADVANTAGE_AIR_STATE_ON,
)
from unittest.mock import AsyncMock
from homeassistant.components.switch import (
DOMAIN as SWITCH_DOMAIN,
SERVICE_TURN_OFF,
@ -14,34 +12,17 @@ from homeassistant.const import ATTR_ENTITY_ID, STATE_OFF, STATE_ON
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from . import (
TEST_SET_RESPONSE,
TEST_SET_THING_URL,
TEST_SET_URL,
TEST_SYSTEM_DATA,
TEST_SYSTEM_URL,
add_mock_config,
)
from tests.test_util.aiohttp import AiohttpClientMocker
from . import add_mock_config
async def test_cover_async_setup_entry(
hass: HomeAssistant,
aioclient_mock: AiohttpClientMocker,
entity_registry: er.EntityRegistry,
mock_get: AsyncMock,
mock_update: AsyncMock,
) -> None:
"""Test switch platform."""
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)
# Test Switch Entity
@ -60,12 +41,8 @@ async def test_cover_async_setup_entry(
{ATTR_ENTITY_ID: [entity_id]},
blocking=True,
)
assert aioclient_mock.mock_calls[-2][0] == "GET"
assert aioclient_mock.mock_calls[-2][1].path == "/setAircon"
data = loads(aioclient_mock.mock_calls[-2][1].query["json"])
assert data["ac1"]["info"]["freshAirStatus"] == ADVANTAGE_AIR_STATE_ON
assert aioclient_mock.mock_calls[-1][0] == "GET"
assert aioclient_mock.mock_calls[-1][1].path == "/getSystemData"
mock_update.assert_called_once()
mock_update.reset_mock()
await hass.services.async_call(
SWITCH_DOMAIN,
@ -73,30 +50,17 @@ async def test_cover_async_setup_entry(
{ATTR_ENTITY_ID: [entity_id]},
blocking=True,
)
assert aioclient_mock.mock_calls[-2][0] == "GET"
assert aioclient_mock.mock_calls[-2][1].path == "/setAircon"
data = loads(aioclient_mock.mock_calls[-2][1].query["json"])
assert data["ac1"]["info"]["freshAirStatus"] == ADVANTAGE_AIR_STATE_OFF
assert aioclient_mock.mock_calls[-1][0] == "GET"
assert aioclient_mock.mock_calls[-1][1].path == "/getSystemData"
mock_update.assert_called_once()
async def test_things_switch(
hass: HomeAssistant,
aioclient_mock: AiohttpClientMocker,
entity_registry: er.EntityRegistry,
mock_get: AsyncMock,
mock_update: AsyncMock,
) -> None:
"""Test things switches."""
aioclient_mock.get(
TEST_SYSTEM_URL,
text=TEST_SYSTEM_DATA,
)
aioclient_mock.get(
TEST_SET_THING_URL,
text=TEST_SET_RESPONSE,
)
await add_mock_config(hass)
# Test Switch Entity
@ -108,7 +72,7 @@ async def test_things_switch(
entry = entity_registry.async_get(entity_id)
assert entry
assert entry.unique_id == "uniqueid-205"
assert entry.unique_id == f"uniqueid-{thing_id}"
await hass.services.async_call(
SWITCH_DOMAIN,
@ -116,13 +80,8 @@ async def test_things_switch(
{ATTR_ENTITY_ID: [entity_id]},
blocking=True,
)
assert aioclient_mock.mock_calls[-2][0] == "GET"
assert aioclient_mock.mock_calls[-2][1].path == "/setThings"
data = loads(aioclient_mock.mock_calls[-2][1].query["json"]).get(thing_id)
assert data["id"] == thing_id
assert data["value"] == 0
assert aioclient_mock.mock_calls[-1][0] == "GET"
assert aioclient_mock.mock_calls[-1][1].path == "/getSystemData"
mock_update.assert_called_once()
mock_update.reset_mock()
await hass.services.async_call(
SWITCH_DOMAIN,
@ -130,10 +89,4 @@ async def test_things_switch(
{ATTR_ENTITY_ID: [entity_id]},
blocking=True,
)
assert aioclient_mock.mock_calls[-2][0] == "GET"
assert aioclient_mock.mock_calls[-2][1].path == "/setThings"
data = loads(aioclient_mock.mock_calls[-2][1].query["json"]).get(thing_id)
assert data["id"] == thing_id
assert data["value"] == 100
assert aioclient_mock.mock_calls[-1][0] == "GET"
assert aioclient_mock.mock_calls[-1][1].path == "/getSystemData"
mock_update.assert_called_once()

View file

@ -1,25 +1,26 @@
"""Test the Advantage Air Update Platform."""
from unittest.mock import AsyncMock
from homeassistant.components.advantage_air.const import DOMAIN
from homeassistant.const import STATE_ON
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from . import TEST_SYSTEM_URL, add_mock_config
from . import add_mock_config
from tests.common import load_fixture
from tests.test_util.aiohttp import AiohttpClientMocker
from tests.common import load_json_object_fixture
TEST_NEEDS_UPDATE = load_json_object_fixture("needsUpdate.json", DOMAIN)
async def test_update_platform(
hass: HomeAssistant,
aioclient_mock: AiohttpClientMocker,
entity_registry: er.EntityRegistry,
mock_get: AsyncMock,
) -> None:
"""Test update platform."""
aioclient_mock.get(
TEST_SYSTEM_URL,
text=load_fixture("advantage_air/needsUpdate.json"),
)
mock_get.return_value = TEST_NEEDS_UPDATE
await add_mock_config(hass)
entity_id = "update.testname_app"