mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Clean up Guardian config flow tests (#64598)
* Clean up Guardian config flow tests * Docstring
This commit is contained in:
parent
ff535053a9
commit
aef8d9ee4d
@ -1,18 +1,54 @@
|
||||
"""Define fixtures for Elexa Guardian tests."""
|
||||
import json
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.guardian.const import DOMAIN
|
||||
from homeassistant.const import CONF_IP_ADDRESS, CONF_PORT
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
@pytest.fixture()
|
||||
def ping_client():
|
||||
"""Define a patched client that returns a successful ping response."""
|
||||
with patch(
|
||||
"homeassistant.components.guardian.async_setup_entry", return_value=True
|
||||
), patch("aioguardian.client.Client.connect"), patch(
|
||||
from tests.common import MockConfigEntry, load_fixture
|
||||
|
||||
|
||||
@pytest.fixture(name="config_entry")
|
||||
def config_entry_fixture(hass, config, unique_id):
|
||||
"""Define a config entry fixture."""
|
||||
entry = MockConfigEntry(domain=DOMAIN, unique_id=unique_id, data=config)
|
||||
entry.add_to_hass(hass)
|
||||
return entry
|
||||
|
||||
|
||||
@pytest.fixture(name="config")
|
||||
def config_fixture(hass):
|
||||
"""Define a config entry data fixture."""
|
||||
return {
|
||||
CONF_IP_ADDRESS: "192.168.1.100",
|
||||
CONF_PORT: 7777,
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture(name="data_ping", scope="session")
|
||||
def data_ping_fixture():
|
||||
"""Define data from a successful ping response."""
|
||||
return json.loads(load_fixture("ping_data.json", "guardian"))
|
||||
|
||||
|
||||
@pytest.fixture(name="setup_guardian")
|
||||
async def setup_guardian_fixture(hass, config, data_ping):
|
||||
"""Define a fixture to set up Guardian."""
|
||||
with patch("aioguardian.client.Client.connect"), patch(
|
||||
"aioguardian.commands.system.SystemCommands.ping",
|
||||
return_value={"command": 0, "status": "ok", "data": {"uid": "ABCDEF123456"}},
|
||||
), patch(
|
||||
"aioguardian.client.Client.disconnect"
|
||||
return_value=data_ping,
|
||||
), patch("aioguardian.client.Client.disconnect"), patch(
|
||||
"homeassistant.components.guardian.PLATFORMS", []
|
||||
):
|
||||
assert await async_setup_component(hass, DOMAIN, config)
|
||||
await hass.async_block_till_done()
|
||||
yield
|
||||
|
||||
|
||||
@pytest.fixture(name="unique_id")
|
||||
def unique_id_fixture(hass):
|
||||
"""Define a config entry unique ID fixture."""
|
||||
return "guardian_3456"
|
||||
|
7
tests/components/guardian/fixtures/ping_data.json
Normal file
7
tests/components/guardian/fixtures/ping_data.json
Normal file
@ -0,0 +1,7 @@
|
||||
{
|
||||
"command": 0,
|
||||
"status": "ok",
|
||||
"data": {
|
||||
"uid": "ABCDEF123456"
|
||||
}
|
||||
}
|
@ -13,35 +13,24 @@ from homeassistant.components.guardian.config_flow import (
|
||||
from homeassistant.config_entries import SOURCE_DHCP, SOURCE_USER, SOURCE_ZEROCONF
|
||||
from homeassistant.const import CONF_IP_ADDRESS, CONF_PORT
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_duplicate_error(hass, ping_client):
|
||||
async def test_duplicate_error(hass, config, config_entry, setup_guardian):
|
||||
"""Test that errors are shown when duplicate entries are added."""
|
||||
conf = {CONF_IP_ADDRESS: "192.168.1.100", CONF_PORT: 7777}
|
||||
|
||||
MockConfigEntry(domain=DOMAIN, unique_id="guardian_3456", data=conf).add_to_hass(
|
||||
hass
|
||||
)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}, data=conf
|
||||
DOMAIN, context={"source": SOURCE_USER}, data=config
|
||||
)
|
||||
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
|
||||
async def test_connect_error(hass):
|
||||
async def test_connect_error(hass, config):
|
||||
"""Test that the config entry errors out if the device cannot connect."""
|
||||
conf = {CONF_IP_ADDRESS: "192.168.1.100", CONF_PORT: 7777}
|
||||
|
||||
with patch(
|
||||
"aioguardian.client.Client.connect",
|
||||
side_effect=GuardianError,
|
||||
):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}, data=conf
|
||||
DOMAIN, context={"source": SOURCE_USER}, data=config
|
||||
)
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert result["errors"] == {CONF_IP_ADDRESS: "cannot_connect"}
|
||||
@ -59,10 +48,8 @@ async def test_get_pin_from_uid():
|
||||
assert pin == "3456"
|
||||
|
||||
|
||||
async def test_step_user(hass, ping_client):
|
||||
async def test_step_user(hass, config, setup_guardian):
|
||||
"""Test the user step."""
|
||||
conf = {CONF_IP_ADDRESS: "192.168.1.100", CONF_PORT: 7777}
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
@ -70,7 +57,7 @@ async def test_step_user(hass, ping_client):
|
||||
assert result["step_id"] == "user"
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}, data=conf
|
||||
DOMAIN, context={"source": SOURCE_USER}, data=config
|
||||
)
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["title"] == "ABCDEF123456"
|
||||
@ -81,7 +68,7 @@ async def test_step_user(hass, ping_client):
|
||||
}
|
||||
|
||||
|
||||
async def test_step_zeroconf(hass, ping_client):
|
||||
async def test_step_zeroconf(hass, setup_guardian):
|
||||
"""Test the zeroconf step."""
|
||||
zeroconf_data = zeroconf.ZeroconfServiceInfo(
|
||||
host="192.168.1.100",
|
||||
@ -134,7 +121,7 @@ async def test_step_zeroconf_already_in_progress(hass):
|
||||
assert result["reason"] == "already_in_progress"
|
||||
|
||||
|
||||
async def test_step_dhcp(hass, ping_client):
|
||||
async def test_step_dhcp(hass, setup_guardian):
|
||||
"""Test the dhcp step."""
|
||||
dhcp_data = dhcp.DhcpServiceInfo(
|
||||
ip="192.168.1.100",
|
||||
|
Loading…
x
Reference in New Issue
Block a user