94 lines
3.1 KiB
Python
94 lines
3.1 KiB
Python
"""Test the air-Q config flow."""
|
|
from unittest.mock import patch
|
|
|
|
from aioairq.core import DeviceInfo, InvalidAuth, InvalidInput
|
|
from aiohttp.client_exceptions import ClientConnectionError
|
|
|
|
from homeassistant import config_entries
|
|
from homeassistant.components.airq.const import DOMAIN
|
|
from homeassistant.const import CONF_IP_ADDRESS, CONF_PASSWORD
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.data_entry_flow import FlowResultType
|
|
|
|
TEST_USER_DATA = {
|
|
CONF_IP_ADDRESS: "192.168.0.0",
|
|
CONF_PASSWORD: "password",
|
|
}
|
|
TEST_DEVICE_INFO = DeviceInfo(
|
|
id="id",
|
|
name="name",
|
|
model="model",
|
|
sw_version="sw",
|
|
hw_version="hw",
|
|
)
|
|
TEST_DATA_OUT = TEST_USER_DATA | {
|
|
"device_info": {k: v for k, v in TEST_DEVICE_INFO.items() if k != "id"}
|
|
}
|
|
|
|
|
|
async def test_form(hass: HomeAssistant) -> None:
|
|
"""Test we get the form."""
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
)
|
|
assert result["type"] == FlowResultType.FORM
|
|
assert result["errors"] is None
|
|
|
|
with patch("aioairq.AirQ.validate"), patch(
|
|
"aioairq.AirQ.fetch_device_info", return_value=TEST_DEVICE_INFO
|
|
):
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
TEST_USER_DATA,
|
|
)
|
|
await hass.async_block_till_done()
|
|
|
|
assert result2["type"] == FlowResultType.CREATE_ENTRY
|
|
assert result2["title"] == TEST_DEVICE_INFO["name"]
|
|
assert result2["data"] == TEST_DATA_OUT
|
|
|
|
|
|
async def test_form_invalid_auth(hass: HomeAssistant) -> None:
|
|
"""Test we handle invalid auth."""
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
)
|
|
|
|
with patch("aioairq.AirQ.validate", side_effect=InvalidAuth):
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"], TEST_USER_DATA | {CONF_PASSWORD: "wrong_password"}
|
|
)
|
|
|
|
assert result2["type"] == FlowResultType.FORM
|
|
assert result2["errors"] == {"base": "invalid_auth"}
|
|
|
|
|
|
async def test_form_cannot_connect(hass: HomeAssistant) -> None:
|
|
"""Test we handle cannot connect error."""
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
)
|
|
|
|
with patch("aioairq.AirQ.validate", side_effect=ClientConnectionError):
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"], TEST_USER_DATA
|
|
)
|
|
|
|
assert result2["type"] == FlowResultType.FORM
|
|
assert result2["errors"] == {"base": "cannot_connect"}
|
|
|
|
|
|
async def test_form_invalid_input(hass: HomeAssistant) -> None:
|
|
"""Test we handle cannot connect error."""
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
)
|
|
|
|
with patch("aioairq.AirQ.validate", side_effect=InvalidInput):
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"], TEST_USER_DATA | {CONF_IP_ADDRESS: "invalid_ip"}
|
|
)
|
|
|
|
assert result2["type"] == FlowResultType.FORM
|
|
assert result2["errors"] == {"base": "invalid_input"}
|