Use constants in config flow scaffold (#104964)

This commit is contained in:
Joost Lekkerkerker 2023-12-04 07:46:36 +01:00 committed by GitHub
parent fe2906f159
commit b56cd169ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 32 deletions

View File

@ -7,6 +7,7 @@ from typing import Any
import voluptuous as vol import voluptuous as vol
from homeassistant import config_entries from homeassistant import config_entries
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResult from homeassistant.data_entry_flow import FlowResult
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
@ -18,9 +19,9 @@ _LOGGER = logging.getLogger(__name__)
# TODO adjust the data schema to the data that you need # TODO adjust the data schema to the data that you need
STEP_USER_DATA_SCHEMA = vol.Schema( STEP_USER_DATA_SCHEMA = vol.Schema(
{ {
vol.Required("host"): str, vol.Required(CONF_HOST): str,
vol.Required("username"): str, vol.Required(CONF_USERNAME): str,
vol.Required("password"): str, vol.Required(CONF_PASSWORD): str,
} }
) )
@ -50,12 +51,12 @@ async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> dict[str,
# If your PyPI package is not built with async, pass your methods # If your PyPI package is not built with async, pass your methods
# to the executor: # to the executor:
# await hass.async_add_executor_job( # await hass.async_add_executor_job(
# your_validate_func, data["username"], data["password"] # your_validate_func, data[CONF_USERNAME], data[CONF_PASSWORD]
# ) # )
hub = PlaceholderHub(data["host"]) hub = PlaceholderHub(data[CONF_HOST])
if not await hub.authenticate(data["username"], data["password"]): if not await hub.authenticate(data[CONF_USERNAME], data[CONF_PASSWORD]):
raise InvalidAuth raise InvalidAuth
# If you cannot connect: # If you cannot connect:

View File

@ -1,16 +1,13 @@
"""Test the NEW_NAME config flow.""" """Test the NEW_NAME config flow."""
from unittest.mock import AsyncMock, patch from unittest.mock import AsyncMock, patch
import pytest
from homeassistant import config_entries from homeassistant import config_entries
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 homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType from homeassistant.data_entry_flow import FlowResultType
pytestmark = pytest.mark.usefixtures("mock_setup_entry")
async def test_form(hass: HomeAssistant, mock_setup_entry: AsyncMock) -> None: async def test_form(hass: HomeAssistant, mock_setup_entry: AsyncMock) -> None:
"""Test we get the form.""" """Test we get the form."""
@ -24,22 +21,22 @@ async def test_form(hass: HomeAssistant, mock_setup_entry: AsyncMock) -> None:
"homeassistant.components.NEW_DOMAIN.config_flow.PlaceholderHub.authenticate", "homeassistant.components.NEW_DOMAIN.config_flow.PlaceholderHub.authenticate",
return_value=True, return_value=True,
): ):
result2 = await hass.config_entries.flow.async_configure( result = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],
{ {
"host": "1.1.1.1", CONF_HOST: "1.1.1.1",
"username": "test-username", CONF_USERNAME: "test-username",
"password": "test-password", CONF_PASSWORD: "test-password",
}, },
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert result2["type"] == FlowResultType.CREATE_ENTRY assert result["type"] == FlowResultType.CREATE_ENTRY
assert result2["title"] == "Name of the device" assert result["title"] == "Name of the device"
assert result2["data"] == { assert result["data"] == {
"host": "1.1.1.1", CONF_HOST: "1.1.1.1",
"username": "test-username", CONF_USERNAME: "test-username",
"password": "test-password", CONF_PASSWORD: "test-password",
} }
assert len(mock_setup_entry.mock_calls) == 1 assert len(mock_setup_entry.mock_calls) == 1
@ -54,17 +51,17 @@ async def test_form_invalid_auth(hass: HomeAssistant) -> None:
"homeassistant.components.NEW_DOMAIN.config_flow.PlaceholderHub.authenticate", "homeassistant.components.NEW_DOMAIN.config_flow.PlaceholderHub.authenticate",
side_effect=InvalidAuth, side_effect=InvalidAuth,
): ):
result2 = await hass.config_entries.flow.async_configure( result = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],
{ {
"host": "1.1.1.1", CONF_HOST: "1.1.1.1",
"username": "test-username", CONF_USERNAME: "test-username",
"password": "test-password", CONF_PASSWORD: "test-password",
}, },
) )
assert result2["type"] == FlowResultType.FORM assert result["type"] == FlowResultType.FORM
assert result2["errors"] == {"base": "invalid_auth"} assert result["errors"] == {"base": "invalid_auth"}
async def test_form_cannot_connect(hass: HomeAssistant) -> None: async def test_form_cannot_connect(hass: HomeAssistant) -> None:
@ -77,14 +74,14 @@ async def test_form_cannot_connect(hass: HomeAssistant) -> None:
"homeassistant.components.NEW_DOMAIN.config_flow.PlaceholderHub.authenticate", "homeassistant.components.NEW_DOMAIN.config_flow.PlaceholderHub.authenticate",
side_effect=CannotConnect, side_effect=CannotConnect,
): ):
result2 = await hass.config_entries.flow.async_configure( result = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],
{ {
"host": "1.1.1.1", CONF_HOST: "1.1.1.1",
"username": "test-username", CONF_USERNAME: "test-username",
"password": "test-password", CONF_PASSWORD: "test-password",
}, },
) )
assert result2["type"] == FlowResultType.FORM assert result["type"] == FlowResultType.FORM
assert result2["errors"] == {"base": "cannot_connect"} assert result["errors"] == {"base": "cannot_connect"}