From 92520fe365dfd8d424f01a761c206db42afba137 Mon Sep 17 00:00:00 2001 From: David Knowles Date: Mon, 2 Dec 2024 09:18:17 -0500 Subject: [PATCH] Ensure Schlage config entry uniqueness (#131732) Co-authored-by: Joost Lekkerkerker --- .../components/schlage/config_flow.py | 1 + tests/components/schlage/test_config_flow.py | 28 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/homeassistant/components/schlage/config_flow.py b/homeassistant/components/schlage/config_flow.py index f359f7dda7185c..6e8f94473ddd13 100644 --- a/homeassistant/components/schlage/config_flow.py +++ b/homeassistant/components/schlage/config_flow.py @@ -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={ diff --git a/tests/components/schlage/test_config_flow.py b/tests/components/schlage/test_config_flow.py index 88b5f1138638f0..3161ebe40976c5 100644 --- a/tests/components/schlage/test_config_flow.py +++ b/tests/components/schlage/test_config_flow.py @@ -12,6 +12,8 @@ from . import MockSchlageConfigEntry +from tests.common import MockConfigEntry + pytestmark = pytest.mark.usefixtures("mock_setup_entry") @@ -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: