hass-core/tests/components/smarthab/test_config_flow.py
Baptiste Candellier 3062312649
Add config flow + async support for SmartHab integration (#34387)
* Setup barebones SmartHab config flow

* Setup authentication flow

* Make setup async, add config flow receivers

* Add French translation

* Fix async issues

* Address review comments (thanks bdraco!)

* Fix unloading entries

* Migrate translations dir according to warning

* Create list of components

* Fix pylint false positive

* Fix bad copy-pastes 🤭

* Add async support to SmartHab component

* Address review comments (bdraco)

* Fix pylint

* Improve exception handling (bdraco)

* Apply suggestions from code review (bdraco)

Co-authored-by: J. Nick Koston <nick@koston.org>

* Don't log exceptions manually, fix error

* Reduce repeated lines in async_step_user (bdraco)

* Remove useless else (pylint)

* Remove broad exception handler

* Create strings.json + remove fr i18n

* Write tests for smarthab config flow

* Test import flow

* Fix import test

* Update homeassistant/components/smarthab/config_flow.py

Co-authored-by: J. Nick Koston <nick@koston.org>

Co-authored-by: J. Nick Koston <nick@koston.org>
2020-07-05 14:20:51 -05:00

126 lines
4.2 KiB
Python

"""Test the SmartHab config flow."""
import pysmarthab
from homeassistant import config_entries, setup
from homeassistant.components.smarthab import DOMAIN
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
from tests.async_mock import patch
async def test_form(hass):
"""Test we get the form."""
await setup.async_setup_component(hass, "persistent_notification", {})
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] == "form"
assert result["errors"] == {}
with patch("pysmarthab.SmartHab.async_login"), patch(
"pysmarthab.SmartHab.is_logged_in", return_value=True
), patch(
"homeassistant.components.smarthab.async_setup", return_value=True
) as mock_setup, patch(
"homeassistant.components.smarthab.async_setup_entry", return_value=True
) as mock_setup_entry:
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_EMAIL: "mock@example.com", CONF_PASSWORD: "test-password"},
)
assert result2["type"] == "create_entry"
assert result2["title"] == "mock@example.com"
assert result2["data"] == {
CONF_EMAIL: "mock@example.com",
CONF_PASSWORD: "test-password",
}
await hass.async_block_till_done()
assert len(mock_setup.mock_calls) == 1
assert len(mock_setup_entry.mock_calls) == 1
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("pysmarthab.SmartHab.async_login"), patch(
"pysmarthab.SmartHab.is_logged_in", return_value=False
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_EMAIL: "mock@example.com", CONF_PASSWORD: "test-password"},
)
assert result2["type"] == "form"
assert result2["errors"] == {"base": "wrong_login"}
async def test_form_service_error(hass):
"""Test we handle service errors."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(
"pysmarthab.SmartHab.async_login",
side_effect=pysmarthab.RequestFailedException(42),
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_EMAIL: "mock@example.com", CONF_PASSWORD: "test-password"},
)
assert result2["type"] == "form"
assert result2["errors"] == {"base": "service"}
async def test_form_unknown_error(hass):
"""Test we handle unknown errors."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(
"pysmarthab.SmartHab.async_login", side_effect=Exception,
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_EMAIL: "mock@example.com", CONF_PASSWORD: "test-password"},
)
assert result2["type"] == "form"
assert result2["errors"] == {"base": "unknown_error"}
async def test_import(hass):
"""Test import."""
await setup.async_setup_component(hass, "persistent_notification", {})
imported_conf = {
CONF_EMAIL: "mock@example.com",
CONF_PASSWORD: "test-password",
}
with patch("pysmarthab.SmartHab.async_login"), patch(
"pysmarthab.SmartHab.is_logged_in", return_value=True
), patch(
"homeassistant.components.smarthab.async_setup", return_value=True
) as mock_setup, patch(
"homeassistant.components.smarthab.async_setup_entry", return_value=True
) as mock_setup_entry:
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, data=imported_conf
)
assert result["type"] == "create_entry"
assert result["title"] == "mock@example.com"
assert result["data"] == {
CONF_EMAIL: "mock@example.com",
CONF_PASSWORD: "test-password",
}
await hass.async_block_till_done()
assert len(mock_setup.mock_calls) == 1
assert len(mock_setup_entry.mock_calls) == 1