mirror of https://github.com/home-assistant/core
114 lines
3.5 KiB
Python
114 lines
3.5 KiB
Python
"""Test the myStrom config flow."""
|
|
|
|
from unittest.mock import AsyncMock, patch
|
|
|
|
from pymystrom.exceptions import MyStromConnectionError
|
|
import pytest
|
|
|
|
from homeassistant import config_entries
|
|
from homeassistant.components.mystrom.const import DOMAIN
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.data_entry_flow import FlowResultType
|
|
|
|
from .conftest import DEVICE_MAC
|
|
|
|
from tests.common import MockConfigEntry
|
|
|
|
pytestmark = pytest.mark.usefixtures("mock_setup_entry")
|
|
|
|
|
|
async def test_form_combined(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"] is FlowResultType.FORM
|
|
assert result["step_id"] == "user"
|
|
assert result["errors"] == {}
|
|
|
|
with patch(
|
|
"pymystrom.get_device_info",
|
|
side_effect=AsyncMock(return_value={"type": 101, "mac": DEVICE_MAC}),
|
|
):
|
|
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"] is FlowResultType.CREATE_ENTRY
|
|
assert result2["title"] == "myStrom Device"
|
|
assert result2["data"] == {"host": "1.1.1.1"}
|
|
|
|
|
|
async def test_form_duplicates(
|
|
hass: HomeAssistant, mock_setup_entry: AsyncMock, config_entry: MockConfigEntry
|
|
) -> None:
|
|
"""Test abort on duplicate."""
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
)
|
|
assert result["type"] is FlowResultType.FORM
|
|
assert result["step_id"] == "user"
|
|
assert result["errors"] == {}
|
|
|
|
with patch(
|
|
"pymystrom.get_device_info",
|
|
return_value={"type": 101, "mac": DEVICE_MAC},
|
|
) as mock_session:
|
|
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"] is FlowResultType.ABORT
|
|
assert result2["reason"] == "already_configured"
|
|
|
|
mock_session.assert_called_once()
|
|
|
|
|
|
async def test_wong_answer_from_device(hass: HomeAssistant) -> None:
|
|
"""Test handling of wrong answers from the device."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
)
|
|
assert result["type"] is FlowResultType.FORM
|
|
assert result["step_id"] == "user"
|
|
assert result["errors"] == {}
|
|
with patch(
|
|
"pymystrom.get_device_info",
|
|
side_effect=MyStromConnectionError(),
|
|
):
|
|
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"] is FlowResultType.FORM
|
|
assert result2["step_id"] == "user"
|
|
assert result2["errors"] == {"base": "cannot_connect"}
|
|
|
|
with patch(
|
|
"pymystrom.get_device_info",
|
|
return_value={"type": 101, "mac": DEVICE_MAC},
|
|
):
|
|
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"] is FlowResultType.CREATE_ENTRY
|
|
assert result2["title"] == "myStrom Device"
|
|
assert result2["data"] == {"host": "1.1.1.1"}
|