"""Test the Wallbox config flow."""
from http import HTTPStatus
import json

import requests_mock

from homeassistant import config_entries, data_entry_flow
from homeassistant.components.wallbox import config_flow
from homeassistant.components.wallbox.const import (
    CONF_ADDED_ENERGY_KEY,
    CONF_ADDED_RANGE_KEY,
    CONF_CHARGING_POWER_KEY,
    CONF_CHARGING_SPEED_KEY,
    CONF_DATA_KEY,
    CONF_MAX_AVAILABLE_POWER_KEY,
    CONF_MAX_CHARGING_CURRENT_KEY,
    DOMAIN,
)
from homeassistant.core import HomeAssistant

from tests.components.wallbox.const import (
    CONF_ERROR,
    CONF_JWT,
    CONF_STATUS,
    CONF_TTL,
    CONF_USER_ID,
)

test_response = json.loads(
    json.dumps(
        {
            CONF_CHARGING_POWER_KEY: 0,
            CONF_MAX_AVAILABLE_POWER_KEY: "xx",
            CONF_CHARGING_SPEED_KEY: 0,
            CONF_ADDED_RANGE_KEY: "xx",
            CONF_ADDED_ENERGY_KEY: "44.697",
            CONF_DATA_KEY: {CONF_MAX_CHARGING_CURRENT_KEY: 24},
        }
    )
)

authorisation_response = json.loads(
    json.dumps(
        {
            CONF_JWT: "fakekeyhere",
            CONF_USER_ID: 12345,
            CONF_TTL: 145656758,
            CONF_ERROR: "false",
            CONF_STATUS: 200,
        }
    )
)

authorisation_response_unauthorised = json.loads(
    json.dumps(
        {
            CONF_JWT: "fakekeyhere",
            CONF_USER_ID: 12345,
            CONF_TTL: 145656758,
            CONF_ERROR: "false",
            CONF_STATUS: 404,
        }
    )
)


async def test_show_set_form(hass: HomeAssistant) -> None:
    """Test that the setup form is served."""
    flow = config_flow.ConfigFlow()
    flow.hass = hass
    result = await flow.async_step_user(user_input=None)

    assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
    assert result["step_id"] == "user"


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

    with requests_mock.Mocker() as mock_request:
        mock_request.get(
            "https://api.wall-box.com/auth/token/user",
            json=authorisation_response,
            status_code=HTTPStatus.FORBIDDEN,
        )
        mock_request.get(
            "https://api.wall-box.com/chargers/status/12345",
            json=test_response,
            status_code=HTTPStatus.FORBIDDEN,
        )
        result2 = await hass.config_entries.flow.async_configure(
            result["flow_id"],
            {
                "station": "12345",
                "username": "test-username",
                "password": "test-password",
            },
        )

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


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

    with requests_mock.Mocker() as mock_request:
        mock_request.get(
            "https://api.wall-box.com/auth/token/user",
            json=authorisation_response_unauthorised,
            status_code=HTTPStatus.NOT_FOUND,
        )
        mock_request.get(
            "https://api.wall-box.com/chargers/status/12345",
            json=test_response,
            status_code=HTTPStatus.NOT_FOUND,
        )
        result2 = await hass.config_entries.flow.async_configure(
            result["flow_id"],
            {
                "station": "12345",
                "username": "test-username",
                "password": "test-password",
            },
        )

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


async def test_form_validate_input(hass):
    """Test we can validate input."""
    result = await hass.config_entries.flow.async_init(
        DOMAIN, context={"source": config_entries.SOURCE_USER}
    )

    with requests_mock.Mocker() as mock_request:
        mock_request.get(
            "https://api.wall-box.com/auth/token/user",
            json=authorisation_response,
            status_code=HTTPStatus.OK,
        )
        mock_request.get(
            "https://api.wall-box.com/chargers/status/12345",
            json=test_response,
            status_code=HTTPStatus.OK,
        )
        result2 = await hass.config_entries.flow.async_configure(
            result["flow_id"],
            {
                "station": "12345",
                "username": "test-username",
                "password": "test-password",
            },
        )

    assert result2["title"] == "Wallbox Portal"
    assert result2["data"]["station"] == "12345"

2fdf8fd53af4531ff38911d30c3c2cbbf8c8155a
<mask token>

<mask token>


async def test_show_set_form(hass: HomeAssistant) ->None:
    """Test that the setup form is served."""
    flow = config_flow.ConfigFlow()
    flow.hass = hass
    result = await flow.async_step_user(user_input=None)
    assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
    assert result['step_id'] == 'user'


