2023-11-07 20:53:22 +00:00
|
|
|
"""Test the V2C config flow."""
|
|
|
|
from unittest.mock import AsyncMock, patch
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
from pytrydan.exceptions import TrydanError
|
|
|
|
|
|
|
|
from homeassistant import config_entries
|
|
|
|
from homeassistant.components.v2c.const import DOMAIN
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.data_entry_flow import FlowResultType
|
|
|
|
|
|
|
|
|
|
|
|
async def test_form(hass: HomeAssistant, mock_setup_entry: AsyncMock) -> 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"] == {}
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"pytrydan.Trydan.get_data",
|
|
|
|
return_value={},
|
|
|
|
):
|
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
{
|
|
|
|
"host": "1.1.1.1",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert result2["type"] == FlowResultType.CREATE_ENTRY
|
|
|
|
assert result2["title"] == "EVSE 1.1.1.1"
|
|
|
|
assert result2["data"] == {
|
|
|
|
"host": "1.1.1.1",
|
|
|
|
}
|
|
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
("side_effect", "error"),
|
|
|
|
[
|
|
|
|
(TrydanError, "cannot_connect"),
|
|
|
|
(Exception, "unknown"),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
async def test_form_cannot_connect(
|
2023-11-12 11:04:10 +00:00
|
|
|
hass: HomeAssistant, mock_setup_entry: AsyncMock, side_effect: Exception, error: str
|
2023-11-07 20:53:22 +00:00
|
|
|
) -> None:
|
|
|
|
"""Test we handle cannot connect error."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"pytrydan.Trydan.get_data",
|
|
|
|
side_effect=side_effect,
|
|
|
|
):
|
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
{
|
|
|
|
"host": "1.1.1.1",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result2["type"] == FlowResultType.FORM
|
|
|
|
assert result2["errors"] == {"base": error}
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"pytrydan.Trydan.get_data",
|
|
|
|
return_value={},
|
|
|
|
):
|
|
|
|
result3 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
{
|
|
|
|
"host": "1.1.1.1",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert result3["type"] == FlowResultType.CREATE_ENTRY
|
|
|
|
assert result3["title"] == "EVSE 1.1.1.1"
|
|
|
|
assert result3["data"] == {
|
|
|
|
"host": "1.1.1.1",
|
|
|
|
}
|