"""Test the Airthings config flow."""
from unittest.mock import patch

import airthings

from homeassistant import config_entries
from homeassistant.components.airthings.const import CONF_ID, CONF_SECRET, DOMAIN
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import RESULT_TYPE_CREATE_ENTRY, RESULT_TYPE_FORM

from tests.common import MockConfigEntry

TEST_DATA = {
    CONF_ID: "client_id",
    CONF_SECRET: "secret",
}


async def test_form(hass: HomeAssistant) -> None:
    """Test we get the form."""

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

    with patch("airthings.get_token", return_value="test_token",), patch(
        "homeassistant.components.airthings.async_setup_entry",
        return_value=True,
    ) as mock_setup_entry:
        result2 = await hass.config_entries.flow.async_configure(
            result["flow_id"],
            TEST_DATA,
        )
        await hass.async_block_till_done()

    assert result2["type"] == RESULT_TYPE_CREATE_ENTRY
    assert result2["title"] == "Airthings"
    assert result2["data"] == TEST_DATA
    assert len(mock_setup_entry.mock_calls) == 1


async def test_form_invalid_auth(hass: HomeAssistant) -> None:
    """Test we handle invalid auth."""
    result = await hass.config_entries.flow.async_init(
        DOMAIN, context={"source": config_entries.SOURCE_USER}
    )

    with patch(
        "airthings.get_token",
        side_effect=airthings.AirthingsAuthError,
    ):
        result2 = await hass.config_entries.flow.async_configure(
            result["flow_id"],
            TEST_DATA,
        )

    assert result2["type"] == RESULT_TYPE_FORM
    assert result2["errors"] == {"base": "invalid_auth"}


async def test_form_cannot_connect(hass: HomeAssistant) -> None:
    """Test we handle cannot connect error."""
    result = await hass.config_entries.flow.async_init(
        DOMAIN, context={"source": config_entries.SOURCE_USER}
    )

    with patch(
        "airthings.get_token",
        side_effect=airthings.AirthingsConnectionError,
    ):
        result2 = await hass.config_entries.flow.async_configure(
            result["flow_id"],
            TEST_DATA,
        )

    assert result2["type"] == RESULT_TYPE_FORM
    assert result2["errors"] == {"base": "cannot_connect"}


async def test_form_unknown_error(hass: HomeAssistant) -> None:
    """Test we handle unknown error."""
    result = await hass.config_entries.flow.async_init(
        DOMAIN, context={"source": config_entries.SOURCE_USER}
    )

    with patch(
        "airthings.get_token",
        side_effect=Exception,
    ):
        result2 = await hass.config_entries.flow.async_configure(
            result["flow_id"],
            TEST_DATA,
        )

    assert result2["type"] == RESULT_TYPE_FORM
    assert result2["errors"] == {"base": "unknown"}


async def test_flow_entry_already_exists(hass: HomeAssistant) -> None:
    """Test user input for config_entry that already exists."""

    first_entry = MockConfigEntry(
        domain="airthings",
        data=TEST_DATA,
        unique_id=TEST_DATA[CONF_ID],
    )
    first_entry.add_to_hass(hass)

    with patch("airthings.get_token", return_value="token"):
        result = await hass.config_entries.flow.async_init(
            DOMAIN, context={"source": config_entries.SOURCE_USER}, data=TEST_DATA
        )

    assert result["type"] == "abort"
    assert result["reason"] == "already_configured"

[((3159, 3245), 'tests.common.MockConfigEntry', 'MockConfigEntry', ([], {'domain': '"""airthings"""', 'data': 'TEST_DATA', 'unique_id': 'TEST_DATA[CONF_ID]'}), "(domain='airthings', data=TEST_DATA, unique_id=TEST_DATA[\n    CONF_ID])\n", (3174, 3245), False, 'from tests.common import MockConfigEntry\n'), ((756, 811), 'unittest.mock.patch', 'patch', (['"""airthings.get_token"""'], {'return_value': '"""test_token"""'}), "('airthings.get_token', return_value='test_token')\n", (761, 811), False, 'from unittest.mock import patch\n'), ((814, 899), 'unittest.mock.patch', 'patch', (['"""homeassistant.components.airthings.async_setup_entry"""'], {'return_value': '(True)'}), "('homeassistant.components.airthings.async_setup_entry', return_value=True\n    )\n", (819, 899), False, 'from unittest.mock import patch\n'), ((1539, 1609), 'unittest.mock.patch', 'patch', (['"""airthings.get_token"""'], {'side_effect': 'airthings.AirthingsAuthError'}), "('airthings.get_token', side_effect=airthings.AirthingsAuthError)\n", (1544, 1609), False, 'from unittest.mock import patch\n'), ((2118, 2194), 'unittest.mock.patch', 'patch', (['"""airthings.get_token"""'], {'side_effect': 'airthings.AirthingsConnectionError'}), "('airthings.get_token', side_effect=airthings.AirthingsConnectionError)\n", (2123, 2194), False, 'from unittest.mock import patch\n'), ((2697, 2748), 'unittest.mock.patch', 'patch', (['"""airthings.get_token"""'], {'side_effect': 'Exception'}), "('airthings.get_token', side_effect=Exception)\n", (2702, 2748), False, 'from unittest.mock import patch\n'), ((3316, 3366), 'unittest.mock.patch', 'patch', (['"""airthings.get_token"""'], {'return_value': '"""token"""'}), "('airthings.get_token', return_value='token')\n", (3321, 3366), False, 'from unittest.mock import patch\n')]