async def test_form_cannot_authenticate(hass):
    """Test we handle cannot connect error."""
    result = await hass.config_entries.flow.async_init(DOMAIN, context={
        'source': config_entries.SOURCE_USER})
    with requests_mock.Mocker() as mock_request:
        mock_request.get('https://api.wall-box.com/auth/token/user', json=
            authorisation_response, status_code=HTTPStatus.FORBIDDEN)
        mock_request.get('https://api.wall-box.com/chargers/status/12345',
            json=test_response, status_code=HTTPStatus.FORBIDDEN)
        result2 = await hass.config_entries.flow.async_configure(result[
            'flow_id'], {'station': '12345', 'username': 'test-username',
            'password': 'test-password'})
    assert result2['type'] == 'form'
    assert result2['errors'] == {'base': 'invalid_auth'}


async def test_form_cannot_connect(hass):
    """Test we handle cannot connect error."""
    result = await hass.config_entries.flow.async_init(DOMAIN, context={
        'source': config_entries.SOURCE_USER})
    with requests_mock.Mocker() as mock_request:
        mock_request.get('https://api.wall-box.com/auth/token/user', json=
            authorisation_response_unauthorised, status_code=HTTPStatus.
            NOT_FOUND)
        mock_request.get('https://api.wall-box.com/chargers/status/12345',
            json=test_response, status_code=HTTPStatus.NOT_FOUND)
        result2 = await hass.config_entries.flow.async_configure(result[
            'flow_id'], {'station': '12345', 'username': 'test-username',
            'password': 'test-password'})
    assert result2['type'] == 'form'
    assert result2['errors'] == {'base': 'cannot_connect'}


async def test_form_validate_input(hass):
    """Test we can validate input."""
    result = await hass.config_entries.flow.async_init(DOMAIN, context={
        'source': config_entries.SOURCE_USER})
    with requests_mock.Mocker() as mock_request:
        mock_request.get('https://api.wall-box.com/auth/token/user', json=
            authorisation_response, status_code=HTTPStatus.OK)
        mock_request.get('https://api.wall-box.com/chargers/status/12345',
            json=test_response, status_code=HTTPStatus.OK)
        result2 = await hass.config_entries.flow.async_configure(result[
            'flow_id'], {'station': '12345', 'username': 'test-username',
            'password': 'test-password'})
    assert result2['title'] == 'Wallbox Portal'
    assert result2['data']['station'] == '12345'

<mask token>
test_response = json.loads(json.dumps({CONF_CHARGING_POWER_KEY: 0,
    CONF_MAX_AVAILABLE_POWER_KEY: 'xx', CONF_CHARGING_SPEED_KEY: 0,
    CONF_ADDED_RANGE_KEY: 'xx', CONF_ADDED_ENERGY_KEY: '44.697',
    CONF_DATA_KEY: {CONF_MAX_CHARGING_CURRENT_KEY: 24}}))
authorisation_response = json.loads(json.dumps({CONF_JWT: 'fakekeyhere',
    CONF_USER_ID: 12345, CONF_TTL: 145656758, CONF_ERROR: 'false',
    CONF_STATUS: 200}))
authorisation_response_unauthorised = json.loads(json.dumps({CONF_JWT:
    'fakekeyhere', CONF_USER_ID: 12345, CONF_TTL: 145656758, CONF_ERROR:
    'false', CONF_STATUS: 404}))


async def test_show_set_form(hass: HomeAssistant) ->None:
    """Test that the setup form is served."""
    flow = config_flow.ConfigFlow()
    flow.hass = hass
    result = await flow.async_step_user(user_input=None)
    assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
    assert result['step_id'] == 'user'


async def test_form_cannot_authenticate(hass):
    """Test we handle cannot connect error."""
    result = await hass.config_entries.flow.async_init(DOMAIN, context={
        'source': config_entries.SOURCE_USER})
    with requests_mock.Mocker() as mock_request:
        mock_request.get('https://api.wall-box.com/auth/token/user', json=
            authorisation_response, status_code=HTTPStatus.FORBIDDEN)
        mock_request.get('https://api.wall-box.com/chargers/status/12345',
            json=test_response, status_code=HTTPStatus.FORBIDDEN)
        result2 = await hass.config_entries.flow.async_configure(result[
            'flow_id'], {'station': '12345', 'username': 'test-username',
            'password': 'test-password'})
    assert result2['type'] == 'form'
    assert result2['errors'] == {'base': 'invalid_auth'}


async def test_form_cannot_connect(hass):
    """Test we handle cannot connect error."""
    result = await hass.config_entries.flow.async_init(DOMAIN, context={
        'source': config_entries.SOURCE_USER})
    with requests_mock.Mocker() as mock_request:
        mock_request.get('https://api.wall-box.com/auth/token/user', json=
            authorisation_response_unauthorised, status_code=HTTPStatus.
            NOT_FOUND)
        mock_request.get('https://api.wall-box.com/chargers/status/12345',
            json=test_response, status_code=HTTPStatus.NOT_FOUND)
        result2 = await hass.config_entries.flow.async_configure(result[
            'flow_id'], {'station': '12345', 'username': 'test-username',
            'password': 'test-password'})
    assert result2['type'] == 'form'
    assert result2['errors'] == {'base': 'cannot_connect'}


