Repository: home-assistant/core
File: tests/components/vesync/test_config_flow.py (Python)

Code before:
"""Test for vesync config flow."""

from unittest.mock import PropertyMock, patch

from pyvesync.utils.errors import VeSyncLoginError

from homeassistant.components.vesync import DOMAIN
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType

from tests.common import MockConfigEntry


async def test_abort_duplicate_unique_id(hass: HomeAssistant) -> None:
    """Test if we abort because component is already setup under that email."""
    mock_entry = MockConfigEntry(
        domain=DOMAIN,
        title="user@user.com",
        unique_id="account_id",
        data={CONF_USERNAME: "user@user.com", CONF_PASSWORD: "pass"},
    )
    mock_entry.add_to_hass(hass)

    result = await hass.config_entries.flow.async_init(
        DOMAIN, context={"source": "user"}
    )
    assert result["type"] is FlowResultType.FORM

    with (
        patch("pyvesync.vesync.VeSync.login"),
        patch(
            "pyvesync.vesync.VeSync.account_id", new_callable=PropertyMock
        ) as mock_account_id,
    ):
        mock_account_id.return_value = "account_id"
        result = await hass.config_entries.flow.async_configure(
            result["flow_id"],
            {CONF_USERNAME: "user@user.com", CONF_PASSWORD: "pass"},
        )

    assert result["type"] is FlowResultType.ABORT
    assert result["reason"] == "already_configured"


async def test_invalid_login_error(hass: HomeAssistant) -> None:
    """Test if we return error for invalid username and password."""
    result = await hass.config_entries.flow.async_init(
        DOMAIN, context={"source": "user"}
    )
    assert result["type"] is FlowResultType.FORM

Review comment (suggestion):
```suggestion
        DOMAIN, context={"source": SOURCE_USER}
```

Code after:
"""Test for vesync config flow."""

from unittest.mock import PropertyMock, patch

from pyvesync.utils.errors import VeSyncLoginError

from homeassistant.components.vesync import DOMAIN
from homeassistant.config_entries import SOURCE_USER
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType

from tests.common import MockConfigEntry


async def test_abort_duplicate_unique_id(hass: HomeAssistant, config_entry) -> None:
    """Test if we abort because component is already setup under that Account ID."""
    config_entry.add_to_hass(hass)

    result = await hass.config_entries.flow.async_init(
        DOMAIN, context={"source": SOURCE_USER}
    )
    assert result["type"] is FlowResultType.FORM

    with (
        patch("pyvesync.vesync.VeSync.login"),
        patch(
            "pyvesync.vesync.VeSync.account_id", new_callable=PropertyMock
        ) as mock_account_id,
    ):
        mock_account_id.return_value = "TESTACCOUNTID"
        result = await hass.config_entries.flow.async_configure(
            result["flow_id"],
            {CONF_USERNAME: "user@user.com", CONF_PASSWORD: "pass"},
        )

    assert result["type"] is FlowResultType.ABORT
    assert result["reason"] == "already_configured"


async def test_invalid_login_error(hass: HomeAssistant) -> None:
    """Test if we return error for invalid username and password."""
    result = await hass.config_entries.flow.async_init(
        DOMAIN, context={"source": "user"}
    )
    assert result["type"] is FlowResultType.FORM

    with patch(
        "pyvesync.vesync.VeSync.login",
        side_effect=VeSyncLoginError("Mock login failed"),
    ):