forked from home-assistant/core
Add init tests for agent dvr (#57022)
* Add init tests for agent dvr * ci
This commit is contained in:
@@ -29,10 +29,8 @@ omit =
|
|||||||
homeassistant/components/ads/*
|
homeassistant/components/ads/*
|
||||||
homeassistant/components/aemet/weather_update_coordinator.py
|
homeassistant/components/aemet/weather_update_coordinator.py
|
||||||
homeassistant/components/aftership/*
|
homeassistant/components/aftership/*
|
||||||
homeassistant/components/agent_dvr/__init__.py
|
|
||||||
homeassistant/components/agent_dvr/alarm_control_panel.py
|
homeassistant/components/agent_dvr/alarm_control_panel.py
|
||||||
homeassistant/components/agent_dvr/camera.py
|
homeassistant/components/agent_dvr/camera.py
|
||||||
homeassistant/components/agent_dvr/const.py
|
|
||||||
homeassistant/components/agent_dvr/helpers.py
|
homeassistant/components/agent_dvr/helpers.py
|
||||||
homeassistant/components/airnow/__init__.py
|
homeassistant/components/airnow/__init__.py
|
||||||
homeassistant/components/airnow/sensor.py
|
homeassistant/components/airnow/sensor.py
|
||||||
|
@@ -33,9 +33,7 @@ class AgentFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
await agent_client.update()
|
await agent_client.update()
|
||||||
except AgentConnectionError:
|
except (AgentConnectionError, AgentError):
|
||||||
pass
|
|
||||||
except AgentError:
|
|
||||||
pass
|
pass
|
||||||
|
|
||||||
await agent_client.close()
|
await agent_client.close()
|
||||||
|
@@ -7,6 +7,23 @@ from homeassistant.core import HomeAssistant
|
|||||||
from tests.common import MockConfigEntry, load_fixture
|
from tests.common import MockConfigEntry, load_fixture
|
||||||
from tests.test_util.aiohttp import AiohttpClientMocker
|
from tests.test_util.aiohttp import AiohttpClientMocker
|
||||||
|
|
||||||
|
CONF_DATA = {
|
||||||
|
CONF_HOST: "example.local",
|
||||||
|
CONF_PORT: 8090,
|
||||||
|
SERVER_URL: "http://example.local:8090/",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def create_entry(hass: HomeAssistant):
|
||||||
|
"""Add config entry in Home Assistant."""
|
||||||
|
entry = MockConfigEntry(
|
||||||
|
domain=DOMAIN,
|
||||||
|
unique_id="c0715bba-c2d0-48ef-9e3e-bc81c9ea4447",
|
||||||
|
data=CONF_DATA,
|
||||||
|
)
|
||||||
|
entry.add_to_hass(hass)
|
||||||
|
return entry
|
||||||
|
|
||||||
|
|
||||||
async def init_integration(
|
async def init_integration(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
@@ -25,17 +42,7 @@ async def init_integration(
|
|||||||
text=load_fixture("agent_dvr/objects.json"),
|
text=load_fixture("agent_dvr/objects.json"),
|
||||||
headers={"Content-Type": CONTENT_TYPE_JSON},
|
headers={"Content-Type": CONTENT_TYPE_JSON},
|
||||||
)
|
)
|
||||||
entry = MockConfigEntry(
|
entry = create_entry(hass)
|
||||||
domain=DOMAIN,
|
|
||||||
unique_id="c0715bba-c2d0-48ef-9e3e-bc81c9ea4447",
|
|
||||||
data={
|
|
||||||
CONF_HOST: "example.local",
|
|
||||||
CONF_PORT: 8090,
|
|
||||||
SERVER_URL: "http://example.local:8090/",
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
entry.add_to_hass(hass)
|
|
||||||
|
|
||||||
if not skip_setup:
|
if not skip_setup:
|
||||||
await hass.config_entries.async_setup(entry.entry_id)
|
await hass.config_entries.async_setup(entry.entry_id)
|
||||||
|
@@ -38,7 +38,9 @@ async def test_user_device_exists_abort(
|
|||||||
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
||||||
|
|
||||||
|
|
||||||
async def test_connection_error(hass: HomeAssistant, aioclient_mock) -> None:
|
async def test_connection_error(
|
||||||
|
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
||||||
|
) -> None:
|
||||||
"""Test we show user form on Agent connection error."""
|
"""Test we show user form on Agent connection error."""
|
||||||
|
|
||||||
aioclient_mock.get("http://example.local:8090/command.cgi?cmd=getStatus", text="")
|
aioclient_mock.get("http://example.local:8090/command.cgi?cmd=getStatus", text="")
|
||||||
@@ -49,13 +51,13 @@ async def test_connection_error(hass: HomeAssistant, aioclient_mock) -> None:
|
|||||||
data={CONF_HOST: "example.local", CONF_PORT: 8090},
|
data={CONF_HOST: "example.local", CONF_PORT: 8090},
|
||||||
)
|
)
|
||||||
|
|
||||||
assert result["errors"] == {"base": "cannot_connect"}
|
assert result["errors"]["base"] == "cannot_connect"
|
||||||
assert result["step_id"] == "user"
|
assert result["step_id"] == "user"
|
||||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||||
|
|
||||||
|
|
||||||
async def test_full_user_flow_implementation(
|
async def test_full_user_flow_implementation(
|
||||||
hass: HomeAssistant, aioclient_mock
|
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test the full manual user flow from start to finish."""
|
"""Test the full manual user flow from start to finish."""
|
||||||
aioclient_mock.get(
|
aioclient_mock.get(
|
||||||
|
56
tests/components/agent_dvr/test_init.py
Normal file
56
tests/components/agent_dvr/test_init.py
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
"""Test Agent DVR integration."""
|
||||||
|
from unittest.mock import AsyncMock, patch
|
||||||
|
|
||||||
|
from agent import AgentError
|
||||||
|
|
||||||
|
from homeassistant.components.agent_dvr.const import DOMAIN
|
||||||
|
from homeassistant.config_entries import ConfigEntryState
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
|
from . import CONF_DATA, create_entry
|
||||||
|
|
||||||
|
from tests.components.agent_dvr import init_integration
|
||||||
|
from tests.test_util.aiohttp import AiohttpClientMocker
|
||||||
|
|
||||||
|
|
||||||
|
async def _create_mocked_agent(available: bool = True):
|
||||||
|
mocked_agent = AsyncMock()
|
||||||
|
mocked_agent.is_available = available
|
||||||
|
return mocked_agent
|
||||||
|
|
||||||
|
|
||||||
|
def _patch_init_agent(mocked_agent):
|
||||||
|
return patch(
|
||||||
|
"homeassistant.components.agent_dvr.Agent",
|
||||||
|
return_value=mocked_agent,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def test_setup_config_and_unload(
|
||||||
|
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
||||||
|
):
|
||||||
|
"""Test setup and unload."""
|
||||||
|
entry = await init_integration(hass, aioclient_mock)
|
||||||
|
assert entry.state == ConfigEntryState.LOADED
|
||||||
|
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
|
||||||
|
assert entry.data == CONF_DATA
|
||||||
|
|
||||||
|
assert await hass.config_entries.async_unload(entry.entry_id)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
assert entry.state is ConfigEntryState.NOT_LOADED
|
||||||
|
assert not hass.data.get(DOMAIN)
|
||||||
|
|
||||||
|
|
||||||
|
async def test_async_setup_entry_not_ready(hass: HomeAssistant):
|
||||||
|
"""Test that it throws ConfigEntryNotReady when exception occurs during setup."""
|
||||||
|
entry = create_entry(hass)
|
||||||
|
with patch(
|
||||||
|
"homeassistant.components.agent_dvr.Agent.update",
|
||||||
|
side_effect=AgentError,
|
||||||
|
):
|
||||||
|
await hass.config_entries.async_setup(entry.entry_id)
|
||||||
|
assert entry.state == ConfigEntryState.SETUP_RETRY
|
||||||
|
with _patch_init_agent(await _create_mocked_agent(available=False)):
|
||||||
|
await hass.config_entries.async_reload(entry.entry_id)
|
||||||
|
assert entry.state == ConfigEntryState.SETUP_RETRY
|
Reference in New Issue
Block a user