async def test_form_validate_input(hass):
    """Test we can validate input."""
    result = await hass.config_entries.flow.async_init(DOMAIN, context={
        'source': config_entries.SOURCE_USER})
    with requests_mock.Mocker() as mock_request:
        mock_request.get('https://api.wall-box.com/auth/token/user', json=
            authorisation_response, status_code=HTTPStatus.OK)
        mock_request.get('https://api.wall-box.com/chargers/status/12345',
            json=test_response, status_code=HTTPStatus.OK)
        result2 = await hass.config_entries.flow.async_configure(result[
            'flow_id'], {'station': '12345', 'username': 'test-username',
            'password': 'test-password'})
    assert result2['title'] == 'Wallbox Portal'
    assert result2['data']['station'] == '12345'

<mask token>
from http import HTTPStatus
import json
import requests_mock
from homeassistant import config_entries, data_entry_flow
from homeassistant.components.wallbox import config_flow
from homeassistant.components.wallbox.const import CONF_ADDED_ENERGY_KEY, CONF_ADDED_RANGE_KEY, CONF_CHARGING_POWER_KEY, CONF_CHARGING_SPEED_KEY, CONF_DATA_KEY, CONF_MAX_AVAILABLE_POWER_KEY, CONF_MAX_CHARGING_CURRENT_KEY, DOMAIN
from homeassistant.core import HomeAssistant
from tests.components.wallbox.const import CONF_ERROR, CONF_JWT, CONF_STATUS, CONF_TTL, CONF_USER_ID
test_response = json.loads(json.dumps({CONF_CHARGING_POWER_KEY: 0,
    CONF_MAX_AVAILABLE_POWER_KEY: 'xx', CONF_CHARGING_SPEED_KEY: 0,
    CONF_ADDED_RANGE_KEY: 'xx', CONF_ADDED_ENERGY_KEY: '44.697',
    CONF_DATA_KEY: {CONF_MAX_CHARGING_CURRENT_KEY: 24}}))
authorisation_response = json.loads(json.dumps({CONF_JWT: 'fakekeyhere',
    CONF_USER_ID: 12345, CONF_TTL: 145656758, CONF_ERROR: 'false',
    CONF_STATUS: 200}))
authorisation_response_unauthorised = json.loads(json.dumps({CONF_JWT:
    'fakekeyhere', CONF_USER_ID: 12345, CONF_TTL: 145656758, CONF_ERROR:
    'false', CONF_STATUS: 404}))


async def test_show_set_form(hass: HomeAssistant) ->None:
    """Test that the setup form is served."""
    flow = config_flow.ConfigFlow()
    flow.hass = hass
    result = await flow.async_step_user(user_input=None)
    assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
    assert result['step_id'] == 'user'


async def test_form_cannot_authenticate(hass):
    """Test we handle cannot connect error."""
    result = await hass.config_entries.flow.async_init(DOMAIN, context={
        'source': config_entries.SOURCE_USER})
    with requests_mock.Mocker() as mock_request:
        mock_request.get('https://api.wall-box.com/auth/token/user', json=
            authorisation_response, status_code=HTTPStatus.FORBIDDEN)
        mock_request.get('https://api.wall-box.com/chargers/status/12345',
            json=test_response, status_code=HTTPStatus.FORBIDDEN)
        result2 = await hass.config_entries.flow.async_configure(result[
            'flow_id'], {'station': '12345', 'username': 'test-username',
            'password': 'test-password'})
    assert result2['type'] == 'form'
    assert result2['errors'] == {'base': 'invalid_auth'}


async def test_form_cannot_connect(hass):
    """Test we handle cannot connect error."""
    result = await hass.config_entries.flow.async_init(DOMAIN, context={
        'source': config_entries.SOURCE_USER})
    with requests_mock.Mocker() as mock_request:
        mock_request.get('https://api.wall-box.com/auth/token/user', json=
            authorisation_response_unauthorised, status_code=HTTPStatus.
            NOT_FOUND)
        mock_request.get('https://api.wall-box.com/chargers/status/12345',
            json=test_response, status_code=HTTPStatus.NOT_FOUND)
        result2 = await hass.config_entries.flow.async_configure(result[
            'flow_id'], {'station': '12345', 'username': 'test-username',
            'password': 'test-password'})
    assert result2['type'] == 'form'
    assert result2['errors'] == {'base': 'cannot_connect'}


