hass-core/tests/components/mystrom/test_config_flow.py
Paul Frank c67f32b924
Add config flow to mystrom (#74719)
* rebase

* fixed review comments

* fix test

* Update tests

* increase test coverage

* implement some review comments

* Enhance device check for old FWs and add tests

* Reworked exception handling

* small code optimizations

* fix test

* Increase test coverage

* Update __init__.py

changed from hass.config_entries.async_setup_platforms(entry, PLATFORMS) to await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

* Update __init__.py

remove spaces

* Bump python-mystrom to 2.2.0

* Migrate to get_device_info from python-mystrom

* Migrate to get_device_info from python-mystrom

* Update __init__.py

* update requirements_all.txt

* update config_flow

* fix tests

* Update homeassistant/components/mystrom/__init__.py

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* Implemented review changes

* increase test coverage

* Implemented user defined title

* implemented user defined title

* Additional review comments

* fix test

* fix linter

* fix linter

* Update homeassistant/components/mystrom/models.py

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* fix review comments

* fix missing import

* simplify

* Update homeassistant/components/mystrom/__init__.py

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* Update homeassistant/components/mystrom/__init__.py

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* Update homeassistant/components/mystrom/config_flow.py

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* Update homeassistant/components/mystrom/config_flow.py

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* Update homeassistant/components/mystrom/config_flow.py

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* Update tests/components/mystrom/test_init.py

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* Update tests/components/mystrom/test_init.py

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* Update tests/components/mystrom/test_init.py

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* Update tests/components/mystrom/test_init.py

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* Update homeassistant/components/mystrom/light.py

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* Update homeassistant/components/mystrom/switch.py

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* fix review comments

* fix review comments

* Update tests/components/mystrom/test_config_flow.py

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* Update tests/components/mystrom/test_config_flow.py

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* fix review comment

* Update tests/components/mystrom/test_config_flow.py

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* Update tests/components/mystrom/test_config_flow.py

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* Update tests/components/mystrom/test_config_flow.py

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* Update tests/components/mystrom/test_config_flow.py

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* Update tests/components/mystrom/test_config_flow.py

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* Update tests/components/mystrom/test_config_flow.py

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

---------

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
2023-06-06 14:16:27 +02:00

133 lines
4.2 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.const import CONF_HOST
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"] == 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"] == 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"] == 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"] == FlowResultType.ABORT
assert result2["reason"] == "already_configured"
mock_session.assert_called_once()
async def test_step_import(hass: HomeAssistant) -> None:
"""Test the import step."""
conf = {
CONF_HOST: "1.1.1.1",
}
with patch("pymystrom.switch.MyStromSwitch.get_state"), patch(
"pymystrom.get_device_info",
return_value={"type": 101, "mac": DEVICE_MAC},
):
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, data=conf
)
await hass.async_block_till_done()
assert result["type"] == FlowResultType.CREATE_ENTRY
assert result["title"] == "myStrom Device"
assert result["data"] == {
CONF_HOST: "1.1.1.1",
}
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"] == 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"] == 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"] == FlowResultType.CREATE_ENTRY
assert result2["title"] == "myStrom Device"
assert result2["data"] == {"host": "1.1.1.1"}