mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Update config flow test scaffolding (#30694)
* Update config flow test scaffolding * asyncpatch -> patch * Update classname
This commit is contained in:
parent
040b283a14
commit
f50714d7e9
@ -13,22 +13,49 @@ _LOGGER = logging.getLogger(__name__)
|
|||||||
DATA_SCHEMA = vol.Schema({"host": str, "username": str, "password": str})
|
DATA_SCHEMA = vol.Schema({"host": str, "username": str, "password": str})
|
||||||
|
|
||||||
|
|
||||||
|
class PlaceholderHub:
|
||||||
|
"""Placeholder class to make tests pass.
|
||||||
|
|
||||||
|
TODO Remove this placeholder class and replace with things from your PyPI package.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, host):
|
||||||
|
"""Initialize."""
|
||||||
|
self.host = host
|
||||||
|
|
||||||
|
async def authenticate(self, username, password) -> bool:
|
||||||
|
"""Test if we can authenticate with the host."""
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
async def validate_input(hass: core.HomeAssistant, data):
|
async def validate_input(hass: core.HomeAssistant, data):
|
||||||
"""Validate the user input allows us to connect.
|
"""Validate the user input allows us to connect.
|
||||||
|
|
||||||
Data has the keys from DATA_SCHEMA with values provided by the user.
|
Data has the keys from DATA_SCHEMA with values provided by the user.
|
||||||
"""
|
"""
|
||||||
# TODO validate the data can be used to set up a connection.
|
# TODO validate the data can be used to set up a connection.
|
||||||
|
|
||||||
|
# If your PyPI package is not built with async, pass your methods
|
||||||
|
# to the executor:
|
||||||
|
# await hass.async_add_executor_job(
|
||||||
|
# your_validate_func, data["username"], data["password"]
|
||||||
|
# )
|
||||||
|
|
||||||
|
hub = PlaceholderHub(data["host"])
|
||||||
|
|
||||||
|
if not await hub.authenticate(data["username"], data["password"]):
|
||||||
|
raise InvalidAuth
|
||||||
|
|
||||||
# If you cannot connect:
|
# If you cannot connect:
|
||||||
# throw CannotConnect
|
# throw CannotConnect
|
||||||
# If the authentication is wrong:
|
# If the authentication is wrong:
|
||||||
# InvalidAuth
|
# InvalidAuth
|
||||||
|
|
||||||
# Return some info we want to store in the config entry.
|
# Return info that you want to store in the config entry.
|
||||||
return {"title": "Name of the device"}
|
return {"title": "Name of the device"}
|
||||||
|
|
||||||
|
|
||||||
class DomainConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||||
"""Handle a config flow for NEW_NAME."""
|
"""Handle a config flow for NEW_NAME."""
|
||||||
|
|
||||||
VERSION = 1
|
VERSION = 1
|
||||||
|
@ -1,12 +1,10 @@
|
|||||||
"""Test the NEW_NAME config flow."""
|
"""Test the NEW_NAME config flow."""
|
||||||
from unittest.mock import patch
|
from asynctest import patch
|
||||||
|
|
||||||
from homeassistant import config_entries, setup
|
from homeassistant import config_entries, setup
|
||||||
from homeassistant.components.NEW_DOMAIN.config_flow import CannotConnect, InvalidAuth
|
from homeassistant.components.NEW_DOMAIN.config_flow import CannotConnect, InvalidAuth
|
||||||
from homeassistant.components.NEW_DOMAIN.const import DOMAIN
|
from homeassistant.components.NEW_DOMAIN.const import DOMAIN
|
||||||
|
|
||||||
from tests.common import mock_coro
|
|
||||||
|
|
||||||
|
|
||||||
async def test_form(hass):
|
async def test_form(hass):
|
||||||
"""Test we get the form."""
|
"""Test we get the form."""
|
||||||
@ -18,13 +16,12 @@ async def test_form(hass):
|
|||||||
assert result["errors"] == {}
|
assert result["errors"] == {}
|
||||||
|
|
||||||
with patch(
|
with patch(
|
||||||
"homeassistant.components.NEW_DOMAIN.config_flow.validate_input",
|
"homeassistant.components.NEW_DOMAIN.config_flow.PlaceholderHub.authenticate",
|
||||||
return_value=mock_coro({"title": "Test Title"}),
|
return_value=True,
|
||||||
), patch(
|
), patch(
|
||||||
"homeassistant.components.NEW_DOMAIN.async_setup", return_value=mock_coro(True)
|
"homeassistant.components.NEW_DOMAIN.async_setup", return_value=True
|
||||||
) as mock_setup, patch(
|
) as mock_setup, patch(
|
||||||
"homeassistant.components.NEW_DOMAIN.async_setup_entry",
|
"homeassistant.components.NEW_DOMAIN.async_setup_entry", return_value=True,
|
||||||
return_value=mock_coro(True),
|
|
||||||
) as mock_setup_entry:
|
) as mock_setup_entry:
|
||||||
result2 = await hass.config_entries.flow.async_configure(
|
result2 = await hass.config_entries.flow.async_configure(
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
@ -36,7 +33,7 @@ async def test_form(hass):
|
|||||||
)
|
)
|
||||||
|
|
||||||
assert result2["type"] == "create_entry"
|
assert result2["type"] == "create_entry"
|
||||||
assert result2["title"] == "Test Title"
|
assert result2["title"] == "Name of the device"
|
||||||
assert result2["data"] == {
|
assert result2["data"] == {
|
||||||
"host": "1.1.1.1",
|
"host": "1.1.1.1",
|
||||||
"username": "test-username",
|
"username": "test-username",
|
||||||
@ -54,7 +51,7 @@ async def test_form_invalid_auth(hass):
|
|||||||
)
|
)
|
||||||
|
|
||||||
with patch(
|
with patch(
|
||||||
"homeassistant.components.NEW_DOMAIN.config_flow.validate_input",
|
"homeassistant.components.NEW_DOMAIN.config_flow.PlaceholderHub.authenticate",
|
||||||
side_effect=InvalidAuth,
|
side_effect=InvalidAuth,
|
||||||
):
|
):
|
||||||
result2 = await hass.config_entries.flow.async_configure(
|
result2 = await hass.config_entries.flow.async_configure(
|
||||||
@ -77,7 +74,7 @@ async def test_form_cannot_connect(hass):
|
|||||||
)
|
)
|
||||||
|
|
||||||
with patch(
|
with patch(
|
||||||
"homeassistant.components.NEW_DOMAIN.config_flow.validate_input",
|
"homeassistant.components.NEW_DOMAIN.config_flow.PlaceholderHub.authenticate",
|
||||||
side_effect=CannotConnect,
|
side_effect=CannotConnect,
|
||||||
):
|
):
|
||||||
result2 = await hass.config_entries.flow.async_configure(
|
result2 = await hass.config_entries.flow.async_configure(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user