Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix minor issues with authentication flow #807

Merged
merged 1 commit into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 8 additions & 15 deletions custom_components/frigate/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,29 +56,25 @@ async def async_step_reconfigure(
"""Handle a flow initialized by a reconfiguration."""
return await self._handle_config_step(
user_input,
step_id="reconfigure",
default_form_input=dict(self._get_reconfigure_entry().data),
)

async def _handle_config_step(
self,
user_input: dict[str, Any] | None = None,
step_id: str = "user",
default_form_input: dict[str, Any] | None = None,
) -> config_entries.ConfigFlowResult:
"""Handle a config step."""

if user_input is None:
return self._show_config_form(step_id, user_input=default_form_input)
return self._show_config_form(user_input=default_form_input)

try:
# Cannot use cv.url validation in the schema itself, so apply extra
# validation here.
cv.url(user_input[CONF_URL])
except vol.Invalid:
return self._show_config_form(
step_id, user_input, errors={"base": "invalid_url"}
)
return self._show_config_form(user_input, errors={"base": "invalid_url"})

try:
session = async_create_clientsession(self.hass)
Expand All @@ -90,9 +86,7 @@ async def _handle_config_step(
)
await client.async_get_stats()
except FrigateApiClientError:
return self._show_config_form(
step_id, user_input, errors={"base": "cannot_connect"}
)
return self._show_config_form(user_input, errors={"base": "cannot_connect"})

# Search for duplicates with the same Frigate CONF_HOST value.
if self.source != config_entries.SOURCE_RECONFIGURE:
Expand All @@ -113,7 +107,6 @@ async def _handle_config_step(

def _show_config_form(
self,
step_id: str,
user_input: dict[str, Any] | None = None,
errors: dict[str, Any] | None = None,
) -> config_entries.ConfigFlowResult:
Expand All @@ -122,18 +115,18 @@ def _show_config_form(
user_input = {}

return self.async_show_form(
step_id=step_id,
step_id="user",
data_schema=vol.Schema(
{
vol.Required(
CONF_URL, default=user_input.get(CONF_URL, DEFAULT_HOST)
): str,
vol.Optional(
CONF_USERNAME, default=user_input.get(CONF_USERNAME)
): vol.Any(str, None),
CONF_USERNAME, default=user_input.get(CONF_USERNAME, "")
): str,
vol.Optional(
CONF_PASSWORD, default=user_input.get(CONF_PASSWORD)
): vol.Any(str, None),
CONF_PASSWORD, default=user_input.get(CONF_PASSWORD, "")
): str,
}
),
errors=errors,
Expand Down
4 changes: 2 additions & 2 deletions tests/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ async def test_user_success(hass: HomeAssistant) -> None:
assert result["title"] == "example.com"
assert result["data"] == {
CONF_URL: TEST_URL,
CONF_PASSWORD: None,
CONF_USERNAME: None,
CONF_PASSWORD: "",
CONF_USERNAME: "",
}
assert len(mock_setup_entry.mock_calls) == 1
assert mock_client.async_get_stats.called
Expand Down
Loading