Skip to content

Commit

Permalink
Merge pull request #807 from dermotduffy/config-flow-fix
Browse files Browse the repository at this point in the history
Fix minor issues with authentication flow
  • Loading branch information
dermotduffy authored Dec 11, 2024
2 parents 9ca6f3c + 8affacb commit 708d6b5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 17 deletions.
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

0 comments on commit 708d6b5

Please sign in to comment.