2021-02-22 06:12:50 +00:00
|
|
|
"""Test the kmtronic config flow."""
|
2021-10-22 21:06:18 +00:00
|
|
|
from http import HTTPStatus
|
2023-03-10 15:05:31 +00:00
|
|
|
from unittest.mock import AsyncMock, Mock, patch
|
2021-02-22 06:12:50 +00:00
|
|
|
|
|
|
|
from aiohttp import ClientConnectorError, ClientResponseError
|
2023-03-10 15:05:31 +00:00
|
|
|
import pytest
|
2021-02-22 06:12:50 +00:00
|
|
|
|
2021-10-07 10:58:00 +00:00
|
|
|
from homeassistant import config_entries, data_entry_flow
|
2021-03-08 21:56:24 +00:00
|
|
|
from homeassistant.components.kmtronic.const import CONF_REVERSE, DOMAIN
|
2021-05-20 17:19:20 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntryState
|
2023-02-08 18:06:59 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-02-22 06:12:50 +00:00
|
|
|
|
|
|
|
from tests.common import MockConfigEntry
|
2023-02-08 18:06:59 +00:00
|
|
|
from tests.test_util.aiohttp import AiohttpClientMocker
|
2021-02-22 06:12:50 +00:00
|
|
|
|
2023-03-10 15:05:31 +00:00
|
|
|
pytestmark = pytest.mark.usefixtures("mock_setup_entry")
|
2021-02-22 06:12:50 +00:00
|
|
|
|
2023-03-10 15:05:31 +00:00
|
|
|
|
|
|
|
async def test_form(hass: HomeAssistant, mock_setup_entry: AsyncMock) -> None:
|
2021-02-22 06:12:50 +00:00
|
|
|
"""Test we get the form."""
|
2021-10-07 10:58:00 +00:00
|
|
|
|
2021-02-22 06:12:50 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
assert result["type"] == "form"
|
|
|
|
assert result["errors"] == {}
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.kmtronic.config_flow.KMTronicHubAPI.async_get_status",
|
|
|
|
return_value=[Mock()],
|
2023-03-10 15:05:31 +00:00
|
|
|
):
|
2021-02-22 06:12:50 +00:00
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
{
|
|
|
|
"host": "1.1.1.1",
|
|
|
|
"username": "test-username",
|
|
|
|
"password": "test-password",
|
|
|
|
},
|
|
|
|
)
|
2023-03-10 16:42:53 +00:00
|
|
|
await hass.async_block_till_done()
|
2021-02-22 06:12:50 +00:00
|
|
|
|
|
|
|
assert result2["type"] == "create_entry"
|
|
|
|
assert result2["title"] == "1.1.1.1"
|
|
|
|
assert result2["data"] == {
|
|
|
|
"host": "1.1.1.1",
|
|
|
|
"username": "test-username",
|
|
|
|
"password": "test-password",
|
|
|
|
}
|
|
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
|
|
|
|
|
|
|
|
2023-02-08 18:06:59 +00:00
|
|
|
async def test_form_options(
|
|
|
|
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
|
|
|
) -> None:
|
2021-03-08 21:56:24 +00:00
|
|
|
"""Test that the options form."""
|
|
|
|
config_entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
data={
|
|
|
|
"host": "1.1.1.1",
|
|
|
|
"username": "admin",
|
|
|
|
"password": "admin",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
config_entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
aioclient_mock.get(
|
|
|
|
"http://1.1.1.1/status.xml",
|
|
|
|
text="<response><relay0>0</relay0><relay1>0</relay1></response>",
|
|
|
|
)
|
|
|
|
await hass.config_entries.async_setup(config_entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2021-05-20 17:19:20 +00:00
|
|
|
assert config_entry.state is ConfigEntryState.LOADED
|
2021-03-08 21:56:24 +00:00
|
|
|
|
|
|
|
result = await hass.config_entries.options.async_init(config_entry.entry_id)
|
2022-07-07 16:57:36 +00:00
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
2021-03-08 21:56:24 +00:00
|
|
|
assert result["step_id"] == "init"
|
|
|
|
|
|
|
|
result = await hass.config_entries.options.async_configure(
|
|
|
|
result["flow_id"], user_input={CONF_REVERSE: True}
|
|
|
|
)
|
|
|
|
|
2022-07-07 16:57:36 +00:00
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
|
2021-03-08 21:56:24 +00:00
|
|
|
assert config_entry.options == {CONF_REVERSE: True}
|
|
|
|
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2021-05-20 17:19:20 +00:00
|
|
|
assert config_entry.state == config_entries.ConfigEntryState.LOADED
|
2021-03-08 21:56:24 +00:00
|
|
|
|
|
|
|
|
2023-02-08 18:06:59 +00:00
|
|
|
async def test_form_invalid_auth(hass: HomeAssistant) -> None:
|
2021-02-22 06:12:50 +00:00
|
|
|
"""Test we handle invalid auth."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.kmtronic.config_flow.KMTronicHubAPI.async_get_status",
|
2021-10-22 21:06:18 +00:00
|
|
|
side_effect=ClientResponseError(None, None, status=HTTPStatus.BAD_REQUEST),
|
2021-02-22 06:12:50 +00:00
|
|
|
):
|
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
{
|
|
|
|
"host": "1.1.1.1",
|
|
|
|
"username": "test-username",
|
|
|
|
"password": "test-password",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result2["type"] == "form"
|
|
|
|
assert result2["errors"] == {"base": "invalid_auth"}
|
|
|
|
|
|
|
|
|
2023-02-08 18:06:59 +00:00
|
|
|
async def test_form_cannot_connect(hass: HomeAssistant) -> None:
|
2021-02-22 06:12:50 +00:00
|
|
|
"""Test we handle cannot connect error."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.kmtronic.config_flow.KMTronicHubAPI.async_get_status",
|
|
|
|
side_effect=ClientConnectorError(None, Mock()),
|
|
|
|
):
|
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
{
|
|
|
|
"host": "1.1.1.1",
|
|
|
|
"username": "test-username",
|
|
|
|
"password": "test-password",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result2["type"] == "form"
|
|
|
|
assert result2["errors"] == {"base": "cannot_connect"}
|
|
|
|
|
|
|
|
|
2023-02-08 18:06:59 +00:00
|
|
|
async def test_form_unknown_error(hass: HomeAssistant) -> None:
|
2021-02-22 06:12:50 +00:00
|
|
|
"""Test we handle unknown errors."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.kmtronic.config_flow.KMTronicHubAPI.async_get_status",
|
|
|
|
side_effect=Exception(),
|
|
|
|
):
|
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
{
|
|
|
|
"host": "1.1.1.1",
|
|
|
|
"username": "test-username",
|
|
|
|
"password": "test-password",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result2["type"] == "form"
|
|
|
|
assert result2["errors"] == {"base": "unknown"}
|