hass-core/tests/components/wallbox/test_config_flow.py
hesselonline c497c0eadd
Add wallbox integration (#48082)
* Wallbox component added

* resolved mergeconflicts from upstream

* fixed an incorrect removal in CODEOWNERS file

* fixes for pullrequest automatic test

* clean up code after PR tests

* fixed strings.json

* fix config_flow error > wallbox

* fixed some formatting issues

* fix pylint warnings

* fixed error in number.py > set value

* pylint warnings fixed

* some more pylint fixes

* isort fixes

* fix unused_import pylint

* remove tests

* remove test requirements

* config flow test

* test errors resolved

* test file formatting

* isort on test file

* sensor test

* isort on test

* isort test const

* remove not working sensor test

* remove test const

* add switch, number and lock test

* docstrings for test classes

* sort test_number, create test_sensor

* additional tests

* fix test error

* reduced PR to 1 component

* newline in const

* ignore test coverage -> dependency on external device (wallbox)

* do not ignore config_flow

* add test for validate_input

* remove obsolete import

* additional test config flow

* change test sensor

* docstring

* add additional test for exceptions

* fix test_config

* more tests

* fix test_config_flow

* fixed http error test

* catch connectionerror and introduce testing for this error

* remove .coveragefile

* change comment

* Update homeassistant/components/wallbox/__init__.py

review suggestion by janiversen

Co-authored-by: jan iversen <jancasacondor@gmail.com>

* Update homeassistant/components/wallbox/__init__.py

review suggestion by janiversen (format only)

Co-authored-by: jan iversen <jancasacondor@gmail.com>

* Processed review comments, include more testing for sensor component

* Isolated the async_add_executor_job to make the solution more async

* add a config flow test

* Revert "add a config flow test"

This reverts commit 9c1af82fff.

* Revert "Isolated the async_add_executor_job to make the solution more async"

This reverts commit 0bf034c331.

* Make component more async and add config flow tests

* Changes based on review comments

* made _ methods in WallboxHub for the 'non-async' call to the API and try-catch. Stored the wallbox in the class.

* moved the coordinator to __init__ and pass it as part of the WallboxHub class

* removed obsolete function in __init__

* removed CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL

* fixed spelling and imports on test files

* did isort on component files

Co-authored-by: jan iversen <jancasacondor@gmail.com>
2021-05-24 13:08:24 +02:00

128 lines
3.8 KiB
Python

"""Test the Wallbox config flow."""
from unittest.mock import patch
from voluptuous.schema_builder import raises
from homeassistant import config_entries, data_entry_flow
from homeassistant.components.wallbox import CannotConnect, InvalidAuth, config_flow
from homeassistant.components.wallbox.const import DOMAIN
from homeassistant.core import HomeAssistant
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_invalid_auth(hass):
"""Test we handle invalid auth."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(
"homeassistant.components.wallbox.config_flow.WallboxHub.async_authenticate",
side_effect=InvalidAuth,
):
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 patch(
"homeassistant.components.wallbox.config_flow.WallboxHub.async_authenticate",
side_effect=CannotConnect,
):
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_validate_input(hass):
"""Test we can validate input."""
data = {
"station": "12345",
"username": "test-username",
"password": "test-password",
}
def alternate_authenticate_method():
return None
def alternate_get_charger_status_method(station):
data = '{"Temperature": 100, "Location": "Toronto", "Datetime": "2020-07-23", "Units": "Celsius"}'
return data
with patch(
"wallbox.Wallbox.authenticate",
side_effect=alternate_authenticate_method,
), patch(
"wallbox.Wallbox.getChargerStatus",
side_effect=alternate_get_charger_status_method,
):
result = await config_flow.validate_input(hass, data)
assert result == {"title": "Wallbox Portal"}
async def test_configflow_class():
"""Test configFlow class."""
configflow = config_flow.ConfigFlow()
assert configflow
with patch(
"homeassistant.components.wallbox.config_flow.validate_input",
side_effect=TypeError,
), raises(Exception):
assert await configflow.async_step_user(True)
with patch(
"homeassistant.components.wallbox.config_flow.validate_input",
side_effect=CannotConnect,
), raises(Exception):
assert await configflow.async_step_user(True)
with patch(
"homeassistant.components.wallbox.config_flow.validate_input",
), raises(Exception):
assert await configflow.async_step_user(True)
def test_cannot_connect_class():
"""Test cannot Connect class."""
cannot_connect = CannotConnect
assert cannot_connect
def test_invalid_auth_class():
"""Test invalid auth class."""
invalid_auth = InvalidAuth
assert invalid_auth