async def test_form_validate_input(hass):
    """Test we can validate input."""
    result = await hass.config_entries.flow.async_init(DOMAIN, context={
        'source': config_entries.SOURCE_USER})
    with requests_mock.Mocker() as mock_request:
        mock_request.get('https://api.wall-box.com/auth/token/user', json=
            authorisation_response, status_code=HTTPStatus.OK)
        mock_request.get('https://api.wall-box.com/chargers/status/12345',
            json=test_response, status_code=HTTPStatus.OK)
        result2 = await hass.config_entries.flow.async_configure(result[
            'flow_id'], {'station': '12345', 'username': 'test-username',
            'password': 'test-password'})
    assert result2['title'] == 'Wallbox Portal'
    assert result2['data']['station'] == '12345'

"""Test the Wallbox config flow."""
from http import HTTPStatus
import json

import requests_mock

from homeassistant import config_entries, data_entry_flow
from homeassistant.components.wallbox import config_flow
from homeassistant.components.wallbox.const import (
    CONF_ADDED_ENERGY_KEY,
    CONF_ADDED_RANGE_KEY,
    CONF_CHARGING_POWER_KEY,
    CONF_CHARGING_SPEED_KEY,
    CONF_DATA_KEY,
    CONF_MAX_AVAILABLE_POWER_KEY,
    CONF_MAX_CHARGING_CURRENT_KEY,
    DOMAIN,
)
from homeassistant.core import HomeAssistant

from tests.components.wallbox.const import (
    CONF_ERROR,
    CONF_JWT,
    CONF_STATUS,
    CONF_TTL,
    CONF_USER_ID,
)

test_response = json.loads(
    json.dumps(
        {
            CONF_CHARGING_POWER_KEY: 0,
            CONF_MAX_AVAILABLE_POWER_KEY: "xx",
            CONF_CHARGING_SPEED_KEY: 0,
            CONF_ADDED_RANGE_KEY: "xx",
            CONF_ADDED_ENERGY_KEY: "44.697",
            CONF_DATA_KEY: {CONF_MAX_CHARGING_CURRENT_KEY: 24},
        }
    )
)

authorisation_response = json.loads(
    json.dumps(
        {
            CONF_JWT: "fakekeyhere",
            CONF_USER_ID: 12345,
            CONF_TTL: 145656758,
            CONF_ERROR: "false",
            CONF_STATUS: 200,
        }
    )
)

authorisation_response_unauthorised = json.loads(
    json.dumps(
        {
            CONF_JWT: "fakekeyhere",
            CONF_USER_ID: 12345,
            CONF_TTL: 145656758,
            CONF_ERROR: "false",
            CONF_STATUS: 404,
        }
    )
)


async def test_show_set_form(hass: HomeAssistant) -> None:
    """Test that the setup form is served."""
    flow = config_flow.ConfigFlow()
    flow.hass = hass
    result = await flow.async_step_user(user_input=None)

    assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
    assert result["step_id"] == "user"


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

    with requests_mock.Mocker() as mock_request:
        mock_request.get(
            "https://api.wall-box.com/auth/token/user",
            json=authorisation_response,
            status_code=HTTPStatus.FORBIDDEN,
        )
        mock_request.get(
            "https://api.wall-box.com/chargers/status/12345",
            json=test_response,
            status_code=HTTPStatus.FORBIDDEN,
        )
        result2 = await hass.config_entries.flow.async_configure(
            result["flow_id"],
            {
                "station": "12345",
                "username": "test-username",
                "password": "test-password",
            },
        )

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


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

    with requests_mock.Mocker() as mock_request:
        mock_request.get(
            "https://api.wall-box.com/auth/token/user",
            json=authorisation_response_unauthorised,
            status_code=HTTPStatus.NOT_FOUND,
        )
        mock_request.get(
            "https://api.wall-box.com/chargers/status/12345",
            json=test_response,
            status_code=HTTPStatus.NOT_FOUND,
        )
        result2 = await hass.config_entries.flow.async_configure(
            result["flow_id"],
            {
                "station": "12345",
                "username": "test-username",
                "password": "test-password",
            },
        )

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


async def test_form_validate_input(hass):
    """Test we can validate input."""
    result = await hass.config_entries.flow.async_init(
        DOMAIN, context={"source": config_entries.SOURCE_USER}
    )

    with requests_mock.Mocker() as mock_request:
        mock_request.get(
            "https://api.wall-box.com/auth/token/user",
            json=authorisation_response,
            status_code=HTTPStatus.OK,
        )
        mock_request.get(
            "https://api.wall-box.com/chargers/status/12345",
            json=test_response,
            status_code=HTTPStatus.OK,
        )
        result2 = await hass.config_entries.flow.async_configure(
            result["flow_id"],
            {
                "station": "12345",
                "username": "test-username",
                "password": "test-password",
            },
        )

    assert result2["title"] == "Wallbox Portal"
    assert result2["data"]["station"] == "12345"
