Skip to content

Commit

Permalink
Ensure Schlage config entry uniqueness (#131732)
Browse files Browse the repository at this point in the history
Co-authored-by: Joost Lekkerkerker <[email protected]>
  • Loading branch information
dknowles2 and joostlek authored Dec 2, 2024
1 parent 2f644eb commit 92520fe
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions homeassistant/components/schlage/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ async def async_step_user(
return self._show_user_form(errors)

await self.async_set_unique_id(user_id)
self._abort_if_unique_id_configured()
return self.async_create_entry(
title=username,
data={
Expand Down
28 changes: 28 additions & 0 deletions tests/components/schlage/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

from . import MockSchlageConfigEntry

from tests.common import MockConfigEntry

pytestmark = pytest.mark.usefixtures("mock_setup_entry")


Expand Down Expand Up @@ -54,6 +56,32 @@ async def test_form(
assert len(mock_setup_entry.mock_calls) == 1


async def test_form_requires_unique_id(
hass: HomeAssistant,
mock_added_config_entry: MockConfigEntry,
mock_pyschlage_auth: Mock,
) -> None:
"""Test entries have unique ids."""
init_result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert init_result["type"] is FlowResultType.FORM
assert init_result["errors"] == {}

create_result = await hass.config_entries.flow.async_configure(
init_result["flow_id"],
{
"username": "test-username",
"password": "test-password",
},
)
await hass.async_block_till_done()

mock_pyschlage_auth.authenticate.assert_called_once_with()
assert create_result["type"] is FlowResultType.ABORT
assert create_result["reason"] == "already_configured"


async def test_form_invalid_auth(
hass: HomeAssistant, mock_pyschlage_auth: Mock
) -> None:
Expand Down

0 comments on commit 92520fe

Please sign in to comment.